diff --git a/src/RynthCore.Engine/WebPanel/WebPanelHost.cs b/src/RynthCore.Engine/WebPanel/WebPanelHost.cs
index 9f872b3..243ad7f 100644
--- a/src/RynthCore.Engine/WebPanel/WebPanelHost.cs
+++ b/src/RynthCore.Engine/WebPanel/WebPanelHost.cs
@@ -913,11 +913,10 @@ internal sealed unsafe class WebPanelHost
PlaceNow(force: true);
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 +984,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 +1021,18 @@ 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)
{
- 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);
+ IntPtr above = GetWindow(game, GW_HWNDPREV);
+ if (above == _staHwnd) return;
+ // Nothing above the game, or only topmost windows: the top of the normal band is directly above it.
+ // Else slot in under the window directly above the game (insert-after places the panel below it).
+ IntPtr after = above == IntPtr.Zero || (GetWindowLongW(above, GWL_EXSTYLE) & WS_EX_TOPMOST) != 0 ? HWND_TOP : above;
+ SetWindowPos(_staHwnd, after, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
}
private void UpdateStateFlags()
diff --git a/src/RynthCore.Engine/WebPanel/WebPanelNative.cs b/src/RynthCore.Engine/WebPanel/WebPanelNative.cs
index 383801c..bcdf592 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,
@@ -66,6 +67,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 uint 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()