From 2ad7778052dd9e2700a4e722e028cd712bab2d7a Mon Sep 17 00:00:00 2001 From: 2sulraw <2sulraw@gmail.com> Date: Thu, 26 Nov 2026 19:47:53 +0330 Subject: [PATCH] rs-shell: keep Windows' dialogs on screen without Explorer; rust-explorer: the Open with chooser too (#191) --- plugins/rs-shell/Cargo.toml | 2 +- plugins/rs-shell/src/main.rs | 39 +++++++++++++++++++++++++++++++ plugins/rust-explorer/src/main.rs | 8 +++++-- 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/plugins/rs-shell/Cargo.toml b/plugins/rs-shell/Cargo.toml index 1fa9251..0041dd2 100644 --- a/plugins/rs-shell/Cargo.toml +++ b/plugins/rs-shell/Cargo.toml @@ -6,7 +6,7 @@ license = "MIT OR Apache-2.0" [dependencies] winrust-common = { path = "../../common" } -windows-sys = { version = "0.61.2", features = ["Win32_Foundation", "Win32_Graphics_Dwm", "Win32_Graphics_Gdi", "Win32_Graphics_GdiPlus", "Win32_System_Console", "Win32_System_Registry", "Win32_System_RemoteDesktop", "Win32_UI_Input_KeyboardAndMouse", "Win32_UI_WindowsAndMessaging"] } +windows-sys = { version = "0.61.2", features = ["Win32_Foundation", "Win32_Graphics_Dwm", "Win32_Graphics_Gdi", "Win32_Graphics_GdiPlus", "Win32_System_Console", "Win32_System_Registry", "Win32_System_RemoteDesktop", "Win32_UI_Accessibility", "Win32_UI_Input_KeyboardAndMouse", "Win32_UI_WindowsAndMessaging"] } [build-dependencies] embed-resource = "3" diff --git a/plugins/rs-shell/src/main.rs b/plugins/rs-shell/src/main.rs index 8f45eaa..88b7780 100644 --- a/plugins/rs-shell/src/main.rs +++ b/plugins/rs-shell/src/main.rs @@ -121,6 +121,43 @@ fn find_exe(name: &str) -> PathBuf { fn bar_up() -> bool { unsafe { !FindWindowW(wide("topbar").as_ptr(), std::ptr::null()).is_null() } } +/// Explorer is not the shell, so the dialogs it normally places (Windows' "How do you want to open this file?" chooser, +/// Properties, ...) can be created at (0,0) or off the monitor and get clipped (#191). Pull a shown top-level window back +/// inside its monitor when it sticks out. Small windows only, so a maximized or full-screen one is never touched. +unsafe fn keep_inside(h: HWND) { + use windows_sys::Win32::Graphics::Gdi::{GetMonitorInfoW, MonitorFromWindow, MONITORINFO, MONITOR_DEFAULTTONEAREST}; + unsafe { + if h.is_null() || IsWindowVisible(h) == 0 || IsIconic(h) != 0 || IsZoomed(h) != 0 { return; } + if GetAncestor(h, GA_ROOT) != h { return; } // top-level only: a child's rectangle is relative to its parent + let mut r: RECT = std::mem::zeroed(); + if GetWindowRect(h, &mut r) == 0 || r.right <= r.left || r.bottom <= r.top { return; } + let mut mi: MONITORINFO = std::mem::zeroed(); + mi.cbSize = size_of::() as u32; + if GetMonitorInfoW(MonitorFromWindow(h, MONITOR_DEFAULTTONEAREST), &mut mi) == 0 { return; } + let a = mi.rcWork; + if r.right - r.left > a.right - a.left || r.bottom - r.top > a.bottom - a.top { return; } // bigger than the screen: not a stray dialog + let x = r.left.min(a.right - (r.right - r.left)).max(a.left); + let y = r.top.min(a.bottom - (r.bottom - r.top)).max(a.top); + if (x, y) != (r.left, r.top) { + SetWindowPos(h, ptr::null_mut(), x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE); + } + } +} + +/// Keeps stray dialogs on screen for sessions without Explorer. A WinEvent hook, not a poll: nothing runs until a window +/// shows, and only that window is looked at. Installed on its own thread, which pumps the messages the hook needs. +fn watch_stray_windows() { + use windows_sys::Win32::UI::Accessibility::SetWinEventHook; + unsafe extern "system" fn on_show(_hook: *mut std::ffi::c_void, _event: u32, h: HWND, id_object: i32, id_child: i32, _thread: u32, _time: u32) { + if id_object == OBJID_WINDOW && id_child == CHILDID_SELF as i32 { unsafe { keep_inside(h) }; } + } + thread::spawn(|| unsafe { + SetWinEventHook(EVENT_OBJECT_SHOW, EVENT_OBJECT_SHOW, ptr::null_mut(), Some(on_show), 0, 0, WINEVENT_OUTOFCONTEXT); + let mut m: MSG = std::mem::zeroed(); + while GetMessageW(&mut m, ptr::null_mut(), 0, 0) > 0 { TranslateMessage(&m); DispatchMessageW(&m); } + }); +} + /// A minimized window left on screen (or minimized before the hide setting was applied) shows as a caption stub on /// the desktop: park it where Windows itself parks hidden minimized windows. unsafe extern "system" fn park(h: HWND, _: LPARAM) -> BOOL { @@ -217,6 +254,7 @@ fn log(msg: &str) { fn run_shell() { unsafe { windows_sys::Win32::System::Console::FreeConsole() }; log("start"); + watch_stray_windows(); // Explorer is gone here: keep its dialogs (the "open this file" chooser) on screen (#191) // without explorer a minimized window is drawn as a little caption stub on the desktop (apps started with // --minimized left grey rectangles over the icons): hide them, the bar lists the windows anyway let mut mm = MINIMIZEDMETRICS { cbSize: size_of::() as u32, iWidth: 0, iHorzGap: 0, iVertGap: 0, iArrange: ARW_HIDE as i32 }; @@ -369,6 +407,7 @@ fn overlay() { unsafe { windows_sys::Win32::System::Console::FreeConsole() }; let replace = replace_on(); log(&format!("overlay start (replace explorer: {replace})")); + if replace { watch_stray_windows(); } // Explorer is ended below: keep its dialogs on screen (#191) let wp = thread::spawn(wallpaper_bmp); // the BMP the desktop window paints; the system wallpaper stays Explorer's if !bar_up() { log(&format!("topbar spawn -> {:?}", quiet(find_exe("topbar.exe")).spawn().map(|_| ()))); } // Win+I / Win+E / Win+D live in reshell: its Run entry may be missing (another account's HKCU), and without it Explorer's own Win+I diff --git a/plugins/rust-explorer/src/main.rs b/plugins/rust-explorer/src/main.rs index b36269e..eda895f 100644 --- a/plugins/rust-explorer/src/main.rs +++ b/plugins/rust-explorer/src/main.rs @@ -341,7 +341,11 @@ unsafe fn activate(h: HWND, p: PathBuf, is_dir: bool) { else { // like Explorer: the exe starts in its own folder (games look for files next to it) let dir = p.parent().map(|d| w(&d.to_string_lossy())); + let before = sh::top_windows(); unsafe { ShellExecuteW(h, ptr::null(), w(&p.to_string_lossy()).as_ptr(), ptr::null(), dir.as_ref().map_or(ptr::null(), |d| d.as_ptr()), SW_SHOWNORMAL) }; + // an unassociated file makes Windows show its "How do you want to open this file?" chooser, which lands partly off + // screen without Explorer as the shell (#191): pull it back in, as the context menu's own dialogs already are + sh::keep_on_screen(before); } } @@ -1117,7 +1121,7 @@ mod sh { TRUE } - fn top_windows() -> Vec { + pub(crate) fn top_windows() -> Vec { let mut v: Vec = vec![]; unsafe { let _ = EnumWindows(Some(collect), LPARAM(&mut v as *mut _ as isize)); } v @@ -1125,7 +1129,7 @@ mod sh { /// Dialogs the shell opens for a command ("Open with" > choose another app, Properties) can land partly off screen on some /// setups. For a few seconds after the command, any new window that sticks out of its monitor is moved back inside. - fn keep_on_screen(before: Vec) { + pub(crate) fn keep_on_screen(before: Vec) { std::thread::spawn(move || unsafe { for _ in 0..40 { std::thread::sleep(std::time::Duration::from_millis(150));