Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
255 changes: 255 additions & 0 deletions tests/launcher-numpad.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
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, "../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})()"
}

const Qt = {
NoModifier: 0,
ShiftModifier: 0x02000000,
ControlModifier: 0x04000000,
AltModifier: 0x08000000,
MetaModifier: 0x10000000,
KeypadModifier: 0x20000000,
GroupSwitchModifier: 0x40000000,
Key_Escape: 0x01000000,
Key_Tab: 0x01000001,
Key_Backtab: 0x01000002,
Key_Backspace: 0x01000003,
Key_Return: 0x01000004,
Key_Enter: 0x01000005,
Key_Insert: 0x01000006,
Key_Delete: 0x01000007,
Key_Clear: 0x0100000b,
Key_Home: 0x01000010,
Key_End: 0x01000011,
Key_Left: 0x01000012,
Key_Up: 0x01000013,
Key_Right: 0x01000014,
Key_Down: 0x01000015,
Key_PageUp: 0x01000016,
Key_PageDown: 0x01000017,
Key_CapsLock: 0x01000024,
Key_NumLock: 0x01000025,
Key_ScrollLock: 0x01000026,
Key_Space: 0x20,
Key_Asterisk: 0x2a,
Key_Plus: 0x2b,
Key_Comma: 0x2c,
Key_Minus: 0x2d,
Key_Period: 0x2e,
Key_Slash: 0x2f,
Key_0: 0x30,
Key_1: 0x31,
Key_9: 0x39,
Key_Equal: 0x3d,
Key_C: 0x43,
Key_D: 0x44,
Key_F: 0x46,
Key_K: 0x4b,
Key_N: 0x4e,
Key_P: 0x50,
Key_T: 0x54,
Key_V: 0x56,
Key_BracketLeft: 0x5b
}

function harness(kind = "root") {
const actions = []
const zooms = []
const moves = []
const recalls = []
const actionPanel = { item: { filter: "", move(d) { moves.push(["panel", d]) }, activateSelected() { actions.push("panel-enter") } } }
const dropdown = { item: { filter: "", move() {}, activateSelected() {} } }
const toastPanel = { item: { filter: "", move() {}, activateSelected() {} } }
const window = {
query: "",
view: { kind },
atRoot: true,
selectedIndex: 0,
historyIndex: -1,
actionPanelOpen: false,
dropdownOpen: false,
toastPanelOpen: false,
aboutOpen: false,
alert: null,
argFocus: -1,
argDefs: [],
argValues: {},
zoom: 1,
vim: false,
setQuery(next) { this.query = next },
setZoom(z) { this.zoom = z; zooms.push(z) },
isActionPanelKey() { return false },
nextSelectable() { return 0 },
recallHistory(d) { recalls.push(d); return true },
move(d) { moves.push(d) },
performNth(n) { actions.push(["nth", n]) },
nthItem(n) { return n - 1 },
perform(action, idx) { actions.push([action, idx]) },
performIfOffered() { return false }
}
const context = vm.createContext({
Qt, window, actionPanel, dropdown, toastPanel,
Util: { editsFilter: () => false }
})
window.typedText = event => {
context.event = event
return vm.runInContext(body("function typedText(event)"), context)
}
window.isTyping = event => {
context.event = event
return vm.runInContext(body("function isTyping(event)"), context)
}
window.superOnly = event => {
context.event = event
return vm.runInContext(body("function superOnly(event)"), context)
}
window.direction = (event, ctrl, alt, inPopup) => {
context.event = event
context.ctrl = ctrl
context.alt = alt
context.inPopup = inPopup
return vm.runInContext(body("function direction(event, ctrl, alt, inPopup)"), context)
}
window.handleArgumentKey = (event, shift) => {
context.event = event
context.shift = shift
return vm.runInContext(body("function handleArgumentKey(event, shift)"), context)
}
window.handlePopupKey = (event, panel, ctrl, alt) => {
context.event = event
context.panel = panel
context.ctrl = ctrl
context.alt = alt
return vm.runInContext(body("function handlePopupKey(event, panel, ctrl, alt)"), context)
}
return {
Qt, window, actionPanel, actions, zooms, moves, recalls,
press(event) {
context.event = event
return vm.runInContext(body("function handleKey(event)"), context)
}
}
}

