Skip to content
Closed
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
10 changes: 7 additions & 3 deletions bin/omarchy-hyprland-focus-app
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
#!/bin/bash

# omarchy:summary=Focus a Hyprland window by application identity
# omarchy:args=<app-name>
# omarchy:args=<app-name> [web-app-host]
# omarchy:examples=omarchy hyprland focus app Slack

usage() {
echo "Usage: omarchy-hyprland-focus-app <app-name>" >&2
echo "Usage: omarchy-hyprland-focus-app <app-name> [web-app-host]" >&2
exit 1
}

app=${1:-}
web_app_host=${2:-}
[[ -n $app ]] || usage

# Agent terminals notify as kitty/foot/etc. while their shared window class is
# org.omarchy.agent, leaving the terminal name only in initialTitle. So match
# by class first, then fall back to the launch-time title of agent windows.
# Chromium-derived browsers notify under their own name for every web app and
# class its window <browser>-<host>__<path>-<profile>, so a host comes first.
address=$(
hyprctl clients -j 2>/dev/null |
jq -r --arg pattern "$app" \
jq -r --arg pattern "$app" --arg host "$web_app_host" \
'def matches($value): ($value // "") | test($pattern; "i");
first(
(.[] | select($host != "" and ((.class // "") | ascii_downcase | (split("__")[0] // "") | sub("^[a-z]+-"; "")) == $host)),

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 Unrelated windows can match

The new host match also accepts non-browser classes such as teams.microsoft.com or custom-teams.microsoft.com. If such a window appears before the sending web app, clicking the notification focuses that unrelated window instead.

Require a supported browser prefix and the __ separator. Add a test with an unrelated matching class before the web app.

(.[] | select(matches(.class))),
(.[] | select(.initialClass == "org.omarchy.agent" and matches(.initialTitle)))
).address // empty'
Expand Down
9 changes: 9 additions & 0 deletions shell/plugins/notifications/NotificationLogic.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ function sanitizeBody(body, app, appIcon) {
.replace(/^\s*(?:https?:\/\/|www\.)?(?:[a-z0-9-]+\.)+[a-z]{2,}(?::\d+)?(?:\/\S*)?\s+/i, "")
}

// Chromium-derived browsers lead a web notification's body with a link to the
// sending origin; its host is what they put in a web app's window class.
function webAppHost(app, appIcon, body) {
if (!isChromiumDerived(app, appIcon)) return ""
var match = /^\s*<a\b[^>]*\bhref="https?:\/\/([a-z0-9.-]+)[:\/"]/i.exec(String(body || ""))
return match ? match[1].toLowerCase() : ""
}

function summaryStartsWithGlyph(summary) {
var text = String(summary || "").replace(/^\s+/, "")
if (!text) return false
Expand Down Expand Up @@ -468,6 +476,7 @@ if (typeof module !== "undefined") {
isChromiumDerived: isChromiumDerived,
sanitizeBody: sanitizeBody,
styledBody: styledBody,
webAppHost: webAppHost,
summaryStartsWithGlyph: summaryStartsWithGlyph,
shouldBypassDnd: shouldBypassDnd,
isEphemeralApp: isEphemeralApp,
Expand Down
15 changes: 8 additions & 7 deletions shell/plugins/notifications/Service.qml
Original file line number Diff line number Diff line change
Expand Up @@ -426,19 +426,20 @@ Item {
// Chat apps (Slack, Discord, Vesktop, etc.) rarely register a "default"
// libnotify action — they just expect clicking the notification to
// focus their window. Fall back to focusing the sending app by class so
// that click-to-jump actually works.
if (!invoked) focusApp(entry)
// that click-to-jump actually works. Chromium's own default action raises
// no window here, so a web app's toast always focuses the web app.
var webAppHost = entry ? NotificationLogic.webAppHost(entry.app, entry.appIcon, entry.body) : ""
if (!invoked || webAppHost) focusApp(entry, webAppHost)
dismissPopup(index)
}

// Try to focus an existing Hyprland window matching the notification's
// sender. The helper handles case-insensitive class matching.
function focusApp(entry) {
function focusApp(entry, webAppHost) {
if (!entry || !entry.app) return
focusAppProc.command = [
service.omarchyPath + "/bin/omarchy-hyprland-focus-app",
String(entry.app)
]
var command = [service.omarchyPath + "/bin/omarchy-hyprland-focus-app", String(entry.app)]
if (webAppHost) command.push(webAppHost)
focusAppProc.command = command
focusAppProc.running = true
}

Expand Down
22 changes: 22 additions & 0 deletions test/shell.d/hyprland-focus-app-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,25 @@ fi
[[ ! -e $dispatch_log ]] || fail "app focus leaves focus unchanged for unrelated title matches"

pass "app focus restricts title matching to agent terminals"

clients_json='[
{"address":"0xunnamed","class":""},
{"address":"0xbrowser","class":"chromium"},
{"address":"0xlookalike","class":"chrome-not-teams.microsoft.com__-Default"},
{"address":"0xteams","class":"chrome-teams.microsoft.com__-Default"}
]'
PATH="$mock_bin:$PATH" OMARCHY_TEST_CLIENTS_JSON="$clients_json" \
OMARCHY_TEST_FOCUS_DISPATCH="$dispatch_log" \
bash "$ROOT/bin/omarchy-hyprland-focus-app" Chromium teams.microsoft.com

grep -F 'hl.dsp.focus({ window = "address:0xteams" })' "$dispatch_log" >/dev/null || \
fail "app focus prefers the web app window for the given host"

PATH="$mock_bin:$PATH" OMARCHY_TEST_CLIENTS_JSON="$clients_json" \
OMARCHY_TEST_FOCUS_DISPATCH="$dispatch_log" \
bash "$ROOT/bin/omarchy-hyprland-focus-app" Chromium example.com

grep -F 'hl.dsp.focus({ window = "address:0xbrowser" })' "$dispatch_log" >/dev/null || \
fail "app focus falls back to the app name when no web app window has the host"

pass "app focus finds a web app window by host before the browser"
25 changes: 25 additions & 0 deletions test/shell.d/notifications-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,27 @@ assert(notifications.isChromiumDerived('Brave Browser', ''), 'notifications dete
assert(notifications.isChromiumDerived('', 'microsoft-edge'), 'notifications detect chromium-derived apps by icon')
assert(!notifications.isChromiumDerived('Slack', ''), 'notifications do not treat unrelated apps as chromium-derived')

assertEqual(
notifications.webAppHost('Chromium', '', '<a href="https://teams.microsoft.com/">teams.microsoft.com</a>\n\nSender: hi'),
'teams.microsoft.com',
'notifications read the sending web app host from a chromium body'
)
assertEqual(
notifications.webAppHost('Brave Origin', '', '<a href="http://localhost:8765/">localhost:8765</a>\n\nhi'),
'localhost',
'notifications drop the port from a web app host, as the window class does'
)
assertEqual(
notifications.webAppHost('Slack', '', '<a href="https://example.com/">example.com</a>'),
'',
'notifications read no web app host from other senders'
)
assertEqual(
notifications.webAppHost('Chromium', '', 'See <a href="https://example.com/">this</a>'),
'',
'notifications read a web app host only from the leading origin link'
)

assertEqual(
notifications.sanitizeBody('<img src="x">Hello', 'Slack', ''),
'Hello',
Expand Down Expand Up @@ -691,6 +712,10 @@ assert(
/if \(!NotificationLogic\.popupRowChanged\(row, updated\)\) return/.test(serviceQml),
'notifications service leaves the row and its file alone when a refresh finds nothing changed'
)
assert(
/NotificationLogic\.webAppHost\(entry\.app, entry\.appIcon, entry\.body\)[\s\S]{0,80}?if \(!invoked \|\| webAppHost\) focusApp\(entry, webAppHost\)/.test(serviceQml),
'notifications service focuses the sending web app even after invoking the default action'
)
assert(
/popupModel\.insert\(0, snapshot\)[\s\S]{0,300}?service\.refreshPopup\(notification, snapshot\.originalId, snapshot\.timestamp\)/.test(serviceQml),
'notifications service catches up on an update that beat the deferred row insert'
Expand Down