Files
magpie/internal/gui/ontop_test.go
T
yetone 139a6be2bb gui: a pin in the title bar keeps the main window on top (NovaStudiosss on X)
NovaStudiosss wanted to watch Usage and Routing while working in other
apps. The main window's toolbar now has a pin between Refresh and
Settings. A click keeps the window above other apps' windows
(Wails SetAlwaysOnTop through POST /api/window/ontop?on=1|0) and saves
it as settings.json's onTop, which this computer keeps for itself
(KeepOwn, not synced). makeMain opens the window pinned on the next
start, and boot.js shows the pin pressed before the first paint.
There is no pin in the panel, in a browser's page (magpie web) or
under Wayland, where always-on-top is ignored (GDK_BACKEND=x11 keeps
it). A refused pin goes back up. Tooltips "Keep on top" and "Stop
keeping on top" are in every language. A Settings save keeps onTop
(settingsapi.Merge), which a test showed was lost before.

Run (rebased on a80a920f5):
- go test -tags nogui ./internal/gui ./internal/settings
  ./internal/settingsapi on mica: ok. TestPinKeepsWindowOnTop,
  TestPinRefusedWithoutAWindowToPin, TestSetOnTop*, TestOnTopOwn and
  the settings-save keep test.
- At the rebased head, TestPinRefused had served webHost through
  Handler and left webPage set, which turned TestSessionTitlesName-
  OtherAgents and TestCursorEditorChatsAreNotCalledCursorCLI red in a
  full package run. It now goes through webPageHandler, and the package
  is ok. TestWebPageHandlerEveryWhere misses a webHost passed in a
  variable.
- window-pin.test.cjs (chromium + webkit, en zh zh-TW ja de) and
  i18n-keys: 36 pass.
- Full GUI suite on mote (pre-rebase tree, still running at push): about
  380 reds so far, none in window-pin. Seven of the red files
  (account-arrange, agent-details-ready, api-key-theme, bark,
  agent-switch-accounts, allow-ips, agent-bulk-visibility) were run on
  origin/main 30de93e9e: 77 of the same reds, and none red only here.
- go vet for darwin, windows, linux nogui and linux gtk3. Windows
  go test ./internal/gui ./internal/settings: ok. gofmt clean.
- Broken by hand: without the route, or without renderOnTop, the Go
  and GUI tests go red. Without the Merge line the settings-save test
  goes red.
- Real app on mote: with onTop saved, the main window opens at the
  floating level (CGWindowList layer 3, 0 unpinned).

Not tried: clicking the pin in the real app (no Accessibility over
ssh), a real Windows or Linux window, Wayland (hidden by design). At
the default 660px window, the English tabs now scroll about 15px, as
Japanese and German already did on main.
2026-11-15 23:05:56 +08:00

135 lines
4.2 KiB
Go

