Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

gui: a Linux alert closed with its × no longer opens the main window over the desktop (#2513)

yetone60a42875cd3c
diff --git a/internal/gui/quota_alert_notify.go b/internal/gui/quota_alert_notify.go
index 98917f689..fcfbb4f2e 100644
--- a/internal/gui/quota_alert_notify.go
+++ b/internal/gui/quota_alert_notify.go
@@ -6,6 +6,7 @@ import (
"context"
"fmt"
"log"
+ "runtime"
"strings"
"sync"
"sync/atomic"
@@ -44,13 +45,13 @@ func (n *notifier) start() error {
log.Println("notifications:", err)
return
}
- // a click on an alert opens the Usage page; on a turn's end, where
- // the user goes is their agent, not magpie
+ if runtime.GOOS == "linux" {
+ _ = ns.RegisterNotificationCategory(clickCategory)
+ }
ns.OnNotificationResponse(func(r notifications.NotificationResult) {
- if strings.HasPrefix(r.Response.ID, turnNotice) {
- return
+ if opensUsage(runtime.GOOS, r.Response) {
+ n.h.whenReady(func() { application.InvokeAsync(func() { n.h.ShowMain("usage") }) })
}
- n.h.whenReady(func() { application.InvokeAsync(func() { n.h.ShowMain("usage") }) })
})
n.ns = ns
})
@@ -100,12 +101,50 @@ func (n *notifier) sendAs(kind, title, body string) {
if !n.allowed() {
return
}
- id := fmt.Sprintf("%s%d", kind, time.Now().UnixNano())
- if err := n.ns.SendNotification(notifications.NotificationOptions{ID: id, Title: title, Body: body}); err != nil {
+ o := notice(runtime.GOOS, fmt.Sprintf("%s%d", kind, time.Now().UnixNano()), title, body)
+ send := n.ns.SendNotification
+ if o.CategoryID != "" {
+ send = n.ns.SendNotificationWithActions
+ }
+ if err := send(o); err != nil {
log.Println("notifications:", err)
}
}
+// linuxClick is the freedesktop action key a click on a notification's body
+// invokes, and clickCategory gives an action that id. Wails reports an
+// action of a category under its own id, and maps "default" to
+// DefaultActionIdentifier only without one; it reports a Linux
+// notification closed by the user, with its × or cleared from the list, as
+// DefaultActionIdentifier too. Under clickCategory the two come back apart.
+const linuxClick = "default"
+
+var clickCategory = notifications.NotificationCategory{
+ ID: "magpie-click",
+ Actions: []notifications.NotificationAction{{ID: linuxClick, Title: "Open"}},
+}
+
+// notice is the notification sent as id: on Linux under clickCategory.
+func notice(goos, id, title, body string) notifications.NotificationOptions {
+ o := notifications.NotificationOptions{ID: id, Title: title, Body: body}
+ if goos == "linux" {
+ o.CategoryID = clickCategory.ID
+ }
+ return o
+}
+
+// opensUsage says whether a response to a notification opens the Usage
+// page: a click on an alert does. On a turn's end, where the user goes is
+// their agent, not magpie. On Linux only a click (linuxClick) is one: an
+// alert dismissed there opened the main window, maximised if it was left
+// so, over the desktop with the focus (#2513).
+func opensUsage(goos string, r notifications.NotificationResponse) bool {
+ if strings.HasPrefix(r.ID, turnNotice) {
+ return false
+ }
+ return goos != "linux" || r.ActionIdentifier == linuxClick
+}
+
// watchAlerts tells the user, with a notification, of a window used past
// the share or a balance fallen to the amount set in Settings (#368).
func (h *host) watchAlerts() {
diff --git a/internal/gui/quota_alert_notify_test.go b/internal/gui/quota_alert_notify_test.go
new file mode 100644
index 000000000..02ad16f3d
--- /dev/null
+++ b/internal/gui/quota_alert_notify_test.go
@@ -0,0 +1,48 @@
+//go:build !nogui
+
+package gui
+
+import (
+ "testing"
+
+ "github.com/wailsapp/wails/v3/pkg/services/notifications"
+)
+
+// A Linux alert dismissed with its × opened the main window over the
+// desktop with the focus (#2513): Wails reports the dismissal as
+// DefaultActionIdentifier, so there only the click's own id opens Usage.
+func TestDismissedAlertOpensNothing(t *testing.T) {
+ alert, turn := "magpie-alert-1", turnNotice+"1"
+ for _, c := range []struct {
+ goos, id, action string
+ want bool
+ }{
+ {"linux", alert, notifications.DefaultActionIdentifier, false}, // dismissed
+ {"linux", alert, linuxClick, true}, // clicked
+ {"linux", turn, linuxClick, false},
+ {"darwin", alert, notifications.DefaultActionIdentifier, true},
+ {"windows", alert, notifications.DefaultActionIdentifier, true},
+ {"windows", turn, notifications.DefaultActionIdentifier, false},
+ } {
+ r := notifications.NotificationResponse{ID: c.id, ActionIdentifier: c.action}
+ if got := opensUsage(c.goos, r); got != c.want {
+ t.Errorf("opensUsage(%s, %s %s) = %v, want %v", c.goos, c.id, c.action, got, c.want)
+ }
+ }
+}
+
+// The click comes back as linuxClick only from a notification sent under
+// clickCategory, whose action has that id.
+func TestLinuxAlertSentUnderClickCategory(t *testing.T) {
+ if o := notice("linux", "magpie-alert-1", "t", "b"); o.CategoryID != clickCategory.ID {
+ t.Fatalf("Linux alert's category = %q, want %q", o.CategoryID, clickCategory.ID)
+ }
+ if a := clickCategory.Actions; len(a) != 1 || a[0].ID != linuxClick || linuxClick != "default" {
+ t.Fatalf("clickCategory's actions = %+v, want one with the body's key %q", a, "default")
+ }
+ for _, goos := range []string{"darwin", "windows"} {
+ if o := notice(goos, "magpie-alert-1", "t", "b"); o.CategoryID != "" {
+ t.Errorf("%s alert's category = %q, want none", goos, o.CategoryID)
+ }
+ }
+}