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/main1534474c8): - 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/maind19afb4c9(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).
90 lines
2.5 KiB
Go
90 lines
2.5 KiB
Go
//go:build !nogui
|
|
|
|
package gui
|
|
|
|
import (
|
|
"errors"
|
|
"log"
|
|
"strings"
|
|
|
|
"github.com/wailsapp/wails/v3/pkg/application"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// errPanelKeyFailed is a refusal that isn't another app's hold: a key
|
|
// this keyboard has no code for, or no display to bind on.
|
|
var errPanelKeyFailed = errors.New("magpie couldn't take this shortcut")
|
|
|
|
// startPanelKey binds the saved shortcut once the app has started, and
|
|
// has Settings bind each one recorded after. A magpie this one took over
|
|
// from (before, its pid) may hold it until it has quit: refused, it is
|
|
// tried again then.
|
|
func (h *host) startPanelKey(before int) {
|
|
onPanelShortcut = h.bindPanelKey
|
|
go func() {
|
|
<-h.ready
|
|
s := settings.Load().PanelShortcut
|
|
if s == "" || !panelKeysState().Can {
|
|
return
|
|
}
|
|
if h.bindPanelKey(s) != nil && before != 0 {
|
|
awaitExit(before, handedOffWait)
|
|
if settings.Load().PanelShortcut == s {
|
|
_ = h.bindPanelKey(s)
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
|
|
// bindPanelKey binds s ("" for none) in place of the shortcut bound now.
|
|
// Refused, the one before is bound again, and a saved one refused is
|
|
// said in Settings (panelKeyTaken) until one is bound.
|
|
func (h *host) bindPanelKey(s string) error {
|
|
h.keyMu.Lock()
|
|
defer h.keyMu.Unlock()
|
|
gs := h.app.GlobalShortcut
|
|
old := h.panelKey
|
|
if old != "" && old != s {
|
|
if err := gs.Unregister(old); err != nil {
|
|
log.Println("panel shortcut:", err)
|
|
}
|
|
h.panelKey = ""
|
|
}
|
|
if s != "" && h.panelKey != s {
|
|
if err := gs.Register(s, h.panelKeyPressed); err != nil {
|
|
log.Println("panel shortcut:", err)
|
|
if old != "" && old != s && gs.Register(old, h.panelKeyPressed) == nil {
|
|
h.panelKey = old
|
|
}
|
|
if s == settings.Load().PanelShortcut {
|
|
panelKeyTaken.Store(s)
|
|
}
|
|
if strings.Contains(err.Error(), "already registered") {
|
|
return errPanelKeyTaken
|
|
}
|
|
return errPanelKeyFailed
|
|
}
|
|
h.panelKey = s
|
|
}
|
|
panelKeyTaken.Store("")
|
|
return nil
|
|
}
|
|
|
|
// dropPanelKey lets the shortcut go, for a Mac's magpie leaving the menu
|
|
// bar to the version it handed over to.
|
|
func (h *host) dropPanelKey() {
|
|
h.keyMu.Lock()
|
|
defer h.keyMu.Unlock()
|
|
if h.panelKey != "" {
|
|
_ = h.app.GlobalShortcut.Unregister(h.panelKey)
|
|
h.panelKey = ""
|
|
}
|
|
}
|
|
|
|
// panelKeyPressed opens the quick panel, or closes it, as a click on the
|
|
// tray icon does when it opens the panel. Wails calls it on a goroutine
|
|
// of its own; togglePanel goes to the main thread.
|
|
func (h *host) panelKeyPressed() {
|
|
h.whenReady(func() { application.InvokeAsync(h.togglePanel) })
|
|
}
|