From 08d1e71aff53fa436d83af290e7f936e319c6f2b Mon Sep 17 00:00:00 2001 From: yetone Date: Fri, 25 Sep 2026 19:00:10 +0800 Subject: [PATCH] =?UTF-8?q?gui:=20the=20window=20opens=20at=20the=20size?= =?UTF-8?q?=20it=20was=20last=20given=20=E2=80=94=20a=20resize=20is=20kept?= =?UTF-8?q?=20once=20it=20settles,=20in=20settings.json,=20unless=20the=20?= =?UTF-8?q?window=20is=20maximised,=20full=20screen=20or=20minimised?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/gui/api.go | 3 ++- internal/gui/app.go | 42 +++++++++++++++++++++++++++++++++-- internal/settings/settings.go | 3 +++ 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/internal/gui/api.go b/internal/gui/api.go index 79f034ec3..8f3258142 100644 --- a/internal/gui/api.go +++ b/internal/gui/api.go @@ -220,9 +220,10 @@ func Handler(w Windows, gw *gateway.Server) http.Handler { return } // the Settings page sends its own choices; how the agents are - // arranged is the Agents page's, and stays as it is + // arranged is the Agents page's, and the window's size its own; both stay as they are cur := settings.Load() in.AgentOrder, in.AgentsHidden, in.AgentsShown = cur.AgentOrder, cur.AgentsHidden, cur.AgentsShown + in.Window = cur.Window // the window's own, as it was last resized if err := settings.Save(in); err != nil { fail(rw, err) return diff --git a/internal/gui/app.go b/internal/gui/app.go index 8a43f9a91..5c5773498 100644 --- a/internal/gui/app.go +++ b/internal/gui/app.go @@ -164,12 +164,17 @@ func Run(version string, showMain bool, link string) error { Windows: application.WindowsWindow{HiddenOnTaskbar: true}, }) + // the window opens at the size it was last given + width, height := 660, 600 + if s := settings.Load().Window; len(s) == 2 && s[0] >= 560 && s[1] >= 420 { + width, height = s[0], s[1] + } h.main = h.app.Window.NewWithOptions(application.WebviewWindowOptions{ Name: "main", Title: "magpie", URL: "/?" + theme, - Width: 660, - Height: 600, + Width: width, + Height: height, MinWidth: 560, MinHeight: 420, Hidden: true, @@ -179,6 +184,32 @@ func Run(version string, showMain bool, link string) error { TitleBar: application.MacTitleBarHiddenInset, }, }) + // A resize is kept once it settles; a maximised or full-screen window + // is the screen's size, not one the user gave it. + var resized *time.Timer + h.main.OnWindowEvent(events.Common.WindowDidResize, func(*application.WindowEvent) { + if resized != nil { + resized.Stop() + } + resized = time.AfterFunc(500*time.Millisecond, func() { + if h.main.IsMaximised() || h.main.IsFullscreen() || h.main.IsMinimised() { + return + } + w, ht := h.main.Size() + if w < 560 || ht < 420 { + return + } + // macOS reports a window a pixel short of the size it was + // opened at; kept as it is, the window would shrink a pixel at + // every start + s := settings.Load() + if len(s.Window) == 2 && abs(s.Window[0]-w) <= 2 && abs(s.Window[1]-ht) <= 2 { + return + } + s.Window = []int{w, ht} + settings.Save(s) + }) + }) // 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() @@ -324,3 +355,10 @@ func singleInstance(h *host) *application.SingleInstanceOptions { }, } } + +func abs(n int) int { + if n < 0 { + return -n + } + return n +} diff --git a/internal/settings/settings.go b/internal/settings/settings.go index ab0476ff8..acfee1ff0 100644 --- a/internal/settings/settings.go +++ b/internal/settings/settings.go @@ -34,6 +34,9 @@ type Settings struct { AgentOrder []string `json:"agentOrder,omitempty"` AgentsHidden []string `json:"agentsHidden,omitempty"` AgentsShown []string `json:"agentsShown,omitempty"` + // The main window's size when it was last resized, width and height, + // so it opens at it again after a restart. + Window []int `json:"window,omitempty"` } // Arrange puts items in the order the user gave the agents, those named