154 qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO <<
"with serial" << serial << m_currentSerial;
161 if (m_cursorPos != m_anchorPos && (m_pendingDeleteBeforeText != 0 || m_pendingDeleteAfterText != 0)) {
162 qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO <<
"Ignore done";
163 m_pendingDeleteBeforeText = 0;
164 m_pendingDeleteAfterText = 0;
165 m_pendingPreeditString.clear();
166 m_pendingCommitString.clear();
170 QObject *focusObject = QGuiApplication::focusObject();
175 qCWarning(qLcQpaWaylandTextInput) << Q_FUNC_INFO << serial <<
"Surface is not enabled yet";
179 if ((m_pendingPreeditString == m_currentPreeditString)
180 && (m_pendingCommitString.isEmpty() && m_pendingDeleteBeforeText == 0
181 && m_pendingDeleteAfterText == 0)) {
183 m_pendingPreeditString.clear();
187 qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO <<
"PREEDIT" << m_pendingPreeditString.text << m_pendingPreeditString.cursorBegin;
189 QList<QInputMethodEvent::Attribute> attributes;
191 if (m_pendingPreeditString.cursorBegin != -1 ||
192 m_pendingPreeditString.cursorEnd != -1) {
195 QInputMethodEvent::Attribute attribute1(QInputMethodEvent::Cursor,
196 m_pendingPreeditString.cursorBegin,
198 attributes.append(attribute1);
202 QTextCharFormat format;
203 format.setFontUnderline(
true);
204 format.setUnderlineStyle(QTextCharFormat::SingleUnderline);
205 QInputMethodEvent::Attribute attribute2(QInputMethodEvent::TextFormat,
207 m_pendingPreeditString.text.length(), format);
208 attributes.append(attribute2);
210 QInputMethodEvent event(m_pendingPreeditString.text, attributes);
212 qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO <<
"DELETE" << m_pendingDeleteBeforeText << m_pendingDeleteAfterText;
213 qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO <<
"COMMIT" << m_pendingCommitString;
216 int replaceLength = 0;
217 if (m_pendingDeleteBeforeText != 0 || m_pendingDeleteAfterText != 0) {
220 m_condReselection =
true;
221 const QByteArray &utf8 = QStringView{m_surroundingText}.toUtf8();
222 if (m_cursorPos <
int(m_pendingDeleteBeforeText)) {
223 replaceFrom = -QString::fromUtf8(QByteArrayView{utf8}.first(m_pendingDeleteBeforeText)).size();
224 replaceLength = QString::fromUtf8(QByteArrayView{utf8}.first(m_pendingDeleteBeforeText + m_pendingDeleteAfterText)).size();
226 replaceFrom = -QString::fromUtf8(QByteArrayView{utf8}.sliced(m_cursorPos - m_pendingDeleteBeforeText, m_pendingDeleteBeforeText)).size();
227 replaceLength = QString::fromUtf8(QByteArrayView{utf8}.sliced(m_cursorPos - m_pendingDeleteBeforeText, m_pendingDeleteBeforeText + m_pendingDeleteAfterText)).size();
231 qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO <<
"DELETE from " << replaceFrom <<
" length " << replaceLength;
232 event.setCommitString(m_pendingCommitString,
235 m_currentPreeditString = m_pendingPreeditString;
236 m_pendingPreeditString.clear();
237 m_pendingCommitString.clear();
238 m_pendingDeleteBeforeText = 0;
239 m_pendingDeleteAfterText = 0;
240 QCoreApplication::sendEvent(focusObject, &event);
242 if (serial == m_currentSerial)
243 updateState(supportedQueries3, update_state_full);
263 qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO << queries << flags;
265 if (!QGuiApplication::focusObject())
268 if (!QGuiApplication::focusWindow() || !QGuiApplication::focusWindow()->handle())
271 auto *window =
static_cast<QWaylandWindow *>(QGuiApplication::focusWindow()->handle());
272 auto *surface = window->wlSurface();
273 if (!surface || (surface != m_surface))
276 queries &= supportedQueries3;
277 bool needsCommit =
false;
279 QInputMethodQueryEvent event(queries);
280 QCoreApplication::sendEvent(QGuiApplication::focusObject(), &event);
283 if (!(queries & Qt::ImSurroundingText) && event.value(Qt::ImSurroundingText).toString().isEmpty()) {
287 if (queries & Qt::ImCursorRectangle) {
288 const QRect &cRect = event.value(Qt::ImCursorRectangle).toRect();
289 const QRect &windowRect = QGuiApplication::inputMethod()->inputItemTransform().mapRect(cRect);
290 const QRect &nativeRect = QHighDpi::toNativePixels(windowRect, QGuiApplication::focusWindow());
291 const QMargins margins = window->clientSideMargins();
292 const QRect &surfaceRect = nativeRect.translated(margins.left(), margins.top());
293 if (surfaceRect != m_cursorRect) {
294 set_cursor_rectangle(surfaceRect.x(), surfaceRect.y(), surfaceRect.width(), surfaceRect.height());
295 m_cursorRect = surfaceRect;
300 if ((queries & Qt::ImSurroundingText) || (queries & Qt::ImCursorPosition) || (queries & Qt::ImAnchorPosition)) {
301 QString text = event.value(Qt::ImSurroundingText).toString();
302 int cursor = event.value(Qt::ImCursorPosition).toInt();
303 int anchor = event.value(Qt::ImAnchorPosition).toInt();
305 qCDebug(qLcQpaWaylandTextInput) <<
"Original surrounding_text from InputMethodQuery: " << text << cursor << anchor;
310 const int MAX_MESSAGE_SIZE = 4000;
312 const QByteArray utf8 = text.toUtf8();
313 const int textSize = utf8.size();
314 if (textSize > MAX_MESSAGE_SIZE) {
315 qCDebug(qLcQpaWaylandTextInput) <<
"SurroundText size is over "
317 <<
" byte, some text will be clipped.";
318 const int selectionStart = qMin(cursor, anchor);
319 const int selectionEnd = qMax(cursor, anchor);
320 const int selectionLength = selectionEnd - selectionStart;
321 QByteArray selection = QStringView{text}.sliced(selectionStart, selectionLength).toUtf8();
322 const int selectionSize = selection.size();
325 if (selectionSize > MAX_MESSAGE_SIZE) {
326 if (anchor > cursor) {
328 anchor = MAX_MESSAGE_SIZE;
329 text = QString::fromUtf8(QByteArrayView{selection}.sliced(0, MAX_MESSAGE_SIZE));
332 cursor = MAX_MESSAGE_SIZE;
333 text = QString::fromUtf8(QByteArrayView{selection}.sliced(selectionSize - MAX_MESSAGE_SIZE, MAX_MESSAGE_SIZE));
341 int selEndSize = QStringView{text}.first(selectionEnd).toUtf8().size();
342 cursor = QWaylandInputMethodEventBuilder::indexToWayland(text, cursor);
343 anchor = QWaylandInputMethodEventBuilder::indexToWayland(text, anchor);
344 if (selEndSize < MAX_MESSAGE_SIZE) {
345 text = QString::fromUtf8(QByteArrayView{utf8}.first(MAX_MESSAGE_SIZE));
347 const int startOffset = selEndSize - MAX_MESSAGE_SIZE;
348 text = QString::fromUtf8(QByteArrayView{utf8}.sliced(startOffset, MAX_MESSAGE_SIZE));
349 cursor -= startOffset;
350 anchor -= startOffset;
354 cursor = QWaylandInputMethodEventBuilder::indexToWayland(text, cursor);
355 anchor = QWaylandInputMethodEventBuilder::indexToWayland(text, anchor);
357 qCDebug(qLcQpaWaylandTextInput) <<
"Modified surrounding_text: " << text << cursor << anchor;
359 if (m_surroundingText != text || m_cursorPos != cursor || m_anchorPos != anchor) {
360 qCDebug(qLcQpaWaylandTextInput) <<
"Current surrounding_text: " << m_surroundingText << m_cursorPos << m_anchorPos;
361 qCDebug(qLcQpaWaylandTextInput) <<
"New surrounding_text: " << text << cursor << anchor;
363 set_surrounding_text(text, cursor, anchor);
367 if (m_condReselection) {
368 qCDebug(qLcQpaWaylandTextInput) <<
"\"commit\" is disabled when Reselection by changing focus";
369 m_condReselection =
false;
374 m_surroundingText = text;
375 m_cursorPos = cursor;
376 m_anchorPos = anchor;
381 if (queries & Qt::ImHints) {
382 QWaylandInputMethodContentType contentType = QWaylandInputMethodContentType::convertV3(
static_cast<Qt::InputMethodHints>(event.value(Qt::ImHints).toInt()));
383 qCDebug(qLcQpaWaylandTextInput) << m_contentHint << contentType.hint;
384 qCDebug(qLcQpaWaylandTextInput) << m_contentPurpose << contentType.purpose;
386 if (m_contentHint != contentType.hint || m_contentPurpose != contentType.purpose) {
387 qCDebug(qLcQpaWaylandTextInput) <<
"set_content_type: " << contentType.hint << contentType.purpose;
388 set_content_type(contentType.hint, contentType.purpose);
390 m_contentHint = contentType.hint;
391 m_contentPurpose = contentType.purpose;