Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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 @@ -97,6 +97,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 @@ -631,6 +633,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 @@ -827,10 +840,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 @@ -842,18 +855,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 @@ -889,7 +910,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 @@ -278,6 +278,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
}

function isVisible(items, itemOrder, whenResults, entry, depth) {
if (!entry) return false
if (entry.when && whenResults && whenResults[entry.id] === false) return false
Expand Down Expand Up @@ -705,6 +725,7 @@ if (typeof module !== "undefined") {
parentPathFor: parentPathFor,
isDescendantOf: isDescendantOf,
childCount: childCount,
nearestRowIndex: nearestRowIndex,
isVisible: isVisible,
isDisabled: isDisabled,
labelFor: labelFor,
Expand Down
18 changes: 18 additions & 0 deletions test/shell.d/menu-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,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 Down