From d545e279ffafc17d2873890a0074c7827d13cf66 Mon Sep 17 00:00:00 2001 From: Adam Brewer Date: Thu, 22 Oct 2026 11:14:52 +0100 Subject: [PATCH] Enables query cursor editing (#25) Uses a native text input so queries can be corrected in place while keeping launcher navigation and shortcuts. Fixes typecraft-dev/oma-cast#25 --- README.md | 4 ++ plugin/ui/LauncherWindow.qml | 48 ++++++------- plugin/ui/QueryInput.qml | 39 +++++++++++ tests/launcher-input.test.js | 120 ++++++++++++++++++++++++++++++++ tests/qml/tst_queryinput.qml | 131 +++++++++++++++++++++++++++++++++++ 5 files changed, 314 insertions(+), 28 deletions(-) create mode 100644 plugin/ui/QueryInput.qml create mode 100644 tests/launcher-input.test.js create mode 100644 tests/qml/tst_queryinput.qml diff --git a/README.md b/README.md index 0ad5d19..30a261d 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,7 @@ need live desktop validation. | `Ctrl+K` | Open the action panel | | `Ctrl+Alt+↵` | Ask the default agent using the current root-search text | | `Ctrl+1`…`9` | Pick a result | +| `←` / `→`, `Home` / `End` | Move the query cursor (`Shift` to select text) | | `↑` on an empty search | Search history | | `Ctrl+↑` / `Ctrl+↓` | Jump between sections | | `Ctrl+N` / `Ctrl+P` | Next / previous | @@ -129,6 +130,9 @@ tests/smoke.sh # live checks against the running shell oma-cast dev # reload on every save ``` +Query editing tests also run headlessly when Qt 6's `qmltestrunner` is installed +at `/usr/lib/qt6/bin/qmltestrunner`. Set `QMLTESTRUNNER` to use another path. + Quickshell caches QML for the life of the process, so run `oma-cast reload` after editing anything under `plugin/`. See [`CLAUDE.md`](CLAUDE.md) for the layout and conventions. ## Notes diff --git a/plugin/ui/LauncherWindow.qml b/plugin/ui/LauncherWindow.qml index 8053637..9430435 100644 --- a/plugin/ui/LauncherWindow.qml +++ b/plugin/ui/LauncherWindow.qml @@ -78,11 +78,17 @@ PanelWindow { window.actionPanelOpen = false window.historyIndex = -1 window.rebuild(true) - Qt.callLater(function() { keyCatcher.forceActiveFocus() }) + Qt.callLater(function() { queryText.forceActiveFocus() }) + } + + onActionPanelOpenChanged: { + if (window.actionPanelOpen) keyCatcher.forceActiveFocus() + else queryText.forceActiveFocus() } onShownChanged: { if (window.shown) return + Qt.inputMethod.reset() window.actionPanelOpen = false var seconds = Number(window.general.popToRootTimeout) if (!(seconds > 0)) window.popToRoot() @@ -386,9 +392,8 @@ PanelWindow { } } - if (Util.editsFilter(event, window.query)) { window.setQuery(Util.editedFilter(event, window.query)); return true } + if (event.modifiers === Qt.ControlModifier && key === Qt.Key_U) { window.setQuery(""); return true } if (key === Qt.Key_Backspace && !window.query && !window.atRoot) { window.pop(); return true } - if (window.isTyping(event)) { window.setQuery(window.query + event.text); return true } return false } @@ -424,12 +429,11 @@ PanelWindow { borderSpec: window.borderSpec clip: true - MouseArea { anchors.fill: parent; onClicked: { window.actionPanelOpen = false; keyCatcher.forceActiveFocus() } } + MouseArea { anchors.fill: parent; onClicked: { window.actionPanelOpen = false; queryText.forceActiveFocus() } } Item { id: keyCatcher anchors.fill: parent - focus: true Keys.priority: Keys.BeforeItem Keys.onPressed: function(event) { if (window.handleKey(event)) event.accepted = true } } @@ -470,38 +474,26 @@ PanelWindow { } } - Text { + QueryInput { id: queryText - textFormat: Text.PlainText anchors.left: lead.right anchors.leftMargin: window.px(12) anchors.right: parent.right anchors.rightMargin: window.px(20) anchors.verticalCenter: parent.verticalCenter - text: window.query || window.placeholder + text: window.query + placeholderText: window.placeholder color: window.look.foreground - opacity: window.query ? 1 : 0.45 + accentColor: window.look.accent + selectionColor: window.look.accent + selectedTextColor: window.look.background + enabled: !window.actionPanelOpen font.family: window.look.fontFamily font.pixelSize: Math.round(Style.font.heading * 1.15 * window.zoom) - elide: Text.ElideLeft - } - - Rectangle { - width: Math.max(1, Style.space(2)) - height: queryText.font.pixelSize + Style.space(4) - anchors.verticalCenter: parent.verticalCenter - x: queryText.x + (window.query ? Math.min(queryText.contentWidth, queryText.width) + Style.space(1) : 0) - color: window.look.accent - visible: window.shown && !window.actionPanelOpen - - SequentialAnimation on opacity { - running: window.shown - loops: Animation.Infinite - NumberAnimation { to: 1; duration: 0 } - PauseAnimation { duration: 530 } - NumberAnimation { to: 0; duration: 0 } - PauseAnimation { duration: 530 } - } + focus: true + onTextEdited: window.setQuery(text) + Keys.priority: Keys.BeforeItem + Keys.onPressed: function(event) { event.accepted = window.handleKey(event) } } Rectangle { diff --git a/plugin/ui/QueryInput.qml b/plugin/ui/QueryInput.qml new file mode 100644 index 0000000..2658ddc --- /dev/null +++ b/plugin/ui/QueryInput.qml @@ -0,0 +1,39 @@ +pragma ComponentBehavior: Bound + +import QtQuick + +TextInput { + id: input + + property string placeholderText: "" + property color accentColor: color + + clip: true + selectByMouse: true + + Text { + anchors.fill: parent + textFormat: Text.PlainText + text: input.placeholderText + font: input.font + color: input.color + opacity: 0.45 + elide: Text.ElideLeft + visible: !input.text && !input.preeditText + } + + cursorDelegate: Rectangle { + width: 2 + color: input.accentColor + visible: input.cursorVisible + + SequentialAnimation on opacity { + running: input.cursorVisible + loops: Animation.Infinite + NumberAnimation { to: 1; duration: 0 } + PauseAnimation { duration: 530 } + NumberAnimation { to: 0; duration: 0 } + PauseAnimation { duration: 530 } + } + } +} diff --git a/tests/launcher-input.test.js b/tests/launcher-input.test.js new file mode 100644 index 0000000..3a45327 --- /dev/null +++ b/tests/launcher-input.test.js @@ -0,0 +1,120 @@ +const test = require("node:test") +const assert = require("node:assert/strict") +const fs = require("node:fs") +const path = require("node:path") +const vm = require("node:vm") +const { spawnSync } = require("node:child_process") + +const source = fs.readFileSync(path.join(__dirname, "../plugin/ui/LauncherWindow.qml"), "utf8") + +function body(signature) { + const start = source.indexOf(signature) + assert.ok(start >= 0, signature) + const open = source.indexOf("{", start) + 1 + const end = source.indexOf("\n }", open) + assert.ok(end > open, signature) + return "(function() {\n" + source.slice(open, end) + "\n})()" +} + +function harness() { + let next = 100 + const Qt = new Proxy({ NoModifier: 0, ControlModifier: 1, ShiftModifier: 2, AltModifier: 4 }, { + get(target, key) { if (!(key in target)) target[key] = next++; return target[key] } + }) + const events = [] + const window = { + query: "12+34", atRoot: true, view: { kind: "root" }, actionPanelOpen: false, + historyIndex: -1, selectedIndex: 0, + setQuery(query) { this.query = query }, + pop: () => events.push("pop"), move: n => events.push(["move", n]), + nextSelectable: () => 0, rebuild: reset => events.push(["rebuild", reset]), + toggleActionPanel: () => events.push("actions"), + backend: { callSync: () => ({ ok: true, data: ["firefox", "12+34"] }) } + } + const context = vm.createContext({ Qt, window, event: null }) + window.recallHistory = direction => { + context.direction = direction + return vm.runInContext(body("function recallHistory(direction)"), context) + } + return { + Qt, window, events, + press(key, modifiers = Qt.NoModifier, text = "") { + context.event = { key: Qt["Key_" + key], modifiers, text } + return vm.runInContext(body("function handleKey(event)"), context) + } + } +} + +test("query editing is delegated to TextInput without changing the query", () => { + const h = harness() + for (const key of ["Left", "Right", "Home", "End", "Backspace", "Delete"]) { + assert.equal(h.press(key), false, key) + } + assert.equal(h.press("5", 0, "5"), false) + assert.equal(h.press("Backspace", h.Qt.ControlModifier), false) + assert.equal(h.press("Left", h.Qt.ShiftModifier), false) + assert.equal(h.window.query, "12+34") +}) + +test("Ctrl+U clears the query and empty Backspace still goes back", () => { + const h = harness() + assert.equal(h.press("U", h.Qt.ControlModifier), true) + assert.equal(h.window.query, "") + assert.equal(h.press("U", h.Qt.ControlModifier | h.Qt.ShiftModifier), false) + assert.equal(h.press("Backspace"), false) + h.window.atRoot = false + assert.equal(h.press("Backspace"), true) + assert.deepEqual(h.events, ["pop"]) +}) + +test("list navigation and action shortcuts retain priority over text editing", () => { + const h = harness() + assert.equal(h.press("Up"), true) + assert.equal(h.press("Down"), true) + assert.equal(h.press("K", h.Qt.ControlModifier), true) + assert.deepEqual(h.events, [["move", -1], ["move", 1], "actions"]) + assert.equal(h.window.query, "12+34") +}) + +test("history recall survives cursor movement and editing exits history browsing", () => { + const h = harness() + h.window.query = "" + assert.equal(h.press("Up"), true) + assert.equal(h.window.query, "firefox") + assert.equal(h.window.historyIndex, 0) + assert.equal(h.press("Left"), false) + assert.equal(h.window.historyIndex, 0) + assert.equal(h.press("Up"), true) + assert.equal(h.window.query, "12+34") + assert.equal(h.window.historyIndex, 1) + assert.equal(h.press("Down"), true) + assert.equal(h.window.query, "firefox") + h.window.query = "12+34" + const context = vm.createContext({ window: h.window, next: "12+534" }) + vm.runInContext(body("function setQuery(next)"), context) + assert.equal(h.window.query, "12+534") + assert.equal(h.window.historyIndex, -1) +}) + +test("aliases keep native editing while rejecting spaces", () => { + const h = harness() + h.window.view.kind = "alias" + assert.equal(h.press("Left"), false) + assert.equal(h.press("5", 0, "5"), false) + assert.equal(h.press("Space", 0, " "), true) +}) + +test("native query input regression tests", t => { + const runner = process.env.QMLTESTRUNNER || "/usr/lib/qt6/bin/qmltestrunner" + const result = spawnSync(runner, ["-input", path.join(__dirname, "qml")], { + encoding: "utf8", timeout: 30000, + env: { ...process.env, QT_QPA_PLATFORM: "offscreen", QT_QPA_PLATFORMTHEME: "", QT_IM_MODULE: "", QSG_RHI_BACKEND: "software", DBUS_SESSION_BUS_ADDRESS: "" } + }) + if (result.error && result.error.code === "ENOENT") { + t.skip("Qt 6 qmltestrunner is not installed; set QMLTESTRUNNER for a custom path") + return + } + assert.ifError(result.error) + assert.ok(result.status === 0, result.stdout + result.stderr) + assert.match(result.stdout, /QueryInput::test_issue25_insertAtCursor\(\)/) +}) diff --git a/tests/qml/tst_queryinput.qml b/tests/qml/tst_queryinput.qml new file mode 100644 index 0000000..1c8bd6f --- /dev/null +++ b/tests/qml/tst_queryinput.qml @@ -0,0 +1,131 @@ +import QtQuick +import QtTest +import "../../plugin/ui" + +Item { + width: 500 + height: 100 + + QueryInput { + id: input + width: 200 + height: 40 + font.pixelSize: 20 + text: tests.query + placeholderText: "Search…" + onTextEdited: tests.query = text + } + + TestCase { + id: tests + name: "QueryInput" + when: windowShown + property string query: "" + + function init() { + query = "" + input.forceActiveFocus() + verify(input.activeFocus) + } + + function typeText(text) { + for (var i = 0; i < text.length; i++) keyClick(text[i]) + } + + function test_issue25_insertAtCursor() { + typeText("12+34") + keyClick(Qt.Key_Left) + keyClick(Qt.Key_Left) + compare(input.cursorPosition, 3) + typeText("5") + compare(query, "12+534") + compare(input.cursorPosition, 4) + keyClick(Qt.Key_Right) + typeText("6") + compare(query, "12+5364") + } + + function test_deleteAtCursor() { + query = "12+34" + keyClick(Qt.Key_Left) + keyClick(Qt.Key_Backspace) + compare(query, "12+4") + keyClick(Qt.Key_Delete) + compare(query, "12+") + } + + function test_selectionReplacement() { + query = "12+34" + keyClick(Qt.Key_Left, Qt.ShiftModifier) + keyClick(Qt.Key_Left, Qt.ShiftModifier) + compare(input.selectedText, "34") + typeText("5") + compare(query, "12+5") + keyClick(Qt.Key_A, Qt.ControlModifier) + keyClick(Qt.Key_Backspace) + compare(query, "") + } + + function test_homeEndAndBoundaries() { + query = "12+34" + keyClick(Qt.Key_Home) + keyClick(Qt.Key_Left) + compare(input.cursorPosition, 0) + typeText("5") + compare(query, "512+34") + keyClick(Qt.Key_End) + keyClick(Qt.Key_Right) + compare(input.cursorPosition, query.length) + } + + function test_wordDeletion() { + query = "first second third" + keyClick(Qt.Key_Left, Qt.ControlModifier) + compare(input.cursorPosition, 13) + keyClick(Qt.Key_Backspace, Qt.ControlModifier) + compare(query, "first third") + } + + function test_externalQueryReplacement() { + query = "old query" + keyClick(Qt.Key_Home) + query = "12+34" + compare(input.cursorPosition, query.length) + keyClick(Qt.Key_Left) + typeText("5") + compare(query, "12+354") + query = "" + compare(input.text, "") + compare(input.cursorPosition, 0) + } + + function test_focusPreservesCursor() { + query = "12+34" + keyClick(Qt.Key_Left) + keyClick(Qt.Key_Left) + input.focus = false + input.forceActiveFocus() + compare(input.cursorPosition, 3) + typeText("5") + compare(query, "12+534") + } + + function test_unicodeDeletion() { + query = "a😀b" + keyClick(Qt.Key_Left) + keyClick(Qt.Key_Backspace) + compare(query, "ab") + compare(input.cursorPosition, 1) + } + + function test_longQueryScrollsWithCursor() { + query = "1234567890".repeat(10) + verify(input.contentWidth > input.width) + verify(input.cursorRectangle.x >= 0 && input.cursorRectangle.x <= input.width) + keyClick(Qt.Key_Home) + verify(input.cursorRectangle.x >= 0 && input.cursorRectangle.x <= input.width) + typeText("5") + compare(query, "5" + "1234567890".repeat(10)) + } + } +}