gui/window: keep the window frame across starts, take the first click

- Options.FrameFile: the window's position, size and maximized state in
  a small JSON file, read at New (a frame on no connected screen is
  ignored, the window opens centered), written shortly after every move
  or resize and at Destroy. macOS: NSWindow frame (the normal frame
  under a zoomed one), checked against NSScreen visible frames; Windows:
  Get/SetWindowPlacement, checked with MonitorFromRect.
- macOS: the web view is a WKWebView subclass that accepts the first
  mouse click, so a page in an inactive window reacts to it instead of
  the click only activating the window (yanks' start dialog).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
terminarandClaude Opus 5.5 committed 2026-11-15 15:19:45 +01:00
1 parent c16b871db5
commit 31dbc347e8
9 files changed
+325 -13

No files matched your search

+61 -1
View File
@@ -56,6 +56,9 @@ var (
pDwmSetAttribute = dwmapi.NewProc("DwmSetWindowAttribute")
pSetWindowPos = user32.NewProc("SetWindowPos")
pGetDpiForSystem = user32.NewProc("GetDpiForSystem")
pGetPlacement = user32.NewProc("GetWindowPlacement")
pSetPlacement = user32.NewProc("SetWindowPlacement")
pMonitorFromRect = user32.NewProc("MonitorFromRect")
)
func setAppIcon([]byte) {} // the icon comes from the .exe resources (RT_GROUP_ICON "APP")
@@ -168,7 +171,9 @@ func newBackend(opt Options) (backend, string, string, error) {
windowsMu.Lock()
byHWND[hwnd] = b
windowsMu.Unlock()
pShowWindow.Call(hwnd, swShow)
if f := opt.restore; f == nil || !restorePlacement(hwnd, *f) {
pShowWindow.Call(hwnd, swShow)
}
pUpdateWindow.Call(hwnd)
pSetFocus.Call(hwnd)
@@ -209,14 +214,17 @@ func wndproc(hwnd, msg, wp, lp uintptr) uintptr {
if b.chromium != nil {
b.chromium.Resize()
}
b.reportFrame()
case wmMove, wmMoving:
if b.chromium != nil {
_ = b.chromium.NotifyParentWindowPositionChanged()
}
b.reportFrame()
case wmApp:
b.runQueue()
return 0
case wmClose:
b.reportFrame()
pDestroyWindow.Call(hwnd)
return 0
case wmDestroy:
@@ -233,6 +241,57 @@ func wndproc(hwnd, msg, wp, lp uintptr) uintptr {
return r
}
// windowPlacement is WINDOWPLACEMENT. rcNormalPosition is the restored
// (not maximized) frame in workspace coordinates; Get and Set use the
// same ones, so a saved frame comes back where it was.
type windowPlacement struct {
length uint32
flags uint32
showCmd uint32
ptMinPosition [2]int32
ptMaxPosition [2]int32
rcNormalPosition [4]int32 // left, top, right, bottom
}
const (
swShowNormal = 1
swShowMinimized = 2
swShowMaximized = 3
wpfRestoreToMaximize = 0x0002
)
// reportFrame hands the window's normal frame and maximized state to
// Options.FrameFile's saver.
func (b *winBackend) reportFrame() {
if b.opt.onFrame == nil {
return
}
var wp windowPlacement
wp.length = uint32(unsafe.Sizeof(wp))
if r, _, _ := pGetPlacement.Call(b.hwnd, uintptr(unsafe.Pointer(&wp))); r == 0 {
return
}
rc := wp.rcNormalPosition
maximized := wp.showCmd == swShowMaximized || (wp.showCmd == swShowMinimized && wp.flags&wpfRestoreToMaximize != 0)
b.opt.onFrame(Frame{X: int(rc[0]), Y: int(rc[1]), Width: int(rc[2] - rc[0]), Height: int(rc[3] - rc[1]), Maximized: maximized})
}
// restorePlacement shows the window with a saved frame, when that frame
// is still on a monitor (MONITOR_DEFAULTTONULL); false: show it as new.
func restorePlacement(hwnd uintptr, f Frame) bool {
rc := [4]int32{int32(f.X), int32(f.Y), int32(f.X + f.Width), int32(f.Y + f.Height)}
if m, _, _ := pMonitorFromRect.Call(uintptr(unsafe.Pointer(&rc)), 0); m == 0 {
return false
}
wp := windowPlacement{showCmd: swShowNormal, rcNormalPosition: rc}
if f.Maximized {
wp.showCmd = swShowMaximized
}
wp.length = uint32(unsafe.Sizeof(wp))
r, _, _ := pSetPlacement.Call(hwnd, uintptr(unsafe.Pointer(&wp)))
return r != 0
}
func (b *winBackend) runQueue() {
b.mu.Lock()
q := b.queue
@@ -277,6 +336,7 @@ func (b *winBackend) Destroy() {
b.destroying = true
b.mu.Unlock()
if ok, _, _ := pIsWindow.Call(b.hwnd); ok != 0 {
b.reportFrame() // a window ended by Terminate, not closed by the user
pDestroyWindow.Call(b.hwnd)
}
windowsMu.Lock()