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
37 changes: 29 additions & 8 deletions shell/plugins/menu/Menu.qml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ Item {
property int applySerial: 0
property var items: ({})
property var itemOrder: []
// Menus left behind by drilling down, each with the row that was entered,
// so Left/Backspace can put the cursor back where it was.
property var navStack: []
property var providersLoaded: ({})
property var providerQueue: []
Expand Down Expand Up @@ -633,6 +635,17 @@ Item {
root.cursorActive = target >= 0
}

// Put the cursor on the row for `itemId` (or the branch it sits under) when
// that row can take it; otherwise leave it where settleCursor parked it.
function placeCursorOn(itemId) {
var rowIds = []
for (var i = 0; i < displayModel.count; i++) rowIds.push(displayModel.get(i).itemId)
var index = MenuModel.nearestRowIndex(root.items, rowIds, itemId)
if (index < 0 || !root.rowSelectable(index)) return
root.selectedIndex = index
root.cursorActive = true
}

function rebuildDmenuDisplay() {
displayModel.clear()
root.searchDivider = false
Expand Down Expand Up @@ -832,10 +845,10 @@ Item {
root.rebuildDisplay()
}

function setActiveMenu(id, pushHistory, fromPointer) {
function setActiveMenu(id, pushHistory, fromPointer, fromItem) {
panel.freezeCardTop()
if (!root.item(id)) id = "root"
if (pushHistory && id !== root.activeMenu) root.navStack = root.navStack.concat([root.activeMenu])
if (pushHistory && id !== root.activeMenu) root.navStack = root.navStack.concat([{ menu: root.activeMenu, item: fromItem || "" }])
root.activeMenu = id
root.filterText = ""
root.selectedIndex = 0
Expand All @@ -850,18 +863,26 @@ Item {
root.loadProviderForMenu(id)
}

// Going back returns the cursor to the row that was drilled into rather
// than the top of the list. Without history -- the menu was summoned
// straight into a submenu -- the parent opens on that submenu's own row.
function goBack() {
if (root.activeMenu === "root") return false

var previous
var returnTo = root.activeMenu
if (root.navStack.length > 0) {
var previous = root.navStack[root.navStack.length - 1]
var frame = root.navStack[root.navStack.length - 1]
root.navStack = root.navStack.slice(0, root.navStack.length - 1)
root.setActiveMenu(previous, false)
return true
previous = frame.menu
returnTo = frame.item || returnTo
} else {
var active = root.item(root.activeMenu)
previous = (active && active.parent) ? active.parent : "root"
}

var active = root.item(root.activeMenu)
root.setActiveMenu((active && active.parent) ? active.parent : "root", false)
root.setActiveMenu(previous, false)
root.placeCursorOn(returnTo)
return true
}

Expand Down Expand Up @@ -897,7 +918,7 @@ Item {

var row = displayModel.get(index)
if (row.kind === "menu" || row.kind === "link") {
root.setActiveMenu(row.target || row.itemId, true, fromPointer)
root.setActiveMenu(row.target || row.itemId, true, fromPointer, row.itemId)
} else if (row.kind === "app") {
var appId = row.appId
var label = row.label
Expand Down
21 changes: 21 additions & 0 deletions shell/plugins/menu/MenuModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,26 @@ function childCount(items, itemOrder, id) {
return count
}

// Row to land the cursor on when coming back to a menu: the one for `id`, or
// for its nearest ancestor when `id` itself is not a row here -- a search
// result drills several levels down at once, and coming back lands on the
// branch it was under. -1 when no ancestor is a row either.
function nearestRowIndex(items, rowIds, id) {
var ids = Array.isArray(rowIds) ? rowIds : []
var current = id
var guard = 0

while (current && guard < 32) {
var index = ids.indexOf(current)
if (index >= 0) return index
var entry = item(items, current)
current = entry ? entry.parent : ""
guard += 1
}

return -1
}

// `alt:` names another action entry that stands in for this row while
// Alt is held: Reboot becomes Reboot to BIOS. The stand-in is an ordinary
// entry, so it translates, searches, and routes like any other; it is only
Expand Down Expand Up @@ -734,6 +754,7 @@ if (typeof module !== "undefined") {
parentPathFor: parentPathFor,
isDescendantOf: isDescendantOf,
childCount: childCount,
nearestRowIndex: nearestRowIndex,
isVisible: isVisible,
altVariant: altVariant,
altVariantIds: altVariantIds,
Expand Down
84 changes: 84 additions & 0 deletions test/shell.d/fixtures/menu-back/shell.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import QtQuick
import Quickshell

// Back out of Appearance, entered from the root menu and then summoned straight
// into, must leave the cursor on Appearance rather than the first row.
ShellRoot {
id: root

readonly property string resultPath: Quickshell.env("OMARCHY_QML_TEST_RESULT")
property var menu: null
property var list: null
property int waited: 0
property string stage: "open"

function finish(failure) {
var payload = JSON.stringify({ ok: !failure, failures: failure ? [failure] : [] })
Quickshell.execDetached(["bash", "-c", "printf '%s' \"$1\" >\"$2\"", "bash", payload, resultPath])
if (menu) menu.close()
}

function findList(item) {
if (item.indexAt && item.itemAtIndex && item.model) return item
for (var i = 0; i < item.children.length; i++) {
var found = findList(item.children[i])
if (found) return found
}
return null
}

function rowIndex(itemId) {
for (var i = 0; i < list.model.count; i++)
if (list.model.get(i).itemId === itemId) return i
return -1
}

function selectedId() {
return root.menu.cursorActive ? root.list.model.get(root.menu.selectedIndex).itemId : ""
}

Component.onCompleted: {
var component = Qt.createComponent(Quickshell.env("OMARCHY_PATH") + "/shell/plugins/menu/Menu.qml", Component.PreferSynchronous)
menu = component.createObject(root)
if (!menu) finish("Menu.qml failed to load: " + component.errorString())
}

Timer {
interval: 100
repeat: true
running: true
onTriggered: {
if (++root.waited > 100) { running = false; return root.finish("the menu never settled at stage " + root.stage) }
if (!root.menu || !root.menu.rowsLoaded) return

if (root.stage === "open") {
root.stage = "entered"
return root.menu.open("{}")
}
if (root.menu.guardsPending) return
// The rows live in the menu's own window.
for (var i = 0; i < root.menu.data.length && !root.list; i++)
if (root.menu.data[i].contentItem) root.list = root.findList(root.menu.data[i].contentItem)
if (!root.list) return

if (root.stage === "entered") {
var appearance = root.rowIndex("appearance")
if (appearance <= 0) return
root.menu.cursorActive = true
root.menu.selectedIndex = appearance
root.menu.activateIndex(appearance)
if (root.menu.activeMenu !== "appearance") { running = false; return root.finish("entering Appearance opened " + root.menu.activeMenu) }
root.menu.goBack()
if (root.selectedId() !== "appearance") { running = false; return root.finish("back from an entered Appearance lands on " + root.selectedId() + ", not Appearance") }
root.stage = "summoned"
return root.menu.open(JSON.stringify({ menu: "appearance" }))
}

running = false
if (root.menu.activeMenu !== "appearance") return root.finish("summoning Appearance opened " + root.menu.activeMenu)
root.menu.goBack()
if (root.selectedId() !== "appearance") return root.finish("back from a summoned Appearance lands on " + root.selectedId() + ", not Appearance")
root.finish("")
}
}
}
21 changes: 21 additions & 0 deletions test/shell.d/menu-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,24 @@ assert(
'provider merge leaves rows belonging to another provider alone'
)

// Going back lands on the row that was drilled into, not the top of the list.
const backItems = menu.mergeMenuSources(menu.parseMenuJsonc(`
{
"items": {
"root": { "label": "Go" },
"style": { "label": "Style" },
"style.bar": { "label": "Menu Bar" },
"style.bar.position": { "label": "Position", "action": "omarchy-bar-position" },
"learn": { "label": "Learn" }
}
}
`), []).items
const rootRows = ['style', 'learn']
assertEqual(menu.nearestRowIndex(backItems, rootRows, 'learn'), 1, 'menu back lands on the row that was entered')
assertEqual(menu.nearestRowIndex(backItems, rootRows, 'style.bar.position'), 0, 'menu back from a search drilldown lands on the branch it was under')
assertEqual(menu.nearestRowIndex(backItems, rootRows, 'missing'), -1, 'menu back leaves the cursor alone for a row it cannot find')
assertEqual(menu.nearestRowIndex(backItems, [], 'style'), -1, 'menu back copes with an empty list')
Comment on lines +442 to +445

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Back navigation stays untested

These tests call only nearestRowIndex; they never enter a submenu or call goBack. Removing the new placeCursorOn call would bring back the original bug while every added assertion still passed. Add a behavior test that enters a non-first row, goes back, and checks the selected row. Include a link whose row differs from its target to check the saved history frame too.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!


// Every glyph the menu draws in the Omarchy font is one the font carries. The
// ranges come from the font itself, so a new glyph needs no edit here.
const fontRanges = require('child_process')
Expand All @@ -439,3 +457,6 @@ const missingGlyphs = fontGlyphRows
.map(({ id, code }) => `${id} U+${code.toString(16)}`)
assertDeepEqual(missingGlyphs, [], 'every menu glyph drawn in the Omarchy font is one the font carries')
JS

require_quickshell "menu back navigation test"
run_qml_fixture menu-back "going back puts the cursor on the row that was entered" Ui Commons