Files
magpie/internal/gui/panelkey_test.go
T
yetone 3bebc38393 gui: a key combination opens the quick panel from any app (ben hanks)
ben hanks on Discord asked for a global shortcut that opens the menu bar
or tray panel. Settings › General has a new row, Quick panel shortcut,
with none set by default. Clicking its field records the next combination:
a letter, a digit, Space or F1–F20 with Ctrl, Alt/Option or Cmd/Win, or an
F key on its own. Esc or a click elsewhere stops recording, and the cross
clears the shortcut. It is shown the system's way (⇧⌘K on a Mac,
Ctrl+Alt+K elsewhere).

POST /api/settings/panel-shortcut checks the combination, has the running
app bind it through Wails' GlobalShortcut (Carbon hot keys on the Mac,
RegisterHotKey on Windows, XGrabKey on X11), and saves it only once it is
bound. One the system refuses is not saved, the old one stays bound, and
the status line says why. A pressed shortcut toggles the panel as a tray
click does. The shortcut belongs to each computer (KeepOwn), the Settings
page's own save leaves it alone, and the old Mac process lets go of it as
it hands over. On Wayland no app may grab keys, so the field is turned off
and the row names the command (`magpie panel`) to bind in the desktop's
own keyboard settings.

Every string is in en, zh, zh-TW, ja and de. No size budget was raised:
app.js is 1,315,252 of its 1,315,316 after older comments were cut.

