190 qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO <<
"with serial" << serial << m_currentSerial;
192 if (!m_enabledSurface)
197 if (m_cursorPos != m_anchorPos && (m_pendingDeleteBeforeText != 0 || m_pendingDeleteAfterText != 0)) {
198 qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO <<
"Ignore done";
199 m_pendingDeleteBeforeText = 0;
200 m_pendingDeleteAfterText = 0;
201 m_pendingPreeditString.clear();
202 m_pendingCommitString.clear();
206 QObject *focusObject = QGuiApplication::focusObject();
210 if ((m_pendingPreeditString == m_currentPreeditString)
211 && (m_pendingCommitString.isEmpty() && m_pendingDeleteBeforeText == 0
212 && m_pendingDeleteAfterText == 0)) {
214 m_pendingPreeditString.clear();
218 const int newCursorIndex = QWaylandInputMethodEventBuilder::indexFromWayland(m_pendingPreeditString.text, m_pendingPreeditString.cursorBegin);
219 qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO <<
"PREEDIT" << m_pendingPreeditString.text << newCursorIndex;
221 QList<QInputMethodEvent::Attribute> attributes;
223 if (m_pendingPreeditString.cursorBegin == -1 &&
224 m_pendingPreeditString.cursorEnd == -1) {
225 QInputMethodEvent::Attribute attribute(QInputMethodEvent::Cursor,
228 attributes.append(attribute);
229 }
else if (m_pendingPreeditString.cursorBegin != 0 ||
230 m_pendingPreeditString.cursorEnd != 0) {
233 QInputMethodEvent::Attribute attribute1(QInputMethodEvent::Cursor,
236 attributes.append(attribute1);
239 if (version() == 1) {
241 QTextCharFormat format;
242 format.setFontUnderline(
true);
243 format.setUnderlineStyle(QTextCharFormat::SingleUnderline);
244 QInputMethodEvent::Attribute attribute2(QInputMethodEvent::TextFormat,
246 m_pendingPreeditString.text.length(), format);
247 attributes.append(attribute2);
249 for (
const StyleHint &prededitStyle : std::as_const(m_pendingPreeditString.styleHints)) {
250 int begin = QWaylandInputMethodEventBuilder::indexFromWayland(m_pendingPreeditString.text, prededitStyle.begin);
251 int end = QWaylandInputMethodEventBuilder::indexFromWayland(m_pendingPreeditString.text, prededitStyle.end);
252 QTextCharFormat format;
255 switch (prededitStyle.hint) {
256 case preedit_hint_whole:
257 format.setUnderlineStyle(QTextCharFormat::SingleUnderline);
259 case preedit_hint_selection:
260 format.setForeground(QPalette().highlightedText());
261 format.setBackground(QPalette().highlight());
263 case preedit_hint_prediction:
265 format.setBackground(QPalette().placeholderText());
267 case preedit_hint_prefix:
268 format.setForeground(QColor(
"#F90F0F"));
270 case preedit_hint_suffix:
271 format.setForeground(QColor(
"#1EDC1A"));
273 case preedit_hint_spelling_error:
274 format.setUnderlineStyle(QTextCharFormat::WaveUnderline);
275 format.setUnderlineColor(QColor(
"#A40000"));
277 case preedit_hint_compose_error:
278 format.setUnderlineStyle(QTextCharFormat::WaveUnderline);
279 format.setUnderlineColor(QColor(
"#FF00FF"));
283 QInputMethodEvent::Attribute attribute2(QInputMethodEvent::TextFormat,
287 attributes.append(attribute2);
291 QInputMethodEvent event(m_pendingPreeditString.text, attributes);
293 qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO <<
"DELETE" << m_pendingDeleteBeforeText << m_pendingDeleteAfterText;
294 qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO <<
"COMMIT" << m_pendingCommitString;
297 int replaceLength = 0;
298 if (m_pendingDeleteBeforeText != 0 || m_pendingDeleteAfterText != 0) {
301 m_condReselection =
true;
302 const QByteArray &utf8 = QStringView{m_surroundingText}.toUtf8();
303 if (m_cursorPos <
int(m_pendingDeleteBeforeText)) {
304 replaceFrom = -QString::fromUtf8(QByteArrayView{utf8}.first(m_pendingDeleteBeforeText)).size();
305 replaceLength = QString::fromUtf8(QByteArrayView{utf8}.first(m_pendingDeleteBeforeText + m_pendingDeleteAfterText)).size();
307 replaceFrom = -QString::fromUtf8(QByteArrayView{utf8}.sliced(m_cursorPos - m_pendingDeleteBeforeText, m_pendingDeleteBeforeText)).size();
308 replaceLength = QString::fromUtf8(QByteArrayView{utf8}.sliced(m_cursorPos - m_pendingDeleteBeforeText, m_pendingDeleteBeforeText + m_pendingDeleteAfterText)).size();
312 qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO <<
"DELETE from " << replaceFrom <<
" length " << replaceLength;
313 event.setCommitString(m_pendingCommitString,
316 m_currentPreeditString = m_pendingPreeditString;
317 m_pendingPreeditString.clear();
318 m_pendingCommitString.clear();
319 m_pendingDeleteBeforeText = 0;
320 m_pendingDeleteAfterText = 0;
321 QCoreApplication::sendEvent(focusObject, &event);
323 if (serial == m_currentSerial)
324 updateState(supportedQueries3, update_state_full);
383 qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO << queries << flags;
385 if (!QGuiApplication::focusObject())
388 if (!QGuiApplication::focusWindow() || !QGuiApplication::focusWindow()->handle())
391 auto *window =
static_cast<QWaylandWindow *>(QGuiApplication::focusWindow()->handle());
392 auto *surface = window->wlSurface();
393 if (!surface || (surface != m_enabledSurface))
396 queries &= supportedQueries3;
397 bool needsCommit =
false;
399 QInputMethodQueryEvent event(queries);
400 QCoreApplication::sendEvent(QGuiApplication::focusObject(), &event);
403 if (!(queries & Qt::ImSurroundingText) && event.value(Qt::ImSurroundingText).toString().isEmpty()) {
407 if (queries & Qt::ImCursorRectangle) {
408 const QRect &cRect = event.value(Qt::ImCursorRectangle).toRect();
409 const QRect &windowRect = QGuiApplication::inputMethod()->inputItemTransform().mapRect(cRect);
410 const QRect &nativeRect = QHighDpi::toNativePixels(windowRect, QGuiApplication::focusWindow());
411 const QMargins margins = window->clientSideMargins();
412 const QRect &surfaceRect = nativeRect.translated(margins.left(), margins.top());
413 if (surfaceRect != m_cursorRect) {
414 set_cursor_rectangle(surfaceRect.x(), surfaceRect.y(), surfaceRect.width(), surfaceRect.height());
415 m_cursorRect = surfaceRect;
420 if ((queries & Qt::ImSurroundingText) || (queries & Qt::ImCursorPosition) || (queries & Qt::ImAnchorPosition)) {
421 QString text = event.value(Qt::ImSurroundingText).toString();
422 int cursor = event.value(Qt::ImCursorPosition).toInt();
423 int anchor = event.value(Qt::ImAnchorPosition).toInt();
425 qCDebug(qLcQpaWaylandTextInput) <<
"Original surrounding_text from InputMethodQuery: " << text << cursor << anchor;
430 const int MAX_MESSAGE_SIZE = 4000;
432 const QByteArray utf8 = text.toUtf8();
433 const int textSize = utf8.size();
434 if (textSize > MAX_MESSAGE_SIZE) {
435 qCDebug(qLcQpaWaylandTextInput) <<
"SurroundText size is over "
437 <<
" byte, some text will be clipped.";
438 const int selectionStart = qMin(cursor, anchor);
439 const int selectionEnd = qMax(cursor, anchor);
440 const int selectionLength = selectionEnd - selectionStart;
441 QByteArray selection = QStringView{text}.sliced(selectionStart, selectionLength).toUtf8();
442 const int selectionSize = selection.size();
445 if (selectionSize > MAX_MESSAGE_SIZE) {
446 if (anchor > cursor) {
448 anchor = MAX_MESSAGE_SIZE;
449 text = QString::fromUtf8(QByteArrayView{selection}.sliced(0, MAX_MESSAGE_SIZE));
452 cursor = MAX_MESSAGE_SIZE;
453 text = QString::fromUtf8(QByteArrayView{selection}.sliced(selectionSize - MAX_MESSAGE_SIZE, MAX_MESSAGE_SIZE));
461 int selEndSize = QStringView{text}.first(selectionEnd).toUtf8().size();
462 cursor = QWaylandInputMethodEventBuilder::indexToWayland(text, cursor);
463 anchor = QWaylandInputMethodEventBuilder::indexToWayland(text, anchor);
464 if (selEndSize < MAX_MESSAGE_SIZE) {
465 text = QString::fromUtf8(QByteArrayView{utf8}.first(MAX_MESSAGE_SIZE));
467 const int startOffset = selEndSize - MAX_MESSAGE_SIZE;
468 text = QString::fromUtf8(QByteArrayView{utf8}.sliced(startOffset, MAX_MESSAGE_SIZE));
469 cursor -= startOffset;
470 anchor -= startOffset;
474 cursor = QWaylandInputMethodEventBuilder::indexToWayland(text, cursor);
475 anchor = QWaylandInputMethodEventBuilder::indexToWayland(text, anchor);
477 qCDebug(qLcQpaWaylandTextInput) <<
"Modified surrounding_text: " << text << cursor << anchor;
479 if (m_surroundingText != text || m_cursorPos != cursor || m_anchorPos != anchor) {
480 qCDebug(qLcQpaWaylandTextInput) <<
"Current surrounding_text: " << m_surroundingText << m_cursorPos << m_anchorPos;
481 qCDebug(qLcQpaWaylandTextInput) <<
"New surrounding_text: " << text << cursor << anchor;
483 set_surrounding_text(text, cursor, anchor);
487 if (m_condReselection) {
488 qCDebug(qLcQpaWaylandTextInput) <<
"\"commit\" is disabled when Reselection by changing focus";
489 m_condReselection =
false;
494 m_surroundingText = text;
495 m_cursorPos = cursor;
496 m_anchorPos = anchor;
501 if (queries & Qt::ImHints) {
502 QWaylandInputMethodContentType contentType = QWaylandInputMethodContentType::convertV3(
static_cast<Qt::InputMethodHints>(event.value(Qt::ImHints).toInt()));
504 if (version() >= ZWP_TEXT_INPUT_V3_CONTENT_HINT_PREEDIT_SHOWN_SINCE_VERSION)
505 contentType.hint |= ZWP_TEXT_INPUT_V3_CONTENT_HINT_PREEDIT_SHOWN;
507 qCDebug(qLcQpaWaylandTextInput) << m_contentHint << contentType.hint;
508 qCDebug(qLcQpaWaylandTextInput) << m_contentPurpose << contentType.purpose;
510 if (m_contentHint != contentType.hint || m_contentPurpose != contentType.purpose) {
511 qCDebug(qLcQpaWaylandTextInput) <<
"set_content_type: " << contentType.hint << contentType.purpose;
512 set_content_type(contentType.hint, contentType.purpose);
514 m_contentHint = contentType.hint;
515 m_contentPurpose = contentType.purpose;