diff --git a/src/RynthCore.Engine/WebPanel/WebPanelHost.cs b/src/RynthCore.Engine/WebPanel/WebPanelHost.cs
index 243ad7f..8094090 100644
--- a/src/RynthCore.Engine/WebPanel/WebPanelHost.cs
+++ b/src/RynthCore.Engine/WebPanel/WebPanelHost.cs
@@ -911,6 +911,8 @@ 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 (Track keeps it under that program).
@@ -1027,12 +1029,50 @@ internal sealed unsafe class WebPanelHost
///
private void KeepAboveGame(IntPtr game)
{
- 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);
+ // "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) return;
+ SetWindowPos(_staHwnd, bandTop ? HWND_TOP : target, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
+ }
+
+ ///
+ /// 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 == 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 bcdf592..16eda28 100644
--- a/src/RynthCore.Engine/WebPanel/WebPanelNative.cs
+++ b/src/RynthCore.Engine/WebPanel/WebPanelNative.cs
@@ -18,7 +18,7 @@ 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 const uint EVENT_SYSTEM_FOREGROUND = 0x0003, WINEVENT_OUTOFCONTEXT = 0x0000;
@@ -67,7 +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 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);