From bb876b55fc6b8af889378406ede80be281544e15 Mon Sep 17 00:00:00 2001 From: Amber Flina Date: Thu, 11 Sep 2025 09:47:32 -0300 Subject: [PATCH] Handle IMEs --- novelwriter/gui/doceditor.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/novelwriter/gui/doceditor.py b/novelwriter/gui/doceditor.py index 0ac4a992..7a0f1c3f 100644 --- a/novelwriter/gui/doceditor.py +++ b/novelwriter/gui/doceditor.py @@ -44,8 +44,8 @@ from PyQt6.QtCore import ( ) from PyQt6.QtGui import ( QAction, QCursor, QDragEnterEvent, QDragMoveEvent, QDropEvent, QKeyEvent, - QKeySequence, QMouseEvent, QPalette, QPixmap, QResizeEvent, QShortcut, - QTextBlock, QTextCursor, QTextDocument, QTextOption + QKeySequence, QInputMethodEvent, QMouseEvent, QPalette, QPixmap, QResizeEvent, + QShortcut, QTextBlock, QTextCursor, QTextDocument, QTextOption, ) from PyQt6.QtWidgets import ( QApplication, QFrame, QGridLayout, QHBoxLayout, QLabel, QLineEdit, QMenu, @@ -1017,6 +1017,29 @@ class GuiDocEditor(QPlainTextEdit): super().resizeEvent(event) return + def inputMethodEvent(self, event: QInputMethodEvent) -> None: + """Handle text being input from CJK input methods""" + super().inputMethodEvent(event) + if event.commitString(): + self.ensureCursorVisible() + if self._completer.isVisible(): + rect = self.cursorRect() + pos = self.mapToGlobal(rect.bottomLeft()) + self._completer.move(pos) + + def inputMethodQuery(self, query: Qt.InputMethodQuery): + """Adjust completion windows for CJK input methods to consider + the viewport margins. + """ + if query == Qt.InputMethodQuery.ImCursorRectangle: + rect = self.cursorRect() + vM = self.viewportMargins() + rect.translate(vM.left(), vM.top()) + + return rect + + return super().inputMethodQuery(query) + ## # Public Slots ##