package gui
import (
"encoding/json"
"net/http"
"net/http/httptest"
"runtime"
"strings"
"testing"
"github.com/yetone/magpie/internal/settings"
)
// pinHost is the app's host as the pin sees it: a window that can be
// kept on top, and what it was last told.
type pinHost struct {
Windows
on *[]bool
}
func (h pinHost) SetOnTop(on bool) { *h.on = append(*h.on, on) }
// tabPinHost is a browser's page in front of a host that could pin: the
// app's page served to a browser.
type tabPinHost struct{ pinHost }
func (tabPinHost) browserTab() {}
// The title bar's pin (NovaStudiosss on X) puts the main window on top at
// once and saves it, so the window is made pinned after a restart
// (makeMain reads settings.OnTop); unpinning does both back. The settings
// state says the pin can be used.
func TestPinKeepsWindowOnTop(t *testing.T) {
sandboxHome(t)
if !canOnTop(runtime.GOOS, func(string) string { return "" }) {
t.Skip("no desktop that can pin here")
}
t.Setenv("XDG_SESSION_TYPE", "")
t.Setenv("WAYLAND_DISPLAY", "")
var told []bool
h := Handler(pinHost{on: &told}, nil)
post := func(url string) {
t.Helper()
r := httptest.NewRecorder()
h.ServeHTTP(r, httptest.NewRequest("POST", url, nil))
if r.Code != 200 {
t.Fatalf("%s: %d %s", url, r.Code, r.Body)
}
var out struct{ OnTop bool }
if err := json.Unmarshal(r.Body.Bytes(), &out); err != nil {
t.Fatal(err)
}
if want := url == "/api/window/ontop?on=1"; out.OnTop != want {
t.Fatalf("%s answered onTop %v", url, out.OnTop)
}
}
post("/api/window/ontop?on=1")
if len(told) != 1 || !told[0] {
t.Fatalf("the window was told %v, want [true]", told)
}
if !settings.Load().OnTop {
t.Fatal("the pin wasn't saved for the next start")
}
if boot := bootOf(t, h); boot["pin"] != true || boot["onTop"] != true {
t.Fatalf("boot.js doesn't show the pin pressed: %v", boot)
}
post("/api/window/ontop?on=0")
if len(told) != 2 || told[1] || settings.Load().OnTop {
t.Fatalf("unpinned: told %v, saved %v", told, settings.Load().OnTop)
}
if boot := bootOf(t, h); boot["pin"] != true || boot["onTop"] != false {
t.Fatalf("boot.js doesn't show the pin up: %v", boot)
}
}
// bootOf is what boot.js tells the page.
func bootOf(t *testing.T, h http.Handler) map[string]any {
t.Helper()
r := httptest.NewRecorder()
h.ServeHTTP(r, httptest.NewRequest("GET", "/boot.js", nil))
body := r.Body.String()
i, j := strings.Index(body, "{"), strings.Index(body, "};")
if i < 0 || j < i {
t.Fatalf("boot.js: %.300s", body)
}
var out map[string]any
if err := json.Unmarshal([]byte(body[i:j+1]), &out); err != nil {
t.Fatalf("boot.js: %v in %.300s", err, body)
}
return out
}
// A browser's page (magpie web, or the app's page served to a browser)
// has no window of its own to pin, and a Wayland session can't keep one
// on top: the pin is refused there and nothing is saved.
func TestPinRefusedWithoutAWindowToPin(t *testing.T) {
sandboxHome(t)
var told []bool
for name, w := range map[string]Windows{
"magpie web": webHost{},
"no pin to make": folderOnly{},
"browser tab": tabPinHost{pinHost{on: &told}},
} {
r := httptest.NewRecorder()
webPageHandler(t, w, nil).ServeHTTP(r, httptest.NewRequest("POST", "/api/window/ontop?on=1", nil))
if r.Code != 400 {
t.Fatalf("%s: %d %s, want refused", name, r.Code, r.Body)
}
if boot := bootOf(t, webPageHandler(t, w, nil)); boot["pin"] != nil {
t.Fatalf("%s: boot.js shows the pin: %v", name, boot)
}
}
if len(told) != 0 || settings.Load().OnTop {
t.Fatalf("a refused pin reached the window (%v) or was saved (%v)", told, settings.Load().OnTop)
}
env := func(m map[string]string) func(string) string { return func(k string) string { return m[k] } }
for _, c := range []struct {
goos string
env map[string]string
can bool
}{
{"darwin", nil, true},
{"windows", nil, true},
{"linux", map[string]string{"XDG_SESSION_TYPE": "x11"}, true},
{"linux", map[string]string{"XDG_SESSION_TYPE": "wayland"}, false},
{"linux", map[string]string{"WAYLAND_DISPLAY": "wayland-0"}, false},
{"linux", map[string]string{"XDG_SESSION_TYPE": "wayland", "GDK_BACKEND": "x11"}, true},
} {
if got := canOnTop(c.goos, env(c.env)); got != c.can {
t.Errorf("canOnTop(%s, %v) = %v, want %v", c.goos, c.env, got, c.can)
}
}
}