Skip to content

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

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

enthonyaraujo wants to merge 2 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.

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.
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.

1 participant