A lightweight, macOS-style dock for Windows 11, with glass visuals, smooth magnification, and Genie effect.
feat: improve window minimize behavior and add new spike_minrect tool
CristianOlivera1181f1362f018
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 6c482e7..0677841 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -8,6 +8,20 @@ copia la sección de la versión que se publica a la release de GitHub.## [Unreleased]+### English++- Genie: maximized and snapped windows no longer jump to a smaller size before+ flying into the dock, nor jump to fill the screen after flying back out. The+ genie took the size the window has when not maximized; it now flies exactly+ the area the window covered on screen.++### Español++- Genio: las ventanas maximizadas o acopladas ya no saltan a un tamaño menor antes+ de volar al dock, ni saltan para llenar la pantalla después de volver. El genio+ tomaba el tamaño que la ventana tiene sin maximizar; ahora vuela exactamente la+ zona que ocupaba en pantalla.+## [0.1.6]### Englishdiff --git a/apps/dock/src/app.rs b/apps/dock/src/app.rsindex 69728e9..54b3c8f 100644--- a/apps/dock/src/app.rs+++ b/apps/dock/src/app.rs@@ -377,6 +377,9 @@ struct App {roster: Roster,tracker: WindowTracker,windows: Vec<TrackedWindow>,+ /// Where each window was last seen on screen while not maximized, for+ /// the genie of snapped windows (see `tracker::rect_before_minimize`).+ last_rects: HashMap<isize, RECT>,entries: Vec<Entry>,icons: IconCache,downloads: Option<String>,@@ -1089,10 +1092,11 @@ impl App {return;};for &hwnd in windows {- let Some(from) = tracker::normal_rect(hwnd) else {+ let last_seen = self.last_rects.get(&hwnd).copied();+ let Some((from, frame)) = tracker::rect_before_minimize(hwnd, last_seen) else {continue;};- if let Some(flight) = Flight::start(&self.gfx, hwnd, from, screen) {+ if let Some(flight) = Flight::start(&self.gfx, hwnd, from, frame, screen) {self.flights.push(flight);self.last_frame = Instant::now();}@@ -1859,6 +1863,13 @@ impl App {/// Refreshes open windows and recomposes the dock if anything changed.fn sync(&mut self) -> Result<()> {self.windows = self.tracker.list();+ let windows = &self.windows;+ self.last_rects+ .retain(|hwnd, _| windows.iter().any(|window| window.hwnd == *hwnd));+ let tracked: Vec<isize> = self.windows.iter().map(|window| window.hwnd).collect();+ for hwnd in tracked {+ self.remember_rect(hwnd);+ }self.calm_windows();let recent_before = self.roster.recent().to_vec();let changed = self@@ -2014,7 +2025,27 @@ impl App {}}+ /// Notes where `hwnd` is on screen. A maximized window forgets it: its+ /// rectangle then comes from the work area, and a stale one from before+ /// could be taken after it is un-maximized.+ fn remember_rect(&mut self, hwnd: isize) {+ match tracker::screen_rect(hwnd) {+ Some((rect, false)) => {+ self.last_rects.insert(hwnd, rect);+ }+ Some((_, true)) => {+ self.last_rects.remove(&hwnd);+ }+ None => {}+ }+ }+fn update_fullscreen(&mut self) -> Result<()> {+ // The window in front is where the user works: note where it is now.+ let front = tracker::foreground_window();+ if self.windows.iter().any(|window| window.hwnd == front) {+ self.remember_rect(front);+ }self.update_menu_bar_app()?;let Some(monitor) = primary_monitor() else {return Ok(());@@ -2528,6 +2559,7 @@ pub fn run() -> Result<()> {roster,tracker: WindowTracker::default(),windows: Vec::new(),+ last_rects: HashMap::new(),entries: Vec::new(),icons: IconCache::default(),downloads: downloads_folder(),diff --git a/apps/dock/src/flight.rs b/apps/dock/src/flight.rsindex 42b28ac..2c8b8c3 100644--- a/apps/dock/src/flight.rs+++ b/apps/dock/src/flight.rs@@ -443,9 +443,14 @@ impl Flight {/// Puts up the stand-in of a window that is being minimized, where it was/// (`from`, a screen rectangle; `screen` is the monitor the overlay/// covers). The flight takes off once [`Flight::aim`] gives it its slot.- pub fn start(gfx: &Gfx, hwnd: isize, from: RECT, screen: RECT) -> Option<Self> {+ ///+ /// `from` is the whole window, invisible borders included, where its live+ /// thumbnail goes; `frame` is the part that showed on screen, which the+ /// genie copies and flies (the work area for a maximized window, so no+ /// strip of the menu bar or desktop around it comes along).+ pub fn start(gfx: &Gfx, hwnd: isize, from: RECT, frame: RECT, screen: RECT) -> Option<Self> {// Only what is on screen can be copied from it.- let visible = intersect(from, screen);+ let visible = intersect(frame, screen);if is_empty(visible) {return None;}diff --git a/crates/muelle-shell/src/tracker.rs b/crates/muelle-shell/src/tracker.rsindex 66023ce..64814de 100644--- a/crates/muelle-shell/src/tracker.rs+++ b/crates/muelle-shell/src/tracker.rs@@ -22,9 +22,9 @@ use windows::{WindowsAndMessaging::{EnumWindows, GW_OWNER, GWL_EXSTYLE, GWL_STYLE, GetClassNameW, GetForegroundWindow,GetWindow, GetWindowLongW, GetWindowPlacement, GetWindowRect, GetWindowTextLengthW,- GetWindowTextW, GetWindowThreadProcessId, IsIconic, IsWindowVisible,- WINDOWPLACEMENT, WS_EX_APPWINDOW, WS_EX_NOACTIVATE, WS_EX_TOOLWINDOW, WS_MAXIMIZE,- WS_THICKFRAME,+ GetWindowTextW, GetWindowThreadProcessId, IsIconic, IsWindowVisible, IsZoomed,+ SM_CXPADDEDBORDER, SM_CXSIZEFRAME, WINDOWPLACEMENT, WPF_RESTORETOMAXIMIZED,+ WS_EX_APPWINDOW, WS_EX_NOACTIVATE, WS_EX_TOOLWINDOW, WS_MAXIMIZE, WS_THICKFRAME,},},},@@ -142,6 +142,85 @@ pub fn normal_rect(hwnd: isize) -> Option<RECT> {(rect.right > rect.left && rect.bottom > rect.top).then_some(rect)}+/// Where a window is on screen now, and whether it is maximized; `None`+/// while it is minimized. Taken whenever the dock looks at its windows, so a+/// minimize knows where a snapped window was: Windows keeps a snapped+/// window's pre-snap rectangle as its "normal" one.+#[must_use]+pub fn screen_rect(hwnd: isize) -> Option<(RECT, bool)> {+ let window = HWND(hwnd as *mut _);+ // SAFETY: Read-only queries on a window handle.+ unsafe {+ if IsIconic(window).as_bool() {+ return None;+ }+ let mut rect = RECT::default();+ GetWindowRect(window, &mut rect).ok()?;+ Some((rect, IsZoomed(window).as_bool()))+ }+}++/// Where a window that has just started minimizing was on screen, which by+/// then it no longer says itself: it is already at -32000.+///+/// Returns the whole window, invisible resize borders included (where its+/// live thumbnail goes), and the part that was visible on screen (what the+/// genie copies and flies). `last_seen` is the [`screen_rect`] taken while+/// it was not maximized, if any.+///+/// A maximized window fills its monitor's work area, plus its borders: its+/// "normal" rectangle is the smaller one it returns to when un-maximized,+/// and flying from it made the genie jump first.+#[must_use]+pub fn rect_before_minimize(hwnd: isize, last_seen: Option<RECT>) -> Option<(RECT, RECT)> {+ use windows::Win32::{+ Graphics::Gdi::{+ GetMonitorInfoW, MONITOR_DEFAULTTONEAREST, MONITORINFO, MonitorFromWindow,+ },+ UI::HiDpi::{GetDpiForWindow, GetSystemMetricsForDpi},+ };+ let window = HWND(hwnd as *mut _);+ let mut placement = WINDOWPLACEMENT {+ length: size_of::<WINDOWPLACEMENT>() as u32,+ ..Default::default()+ };+ // SAFETY: The structure has its size initialized and lives for the call.+ unsafe { GetWindowPlacement(window, &mut placement) }.ok()?;+ if placement.flags.0 & WPF_RESTORETOMAXIMIZED.0 == 0 {+ let rect = last_seen.or_else(|| normal_rect(hwnd))?;+ return Some((rect, rect));+ }+ // SAFETY: Read-only monitor, DPI and style queries.+ unsafe {+ let monitor = MonitorFromWindow(window, MONITOR_DEFAULTTONEAREST);+ let mut info = MONITORINFO {+ cbSize: size_of::<MONITORINFO>() as u32,+ ..Default::default()+ };+ if !GetMonitorInfoW(monitor, &mut info).as_bool() {+ return None;+ }+ let work = info.rcWork;+ // Windows maximizes a resizable window with its borders past the+ // work area's edges, so that only the window itself shows.+ let resizable = GetWindowLongW(window, GWL_STYLE) as u32 & WS_THICKFRAME.0 != 0;+ let border = if resizable {+ let dpi = GetDpiForWindow(window);+ GetSystemMetricsForDpi(SM_CXSIZEFRAME, dpi)+ + GetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)+ } else {+ 0+ };+ let whole = RECT {+ left: work.left - border,+ top: work.top - border,+ right: work.right + border,+ bottom: work.bottom + border,+ };+ Some((whole, work))+ }+}+#[must_use]pub fn foreground_window() -> isize {unsafe { GetForegroundWindow().0 as isize }diff --git a/spikes/src/bin/spike_cloak.rs b/spikes/src/bin/spike_cloak.rsindex 8adbb0b..ee5f4e3 100644--- a/spikes/src/bin/spike_cloak.rs+++ b/spikes/src/bin/spike_cloak.rs@@ -35,7 +35,10 @@ unsafe extern "system" fn on_event(}fn main() {- let seconds: u64 = std::env::args().nth(1).and_then(|s| s.parse().ok()).unwrap_or(20);+ let seconds: u64 = std::env::args()+ .nth(1)+ .and_then(|s| s.parse().ok())+ .unwrap_or(20);// SAFETY: An out-of-context hook delivered through this thread's message loop.let hook = unsafe {SetWinEventHook(@@ -63,5 +66,8 @@ fn main() {unsafe {let _ = UnhookWinEvent(hook);}- println!("cloak/uncloak events in {seconds} s: {}", EVENTS.load(Ordering::Relaxed));+ println!(+ "cloak/uncloak events in {seconds} s: {}",+ EVENTS.load(Ordering::Relaxed)+ );}diff --git a/spikes/src/bin/spike_minrect.rs b/spikes/src/bin/spike_minrect.rsnew file mode 100644index 0000000..c95c5c3--- /dev/null+++ b/spikes/src/bin/spike_minrect.rs@@ -0,0 +1,136 @@+//! What a window reports about its own rectangle at the moment it starts to+//! minimize (EVENT_SYSTEM_MINIMIZESTART), when it was maximized.++use std::{thread::sleep, time::Duration};++use windows::{+ Win32::{+ Foundation::{HWND, LPARAM, LRESULT, RECT, WPARAM},+ Graphics::Dwm::{DWMWA_EXTENDED_FRAME_BOUNDS, DwmGetWindowAttribute},+ System::LibraryLoader::GetModuleHandleW,+ UI::{+ Accessibility::{HWINEVENTHOOK, SetWinEventHook},+ WindowsAndMessaging::{+ CreateWindowExW, DefWindowProcW, DestroyWindow, DispatchMessageW,+ EVENT_SYSTEM_MINIMIZESTART, GetWindowPlacement, GetWindowRect, IsIconic, MSG,+ PM_REMOVE, PeekMessageW, RegisterClassW, SW_MAXIMIZE, SW_MINIMIZE, ShowWindow,+ WINDOWPLACEMENT, WINEVENT_OUTOFCONTEXT, WNDCLASSW, WS_OVERLAPPEDWINDOW,+ },+ },+ },+ core::w,+};++unsafe extern "system" fn proc(hwnd: HWND, msg: u32, w: WPARAM, l: LPARAM) -> LRESULT {+ // SAFETY: Default handling.+ unsafe { DefWindowProcW(hwnd, msg, w, l) }+}++unsafe extern "system" fn on_event(+ _hook: HWINEVENTHOOK,+ _event: u32,+ hwnd: HWND,+ _object: i32,+ _child: i32,+ _thread: u32,+ _time: u32,+) {+ // SAFETY: Read-only queries on a live window.+ unsafe {+ let mut rect = RECT::default();+ let _ = GetWindowRect(hwnd, &mut rect);+ let mut frame = RECT::default();+ let _ = DwmGetWindowAttribute(+ hwnd,+ DWMWA_EXTENDED_FRAME_BOUNDS,+ (&raw mut frame).cast(),+ size_of::<RECT>() as u32,+ );+ let mut placement = WINDOWPLACEMENT {+ length: size_of::<WINDOWPLACEMENT>() as u32,+ ..Default::default()+ };+ let _ = GetWindowPlacement(hwnd, &mut placement);+ println!(+ "computed (whole, visible): {:?}",+ muelle_shell::tracker::rect_before_minimize(hwnd.0 as isize, None)+ );+ println!(+ "at MINIMIZESTART: iconic={} window={:?} frame={:?} flags={:#x} showCmd={} normal={:?}",+ IsIconic(hwnd).as_bool(),+ rect,+ frame,+ placement.flags.0,+ placement.showCmd,+ placement.rcNormalPosition+ );+ }+}++fn pump(ms: u64) {+ let mut msg = MSG::default();+ for _ in 0..ms / 5 {+ // SAFETY: Standard pump.+ unsafe {+ while PeekMessageW(&mut msg, None, 0, 0, PM_REMOVE).as_bool() {+ DispatchMessageW(&msg);+ }+ }+ sleep(Duration::from_millis(5));+ }+}++fn main() {+ // SAFETY: Our own window and hook.+ unsafe {+ let Ok(instance) = GetModuleHandleW(None) else {+ return;+ };+ RegisterClassW(&WNDCLASSW {+ lpfnWndProc: Some(proc),+ hInstance: instance.into(),+ lpszClassName: w!("MuelleSpikeMinRect"),+ ..Default::default()+ });+ let Ok(hwnd) = CreateWindowExW(+ Default::default(),+ w!("MuelleSpikeMinRect"),+ w!("min rect"),+ WS_OVERLAPPEDWINDOW,+ 300,+ 200,+ 700,+ 450,+ None,+ None,+ Some(instance.into()),+ None,+ ) else {+ return;+ };+ let _hook = SetWinEventHook(+ EVENT_SYSTEM_MINIMIZESTART,+ EVENT_SYSTEM_MINIMIZESTART,+ None,+ Some(on_event),+ std::process::id(),+ 0,+ WINEVENT_OUTOFCONTEXT,+ );+ let _ = ShowWindow(hwnd, SW_MAXIMIZE);+ pump(600);+ let mut rect = RECT::default();+ let _ = GetWindowRect(hwnd, &mut rect);+ let mut frame = RECT::default();+ let _ = DwmGetWindowAttribute(+ hwnd,+ DWMWA_EXTENDED_FRAME_BOUNDS,+ (&raw mut frame).cast(),+ size_of::<RECT>() as u32,+ );+ println!("maximized on screen: window={rect:?} visible={frame:?}");+ let _ = ShowWindow(hwnd, SW_MINIMIZE);+ pump(600);+ let _ = DestroyWindow(hwnd);+ }+}