A lightweight, macOS-style dock for Windows 11, with glass visuals, smooth magnification, and Genie effect.
perf: reduce dock startup and idle work
CristianOlivera13b75eeabb76f
diff --git a/apps/dock/src/app.rs b/apps/dock/src/app.rsindex f482ff4..52765e9 100644--- a/apps/dock/src/app.rs+++ b/apps/dock/src/app.rs@@ -101,6 +101,8 @@ const WM_MOUSELEAVE: u32 = 0x02A3;const HSHELL_WINDOWACTIVATED: usize = 4;const HSHELL_RUDEAPPACTIVATED: usize = 0x8004;const HSHELL_FLASH: usize = 0x8006;+/// A window's title or icon changed.+const HSHELL_REDRAW: usize = 6;/// `wParam` of WM_SETTINGCHANGE when the work area changes. The dock's own/// reservation causes this, so handling it would create a loop.const SPI_SETWORKAREA: usize = 0x002F;@@ -675,15 +677,12 @@ impl App {let screen_width = (screen.right - screen.left) as f32;let dock_x = screen.left as f32 + ((screen_width - dock.width) / 2.0).round();let dock_y = screen.bottom as f32 - px(FLOAT_MARGIN) - dock.height;- if let Some(appbar) = &mut self.appbar {- // A hidden dock leaves the whole screen to maximized windows.- let strip = if self.config.auto_hide {- 0- } else {- (dock.height + 2.0 * px(FLOAT_MARGIN)) as i32- };- appbar.reserve(Edge::Bottom, screen, strip);- }+ // A hidden dock leaves the whole screen to maximized windows.+ let strip = if self.config.auto_hide {+ 0+ } else {+ (dock.height + 2.0 * px(FLOAT_MARGIN)) as i32+ };let (dock_right, dock_bottom) = (dock_x + dock.width, dock_y + dock.height);let zone = move |left: f32, top: f32, right: f32, bottom: f32| RECT {left: (dock_x - left) as i32,@@ -760,7 +759,13 @@ impl App {self.stage = Some(stage);self.place_windows()?;self.schedule_trim();- self.render()+ self.render()?;+ // Last: reserving space is a synchronous call to Explorer (40-60 ms+ // measured), and the dock is already on screen meanwhile.+ if let Some(appbar) = &mut self.appbar {+ appbar.reserve(Edge::Bottom, screen, strip);+ }+ Ok(())}fn place_windows(&self) -> Result<()> {@@ -2150,6 +2155,10 @@ unsafe extern "system" fn wndproc(hwnd: HWND, msg: u32, w: WPARAM, l: LPARAM) ->HSHELL_WINDOWACTIVATED | HSHELL_RUDEAPPACTIVATED => {with_app("ventana activa", App::update_fullscreen);}+ // Terminals and browsers retitle their windows many times a minute.+ // The dock shows no titles, so only a window it does not list yet+ // (an app that declares its identity late) needs a new look.+ HSHELL_REDRAW if is_tracked(l.0) => {}code => {if code == HSHELL_FLASH {with_app("aviso de atención", |app| {@@ -2166,6 +2175,16 @@ unsafe extern "system" fn wndproc(hwnd: HWND, msg: u32, w: WPARAM, l: LPARAM) ->LRESULT(0)}+/// `true` if the dock already lists the window `hwnd`.+fn is_tracked(hwnd: isize) -> bool {+ APP.with(|cell| {+ cell.try_borrow().is_ok_and(|app| {+ app.as_ref()+ .is_some_and(|app| app.windows.iter().any(|window| window.hwnd == hwnd))+ })+ })+}+fn is_animating() -> bool {APP.with_borrow(|app| app.as_ref().is_some_and(App::is_animating))}@@ -2317,12 +2336,18 @@ pub fn run() -> Result<()> {let _ = SetConsoleCtrlHandler(None, false);let _ = SetConsoleCtrlHandler(Some(on_console_ctrl), true);- app.apply_menu_bar_setting();app.sync()?;// `sync` recomposes only when the list changes; run it unconditionally at startup.if app.stage.is_none() {app.refresh()?;}+ // After the dock: creating the bar and reserving its strip take+ // 60-130 ms, which the dock no longer waits for.+ app.apply_menu_bar_setting();+ // Otherwise the bar shows no app name until the user switches windows.+ if let Err(error) = app.update_menu_bar_app() {+ eprintln!("muelle: barra superior: {error:#}");+ }// The shell notifies this window when a window is created, closed,// activated, or requests attention.diff --git a/apps/dock/src/menubar.rs b/apps/dock/src/menubar.rsindex 958d014..97ca44c 100644--- a/apps/dock/src/menubar.rs+++ b/apps/dock/src/menubar.rs@@ -23,6 +23,7 @@ use muelle_shell::{status::{self, AudioWatch, Network, NetworkWatch, TaskbarButtons, Volume},system::{self, Battery, Monitor},};+use std::sync::atomic::{AtomicBool, Ordering};use windows::{UI::{Color,@@ -38,9 +39,10 @@ use windows::{UI::Input::KeyboardAndMouse::{TME_LEAVE, TRACKMOUSEEVENT, TrackMouseEvent},UI::WindowsAndMessaging::{CreateWindowExW, DefWindowProcW, DestroyWindow, HWND_TOPMOST, IDC_ARROW, LoadCursorW,- RegisterClassW, SWP_HIDEWINDOW, SWP_NOACTIVATE, SWP_SHOWWINDOW, SetTimer, SetWindowPos,- WM_APP, WM_LBUTTONUP, WM_MOUSEMOVE, WM_SETTINGCHANGE, WM_TIMER, WNDCLASSW,- WS_EX_NOACTIVATE, WS_EX_NOREDIRECTIONBITMAP, WS_EX_TOOLWINDOW, WS_EX_TOPMOST, WS_POPUP,+ PostMessageW, RegisterClassW, SWP_HIDEWINDOW, SWP_NOACTIVATE, SWP_SHOWWINDOW, SetTimer,+ SetWindowPos, WM_APP, WM_LBUTTONUP, WM_MOUSEMOVE, WM_SETTINGCHANGE, WM_TIMER,+ WNDCLASSW, WS_EX_NOACTIVATE, WS_EX_NOREDIRECTIONBITMAP, WS_EX_TOOLWINDOW,+ WS_EX_TOPMOST, WS_POPUP,},},core::{BOOL, Interface, w},@@ -69,6 +71,13 @@ const WM_MOUSELEAVE: u32 = 0x02A3;const APPBAR_CALLBACK: u32 = WM_APP + 20;/// Posted by the network and audio watches.const STATUS_CHANGED: u32 = WM_APP + 21;+/// Posted by the bar to itself to update from the message loop.+const TICK: u32 = WM_APP + 22;+/// `wParam` of WM_SETTINGCHANGE when the work area changes, which every+/// AppBar reservation (the dock's, this bar's) causes.+const SPI_SETWORKAREA: usize = 0x002F;+/// A `TICK` is already posted.+static TICK_PENDING: AtomicBool = AtomicBool::new(false);/// Segoe Fluent Icons code points.mod glyph {@@ -146,7 +155,23 @@ unsafe extern "system" fn bar_proc(hwnd: HWND, msg: u32, w: WPARAM, l: LPARAM) -WM_MOUSELEAVE => app::menu_bar_hover(false),// The minute changed, the charger was plugged or unplugged, the// network or volume changed, or a Windows setting (taskbar buttons).- WM_TIMER | WM_POWERBROADCAST | STATUS_CHANGED | WM_SETTINGCHANGE => app::menu_bar_tick(),+ // Nothing the bar shows depends on the work area.+ WM_SETTINGCHANGE if w.0 == SPI_SETWORKAREA => {}+ // Some of these are sent while the dock is busy reserving space, and+ // updating then was dropped. From the message loop the dock is never+ // busy, and a burst of them makes a single update.+ WM_TIMER | WM_POWERBROADCAST | STATUS_CHANGED | WM_SETTINGCHANGE => {+ if !TICK_PENDING.swap(true, Ordering::Relaxed) {+ // SAFETY: Posting to our own live window.+ unsafe {+ let _ = PostMessageW(Some(hwnd), TICK, WPARAM(0), LPARAM(0));+ }+ }+ }+ TICK => {+ TICK_PENDING.store(false, Ordering::Relaxed);+ app::menu_bar_tick();+ }// SAFETY: Standard forwarding of unhandled messages._ => return unsafe { DefWindowProcW(hwnd, msg, w, l) },}diff --git a/scripts/measure.ps1 b/scripts/measure.ps1index e08ac57..e89effd 100644--- a/scripts/measure.ps1+++ b/scripts/measure.ps1@@ -15,7 +15,10 @@#>param([string]$Exe = (Join-Path $env:USERPROFILE '.cargo-target\muelle\release\muelle.exe'),- [int]$IdleSeconds = 10+ [int]$IdleSeconds = 10,+ # Startup work (icon cache, taskbar, status watches) settles over the first+ # seconds; sampling earlier counts it as idle use.+ [int]$WarmSeconds = 20)$ErrorActionPreference = 'Stop'@@ -40,7 +43,7 @@ while ($clock.ElapsedMilliseconds -lt 10000) {Start-Sleep -Milliseconds 5}-Start-Sleep -Seconds 3 # Let memory trimming finish before sampling.+Start-Sleep -Seconds $WarmSeconds # Let startup and memory trimming finish.$procs = Get-CimInstance Win32_Process -Filter "Name='muelle.exe'"$cpuBefore = @{}foreach ($p in $procs) { $cpuBefore[$p.ProcessId] = (Get-Process -Id $p.ProcessId).TotalProcessorTime }diff --git a/spikes/src/bin/spike_genie.rs b/spikes/src/bin/spike_genie.rsindex fb58e23..3673d6d 100644--- a/spikes/src/bin/spike_genie.rs+++ b/spikes/src/bin/spike_genie.rs@@ -14,9 +14,9 @@ use windows::{Graphics::Gdi::CreateSolidBrush,System::LibraryLoader::GetModuleHandleW,UI::WindowsAndMessaging::{- CreateWindowExW, DefWindowProcW, DispatchMessageW, MSG, PM_REMOVE, PeekMessageW,- RegisterClassW, SW_MINIMIZE, SW_RESTORE, SW_SHOWNOACTIVATE, ShowWindow, WNDCLASSW,- WS_OVERLAPPEDWINDOW,+ CreateWindowExW, DefWindowProcW, DestroyWindow, DispatchMessageW, MSG, PM_REMOVE,+ PeekMessageW, RegisterClassW, SW_MINIMIZE, SW_RESTORE, SW_SHOWNOACTIVATE, ShowWindow,+ WNDCLASSW, WS_OVERLAPPEDWINDOW,},},core::w,@@ -58,6 +58,44 @@ fn magenta(rect: RECT, y: i32) -> f32 {})}+/// Opens and closes `cycles` extra windows of this app, so the dock adds and+/// removes them, to look for leaks.+fn churn(first: HWND, cycles: u32) {+ pump(2500);+ for _ in 0..cycles {+ // SAFETY: Creates and destroys windows of the class registered in `main`.+ unsafe {+ let Ok(instance) = GetModuleHandleW(None) else {+ return;+ };+ let Ok(extra) = CreateWindowExW(+ Default::default(),+ w!("MuelleSpikeGenie"),+ w!("Genie spike churn"),+ WS_OVERLAPPEDWINDOW,+ 400,+ 200,+ 600,+ 400,+ None,+ None,+ Some(instance.into()),+ None,+ ) else {+ return;+ };+ let _ = ShowWindow(extra, SW_SHOWNOACTIVATE);+ pump(600);+ let _ = DestroyWindow(extra);+ pump(600);+ }+ }+ // SAFETY: Our own window.+ unsafe {+ let _ = DestroyWindow(first);+ }+}+fn main() {let mut args = std::env::args().skip(1);let cycles: u32 = args@@ -106,6 +144,10 @@ fn main() {let _ = ShowWindow(hwnd, SW_SHOWNOACTIVATE);hwnd};+ if std::env::var_os("SPIKE_CHURN").is_some() {+ churn(hwnd, cycles);+ return;+ }pump(2500);// Rows well inside the client area, near its top and its bottom.let (top, bottom) = (rect.top + 80, rect.bottom - 40);