function numpadKey(text, extraModifiers = 0) {
return {
key: text.toUpperCase().charCodeAt(0),
text,
modifiers: Qt.KeypadModifier | extraModifiers
}
}

test("isTyping accepts numpad digits, operators, and decimal separators with KeypadModifier", () => {
const h = harness()
for (const ch of "0123456789+-*/.=,") {
assert.equal(Boolean(h.window.isTyping(numpadKey(ch))), true, `expected ${ch} to be accepted`)
assert.equal(Boolean(h.window.isTyping(numpadKey(ch, Qt.ShiftModifier))), true, `expected Shift+${ch} to be accepted`)
}
})

test("isTyping rejects Ctrl, Alt, Meta, and GroupSwitch chords on the numpad", () => {
const h = harness()
for (const mod of [Qt.ControlModifier, Qt.AltModifier, Qt.MetaModifier, Qt.GroupSwitchModifier]) {
assert.equal(Boolean(h.window.isTyping(numpadKey("5", mod))), false)
}
})

test("numpad keys without event.text map from keypad navigation keysyms and Wayland scan codes without hijacking list movement", () => {
const h = harness()
const navPad = [
[Qt.Key_Insert, "0"],
[Qt.Key_End, "1"],
[Qt.Key_Down, "2"],
[Qt.Key_PageDown, "3"],
[Qt.Key_Left, "4"],
[Qt.Key_Clear, "5"],
[Qt.Key_Right, "6"],
[Qt.Key_Home, "7"],
[Qt.Key_Up, "8"],
[Qt.Key_PageUp, "9"],
[Qt.Key_Delete, "."],
[Qt.Key_Plus, "+"],
[Qt.Key_Minus, "-"],
[Qt.Key_Asterisk, "*"],
[Qt.Key_Slash, "/"],
[Qt.Key_Equal, "="]
]
for (const [key, expected] of navPad) {
assert.equal(h.press({ key, text: "", modifiers: Qt.KeypadModifier }), true)
}
assert.equal(h.window.query, "0123456789.+-*/=")
assert.deepEqual(h.moves, [])
assert.deepEqual(h.recalls, [])

// Wayland XKB scan codes when KeypadModifier is absent; NumLock (77) must not insert 6.
const h2 = harness()
assert.equal(h2.press({ key: Qt.Key_NumLock, text: "", modifiers: 0, nativeScanCode: 77 }), false)
assert.equal(h2.press({ key: Qt.Key_Up, text: "", modifiers: 0, nativeScanCode: 80 }), true)
assert.equal(h2.press({ key: Qt.Key_Down, text: "", modifiers: 0, nativeScanCode: 88 }), true)
assert.equal(h2.window.query, "82")
assert.deepEqual(h2.moves, [])
assert.deepEqual(h2.recalls, [])

// Dedicated arrow and PageDown keys still navigate.
assert.equal(h2.press({ key: Qt.Key_Down, text: "", modifiers: 0, nativeScanCode: 116 }), true)
assert.equal(h2.press({ key: Qt.Key_PageDown, text: "", modifiers: 0, nativeScanCode: 117 }), true)
assert.deepEqual(h2.moves, [1, 8])
})

test("numpad digits and operators append to root query and alias query", () => {
for (const [kind, expr] of [["root", "123+45.6*7-8/9"], ["alias", "42"]]) {
const h = harness(kind)
for (const ch of expr) {
assert.equal(h.press(numpadKey(ch)), true)
}
assert.equal(h.window.query, expr)
assert.deepEqual(h.actions, [])
}
})