Run at this head (rebased on origin/main 1534474c8):
- mote (macOS 27): gofmt; go vet for darwin, GOOS=linux -tags nogui and
  GOOS=windows on internal/gui and internal/settings; go build ./...:
  ok. go test: settings ok; gui ok except TestMainWindowCorners/
  macOS_26_SDK, red on macOS 27 already (#3949).
- Windows (xiaomi-gpu, win-run): go test internal/gui internal/settings.
  settings ok; gui has 5 reds, the same 5 on origin/main d19afb4c9
  (TestGatewayHeldByAContainer #2107/#3649; TestLodyImportHandoff,
  TestImportStatusUnreadableIsUnknown, TestAionUiImportRow and
  TestUpdateThatWontStart #4115).
- Linux (WSL magpie-desktop, wsl-run -tags nogui): internal/gui (the
  Wayland branch included) and internal/settings: ok.
- GUI: panel-shortcut.test.cjs (new) in Chromium and WebKit, in all 5
  languages, faking both a Mac and Windows: ok. Whole suite (make test-ui)
  on mote: unfinished at the push. Three other sessions' suites were on
  the same box (load 130 to 250), and after 1605 tests it was still in
  the "c" files. Its reds so far are agent-panel-name-width and
  connect-snippet-shell and context-map (each on #4304), and allow-ips
  zh, which is red alone on origin/main too (#4357).

Broken by hand, each red:
- leaving the shortcut out of the general Settings save:
  TestPanelShortcutSetting "the Settings page's save changed the
  shortcut";
- dropping KeepOwn's line: "a synced setup took this computer's shortcut";
- dropping the recorder's stopImmediatePropagation: panel-shortcut.test.cjs
  "a key typing needs was sent, or a window key ran".
TestSettingsSaveKeepsWhatItDoesNotSend now sets the new field.

Real build on mote: a dev build in a sandbox HOME bound a saved
Ctrl+Option+Shift+Cmd+F18 as it started, and a POST of F16 saved it and
bound it.

Not tried:
- a real key press on any system: mote gives ssh no Accessibility access
  to post events, and Windows' ssh session has no desktop;
- the Windows and X11 refusal of a combination another app holds (only
  the fake). On the Mac, Carbon takes a combination a second app
  registered too, so both apps get it and magpie can't say so. This was
  checked on mote with a helper holding F17;
- Wails' GlobalShortcuts portal on Wayland, which isn't used;
- a Linux GUI-tag build (WSL has no gtk4/webkitgtk-6.0).
2026-10-30 12:06:33 +08:00

134 lines
5.0 KiB
Go

package gui
import (
"encoding/json"
"net/http"
"net/http/httptest"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/yetone/magpie/internal/settings"
)
func TestPanelShortcutNames(t *testing.T) {
for _, c := range []struct{ in, goos, want string }{
{"", "darwin", ""},
{"Cmd+Shift+m", "darwin", "Shift+Cmd+M"},
{"Option+Ctrl+Space", "darwin", "Ctrl+Option+Space"},
{"Super+Alt+k", "linux", "Alt+Super+K"},
{"Cmd+K", "windows", "Super+K"},
{"Option+1", "windows", "Alt+1"},
{"F5", "windows", "F5"},
{"Shift+F20", "linux", "Shift+F20"},
} {
got, err := panelShortcut(c.in, c.goos)
if err != nil || got != c.want {
t.Errorf("panelShortcut(%q, %s) = %q, %v; want %q", c.in, c.goos, got, err, c.want)
}
}
// a key typing needs, a key no system's table has, a made-up modifier
for _, in := range []string{"K", "Shift+K", "Ctrl+Tab", "Ctrl+F21", "Ctrl+", "Hyper+K", "Ctrl+KK", "Cmd+;"} {
if got, err := panelShortcut(in, "darwin"); err == nil {
t.Errorf("panelShortcut(%q) = %q, want refused", in, got)
}
}
}
func TestWaylandSession(t *testing.T) {
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
want bool
}{
{"linux", map[string]string{"XDG_SESSION_TYPE": "wayland"}, true},
{"linux", map[string]string{"XDG_SESSION_TYPE": "x11", "WAYLAND_DISPLAY": "wayland-1"}, false},
{"linux", map[string]string{"WAYLAND_DISPLAY": "wayland-1"}, true},
{"linux", map[string]string{}, false},
{"darwin", map[string]string{"XDG_SESSION_TYPE": "wayland"}, false},
} {
if got := waylandSession(c.goos, env(c.env)); got != c.want {
t.Errorf("waylandSession(%s, %v) = %v", c.goos, c.env, got)
}
}
if got := shellWord("/opt/My Apps/magpie"); got != `'/opt/My Apps/magpie'` {
t.Errorf("shellWord: %s", got)
}
}
// The shortcut is saved only once the app has it; one the system refuses
// is not saved and the old one stays bound; the Settings page's own save
// and a sync's leave it as it was.
func TestPanelShortcutSetting(t *testing.T) {
h := t.TempDir()
t.Setenv("HOME", h)
t.Setenv("USERPROFILE", h)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
t.Setenv("XDG_SESSION_TYPE", "x11")
var bound []string
refuse := ""
was := onPanelShortcut
onPanelShortcut = func(s string) error {
if s != "" && s == refuse {
return errPanelKeyTaken
}
bound = append(bound, s)
return nil
}
t.Cleanup(func() { onPanelShortcut = was })
srv := Handler(&zoomWindows{}, nil)
post := func(path, body string) *httptest.ResponseRecorder {
rec := httptest.NewRecorder()
srv.ServeHTTP(rec, httptest.NewRequest("POST", path, strings.NewReader(body)))
return rec
}
rec := post("/api/settings/panel-shortcut", `{"shortcut":"Ctrl+Alt+Shift+K"}`)
if rec.Code != http.StatusOK {
t.Fatalf("%d %s", rec.Code, rec.Body)
}
key, _ := panelShortcut("Ctrl+Alt+Shift+K", runtime.GOOS)
var got struct {
PanelShortcut string
PanelKeys panelKeysJSON
}
if json.Unmarshal(rec.Body.Bytes(), &got); got.PanelShortcut != key || settings.Load().PanelShortcut != key || !got.PanelKeys.Can {
t.Fatalf("answered %+v, saved %q", got, settings.Load().PanelShortcut)
}
if len(bound) != 1 || bound[0] != key {
t.Fatalf("bound %v", bound)
}
refuse = "Ctrl+J"
if rec := post("/api/settings/panel-shortcut", `{"shortcut":"Ctrl+J"}`); rec.Code == http.StatusOK || !strings.Contains(rec.Body.String(), errPanelKeyTaken.Error()) || settings.Load().PanelShortcut != key {
t.Fatalf("a refused shortcut: %d %s, saved %q", rec.Code, rec.Body, settings.Load().PanelShortcut)
}
if rec := post("/api/settings/panel-shortcut", `{"shortcut":"Shift+K"}`); rec.Code == http.StatusOK || settings.Load().PanelShortcut != key || len(bound) != 1 {
t.Fatalf("a key typing needs was taken: %d", rec.Code)
}
if rec := post("/api/settings", `{"theme":"dark","lang":"en"}`); rec.Code != http.StatusOK || settings.Load().PanelShortcut != key {
t.Fatalf("the Settings page's save changed the shortcut: %d %q", rec.Code, settings.Load().PanelShortcut)
}
in := settings.Settings{}
in.KeepOwn(settings.Load())
if in.PanelShortcut != key {
t.Fatalf("a synced setup took this computer's shortcut: %q", in.PanelShortcut)
}
if rec := post("/api/settings/panel-shortcut", `{"shortcut":""}`); rec.Code != http.StatusOK || settings.Load().PanelShortcut != "" || bound[len(bound)-1] != "" {
t.Fatalf("clearing: %d, saved %q, bound %v", rec.Code, settings.Load().PanelShortcut, bound)
}
// a Wayland desktop binds it itself: nothing is taken, and the row is
// told the command
if runtime.GOOS != "linux" {
return
}
t.Setenv("XDG_SESSION_TYPE", "wayland")
if rec := post("/api/settings/panel-shortcut", `{"shortcut":"Ctrl+K"}`); rec.Code == http.StatusOK || settings.Load().PanelShortcut != "" || len(bound) != 2 {
t.Fatalf("taken on Wayland: %d, bound %v", rec.Code, bound)
}
if st := panelKeysState(); st.Can || !strings.HasSuffix(st.Command, " panel") {
t.Fatalf("Wayland's state: %+v", st)
}
}