Skip to content

topbar: fix switching back to exclusive-fullscreen games - #165

Open
enthonyaraujo wants to merge 5 commits into
Lemxy:mainfrom
enthonyaraujo:fix/fullscreen-window-switch
Open

enthonyaraujo wants to merge 5 commits into
Lemxy:mainfrom
enthonyaraujo:fix/fullscreen-window-switch

Conversation

@enthonyaraujo

Copy link
Copy Markdown

Problem

When running an exclusive-fullscreen DirectX game (tested with PUBG / `TslGame.exe`) and switching away to the
desktop or another window, clicking the game's button on `topbar` or pressing `Alt+Tab` fails to bring the game
back — it immediately bounces back to the desktop or previous window unless `Alt+Tab` is spammed repeatedly.

## Root causes
1. **Topmost slide-out animation over fullscreen transitions:** When `FULLSCREEN` flipped to `true`, `fs_update`
started the 240 ms `bar_anim` timer, calling `SetWindowPos(bar, HWND_TOPMOST, ...)` every 15 ms over the primary
monitor while DXGI was transitioning into exclusive fullscreen.
2. **`place()` running mid-mode-switch:** `WM_DISPLAYCHANGE` and `ABN_POSCHANGED` unconditionally called
`place()`, invoking `SHAppBarMessage`, `SPI_SETWORKAREA` + `WM_SETTINGCHANGE`, and `SetWindowPos(bar,
HWND_TOPMOST, ...)` during exclusive-fullscreen mode changes.
3. **External window activation:**
   - Bar clicks called bare `ShowWindow(SW_RESTORE)` + `SetForegroundWindow(w)` on `WM_LBUTTONDOWN` from a
   `WS_EX_NOACTIVATE` window.
   - `Alt+Tab`, previews, app menu and `Win+1..9` called `ShowWindow(SW_RESTORE)` *before* acquiring foreground
   rights (restoring the game behind the current window so DXGI re-minimized it), then called `fg::force()`
   (meant for our own popups), which injected synthetic `VK_MENU` (`Alt`) keys when `GetForegroundWindow()`
   lagged behind a cross-process fullscreen switch.

## Changes
- **`common/src/fg.rs`**: Added `fg::activate(w)` for external application windows: resolves
`GetLastActivePopup`, acquires foreground rights via `AttachThreadInput` + `AllowSetForegroundWindow` *before*
`ShowWindow(SW_RESTORE)` / `SetForegroundWindow`, and only falls back to `VK_MENU` / `SwitchToThisWindow` when
`SetForegroundWindow` actually fails.
- **`plugins/topbar/src/main.rs` & `thumbs.rs`**:
  - Unified window switching (`click`, `alt_done`, `pv_proc`, `app_menu`, `win_num`) through `switch_to(w)`,
  hiding the bar immediately (`hide_bar_now`) before activating fullscreen targets (including minimized ones via
  `GetWindowPlacement`).
  - Hide the bar immediately on entering fullscreen instead of animating `HWND_TOPMOST` over the game (keeping
  the smooth slide-in when leaving fullscreen).
  - Defer `place()` during `FULLSCREEN` on `WM_DISPLAYCHANGE` / `ABN_POSCHANGED` and apply it once fullscreen
  ends; skip raising `HOST` to `HWND_TOPMOST` while fullscreen.
  - Trigger app-button left clicks on `WM_LBUTTONUP` (matching preview and `Alt+Tab` cards).
  - Start `Alt+Tab` selection at index `0` when the desktop is currently focused.
  - Added unit tests for `work_to_screen` and `initial_alt_tab_index`.

## Verification
- `cargo test -p winrust-common -p topbar`: all 38 tests pass (`23` in `winrust-common`, `15` in `topbar`).
- `cargo test` in `plugins/rust-search`: all 18 tests pass.
- `cargo build --release -p topbar`: builds cleanly with zero warnings.
- Manually tested with PUBG in exclusive fullscreen on Windows: both bar clicks and `Alt+Tab` now switch back
into the game on the first try.

enthonyaraujo and others added 3 commits November 24, 2026 15:52
Switching to an exclusive-fullscreen game (such as PUBG) via the bar or Alt+Tab could bounce right back to the desktop or previous window:

- Hide the bar immediately when entering fullscreen (and before activating a fullscreen target window) instead of running a 240 ms slide-out loop that repeatedly called SetWindowPos(HWND_TOPMOST) over the display.
- Defer place() (SHAppBarMessage / SPI_SETWORKAREA / HWND_TOPMOST) when WM_DISPLAYCHANGE or ABN_POSCHANGED arrives during a fullscreen mode switch, applying it once fullscreen ends; also skip raising HOST to HWND_TOPMOST while fullscreen.
- Add winrust_common::fg::activate() for external windows (used by bar clicks, Alt+Tab, hover previews, app menu and Win+1..9): acquires foreground rights before ShowWindow(SW_RESTORE), calls AllowSetForegroundWindow, resolves GetLastActivePopup, and avoids injecting synthetic Alt keys once SetForegroundWindow succeeds.
- Trigger app-button left clicks on WM_LBUTTONUP (matching preview / Alt+Tab cards) so the mouse button is not held during activation.
- In Alt+Tab, start at index 0 when the desktop is currently focused instead of skipping the most recent window.
A game switching into exclusive fullscreen (Elden Ring) dropped out of it again, in a loop, or stayed black after Alt+Tab:

- while the game switches the display mode there is no foreground window for a moment; the bar took that as "fullscreen
  is over", came back on top and knocked the game out. The bar now returns only after 1.5 s without fullscreen.