test("numpad input types into focused command argument pills", () => {
const h = harness()
h.window.argDefs = [{ name: "port" }]
h.window.argFocus = 0
for (const ch of "8080") {
assert.equal(h.press(numpadKey(ch)), true)
}
assert.equal(h.window.argValues.port, "8080")
assert.equal(h.window.query, "")
})

test("numpad input filters open popup panels without mutating root query", () => {
const h = harness()
h.window.query = "calc"
h.window.actionPanelOpen = true
for (const ch of "12") {
assert.equal(h.press(numpadKey(ch)), true)
}
assert.equal(h.actionPanel.item.filter, "12")
assert.equal(h.window.query, "calc")
})

test("numpad Enter and Ctrl+numpad shortcuts retain their actions", () => {
const h = harness()
assert.equal(h.press({ key: Qt.Key_Enter, text: "\r", modifiers: Qt.KeypadModifier }), true)
assert.equal(h.press({ key: Qt.Key_Enter, text: "\r", modifiers: Qt.KeypadModifier | Qt.ControlModifier }), true)
assert.equal(h.press({ key: Qt.Key_1, text: "1", modifiers: Qt.KeypadModifier | Qt.ControlModifier }), true)
assert.deepEqual(h.actions, [["nth", 0], ["nth", 1], ["open", 0]])

assert.equal(h.press({ key: Qt.Key_Plus, text: "+", modifiers: Qt.KeypadModifier | Qt.ControlModifier }), true)
assert.equal(h.press({ key: Qt.Key_Minus, text: "-", modifiers: Qt.KeypadModifier | Qt.ControlModifier }), true)
assert.equal(h.press({ key: Qt.Key_0, text: "0", modifiers: Qt.KeypadModifier | Qt.ControlModifier }), true)
assert.deepEqual(h.zooms, [1.1, 1, 1])
})
77 changes: 69 additions & 8 deletions ui/LauncherWindow.qml
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,8 @@ PanelWindow {
return window.isTyping(event)
}
if (Util.editsFilter(event, value)) { set(Util.editedFilter(event, value)); return true }
if (window.isTyping(event)) { set(value + event.text); return true }
var typed = window.typedText ? window.typedText(event) : (window.isTyping(event) ? event.text : "")
if (typed) { set(value + typed); return true }
return false
}

