From 51914097083e1b3540cd9c3ff0609e33c671f38d Mon Sep 17 00:00:00 2001 From: qiannian Date: Fri, 26 Jun 2026 23:11:22 +0800 Subject: [PATCH] app: [Windows] support TopMost option Use HWND_TOPMOST and HWND_NOTOPMOST when applying Config.TopMost on Windows. Leave SWP_NOZORDER set when TopMost hasn't changed, so other Configure calls don't reorder the window. Signed-off-by: qiannian --- app/internal/windows/windows.go | 5 +++-- app/os_windows.go | 13 +++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/app/internal/windows/windows.go b/app/internal/windows/windows.go index 151c749f..30154fe3 100644 --- a/app/internal/windows/windows.go +++ b/app/internal/windows/windows.go @@ -207,8 +207,9 @@ const ( CFS_POINT = 0x0002 CFS_CANDIDATEPOS = 0x0040 - HWND_TOP = syscall.Handle(0) - HWND_TOPMOST = ^(syscall.Handle(1) - 1) // -1 + HWND_TOP = syscall.Handle(0) + HWND_TOPMOST = ^(syscall.Handle(1) - 1) // -1 + HWND_NOTOPMOST = ^(syscall.Handle(2) - 1) // -2 HTCAPTION = 2 HTCLIENT = 1 diff --git a/app/os_windows.go b/app/os_windows.go index 8e46a9d3..8af2d6a8 100644 --- a/app/os_windows.go +++ b/app/os_windows.go @@ -747,7 +747,16 @@ func (w *window) Configure(options []Option) { style := windows.GetWindowLong(w.hwnd, windows.GWL_STYLE) var showMode int32 var x, y, width, height int32 - swpStyle := uintptr(windows.SWP_NOZORDER | windows.SWP_FRAMECHANGED) + swpStyle := uintptr(windows.SWP_FRAMECHANGED) + if cnf.TopMost == w.config.TopMost { + // Don't change the z-order if TopMost didn't change. + swpStyle |= windows.SWP_NOZORDER + } + hwndAfter := windows.HWND_NOTOPMOST + if cnf.TopMost { + hwndAfter = windows.HWND_TOPMOST + } + w.config.TopMost = cnf.TopMost winStyle := uintptr(windows.WS_OVERLAPPEDWINDOW) style &^= winStyle switch cnf.Mode { @@ -799,7 +808,7 @@ func (w *window) Configure(options []Option) { // Note: these invocation all trigger the windows callback method which may process a pending system.ActionCenter // action, so SetWindowPos should come first so as to not "overwrite" system.ActionCenter. - windows.SetWindowPos(w.hwnd, 0, x, y, width, height, swpStyle) + windows.SetWindowPos(w.hwnd, hwndAfter, x, y, width, height, swpStyle) windows.SetWindowLong(w.hwnd, windows.GWL_STYLE, style) windows.ShowWindow(w.hwnd, showMode) }