shangyuan0818 on X (Windows, AutoRestart and auto-update on, lightweight
mode off): magpie's window turned up on the taskbar by itself, its
button flashing orange, while they were in another app.
The cause: Wails (v3.0.0-beta.24) activates every window as it makes
it, hidden ones too. The panel's frame change (Frameless, run() in
webview_window_windows.go) and the main window's centring
(CenterWindow) are SetWindowPos calls without SWP_NOACTIVATE. The new
magpie a restart by itself starts runs in the background and may not
take the foreground, and Windows answers that ask by flashing the
window's taskbar button (HSHELL_FLASH) until it is clicked. Where it
may (nobody at the keyboard for a while), a hidden magpie window took
the keyboard from the app in front instead. The main window was then
shown with SW_SHOW, which activates it again, and a minimised one with
SetWindowPlacement(SW_SHOWMINNOACTIVE), which activates it all the
same.
Now a WH_CBT hook on Wails' main thread (noactivate_windows.go, put on
from appWndProc, which every WailsWebviewWindow message passes) turns
down HCBT_ACTIVATE for a magpie window that isn't shown, and for one
being shown quietly: showHere without focus (showQuietly) and
showMinimised. The tray's message-only window and Wails' disabled
main-thread window share the class and are left alone. A window the
user opens is shown and focused as before. Adding SWP_NOACTIVATE to the
moves' WINDOWPOS in WM_WINDOWPOSCHANGING doesn't stop the activation
(tried on the box).
TestRestartBySelfAsksNoAttention (new) plays the restart in a child
process, window and tray modes, another window in front: it records
the shell hook's notes for magpie's windows and the foreground, needs
none of either, the page drawn and visible, then that a user's
ShowMain brings the window to the front.
TestRestartMinimisedComesBackMinimised now makes its app with
windowsOptions(), as the real app does; without them its window kept
the foreground it took at creation and it was red on main and on this
change alike.
Run, on the Windows box (xiaomi-gpu, Windows 11, interactive session):
- TestRestartBySelfAsksNoAttention: red on the old code, green both
modes with this change, green again at the pushed tree.
- TestRestartMinimisedComesBackMinimised: red on the old code ("the
window minimised took the foreground"), green with this change,
restore and draw included.
- TestRestartedWindowIsDrawn (13 cases, the behind/cover cases shown
quietly under another app): 13 of 13 at the pushed tree, with the
setup fix in the commit after this one.
- The whole internal/gui package, old code and this change, compared:
the same reds both sides apart from the two tests above (plugin-page,
import-row, container and client-cert tests red on the box on main
as well) and TestRestartedWindowIsDrawn's setup flake, fixed after.
- Broken by hand: the hook not put on, red (window 0x4 activation,
tray took the foreground); showingQuietly never set, red (window:
0x4).
Full suite on mica at 31a08ce8c (this change on 8334f1e6e), -timeout
45m: every package ok, internal/gui included, but internal/cli
(TestEveryRouteHasACommandOrIsNamed: 80a315a70's /api/page-away, since
named on main in 69946f43c; internal/cli green with that line) and internal/gateway, whose reds
are mica's disk filling up ("no space left on device", 2261 lines);
gateway doesn't import internal/gui, and it is ok at the pushed tree in
WSL (Ubuntu 24.04, 868s; mote was busy with another suite). go vet ./... (darwin) ok; Windows
vet and test compile on mote, Linux nogui vet on mote and gtk3 vet in
WSL ok at the pushed tree; gofmt clean.
Not tried: the grey, blank window they also saw was not reproduced; the
page was drawn on every activation tried. Linux (gtk_window_present
asks for focus the same way) is #6634. macOS needs no change:
makeKeyAndOrderFront doesn't activate a background app, and
requestUserAttention is called only by Flash, which magpie doesn't use;
not tried on a real Mac.
53 lines
1.5 KiB
Go
53 lines
1.5 KiB
Go
//go:build windows && !nogui
|
|
|
|
package gui
|
|
|
|
import (
|
|
"unsafe"
|
|
|
|
"github.com/wailsapp/wails/v3/pkg/application"
|
|
"github.com/wailsapp/wails/v3/pkg/w32"
|
|
)
|
|
|
|
// showsMinimised: Windows can put a window not shown yet straight onto the
|
|
// taskbar, to restore at its place and size (#3495).
|
|
const showsMinimised = true
|
|
|
|
// wpfRestoreToMaximized is WINDOWPLACEMENT's WPF_RESTORETOMAXIMIZED: the
|
|
// window, restored from the taskbar, comes back maximised.
|
|
const wpfRestoreToMaximized = 0x0002
|
|
|
|
// showMinimised puts w, placed and not shown yet, onto the taskbar as a
|
|
// window minimised there: neither shown on screen nor activated, so the
|
|
// app the user is in keeps the keyboard. Wails' Show (SW_SHOW) would
|
|
// activate it, and so does SW_SHOWMINNOACTIVE, which quietly turns
|
|
// down. Restored, it comes back where it was put, maximised if maximise,
|
|
// its page shown (TestRestartMinimisedComesBackMinimised). On the main
|
|
// thread.
|
|
func showMinimised(w *application.WebviewWindow, maximise bool) {
|
|
hwnd := w32.HWND(uintptr(w.NativeWindow()))
|
|
wp := w32.WINDOWPLACEMENT{}
|
|
wp.Length = uint32(unsafe.Sizeof(wp))
|
|
if hwnd == 0 || !w32.GetWindowPlacement(hwnd, &wp) {
|
|
showHere(w, false) // shown quietly, as before #3495
|
|
if maximise {
|
|
w.Maximise()
|
|
}
|
|
return
|
|
}
|
|
wp.ShowCmd = w32.SW_SHOWMINNOACTIVE
|
|
if maximise {
|
|
wp.Flags |= wpfRestoreToMaximized
|
|
} else {
|
|
wp.Flags &^= wpfRestoreToMaximized
|
|
}
|
|
var placed bool
|
|
quietly(uintptr(hwnd), func() { placed = w32.SetWindowPlacement(hwnd, &wp) })
|
|
if !placed {
|
|
showHere(w, false)
|
|
if maximise {
|
|
w.Maximise()
|
|
}
|
|
}
|
|
}
|