From 2887eea3ecb7a3e8e2e7ec537dfac8cec365f6a5 Mon Sep 17 00:00:00 2001 From: Jiaxing Hu Date: Fri, 30 Oct 2026 21:02:54 +1300 Subject: [PATCH] Let search take Chinese input The search box was a Qt.Popup window, and macOS gives popup windows no input method: pinyin couldn't be typed into it, only Latin letters. It's now a panel inside the main window, with a drop shadow, so its field takes input like any other. A click elsewhere in the window, the focus moving away, or Escape (taken before the window's Cancel shortcut) closes it; the click filter is installed only while it's open. --- rkdeveloptool-gui/search.py | 50 ++++++++++++++++++++++++++++++------- tests/smoke.py | 13 ++++++++++ 2 files changed, 54 insertions(+), 9 deletions(-) diff --git a/rkdeveloptool-gui/search.py b/rkdeveloptool-gui/search.py index f493d42..b53b0a0 100644 --- a/rkdeveloptool-gui/search.py +++ b/rkdeveloptool-gui/search.py @@ -12,8 +12,8 @@ from PySide6.QtCore import QEvent, QObject, QPoint, Qt, QTimer from PySide6.QtGui import QColor, QPainter, QPalette, QPen from PySide6.QtWidgets import ( - QAbstractButton, QFrame, QGroupBox, QLineEdit, QListWidget, QListWidgetItem, QScrollArea, - QVBoxLayout, QWidget, + QAbstractButton, QApplication, QFrame, QGraphicsDropShadowEffect, QGroupBox, QLineEdit, + QListWidget, QListWidgetItem, QScrollArea, QVBoxLayout, QWidget, ) from .i18n import TRANSLATIONS @@ -200,16 +200,28 @@ def scroll(): class SearchPalette(QFrame): - """The search window: opened with Ctrl/Cmd+K or the status-bar button, + """The search box: opened with Ctrl/Cmd+K or the status-bar button, over the top of the window, with the results listed under the field. - Enter or a click opens a result; Escape or a click elsewhere closes it.""" + Enter or a click opens a result; Escape, a click elsewhere or moving + the focus away closes it. + + It's a panel inside the main window, not a popup window of its own: + macOS gives a Qt.Popup window no input method, so Chinese (pinyin) + couldn't be typed into it.""" WIDTH = 520 def __init__(self, gui): - super().__init__(gui, Qt.WindowType.Popup) + super().__init__(gui) self.gui = gui + self.hide() self.setFrameShape(QFrame.Shape.StyledPanel) + self.setAutoFillBackground(True) + shadow = QGraphicsDropShadowEffect(self) + shadow.setBlurRadius(24) + shadow.setOffset(0, 4) + shadow.setColor(QColor(0, 0, 0, 120)) + self.setGraphicsEffect(shadow) layout = QVBoxLayout(self) layout.setContentsMargins(8, 8, 8, 8) self.field = QLineEdit() @@ -223,20 +235,40 @@ def __init__(self, gui): self.field.textChanged.connect(self._update) self.field.returnPressed.connect(lambda: self._open(self.results.currentItem())) self.field.installEventFilter(self) + QApplication.instance().focusChanged.connect(self._focus_changed) self._index = None def open(self): self._index = None # the window may have changed self.field.clear() self._update("") - top_left = self.gui.mapToGlobal(QPoint((self.gui.width() - self.WIDTH) // 2, 60)) - self.move(top_left) + self.move((self.gui.width() - self.WIDTH) // 2, 60) + # Watch clicks elsewhere only while open: an application-wide + # filter sees every event. + QApplication.instance().installEventFilter(self) self.show() - self.activateWindow() + self.raise_() self.field.setFocus(Qt.FocusReason.ShortcutFocusReason) + def hideEvent(self, event): + QApplication.instance().removeEventFilter(self) + super().hideEvent(event) + + def _focus_changed(self, _old, new): + if self.isVisible() and (new is None or not self.isAncestorOf(new)): + self.hide() + def eventFilter(self, obj, event): - if obj is self.field and event.type() == QEvent.Type.KeyPress: + kind = event.type() + if kind == QEvent.Type.MouseButtonPress and self.isVisible() and isinstance(obj, QWidget) \ + and obj.window() is self.gui and obj is not self and not self.isAncestorOf(obj): + self.hide() # a click elsewhere in the window + return False + if obj is self.field and kind == QEvent.Type.ShortcutOverride \ + and event.key() == Qt.Key.Key_Escape: + event.accept() # Escape closes search, not the window's Cancel + return True + if obj is self.field and kind == QEvent.Type.KeyPress: key = event.key() if key in (Qt.Key.Key_Down, Qt.Key.Key_Up) and self.results.count(): row = self.results.currentRow() + (1 if key == Qt.Key.Key_Down else -1) diff --git a/tests/smoke.py b/tests/smoke.py index 0af6c5a..8192df3 100644 --- a/tests/smoke.py +++ b/tests/smoke.py @@ -2821,6 +2821,19 @@ def check_search(app): app.processEvents() if not palette.isVisible() or palette.field.text(): errors.append("the search button didn't open an empty search") + # Chinese is typed through the input method; a Qt.Popup window got + # none on macOS. The panel's field takes a composed string. + from PySide6.QtCore import Qt + from PySide6.QtGui import QInputMethodEvent + if not palette.field.testAttribute(Qt.WidgetAttribute.WA_InputMethodEnabled) \ + or palette.isWindow(): + errors.append("the search field can't take input-method text") + QApplication.sendEvent(palette.field, QInputMethodEvent("bei", [])) + commit = QInputMethodEvent() + commit.setCommitString("备份") + QApplication.sendEvent(palette.field, commit) + if palette.field.text() != "备份": + errors.append(f"pinyin into search gave {palette.field.text()!r}") palette.field.setText(erase) app.processEvents() if palette.results.count() == 0: