diff --git a/bin/omarchy-menu-keybindings b/bin/omarchy-menu-keybindings index 153a4d29432c..2ddc73e2f347 100755 --- a/bin/omarchy-menu-keybindings +++ b/bin/omarchy-menu-keybindings @@ -5,7 +5,13 @@ # Hyprland's Lua config provider currently reports Lua binds as dispatcher # __lua in `hyprctl binds`. Keep a lightweight source-derived cache so the # menu can still show and dispatch those bindings. +# +# What a bind dispatches is looked up by its id, so renaming what the bind is +# called cannot move it. Hyprland's output carries no id, so the first two maps +# below take a reported description and hand back the key and the id it was +# declared with; they are the whole of what still reads display text. declare -A LUA_BIND_KEY_MAP +declare -A LUA_BIND_ID_MAP declare -A LUA_BIND_DISPATCHER_MAP declare -A LUA_BIND_ARG_MAP @@ -69,15 +75,17 @@ parse_keycodes() { # Supplement `hyprctl binds` for Lua-only binds that Hyprland currently # reports as dispatcher __lua and without their original key for code: binds. build_lua_bind_cache() { - local modmask description key dispatcher arg cache_key + local modmask description id key dispatcher arg cache_key omarchy-cmd-present lua || return 0 - while IFS=$'\t' read -r modmask description key dispatcher arg; do - [[ -z $modmask || -z $description || -z $key ]] && continue + while IFS=$'\t' read -r modmask description id key dispatcher arg; do + [[ -z $modmask || -z $description || -z $id || -z $key ]] && continue LUA_BIND_KEY_MAP["$modmask,$description"]="$key" - cache_key="$modmask,$description,$key" + LUA_BIND_ID_MAP["$modmask,$description,$key"]="$id" + + cache_key="$modmask,$id,$key" LUA_BIND_DISPATCHER_MAP["$cache_key"]="$dispatcher" LUA_BIND_ARG_MAP["$cache_key"]="$arg" done < <( @@ -194,6 +202,23 @@ noop = setmetatable({}, { end, }) +-- A description, an id and a command are the config author's own text, and the +-- record below is tab delimited, one bind to a line. A tab inside a field would +-- move every field after it one column along and a newline would split the bind +-- into two records, so fold either into a space: a bind is worth reporting with +-- its label squeezed, and not worth losing. +local function one_line(value) + return (tostring(value):gsub("[\t\n\r]", " ")) +end + +-- The description is the one field the menu matches against what `hyprctl binds` +-- reports, and hyprctl prints one field to a line, so the description it reports +-- stops at the first newline. Stop this copy there too: a field that folded the +-- newline into a space could never meet the description it is looked up by. +local function first_line(value) + return (tostring(value):gsub("\n.*", "")) +end + hl = setmetatable({ dsp = dsp_proxy("hl.dsp"), bind = function(keys, bind_dispatcher, opts) @@ -203,6 +228,11 @@ hl = setmetatable({ local modmask, key = split_keys(keys) local kind = "" local arg = "" + -- o.bind fills this in; a bind written against hl.bind directly may not. + local id = opts.id + if id == nil or id == "" then + id = opts.description + end if type(bind_dispatcher) == "table" and bind_dispatcher.__omarchy_dispatcher then kind = bind_dispatcher.kind or "" @@ -215,7 +245,14 @@ hl = setmetatable({ arg = o.bind_commands[bind_dispatcher] end - print(table.concat({ tostring(modmask), opts.description, key, kind, arg }, "\t")) + print(table.concat({ + one_line(modmask), + one_line(first_line(opts.description)), + one_line(id), + one_line(key), + one_line(kind), + one_line(arg), + }, "\t")) end return noop @@ -273,7 +310,7 @@ modmask_to_text() { # - Map numeric modifier key mask to a textual rendition # - Output comma-separated values that the parser can understand dynamic_bindings() { - local modmask key keycode description dispatcher arg modifiers cache_key + local modmask key keycode description dispatcher arg id modifiers cache_key # Parse the plain `hyprctl binds` output rather than `hyprctl -j binds`: # Hyprland 0.56.0 emits invalid JSON for binds (misaligned fields in @@ -293,16 +330,36 @@ dynamic_bindings() { # modifiers are already carried separately in modmask. key="${key##* + }" + # The description Hyprland hands back is the config author's own text, and + # this is where it enters a tab delimited record. Fold the delimiter out of + # it the way the Lua scan folds its own copy: a tab left in reaches the + # record as a field of its own, where the menu reads the back half of a + # label as the dispatcher it would run, and no folded description the scan + # stored can be found by an unfolded one. + description="${description//[$'\t\r']/ }" + if [[ -z $key && $keycode != "0" ]]; then key="code:$keycode" fi + # The loop-generated binds reach here with neither key nor keycode, and + # their description is the only thing Hyprland gives to find the key by. + # That makes this the one lookup that cannot key on the id: recovering an + # id needs the key it is looking for. if [[ -z $key && -n $description ]]; then key="${LUA_BIND_KEY_MAP["$modmask,$description"]}" fi + # With a key in hand, the description resolves to the id the bind was + # declared with. Everything below this line keys on the id. + id="${LUA_BIND_ID_MAP["$modmask,$description,$key"]}" + + # A bind the cache has nothing for still has to come out of here with no + # dispatcher at all: __lua is not something the menu can run, and a row + # that kept it would read as agreeing with every other row that also + # failed to resolve. if [[ $dispatcher == "__lua" && -n $description && -n $key ]]; then - cache_key="$modmask,$description,$key" + cache_key="$modmask,$id,$key" dispatcher="${LUA_BIND_DISPATCHER_MAP["$cache_key"]}" arg="${LUA_BIND_ARG_MAP["$cache_key"]}" fi @@ -533,7 +590,7 @@ output_binding_records_uncached() { keybindings_cache_key() { { - printf 'v14\n' + printf 'v17\n' hyprctl devices 2>/dev/null | grep -F 'active keymap:' hyprctl binds 2>/dev/null } | sha256sum | awk '{ print $1 }' diff --git a/default/hypr/bindings/applications.lua b/default/hypr/bindings/applications.lua index e4428a9fb38a..e852d3ebeb61 100644 --- a/default/hypr/bindings/applications.lua +++ b/default/hypr/bindings/applications.lua @@ -25,10 +25,14 @@ if o.preinstalled_bindings_enabled() then o.bind("SUPER + SHIFT + E", "Email", { webapp = "https://app.hey.com" }) o.bind("SUPER + SHIFT + ALT + E", "New email", { webapp = "https://app.hey.com/messages/new?display=standalone&new_window=true" }) o.bind("SUPER + SHIFT + Y", "YouTube", { webapp = "https://youtube.com/" }) - o.bind("SUPER + SHIFT + ALT + G", "WhatsApp", { webapp = "https://web.whatsapp.com/", focus = true }) - o.bind( "SUPER + SHIFT + CTRL + G", "Google Messages", { webapp = "https://messages.google.com/web/conversations", focus = true }) - o.bind("SUPER + SHIFT + P", "Google Photos", { webapp = "https://photos.google.com/", focus = true }) - o.bind("SUPER + SHIFT + S", "Google Maps", { webapp = "https://maps.google.com/", focus = true }) + -- These four focus an open window instead of opening a second one, by + -- matching their id against the window's own class or title. That makes the + -- id a pattern the web app decides, not a label of ours, so it is spelled out + -- rather than inherited from the description next to it. + o.bind("SUPER + SHIFT + ALT + G", "WhatsApp", { webapp = "https://web.whatsapp.com/", focus = true }, { id = "WhatsApp" }) + o.bind( "SUPER + SHIFT + CTRL + G", "Google Messages", { webapp = "https://messages.google.com/web/conversations", focus = true }, { id = "Google Messages" }) + o.bind("SUPER + SHIFT + P", "Google Photos", { webapp = "https://photos.google.com/", focus = true }, { id = "Google Photos" }) + o.bind("SUPER + SHIFT + S", "Google Maps", { webapp = "https://maps.google.com/", focus = true }, { id = "Google Maps" }) o.bind("SUPER + SHIFT + X", "X", { webapp = "https://x.com/" }) o.bind("SUPER + SHIFT + ALT + X", "X Post", { webapp = "https://x.com/compose/post" }) end diff --git a/default/hypr/helpers.lua b/default/hypr/helpers.lua index 05f8f91e51b8..b13fa5ad0635 100644 --- a/default/hypr/helpers.lua +++ b/default/hypr/helpers.lua @@ -87,7 +87,7 @@ local function shell_dispatcher(kind, target, command) return command end -local function command_from(value, description) +local function command_from(value, id) if type(value) ~= "table" then return value end @@ -112,7 +112,7 @@ local function command_from(value, description) return o.launch(value.launch) elseif value.webapp then if value.focus then - return o.launch_webapp_sole(description, value.webapp) + return o.launch_webapp_sole(id, value.webapp) else return o.launch_webapp(value.webapp) end @@ -136,13 +136,36 @@ function o.preinstalled_bindings_enabled() end function o.bind(keys, description, dispatcher, options) - local opts = options or {} + -- Copy what the caller handed over rather than writing into it. The id below + -- fills in only when none is set yet, so a config reusing one options table + -- across several binds would give every later bind the first one's id. + local opts = {} + + for key, value in pairs(options or {}) do + opts[key] = value + end + + -- pairs sees a table's own keys and nothing it inherits, so carry the + -- metatable over too: a config that keeps shared flags on one table and + -- points each bind's options at it through __index would otherwise hand + -- hl.bind a copy that has lost them. + local inherited = getmetatable(options or {}) + + if type(inherited) == "table" then + setmetatable(opts, inherited) + end if description then opts.description = description end - dispatcher = command_from(dispatcher, description) + -- A description is what a bind is called; an id is what it is. Anything that + -- has to recognize a bind again later (a cache key, a window match) keys on + -- the id, so a renamed description does not drag behaviour along with it. + -- The description is the only name most binds need, so it is the default. + opts.id = opts.id or description + + dispatcher = command_from(dispatcher, opts.id) if type(dispatcher) == "string" then dispatcher = hl.dsp.exec_cmd(dispatcher) diff --git a/test/shell.d/hyprland-bind-id-test.sh b/test/shell.d/hyprland-bind-id-test.sh new file mode 100755 index 000000000000..465399c730d2 --- /dev/null +++ b/test/shell.d/hyprland-bind-id-test.sh @@ -0,0 +1,196 @@ +#!/bin/bash + +source "$(dirname "${BASH_SOURCE[0]}")/base-test.sh" + +require_command lua + +tmpdir=$(mktemp -d) && [[ -n $tmpdir && -d $tmpdir ]] || + fail "the test gets a temporary directory to load the Lua config in" +trap 'rm -rf "$tmpdir"' EXIT + +# What o.bind hands to hl.bind, one declaration at a time. The dispatcher is +# printed as the command string Hyprland would run, since that is where a web +# app bind carries the name it matches a window by. locked stands in for the +# flags o.bind only forwards, having no opinion of its own about them. +bound() { + cat >"$tmpdir/bind.lua" <"$tmpdir/scan.lua" +[[ -s $tmpdir/scan.lua ]] || + fail "the menu's Lua bind scan can be read out of the script" + +home="$tmpdir/home" +mkdir -p "$home/.config/hypr" + +# The scan reads ~/.config/hypr/hyprland.lua. Pull in the one binding file this +# is about rather than the whole config: the rest of it is not what is under +# test. The three binds below are what a personal config adds: an id that is +# nothing like its label, a bind written straight against hl.bind, and a label +# and id with a tab and a newline of their own in them. +cat >"$home/.config/hypr/hyprland.lua" <"$tmpdir/webapps.lua" </dev/null grep -qP '→ Terminal\texec\tomarchy-launch-terminal$' "$tmpdir"/cache/omarchy/keybindings-*.records || fail "picking the terminal bind from the menu launches a terminal" "$(cat "$tmpdir"/cache/omarchy/keybindings-*.records)" pass "picking the terminal bind from the menu launches a terminal" + +# A label is the config author's own text and the record is tab delimited, so a +# tab in a label used to take the field after it: the menu read the back half of +# the label as the dispatcher it would run. Hyprland reports one field to a line, +# which cuts a label at a newline, so the scan has to key on what it reported. +# A config of its own here, since these are binds no Omarchy install ships. +own_home="$tmpdir/own-home" +mkdir -p "$own_home/.config/hypr" +cat >"$own_home/.config/hypr/hyprland.lua" </dev/null +own_records=$(cat "$tmpdir"/cache/omarchy/keybindings-*.records) + +grep -qP '→ Plain label\texec\ttrue$' <<<"$own_records" || + fail "a bind in the author's own config resolves what it runs" "$own_records" +grep -qP '→ Tabbed label\texec\ttrue$' <<<"$own_records" || + fail "a tab in a label does not become the dispatcher the menu runs" "$own_records" +grep -qP '→ Split\texec\ttrue$' <<<"$own_records" || + fail "a label cut short by Hyprland still finds what it runs" "$own_records" +pass "a label carrying a tab or a newline still resolves what it runs"