Skip to content
24 changes: 21 additions & 3 deletions shell/plugins/menu/Menu.qml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ Item {
property bool altHeld: false
property int requestSerial: 0
property int applySerial: 0
// Completion writes share one Process, so they queue: a supersede can land
// while the previous write is still spawning, and reassigning a running
// Process would silently drop its command.
property var pendingDoneWrites: []

function runNextDoneWrite() {
if (resultProc.running || root.pendingDoneWrites.length === 0) return
resultProc.command = root.pendingDoneWrites.shift()
resultProc.running = true
}
property var items: ({})
property var itemOrder: []
property var navStack: []
Expand Down Expand Up @@ -169,11 +179,11 @@ Item {
root.doneFile = ""

if (selection === null || selection === undefined) {
resultProc.command = ["bash", "-c", ": > " + Util.shellQuote(activeDoneFile)]
root.pendingDoneWrites.push(["bash", "-c", ": > " + Util.shellQuote(activeDoneFile)])
} else {
resultProc.command = ["bash", "-c", "printf '%s\\n' " + Util.shellQuote(selection) + " > " + Util.shellQuote(activeSelectionFile) + "; : > " + Util.shellQuote(activeDoneFile)]
root.pendingDoneWrites.push(["bash", "-c", "printf '%s\\n' " + Util.shellQuote(selection) + " > " + Util.shellQuote(activeSelectionFile) + "; : > " + Util.shellQuote(activeDoneFile)])
}
resultProc.running = true
root.runNextDoneWrite()
}

function runAction(action) {
Expand Down Expand Up @@ -1019,6 +1029,9 @@ Item {
}

function openExistingMenu(initialMenu) {
// Taking the surface over must not drop a live select/input request:
// its waiter polls the done file forever once these handles are cleared.
if (root.requestActive) root.finishRequest(null)
altHeld = false
requestSerial += 1
mode = "menu"
Expand All @@ -1044,6 +1057,9 @@ Item {
}

function openDmenu(payload) {
// Same contract as openExistingMenu: a request displaced by a newer one
// still gets its done file written, or the waiting script never exits.
if (root.requestActive) root.finishRequest(null)
requestSerial += 1
mode = payload.mode === "input" ? "input" : "select"
dmenuPrompt = String(payload.prompt || (mode === "input" ? Commons.I18n.t("menu.inputPrompt") : Commons.I18n.t("menu.selectPrompt")))
Expand Down Expand Up @@ -1090,6 +1106,7 @@ Item {
}

function openKeybindings() {
if (root.requestActive) root.finishRequest(null)
altHeld = false
requestSerial += 1
mode = "keybindings"
Expand Down Expand Up @@ -1251,6 +1268,7 @@ Item {
Process {
id: resultProc
onExited: {
root.runNextDoneWrite()
if (root.applySerial === root.requestSerial)
root.opened = false
}
Expand Down
129 changes: 129 additions & 0 deletions test/shell.d/fixtures/menu-select-supersede/shell.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import QtQuick
import Quickshell

// Displacing a real Menu.qml request must write its completion file so the
// waiting command exits, including when keybindings take over the surface.
ShellRoot {
id: root

readonly property string resultPath: Quickshell.env("OMARCHY_QML_TEST_RESULT")
readonly property string scratchDir: Quickshell.env("OMARCHY_MENU_TMP")
property var menu: null
property var failures: []

function fail(message) {
failures.push(String(message))
}

function assertTrue(condition, message) {
if (!condition) fail(message)
}

function shellQuote(value) {
return "'" + String(value).replace(/'/g, "'\\''") + "'"
}

function writeResult() {
var payload = JSON.stringify({
ok: failures.length === 0,
failures: failures
})

if (resultPath) {
Quickshell.execDetached(["bash", "-lc", "printf '%s' " + shellQuote(payload) + " > " + shellQuote(resultPath)])
}
}

function selectPayload(prompt, name) {
return JSON.stringify({
mode: "select",
prompt: prompt,
options: ["alpha", "bravo"],
selectionFile: scratchDir + "/sel-" + name,
doneFile: scratchDir + "/done-" + name
})
}

Timer {
id: driver
interval: 100
repeat: true
running: true
property int tick: 0

onTriggered: {
try {
tick += 1

if (tick === 1) {
var component = Qt.createComponent("file://" + Quickshell.env("OMARCHY_PATH") + "/shell/plugins/menu/Menu.qml", Component.PreferSynchronous)
if (component.status !== Component.Ready) {
root.fail("Menu.qml failed to load: " + component.errorString())
root.finish()
return
}
root.menu = component.createObject(null)
if (!root.menu) {
root.fail("Menu.qml failed to instantiate")
root.finish()
return
}
}

// Scenario A: a live select request is superseded by the regular menu.
if (tick === 3) {
root.menu.open(root.selectPayload("A", "a"))
root.assertTrue(root.menu.requestActive === true, "select A becomes the active request")
root.assertTrue(root.menu.dmenuActive === true, "select A puts the menu into dmenu mode")
root.menu.openExistingMenu("root")
root.assertTrue(root.menu.mode === "menu", "the regular menu takes over from select A")
}

// Scenario B: several requests opened back-to-back with no delay in
// between, so b, c and d are displaced synchronously — possibly while
// an earlier completion write is still spawning. Their done files must
// all land anyway.
if (tick === 12) {
root.menu.open(root.selectPayload("B", "b"))
root.assertTrue(root.menu.requestActive === true, "select B becomes the active request")
root.menu.open(root.selectPayload("C", "c"))
root.menu.open(root.selectPayload("D", "d"))
root.menu.open(root.selectPayload("E", "e"))
root.assertTrue(root.menu.doneFile === root.scratchDir + "/done-e", "select E holds the live request afterwards")
root.menu.applyDmenuSelection("bravo")
}

// Scenario C: the native keybindings menu replaces a live select.
if (tick === 20) {
root.menu.open(root.selectPayload("F", "f"))
root.menu.open(JSON.stringify({ mode: "keybindings" }))
root.assertTrue(root.menu.mode === "keybindings", "keybindings take over from select F")
root.assertTrue(!root.menu.requestActive, "keybindings clear the old request")
}

if (tick === 28) {
root.assertTrue(root.menu.opened, "completion writes leave the replacement menu open")
root.finish()
}
} catch (error) {
root.fail("menu supersede fixture threw: " + error)
root.finish()
}
}
}

function finish() {
driver.running = false
// Result first, quit later: execDetached spawns asynchronously, and an
// immediate Qt.quit() can win the race and leave the result unwritten.
root.writeResult()
quitDelay.restart()
}

Timer {
id: quitDelay
interval: 500
repeat: false
onTriggered: Qt.quit()
}
}
23 changes: 23 additions & 0 deletions test/shell.d/menu-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -539,3 +539,26 @@ rows=$(BOOT_BACKEND=limine CALLS="$select_tmp/snapshot-calls" PATH="$select_tmp/
rows=$(BOOT_BACKEND=systemd-boot CALLS="$select_tmp/snapshot-calls" PATH="$select_tmp/snapshots:$PATH" bash -c "$snapshots_script")
[[ $rows == $'Snapshot 12 (update)\t12\t' ]] || fail "on systemd-boot the Snapshots provider lists the entries" "$rows"
pass "the Snapshots provider lists systemd-boot's entries and asks nothing of Limine"

# A select displaced by the menu or by a newer select still gets its done file,
# or the omarchy-menu-select waiting on it polls forever.
require_quickshell "menu select supersede test"

qml_fixture_scratch
trap 'qml_fixture_cleanup; rm -rf "$select_tmp"' EXIT
scratch="$QML_FIXTURE_TMP/scratch"
mkdir -p "$scratch"

OMARCHY_MENU_TMP="$scratch" run_qml_fixture menu-select-supersede "the menu takes over from pending select requests" Ui Commons

for name in a b c d f; do
if [[ ! -e $scratch/done-$name ]]; then
sed -n '1,220p' "$QML_FIXTURE_LOG" >&2
fail "superseding a pending menu select strands its waiter" "done-$name was never written; the displaced omarchy-menu-select waiter would spin forever"
fi
[[ ! -s $scratch/sel-$name && ! -s $scratch/done-$name ]] || fail "a displaced menu request is cancelled" "request $name returned a selection"
done

[[ -e $scratch/done-e && $(cat "$scratch/sel-e") == "bravo" ]] || fail "a selected value queues behind cancellation writes" "select E did not return bravo"

pass "superseding a pending menu select finishes its request instead of leaking the waiter"