Files
magpie/internal/gui/window_rule.go
T
yetone 2ec3453647 gui: the main window opens where it was left (jorbenzhu)
jorbenzhu on Discord: after every update and restart the panel is back in
the middle of the screen. It is the main window: after an update magpie
starts again with `magpie gui <tab>`, and Wails opens a window centred
unless told otherwise. Its size and maximised state were kept; its place
never was (gui-shell.md said "The window's position is not kept"), so
nothing was lost on upgrade, there was nothing to restore. The tray's
panel opens under the tray icon on the Mac and Windows, and where the
click was on Linux, so it is not what moved.

Now settings.WindowAt keeps the window's top left corner (system
coordinates across every screen), per machine like Window (KeepOwn, and
the Settings page's save carries it over). keepAt saves it once a move or
resize has rested 500ms, unless the window is maximised, full screen,
minimised or hidden; settings.Save writes through temp and rename, and an
unreadable settings file is left alone. On the first show placeMain puts
it back (putBack): openAt picks the screen holding most of the window and
moves it fully onto that screen's work area; when no screen holds any of
it (a monitor gone) or the screens or the file can't be read, it stays
centred. Under Wayland a window can't place itself, so nothing is kept or
restored there unless GDK_BACKEND is x11 (keepsPlace).

Run:
- mote, at 3f8f1d88b (on d99e213f5): go test -timeout 45m -count=1 ./...
  80 packages ok; internal/gui red only in its 10 real-window AppKit
  tests (TestDockJoinLeadsCommandTab, TestPanelCloseHidesIt,
  TestPanelJoinsFullScreenSpaces, TestPluginPageLoadsItsFilesInTheWindow,
  TestDockReopenKeepsTheWindowsSpace, TestMainWindowCorners,
  TestMainWindowRightToLeft, TestHandoverLeavesTheMenuBarIconForTheNextMagpie,
  TestTrayCellClickReleasedPanel, TestCoveredWindowStaysUp), each killed
  at its deadline. The same 10 fail the same way on origin/main
  fa427216a: mote has no console session (#4125).
- mote, after rebasing onto 024393fbb (two gateway commits, none of
  these files): internal/gui (less the 10 above), internal/settings and
  internal/gateway: ok.
- mote: gofmt -l; go vet ./internal/gui ./internal/settings; GOOS=linux
  go vet -tags nogui ./...; GOOS=windows go vet ./...; go build, and
  GOOS=windows / GOOS=linux -tags nogui builds: clean.
- Windows (win-run) and Linux (wsl-run, -tags gtk3): TestOpenAt,
  TestSettleAt, TestKeepsPlace, TestOpenSize, TestSettle*,
  TestSettingsSaveKeeps*, TestWindowMaximisedKeptAndOwn: ok.

Broken by hand (each red, then restored):
- api.go not carrying WindowAt over a Settings save:
  TestSettingsSaveKeepsWhatItDoesNotSend red ("WindowAt: [] after a save
  of the Settings page, was [-1440 120]").
- KeepOwn not keeping WindowAt: TestWindowMaximisedKeptAndOwn red.
- openAt without the clamp and the gone-screen check: TestOpenAt red in
  7 of 12 cases.

Real app, Linux X11 (magpie-desktop, Xvfb 1920x1080 + xfwm4, sandbox
HOME/XDG dirs, MAGPIE_ADDR 127.0.0.1:3447, gtk3 build of this tree):
first start centred (630,240 in X terms); xdotool moved it to 200,150 and
settings.json got windowAt; restarted, it opened at 200,150; windowAt
[5000,4000] (a gone monitor) opened centred; [1700,900] opened at
[1260,480], flush with the bottom right (660x600 + GTK's 26px shadow).

Not tried: the real app on macOS (mote has no console session, #4125)
and on Windows (a sandbox magpie.exe rewrites HKCU\Software\Classes\magpie,
which points at another session's build there). Wayland is not restored
by design. Multi-monitor setups with mixed scaling were not tried on real
hardware; openAt's cases cover a second screen left of and right of the
main one.
2026-10-30 21:56:29 +08:00

141 lines
4.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package gui
import (
"strings"
"github.com/yetone/magpie/internal/settings"
)
// The main window opens as it was last left: at the size it was last given
// (settings.Window), where it was last put (settings.WindowAt) and maximised
// again if it was (settings.WindowMaximised).
// windowW, windowH is the main window's size, in points, before it is first
// given one.
const windowW, windowH = 660, 600
// openSize is the size, in points, the main window opens at: the one it was
// last given (saved), else windowW×windowH; no smaller than its least (minW,
// minH).
func openSize(saved []int, minW, minH int) (int, int) {
w, h := windowW, windowH
if len(saved) == 2 && saved[0] >= windowMinW && saved[1] >= windowMinH {
w, h = saved[0], saved[1]
}
return max(w, minW), max(h, minH)
}
// fitRoom is w×h no larger than the work area of the screen it is on (sw,
// sh; 0 when unknown), and whether that changed it: a size kept on a larger
// screen, or before the display was scaled up, still fits, title bar in
// sight.
func fitRoom(w, h, sw, sh int) (int, int, bool) {
fw, fh := w, h
if sw > 0 {
fw = min(fw, sw)
}
if sh > 0 {
fh = min(fh, sh)
}
return fw, fh, fw != w || fh != h
}
// settle keeps in s the main window as it has settled, w×h and maximised or
// not, on a screen whose work area is sw×sh (0s when unknown), and says
// whether s changed. A maximised window is the screen's size, not one the
// user gave it: the size it is restored to stays as it was. So does one
// under the least, or within 2 points of the size kept — macOS opens a
// window a point short of the size it was given, and kept as it is the
// window would shrink a point at every start. A side as long as the screen's
// keeps a longer one kept: the window was fitted to a smaller screen
// (fitRoom), and opens at its own size again on the larger one.
func settle(s *settings.Settings, w, h, sw, sh int, maximised bool) bool {
changed := s.WindowMaximised != maximised
s.WindowMaximised = maximised
if maximised || w < windowMinW || h < windowMinH {
return changed
}
if len(s.Window) == 2 {
w, h = fitted(s.Window[0], w, sw), fitted(s.Window[1], h, sh)
if abs(s.Window[0]-w) <= 2 && abs(s.Window[1]-h) <= 2 {
return changed
}
}
s.Window = []int{w, h}
return true
}
// fitted is the side kept for one now as long as the screen's, room: the
// longer one kept before, if it was.
func fitted(kept, now, room int) int {
if room > 0 && kept > now && abs(now-room) <= 2 {
return kept
}
return now
}
func abs(n int) int {
if n < 0 {
return -n
}
return n
}
// area is a screen's work area, in the units and coordinates of the main
// window's Position: x, y of its top left corner, width and height.
type area struct{ x, y, w, h int }
// openAt is where the main window, w×h, opens: at the place it was last
// left (saved: x, y), moved as little as it takes to lie whole on the work
// area of the screen holding most of it now (areas). Not ok — left where the
// system puts it, the middle of the screen — when no place was kept, or
// no screen there now holds any of it: the screen it was on is gone, the
// screens were laid out again, or none is known. A window larger than the
// area keeps its top left corner in it, title bar in reach.
func openAt(saved []int, w, h int, areas []area) (x, y int, ok bool) {
if len(saved) != 2 || w <= 0 || h <= 0 {
return 0, 0, false
}
x, y = saved[0], saved[1]
best, most := area{}, 0
for _, a := range areas {
if n := overlap(x, w, a.x, a.w) * overlap(y, h, a.y, a.h); n > most {
best, most = a, n
}
}
if most == 0 {
return 0, 0, false
}
return within(x, w, best.x, best.w), within(y, h, best.y, best.h), true
}
// overlap is how much of the span from p, n long, lies in the one from
// ap, an long.
func overlap(p, n, ap, an int) int {
return max(0, min(p+n, ap+an)-max(p, ap))
}
// within is p moved as little as it takes for the span from it, n long, to
// lie in the one from ap, an long; ap when it is the longer.
func within(p, n, ap, an int) int {
return max(ap, min(p, ap+an-n))
}
// settleAt keeps in s the place the main window was left at, x, y, and
// says whether s changed.
func settleAt(s *settings.Settings, x, y int) bool {
if len(s.WindowAt) == 2 && s.WindowAt[0] == x && s.WindowAt[1] == y {
return false
}
s.WindowAt = []int{x, y}
return true
}
// keepsPlace says whether the main window's place can be read and set on
// goos: not in a Wayland session (unless GTK is made to speak X11 there,
// GDK_BACKEND=x11), where a window has no place it can know and the
// compositor puts it where it likes; Wails reads 0, 0 there.
func keepsPlace(goos string, getenv func(string) string) bool {
return !waylandSession(goos, getenv) || strings.HasPrefix(getenv("GDK_BACKEND"), "x11")
}