From a03d61292ec5eb83a4e8ba9eb5bab3b4602c4e81 Mon Sep 17 00:00:00 2001 From: pagliarinilucas <208635281+pagliarinilucas@users.noreply.github.com> Date: Thu, 26 Nov 2026 20:45:42 -0300 Subject: [PATCH] topbar: fullscreen games keep fullscreen, also after Alt+Tab 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 --- plugins/topbar/src/main.rs | 94 +++++++++++++++++++++++++++--------- plugins/topbar/src/thumbs.rs | 5 +- 2 files changed, 71 insertions(+), 28 deletions(-) diff --git a/plugins/topbar/src/main.rs b/plugins/topbar/src/main.rs index 96471a6f..1b232302 100644 --- a/plugins/topbar/src/main.rs +++ b/plugins/topbar/src/main.rs @@ -240,6 +240,8 @@ thread_local! { static BAR_T: Cell = const { Cell::new(0) }; static DESK_T: Cell = const { Cell::new(0) }; // last moment the bar had a reason to stay on the desktop static SCALE: Cell = const { Cell::new(1.0) }; + static FS_WND: Cell = const { Cell::new(0) }; // the window last seen fullscreen (fullscreen_now) + static FS_OFF: Cell = const { Cell::new(0) }; // when a fullscreen window was last missing, ms (0 = it is there) static OLD_STATE: Cell = const { Cell::new(ABS_ALWAYSONTOP as usize) }; static BAT: Cell<(i32, bool)> = const { Cell::new((-1, false)) }; // (percent or -1 = no battery, on AC) static VOL: Cell<(i32, bool)> = const { Cell::new((-1, false)) }; // (percent or -1 = unknown, muted) @@ -621,10 +623,7 @@ unsafe fn alt_done(cancel: bool) { }); let (v, i) = SW.take(); if let Some(&w) = v.get(i).filter(|_| !cancel) { - if IsIconic(w) != 0 { - ShowWindow(w, SW_RESTORE); - } - force_fg(w); + bring(w); } } @@ -774,9 +773,11 @@ unsafe fn place(hwnd: HWND) { // the dock is a pill; the other styles a plain strip SetWindowRgn(hwnd, if dock() { CreateRoundRectRgn(0, 0, w + 1, bar_h() + 1, bar_h(), bar_h()) } else { null_mut() }, 1); if vert() { - SetWindowPos(hwnd, HWND_TOPMOST, bar_x(), 0, bar_h(), cy, SWP_NOACTIVATE); + SetWindowPos(hwnd, HWND_TOPMOST, bar_x(), 0, bar_h(), cy, SWP_NOACTIVATE | SWP_NOZORDER); } else { - SetWindowPos(hwnd, HWND_TOPMOST, x, bar_y(), w, bar_h(), SWP_NOACTIVATE); + // SWP_NOZORDER: the bar is topmost from its creation on. Raising it again here (this runs on every display change, which + // a game switching into fullscreen causes) put it over the game and knocked that out of fullscreen, in a loop. + SetWindowPos(hwnd, HWND_TOPMOST, x, bar_y(), w, bar_h(), SWP_NOACTIVATE | SWP_NOZORDER); } InvalidateRect(hwnd, null(), 0); } @@ -1550,11 +1551,13 @@ unsafe fn bar_step(hwnd: HWND) { let p = BAR_P.get(); let p = if p < target { (p + dt / 380.0).min(target) } else { (p - dt / 240.0).max(target) }; BAR_P.set(p); - if p > 0.0 && IsWindowVisible(hwnd) == 0 { + let shown = p > 0.0 && IsWindowVisible(hwnd) == 0; + if shown { ShowWindow(hwnd, SW_SHOWNOACTIVATE); } let (px, py) = bar_pos(); - SetWindowPos(hwnd, HWND_TOPMOST, px, py, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE); + // on top of the other topmost windows only when it comes back; sliding away must not raise it over a fullscreen game + SetWindowPos(hwnd, HWND_TOPMOST, px, py, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE | if shown { 0 } else { SWP_NOZORDER }); if p == target { KillTimer(hwnd, 5); if p == 0.0 { @@ -1567,9 +1570,19 @@ unsafe fn bar_step(hwnd: HWND) { /// A maximized window does not: the bar's reserved strip keeps it below the bar. unsafe fn fullscreen_now(bar: HWND) -> bool { let fg = GetForegroundWindow(); - if fg.is_null() || fg == bar || IsWindowVisible(fg) == 0 || IsIconic(fg) != 0 { + if fg.is_null() || fg == bar || IsWindowVisible(fg) == 0 { return false; } + // A fullscreen game brought back with Alt+Tab gets the foreground while still minimized and only then switches the screen + // over: if the bar stayed on top then, the game lost fullscreen again. The window that was fullscreen counts as such + // while it comes back, and so does any Direct3D game Windows itself reports as fullscreen. + let mut quns = 0; + if windows_sys::Win32::UI::Shell::SHQueryUserNotificationState(&mut quns) >= 0 && quns == 3 { + return true; // QUNS_RUNNING_D3D_FULL_SCREEN + } + if IsIconic(fg) != 0 { + return fg as isize == FS_WND.get(); + } let mut cls = [0u16; 64]; let n = GetClassNameW(fg, cls.as_mut_ptr(), 64).max(0) as usize; if matches!(String::from_utf16_lossy(&cls[..n]).as_str(), "Progman" | "WorkerW" | "Shell_TrayWnd" | "Shell_SecondaryTrayWnd" | "RustExplorer") { @@ -1582,7 +1595,33 @@ unsafe fn fullscreen_now(bar: HWND) -> bool { GetMonitorInfoW(MonitorFromWindow(fg, MONITOR_DEFAULTTONEAREST), &mut mi); let m = mi.rcMonitor; let caption = GetWindowLongW(fg, GWL_STYLE) as u32 & WS_CAPTION == WS_CAPTION; - (m.left, m.top) == (0, 0) && covers_monitor(r, m, caption, IsZoomed(fg) != 0) + let fs = (m.left, m.top) == (0, 0) && covers_monitor(r, m, caption, IsZoomed(fg) != 0); + if fs { + FS_WND.set(fg as isize); + } + fs +} + +/// A game switching into fullscreen leaves the screen without any foreground window for a moment, again and again until it +/// settles: the bar must not come back in that moment (it would land on top and knock the game out of fullscreen, which then +/// tries again, in a loop). So the bar returns only once fullscreen has been gone for 1.5 s without a break. +unsafe fn fs_settled(now_fs: bool) -> bool { + if now_fs { + FS_OFF.set(0); + return true; + } + if !FULLSCREEN.load(Relaxed) { + return false; + } + let now = now_ms(); + if FS_OFF.get() == 0 { + FS_OFF.set(now); + } + if now.saturating_sub(FS_OFF.get()) < 1500 { + return true; + } + FS_OFF.set(0); + false } /// A framed maximized window overhangs the monitor by its border and is not fullscreen (#88); but a game in "borderless @@ -1600,7 +1639,7 @@ unsafe fn desktop_now() -> bool { } unsafe fn fs_update(hwnd: HWND) { - let fs = fullscreen_now(hwnd); + let fs = fs_settled(fullscreen_now(hwnd)); // a fullscreen game keeps the screen: the pointer at the bottom edge does not bring the bar (games park it at the // edges all the time); Win opens Start, which brings the bar with it let mut pt = POINT { x: 0, y: 0 }; @@ -1629,7 +1668,7 @@ unsafe fn fs_update(hwnd: HWND) { } // the 250 ms watch is only needed while the pointer decides (auto-hide, dock, desktop, bar away); otherwise // the foreground event, ABN_FULLSCREENAPP and the 1 s tick call this - let poll = i18n::style::autohide() || dock() || desktop_now() || DESK_HIDE.load(Relaxed); + let poll = i18n::style::autohide() || dock() || desktop_now() || DESK_HIDE.load(Relaxed) || FS_OFF.get() != 0; if FS_POLL.replace(poll) != poll { if poll { SetTimer(hwnd, 3, 250, None); @@ -1666,6 +1705,22 @@ unsafe fn force_fg(hw: HWND) { winrust_common::fg::force(hw); } +/// Switches to a window (Alt+Tab, its button, a preview, a menu). A minimized one goes through SwitchToThisWindow, the call +/// Windows' own Alt+Tab makes: it activates and restores in one step. Restoring first and activating after made a fullscreen +/// game get the foreground, lose it to the previous window and get it again; it then gave up on fullscreen (stayed black). +pub(crate) unsafe fn bring(w: HWND) { + if IsIconic(w) != 0 { + SwitchToThisWindow(w, 1); + if GetForegroundWindow() == w { + return; + } + } + force_fg(w); + if IsIconic(w) != 0 { + ShowWindow(w, SW_RESTORE); + } +} + /// Fills `r` (right / bottom exclusive) in colour `c`; `rad` > 0 rounds the corners (rad = corner diameter), anti-aliased /// through GDI+. Plain GDI for square boxes and when GDI+ is not available. unsafe fn fill(dc: HDC, r: RECT, c: u32, rad: i32) { @@ -2341,10 +2396,7 @@ unsafe fn app_menu(hwnd: HWND, h: i32) { fly::menu(tip_cx(&bar_layout(hwnd), h), Some((icon, "", name, sub)), items, move |i| { match acts.get(i).copied() { Some(A::Win(w)) => { - if IsIconic(w) != 0 { - ShowWindow(w, SW_RESTORE); - } - force_fg(w); + bring(w); } Some(A::Open) => open(&wide(&launch)), Some(A::Pin) => { @@ -2752,10 +2804,7 @@ unsafe fn win_num(n: usize) { if wins.len() == 1 && i.is_some() { ShowWindow(w, SW_MINIMIZE); } else { - if IsIconic(w) != 0 { - ShowWindow(w, SW_RESTORE); - } - force_fg(w); + bring(w); } } @@ -2854,10 +2903,7 @@ unsafe fn click(h: i32, left: bool, mid: bool) { if wins.contains(&fg) && IsIconic(fg) == 0 { ShowWindow(fg, SW_MINIMIZE); } else { - if IsIconic(w) != 0 { - ShowWindow(w, SW_RESTORE); - } - SetForegroundWindow(w); + bring(w); } } _ => {} diff --git a/plugins/topbar/src/thumbs.rs b/plugins/topbar/src/thumbs.rs index 20b15f3f..de93434c 100644 --- a/plugins/topbar/src/thumbs.rs +++ b/plugins/topbar/src/thumbs.rs @@ -239,10 +239,7 @@ pub(crate) unsafe extern "system" fn pv_proc(hwnd: HWND, msg: u32, wp: WPARAM, l } } else { hide_preview(); - if IsIconic(w) != 0 { - ShowWindow(w, SW_RESTORE); - } - force_fg(w); + bring(w); } } WM_TIMER if wp == 1 => {