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
7 changes: 5 additions & 2 deletions bin/omarchy-menu-select
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
# omarchy:group=menu
# omarchy:name=select
# omarchy:args=prompt [option...] [-- --multiple --selected <value> --on-change <command-json> --change-key <key> --width <pixels> --maxheight <pixels>]
# omarchy:examples=omarchy menu select Format jpg png|omarchy-menu-select Resolution 4k 1080p 720p -- --width 400
# omarchy:examples=omarchy menu select Format jpg png|omarchy-menu-select Resolution 4k 1080p 720p -- --width 400|omarchy-menu-select Open --heading=Recent a.txt --heading=Projects omarchy

# An option may lead with an icon, as "<glyph><TAB><label>", and may trail a
# subtext shown under the label, as "<glyph><TAB><label><TAB><subtext>". The
# menu shows the glyph but never returns it. A plain option returns the label
# alone; an option with a subtext returns "<label><TAB><subtext>", so callers
# with same-named rows get the subtext back as the stable key.

# Use "--heading=<text>" to group the choices below it. A heading cannot be
# selected. Search hides it when none of its choices match.

# With --multiple, clicking/Enter toggles rows and Apply returns a JSON array.
# --on-change takes a JSON command array; each toggle runs it with the selection
# JSON appended, applies immediately, and needs no Apply row. A successful
Expand Down Expand Up @@ -126,7 +129,7 @@ payload=$(
{
mode: "select",
prompt: $prompt,
options: $ARGS.positional,
options: ($ARGS.positional | map(if startswith("--heading=") then { heading: .[10:] } else . end)),
multiple: $multiple,
selected: (if length == 0 then [] else split("\u0000")[:-1] end),
onChange: $onChange,
Expand Down
5 changes: 5 additions & 0 deletions docs/menu.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ renders under the label, filters with it, and comes back as
how the pickers behind menu actions (`omarchy-menu-plugin`,
`omarchy-menu-timezone`, ...) present lists without owning any UI.

Use `--heading=<text>` to group the options that follow it. Headings have a separator line. You cannot select or return a heading. Search hides a heading when none of its options match.

```bash
omarchy-menu-select "Start in" --heading=Repositories api web --heading=Folders notes
```

Pass `-- --multiple` to `omarchy-menu-select` for a multi-select request. Repeat `--selected <value>` to preselect returned values (labels plus any subtext, without icons). Clicking or pressing Enter toggles an option without closing the menu; Apply returns a JSON array in option order, including `[]` when all choices are deselected. Cancellation still exits 1. Selections survive filtering.

Expand Down
81 changes: 57 additions & 24 deletions shell/plugins/menu/Menu.qml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ Item {
property int rowPeek: Math.round(baseRowHeight * 0.55)
property int rowSpacing: Style.spacing.xs
property int dividerHeight: Style.space(17)
property int headingRowHeight: Style.font.caption + Style.space(22)
property bool searchDivider: false
property int layoutSerial: 0
// Menus whose rows carry long labels get the wide card.
Expand Down Expand Up @@ -207,6 +208,10 @@ Item {
return root.showsDetail(itemId, detail) ? root.detailRowHeight : root.baseRowHeight
}

function rowHeightFor(itemId, kind, detail) {
return kind === "heading" ? root.headingRowHeight : root.rowHeightForDetail(itemId, detail)
}

// Height the card can devote to rows before running off the screen — or
// past the frozen top edge once a search has pinned the card in place.
// Uses panel.cardTop rather than effectiveCardTop: the centered top is
Expand Down Expand Up @@ -270,7 +275,7 @@ Item {
var total = 0
for (var i = 0; i < rows.length; i++) {
if (i > 0) total += root.rowSpacing
total += root.rowHeightForDetail(rows[i].itemId, rows[i].detail)
total += root.rowHeightFor(rows[i].itemId, rows[i].kind, rows[i].detail)
totals.push(total)
}

Expand Down Expand Up @@ -698,34 +703,33 @@ Item {
action: "", provider: "", score: -1, section: ""
})
}
var query = root.filterText.trim().toLowerCase()
for (var i = 0; i < root.dmenuOptions.length; i++) {
// An option is "<label>", "<glyph>\t<label>", or
// "<glyph>\t<label>\t<subtext>". The glyph never comes back with the
// selection; the subtext renders under the label, filters alongside it,
// and returns with the selection as a stable key for same-named rows.
var parts = String(root.dmenuOptions[i] || "").split("\t")
var icon = parts.length > 1 ? parts.shift() : ""
var label = parts.shift() || ""
var detail = parts.join("\t")
if (query && label.toLowerCase().indexOf(query) < 0
&& detail.toLowerCase().indexOf(query) < 0) continue
var options = MenuModel.dmenuRows(root.dmenuOptions, root.filterText)
for (var i = 0; i < options.length; i++) {
var row = options[i]
if (row.heading) {
rows.push({
itemId: "dmenu.heading." + i, disabled: true, kind: "heading", icon: "", iconFont: "",
appIcon: "", appId: "", label: row.label, target: "", detail: "", path: "", childCount: 0,
action: "", provider: "", score: -1, section: ""
})
continue
}
rows.push({
itemId: "dmenu." + i,
itemId: "dmenu." + row.index,
disabled: false,
kind: "dmenu",
icon: root.dmenuMultiple ? (root.dmenuSelected.indexOf(MenuModel.dmenuValue(root.dmenuOptions[i])) !== -1 ? "✓" : "○") : icon,
icon: root.dmenuMultiple ? root.dmenuCheckIcon(row.index) : row.icon,
iconFont: "",
appIcon: "",
appId: "",
label: label,
label: row.label,
target: "",
detail: detail,
detail: row.detail,
path: "",
childCount: 0,
action: "",
provider: "",
score: i,
score: row.index,
section: ""
})
}
Expand All @@ -737,20 +741,25 @@ Item {
if (displayModel.count === 0) selectedIndex = 0
else if (selectedIndex >= displayModel.count) selectedIndex = displayModel.count - 1
else if (selectedIndex < 0) selectedIndex = 0
// Never leave the cursor on a heading.
if (root.cursorActive && !root.rowSelectable(selectedIndex)) root.settleCursor()

Qt.callLater(function() {
if (displayModel.count > 0) root.revealCursor()
})
}

function dmenuCheckIcon(optionIndex) {
return root.dmenuSelected.indexOf(MenuModel.dmenuValue(root.dmenuOptions[optionIndex])) !== -1 ? "✓" : "○"
}

function updateDmenuChecks() {
for (var i = 0; i < displayModel.count; i++) {
var row = displayModel.get(i)
if (row.itemId === "dmenu.apply") {
displayModel.setProperty(i, "detail", Commons.I18n.tn("menu.selectedCount", root.dmenuSelected.length).arg(Commons.I18n.formatNumber(root.dmenuSelected.length)))
} else if (row.kind === "dmenu") {
var value = MenuModel.dmenuValue(root.dmenuOptions[Number(row.itemId.substring(6))])
displayModel.setProperty(i, "icon", root.dmenuSelected.indexOf(value) !== -1 ? "✓" : "○")
displayModel.setProperty(i, "icon", root.dmenuCheckIcon(Number(row.itemId.substring(6))))
}
}
}
Expand Down Expand Up @@ -1725,13 +1734,12 @@ Item {

readonly property bool hasCursor: root.cursorActive && row.index === root.selectedIndex
readonly property bool isApp: row.kind === "app"
readonly property bool isHeading: row.kind === "heading"
readonly property bool hasIcon: row.icon.length > 0 || row.isApp

width: ListView.view.width
height: root.rowHeightForDetail(row.itemId, row.detail)
// Faded: the row is here to say the software is already
// installed, not to be picked.
opacity: row.disabled ? 0.4 : 1
height: root.rowHeightFor(row.itemId, row.kind, row.detail)
opacity: row.disabled && !row.isHeading ? 0.4 : 1
// The entry standing in for this row while Alt or Ctrl is held.
readonly property var altVariant: (root.heldModifiers & (Qt.AltModifier | Qt.ControlModifier)) && row.kind === "action"
? root.heldVariantFor(row.itemId) : null
Expand Down Expand Up @@ -1798,8 +1806,33 @@ Item {

HoverHandler { id: rowHover }

PanelSectionHeader {
id: headingText
visible: row.isHeading
text: row.label
width: Math.min(implicitWidth, parent.width - anchors.leftMargin - Style.space(40))
elide: Text.ElideRight
foreground: root.foreground
fontFamily: root.fontFamily
anchors.left: parent.left
anchors.leftMargin: root.rowReservedBorderLeft + Style.space(10)
anchors.bottom: parent.bottom
anchors.bottomMargin: Style.space(4)
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
}

PanelSeparator {
visible: row.isHeading
foreground: root.foreground
anchors.left: headingText.right
anchors.leftMargin: Style.space(10)
anchors.right: parent.right
anchors.rightMargin: Style.space(10)
anchors.verticalCenter: headingText.verticalCenter
}

Column {
id: contentColumn
visible: !row.isHeading
anchors.left: row.hasIcon ? iconText.right : parent.left
anchors.leftMargin: row.hasIcon ? Style.space(6) : root.rowReservedBorderLeft + Style.space(18)
anchors.right: row.isApp ? uninstallButton.left : (root.isSnapshotRow(row.itemId) ? parent.right : trail.left)
Expand Down
37 changes: 36 additions & 1 deletion shell/plugins/menu/MenuModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,40 @@ function summonAction(action) {
}


// A select option is a string ("label", "glyph\tlabel", or
// "glyph\tlabel\tsubtext") or a heading, {"heading": "Text"}, which groups the
// options after it and is never returned.
function isDmenuHeading(option) {
return !!option && typeof option === "object" && typeof option.heading === "string"
}

// The rows a select request shows for a filter: options that match, each
// heading only when something under it survives, so a search never leaves a
// heading standing over nothing.
function dmenuRows(options, query) {
var rows = []
var heading = null
var needle = String(query || "").trim().toLowerCase()
for (var i = 0; i < options.length; i++) {
if (isDmenuHeading(options[i])) {
heading = options[i].heading
continue
}
var parts = String(options[i] || "").split("\t")
var icon = parts.length > 1 ? parts.shift() : ""
var label = parts.shift() || ""
var detail = parts.join("\t")
if (needle && label.toLowerCase().indexOf(needle) < 0
&& detail.toLowerCase().indexOf(needle) < 0) continue
if (heading !== null) {
rows.push({ heading: true, label: heading, index: -1 })
heading = null
}
rows.push({ heading: false, icon: icon, label: label, detail: detail, index: i })
}
return rows
}

// Multi-select requests return stable values, without icons, in option order.
function dmenuValue(option) {
var parts = String(option || "").split("\t")
Expand All @@ -698,7 +732,7 @@ function dmenuValue(option) {
}

function dmenuSelections(options, selected, preserveOrder) {
var available = options.map(dmenuValue)
var available = options.filter(function(option) { return !isDmenuHeading(option) }).map(dmenuValue)
return (preserveOrder ? selected : available).filter(function(value, index, values) {
return available.indexOf(value) !== -1 && selected.indexOf(value) !== -1 && values.indexOf(value) === index
})
Expand Down Expand Up @@ -806,6 +840,7 @@ if (typeof module !== "undefined") {
descriptionTextMatches: descriptionTextMatches,
matchesQuery: matchesQuery,
searchScore: searchScore,
dmenuRows: dmenuRows,
dmenuValue: dmenuValue,
dmenuSelections: dmenuSelections,
toggleDmenuSelection: toggleDmenuSelection,
Expand Down
10 changes: 8 additions & 2 deletions test/shell.d/menu-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,12 @@ import json, runpy, sys
print(json.dumps(runpy.run_path(sys.argv[1])['tokenize']('a4.262 4.262 0 00.02.433 1 1 0 11.5.5')))
`, path.join(root, 'bin/omarchy-dev-font')], { encoding: 'utf8' }))
assertDeepEqual(arcTokens, ['a', 4.262, 4.262, 0, 0, 0, 0.02, 0.433, 1, 1, 0, 1, 1, 0.5, 0.5], 'omarchy dev font reads compact arc flags, including in a repeated arc')

const headed = [{ heading: 'Repositories' }, 'x\tapi\tmain', 'x\tweb', { heading: 'Folders' }, 'notes']
assertDeepEqual(menu.dmenuRows(headed, '').map(row => row.heading ? '# ' + row.label : row.label), ['# Repositories', 'api', 'web', '# Folders', 'notes'], 'select headings sit over their options')
assertDeepEqual(menu.dmenuRows(headed, 'note').map(row => row.heading ? '# ' + row.label : row.label), ['# Folders', 'notes'], 'a search drops headings left with nothing under them')
assertDeepEqual(menu.dmenuRows(headed, '').filter(row => !row.heading).map(row => row.index), [1, 2, 4], 'select rows keep their option index for the returned value')
assertDeepEqual(menu.dmenuSelections(headed, ['notes', menu.dmenuValue(headed[0]), 'api\tmain']), ['api\tmain', 'notes'], 'headings are never selectable values')
JS

# Verify omarchy-menu-select and omarchy-menu-input serialize payloads faithfully
Expand All @@ -492,14 +498,14 @@ SH
chmod +x "$select_tmp/bin/omarchy-shell"

select_answer=$(PATH="$select_tmp/bin:$PATH" PAYLOAD_OUT="$select_tmp/select.json" FAKE_ANSWER="-dash" \
"$ROOT/bin/omarchy-menu-select" "Pick (æøå)" "-dash" $'✓\tSecond\tsub' -- \
"$ROOT/bin/omarchy-menu-select" "Pick (æøå)" --heading="Récent" "-dash" $'✓\tSecond\tsub' -- \
--multiple --selected "-dash" --selected $'Second\tsub' \
--on-change '["apply","--set"]' --change-key "k1" --width 640 --maxheight 480)
[[ $select_answer == "-dash" ]] || fail "omarchy-menu-select returns the selected value" "$select_answer"
jq -e '
.mode == "select"
and .prompt == "Pick (æøå)"
and .options == ["-dash", "✓\tSecond\tsub"]
and .options == [{"heading": "Récent"}, "-dash", "✓\tSecond\tsub"]
and .multiple == true
and .selected == ["-dash", "Second\tsub"]
and .onChange == ["apply", "--set"]
Expand Down