139 qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO <<
"with serial" << serial << m_currentSerial;
146 if (m_cursorPos != m_anchorPos && (m_pendingDeleteBeforeText != 0 || m_pendingDeleteAfterText != 0)) {
147 qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO <<
"Ignore done";
148 m_pendingDeleteBeforeText = 0;
149 m_pendingDeleteAfterText = 0;
150 m_pendingPreeditString.clear();
151 m_pendingCommitString.clear();
155 QObject *focusObject = QGuiApplication::focusObject();
160 qCWarning(qLcQpaWaylandTextInput) << Q_FUNC_INFO << serial <<
"Surface is not enabled yet";
164 if ((m_pendingPreeditString == m_currentPreeditString)
165 && (m_pendingCommitString.isEmpty() && m_pendingDeleteBeforeText == 0
166 && m_pendingDeleteAfterText == 0)) {
168 m_pendingPreeditString.clear();
172 qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO <<
"PREEDIT" << m_pendingPreeditString.text << m_pendingPreeditString.cursorBegin;
174 QList<QInputMethodEvent::Attribute> attributes;
176 if (m_pendingPreeditString.cursorBegin != -1 ||
177 m_pendingPreeditString.cursorEnd != -1) {
180 QInputMethodEvent::Attribute attribute1(QInputMethodEvent::Cursor,
181 m_pendingPreeditString.cursorBegin,
183 attributes.append(attribute1);
187 QTextCharFormat format;
188 format.setFontUnderline(
true);
189 format.setUnderlineStyle(QTextCharFormat::SingleUnderline);
190 QInputMethodEvent::Attribute attribute2(QInputMethodEvent::TextFormat,
192 m_pendingPreeditString.text.length(), format);
193 attributes.append(attribute2);
195 QInputMethodEvent event(m_pendingPreeditString.text, attributes);
197 qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO <<
"DELETE" << m_pendingDeleteBeforeText << m_pendingDeleteAfterText;
198 qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO <<
"COMMIT" << m_pendingCommitString;
201 int replaceLength = 0;
202 if (m_pendingDeleteBeforeText != 0 || m_pendingDeleteAfterText != 0) {
205 m_condReselection =
true;
206 const QByteArray &utf8 = QStringView{m_surroundingText}.toUtf8();
207 if (m_cursorPos <
int(m_pendingDeleteBeforeText)) {
208 replaceFrom = -QString::fromUtf8(QByteArrayView{utf8}.first(m_pendingDeleteBeforeText)).size();
209 replaceLength = QString::fromUtf8(QByteArrayView{utf8}.first(m_pendingDeleteBeforeText + m_pendingDeleteAfterText)).size();
211 replaceFrom = -QString::fromUtf8(QByteArrayView{utf8}.sliced(m_cursorPos - m_pendingDeleteBeforeText, m_pendingDeleteBeforeText)).size();
212 replaceLength = QString::fromUtf8(QByteArrayView{utf8}.sliced(m_cursorPos - m_pendingDeleteBeforeText, m_pendingDeleteBeforeText + m_pendingDeleteAfterText)).size();
216 qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO <<
"DELETE from " << replaceFrom <<
" length " << replaceLength;
217 event.setCommitString(m_pendingCommitString,
220 m_currentPreeditString = m_pendingPreeditString;
221 m_pendingPreeditString.clear();
222 m_pendingCommitString.clear();
223 m_pendingDeleteBeforeText = 0;
224 m_pendingDeleteAfterText = 0;
225 QCoreApplication::sendEvent(focusObject, &event);
227 if (serial == m_currentSerial)
228 updateState(supportedQueries3, update_state_full);
248 qCDebug(qLcQpaWaylandTextInput) << Q_FUNC_INFO << queries << flags;
250 if (!QGuiApplication::focusObject())
253 if (!QGuiApplication::focusWindow() || !QGuiApplication::focusWindow()->handle())
256 auto *window =
static_cast<QWaylandWindow *>(QGuiApplication::focusWindow()->handle());
257 auto *surface = window->wlSurface();
258 if (!surface || (surface != m_surface))
261 queries &= supportedQueries3;
262 bool needsCommit =
false;
264 QInputMethodQueryEvent event(queries);
265 QCoreApplication::sendEvent(QGuiApplication::focusObject(), &event);
268 if (!(queries & Qt::ImSurroundingText) && event.value(Qt::ImSurroundingText).toString().isEmpty()) {
272 if (queries & Qt::ImCursorRectangle) {
273 const QRect &cRect = event.value(Qt::ImCursorRectangle).toRect();
274 const QRect &windowRect = QGuiApplication::inputMethod()->inputItemTransform().mapRect(cRect);
275 const QRect &nativeRect = QHighDpi::toNativePixels(windowRect, QGuiApplication::focusWindow());
276 const QMargins margins = window->clientSideMargins();
277 const QRect &surfaceRect = nativeRect.translated(margins.left(), margins.top());
278 if (surfaceRect != m_cursorRect) {
279 set_cursor_rectangle(surfaceRect.x(), surfaceRect.y(), surfaceRect.width(), surfaceRect.height());
280 m_cursorRect = surfaceRect;
285 if ((queries & Qt::ImSurroundingText) || (queries & Qt::ImCursorPosition) || (queries & Qt::ImAnchorPosition)) {
286 QString text = event.value(Qt::ImSurroundingText).toString();
287 int cursor = event.value(Qt::ImCursorPosition).toInt();
288 int anchor = event.value(Qt::ImAnchorPosition).toInt();
290 qCDebug(qLcQpaWaylandTextInput) <<
"Original surrounding_text from InputMethodQuery: " << text << cursor << anchor;
295 const int MAX_MESSAGE_SIZE = 4000;
297 const QByteArray utf8 = text.toUtf8();
298 const int textSize = utf8.size();
299 if (textSize > MAX_MESSAGE_SIZE) {
300 qCDebug(qLcQpaWaylandTextInput) <<
"SurroundText size is over "
302 <<
" byte, some text will be clipped.";
303 const int selectionStart = qMin(cursor, anchor);
304 const int selectionEnd = qMax(cursor, anchor);
305 const int selectionLength = selectionEnd - selectionStart;
306 QByteArray selection = QStringView{text}.sliced(selectionStart, selectionLength).toUtf8();
307 const int selectionSize = selection.size();
310 if (selectionSize > MAX_MESSAGE_SIZE) {
311 if (anchor > cursor) {
313 anchor = MAX_MESSAGE_SIZE;
314 text = QString::fromUtf8(QByteArrayView{selection}.sliced(0, MAX_MESSAGE_SIZE));
317 cursor = MAX_MESSAGE_SIZE;
318 text = QString::fromUtf8(QByteArrayView{selection}.sliced(selectionSize - MAX_MESSAGE_SIZE, MAX_MESSAGE_SIZE));
326 int selEndSize = QStringView{text}.first(selectionEnd).toUtf8().size();
327 cursor = QWaylandInputMethodEventBuilder::indexToWayland(text, cursor);
328 anchor = QWaylandInputMethodEventBuilder::indexToWayland(text, anchor);
329 if (selEndSize < MAX_MESSAGE_SIZE) {
330 text = QString::fromUtf8(QByteArrayView{utf8}.first(MAX_MESSAGE_SIZE));
332 const int startOffset = selEndSize - MAX_MESSAGE_SIZE;
333 text = QString::fromUtf8(QByteArrayView{utf8}.sliced(startOffset, MAX_MESSAGE_SIZE));
334 cursor -= startOffset;
335 anchor -= startOffset;
339 cursor = QWaylandInputMethodEventBuilder::indexToWayland(text, cursor);
340 anchor = QWaylandInputMethodEventBuilder::indexToWayland(text, anchor);
342 qCDebug(qLcQpaWaylandTextInput) <<
"Modified surrounding_text: " << text << cursor << anchor;
344 if (m_surroundingText != text || m_cursorPos != cursor || m_anchorPos != anchor) {
345 qCDebug(qLcQpaWaylandTextInput) <<
"Current surrounding_text: " << m_surroundingText << m_cursorPos << m_anchorPos;
346 qCDebug(qLcQpaWaylandTextInput) <<
"New surrounding_text: " << text << cursor << anchor;
348 set_surrounding_text(text, cursor, anchor);
352 if (m_condReselection) {
353 qCDebug(qLcQpaWaylandTextInput) <<
"\"commit\" is disabled when Reselection by changing focus";
354 m_condReselection =
false;
359 m_surroundingText = text;
360 m_cursorPos = cursor;
361 m_anchorPos = anchor;
366 if (queries & Qt::ImHints) {
367 QWaylandInputMethodContentType contentType = QWaylandInputMethodContentType::convertV3(
static_cast<Qt::InputMethodHints>(event.value(Qt::ImHints).toInt()));
368 qCDebug(qLcQpaWaylandTextInput) << m_contentHint << contentType.hint;
369 qCDebug(qLcQpaWaylandTextInput) << m_contentPurpose << contentType.purpose;
371 if (m_contentHint != contentType.hint || m_contentPurpose != contentType.purpose) {
372 qCDebug(qLcQpaWaylandTextInput) <<
"set_content_type: " << contentType.hint << contentType.purpose;
373 set_content_type(contentType.hint, contentType.purpose);
375 m_contentHint = contentType.hint;
376 m_contentPurpose = contentType.purpose;