Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugins/rs-shell/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
39 changes: 39 additions & 0 deletions plugins/rs-shell/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<MONITORINFO>() 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 {
Expand Down Expand Up @@ -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::<MINIMIZEDMETRICS>() as u32, iWidth: 0, iHorzGap: 0, iVertGap: 0, iArrange: ARW_HIDE as i32 };
Expand Down Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions plugins/rust-explorer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -1117,15 +1121,15 @@ mod sh {
TRUE
}

fn top_windows() -> Vec<isize> {
pub(crate) fn top_windows() -> Vec<isize> {
let mut v: Vec<isize> = vec![];
unsafe { let _ = EnumWindows(Some(collect), LPARAM(&mut v as *mut _ as isize)); }
v
}

/// 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<isize>) {
pub(crate) fn keep_on_screen(before: Vec<isize>) {
std::thread::spawn(move || unsafe {
for _ in 0..40 {
std::thread::sleep(std::time::Duration::from_millis(150));
Expand Down
Loading