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
31 changes: 29 additions & 2 deletions bin/omarchy-menu-select
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# omarchy:summary=Pick one or multiple options from a menu
# 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:args=prompt [option...] [-- --multiple --selected <value> --on-change <command-json> --change-key <key> --confirm <message> --confirm-label <text> --width <pixels> --maxheight <pixels>]
# omarchy:examples=omarchy menu select Format jpg png|omarchy-menu-select Resolution 4k 1080p 720p -- --width 400

# An option may lead with an icon, as "<glyph><TAB><label>", and may trail a
Expand All @@ -18,6 +18,11 @@
# command may print a JSON array of actual selections to update the checkmarks.
# --selected takes a returned value (label plus any subtext), and can be repeated.

# --confirm asks before a pick returns, in the same menu: the message (%1 is
# the picked label) with Cancel focused and a button named by --confirm-label.
# Cancel, Escape or a click outside it goes back to the list. It cannot be
# combined with --multiple.

set -euo pipefail

if (( $# < 1 )); then
Expand All @@ -35,6 +40,8 @@ multiple=0
on_change="[]"
change_key=""
selected=()
confirm=""
confirm_label=""

while (( $# > 0 )); do
if [[ $1 == "--" ]]; then
Expand All @@ -60,6 +67,19 @@ while (( $# > 0 )); do
--multiple)
multiple=1
;;
--confirm|--confirm-label)
arg="$1"
shift
if (( $# == 0 )); then
echo "omarchy-menu-select: $arg requires a value" >&2
exit 1
fi
if [[ $arg == "--confirm" ]]; then
confirm="$1"
else
confirm_label="$1"
fi
;;
--selected)
shift
if (( $# == 0 )); then
Expand Down Expand Up @@ -104,6 +124,11 @@ if (( ${#options[@]} == 0 )); then
exit 1
fi

if (( multiple )) && [[ -n $confirm ]]; then
echo "omarchy-menu-select: --confirm cannot be combined with --multiple" >&2
exit 1
fi

selection_file=$(mktemp)
done_file=$(mktemp)
rm -f "$done_file"
Expand All @@ -125,8 +150,10 @@ payload=$(perl -MEncode=decode -MJSON::PP=encode_json,decode_json -e '
};
$payload->{width} = int($ARGV[4]) if length($ARGV[4] // "");
$payload->{maxHeight} = int($ARGV[5]) if length($ARGV[5] // "");
$payload->{confirm} = decode("UTF-8", $ARGV[10]) if length($ARGV[10] // "");
$payload->{confirmLabel} = decode("UTF-8", $ARGV[11]) if length($ARGV[11] // "");
print encode_json($payload)
' "$prompt" "$options_json" "$selection_file" "$done_file" "$menu_width" "$menu_maxheight" "$multiple" "$selected_json" "$on_change" "$change_key")
' "$prompt" "$options_json" "$selection_file" "$done_file" "$menu_width" "$menu_maxheight" "$multiple" "$selected_json" "$on_change" "$change_key" "$confirm" "$confirm_label")

omarchy-shell shell summon omarchy.menu "$payload" >/dev/null || exit 2

Expand Down
2 changes: 2 additions & 0 deletions docs/menu.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,5 @@ Pass `-- --multiple` to `omarchy-menu-select` for a multi-select request. Repeat
With `--on-change <JSON command array>`, each toggle runs that command with the proposed selection JSON appended as its final argument, and no Apply row is shown. The checkbox changes only after the command succeeds; a failed command retains the previous selection. Changes persist when the menu closes. The typing selectors use this mode.

For immediate multi-select requests, `--change-key <key>` identifies the setting being changed across picker openings. The menu keeps queued selections for that key when reopened, so a new toggle cannot discard a change still being saved. Without a key, the command array identifies the setting.

Pass `-- --confirm <message>` to ask before a pick returns, without closing the menu: choosing a row opens a Cancel/Confirm prompt over the list, with `%1` in the message replaced by the row's label, Cancel focused, and the confirm button named by `--confirm-label` (default "Confirm"). Cancel, Escape, or a click anywhere outside the prompt (on the list or past the menu's edge) goes back to the list with the search kept; once back, Escape or a click past the edge closes the menu as usual. A new request summoned while the prompt is open replaces it, so a confirm never answers a later request. The prompt is the same one the Apps menu uses to confirm an uninstall. `--confirm` is for single picks and is rejected with `--multiple`.
23 changes: 22 additions & 1 deletion shell/Ui/ConfirmDialog.qml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Item {
}

visible: opened
// The prompt card's own height, so a short host can grow to hold it.
implicitHeight: card.height

Rectangle {
anchors.fill: parent
Expand Down Expand Up @@ -81,7 +83,24 @@ Item {
wrapMode: Text.WordWrap
}

TextMetrics { id: cancelMetrics; font.family: root.fontFamily; font.pixelSize: Style.font.caption; text: root.cancelText }
TextMetrics { id: confirmMetrics; font.family: root.fontFamily; font.pixelSize: Style.font.caption; text: root.confirmText }

Row {
id: buttons

// Each button fits its label, never narrower than the usual 88. When
// the card is too narrow for both, the longer label gives way first
// and elides, so the buttons never overlap or leave the card.
readonly property var natural: [cancelMetrics.advanceWidth, confirmMetrics.advanceWidth].map(function(width) {
return Math.max(Style.space(88), Math.ceil(width) + Style.space(24))
})
readonly property real room: parent.width - spacing

function buttonWidth(index) {
return Math.min(natural[index], Math.max(room / 2, room - natural[1 - index]))
}

anchors.right: parent.right
anchors.bottom: parent.bottom
spacing: Style.space(10)
Expand All @@ -96,7 +115,7 @@ Item {
readonly property bool selected: root.selectedIndex === index
readonly property bool destructive: index === 1

width: Style.space(88)
width: buttons.buttonWidth(index)
height: Style.space(34)
color: selected
? (destructive ? Util.alpha(Commons.Color.urgent, 0.22) : root.selectedBackground)
Expand All @@ -109,6 +128,8 @@ Item {
Text {
textFormat: Text.PlainText
anchors.centerIn: parent
width: Math.min(implicitWidth, parent.width - Style.space(12))
elide: Text.ElideRight
text: modelData
color: destructive ? (selected ? Commons.Color.urgent : root.foreground) : (selected ? root.selectedText : root.foreground)
font.family: root.fontFamily
Expand Down
95 changes: 64 additions & 31 deletions shell/plugins/menu/Menu.qml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ Item {
property var dmenuSaved: []
property string dmenuChangeKey: ""
property var dmenuOnChange: []
// With a confirm prompt, a pick asks before it returns; %1 is the row's label.
property string dmenuConfirm: ""
property string dmenuConfirmLabel: ""
property string selectionFile: ""
property string doneFile: ""
property int dmenuWidth: 300
Expand All @@ -105,9 +108,11 @@ Item {
// Shared application engine (entries, hidden filters, icons, launch,
// removal), owned by the shell and also used by the standalone launcher.
readonly property var appLibrary: root.shell ? root.shell.appLibrary : null
property bool deleteConfirmOpen: false
property var deleteTarget: null
onOpenedChanged: if (!opened) { deleteConfirmOpen = false; deleteTarget = null }
// What the confirmation is asking about: an app to uninstall in the menu, or
// a picked row awaiting the caller's confirm prompt in select mode.
property bool confirmOpen: false
property var confirmTarget: null
onOpenedChanged: if (!opened) dropConfirm()
// Bound to the central [menu] section in shell.toml via Commons.Color.qml.
// Each color already includes its alpha companion (composed in the
// singleton), so consumers can drop them straight into a Rectangle.
Expand Down Expand Up @@ -137,9 +142,11 @@ Item {
property int layoutSerial: 0
property int cardWidth: Math.min(root.dmenuActive ? Style.space(root.dmenuWidth) : ((root.activeMenu === "trigger.capture.screenrecord" || root.activeMenu === "appearance.font" || root.activeMenu === "update.kernel") ? Style.space(520) : Style.space(300)), panel.width - Style.gapsOut * 2)
property int visibleRowsHeight: root.dmenuActive ? dmenuRowListHeight(layoutSerial, displayModel.count, filterText) : rowListHeight(layoutSerial, displayModel.count, filterText, searchDivider)
property int cardHeight: root.dmenuActive
? Math.min(contentMargin * 2 + headerHeight + (mode === "input" ? 0 : contentSpacing + visibleRowsHeight), panel.height - Style.gapsOut * 2)
: Math.min(contentMargin * 2 + headerHeight + contentSpacing + visibleRowsHeight, panel.height - Style.gapsOut * 2)
property int cardHeight: Math.min(Math.max(root.dmenuActive
? contentMargin * 2 + headerHeight + (mode === "input" ? 0 : contentSpacing + visibleRowsHeight)
: contentMargin * 2 + headerHeight + contentSpacing + visibleRowsHeight,
// A short list grows while a confirmation is open so the prompt fits inside it.
confirmOpen ? confirmDialog.implicitHeight + contentMargin * 2 : 0), panel.height - Style.gapsOut * 2)

function finishRequest(selection) {
if (!root.requestActive || !root.doneFile) {
Expand Down Expand Up @@ -858,7 +865,7 @@ Item {
}

function activateIndex(index, fromPointer) {
if (root.deleteConfirmOpen) return
if (root.confirmOpen) return
if (root.dmenuActive) {
if (root.mode === "input") {
root.applyDmenuSelection(root.filterText)
Expand All @@ -880,7 +887,10 @@ Item {
}
}
} else {
root.applyDmenuSelection(picked.detail ? picked.label + "\t" + picked.detail : picked.label)
var choice = picked.detail ? picked.label + "\t" + picked.detail : picked.label
// Cancel starts focused: a confirm prompt guards something worth a second look.
if (root.dmenuConfirm) root.requestConfirm({ value: choice, label: picked.label }, 0)
else root.applyDmenuSelection(choice)
}
return
}
Expand All @@ -906,24 +916,37 @@ Item {
if (!root.cursorActive || root.selectedIndex < 0 || root.selectedIndex >= displayModel.count) return
var row = displayModel.get(root.selectedIndex)
if (!row || row.kind !== "app") return
root.deleteTarget = { appId: row.appId, label: row.label }
deleteConfirm.selectedIndex = 1
root.deleteConfirmOpen = true
root.requestConfirm({ appId: row.appId, label: row.label }, 1)
}

function cancelDelete() {
root.deleteConfirmOpen = false
root.deleteTarget = null
deleteConfirm.selectedIndex = 1
// A confirmation belongs to the request it was asked in.
function requestConfirm(target, focusIndex) {
target.serial = root.requestSerial
root.confirmTarget = target
confirmDialog.selectedIndex = focusIndex
root.confirmOpen = true
}

function dropConfirm() {
root.confirmOpen = false
root.confirmTarget = null
}

// Backing out of a confirmation returns to the list, not out of the menu.
function cancelConfirm() {
root.dropConfirm()
root.disarmPointer()
Qt.callLater(function() { filterInput.forceActiveFocus() })
}

function confirmDelete() {
var target = root.deleteTarget
root.deleteConfirmOpen = false
root.deleteTarget = null
if (!target) return
function acceptConfirm() {
var target = root.confirmTarget
root.dropConfirm()
if (!target || target.serial !== root.requestSerial) return
if (root.dmenuActive) {
root.applyDmenuSelection(target.value)
return
}
root.cancel()
if (root.appLibrary) root.appLibrary.remove(target.appId, target.label)
}
Expand Down Expand Up @@ -952,6 +975,7 @@ Item {

function openExistingMenu(initialMenu) {
requestSerial += 1
root.dropConfirm()
mode = "menu"
requestActive = false
selectionFile = ""
Expand All @@ -976,13 +1000,17 @@ Item {

function openDmenu(payload) {
requestSerial += 1
// A new request replaces the old one whole, prompt included.
root.dropConfirm()
mode = payload.mode === "input" ? "input" : "select"
dmenuPrompt = String(payload.prompt || (mode === "input" ? Commons.I18n.t("menu.inputPrompt") : Commons.I18n.t("menu.selectPrompt")))
dmenuOptions = Array.isArray(payload.options) ? payload.options : []
dmenuMultiple = mode === "select" && payload.multiple === true
dmenuOnChange = dmenuMultiple && Array.isArray(payload.onChange)
&& payload.onChange.every(function(arg) { return typeof arg === "string" }) ? payload.onChange : []
dmenuChangeKey = String(payload.changeKey || JSON.stringify(dmenuOnChange))
dmenuConfirm = mode === "select" && !dmenuMultiple ? String(payload.confirm || "") : ""
dmenuConfirmLabel = String(payload.confirmLabel || "")
dmenuSelected = MenuModel.dmenuSelections(dmenuOptions, Array.isArray(payload.selected) ? payload.selected : [], dmenuOnChange.length > 0)
dmenuSaved = dmenuSelected
if (dmenuOnChange.length > 0)
Expand Down Expand Up @@ -1299,9 +1327,12 @@ Item {
color: root.scrim
}

// A click past the card closes the menu. A select request's confirmation
// was asked for by its caller in the middle of a pick, so a click there
// backs out of it to the list instead, like any click outside the prompt.
MouseArea {
anchors.fill: parent
onClicked: root.cancel()
onClicked: root.confirmOpen && root.dmenuActive ? root.cancelConfirm() : root.cancel()
}

BorderSurface {
Expand All @@ -1320,13 +1351,13 @@ Item {
Item {
id: keyCatcher
anchors.fill: parent
z: root.deleteConfirmOpen ? 20 : 0
z: root.confirmOpen ? 20 : 0

// Focus is on filterInput, which forwards every key here first.
Keys.onPressed: function(event) {
if (root.deleteConfirmOpen) {
if (root.confirmOpen) {
// Swallow the rest too, or it would type into the search line behind the dialog.
deleteConfirm.handleKey(event)
confirmDialog.handleKey(event)
event.accepted = true
return
}
Expand Down Expand Up @@ -1367,22 +1398,24 @@ Item {
}

ConfirmDialog {
id: deleteConfirm
id: confirmDialog

readonly property string targetLabel: (root.confirmTarget && root.confirmTarget.label) || ""

anchors.fill: parent
opened: root.deleteConfirmOpen
opened: root.confirmOpen
z: 10
message: Commons.I18n.t("common.uninstallConfirm").arg((root.deleteTarget && root.deleteTarget.label) || "")
confirmText: Commons.I18n.t("common.uninstall")
message: root.dmenuActive ? root.dmenuConfirm.split("%1").join(confirmDialog.targetLabel) : Commons.I18n.t("common.uninstallConfirm").arg(confirmDialog.targetLabel)
confirmText: root.dmenuActive ? (root.dmenuConfirmLabel || Commons.I18n.t("common.confirm")) : Commons.I18n.t("common.uninstall")
background: root.background
foreground: root.foreground
scrim: root.scrim
selectedBackground: root.selectedBackground
selectedText: root.selectedText
fontFamily: root.fontFamily
cornerRadius: root.cornerRadius
onCanceled: root.cancelDelete()
onConfirmed: root.confirmDelete()
onCanceled: root.cancelConfirm()
onConfirmed: root.acceptConfirm()
}
}

Expand All @@ -1407,7 +1440,7 @@ Item {
anchors.verticalCenter: parent.verticalCenter
keyHandler: keyCatcher
// Input-method commits bypass key handling, so the dialog has to refuse them here.
readOnly: root.deleteConfirmOpen
readOnly: root.confirmOpen
color: root.foreground
font.family: root.fontFamily
font.pixelSize: Style.font.heading
Expand Down
Loading