diff --git a/README.md b/README.md index 0ad5d19..392b025 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,7 @@ need live desktop validation. |---|---| | `↵` / `Ctrl+↵` / `Ctrl+Shift+↵` | Run the first, second or third action | | `Ctrl+K` | Open the action panel | +| `Super+V` / `Ctrl+V` / `Ctrl+Shift+V` / `Shift+Insert` | Paste text into the query (also accepts clipboard-history selections) | | `Ctrl+Alt+↵` | Ask the default agent using the current root-search text | | `Ctrl+1`…`9` | Pick a result | | `↑` on an empty search | Search history | diff --git a/plugin/ui/LauncherWindow.qml b/plugin/ui/LauncherWindow.qml index dfabc29..24a282e 100644 --- a/plugin/ui/LauncherWindow.qml +++ b/plugin/ui/LauncherWindow.qml @@ -433,6 +433,30 @@ PanelWindow { window.rebuild(true) } + function pasteClipboard() { + clipboardInput.clear() + clipboardInput.paste() + var text = clipboardInput.text.replace(/[\r\n\u2028\u2029]+/g, " ") + clipboardInput.clear() + if (!text) return + if (window.actionPanelOpen) actionPanel.filter += text + else if (window.argFocus >= 0) { + var name = window.argDefs[window.argFocus].name + var values = Object.assign({}, window.argValues) + values[name] = (values[name] || "") + text + window.argValues = values + } else { + if (window.view.kind === "alias") text = text.replace(/\s/g, "") + window.setQuery(window.query + text) + } + } + + TextEdit { + id: clipboardInput + visible: false + textFormat: TextEdit.PlainText + } + function recallHistory(direction) { var r = window.backend.callSync("rootSearch.getSearchHistory", null) var history = r.ok ? r.data : [] @@ -511,6 +535,12 @@ PanelWindow { if (ctrl && key === Qt.Key_Minus) { window.setZoom(window.zoom - 0.1); return true } if (ctrl && key === Qt.Key_0) { window.setZoom(1); return true } if (ctrl && key === Qt.Key_BracketLeft) { window.closePopups(); window.pop(); return true } + // Ctrl+V, Ctrl+Shift+V (the terminal habit) and Shift+Insert paste. + var meta = (event.modifiers & Qt.MetaModifier) !== 0 + if ((key === Qt.Key_V && ctrl && !alt && !meta) || (key === Qt.Key_Insert && shift && !ctrl && !alt && !meta)) { + window.pasteClipboard() + return true + } if (window.aboutOpen) { if (ctrl && shift && key === Qt.Key_K) window.aboutOpen = false diff --git a/tests/launcher-paste.test.js b/tests/launcher-paste.test.js new file mode 100644 index 0000000..bccb929 --- /dev/null +++ b/tests/launcher-paste.test.js @@ -0,0 +1,114 @@ +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 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) + return "(function() {\n" + source.slice(open, end) + "\n})()" +} + +function harness(text) { + let next = 16 + const Qt = new Proxy({ NoModifier: 0, ControlModifier: 1, ShiftModifier: 2, AltModifier: 4, MetaModifier: 8 }, { + get(target, key) { if (!(key in target)) target[key] = next++; return target[key] } + }) + const changes = [] + const window = { + query: "prefix ", actionPanelOpen: false, view: { kind: "root" }, historyIndex: 0, argFocus: -1, argDefs: [], + rebuild: reset => changes.push(reset), isTyping: () => false, + isActionPanelKey: () => false, direction: () => 0 + } + const actionPanel = { filter: "" } + const clipboardInput = { text: "", clear() { this.text = "" }, paste() { this.text = text } } + const context = vm.createContext({ Qt, window, actionPanel, clipboardInput, Util: { editsFilter: () => false } }) + window.setQuery = next => { + context.next = next + vm.runInContext(body("function setQuery(next)"), context) + } + window.pasteClipboard = () => vm.runInContext(body("function pasteClipboard()"), context) + return { + Qt, window, actionPanel, clipboardInput, changes, + press(key, modifiers) { + context.event = { key: Qt["Key_" + key], modifiers } + return vm.runInContext(body("function handleKey(event)"), context) + } + } +} + +test("both Omarchy paste shortcuts insert text once without activating a result", () => { + for (const [key, modifier] of [["V", "ControlModifier"], ["Insert", "ShiftModifier"]]) { + const h = harness("12+34") + assert.equal(h.press(key, h.Qt[modifier]), true) + assert.equal(h.window.query, "prefix 12+34") + assert.equal(h.window.historyIndex, -1) + assert.deepEqual(h.changes, [true]) + assert.equal(h.clipboardInput.text, "") + } +}) + +test("Ctrl+Shift+V pastes like Ctrl+V", () => { + const h = harness("12+34") + assert.equal(h.press("V", h.Qt.ControlModifier | h.Qt.ShiftModifier), true) + assert.equal(h.window.query, "prefix 12+34") +}) + +test("paste goes into a focused argument pill, not the query", () => { + const h = harness("oma cast") + h.window.argFocus = 0 + h.window.argDefs = [{ name: "query" }] + h.window.argValues = { query: "about " } + h.press("V", h.Qt.ControlModifier) + assert.equal(h.window.argValues.query, "about oma cast") + assert.equal(h.window.query, "prefix ") +}) + +test("paste keeps Unicode and markup literal and flattens line breaks", () => { + const h = harness("你好 $(id)\r\nsecond\nthird\u2028fourth\u2029fifth") + h.press("V", h.Qt.ControlModifier) + assert.equal(h.window.query, "prefix 你好 $(id) second third fourth fifth") +}) + +test("empty or non-text clipboard leaves the query and history unchanged", () => { + const h = harness("") + assert.equal(h.press("Insert", h.Qt.ShiftModifier), true) + assert.equal(h.window.query, "prefix ") + assert.equal(h.window.historyIndex, 0) + assert.deepEqual(h.changes, []) +}) + +test("clipboard paste also works in submenu queries and alias editing", () => { + const h = harness("some text\nwith\tspaces") + h.window.view.kind = "menu" + h.press("V", h.Qt.ControlModifier) + assert.equal(h.window.query, "prefix some text with\tspaces") + h.window.view.kind = "alias" + h.window.query = "" + h.press("Insert", h.Qt.ShiftModifier) + assert.equal(h.window.query, "sometextwithspaces") +}) + +test("action-panel paste edits its filter without changing the query", () => { + const h = harness("Copy\nResult") + h.window.actionPanelOpen = true + h.actionPanel.filter = "Find " + h.press("V", h.Qt.ControlModifier) + assert.equal(h.actionPanel.filter, "Find Copy Result") + assert.equal(h.window.query, "prefix ") + assert.deepEqual(h.changes, []) +}) + +test("unrelated V and Insert shortcuts never read the clipboard", () => { + for (const [key, modifier] of [["V", "NoModifier"], ["V", "AltModifier"], ["V", "MetaModifier"], ["Insert", "ControlModifier"]]) { + const h = harness("must not paste") + h.press(key, h.Qt[modifier]) + assert.equal(h.window.query, "prefix ") + assert.deepEqual(h.changes, []) + } +})