Expand Down Expand Up @@ -729,6 +730,7 @@ PanelWindow {
// -1 up, 1 down or 0, per the navigation bindings. Ctrl+P only moves up
// inside popups: in the list it opens the dropdown.
function direction(event, ctrl, alt, inPopup) {
if (window.typedText && window.typedText(event)) return 0
var key = event.key
if (!ctrl && !alt && key === Qt.Key_Down) return 1
if (!ctrl && !alt && key === Qt.Key_Up) return -1
Expand All @@ -754,7 +756,8 @@ PanelWindow {
if (dir) { panel.move(dir); return true }
if (event.key === Qt.Key_Return || event.key === Qt.Key_Enter) { panel.activateSelected(); return true }
if (Util.editsFilter(event, panel.filter)) { panel.filter = Util.editedFilter(event, panel.filter); return true }
if (window.isTyping(event)) { panel.filter += event.text; return true }
var typed = window.typedText ? window.typedText(event) : (window.isTyping(event) ? event.text : "")
if (typed) { panel.filter += typed; return true }
return true
}

Expand Down Expand Up @@ -828,11 +831,12 @@ PanelWindow {
if (window.historyIndex >= 0) { window.recallHistory(-1); return true }
window.move(1); return true
}
if (window.grid && !ctrl && !alt && (key === Qt.Key_Left || key === Qt.Key_Right)) { window.moveColumn(key === Qt.Key_Left ? -1 : 1); return true }
var pageTyped = window.typedText ? window.typedText(event) : (window.isTyping(event) ? event.text : "")
if (!pageTyped && window.grid && !ctrl && !alt && (key === Qt.Key_Left || key === Qt.Key_Right)) { window.moveColumn(key === Qt.Key_Left ? -1 : 1); return true }
if (ctrl && shift && key === Qt.Key_P) { window.performIfOffered(["pin", "unpin"]); return true }
if (ctrl && !shift && key === Qt.Key_O) { window.performIfOffered(["open-link"]); return true }
if (key === Qt.Key_PageUp) { window.move(-8); return true }
if (key === Qt.Key_PageDown) { window.move(8); return true }
if (!pageTyped && key === Qt.Key_PageUp) { window.move(-8); return true }
if (!pageTyped && key === Qt.Key_PageDown) { window.move(8); return true }
if (ctrl && shift && key === Qt.Key_F) { window.performIfOffered(["favorite-add", "favorite-remove"]); return true }
if (ctrl && shift && key === Qt.Key_C) { window.performIfOffered(["copy-command", "copy-id", "copy-question-answer", "copy-name"]); return true }
if (ctrl && shift && key === Qt.Key_D) { window.performIfOffered(["delete-all-entries", "clear-recent"]); return true }
Expand All @@ -849,13 +853,70 @@ PanelWindow {
// Super+Backspace (Omarchy's delete to line start) arrives raw under the submap: clear the query.
if (key === Qt.Key_Backspace && window.superOnly(event)) { 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 }
var typed = window.typedText ? window.typedText(event) : (window.isTyping(event) ? event.text : "")
if (typed) { window.setQuery(window.query + typed); return true }
return false
}

// Numpad keys on Qt/Wayland arrive with Qt.KeypadModifier, and when NumLock
// is off or not synced to the layer-shell surface Qt reports them as keypad
// navigation keysyms (Insert/End/Down/PageDown/Left/Clear/Right/Home/Up/PageUp/Delete)
// with empty event.text, or via XKB keypad nativeScanCode (evdev + 8).
function typedText(event) {
if (!event || event.key === Qt.Key_NumLock || event.key === Qt.Key_CapsLock || event.key === Qt.Key_ScrollLock)
return ""
var modifiers = event.modifiers & ~Qt.KeypadModifier
if (modifiers !== Qt.NoModifier && modifiers !== Qt.ShiftModifier) return ""
if (event.text && event.text.length === 1 && event.text.charCodeAt(0) >= 32 && event.text.charCodeAt(0) !== 127)
return event.text
if ((event.modifiers & Qt.KeypadModifier) && !(event.modifiers & Qt.ShiftModifier)) {
if (event.key >= Qt.Key_0 && event.key <= Qt.Key_9) return String.fromCharCode(event.key)
switch (event.key) {
case Qt.Key_Insert: return "0"
case Qt.Key_End: return "1"
case Qt.Key_Down: return "2"
case Qt.Key_PageDown: return "3"
case Qt.Key_Left: return "4"
case Qt.Key_Clear: return "5"
case Qt.Key_Right: return "6"
case Qt.Key_Home: return "7"
case Qt.Key_Up: return "8"
case Qt.Key_PageUp: return "9"
case Qt.Key_Period:
case Qt.Key_Delete: return "."
case Qt.Key_Comma: return ","
case Qt.Key_Plus: return "+"
case Qt.Key_Minus: return "-"
case Qt.Key_Asterisk: return "*"
case Qt.Key_Slash: return "/"
case Qt.Key_Equal: return "="
}
}
if (!(event.modifiers & Qt.ShiftModifier)) {
switch (event.nativeScanCode) {
case 63: return "*"
case 79: return "7"
case 80: return "8"
case 81: return "9"
case 82: return "-"
case 83: return "4"
case 84: return "5"
case 85: return "6"
case 86: return "+"
case 87: return "1"
case 88: return "2"
case 89: return "3"
case 90: return "0"
case 91: return "."
case 106: return "/"
case 125: return "="
}
}
return ""
}

function isTyping(event) {
return event.text && event.text.length === 1 && event.text.charCodeAt(0) >= 32 && event.text.charCodeAt(0) !== 127
&& (event.modifiers === Qt.NoModifier || event.modifiers === Qt.ShiftModifier)
return window.typedText(event) !== ""
}

Connections {
Expand Down