diff --git a/src/RynthCore.Engine/WebPanel/WebPanelHost.cs b/src/RynthCore.Engine/WebPanel/WebPanelHost.cs index 9f872b3..52248bc 100644 --- a/src/RynthCore.Engine/WebPanel/WebPanelHost.cs +++ b/src/RynthCore.Engine/WebPanel/WebPanelHost.cs @@ -911,13 +911,14 @@ internal sealed unsafe class WebPanelHost private void Show() { PlaceNow(force: true); + IntPtr game = _env.GameHwnd; + if (game != IntPtr.Zero && IsWindow(game)) KeepAboveGame(game); // its z-slot before it shows: never over another program ShowWindow(_staHwnd, SW_SHOWNA); // Focus the panel only from within this process (the game, or a pop-out): a plugin opening it while the - // player is in another program must not pull the foreground away (the PID rule keeps it hidden meanwhile). + // player is in another program must not pull the foreground away (Track keeps it under that program). // Never while the foreground is null (mid alt-tab): that would grab focus on the way to another program. // Only this process, defensively: a foreground in the WebView2 browser process is assumed (not observed) to - // be the page's own dialog, which should keep it (IsOurPid, which includes the browser, is for the - // visibility rule only). + // be the page's own dialog, which should keep it. bool inProcess = ForegroundIsThisProcess(); if (inProcess) SetForegroundWindow(_staHwnd); if (_ctl != null) { try { _ctl.IsVisible = true; } catch { } } @@ -985,41 +986,23 @@ internal sealed unsafe class WebPanelHost if (!_open || _staHwnd == IntPtr.Zero) return; IntPtr game = _env.GameHwnd; if (game == IntPtr.Zero || !IsWindow(game)) { CloseNow(_serving); return; } - IntPtr fg = GetForegroundWindow(); - uint fgPid = 0; - if (fg != IntPtr.Zero) GetWindowThreadProcessId(fg, out fgPid); - // The page's own dialogs (alert, file picker, sign-in) are windows of the WebView2 browser process: count - // that process as ours so they do not hide the panel they belong to. - if (IsOurPid(fgPid)) fgPid = _ourPid; - bool? visible = WebPanelWindowPolicy.Visible(true, true, IsIconic(game), IsWindowVisible(game), fg == IntPtr.Zero, fgPid, _ourPid); + // Shown while the game window shows, whichever window has the foreground (clicking the game or another + // program does not hide it); only closing it, or minimizing or hiding the game, takes it away. + bool visible = WebPanelWindowPolicy.Visible(true, true, IsIconic(game), IsWindowVisible(game)); bool shown = IsWindowVisible(_staHwnd); - if (visible == false) + if (!visible) { if (shown) ShowWindow(_staHwnd, SW_HIDE); } else { - if (visible == true && !shown) { ShowWindow(_staHwnd, SW_SHOWNA); shown = true; } - if (shown) - { - if (!_inSizeMove) PlaceNow(force: false); - RaiseAboveGame(game); - } + if (!shown) ShowWindow(_staHwnd, SW_SHOWNA); + if (!_inSizeMove) PlaceNow(force: false); + KeepAboveGame(game); } UpdateStateFlags(); } - /// - /// This process, or this panel's WebView2 browser process. The browser PID is CoreWebView2.BrowserProcessId, - /// read when the controller is created (OnController) and cleared by TearDownWebView; 0 while there is none. - /// - private bool IsOurPid(uint pid) - { - if (pid == 0) return false; - uint browser = _browserPid; - return pid == _ourPid || (browser != 0 && pid == browser); - } - private bool GameClientScreen(out PanelRect rect) { rect = default; @@ -1040,13 +1023,80 @@ internal sealed unsafe class WebPanelHost SetWindowPos(_staHwnd, IntPtr.Zero, want.Left, want.Top, want.Width, want.Height, SWP_NOZORDER | SWP_NOACTIVATE); } - /// Raises the panel only when it is below the game window, so it never covers a window the user brought forward. - private void RaiseAboveGame(IntPtr game) + /// + /// Keeps the panel directly above the game window in the z-order: over the game whenever the game shows, + /// and under any window the user brought over the game, so it never covers another program. + /// + private void KeepAboveGame(IntPtr game) + { + // "Directly above the game" counts the game's own windows (RynthCore's bar and floating panels are owned + // by the game) as part of the game, so the panel stays over them as it did before; the panel's own + // windows (WebView2 dialogs) ride with the panel. The target is the first other window above all that. + IntPtr target = NextAbove(game, game, skipTopmost: false); + // No target, or a topmost one: the slot is the top of the normal band (HWND_TOP). Else the slot is under + // the target (insert-after places the panel below it). + bool bandTop = target == IntPtr.Zero || IsTopmost(target); + // In place: the panel is above the game, and the first window above it (past its own dialogs) is the + // target, or, for the band top, nothing but topmost windows are above it. + bool inPlace = IsAbove(_staHwnd, game) && (bandTop + ? NextAbove(_staHwnd, IntPtr.Zero, skipTopmost: true) == IntPtr.Zero + : NextAbove(_staHwnd, IntPtr.Zero, skipTopmost: false) == target); + if (inPlace) { ResetRaise(); return; } + SetWindowPos(_staHwnd, bandTop ? HWND_TOP : target, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE); + if (!bandTop || AtBandTop(game)) { ResetRaise(); return; } + // A topmost game: nothing in the normal band can go over it; leave it. + if (IsTopmost(game)) return; + if (_raiseBackoff > 0) { _raiseBackoff--; return; } + // While the game is the foreground window, HWND_TOP does not put ANY window over it, not even a new + // window of another process (probed in game 2026-11-13). Going topmost and straight back does: the + // panel lands at the top of the normal band, over the game, and the game keeps the foreground. + // NOACTIVATE on both, so focus never moves. + if (!_raiseLogged) { _raiseLogged = true; SafeLog("HWND_TOP did not raise the panel over the foreground game; raising via topmost and back"); } + SetWindowPos(_staHwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE); + SetWindowPos(_staHwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE); + if (AtBandTop(game)) { ResetRaise(); return; } + // If that keeps not working, say so once and back off to about once a second. + if (++_raiseMisses == 10) SafeLog("raising via topmost and back did not put the panel over the game"); + if (_raiseMisses > 10) _raiseBackoff = 20; + } + + /// Over the game with nothing but topmost windows (and its own dialogs) above it. + private bool AtBandTop(IntPtr game) => IsAbove(_staHwnd, game) && NextAbove(_staHwnd, IntPtr.Zero, skipTopmost: true) == IntPtr.Zero; + + private void ResetRaise() { _raiseMisses = 0; _raiseBackoff = 0; } + + private bool _raiseLogged; // panel thread only: log the topmost raise once per engine generation + private int _raiseMisses; // panel thread only: consecutive ticks the panel stayed under the game + private int _raiseBackoff; // panel thread only: ticks to skip before the next topmost attempt + + /// + /// The first window above that is not the panel or in its owner tree, not in + /// 's owner tree (the game, or none), and (with ) + /// not topmost; zero when there is none. + /// + private IntPtr NextAbove(IntPtr from, IntPtr skipTree, bool skipTopmost) + { + int guard = 0; + for (IntPtr h = GetWindow(from, GW_HWNDPREV); h != IntPtr.Zero && guard < 4096; h = GetWindow(h, GW_HWNDPREV), guard++) + { + IntPtr root = GetAncestor(h, GA_ROOTOWNER); + if (h == _staHwnd || root == _staHwnd) continue; + if (skipTree != IntPtr.Zero && root == skipTree) continue; + if (skipTopmost && IsTopmost(h)) continue; + return h; + } + return IntPtr.Zero; + } + + private static bool IsTopmost(IntPtr h) => h != IntPtr.Zero && (unchecked((uint)GetWindowLongW(h, GWL_EXSTYLE)) & WS_EX_TOPMOST) != 0; + + /// The panel is somewhere above the game in the z-order. + private bool IsAbove(IntPtr panel, IntPtr game) { int guard = 0; for (IntPtr h = GetWindow(game, GW_HWNDPREV); h != IntPtr.Zero && guard < 4096; h = GetWindow(h, GW_HWNDPREV), guard++) - if (h == _staHwnd) return; - SetWindowPos(_staHwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE); + if (h == panel) return true; + return false; } private void UpdateStateFlags() diff --git a/src/RynthCore.Engine/WebPanel/WebPanelNative.cs b/src/RynthCore.Engine/WebPanel/WebPanelNative.cs index 383801c..6a4cf92 100644 --- a/src/RynthCore.Engine/WebPanel/WebPanelNative.cs +++ b/src/RynthCore.Engine/WebPanel/WebPanelNative.cs @@ -7,7 +7,8 @@ namespace RynthCore.Engine.WebPanel; internal static unsafe class WebPanelNative { public const uint WS_POPUP = 0x80000000, WS_CLIPCHILDREN = 0x02000000, WS_THICKFRAME = 0x00040000; - public const uint WS_EX_TOOLWINDOW = 0x00000080; + public const uint WS_EX_TOOLWINDOW = 0x00000080, WS_EX_TOPMOST = 0x00000008; + public const int GWL_EXSTYLE = -20; public const uint CS_VREDRAW = 0x0001, CS_HREDRAW = 0x0002; public const uint WM_NCCREATE = 0x0081, WM_SIZE = 0x0005, WM_SETFOCUS = 0x0007, WM_PAINT = 0x000F, WM_CLOSE = 0x0010, @@ -17,8 +18,9 @@ internal static unsafe class WebPanelNative public const uint SWP_NOSIZE = 0x0001, SWP_NOMOVE = 0x0002, SWP_NOZORDER = 0x0004, SWP_NOACTIVATE = 0x0010; public const int SW_HIDE = 0, SW_SHOWNORMAL = 1, SW_SHOWNA = 8; - public const uint GW_HWNDPREV = 3, GA_ROOT = 2; + public const uint GW_HWNDPREV = 3, GA_ROOT = 2, GA_ROOTOWNER = 3; public static readonly IntPtr HWND_TOP = IntPtr.Zero; + public static readonly IntPtr HWND_TOPMOST = new(-1), HWND_NOTOPMOST = new(-2); public const uint EVENT_SYSTEM_FOREGROUND = 0x0003, WINEVENT_OUTOFCONTEXT = 0x0000; public const int TRANSPARENT = 1, DEFAULT_GUI_FONT = 17, IDC_ARROW = 32512; @@ -66,6 +68,7 @@ internal static unsafe class WebPanelNative [DllImport("user32.dll")] public static extern bool KillTimer(IntPtr h, IntPtr id); [DllImport("user32.dll")] public static extern uint GetWindowThreadProcessId(IntPtr h, out uint pid); [DllImport("user32.dll")] public static extern IntPtr GetWindow(IntPtr h, uint cmd); + [DllImport("user32.dll")] public static extern int GetWindowLongW(IntPtr h, int index); [DllImport("user32.dll")] public static extern IntPtr GetAncestor(IntPtr h, uint flags); [DllImport("user32.dll")] public static extern bool InvalidateRect(IntPtr h, IntPtr rect, bool erase); [DllImport("user32.dll")] public static extern IntPtr BeginPaint(IntPtr h, out PAINTSTRUCT ps); diff --git a/src/RynthCore.Engine/WebPanel/WebPanelWindowPolicy.cs b/src/RynthCore.Engine/WebPanel/WebPanelWindowPolicy.cs index 405eaac..2933437 100644 --- a/src/RynthCore.Engine/WebPanel/WebPanelWindowPolicy.cs +++ b/src/RynthCore.Engine/WebPanel/WebPanelWindowPolicy.cs @@ -16,17 +16,12 @@ internal static class WebPanelWindowPolicy public const int DefaultPercent = 88, MinPercent = 30, MaxPercent = 100, MinWidthPx = 480, MinHeightPx = 320; /// - /// false when closed, the game is gone, iconic or invisible; null (keep current) when the - /// foreground window is null mid alt-tab, so the panel never flickers; otherwise true only - /// when the foreground window belongs to the game process. + /// false when closed, the game is gone, iconic or invisible; otherwise true. The foreground window does + /// not count: the panel stays over the game while the player clicks the game or another program, and the + /// z-order (directly above the game) keeps it under any window brought over the game. /// - public static bool? Visible(bool open, bool gameAlive, bool gameIconic, bool gameVisible, - bool foregroundNull, uint foregroundPid, uint ourPid) - { - if (!open || !gameAlive || gameIconic || !gameVisible) return false; - if (foregroundNull) return null; - return foregroundPid == ourPid; - } + public static bool Visible(bool open, bool gameAlive, bool gameIconic, bool gameVisible) + => open && gameAlive && !gameIconic && gameVisible; /// Centered fractions for a size in percent of the client; 0 means the default (88). public static PanelFractions Default(int widthPercent, int heightPercent) diff --git a/tools/WebPanelHarness/Program.cs b/tools/WebPanelHarness/Program.cs index a50be4f..186d64b 100644 --- a/tools/WebPanelHarness/Program.cs +++ b/tools/WebPanelHarness/Program.cs @@ -110,7 +110,7 @@ internal static unsafe class Program Guard("CreateFailureBrowserFails", () => CheckCreateFailureBrowserFails(s)); Guard("NoFocusFromAnotherProcess", () => { int r = CheckNoFocusFromAnotherProcess(s); if (r > 0) handle = r; }); Guard("VisibilitySameProcess", () => handle = CheckVisibilitySameProcess(s)); - Unverified("VisibilityOtherProcess", "needs another process to take the foreground (alt-tab); the harness never forces it. Owner in-game check."); + Unverified("VisibilityOtherProcess", "the panel should stay shown, under the other program, while another process holds the foreground (alt-tab); the harness never forces it. Owner in-game check."); Guard("LayoutRemembered", () => handle = CheckLayout(s, lad)); Guard("UnloadAfterCloseCollected", () => CheckUnloadAfterClose(ref s, handle)); @@ -700,8 +700,8 @@ internal static unsafe class Program W($"session 2 open: state [{string.Join(",", State(s, h))}] host visible {visible}"); var sd = Stopwatch.StartNew(); s.Call("Shutdown", 2000); - W($"Shutdown(2000) returned in {sd.ElapsedMilliseconds} ms (panel open; window shown only if the harness held the foreground)"); - UnloadAndJudge("UnloadWhileVisibleCollected", ref s, visible ? null : "collected, but the panel window was not visible (the PID rule hid it: the harness did not hold the foreground), so the visible-teardown path was not shown"); + W($"Shutdown(2000) returned in {sd.ElapsedMilliseconds} ms (panel open)"); + UnloadAndJudge("UnloadWhileVisibleCollected", ref s, visible ? null : "collected, but the panel window was not visible (hidden or not yet shown), so the visible-teardown path was not shown"); } private static void UnloadAndJudge(string name, ref Session s, string? unverifiedIfCollected = null) diff --git a/tools/WebPanelTests/Program.cs b/tools/WebPanelTests/Program.cs index 9073cc5..49175c6 100644 --- a/tools/WebPanelTests/Program.cs +++ b/tools/WebPanelTests/Program.cs @@ -163,16 +163,13 @@ internal static unsafe class Program private static void Visibility() { Case("Visibility"); - // open, alive, !iconic, visible, !fgNull, fgPid, ourPid - Eq(WebPanelWindowPolicy.Visible(true, true, false, true, false, 5, 5), (bool?)true, "same process shows"); - Eq(WebPanelWindowPolicy.Visible(true, true, false, true, false, 6, 5), (bool?)false, "other process hides"); - Eq(WebPanelWindowPolicy.Visible(true, true, false, true, true, 0, 5), (bool?)null, "null foreground keeps current"); - Eq(WebPanelWindowPolicy.Visible(true, true, true, true, false, 5, 5), (bool?)false, "iconic hides"); - Eq(WebPanelWindowPolicy.Visible(false, true, false, true, false, 5, 5), (bool?)false, "closed hides"); - Eq(WebPanelWindowPolicy.Visible(true, false, false, true, false, 5, 5), (bool?)false, "dead game hides"); - Eq(WebPanelWindowPolicy.Visible(true, true, false, false, false, 5, 5), (bool?)false, "invisible game hides"); - Eq(WebPanelWindowPolicy.Visible(false, true, false, true, true, 0, 5), (bool?)false, "closed beats null foreground"); - Eq(WebPanelWindowPolicy.Visible(true, true, true, true, true, 0, 5), (bool?)false, "iconic beats null foreground"); + // open, alive, iconic, visible: the foreground window no longer counts (clicking the game or another program keeps it) + Eq(WebPanelWindowPolicy.Visible(true, true, false, true), true, "open over a shown game shows"); + Eq(WebPanelWindowPolicy.Visible(true, true, true, true), false, "iconic hides"); + Eq(WebPanelWindowPolicy.Visible(false, true, false, true), false, "closed hides"); + Eq(WebPanelWindowPolicy.Visible(true, false, false, true), false, "dead game hides"); + Eq(WebPanelWindowPolicy.Visible(true, true, false, false), false, "invisible game hides"); + Eq(WebPanelWindowPolicy.Visible(false, false, true, false), false, "closed beats everything"); } private static void Layout()