From f3a820dc17ff85b05e08379d1823ba0e330ab35b Mon Sep 17 00:00:00 2001 From: Enthony Araujo de Oliveira Date: Tue, 24 Nov 2026 15:52:35 -0300 Subject: [PATCH 1/3] topbar: fix switching back to exclusive-fullscreen games 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. --- common/src/fg.rs | 90 ++++++++++++++- plugins/topbar/src/main.rs | 216 +++++++++++++++++++++++++++-------- plugins/topbar/src/thumbs.rs | 5 +- 3 files changed, 253 insertions(+), 58 deletions(-) diff --git a/common/src/fg.rs b/common/src/fg.rs index 7aed0a09..6ab1cb89 100644 --- a/common/src/fg.rs +++ b/common/src/fg.rs @@ -1,12 +1,15 @@ -//! Bringing our own popup (Start, the search card) to the front with the keyboard focus. Windows refuses the foreground to a -//! process that did not get the last input: the popup then shows, but typing goes to the old window (#72). Used by topbar -//! and rs-launcher; call it on the thread that owns `h`. +//! Bringing windows to the front: `force()` for our own popups (Start, the search card) that need keyboard focus +//! even without recent input (#72), and `activate()` for switching to another application's window (restoring it if +//! minimized without disrupting exclusive-fullscreen games). use windows_sys::Win32::{ Foundation::HWND, System::Threading::{AttachThreadInput, GetCurrentThreadId}, UI::{ - Input::KeyboardAndMouse::{INPUT, INPUT_KEYBOARD, KEYEVENTF_KEYUP, SendInput, SetFocus, VK_MENU}, - WindowsAndMessaging::{GetForegroundWindow, GetWindowThreadProcessId, SetForegroundWindow}, + Input::KeyboardAndMouse::{INPUT, INPUT_KEYBOARD, IsWindowEnabled, KEYEVENTF_KEYUP, SendInput, SetFocus, VK_MENU}, + WindowsAndMessaging::{ + AllowSetForegroundWindow, BringWindowToTop, GetForegroundWindow, GetLastActivePopup, GetWindowThreadProcessId, + IsIconic, IsWindow, IsWindowVisible, SW_RESTORE, SetForegroundWindow, ShowWindow, SwitchToThisWindow, + }, }, }; @@ -37,6 +40,83 @@ pub fn force(h: HWND) { } } +/// Switches to another application's window `w` (restoring it first if minimized). +/// Acquires foreground rights before calling `ShowWindow(w, SW_RESTORE)` so a minimized exclusive-fullscreen game is +/// not restored behind the current window (causing DXGI to immediately re-minimize it), and avoids injecting synthetic +/// Alt keys once `SetForegroundWindow` succeeds. +pub fn activate(w: HWND) { + unsafe { + if w.is_null() || IsWindow(w) == 0 { + return; + } + let target = last_active_popup(w); + let mut pid = 0u32; + GetWindowThreadProcessId(target, &mut pid); + let iconic = IsIconic(w) != 0; + + // Already un-minimized and we hold foreground rights (e.g. a click on the bar): switch directly without + // attaching thread queues or injecting keys. + if !iconic { + if pid != 0 { + AllowSetForegroundWindow(pid); + } + if SetForegroundWindow(target) != 0 { + return; + } + } + + // 1) Share the current foreground thread's input queue *before* restoring/activating so we inherit foreground + // permission during ShowWindow(SW_RESTORE) and AllowSetForegroundWindow(pid). + let me = GetCurrentThreadId(); + let fg = GetForegroundWindow(); + let ft = if !fg.is_null() { GetWindowThreadProcessId(fg, std::ptr::null_mut()) } else { 0 }; + let attach = ft != 0 && ft != me && AttachThreadInput(me, ft, 1) != 0; + if pid != 0 { + AllowSetForegroundWindow(pid); + } + if iconic { + ShowWindow(w, SW_RESTORE); + } + BringWindowToTop(target); + let ok = SetForegroundWindow(target) != 0; + if attach { + AttachThreadInput(me, ft, 0); + } + + // 2) Fallback only when SetForegroundWindow actually failed (looking at the desktop with no attachable + // foreground thread, or UIPI refused AttachThreadInput). Never run this after a successful cross-process + // SetForegroundWindow: GetForegroundWindow() can lag behind an exclusive-fullscreen mode switch, and a + // synthetic Alt tap then knocks the game right back out of fullscreen. + if !ok && !is_fg(w, target) { + keys(&[(VK_MENU, false), (0xE8, false), (0xE8, true)]); + if pid != 0 { + AllowSetForegroundWindow(pid); + } + if IsIconic(w) != 0 { + ShowWindow(w, SW_RESTORE); + } + BringWindowToTop(target); + let ok2 = SetForegroundWindow(target) != 0; + keys(&[(VK_MENU, true)]); + if !ok2 && !is_fg(w, target) { + SwitchToThisWindow(target, 1); + } + } + } +} + +fn last_active_popup(w: HWND) -> HWND { + unsafe { + let p = GetLastActivePopup(w); + if !p.is_null() && p != w && IsWindowVisible(p) != 0 && IsWindowEnabled(p) != 0 { p } else { w } + } +} + +fn is_fg(w: HWND, target: HWND) -> bool { + let fg = unsafe { GetForegroundWindow() }; + !fg.is_null() && (fg == target || fg == w) +} + fn keys(k: &[(u16, bool)]) { for &(vk, up) in k { let mut i: INPUT = unsafe { std::mem::zeroed() }; diff --git a/plugins/topbar/src/main.rs b/plugins/topbar/src/main.rs index e1efe5ee..a0097d65 100644 --- a/plugins/topbar/src/main.rs +++ b/plugins/topbar/src/main.rs @@ -228,6 +228,8 @@ thread_local! { static TF_ANIM: Cell<(u64, i32, i32)> = const { Cell::new((0, 0, 0)) }; // tray flyout: when it opened, its final x, y static FHOT: Cell = const { Cell::new(-1) }; static HOT: Cell = const { Cell::new(NONE) }; + static DOWN_BTN: Cell = const { Cell::new(NONE) }; // app button pressed on WM_LBUTTONDOWN, activated on WM_LBUTTONUP + static NEED_PLACE: Cell = const { Cell::new(false) }; // display/pos change arrived while fullscreen; run place() after leaving fullscreen static ORDER: RefCell> = const { RefCell::new(Vec::new()) }; // unpinned windows in the order their buttons were added static HV: RefCell> = RefCell::new(HashMap::new()); // hover fade per element: (0..1, last update ms) static BUSY: Cell = const { Cell::new(false) }; // a fade is still running: the painter asks for another frame @@ -548,7 +550,10 @@ unsafe fn alt_tab(back: bool) { if n == 0 { return SWITCHING.store(false, Relaxed); } - SW.set((v, if n == 1 { 0 } else if back { n - 1 } else { 1 })); + let fg = GetForegroundWindow(); + let fg_root = if fg.is_null() { null_mut() } else { GetAncestor(fg, GA_ROOTOWNER) }; + let idx = initial_alt_tab_index(&v, fg_root, back); + SW.set((v, idx)); SW_MON.set(mon); sw_layout(sw); } else { @@ -557,6 +562,19 @@ unsafe fn alt_tab(back: bool) { InvalidateRect(sw, null(), 0); } +/// First card highlighted when the Alt+Tab grid opens: the previous window (`1`) when `v[0]` is already +/// the active app, or `0` when the desktop (not in `v`) is currently in front. +fn initial_alt_tab_index(v: &[HWND], fg_root: HWND, back: bool) -> usize { + let n = v.len(); + if n <= 1 { + return 0; + } + if back { + return n - 1; + } + if !fg_root.is_null() && v[0] == fg_root { 1 } else { 0 } +} + /// Sizes the switcher for its windows (centered on its monitor) and puts the live previews on the cards. unsafe fn sw_layout(sw: HWND) { let mon = SW_MON.get(); @@ -604,10 +622,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); + switch_to(w); } } @@ -713,7 +728,13 @@ unsafe fn hide_taskbar_if_shown() { } /// Reserves the bottom strip of the primary monitor (maximized windows stay above it). +/// Deferred while a fullscreen app owns the display: changing the work area or reasserting HWND_TOPMOST +/// during an exclusive-fullscreen mode switch knocks the game back to the desktop. unsafe fn place(hwnd: HWND) { + if FULLSCREEN.load(Relaxed) { + NEED_PLACE.set(true); + return; + } apply_opacity(hwnd); let (cx, cy) = (GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN)); let mut ab: APPBARDATA = zeroed(); @@ -1321,7 +1342,9 @@ fn refresh_status() { }) }); // explorer re-raises its own Shell_TrayWnd now and then; ours must stay in front so apps keep finding it first - SetWindowPos(HOST.get(), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE); + if !FULLSCREEN.load(Relaxed) { + SetWindowPos(HOST.get(), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE); + } } } @@ -1472,9 +1495,27 @@ unsafe fn bar_anim(hwnd: HWND) { SetTimer(hwnd, 5, 15, None); } +/// Hides the bar immediately without animating: an exclusive-fullscreen game drops back to the desktop if an +/// elevated topmost bar keeps calling SetWindowPos(HWND_TOPMOST) over it while sliding out. +unsafe fn hide_bar_now(hwnd: HWND) { + KillTimer(hwnd, 5); + KillTimer(hwnd, 4); + BAR_P.set(0.0); + fly::close_now(); + hide_tip(hwnd); + thumbs::hide_preview(); + if IsWindowVisible(hwnd) != 0 { + ShowWindow(hwnd, SW_HIDE); + } +} + unsafe fn bar_step(hwnd: HWND) { - // hides for a fullscreen game or video, and with "auto-hide" on while the pointer is away (never otherwise: not over the desktop, not in any style) - let target = if FULLSCREEN.load(Relaxed) || (i18n::style::autohide() && DESK_HIDE.load(Relaxed)) { 0.0 } else { 1.0 }; + if FULLSCREEN.load(Relaxed) { + hide_bar_now(hwnd); + return; + } + // hides with "auto-hide" on while the pointer is away (never otherwise: not over the desktop, not in any style) + let target = if i18n::style::autohide() && DESK_HIDE.load(Relaxed) { 0.0 } else { 1.0 }; let now = now_ms(); let dt = now.saturating_sub(BAR_T.replace(now)).min(50) as f32; let p = BAR_P.get(); @@ -1493,26 +1534,59 @@ unsafe fn bar_step(hwnd: HWND) { } } -/// True when the foreground window covers the whole primary monitor (game, F11 video, presentation). -/// 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 { +/// Converts a `WINDOWPLACEMENT::rcNormalPosition` (workspace coords) back to screen coords on `mon`. +fn work_to_screen(r: RECT, work: RECT, mon: RECT) -> RECT { + let (dx, dy) = (work.left - mon.left, work.top - mon.top); + RECT { left: r.left + dx, top: r.top + dy, right: r.right + dx, bottom: r.bottom + dy } +} + +/// True when `w` covers (or, when minimized, will restore to cover) the whole primary monitor. +unsafe fn is_fullscreen_window(w: HWND, bar: HWND) -> bool { + if w.is_null() || w == bar || IsWindowVisible(w) == 0 { return false; } let mut cls = [0u16; 64]; - let n = GetClassNameW(fg, cls.as_mut_ptr(), 64).max(0) as usize; + let n = GetClassNameW(w, 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") { return false; // the desktop itself is full size (rs-shell's desktop is a rust-explorer window; a file manager is never "fullscreen") } - let mut r: RECT = zeroed(); - GetWindowRect(fg, &mut r); - let mut mi: MONITORINFO = zeroed(); - mi.cbSize = size_of::() as u32; - 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 caption = GetWindowLongW(w, GWL_STYLE) as u32 & WS_CAPTION == WS_CAPTION; + let (r, m, zoomed) = if IsIconic(w) != 0 { + // A minimized window sits at (-32000, -32000): check where Windows will restore it. + let mut wp: WINDOWPLACEMENT = zeroed(); + wp.length = size_of::() as u32; + if GetWindowPlacement(w, &mut wp) == 0 { + return false; + } + let mut mi: MONITORINFO = zeroed(); + mi.cbSize = size_of::() as u32; + if GetMonitorInfoW(MonitorFromRect(&wp.rcNormalPosition, MONITOR_DEFAULTTONEAREST), &mut mi) == 0 { + return false; + } + let tool = GetWindowLongW(w, GWL_EXSTYLE) as u32 & WS_EX_TOOLWINDOW != 0; + let r = if tool { wp.rcNormalPosition } else { work_to_screen(wp.rcNormalPosition, mi.rcWork, mi.rcMonitor) }; + (r, mi.rcMonitor, wp.flags & WPF_RESTORETOMAXIMIZED != 0) + } else { + let mut r: RECT = zeroed(); + GetWindowRect(w, &mut r); + let mut mi: MONITORINFO = zeroed(); + mi.cbSize = size_of::() as u32; + if GetMonitorInfoW(MonitorFromWindow(w, MONITOR_DEFAULTTONEAREST), &mut mi) == 0 { + return false; + } + (r, mi.rcMonitor, IsZoomed(w) != 0) + }; + (m.left, m.top) == (0, 0) && covers_monitor(r, m, caption, zoomed) +} + +/// True when the foreground window covers the whole primary monitor (game, F11 video, presentation). +/// 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() || IsIconic(fg) != 0 { + return false; + } + is_fullscreen_window(fg, bar) } /// A framed maximized window overhangs the monitor by its border and is not fullscreen (#88); but a game in "borderless @@ -1551,11 +1625,15 @@ unsafe fn fs_update(hwnd: HWND) { } if FULLSCREEN.swap(fs, Relaxed) != fs { if fs { - fly::close_now(); - hide_tip(hwnd); - thumbs::hide_preview(); + hide_bar_now(hwnd); + } else { + if NEED_PLACE.replace(false) { + place(hwnd); + } + bar_anim(hwnd); } - bar_anim(hwnd); + } else if !fs && NEED_PLACE.replace(false) { + place(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 @@ -1596,6 +1674,17 @@ unsafe fn force_fg(hw: HWND) { winrust_common::fg::force(hw); } +/// Switches to another application's window (restoring it if minimized). Hides the bar first when switching to a +/// fullscreen window on the primary monitor so the topmost bar does not disrupt a DXGI fullscreen transition. +pub(crate) unsafe fn switch_to(w: HWND) { + let bar = BAR.load(Relaxed) as HWND; + if !bar.is_null() && is_fullscreen_window(w, bar) { + FULLSCREEN.store(true, Relaxed); + hide_bar_now(bar); + } + winrust_common::fg::activate(w); +} + /// 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) { @@ -2259,12 +2348,7 @@ unsafe fn app_menu(hwnd: HWND, h: i32) { let first_pid = wins.first().map_or(0, |&w| { let mut pid = 0; GetWindowThreadProcessId(w, &mut pid); pid }); 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); - } + Some(A::Win(w)) => switch_to(w), Some(A::Open) => open(&wide(&launch)), Some(A::Pin) => { // by path, not by the index seen when the menu opened: PINS may have changed meanwhile @@ -2574,10 +2658,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); + switch_to(w); } } @@ -2675,10 +2756,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); + switch_to(w); } } _ => {} @@ -2799,7 +2877,7 @@ unsafe extern "system" fn wndproc(hwnd: HWND, msg: u32, wp: WPARAM, lp: LPARAM) } else if GetKeyboardLayout(tid) as usize == before { let w = app_window(); if GetForegroundWindow() != w { - force_fg(w); // Win+Space switches the window in front: put the app back there first + switch_to(w); // Win+Space switches the window in front: put the app back there first } keys(&[VK_LWIN, VK_SPACE]); // the app ignored the request: the system switcher always works (with explorer) } @@ -2858,6 +2936,7 @@ unsafe extern "system" fn wndproc(hwnd: HWND, msg: u32, wp: WPARAM, lp: LPARAM) } WM_MOUSELEAVE => { hide_tip(hwnd); + DOWN_BTN.set(NONE); HOT.set(-1); InvalidateRect(hwnd, null(), 0); } @@ -2880,23 +2959,44 @@ unsafe extern "system" fn wndproc(hwnd: HWND, msg: u32, wp: WPARAM, lp: LPARAM) if h != NONE { press(h); } - if h == NONE && msg == WM_RBUTTONDOWN { - fly::context_menu(x); // right click on an empty spot - } else if h >= 1 && msg == WM_RBUTTONDOWN { - app_menu(hwnd, h); + if h >= 1 && msg == WM_LBUTTONDOWN { + // Switch / launch app buttons on release (like the Windows taskbar and preview / Alt+Tab cards): + // activating on DOWN leaves the mouse button held during AttachThreadInput and fullscreen restore. + DOWN_BTN.set(h); } else { - click(h, msg == WM_LBUTTONDOWN, msg == WM_MBUTTONDOWN); + DOWN_BTN.set(NONE); + if h == NONE && msg == WM_RBUTTONDOWN { + fly::context_menu(x); // right click on an empty spot + } else if h >= 1 && msg == WM_RBUTTONDOWN { + app_menu(hwnd, h); + } else { + click(h, msg == WM_LBUTTONDOWN, msg == WM_MBUTTONDOWN); + } } InvalidateRect(hwnd, null(), 0); } + WM_LBUTTONUP => { + let down = DOWN_BTN.replace(NONE); + let h = hit(hwnd, (lp & 0xffff) as i16 as i32, (lp >> 16) as i16 as i32); + if down >= 1 && h == down { + click(h, true, false); + if IsWindowVisible(hwnd) != 0 { + InvalidateRect(hwnd, null(), 0); + } + } + } CALLBACK => { if wp == ABN_FULLSCREENAPP as usize { fs_update(hwnd); } else if wp == ABN_POSCHANGED as usize { - place(hwnd); + NEED_PLACE.set(true); + fs_update(hwnd); } } - WM_DISPLAYCHANGE => place(hwnd), + WM_DISPLAYCHANGE => { + NEED_PLACE.set(true); + fs_update(hwnd); + } WM_DESTROY => { let mut ab: APPBARDATA = zeroed(); ab.cbSize = size_of::() as u32; @@ -3069,13 +3169,31 @@ mod tests { #[test] fn borderless_games_count_as_fullscreen() { - use super::{RECT, covers_monitor}; + use super::{RECT, covers_monitor, work_to_screen}; let m = RECT { left: 0, top: 0, right: 1920, bottom: 1080 }; assert!(covers_monitor(m, m, false, false)); // plain borderless / exclusive fullscreen assert!(covers_monitor(m, m, true, false)); // borderless window that kept its caption style assert!(!covers_monitor(RECT { left: -8, top: -8, right: 1928, bottom: 1088 }, m, true, true)); // framed maximized window (#88) assert!(!covers_monitor(m, m, true, true)); // a maximized window is never a game assert!(!covers_monitor(RECT { left: 0, top: 0, right: 1920, bottom: 1032 }, m, false, false)); // leaves the bar's strip + // minimized fullscreen restore rect (workspace coords) under bottom, top and left bars + let bot_w = RECT { left: 0, top: 0, right: 1920, bottom: 1040 }; + assert!(covers_monitor(work_to_screen(m, bot_w, m), m, false, false)); + let top_w = RECT { left: 0, top: 40, right: 1920, bottom: 1080 }; + assert!(covers_monitor(work_to_screen(RECT { left: 0, top: -40, right: 1920, bottom: 1040 }, top_w, m), m, false, false)); + let left_w = RECT { left: 60, top: 0, right: 1920, bottom: 1080 }; + assert!(covers_monitor(work_to_screen(RECT { left: -60, top: 0, right: 1860, bottom: 1080 }, left_w, m), m, false, false)); + } + + #[test] + fn alt_tab_initial_selection_respects_desktop_focus() { + use super::{HWND, initial_alt_tab_index}; + let (a, b, c) = (1usize as HWND, 2usize as HWND, 3usize as HWND); + assert_eq!(initial_alt_tab_index(&[a, b, c], a, false), 1); // active app is v[0]: switch to previous + assert_eq!(initial_alt_tab_index(&[a, b, c], a, true), 2); // Shift+Alt+Tab goes to last + assert_eq!(initial_alt_tab_index(&[a, b, c], 99usize as HWND, false), 0); // desktop in front: pick v[0] + assert_eq!(initial_alt_tab_index(&[a, b, c], std::ptr::null_mut(), false), 0); + assert_eq!(initial_alt_tab_index(&[a], a, false), 0); } #[test] diff --git a/plugins/topbar/src/thumbs.rs b/plugins/topbar/src/thumbs.rs index 20b15f3f..f17fd77f 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); + switch_to(w); } } WM_TIMER if wp == 1 => { 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 2/3] 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 => { From e97bcad5bc576d16afb2d80c5203f4d4a123c4c9 Mon Sep 17 00:00:00 2001 From: Enthony Araujo de Oliveira Date: Fri, 27 Nov 2026 11:21:47 -0300 Subject: [PATCH 3/3] topbar: fix remaining fullscreen edge cases on top of #197 Rebased on #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. --- plugins/topbar/src/main.rs | 276 +++++++++++++++++++++++++++++++------ 1 file changed, 233 insertions(+), 43 deletions(-) diff --git a/plugins/topbar/src/main.rs b/plugins/topbar/src/main.rs index 1b232302..c66c3a9d 100644 --- a/plugins/topbar/src/main.rs +++ b/plugins/topbar/src/main.rs @@ -233,6 +233,8 @@ thread_local! { static TF_ANIM: Cell<(u64, i32, i32)> = const { Cell::new((0, 0, 0)) }; // tray flyout: when it opened, its final x, y static FHOT: Cell = const { Cell::new(-1) }; static HOT: Cell = const { Cell::new(NONE) }; + static DOWN_BTN: Cell = const { Cell::new(NONE) }; // app button pressed on WM_LBUTTONDOWN, activated on WM_LBUTTONUP + static NEED_PLACE: Cell = const { Cell::new(false) }; // display/pos change arrived while fullscreen; run place() once fullscreen ends static ORDER: RefCell> = const { RefCell::new(Vec::new()) }; // unpinned windows in the order their buttons were added static HV: RefCell> = RefCell::new(HashMap::new()); // hover fade per element: (0..1, last update ms) static BUSY: Cell = const { Cell::new(false) }; // a fade is still running: the painter asks for another frame @@ -566,7 +568,10 @@ unsafe fn alt_tab(back: bool) { if n == 0 { return SWITCHING.store(false, Relaxed); } - SW.set((v, if n == 1 { 0 } else if back { n - 1 } else { 1 })); + let fg = GetForegroundWindow(); + let fg_root = if fg.is_null() { null_mut() } else { GetAncestor(fg, GA_ROOTOWNER) }; + let idx = initial_alt_tab_index(&v, fg_root, back); + SW.set((v, idx)); SW_MON.set(mon); SW_PT.set(Some(cursor())); sw_layout(sw); @@ -576,6 +581,19 @@ unsafe fn alt_tab(back: bool) { InvalidateRect(sw, null(), 0); } +/// First card highlighted when the Alt+Tab grid opens: the previous window (`1`) when `v[0]` is already +/// the active app, or `0` when the desktop (not in `v`) is currently in front. +fn initial_alt_tab_index(v: &[HWND], fg_root: HWND, back: bool) -> usize { + let n = v.len(); + if n <= 1 { + return 0; + } + if back { + return n - 1; + } + if !fg_root.is_null() && v[0] == fg_root { 1 } else { 0 } +} + /// Sizes the switcher for its windows (centered on its monitor) and puts the live previews on the cards. unsafe fn sw_layout(sw: HWND) { let mon = SW_MON.get(); @@ -733,7 +751,14 @@ unsafe fn hide_taskbar_if_shown() { } /// Reserves the bottom strip of the primary monitor (maximized windows stay above it). +/// Deferred while a fullscreen app owns the display: a lower-resolution game fires WM_DISPLAYCHANGE, and running +/// set_work_area / fit_maximized then shrinks maximized windows to the game's resolution (fit_maximized only trims, +/// so they stay stuck shrunk after leaving the game). unsafe fn place(hwnd: HWND) { + if FULLSCREEN.load(Relaxed) { + NEED_PLACE.set(true); + return; + } apply_opacity(hwnd); let (cx, cy) = (GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN)); let mut ab: APPBARDATA = zeroed(); @@ -1359,7 +1384,9 @@ fn refresh_status() { }) }); // explorer re-raises its own Shell_TrayWnd now and then; ours must stay in front so apps keep finding it first - SetWindowPos(HOST.get(), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE); + if !FULLSCREEN.load(Relaxed) { + SetWindowPos(HOST.get(), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE); + } } } @@ -1543,9 +1570,27 @@ unsafe fn bar_anim(hwnd: HWND) { SetTimer(hwnd, 5, 15, None); } +/// Hides the bar without the 240 ms slide-out: a visible topmost bar overlapping the primary monitor while a game +/// enters exclusive fullscreen can knock DXGI back out of fullscreen. +unsafe fn hide_bar_now(hwnd: HWND) { + KillTimer(hwnd, 5); + KillTimer(hwnd, 4); + BAR_P.set(0.0); + fly::close_now(); + hide_tip(hwnd); + thumbs::hide_preview(); + if IsWindowVisible(hwnd) != 0 { + ShowWindow(hwnd, SW_HIDE); + } +} + unsafe fn bar_step(hwnd: HWND) { - // hides for a fullscreen game or video, and with "auto-hide" on while the pointer is away (never otherwise: not over the desktop, not in any style) - let target = if FULLSCREEN.load(Relaxed) || (i18n::style::autohide() && DESK_HIDE.load(Relaxed)) { 0.0 } else { 1.0 }; + if FULLSCREEN.load(Relaxed) { + hide_bar_now(hwnd); + return; + } + // hides with "auto-hide" on while the pointer is away (never otherwise: not over the desktop, not in any style) + let target = if i18n::style::autohide() && DESK_HIDE.load(Relaxed) { 0.0 } else { 1.0 }; let now = now_ms(); let dt = now.saturating_sub(BAR_T.replace(now)).min(50) as f32; let p = BAR_P.get(); @@ -1566,36 +1611,85 @@ unsafe fn bar_step(hwnd: HWND) { } } +/// Converts a `WINDOWPLACEMENT::rcNormalPosition` (workspace coords) back to screen coords on `mon`. +fn work_to_screen(r: RECT, work: RECT, mon: RECT) -> RECT { + let (dx, dy) = (work.left - mon.left, work.top - mon.top); + RECT { left: r.left + dx, top: r.top + dy, right: r.right + dx, bottom: r.bottom + dy } +} + +fn is_shell_surface(w: HWND) -> bool { + let mut cls = [0u16; 64]; + let n = unsafe { GetClassNameW(w, cls.as_mut_ptr(), 64) }.max(0) as usize; + matches!(String::from_utf16_lossy(&cls[..n]).as_str(), "Progman" | "WorkerW" | "Shell_TrayWnd" | "Shell_SecondaryTrayWnd" | "RustExplorer") +} + +/// A minimized window whose restored rect covers the primary monitor (catches fullscreen games when FS_WND was +/// overwritten by another fullscreen window such as an F11 browser, or after the bar restarted). +unsafe fn iconic_covers_primary(w: HWND) -> bool { + if is_shell_surface(w) { + return false; + } + let mut wp: WINDOWPLACEMENT = zeroed(); + wp.length = size_of::() as u32; + if GetWindowPlacement(w, &mut wp) == 0 { + return false; + } + let mut mi: MONITORINFO = zeroed(); + mi.cbSize = size_of::() as u32; + if GetMonitorInfoW(MonitorFromRect(&wp.rcNormalPosition, MONITOR_DEFAULTTONEAREST), &mut mi) == 0 { + return false; + } + let tool = GetWindowLongW(w, GWL_EXSTYLE) as u32 & WS_EX_TOOLWINDOW != 0; + let r = if tool { wp.rcNormalPosition } else { work_to_screen(wp.rcNormalPosition, mi.rcWork, mi.rcMonitor) }; + let caption = GetWindowLongW(w, GWL_STYLE) as u32 & WS_CAPTION == WS_CAPTION; + (mi.rcMonitor.left, mi.rcMonitor.top) == (0, 0) && covers_monitor(r, mi.rcMonitor, caption, wp.flags & WPF_RESTORETOMAXIMIZED != 0) +} + +unsafe fn window_covers_primary(w: HWND, bar: HWND) -> bool { + if w.is_null() || w == bar || IsWindowVisible(w) == 0 || is_shell_surface(w) { + return false; + } + let mut r: RECT = zeroed(); + GetWindowRect(w, &mut r); + let mut mi: MONITORINFO = zeroed(); + mi.cbSize = size_of::() as u32; + if GetMonitorInfoW(MonitorFromWindow(w, MONITOR_DEFAULTTONEAREST), &mut mi) == 0 { + return false; + } + let m = mi.rcMonitor; + let caption = GetWindowLongW(w, GWL_STYLE) as u32 & WS_CAPTION == WS_CAPTION; + (m.left, m.top) == (0, 0) && covers_monitor(r, m, caption, IsZoomed(w) != 0) +} + /// True when the foreground window covers the whole primary monitor (game, F11 video, presentation). /// 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(); + // A Direct3D game Windows reports as exclusive-fullscreen counts even during the brief moment when GetForegroundWindow() + // is null across a display-mode switch (unless one of our own panels, e.g. Start, is in front). + if !own_foreground() { + let mut quns = 0; + if windows_sys::Win32::UI::Shell::SHQueryUserNotificationState(&mut quns) >= 0 && quns == 3 { + return true; // QUNS_RUNNING_D3D_FULL_SCREEN + } + } 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 - } + // over: if the bar stayed on top then, the game lost fullscreen again. Both the last seen fullscreen window (which also + // covers lower-resolution stretched games) and any minimized window whose restored rect covers the primary monitor count. 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") { - return false; // the desktop itself is full size (rs-shell's desktop is a rust-explorer window; a file manager is never "fullscreen") + if fg as isize == FS_WND.get() { + return true; + } + let fs = iconic_covers_primary(fg); + if fs { + FS_WND.set(fg as isize); + } + return fs; } - let mut r: RECT = zeroed(); - GetWindowRect(fg, &mut r); - let mut mi: MONITORINFO = zeroed(); - mi.cbSize = size_of::() as u32; - GetMonitorInfoW(MonitorFromWindow(fg, MONITOR_DEFAULTTONEAREST), &mut mi); - let m = mi.rcMonitor; - let caption = GetWindowLongW(fg, GWL_STYLE) as u32 & WS_CAPTION == WS_CAPTION; - let fs = (m.left, m.top) == (0, 0) && covers_monitor(r, m, caption, IsZoomed(fg) != 0); + let fs = window_covers_primary(fg, bar); if fs { FS_WND.set(fg as isize); } @@ -1660,11 +1754,15 @@ unsafe fn fs_update(hwnd: HWND) { } if FULLSCREEN.swap(fs, Relaxed) != fs { if fs { - fly::close_now(); - hide_tip(hwnd); - thumbs::hide_preview(); + hide_bar_now(hwnd); + } else { + if NEED_PLACE.replace(false) { + place(hwnd); + } + bar_anim(hwnd); } - bar_anim(hwnd); + } else if !fs && NEED_PLACE.replace(false) { + place(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 @@ -1705,20 +1803,72 @@ 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). +/// Switches to a window (Alt+Tab, its button, a preview, a menu). Minimized windows go through SwitchToThisWindow +/// like Windows' own Alt+Tab; if the target is fullscreen on the primary monitor, the bar hides beforehand. Unlike +/// `force_fg` (meant for our own popups), this resolves owned modal popups and avoids injecting synthetic Alt keys +/// once `SetForegroundWindow` already succeeded. pub(crate) unsafe fn bring(w: HWND) { + if w.is_null() || IsWindow(w) == 0 { + return; + } + let bar = BAR.load(Relaxed) as HWND; + if !bar.is_null() && (w as isize == FS_WND.get() || if IsIconic(w) != 0 { iconic_covers_primary(w) } else { window_covers_primary(w, bar) }) { + FULLSCREEN.store(true, Relaxed); + FS_OFF.set(0); + hide_bar_now(bar); + } + let p = GetLastActivePopup(w); + let target = if !p.is_null() && p != w && IsWindowVisible(p) != 0 && IsWindowEnabled(p) != 0 { p } else { w }; + let is_fg = || { + let fg = GetForegroundWindow(); + !fg.is_null() && (fg == target || fg == w) + }; + let mut pid = 0u32; + GetWindowThreadProcessId(target, &mut pid); + if pid != 0 { + AllowSetForegroundWindow(pid); + } if IsIconic(w) != 0 { - SwitchToThisWindow(w, 1); - if GetForegroundWindow() == w { + SwitchToThisWindow(target, 1); + if is_fg() { return; } + } else if SetForegroundWindow(target) != 0 { + return; + } + let me = windows_sys::Win32::System::Threading::GetCurrentThreadId(); + let fg = GetForegroundWindow(); + let ft = if !fg.is_null() { GetWindowThreadProcessId(fg, null_mut()) } else { 0 }; + let attach = ft != 0 && ft != me && windows_sys::Win32::System::Threading::AttachThreadInput(me, ft, 1) != 0; + if pid != 0 { + AllowSetForegroundWindow(pid); } - force_fg(w); if IsIconic(w) != 0 { ShowWindow(w, SW_RESTORE); } + BringWindowToTop(target); + let ok = SetForegroundWindow(target) != 0; + if attach { + windows_sys::Win32::System::Threading::AttachThreadInput(me, ft, 0); + } + if !ok && !is_fg() { + let send = |vk: u16, up: bool| { + let mut i: INPUT = zeroed(); + i.r#type = INPUT_KEYBOARD; + i.Anonymous.ki.wVk = vk; + i.Anonymous.ki.dwFlags = if up { KEYEVENTF_KEYUP } else { 0 }; + SendInput(1, &i, size_of::() as i32); + }; + for &(vk, up) in &[(VK_MENU, false), (0xE8, false), (0xE8, true)] { + send(vk, up); + } + if IsIconic(w) != 0 { + ShowWindow(w, SW_RESTORE); + } + BringWindowToTop(target); + SetForegroundWindow(target); + send(VK_MENU, true); + } } /// Fills `r` (right / bottom exclusive) in colour `c`; `rad` > 0 rounds the corners (rad = corner diameter), anti-aliased @@ -3025,7 +3175,7 @@ unsafe extern "system" fn wndproc(hwnd: HWND, msg: u32, wp: WPARAM, lp: LPARAM) } else if GetKeyboardLayout(tid) as usize == before { let w = app_window(); if GetForegroundWindow() != w { - force_fg(w); // Win+Space switches the window in front: put the app back there first + bring(w); // Win+Space switches the window in front: put the app back there first } keys(&[VK_LWIN, VK_SPACE]); // the app ignored the request: the system switcher always works (with explorer) } @@ -3089,6 +3239,7 @@ unsafe extern "system" fn wndproc(hwnd: HWND, msg: u32, wp: WPARAM, lp: LPARAM) } WM_MOUSELEAVE => { hide_tip(hwnd); + DOWN_BTN.set(NONE); HOT.set(-1); InvalidateRect(hwnd, null(), 0); } @@ -3111,23 +3262,44 @@ unsafe extern "system" fn wndproc(hwnd: HWND, msg: u32, wp: WPARAM, lp: LPARAM) if h != NONE { press(h); } - if h == NONE && msg == WM_RBUTTONDOWN { - fly::context_menu(x); // right click on an empty spot - } else if h >= 1 && msg == WM_RBUTTONDOWN { - app_menu(hwnd, h); + if h >= 1 && msg == WM_LBUTTONDOWN { + // Switch / launch app buttons on release (like the Windows taskbar and preview / Alt+Tab cards): + // activating on DOWN leaves the mouse button held during AttachThreadInput and fullscreen restore. + DOWN_BTN.set(h); } else { - click(h, msg == WM_LBUTTONDOWN, msg == WM_MBUTTONDOWN); + DOWN_BTN.set(NONE); + if h == NONE && msg == WM_RBUTTONDOWN { + fly::context_menu(x); // right click on an empty spot + } else if h >= 1 && msg == WM_RBUTTONDOWN { + app_menu(hwnd, h); + } else { + click(h, msg == WM_LBUTTONDOWN, msg == WM_MBUTTONDOWN); + } } InvalidateRect(hwnd, null(), 0); } + WM_LBUTTONUP => { + let down = DOWN_BTN.replace(NONE); + let h = hit(hwnd, (lp & 0xffff) as i16 as i32, (lp >> 16) as i16 as i32); + if down >= 1 && h == down { + click(h, true, false); + if IsWindowVisible(hwnd) != 0 { + InvalidateRect(hwnd, null(), 0); + } + } + } CALLBACK => { if wp == ABN_FULLSCREENAPP as usize { fs_update(hwnd); } else if wp == ABN_POSCHANGED as usize { - place(hwnd); + NEED_PLACE.set(true); + fs_update(hwnd); } } - WM_DISPLAYCHANGE => place(hwnd), + WM_DISPLAYCHANGE => { + NEED_PLACE.set(true); + fs_update(hwnd); + } WM_DESTROY => { let mut ab: APPBARDATA = zeroed(); ab.cbSize = size_of::() as u32; @@ -3314,13 +3486,31 @@ mod tests { #[test] fn borderless_games_count_as_fullscreen() { - use super::{RECT, covers_monitor}; + use super::{RECT, covers_monitor, work_to_screen}; let m = RECT { left: 0, top: 0, right: 1920, bottom: 1080 }; assert!(covers_monitor(m, m, false, false)); // plain borderless / exclusive fullscreen assert!(covers_monitor(m, m, true, false)); // borderless window that kept its caption style assert!(!covers_monitor(RECT { left: -8, top: -8, right: 1928, bottom: 1088 }, m, true, true)); // framed maximized window (#88) assert!(!covers_monitor(m, m, true, true)); // a maximized window is never a game assert!(!covers_monitor(RECT { left: 0, top: 0, right: 1920, bottom: 1032 }, m, false, false)); // leaves the bar's strip + // minimized fullscreen restore rect (workspace coords) under bottom, top and left bars + let bot_w = RECT { left: 0, top: 0, right: 1920, bottom: 1040 }; + assert!(covers_monitor(work_to_screen(m, bot_w, m), m, false, false)); + let top_w = RECT { left: 0, top: 40, right: 1920, bottom: 1080 }; + assert!(covers_monitor(work_to_screen(RECT { left: 0, top: -40, right: 1920, bottom: 1040 }, top_w, m), m, false, false)); + let left_w = RECT { left: 60, top: 0, right: 1920, bottom: 1080 }; + assert!(covers_monitor(work_to_screen(RECT { left: -60, top: 0, right: 1860, bottom: 1080 }, left_w, m), m, false, false)); + } + + #[test] + fn alt_tab_initial_selection_respects_desktop_focus() { + use super::{HWND, initial_alt_tab_index}; + let (a, b, c) = (1usize as HWND, 2usize as HWND, 3usize as HWND); + assert_eq!(initial_alt_tab_index(&[a, b, c], a, false), 1); // active app is v[0]: switch to previous + assert_eq!(initial_alt_tab_index(&[a, b, c], a, true), 2); // Shift+Alt+Tab goes to last + assert_eq!(initial_alt_tab_index(&[a, b, c], 99usize as HWND, false), 0); // desktop in front: pick v[0] + assert_eq!(initial_alt_tab_index(&[a, b, c], std::ptr::null_mut(), false), 0); + assert_eq!(initial_alt_tab_index(&[a], a, false), 0); } #[test]