From d347eb46e7f3aa3fa588ed0430ccdf5afddd47a3 Mon Sep 17 00:00:00 2001 From: gongheng Date: Wed, 18 Nov 2026 15:25:37 +0800 Subject: [PATCH] fix: shield cursor rect during preedit to prevent IM candidate box jump MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Root cause: during inputMethodEvent, undoing the previous preedit moves the cursor to a stale position before the new preedit is inserted; the input method (e.g. Sogou) queries cursorRect in this intermediate window and gets the wrong coordinate, causing the candidate box to jump left 2. Fix: wrap the undo+reinsert block in QSignalBlocker so updateMicroFocus signals are suppressed mid-update; cache the correct cursorRect before and after the block; override inputMethodQuery to return the cached rect while m_bInputMethodUpdating is true 3. Impact: only affects preedit rendering path; no API change, inputMethodQuery falls through to QPlainTextEdit when not updating Log: fix input method candidate box jumping left during preedit in text editor Influence: 1. Test preedit input with Sogou IME, verify candidate box stays in place 2. Test normal text input without IME, verify no regression 3. Test overwrite mode with IME preedit, verify cursor behavior correct fix: 屏蔽 preedit 期间光标坐标中间态,修复输入法候选框左跳 1. 根因:inputMethodEvent 中撤销上一次 preedit 后光标短暂处于旧 位置,新 preedit 插入前输入法(如搜狗)查询 cursorRect 拿到 错误坐标,导致候选框左跳 2. 方案:用 QSignalBlocker 包裹撤销+重新插入过程,屏蔽 updateMicroFocus 信号;在屏蔽前后缓存正确光标矩形;重写 inputMethodQuery 在 m_bInputMethodUpdating 期间返回缓存坐标 3. 影响:仅影响 preedit 渲染路径,无接口变更,非更新期间 inputMethodQuery 透传 QPlainTextEdit 默认实现 Log: 修复文本编辑器输入法 preedit 期间候选框左跳问题 Influence: 1. 使用搜狗输入法测试 preedit 输入,验证候选框位置不跳动 2. 测试无输入法的正常文本输入,验证无回归 3. 测试覆盖模式下输入法 preedit,验证光标行为正确 PMS: BUG-382615 --- src/editor/dtextedit.cpp | 89 +++++++++++++++++++++++++++------------- src/editor/dtextedit.h | 6 +++ 2 files changed, 67 insertions(+), 28 deletions(-) diff --git a/src/editor/dtextedit.cpp b/src/editor/dtextedit.cpp index ed537ebb..13c26e3c 100644 --- a/src/editor/dtextedit.cpp +++ b/src/editor/dtextedit.cpp @@ -6630,43 +6630,76 @@ void TextEdit::inputMethodEvent(QInputMethodEvent *e) return; } - if (m_isPreeditBefore) { - // 每次 preedit 都是完整的字符串,所以撤销上次的 preedit - undo_(); - - // 将光标移回原位 - if (Overwrite == m_cursorMode) { - auto cursor = textCursor(); - cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::MoveAnchor, m_preeditLengthBefore); - setTextCursor(cursor); - } - } - bool isPreedit = !e->preeditString().isEmpty(); - if (isPreedit || !e->commitString().isEmpty()) { - const QString &text = isPreedit ? e->preeditString() : e->commitString(); + // 标记进入 preedit 更新过程,屏蔽中间态信号,避免输入法查询到撤销后的旧坐标 + m_bInputMethodUpdating = true; + // 进入前先缓存当前(正确的)光标矩形,供 inputMethodQuery 在屏蔽期间返回 + m_cachedCursorRect = QPlainTextEdit::cursorRect(); - //列编辑添加撤销重做 - if (m_bIsAltMod && !m_altModSelections.isEmpty()) { - insertColumnEditTextEx(text); - } else { - // 覆盖模式下输入法输入时,单独处理,模拟选中替换处理 + // 用单独作用域包裹 QSignalBlocker,确保 updateMicroFocus 在解除屏蔽后调用 + { + const QSignalBlocker blocker(this); + + if (m_isPreeditBefore) { + // 每次 preedit 都是完整的字符串,所以撤销上次的 preedit + undo_(); + + // 将光标移回原位 if (Overwrite == m_cursorMode) { - auto cursor = this->textCursor(); - // 设置光标选中后续与当前输入法输入的文本相同长度 - cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, text.size()); - // 使用此光标信息插入将输入的文本替换选中的长度 - insertSelectTextEx(cursor, text); + auto cursor = textCursor(); + cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::MoveAnchor, m_preeditLengthBefore); + setTextCursor(cursor); + } + } + bool isPreedit = !e->preeditString().isEmpty(); + if (isPreedit || !e->commitString().isEmpty()) { + const QString &text = isPreedit ? e->preeditString() : e->commitString(); + + //列编辑添加撤销重做 + if (m_bIsAltMod && !m_altModSelections.isEmpty()) { + insertColumnEditTextEx(text); } else { - insertSelectTextEx(textCursor(), text); + // 覆盖模式下输入法输入时,单独处理,模拟选中替换处理 + if (Overwrite == m_cursorMode) { + auto cursor = this->textCursor(); + // 设置光标选中后续与当前输入法输入的文本相同长度 + cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, text.size()); + // 使用此光标信息插入将输入的文本替换选中的长度 + insertSelectTextEx(cursor, text); + } else { + insertSelectTextEx(textCursor(), text); + } } + + m_isSelectAll = false; } - m_isSelectAll = false; + m_isPreeditBefore = isPreedit; + m_preeditLengthBefore = e->preeditString().length(); + + // 更新缓存为最终光标矩形,解除屏蔽后对外返回的是正确坐标 + m_cachedCursorRect = QPlainTextEdit::cursorRect(); } + // blocker 在此析构,解除信号屏蔽 + + // 关闭 query 屏蔽标志,后续输入法查询走默认实现拿到正确坐标 + m_bInputMethodUpdating = false; + // 主动触发一次 microFocus 更新,让输入法拿到最终坐标 + updateMicroFocus(); +} - m_isPreeditBefore = isPreedit; - m_preeditLengthBefore = e->preeditString().length(); +QVariant TextEdit::inputMethodQuery(Qt::InputMethodQuery query) const +{ + // 在 preedit 更新过程(undo 旧 preedit + 重新插入新 preedit)中, + // 光标会短暂处于撤销后的旧位置。此时若输入法(如搜狗候选首选项) + // 查询光标矩形,会拿到错误的旧坐标导致候选框左跳。 + // 屏蔽期间统一返回缓存的正确坐标,避免中间态坐标外泄。 + // 注意:ImMicroFocus 是 ImCursorRectangle 的废弃别名,两者值相同(0x2), + // 故只需判断 ImCursorRectangle 即可同时覆盖两者。 + if (m_bInputMethodUpdating && query == Qt::ImCursorRectangle) { + return QVariant(m_cachedCursorRect); + } + return QPlainTextEdit::inputMethodQuery(query); } void TextEdit::mousePressEvent(QMouseEvent *e) diff --git a/src/editor/dtextedit.h b/src/editor/dtextedit.h index a15c1844..2ad2a66d 100644 --- a/src/editor/dtextedit.h +++ b/src/editor/dtextedit.h @@ -578,6 +578,7 @@ public slots: void dragMoveEvent(QDragMoveEvent *event) override; void dropEvent(QDropEvent *event) override; void inputMethodEvent(QInputMethodEvent *e) override; + QVariant inputMethodQuery(Qt::InputMethodQuery query) const override; void mousePressEvent(QMouseEvent *e) override; void mouseMoveEvent(QMouseEvent *e) override; void mouseReleaseEvent(QMouseEvent *e) override; @@ -889,6 +890,11 @@ private slots: bool m_isPreeditBefore = false; // 上一个输入法时间是否是 preedit int m_preeditLengthBefore = 0; + // preedit 更新过程中(undo 旧 preedit + 重新插入新 preedit)屏蔽中间态信号, + // 避免输入法查询到撤销后的旧坐标导致候选框左跳(搜狗候选首选项场景) + bool m_bInputMethodUpdating = false; + mutable QRect m_cachedCursorRect; // 屏蔽期间对外返回的缓存光标矩形 + Qt::CaseSensitivity defaultCaseSensitive = Qt::CaseInsensitive; // 查找匹配时默认不区分 }; #endif