- place() and the slide animation re-inserted the bar at HWND_TOPMOST on every display change, i.e. above the game that
  had just become topmost. They keep the bar's z-order now (it is topmost from its creation); only a bar that comes back
  from hidden is raised.
- a fullscreen game brought back with Alt+Tab has the foreground while still minimized: the window that was last seen
  fullscreen (and any D3D app Windows reports as fullscreen, SHQueryUserNotificationState) counts as fullscreen then.
- switching to a minimized window (Alt+Tab, its button, a preview, the menu) restored it first and activated it after;
  the game got the foreground, lost it to the previous window and got it again, and gave up on fullscreen. Minimized
  windows now go through SwitchToThisWindow like Windows' own Alt+Tab.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Lemxy

Lemxy commented Nov 27, 2026

Copy link
Copy Markdown
Owner

Thanks for this! #197 (just merged into dev) fixes the same thing, switching back to an exclusive-fullscreen game: it keeps the last fullscreen window counted while it is minimized, uses SwitchToThisWindow for minimized windows and waits 1.5 s before the bar comes back. Your PR now conflicts with it in main.rs and thumbs.rs. Could you check whether a case you fixed is still broken on top of it? If so, I would gladly take that part.

Rebased on Lemxy#197, keeping only the cases still broken on top of it:

- Defer place() while FULLSCREEN is active (on WM_DISPLAYCHANGE / ABN_POSCHANGED) and run it once fullscreen ends: a game running at a lower resolution than the desktop (e.g. stretched 1280x960 on 1920x1080) otherwise shrinks SPI_SETWORKAREA and trims maximized desktop windows via fit_maximized(), leaving them permanently shrunk after exiting the game.
- Recognize any minimized fullscreen window on the primary monitor via GetWindowPlacement (in addition to FS_WND), so switching back still counts as fullscreen after another fullscreen window (e.g. F11 browser) overwrote FS_WND or after the bar restarted; also check QUNS_RUNNING_D3D_FULL_SCREEN before returning false on a transiently null foreground window.
- Hide the bar immediately when entering fullscreen (and before activating a fullscreen target in bring()) instead of keeping the WS_EX_TOPMOST bar visible during a 240 ms slide-out, and skip raising HOST to HWND_TOPMOST while fullscreen.
- In bring(), resolve GetLastActivePopup(w) and avoid injecting synthetic VK_MENU keys when SetForegroundWindow already succeeded; trigger app-button left clicks on WM_LBUTTONUP so the mouse button is not held down during AttachThreadInput / restore.
- In Alt+Tab, start at index 0 when the desktop is focused instead of skipping the most recent window.
@enthonyaraujo

Copy link
Copy Markdown
Author

Thanks for the heads-up! I checked #197 and rebased this branch directly on top of it (a03d612, touching only
plugins/topbar/src/main.rs now) keeping only the cases that are still broken on top of #197:

1. **Lower-resolution / stretched fullscreen games shrink maximized desktop windows (`place()` on
`WM_DISPLAYCHANGE`):** `#197` added `SWP_NOZORDER` to `place()`, but `place()` still runs on every
`WM_DISPLAYCHANGE` while fullscreen. When a game switches the display to a lower resolution (e.g. `1280×960` or
`1728×1080` on a `1920×1080` monitor), `set_work_area` updates `SPI_SETWORKAREA`, broadcasts `WM_SETTINGCHANGE`
mid-switch, and runs `fit_maximized()`, which clamps maximized windows down to the game's resolution (and since
`fit_maximized` only trims via `.min(...)`, they remain stuck shrunk after leaving the game). `place()` is now
deferred (`NEED_PLACE`) while `FULLSCREEN` is active and applied once fullscreen ends.
2. **Minimized fullscreen window when `FS_WND` was overwritten or unset:** `FS_WND` only tracks the single last
fullscreen window, so Alt-Tabbing out of a game, opening an F11 browser / fullscreen video (`FS_WND` becomes the
browser), exiting F11 and switching back to the game — or restarting `topbar` while the game is minimized —
leaves `fg == FS_WND` false while the game is still iconic. Kept `FS_WND` (for stretched lower-res games) and
added a `GetWindowPlacement` (`work_to_screen`) check for minimized windows, plus checking
`QUNS_RUNNING_D3D_FULL_SCREEN` before bailing out on a transiently null `GetForegroundWindow()`.
3. **`bring()` fallback / modal popups, immediate bar hide, and `WM_LBUTTONUP`:**
   - Hide the bar immediately (`hide_bar_now`) on entering fullscreen (and before activating a fullscreen target
   in `bring()`) instead of keeping the `WS_EX_TOPMOST` bar visible during the 240 ms slide-out, and skip raising
   `HOST` to `HWND_TOPMOST` while fullscreen.
   - In `bring()`, resolve `GetLastActivePopup(w)` (otherwise an app with an active modal dialog never matches
   `GetForegroundWindow() == w` and always falls through to `force_fg`), and only inject the synthetic `VK_MENU`
   tap when `SetForegroundWindow` actually returned `0` rather than when `GetForegroundWindow()` lags
   cross-process.
   - Fire app-button left clicks on `WM_LBUTTONUP` (matching preview and `Alt+Tab` cards) so the mouse button
   isn't held down during `AttachThreadInput` / restore.
4. **`Alt+Tab` from the desktop:** Start selection at index `0` when the desktop has focus (`v[0]` is not the
active root window) instead of skipping the most recent window.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants