A fast file manager for Windows and macOS that never freezes: tabs, dual panes, archives as folders, Quick Look-style previews. A File Explorer and Finder alternative written in Rust.
Rust archive-managerdesktop-appdual-paneexplorer-alternativefile-browserfile-explorerfile-managerfluent-designgpu-acceleratedgpuimacosmicaproductivityquick-lookrustrust-guitabswindowswindows-11windows-explorer
//! "Is a full-screen application in front?", so the islands can get out of the way of a game or a video and come back after it.
//!
//! The decision is pure ([`is_fullscreen`]): the window in front covers the whole monitor, is not the desktop or a shell window, is
//! not one of ours, and is not an ordinary maximised window. A maximised window covers the whole monitor too when the Windows
//! taskbar hides itself; it keeps its title bar, or (a frameless Electron or Qt window) its sizing frame together with the maximised
//! state. A game or a full-screen video has neither. Exceptions that keep `WS_CAPTION` bits in `GWL_STYLE` while presenting full
//! screen: a UWP frame window fitting the monitor exactly, and a borderless `WS_POPUP` window without a sizing frame (`WS_THICKFRAME`,
//! e.g. SDL / Source 2 games in borderless full-screen mode).
use super::layout::Rect;
/// What the decision needs to know about the window in front.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Foreground {
pub rect: Rect,
/// The desktop, the taskbar, the Start menu or another piece of the shell.
pub shell: bool,
/// A window of this process (the islands themselves, the app's own windows).
pub own: bool,
/// Has both the border and the dialog-frame style bits that make a title bar (`WS_CAPTION`).
pub caption: bool,
/// Is maximised (`WS_MAXIMIZE`) and has a sizing frame (`WS_THICKFRAME`): an ordinary maximised window, however it paints its
/// own title bar.
pub maximized_frame: bool,
/// A `WS_POPUP` window without a sizing frame (`WS_THICKFRAME`): games in borderless full-screen mode (such as SDL / Source 2)
/// may keep `WS_CAPTION` bits in `GWL_STYLE` while covering the monitor as a borderless popup.
pub borderless_popup: bool,
/// An `ApplicationFrameWindow`: the frame every UWP app lives in.
pub uwp_frame: bool,
}
/// The frame window of every UWP app.
pub const UWP_FRAME_CLASS: &str = "ApplicationFrameWindow";
/// Window classes of the shell: they may cover the whole screen without being a full-screen application.
pub const SHELL_CLASSES: [&str; 8] = [
"Progman",
"WorkerW",
"Shell_TrayWnd",
"Shell_SecondaryTrayWnd",
"Windows.UI.Core.CoreWindow",
"XamlExplorerHostIslandWindow",
"MultitaskingViewFrame",
"TaskListThumbnailWnd",
];
/// Whether `class` (a window class name) belongs to the shell.
pub fn is_shell_class(class: &str) -> bool {
SHELL_CLASSES.iter().any(|c| c.eq_ignore_ascii_case(class))
}
/// The decision: `fg` is the window in front (`None`: nothing is, or it could not be read), `monitor` the primary monitor's whole
/// rectangle (not its work area).
pub fn is_fullscreen(fg: Option<&Foreground>, monitor: Rect) -> bool {
let Some(fg) = fg else { return false };
if fg.shell || fg.own || fg.maximized_frame {
return false;
}
if monitor.width() <= 0 || monitor.height() <= 0 || !fg.rect.covers(&monitor) {
return false;
}
// a title bar means an ordinary window that happens to reach the edges - except a UWP app's frame fitting the monitor exactly,
// or a borderless WS_POPUP without a sizing frame (e.g. SDL / Source 2 games keeping WS_CAPTION in borderless full-screen mode)
!fg.caption || (fg.uwp_frame && fg.rect == monitor) || fg.borderless_popup
}
/// `WS_CAPTION` is `WS_BORDER | WS_DLGFRAME`: both bits make a title bar.
pub const WS_CAPTION_BITS: u32 = 0x00C0_0000;
pub fn has_caption(style: u32) -> bool {
style & WS_CAPTION_BITS == WS_CAPTION_BITS
}
const WS_POPUP: u32 = 0x8000_0000;
const WS_MAXIMIZE: u32 = 0x0100_0000;
const WS_THICKFRAME: u32 = 0x0004_0000;
/// A maximised window with a sizing frame (see [`Foreground::maximized_frame`]).
pub fn has_maximized_frame(style: u32) -> bool {
style & (WS_MAXIMIZE | WS_THICKFRAME) == (WS_MAXIMIZE | WS_THICKFRAME)
}
/// A `WS_POPUP` window without a sizing frame (`WS_THICKFRAME`) (see [`Foreground::borderless_popup`]).
pub fn is_borderless_popup(style: u32) -> bool {
(style & WS_POPUP) != 0 && (style & WS_THICKFRAME) == 0
}
/// Reads window information for any given HWND. `None` when there is none or it cannot be measured.
pub(crate) fn window_info(h: windows::Win32::Foundation::HWND) -> Option<Foreground> {
use windows::Win32::Foundation::RECT;
use windows::Win32::System::Threading::GetCurrentProcessId;
use windows::Win32::UI::WindowsAndMessaging::{GWL_STYLE, GetClassNameW, GetWindowLongPtrW, GetWindowRect, GetWindowThreadProcessId};
if h.0.is_null() {
return None;
}
unsafe {
let mut r = RECT::default();
GetWindowRect(h, &mut r).ok()?;
let mut pid = 0u32;
GetWindowThreadProcessId(h, Some(&mut pid));
let mut class = [0u16; 128];
let n = GetClassNameW(h, &mut class);
let class = String::from_utf16_lossy(&class[..n.max(0) as usize]);
let style = GetWindowLongPtrW(h, GWL_STYLE) as u32;
Some(Foreground {
rect: Rect::new(r.left, r.top, r.right, r.bottom),
// (our own desktop's cover windows are the desktop: never a full-screen application, whichever process asks - the first
// test knows this process's covers, the second reads the mark every cover of a build with this change carries, so that the
// cover of ANOTHER host process of such a build is not taken for a game either. A cover of an older build carries no mark
// and is not recognised; the cover pin, not this test, is what keeps a cover from ever reaching all four edges)
shell: is_shell_class(&class) || crate::desktop::cover::is_cover(h.0 as isize) || crate::desktop::cover::is_marked_cover(h.0 as isize),
own: pid == GetCurrentProcessId(),
caption: has_caption(style),
maximized_frame: has_maximized_frame(style),
borderless_popup: is_borderless_popup(style),
uwp_frame: class.eq_ignore_ascii_case(UWP_FRAME_CLASS),
})
}
}
/// Reads the window in front. `None` when there is none or it cannot be measured.
pub fn foreground() -> Option<Foreground> {
unsafe {
let h = windows::Win32::UI::WindowsAndMessaging::GetForegroundWindow();
window_info(h)
}
}
/// Whether a full-screen application is in front on `monitor` right now.
///
/// Designed for minimal CPU overhead (0ms, no full window enumeration):
/// 1. Fast-path: checks active `GetForegroundWindow()`.
/// 2. Spatial O(1) probe: samples `WindowFromPoint` at monitor center.
/// 3. Bounded Z-order walk: if needed, walks at most 15 top windows using `GetTopWindow`.
pub fn on_monitor(monitor: Rect) -> bool {
use windows::Win32::Foundation::POINT;
use windows::Win32::UI::WindowsAndMessaging::{GA_ROOT, GetAncestor, WindowFromPoint};
if monitor.width() <= 0 || monitor.height() <= 0 {
return false;
}
// 1. Fast path: if the active foreground window covers this monitor and is full screen
let fg = foreground();
if let Some(ref fg) = fg {
if fg.rect.covers(&monitor) {
return is_fullscreen(Some(fg), monitor);
}
}
// If the active foreground window is on this monitor and NOT full screen (and not our dock or desktop),
// then an ordinary window has active focus on this monitor, so it is not full screen.
if let Some(ref fg) = fg {
if !fg.shell && !fg.own && crate::dock::covered::meets(fg.rect, monitor) {
let overlap_w = fg.rect.right.min(monitor.right) - fg.rect.left.max(monitor.left);
let overlap_h = fg.rect.bottom.min(monitor.bottom) - fg.rect.top.max(monitor.top);
if overlap_w > 16 && overlap_h > 16 {
return false;
}
}
}
// 2. Spatial O(1) point query: a fullscreen video/game covers the center of the monitor
let cx = monitor.left + monitor.width() / 2;
let cy = monitor.top + monitor.height() / 2;
let child = unsafe { WindowFromPoint(POINT { x: cx, y: cy }) };
if !child.0.is_null() {
let root = unsafe { GetAncestor(child, GA_ROOT) };
if !root.0.is_null() {
if let Some(w) = window_info(root) {
if !w.shell && !w.own && w.rect.covers(&monitor) {
return is_fullscreen(Some(&w), monitor);
}
}
}
}
// 3. Bounded Z-order walk (at most 15 windows, early exit on first match on this monitor)
topmost_on_monitor(monitor)
}
fn topmost_on_monitor(monitor: Rect) -> bool {
use windows::Win32::Foundation::{HWND, RECT};
use windows::Win32::Graphics::Dwm::{DWMWA_CLOAKED, DwmGetWindowAttribute};
use windows::Win32::System::Threading::GetCurrentProcessId;
use windows::Win32::UI::WindowsAndMessaging::{
GW_HWNDNEXT, GW_OWNER, GWL_EXSTYLE, GetTopWindow, GetWindow, GetWindowLongPtrW, GetWindowRect,
GetWindowThreadProcessId, IsIconic, IsWindowVisible, WS_EX_NOACTIVATE, WS_EX_TOOLWINDOW,
};
let own_pid = unsafe { GetCurrentProcessId() };
unsafe {
let mut h = match GetTopWindow(None) {
Ok(h) if !h.0.is_null() => h,
_ => return false,
};
// At most 15 windows in Z-order: the top window on any monitor is always within the first few.
for _ in 0..15 {
if IsWindowVisible(h).as_bool() && !IsIconic(h).as_bool() {
let mut r = RECT::default();
if GetWindowRect(h, &mut r).is_ok() {
let w_rect = Rect::new(r.left, r.top, r.right, r.bottom);
// Fast integer check: does window overlap with this monitor?
let overlap_w = w_rect.right.min(monitor.right) - w_rect.left.max(monitor.left);
let overlap_h = w_rect.bottom.min(monitor.bottom) - w_rect.top.max(monitor.top);
if overlap_w > 16 && overlap_h > 16 {
// Check if it's a tool/popup window or owned
let ex = GetWindowLongPtrW(h, GWL_EXSTYLE) as u32;
let tool = ex & (WS_EX_TOOLWINDOW.0 | WS_EX_NOACTIVATE.0) != 0;
let owned = GetWindow(h, GW_OWNER).is_ok_and(|o| !o.is_invalid() && !o.0.is_null());
let mut pid = 0u32;
GetWindowThreadProcessId(h, Some(&mut pid));
let mut cloaked = 0u32;
let _ = DwmGetWindowAttribute(h, DWMWA_CLOAKED, &mut cloaked as *mut _ as *mut _, 4);
if !tool && !owned && pid != own_pid && cloaked == 0 {
if let Some(fg) = window_info(h) {
if fg.shell {
if fg.rect.covers(&monitor) {
return false; // Desktop wallpaper covers monitor
}
} else {
// Topmost application window on this monitor found!
return is_fullscreen(Some(&fg), monitor);
}
}
}
}
}
}
match GetWindow(HWND(h.0), GW_HWNDNEXT) {
Ok(next) if !next.0.is_null() => h = next,
_ => break,
}
}
}
false
}
/// Whether a full-screen application is in front on the primary monitor right now. Cheap (a few window calls): fine to call every
/// half second or on every foreground change.
pub fn on_primary() -> bool {
let Some(m) = super::system::primary_monitor() else { return false };
on_monitor(m.rect)
}
#[cfg(test)]
mod tests {
use super::*;
const MON: Rect = Rect::new(0, 0, 1920, 1080);
fn fg(rect: Rect) -> Foreground {
Foreground {
rect,
shell: false,
own: false,
caption: false,
maximized_frame: false,
borderless_popup: false,
uwp_frame: false,
}
}
#[test]
fn a_game_covering_the_monitor_is_full_screen() {
assert!(is_fullscreen(Some(&fg(MON)), MON));
// a borderless window a few px bigger than the monitor
assert!(is_fullscreen(Some(&fg(Rect::new(-1, -1, 1921, 1081))), MON));
}
#[test]
fn an_ordinary_or_smaller_window_is_not() {
assert!(!is_fullscreen(Some(&fg(Rect::new(0, 0, 1920, 1032))), MON), "a maximised window above the taskbar");
assert!(!is_fullscreen(Some(&fg(Rect::new(100, 100, 900, 700))), MON));
assert!(!is_fullscreen(Some(&fg(Rect::new(-32000, -32000, -31840, -31972))), MON), "a minimised window");
assert!(!is_fullscreen(None, MON));
}
#[test]
fn a_maximised_window_with_a_title_bar_is_not_even_without_a_taskbar() {
// with the Windows taskbar on auto-hide a maximised window reaches the monitor's edges
let max = Foreground { caption: true, ..fg(Rect::new(-8, -8, 1928, 1088)) };
assert!(!is_fullscreen(Some(&max), MON));
}
#[test]
fn a_frameless_maximised_window_is_not_full_screen_either() {
// Electron / Qt windows without a title bar: maximised with a sizing frame, grown by the frame past the monitor
let max = Foreground { maximized_frame: true, ..fg(Rect::new(-8, -8, 1928, 1088)) };
assert!(!is_fullscreen(Some(&max), MON));
// a game's borderless window has no sizing frame, whatever else it has
assert!(is_fullscreen(Some(&fg(Rect::new(0, 0, 1920, 1080))), MON));
}
#[test]
fn a_full_screen_uwp_app_keeps_its_title_bar_but_fits_the_monitor_exactly() {
let uwp = |rect| Foreground { caption: true, uwp_frame: true, ..fg(rect) };
assert!(is_fullscreen(Some(&uwp(MON)), MON));
// maximised: a few px outside the monitor, not full screen
assert!(!is_fullscreen(Some(&uwp(Rect::new(-8, -8, 1928, 1088))), MON));
// an ordinary window with a title bar that fits the monitor exactly is still not
assert!(!is_fullscreen(Some(&Foreground { caption: true, ..fg(MON) }), MON));
}
#[test]
fn a_borderless_popup_game_keeping_caption_is_full_screen() {
// e.g. Dota 2 / SDL_app borderless full-screen (style 0x96CA0000: WS_POPUP | WS_CAPTION, no WS_THICKFRAME)
let style = 0x96CA_0000u32;
assert!(has_caption(style));
assert!(!has_maximized_frame(style));
assert!(is_borderless_popup(style));
assert!(!is_borderless_popup(style | WS_THICKFRAME), "a resizable popup with a sizing frame is not borderless");
let game = |rect| Foreground { caption: true, borderless_popup: true, ..fg(rect) };
assert!(is_fullscreen(Some(&game(MON)), MON));
assert!(is_fullscreen(Some(&game(Rect::new(-1, -1, 1921, 1081))), MON));
assert!(!is_fullscreen(Some(&game(Rect::new(100, 100, 900, 700))), MON), "a smaller popup dialog is not full screen");
}
#[test]
fn the_desktop_the_shell_and_ourselves_are_never_full_screen() {
assert!(!is_fullscreen(Some(&Foreground { shell: true, ..fg(MON) }), MON));
assert!(!is_fullscreen(Some(&Foreground { own: true, ..fg(MON) }), MON));
}
#[test]
fn only_the_primary_monitor_counts() {
// a game on a second monitor to the right does not cover the primary one
assert!(!is_fullscreen(Some(&fg(Rect::new(1920, 0, 3840, 1080))), MON));
// a degenerate monitor never makes everything full screen
assert!(!is_fullscreen(Some(&fg(MON)), Rect::new(0, 0, 0, 0)));
}
#[test]
fn shell_classes_and_styles() {
assert!(is_shell_class("Progman"));
assert!(is_shell_class("workerw"));
assert!(is_shell_class("Shell_TrayWnd"));
assert!(!is_shell_class("Chrome_WidgetWin_1"));
assert!(has_caption(0x00C0_0000 | 0x1000_0000));
assert!(!has_caption(0x8000_0000)); // WS_POPUP alone
assert!(!has_caption(0x0080_0000)); // WS_BORDER alone
assert!(has_maximized_frame(0x0100_0000 | 0x0004_0000 | 0x1000_0000));
assert!(!has_maximized_frame(0x0100_0000), "maximised without a sizing frame (a game)");
assert!(!has_maximized_frame(0x0004_0000), "a sizing frame without being maximised");
}
/// Read-only: a window that is nobody's cover carries no mark (the real marked case needs a window, which no test here shows).
#[test]
fn an_ordinary_window_is_not_a_marked_cover() {
use windows::Win32::UI::WindowsAndMessaging::GetDesktopWindow;
assert!(!crate::desktop::cover::is_marked_cover(0));
assert!(!crate::desktop::cover::is_marked_cover(unsafe { GetDesktopWindow() }.0 as isize));
}
/// Read-only: the live read of the window in front is self-consistent (a rectangle that is not inverted, or no window at all).
#[test]
fn the_live_read_is_sane() {
if let Some(f) = foreground() {
assert!(f.rect.right >= f.rect.left && f.rect.bottom >= f.rect.top, "{f:?}");
}
on_primary(); // must not panic or hang
on_monitor(MON);
on_monitor(Rect::new(1920, 0, 3840, 1080));
}
}