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
Recover lost dock work area for maximized windows
Atharv Tamboli081d6a6cf04c
diff --git a/crates/rf-shellguard/src/tbctl.rs b/crates/rf-shellguard/src/tbctl.rsindex ecb8dc14..de9efb04 100644--- a/crates/rf-shellguard/src/tbctl.rs+++ b/crates/rf-shellguard/src/tbctl.rs@@ -85,6 +85,8 @@ pub trait HideWorld {let _ = from;self.set_work_area(to)}+ /// Refit ordinary maximized windows that opened at full monitor size after a quiet work-area change.+ fn repair_maximized(&mut self, _work: [i32; 4]) {}/// The dock takes the bottom edge: its placement is told that its band is `edge` (the bottom strip of the primary monitor; empty/// when the dock reserves nothing) and its own desktop-toolbar registration with Explorer is given back and stays given back./// Does not wait: [`HideWorld::dock_edge_ready`] says when the registration is gone. Telling it again only updates the strip.@@ -148,6 +150,7 @@ pub struct Controller {guard_fault: Option<Refusal>,guard_confirmed_ms: u64,throttle: Throttle,+ recovery_throttle: Throttle,/// The work area this controller last set for each monitor it changed (monitor id, rectangle): a work area that is one of them/// is ours (`tbwork::plan_edge_held`). Forgotten when the edge is given back and when the user's rectangles are put back.held: Vec<(String, [i32; 4])>,@@ -193,6 +196,7 @@ impl Controller {guard_confirmed_ms: 0,// Explorer may recompute the work area whenever it likes: give it back at most 4 times a minute.throttle: Throttle::new(4, 60_000),+ recovery_throttle: Throttle::new(1, 2_000),held: Vec::new(),throttle_said: false,band_owed: false,@@ -349,6 +353,7 @@ impl Controller {/// not refused for the ones before it (a monitor switched off and on would otherwise wait up to a minute for the strip).pub fn screens_changed(&mut self, w: &mut dyn HideWorld) {self.throttle.reset();+ self.recovery_throttle.reset();self.displays_changed(w);}@@ -717,7 +722,12 @@ impl Controller {}fn set_changes(&mut self, w: &mut dyn HideWorld, now: u64, plan: &tbwork::EdgePlan) {- let changes = plan.changes();+ for m in &plan.monitors {+ if m.verdict == tbwork::Verdict::AsWanted && m.target != m.rect {+ w.repair_maximized(m.target);+ }+ }+ let mut changes = plan.changes();if changes.is_empty() {self.band_owed = false;return;@@ -731,11 +741,19 @@ impl Controller {// of its band is not Explorer's doing: the thin dock changes it as windows are maximised and restored, never faster than// every two seconds, and the work area must be the one the dock stands under at every one of them.)if !std::mem::take(&mut self.band_owed) && !self.throttle.allow(now) {- if !self.throttle_said {- self.throttle_said = true;- w.warn("taskbar: the work area was set too often lately; left alone");+ // A completely lost band that was already ours leaves maximized apps under the dock.+ // Recover that known reservation promptly, while still limiting repeated writes.+ changes.retain(|c| {+ plan.monitors.iter().any(|m| m.id == c.id && m.rect == c.from && c.to != m.rect)+ && self.held.iter().any(|(id, rect)| id == &c.id && *rect == c.to)+ });+ if changes.is_empty() || !self.recovery_throttle.allow(now) {+ if !self.throttle_said {+ self.throttle_said = true;+ w.warn("taskbar: the work area was set too often lately; left alone");+ }+ return;}- return;}self.throttle_said = false;for c in &changes {diff --git a/crates/rf-shellguard/src/tbedge_tests.rs b/crates/rf-shellguard/src/tbedge_tests.rsindex 080f4804..d4b0142f 100644--- a/crates/rf-shellguard/src/tbedge_tests.rs+++ b/crates/rf-shellguard/src/tbedge_tests.rs@@ -240,6 +240,28 @@ fn settled() -> (Controller, FakeWorld) {(c, w)}+#[test]+fn a_parked_taskbar_losing_the_entire_band_recovers_even_after_the_normal_throttle() {+ let (mut c, mut w) = settled();+ for _ in 0..8 {+ w.monitors[0].work = [0, 0, 2560, 1392];+ w.now += 1_000;+ c.work_area_changed(&mut w);+ }+ w.desktop.borrow_mut().iter_mut().find(|x| x.class == PRIMARY).unwrap().rect = [0, 1438, 2560, 1486];+ w.monitors[0].work = A;+ c.tick(&mut w);+ assert_eq!(work_a(&w), [0, 0, 2560, 1362]);++ w.monitors[0].work = A;+ w.now += 1_000;+ c.tick(&mut w);+ assert_eq!(work_a(&w), A, "repeated recovery is rate limited");+ w.now += 1_001;+ c.tick(&mut w);+ assert_eq!(work_a(&w), [0, 0, 2560, 1362]);+}+#[test]fn switching_off_puts_things_back_in_a_fixed_order_and_the_dock_registers_its_band_again() {let (mut c, mut w) = settled();diff --git a/crates/rf-shellguard/src/tbmodel.rs b/crates/rf-shellguard/src/tbmodel.rsindex 5ab66c21..873b19f9 100644--- a/crates/rf-shellguard/src/tbmodel.rs+++ b/crates/rf-shellguard/src/tbmodel.rs@@ -274,12 +274,12 @@ pub struct MonitorGeom {////// A maximized window overhangs the work area by its frame on every side (8 px for most, 0 for some): each edge keeps its overhang./// A window that was not fitted to `from` (an earlier quiet change it missed) takes the overhang of its left and top edges for the-/// right and bottom. Left alone: a window that is exactly the monitor (a borderless full-screen program that calls itself-/// maximized), one whose edges fit neither reading, and one that is already where it belongs.-pub fn refit_maximized(win: [i32; 4], monitor: [i32; 4], from: [i32; 4], to: [i32; 4]) -> Option<[i32; 4]> {+/// right and bottom. A window that is exactly the monitor is only fitted when its native style confirms it is ordinarily+/// maximized. Windows whose edges fit neither reading, and windows already where they belong, are left alone.+pub fn refit_maximized(win: [i32; 4], monitor: [i32; 4], from: [i32; 4], to: [i32; 4], ordinary: bool) -> Option<[i32; 4]> {const FRAME: std::ops::RangeInclusive<i32> = 0..=32;if win == monitor {- return None;+ return (ordinary && to != win && to[2] > to[0] && to[3] > to[1]).then_some(to);}let over = [from[0] - win[0], from[1] - win[1], win[2] - from[2], win[3] - from[3]];let new = if over.iter().all(|o| FRAME.contains(o)) {@@ -350,21 +350,23 @@ mod tests {let mon = [0, 0, 2560, 1440];let (old, new) = ([0, 0, 2560, 1392], [0, 0, 2560, 1362]);// the usual 8 px frame: every edge keeps its overhang (measured: a browser at -8,-8 2576x1408 on the 1392 work area)- assert_eq!(refit_maximized([-8, -8, 2568, 1400], mon, old, new), Some([-8, -8, 2568, 1370]));+ assert_eq!(refit_maximized([-8, -8, 2568, 1400], mon, old, new, true), Some([-8, -8, 2568, 1370]));// a window without overhang- assert_eq!(refit_maximized([0, 0, 2560, 1392], mon, old, new), Some([0, 0, 2560, 1362]));+ assert_eq!(refit_maximized([0, 0, 2560, 1392], mon, old, new, true), Some([0, 0, 2560, 1362]));// the work area grows to the whole monitor (a second monitor whose taskbar strip is given back) and shrinks againlet side = [2560, -618, 4000, 1942];let side_old = [2560, -618, 4000, 1894];- assert_eq!(refit_maximized([2552, -626, 4008, 1902], side, side_old, side), Some([2552, -626, 4008, 1950]));- assert_eq!(refit_maximized([2552, -626, 4008, 1950], side, side, side_old), Some([2552, -626, 4008, 1902]));+ assert_eq!(refit_maximized([2552, -626, 4008, 1902], side, side_old, side, true), Some([2552, -626, 4008, 1950]));+ assert_eq!(refit_maximized([2552, -626, 4008, 1950], side, side, side_old, true), Some([2552, -626, 4008, 1902]));// it missed an earlier change (still fitted to an older, larger area): the left and top overhang decide- assert_eq!(refit_maximized([-8, -8, 2568, 1448], mon, old, new), Some([-8, -8, 2568, 1370]));+ assert_eq!(refit_maximized([-8, -8, 2568, 1448], mon, old, new, true), Some([-8, -8, 2568, 1370]));// exactly the monitor: a full-screen program, not ours to move- assert_eq!(refit_maximized(mon, mon, old, new), None);+ assert_eq!(refit_maximized(mon, mon, old, new, false), None);+ assert_eq!(refit_maximized(mon, mon, mon, new, true), Some(new));+ assert_eq!(refit_maximized(mon, mon, mon, mon, true), None);// already there, or placed by its own rules- assert_eq!(refit_maximized([-8, -8, 2568, 1370], mon, new, new), None);- assert_eq!(refit_maximized([300, 200, 1500, 900], mon, old, new), None);+ assert_eq!(refit_maximized([-8, -8, 2568, 1370], mon, new, new, true), None);+ assert_eq!(refit_maximized([300, 200, 1500, 900], mon, old, new, true), None);}fn live(handle: isize, class: &str, pid: u32, start: u64, visible: bool) -> LiveWindow {diff --git a/crates/rf-shellguard/src/tbwork.rs b/crates/rf-shellguard/src/tbwork.rsindex e5783171..3834f867 100644--- a/crates/rf-shellguard/src/tbwork.rs+++ b/crates/rf-shellguard/src/tbwork.rs@@ -68,6 +68,12 @@ fn inside(inner: [i32; 4], outer: [i32; 4]) -> bool {inner[2] > inner[0] && inner[3] > inner[1] && inner[0] >= outer[0] && inner[1] >= outer[1] && inner[2] <= outer[2] && inner[3] <= outer[3]}+// Explorer can park an auto-hiding taskbar below the screen, leaving only a few pixels at the edge.+fn parked_bottom(monitor: [i32; 4], bar: [i32; 4]) -> bool {+ (1..=8).contains(&(monitor[3] - bar[1])) && bar[3] > monitor[3] + 1+ && bar[3] - bar[1] < (monitor[3] - monitor[1]) / 2+}+/// The edge a bar is on: it spans (nearly) the whole monitor along that edge and touches it. `None`: it is not a bar on an edge.pub fn side_of(monitor: [i32; 4], bar: [i32; 4]) -> Option<Side> {let (mw, mh) = (monitor[2] - monitor[0], monitor[3] - monitor[1]);@@ -77,7 +83,7 @@ pub fn side_of(monitor: [i32; 4], bar: [i32; 4]) -> Option<Side> {}let wide = i64::from(bw) * 10 >= i64::from(mw) * 9;let tall = i64::from(bh) * 10 >= i64::from(mh) * 9;- if wide && near(bar[3], monitor[3]) && bar[1] > monitor[1] + mh / 2 {+ if wide && (near(bar[3], monitor[3]) || parked_bottom(monitor, bar)) && bar[1] > monitor[1] + mh / 2 {Some(Side::Bottom)} else if wide && near(bar[1], monitor[1]) && bar[3] < monitor[1] + mh / 2 {Some(Side::Top)@@ -92,6 +98,9 @@ pub fn side_of(monitor: [i32; 4], bar: [i32; 4]) -> Option<Side> {/// The monitor less the strip of a bar on its bottom edge: what the work area is with only that taskbar reserving room.pub fn without_strip(monitor: [i32; 4], bar: [i32; 4]) -> [i32; 4] {+ if parked_bottom(monitor, bar) {+ return monitor;+ }[monitor[0], monitor[1], monitor[2], bar[1]]}@@ -309,6 +318,20 @@ mod tests {p.monitors.iter().map(|m| m.verdict).collect()}+ #[test]+ fn parked_taskbar_at_125_percent_scale_allows_the_docks_band_to_be_recovered() {+ let monitor = [0, 0, 1920, 1080];+ let parked = [0, 1078, 1920, 1138];+ let target = [0, 0, 1920, 1010];+ assert_eq!(side_of(monitor, parked), Some(Side::Bottom));+ assert_eq!(without_strip(monitor, parked), monitor);+ let plan = plan_edge(&[mon("A", monitor, monitor, true)], &[parked], Some(70));+ assert_eq!(plan.changes(), vec![WorkChange { id: "A".into(), from: monitor, to: target }]);+ assert_eq!(plan.primary().unwrap().user_work, monitor);+ let foreign = plan_edge(&[mon("A", monitor, [0, 30, 1920, 1080], true)], &[parked], Some(70));+ assert_eq!(verdicts(&foreign), vec![Verdict::Foreign]);+ }+// ---------------------------------------------------------------- the target#[test]diff --git a/crates/rf-win/src/dock/fullscreen.rs b/crates/rf-win/src/dock/fullscreen.rsindex eaa11ed9..cc220725 100644--- a/crates/rf-win/src/dock/fullscreen.rs+++ b/crates/rf-win/src/dock/fullscreen.rs@@ -86,7 +86,7 @@ pub fn is_borderless_popup(style: u32) -> bool {}/// Reads window information for any given HWND. `None` when there is none or it cannot be measured.-fn window_info(h: windows::Win32::Foundation::HWND) -> Option<Foreground> {+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};diff --git a/crates/rf-win/src/taskbar_hide.rs b/crates/rf-win/src/taskbar_hide.rsindex 82f19c08..f0ec65eb 100644--- a/crates/rf-win/src/taskbar_hide.rs+++ b/crates/rf-win/src/taskbar_hide.rs@@ -223,6 +223,10 @@ impl HideWorld for RealWorld {Ok(())}+ fn repair_maximized(&mut self, work: [i32; 4]) {+ crate::workfit::repair_maximized(work);+ }+fn load_state(&mut self) -> ShellStateFile {ShellStateFile::load(&self.state_path())}diff --git a/crates/rf-win/src/workfit.rs b/crates/rf-win/src/workfit.rsindex ec56e407..b9dbe6b7 100644--- a/crates/rf-win/src/workfit.rs+++ b/crates/rf-win/src/workfit.rs@@ -24,14 +24,24 @@ struct Fit {monitor: HMONITOR,monitor_rect: [i32; 4],own: u32,+ monitor_sized_only: bool,}/// Asks every visible maximized window of the monitor that holds `to` (not this process's own) to take the place it has in the work/// area `to`, given that the monitor's work area was `from`. Queued (a program that hangs does not hold the caller); a window of a/// higher integrity level refuses and stays as it is. Call it on a per-monitor DPI aware thread: the rectangles are physical pixels.pub fn maximized_windows(from: [i32; 4], to: [i32; 4]) {+ fit_windows(from, to, false);+}++/// Catch an ordinary maximized window that missed the quiet change or opened just after it.+pub(crate) fn repair_maximized(work: [i32; 4]) {+ fit_windows(work, work, true);+}++fn fit_windows(from: [i32; 4], to: [i32; 4], monitor_sized_only: bool) {let [left, top, right, bottom] = to;- let mut fit = Fit { from, to, monitor: HMONITOR::default(), monitor_rect: [0; 4], own: std::process::id() };+ let mut fit = Fit { from, to, monitor: HMONITOR::default(), monitor_rect: [0; 4], own: std::process::id(), monitor_sized_only };// SAFETY: plain queries; `fit` outlives EnumWindows, which calls back on this thread.unsafe {fit.monitor = MonitorFromRect(&RECT { left, top, right, bottom }, MONITOR_DEFAULTTONULL);@@ -58,7 +68,15 @@ unsafe extern "system" fn each(hwnd: HWND, lparam: LPARAM) -> BOOL {if pid == fit.own || GetWindowRect(hwnd, &mut r).is_err() {return true.into();}- if let Some([l, t, rr, b]) = rf_shellguard::tbmodel::refit_maximized([r.left, r.top, r.right, r.bottom], fit.monitor_rect, fit.from, fit.to) {+ let rect = [r.left, r.top, r.right, r.bottom];+ if fit.monitor_sized_only && rect != fit.monitor_rect {+ return true.into();+ }+ // An ordinary maximized window can fill the monitor exactly when Explorer loses the reserved band.+ let ordinary = rect == fit.monitor_rect && crate::dock::fullscreen::window_info(hwnd).is_some_and(|fg| {+ !fg.shell && !fg.own && (fg.maximized_frame || (fg.caption && !fg.uwp_frame && !fg.borderless_popup))+ });+ if let Some([l, t, rr, b]) = rf_shellguard::tbmodel::refit_maximized(rect, fit.monitor_rect, fit.from, fit.to, ordinary) {let _ = SetWindowPos(HWND(hwnd.0 as *mut c_void), None, l, t, rr - l, b - t, SWP_ASYNCWINDOWPOS | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOACTIVATE);}true.into()