Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ The addon and the companion app share one version number.

## [Unreleased]

### Fixed
- Companion app: after running for a few minutes, the floating window opened behind the game again. Windows' foreground lock refuses `HWND_TOPMOST` from a background app once its start-up grace period (`ForegroundLockTimeout`) ends. The window is now created topmost from the start, and if Windows still refuses, the call is retried while sharing the game thread's input state (focus does not move).

## [1.2.1] - 2026-11-18

### Fixed
Expand Down
24 changes: 21 additions & 3 deletions companion/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ sealed class OverlayForm : Form
[DllImport("user32.dll")] static extern bool ShowWindow(IntPtr hWnd, int cmd);
[DllImport("user32.dll")] static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll")] static extern int GetWindowLong(IntPtr hWnd, int index);
[DllImport("user32.dll")] static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool attach);
[DllImport("kernel32.dll")] static extern uint GetCurrentThreadId();
const int SW_SHOWNOACTIVATE = 4, GWL_EXSTYLE = -20, WS_EX_TOPMOST = 0x8;
static readonly IntPtr HWND_TOPMOST = new(-1);
static readonly IntPtr HWND_NOTOPMOST = new(-2);
Expand Down Expand Up @@ -172,6 +174,10 @@ public OverlayForm()
try { Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath); } catch { }
PlaceInitially();
Opacity = Math.Clamp(cfg.Opacity, 0.3, 1.0);
// Created topmost from the start. Later SetWindowPos(HWND_TOPMOST) calls
// are refused by Windows' foreground lock once the app has been running
// for a while (ForegroundLockTimeout) and the game has the focus.
TopMost = cfg.AlwaysOnTop;

Controls.Add(web);
Controls.Add(CreateToolbar());
Expand Down Expand Up @@ -486,7 +492,7 @@ void ShowOverlay()
// hidden or behind the game in some cases.
ShowWindow(Handle, SW_SHOWNOACTIVATE);
ApplyTopMost();
Log($"window shown at {Bounds} · visible = {IsWindowVisible(Handle)} · topmost = {(GetWindowLong(Handle, GWL_EXSTYLE) & WS_EX_TOPMOST) != 0}");
Log($"window shown at {Bounds} · visible = {IsWindowVisible(Handle)} · topmost = {IsTopMost()}");
webInit ??= InitWebView();
}

Expand All @@ -505,12 +511,24 @@ void ToggleOverlay()
if (!navigated) Navigate(Strings.WowheadHome);
}

bool IsTopMost() => (GetWindowLong(Handle, GWL_EXSTYLE) & WS_EX_TOPMOST) != 0;

void ApplyTopMost()
{
TopMost = cfg.AlwaysOnTop;
// Re-assert above the (borderless) game window without stealing focus.
uint show = Visible ? SWP_SHOWWINDOW : 0;
SetWindowPos(Handle, cfg.AlwaysOnTop ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | show);
uint flags = SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | (Visible ? SWP_SHOWWINDOW : 0);
SetWindowPos(Handle, cfg.AlwaysOnTop ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0, flags);
if (!cfg.AlwaysOnTop || IsTopMost()) return;

// Still refused: share the input state of the foreground (game) thread
// for this one call, which lifts the foreground lock. Focus does not move.
uint gameThread = GetWindowThreadProcessId(GetForegroundWindow(), out _);
uint ownThread = GetCurrentThreadId();
bool attached = gameThread != 0 && gameThread != ownThread && AttachThreadInput(ownThread, gameThread, true);
try { SetWindowPos(Handle, HWND_TOPMOST, 0, 0, 0, 0, flags); }
finally { if (attached) AttachThreadInput(ownThread, gameThread, false); }
Log($"topmost refused, retried with AttachThreadInput (attached = {attached}) · topmost = {IsTopMost()}");
}

async Task InitWebView()
Expand Down
Loading