From dad2787f55e575e59ccca4a8d375444d38e8c5df Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 27 Sep 2026 16:14:52 +0800 Subject: [PATCH] =?UTF-8?q?settings:=20the=20Dock=20can=20follow=20the=20w?= =?UTF-8?q?indow=20=E2=80=94=20Hide,=20With=20window,=20Show.=20With=20win?= =?UTF-8?q?dow,=20magpie=20is=20in=20the=20Dock=20(and=20Cmd-Tab)=20while?= =?UTF-8?q?=20its=20window=20is=20open=20and=20leaves=20it=20when=20the=20?= =?UTF-8?q?window=20is=20closed,=20so=20it=20can=20be=20switched=20to=20wi?= =?UTF-8?q?thout=20an=20icon=20kept=20there=20the=20rest=20of=20the=20time?= =?UTF-8?q?.=20Tested=20on=20a=20sandboxed=20bundle:=20shown=20at=20start,?= =?UTF-8?q?=20gone=20on=20close,=20back=20on=20reopen;=20Show=20and=20Hide?= =?UTF-8?q?=20behave=20as=20before.=20Issue=20#101?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/backup/backup.go | 4 ++-- internal/davsync/davsync.go | 2 +- internal/gui/api.go | 10 +++++----- internal/gui/app.go | 12 +++++++++++- internal/gui/assets/app.js | 5 +++-- internal/gui/assets/i18n.js | 3 ++- internal/gui/assets/index.html | 2 +- internal/gui/glide_darwin.go | 18 +++++++++++------- internal/gui/glide_other.go | 2 +- internal/settings/settings.go | 4 ++++ 10 files changed, 41 insertions(+), 21 deletions(-) diff --git a/internal/backup/backup.go b/internal/backup/backup.go index 9e77b04c1..e0eef0954 100644 --- a/internal/backup/backup.go +++ b/internal/backup/backup.go @@ -49,7 +49,7 @@ type Bundle struct { App string `json:"app,omitempty"` // the magpie that made it Keys bool `json:"keys"` // whether the providers carry their keys Providers []provider.Provider `json:"providers"` - Icons map[string][]byte `json:"icons,omitempty"` // pictures picked for providers, by file name + Icons map[string][]byte `json:"icons,omitempty"` // pictures picked for providers, by file name Groups []provider.Group `json:"groups,omitempty"` // the user's model groups Settings *settings.Settings `json:"settings,omitempty"` Profiles map[string]profile.Profile `json:"profiles,omitempty"` @@ -237,7 +237,7 @@ func Restore(b Bundle, parts Parts) (Result, error) { if parts.Settings && b.Settings != nil { // the window's size and the proxy are this machine's own s, cur := *b.Settings, settings.Load() - s.Window, s.Proxy, s.Dock = cur.Window, cur.Proxy, cur.Dock + s.Window, s.Proxy, s.Dock, s.DockWindow = cur.Window, cur.Proxy, cur.Dock, cur.DockWindow if err := settings.Save(s); err != nil { return r, err } diff --git a/internal/davsync/davsync.go b/internal/davsync/davsync.go index b9e607e4e..7603727b2 100644 --- a/internal/davsync/davsync.go +++ b/internal/davsync/davsync.go @@ -261,7 +261,7 @@ func hashes(b backup.Bundle) map[string]string { if b.Settings != nil { s = *b.Settings } - s.Window, s.Proxy, s.Dock = nil, "", false // this computer's own: never synced + s.Window, s.Proxy, s.Dock, s.DockWindow = nil, "", false, false // this computer's own: never synced return map[string]string{ "providers": h([]any{b.Providers, b.Icons, b.Groups}), "settings": h(s), diff --git a/internal/gui/api.go b/internal/gui/api.go index 838f90996..bff9dd728 100644 --- a/internal/gui/api.go +++ b/internal/gui/api.go @@ -126,9 +126,9 @@ func settingsState() settingsJSON { return s } -// onDock puts the app in the Mac's Dock or takes it out, when the Settings -// page changes that; set by the process that has the app. -var onDock func(bool) +// onDock puts the app in the Mac's Dock or takes it out as the settings say, +// when the Settings page changes them; set by the process that has the app. +var onDock func(settings.Settings) // assetTypes are the page's own files' types. The file server takes them // from Windows' registry, which another program may have changed — an SVG @@ -312,8 +312,8 @@ func Handler(w Windows, gw *gateway.Server) http.Handler { fail(rw, err) return } - if in.Dock != cur.Dock && onDock != nil { - onDock(in.Dock) + if (in.Dock != cur.Dock || in.DockWindow != cur.DockWindow) && onDock != nil { + onDock(in) } writeJSON(rw, settingsState()) }) diff --git a/internal/gui/app.go b/internal/gui/app.go index 98f22a3ca..a266a933e 100644 --- a/internal/gui/app.go +++ b/internal/gui/app.go @@ -66,6 +66,7 @@ func (h *host) ShowMain(view string) { if view != "" { h.main.SetURL("/?view=" + view + h.query) } + h.dock(settings.Load(), true) h.main.Show() h.main.Focus() } @@ -76,10 +77,18 @@ func (h *host) Import(link string) { h.whenReady(func() { h.panel.Hide() h.main.SetURL("/?view=providers&import=" + id + h.query) + h.dock(settings.Load(), true) h.main.Show() h.main.Focus() }) } + +// dock puts magpie in the Dock or takes it out as s says, with the window +// shown or not: always, never, or while the window is. +func (h *host) dock(s settings.Settings, shown bool) { + setDock(s.Dock || s.DockWindow && shown, shown) +} + func (h *host) Quit() { h.app.Quit() } func (h *host) OpenURL(url string) { _ = h.app.Browser.OpenURL(url) } func (h *host) OpenFolder(path string) error { return openFolder(h.app, path) } @@ -152,7 +161,7 @@ func Run(version string, showMain bool, link string) error { ErrorHandler: func(err error) { log.Println("magpie:", err) }, }) - onDock = setDock + onDock = func(s settings.Settings) { h.dock(s, h.main.IsVisible()) } // The Dock icon opens the window. Wails would show every hidden window // on it, the panel too, so the hook answers first and stops it. h.app.Event.RegisterApplicationEventHook(events.Mac.ApplicationShouldHandleReopen, func(e *application.ApplicationEvent) { @@ -229,6 +238,7 @@ func Run(version string, showMain bool, link string) error { // Closing the window keeps the tray alive; quitting is a menu action. h.main.RegisterHook(events.Common.WindowClosing, func(e *application.WindowEvent) { h.main.Hide() + h.dock(settings.Load(), false) e.Cancel() }) diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index 67ed08568..c44bb36e4 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -3963,14 +3963,15 @@ const DISCORD_SVG = ' [id, t(name)]), s.theme, (theme) => savePrefs({ ...keep, theme }))); $("#langSegs").replaceChildren(segs(LOCALES.map(([id, name]) => [id, t(name)]), s.lang, (lang) => savePrefs({ ...keep, lang }))); $("#traySegs").replaceChildren(segs(TRAYS.map(([id, name]) => [id, t(name)]), s.tray || "panel", (tray) => savePrefs({ ...keep, tray }))); // the Dock is the Mac's $("#dockRow").hidden = !document.body.classList.contains("mac"); - $("#dockSegs").replaceChildren(segs([["off", t("Hide")], ["on", t("Show")]], s.dock ? "on" : "off", (v) => savePrefs({ ...keep, dock: v === "on" }))); + $("#dockSegs").replaceChildren(segs([["off", t("Hide")], ["window", t("With window")], ["on", t("Show")]], + s.dock ? "on" : s.dockWindow ? "window" : "off", (v) => savePrefs({ ...keep, dock: v === "on", dockWindow: v === "window" }))); // the system's record, set on its own, not with the other choices $("#loginSegs").replaceChildren(segs([["off", t("Off")], ["on", t("On")]], s.login ? "on" : "off", (v) => api("settings/login", { on: v === "on" }).then((ns) => { prefs = ns; renderSettings(); }).catch((e) => { status(t(e.message), "err"); renderSettings(); }))); diff --git a/internal/gui/assets/i18n.js b/internal/gui/assets/i18n.js index c6d9a2066..1521a776c 100644 --- a/internal/gui/assets/i18n.js +++ b/internal/gui/assets/i18n.js @@ -62,7 +62,8 @@ const I18N = { "Open at login": "开机自启", "Start magpie in the tray when you log in, so agents find the gateway from the start": "登录系统时在托盘里启动 magpie,各个 Agent 一开始就能连上网关", "move magpie to Applications first, then turn this on": "请先把 magpie 移到「应用程序」文件夹,再打开这个选项", - "Keep magpie in the Dock too, for a menu bar too full to show its icon": "让 magpie 同时常驻程序坞,菜单栏图标被挤掉时也能打开", + "Keep magpie in the Dock too, always or while its window is open, for Cmd-Tab and a menu bar too full to show its icon": "让 magpie 也出现在程序坞:常驻,或只在窗口打开时显示,方便 Cmd-Tab 切换,菜单栏图标被挤掉时也能打开", + "With window": "跟随窗口", "What clicking magpie’s icon in the menu bar or tray opens": "点击菜单栏或托盘里的 magpie 图标时打开什么", "Quick panel": "快捷面板", "Main window": "主窗口", diff --git a/internal/gui/assets/index.html b/internal/gui/assets/index.html index 5f04909fa..bbe101793 100644 --- a/internal/gui/assets/index.html +++ b/internal/gui/assets/index.html @@ -125,7 +125,7 @@
diff --git a/internal/gui/glide_darwin.go b/internal/gui/glide_darwin.go index 1f5a207a2..da9cf10c5 100644 --- a/internal/gui/glide_darwin.go +++ b/internal/gui/glide_darwin.go @@ -78,12 +78,15 @@ static int tintPanel(void *w, int r, int g, int b, int a, int ms) { } // A Dock icon is the Regular activation policy, none is Accessory. Leaving -// the Dock deactivates the app, so it is brought back to the front after, -// with the window the Settings page is in. -static void setDock(int on) { +// the Dock deactivates the app, so with front set it is brought back to the +// front after, with the window the Settings page is in; a window just closed +// leaves it where it is. +static void setDock(int on, int front) { dispatch_async(dispatch_get_main_queue(), ^{ - [NSApp setActivationPolicy:on ? NSApplicationActivationPolicyRegular : NSApplicationActivationPolicyAccessory]; - if (!on) [NSApp activateIgnoringOtherApps:YES]; + NSApplicationActivationPolicy p = on ? NSApplicationActivationPolicyRegular : NSApplicationActivationPolicyAccessory; + if ([NSApp activationPolicy] == p) return; + [NSApp setActivationPolicy:p]; + if (front) [NSApp activateIgnoringOtherApps:YES]; }); } */ @@ -119,8 +122,9 @@ func dockPolicy(on bool) application.ActivationPolicy { return application.ActivationPolicyAccessory } -// setDock shows magpie in the Dock or takes it out, at once. -func setDock(on bool) { C.setDock(C.int(boolInt(on))) } +// setDock shows magpie in the Dock or takes it out, at once, bringing it to +// the front after when front. +func setDock(on, front bool) { C.setDock(C.int(boolInt(on)), C.int(boolInt(front))) } func boolInt(b bool) int { if b { diff --git a/internal/gui/glide_other.go b/internal/gui/glide_other.go index fc24e79cb..110cb6e49 100644 --- a/internal/gui/glide_other.go +++ b/internal/gui/glide_other.go @@ -6,7 +6,7 @@ import "github.com/wailsapp/wails/v3/pkg/application" // The Dock is the Mac's; elsewhere magpie stays in the tray. func dockPolicy(bool) application.ActivationPolicy { return application.ActivationPolicyAccessory } -func setDock(bool) {} +func setDock(bool, bool) {} // glidePanel steps the shown panel to height a frame at a time, keeping it // by the tray icon as it goes. diff --git a/internal/settings/settings.go b/internal/settings/settings.go index 76d124737..904318892 100644 --- a/internal/settings/settings.go +++ b/internal/settings/settings.go @@ -25,6 +25,10 @@ type Settings struct { // Dock keeps magpie in the Mac's Dock as well as the menu bar, for a // menu bar too full to show its icon. Dock bool `json:"dock,omitempty"` + // DockWindow shows it in the Dock only while its window is open, so + // Cmd-Tab reaches the window without an icon kept there the rest of + // the time. Dock wins over it. + DockWindow bool `json:"dockWindow,omitempty"` // Proxy for magpie's own requests to vendors: "" follows the // environment and then the system, "direct" uses none, anything else // is the proxy (http://, https:// or socks5://; host:port means http).