misterdownloadmanager: The ultimate open-source, clean-room download manager written from scratch in pure Rust for Windows, Mac, and Linux
Rust bittorrent-clientdebriddownload-acceleratordownload-managerdownloadmanagereguifile-downloadergallery-dlidmidm-alternativeinternet-download-managerm3u8-downloadermagnetplaylist-downloaderreal-debridrusttorrent-clientvideo-downloaderyoutube-downloaderyt-dlp
Windows with a drawn title bar have no title bar style on Windows 11: no black band (part of #208) (#291)
Ahmed Nefzaoui984427bf40b2
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 38f09a99..acb44579 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -672,6 +672,17 @@ choose. And Linux, checked on a real desktop with every change.picture where the title bar would have ended. Menus and the drop target are now plain pop-upwindows, as Windows' own menus are, with nothing above their contents to take for a title bar;every window of MDM without a title bar is made one the same way.+- **The progress window, and the windows with a style theme's title bar, without a black band+ across their top** ([#208](https://github.com/anefzaoui/misterdownloadmanager/issues/208)): on+ the same Windows 11 Insider build, the progress window showed a black band a title bar tall+ above its own title bar and a thin one down its left side, its contents moved down and cut off+ at the bottom; so could every dialog and the main window with Metro, Luna, Aero or Aqua. MDM+ draws these title bars itself, in windows that kept the system's title bar style, hidden.+ On Windows 11 they now have no title bar style at all: the progress window and the dialogs are+ plain windows, all theirs; the main window keeps the system's resizing edges, so it still+ resizes, snaps and maximizes as before, with a thin strip of its title bar's colour above it.+ Moving them by their title bar, their taskbar button, Alt+Tab and the window menu (Alt+Space,+ or a right-click on the title bar) work as before. Windows 10 keeps the frames it had.- **A window closed with its title bar's × no longer turns black first**([#207](https://github.com/anefzaoui/misterdownloadmanager/issues/207)): every dialog and theprogress window turned black for a moment when closed with the × or Alt+F4, rather than withdiff --git a/ROADMAP.md b/ROADMAP.mdindex c38b20eb..72ef3e13 100644--- a/ROADMAP.md+++ b/ROADMAP.md@@ -415,10 +415,11 @@ Everything below ships together, in the next release.### Bugs being looked into-- 🔍 Menus and the drop target with a black band across their top on a Windows 11 Insider build- ([#208](https://github.com/anefzaoui/misterdownloadmanager/issues/208)): menus are plain pop-up- windows now, as Windows' own are, and the reporter confirmed them fixed; the band on windows- with a drawn title bar (the download's progress window) is being fixed+- 🔍 Windows with a black band across their top on a Windows 11 Insider build+ ([#208](https://github.com/anefzaoui/misterdownloadmanager/issues/208)): menus and the drop+ target are plain pop-up windows now, as Windows' own are, and the reporter confirmed them fixed;+ the progress window and every window whose title bar MDM draws (the style themes) are framed+ without a title bar style too, their test build is with the reporter- 🔍 Menus opening as the pointer crosses the menu bar on a tiling window manager([#277](https://github.com/anefzaoui/misterdownloadmanager/issues/277)): after a command thebar waits for a click, as before the first one (now tested on Windows and Linux); once a menu isdiff --git a/crates/app/src/app.rs b/crates/app/src/app.rsindex a5e1b5ed..d1fe9df8 100644--- a/crates/app/src/app.rs+++ b/crates/app/src/app.rs@@ -6075,6 +6075,8 @@ impl App {];let bar = caption::title_bar(ui, rect, crate::MAIN_TITLE, &buttons);if let Some(h) = self.main_hwnd {+ // The sizing frame above the bar (Windows 11, #208) in the bar's colour.+ crate::win32::caption_colour(h, [bar.top.r(), bar.top.g(), bar.top.b()]);let ppp = ui.ctx().pixels_per_point();let px = |r: egui::Rect| {let (a, b) = ((r.min - full.min) * ppp, (r.max - full.min) * ppp);diff --git a/crates/app/src/win32.rs b/crates/app/src/win32.rsindex b4f7e67f..6d29ad3d 100644--- a/crates/app/src/win32.rs+++ b/crates/app/src/win32.rs@@ -535,6 +535,20 @@ unsafe extern "system" fn drawn_frame_proc(pub fn drawn_frame(title: &str, on: bool) -> Option<isize> {use windows_sys::Win32::UI::Shell::RemoveWindowSubclass;let hwnd = find_own_window(title)?;+ if caption_less_frames() {+ // SAFETY: our own window, on its thread.+ unsafe {+ if on {+ caption_less(hwnd, Drawn::Sizable);+ } else {+ caption_back(hwnd);+ }+ }+ if !on {+ forget_caption(hwnd as isize);+ }+ return Some(hwnd as isize);+ }// SAFETY: our own window; the subclass id is ours.unsafe {if on {@@ -577,8 +591,13 @@ pub fn thin_frame(AppendMenuW, DeleteMenu, GetSystemMenu, MF_BYCOMMAND, MF_STRING,};let hwnd = find_own_window(title)?;+ let plain = caption_less_frames();unsafe {- SetWindowSubclass(hwnd, Some(thin_frame_proc), 0x1d6, 0);+ if plain {+ caption_less(hwnd, Drawn::Fixed);+ } else {+ SetWindowSubclass(hwnd, Some(thin_frame_proc), 0x1d6, 0);+ }let menu = GetSystemMenu(hwnd, 0);if !menu.is_null() {DeleteMenu(menu, SC_HIDE_TO_TRAY as u32, MF_BYCOMMAND);@@ -587,20 +606,488 @@ pub fn thin_frame(AppendMenuW(menu, MF_STRING, SC_HIDE_TO_TRAY, w.as_ptr());}}+ // The thin frame's 2 px each side but the top, or none: a plain window is all client+ // (Windows 11 draws its 1 px border over the client's edge).let dpi = GetDpiForWindow(hwnd).max(96) as f32 / 96.0;+ let (fw, fh) = if plain { (0.0, 0.0) } else { (4.0, 2.0) };SetWindowPos(hwnd,std::ptr::null_mut(),0,0,- (client[0] * scale + 4.0 * dpi).round() as i32,- (client[1] * scale + 2.0 * dpi).round() as i32,+ (client[0] * scale + fw * dpi).round() as i32,+ (client[1] * scale + fh * dpi).round() as i32,SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE,);}Some(hwnd as isize)}+/// Whether the windows whose title bar is ours are framed without a caption style+/// ([`caption_less`], #208): on Windows 11 (build 22000 on), which rounds such a window when asked+/// and draws its border. Windows 10 does neither for a window without a caption style, so it+/// keeps the frames above (the caption style, its caption given to the client): the band they+/// showed was seen on a Windows 11 build only.+fn caption_less_frames() -> bool {+ use windows_sys::Win32::System::Registry::HKEY_LOCAL_MACHINE;+ static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();+ *ON.get_or_init(|| {+ reg_string_in(+ HKEY_LOCAL_MACHINE,+ r"SOFTWARE\Microsoft\Windows NT\CurrentVersion",+ "CurrentBuild",+ )+ .and_then(|b| b.parse::<u32>().ok())+ .is_some_and(|b| b >= 22000)+ })+}++/// How a window whose title bar is ours is framed without a caption style ([`caption_less`]).+#[derive(Clone, Copy, Debug, PartialEq, Eq)]+pub(crate) enum Drawn {+ /// A fixed size (the progress window, a dialog with a style theme's title bar): a plain+ /// popup, all client, as the drop target and the menus are (#210, #231).+ Fixed,+ /// Resizable (the main window with a style theme): a popup with the system's sizing frame,+ /// for resizing on every edge, Aero Snap and Snap Layouts, the shadow, and maximizing.+ Sizable,+}++/// The subclass of a window framed by [`caption_less`]: its id, and its data for each kind.+const DRAWN_SUBCLASS: usize = 0x1d8;++impl Drawn {+ fn data(self) -> usize {+ match self {+ Drawn::Fixed => 0,+ Drawn::Sizable => 1,+ }+ }+}++/// The style of a window whose title bar is ours, framed without a caption style (#208): a+/// popup with no caption, border or dialog frame, with the system's sizing frame when it is+/// resizable; its system menu and its Minimize and Maximize kept (the taskbar's and Alt+Space's+/// commands). With a caption style hidden by our WM_NCCALCSIZE, a Windows 11 Insider build+/// (22635) placed the window's picture where the style's client would have been: a black band a+/// title bar tall across the top of the progress window, as it did with the menus (#210) and the+/// drop target (#231). Here what the styles describe is what the window is: the system's own+/// WM_NCCALCSIZE frames it, no other reading can differ.+pub(crate) fn drawn_style(style: u32, kind: Drawn) -> u32 {+ use windows_sys::Win32::UI::WindowsAndMessaging::{WS_CAPTION, WS_POPUP};+ let plain = (style & !(WS_CAPTION | WS_THICKFRAME)) | WS_POPUP;+ match kind {+ Drawn::Fixed => plain,+ Drawn::Sizable => plain | WS_THICKFRAME,+ }+}++/// Its extended styles: without the edges that add to a frame (IDM's dialog frame among them).+/// The rest stays: a taskbar button where it had one, never-activate in an off-screen test copy.+pub(crate) fn drawn_ex_style(ex: u32) -> u32 {+ use windows_sys::Win32::UI::WindowsAndMessaging::{WS_EX_CLIENTEDGE, WS_EX_STATICEDGE};+ ex & !(WS_EX_DLGMODALFRAME | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE)+}++/// Frame our window `hwnd`, whose title bar is ours, without a caption style ([`drawn_style`]).+/// Its subclass keeps the styles when winit sets its own again (on every change of its window+/// flags) and hit-tests the drawn title bar. Again on the same window: the styles applied again.+///+/// # Safety+/// `hwnd` is a window of this thread.+unsafe fn caption_less(hwnd: HWND, kind: Drawn) {+ use windows_sys::Win32::Graphics::Dwm::{+ DWMWA_WINDOW_CORNER_PREFERENCE, DWMWCP_ROUND, DwmSetWindowAttribute,+ };+ use windows_sys::Win32::UI::WindowsAndMessaging::SWP_NOSIZE;+ // SAFETY: (the caller's) our own window; the proc lives as long as the program; the DWM+ // attribute is read from a local during the call.+ unsafe {+ // The subclass first: it keeps the styles below when winit sets its own again.+ SetWindowSubclass(hwnd, Some(caption_less_proc), DRAWN_SUBCLASS, kind.data());+ let ex = GetWindowLongPtrW(hwnd, GWL_EXSTYLE) as u32;+ SetWindowLongPtrW(hwnd, GWL_EXSTYLE, drawn_ex_style(ex) as isize);+ let style = GetWindowLongPtrW(hwnd, GWL_STYLE) as u32;+ SetWindowLongPtrW(hwnd, GWL_STYLE, drawn_style(style, kind) as isize);+ // Windows 11 rounds a window with a caption style by itself, not a popup: asked for.+ let corner = DWMWCP_ROUND;+ DwmSetWindowAttribute(+ hwnd,+ DWMWA_WINDOW_CORNER_PREFERENCE as u32,+ &corner as *const _ as *const c_void,+ size_of::<i32>() as u32,+ );+ // The frame measured again (WM_NCCALCSIZE), by the system.+ SetWindowPos(+ hwnd,+ std::ptr::null_mut(),+ 0,+ 0,+ 0,+ 0,+ SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE,+ );+ }+}++/// The system's title bar back on a window framed by [`caption_less`] (the main window, back to+/// a theme without a title bar of its own).+///+/// # Safety+/// `hwnd` is a window of this thread.+unsafe fn caption_back(hwnd: HWND) {+ use windows_sys::Win32::Graphics::Dwm::{+ DWMWA_CAPTION_COLOR, DWMWA_COLOR_DEFAULT, DWMWA_WINDOW_CORNER_PREFERENCE, DWMWCP_DEFAULT,+ DwmSetWindowAttribute,+ };+ use windows_sys::Win32::UI::Shell::RemoveWindowSubclass;+ use windows_sys::Win32::UI::WindowsAndMessaging::{SWP_NOSIZE, WS_CAPTION, WS_POPUP};+ // SAFETY: (the caller's) our own window; DWM attributes read from locals during the calls.+ unsafe {+ RemoveWindowSubclass(hwnd, Some(caption_less_proc), DRAWN_SUBCLASS);+ let style = GetWindowLongPtrW(hwnd, GWL_STYLE) as u32;+ SetWindowLongPtrW(hwnd, GWL_STYLE, ((style & !WS_POPUP) | WS_CAPTION) as isize);+ let corner = DWMWCP_DEFAULT;+ DwmSetWindowAttribute(+ hwnd,+ DWMWA_WINDOW_CORNER_PREFERENCE as u32,+ &corner as *const _ as *const c_void,+ size_of::<i32>() as u32,+ );+ let colour = DWMWA_COLOR_DEFAULT;+ DwmSetWindowAttribute(+ hwnd,+ DWMWA_CAPTION_COLOR as u32,+ &colour as *const u32 as *const c_void,+ size_of::<u32>() as u32,+ );+ SetWindowPos(+ hwnd,+ std::ptr::null_mut(),+ 0,+ 0,+ 0,+ 0,+ SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE,+ );+ }+ CAPTION_COLOUR.with(|m| {+ if let Ok(mut m) = m.try_borrow_mut() {+ m.remove(&(hwnd as isize));+ }+ });+}++thread_local! {+ /// The colour last given to each resizable window's frame above its drawn title bar.+ static CAPTION_COLOUR: std::cell::RefCell<std::collections::HashMap<isize, u32>> =+ Default::default();+}++/// The colour of the sizing frame's strip above the main window's drawn title bar (`rgb`, the+/// top of the bar), so the two read as one bar (Windows 11 paints the frame of a window without+/// a caption style in its caption colour). Cheap: DWM is asked only when it changes.+pub fn caption_colour(hwnd: isize, rgb: [u8; 3]) {+ use windows_sys::Win32::Graphics::Dwm::{DWMWA_CAPTION_COLOR, DwmSetWindowAttribute};+ if !caption_less_frames() {+ return;+ }+ let colour = u32::from_le_bytes([rgb[0], rgb[1], rgb[2], 0]);+ let changed = CAPTION_COLOUR.with(|m| {+ m.try_borrow_mut()+ .is_ok_and(|mut m| m.insert(hwnd, colour) != Some(colour))+ });+ if changed {+ // SAFETY: our own window; the colour is read from a local during the call.+ unsafe {+ DwmSetWindowAttribute(+ hwnd as HWND,+ DWMWA_CAPTION_COLOR as u32,+ &colour as *const u32 as *const c_void,+ size_of::<u32>() as u32,+ );+ }+ }+}++/// The window menu (the system menu) of a window framed by [`caption_less`], opened at screen+/// point `(x, y)` as the system opens it from a caption: right-click on the title bar, a click on+/// its icon. Its items are enabled as the window's state allows; the command chosen is carried+/// out as the system's (WM_SYSCOMMAND). Alt+Space opens it through the system already.+fn window_menu(hwnd: HWND, x: i32, y: i32, sizable: bool) {+ use windows_sys::Win32::UI::WindowsAndMessaging::{+ EnableMenuItem, GetSystemMenu, IsIconic, IsZoomed, MF_BYCOMMAND, MF_ENABLED, MF_GRAYED,+ PostMessageW, SC_CLOSE, SC_MAXIMIZE, SC_MINIMIZE, SC_MOVE, SC_RESTORE, SC_SIZE,+ SetMenuDefaultItem, TPM_RETURNCMD, TPM_RIGHTBUTTON, TrackPopupMenu, WM_SYSCOMMAND,+ WS_MAXIMIZEBOX, WS_MINIMIZEBOX,+ };+ // SAFETY: our own window and its system menu, on its thread.+ unsafe {+ let menu = GetSystemMenu(hwnd, 0);+ if menu.is_null() {+ return;+ }+ let (zoomed, iconic) = (IsZoomed(hwnd) != 0, IsIconic(hwnd) != 0);+ let style = GetWindowLongPtrW(hwnd, GWL_STYLE) as u32;+ for (cmd, on) in [+ (SC_RESTORE, zoomed || iconic),+ (SC_MOVE, !zoomed),+ (SC_SIZE, sizable && !zoomed && !iconic),+ (SC_MINIMIZE, style & WS_MINIMIZEBOX != 0 && !iconic),+ (+ SC_MAXIMIZE,+ sizable && style & WS_MAXIMIZEBOX != 0 && !zoomed,+ ),+ (SC_CLOSE, true),+ ] {+ let flag = if on { MF_ENABLED } else { MF_GRAYED };+ EnableMenuItem(menu, cmd, MF_BYCOMMAND | flag);+ }+ SetMenuDefaultItem(menu, SC_CLOSE, 0);+ let cmd = TrackPopupMenu(+ menu,+ TPM_RETURNCMD | TPM_RIGHTBUTTON,+ x,+ y,+ 0,+ hwnd,+ std::ptr::null(),+ );+ if cmd != 0 {+ PostMessageW(hwnd, WM_SYSCOMMAND, cmd as WPARAM, 0);+ }+ }+}++/// A window framed by [`caption_less`]: it keeps its styles, the system frames it+/// (WM_NCCALCSIZE as its styles say), and its drawn title bar is hit-tested as a caption: the+/// buttons are the client's (egui), the icon the system menu's, the rest the caption (moved,+/// snapped, shaken, double-clicked by the system). A resizable one's Maximize answers as the+/// system's (Snap Layouts) and it maximizes to the work area, as a window with a caption does.+unsafe extern "system" fn caption_less_proc(+ hwnd: HWND,+ msg: u32,+ wparam: WPARAM,+ lparam: LPARAM,+ _id: usize,+ data: usize,+) -> LRESULT {+ use windows_sys::Win32::Foundation::POINT;+ use windows_sys::Win32::Graphics::Gdi::ScreenToClient;+ use windows_sys::Win32::UI::Input::KeyboardAndMouse::{+ TME_LEAVE, TME_NONCLIENT, TRACKMOUSEEVENT, TrackMouseEvent,+ };+ use windows_sys::Win32::UI::WindowsAndMessaging::{+ DefWindowProcW, HTMAXBUTTON, IsZoomed, PostMessageW, SC_CLOSE, SC_MAXIMIZE, SC_RESTORE,+ STYLESTRUCT, SendMessageW, WM_GETMINMAXINFO, WM_NCACTIVATE, WM_NCLBUTTONDBLCLK,+ WM_NCLBUTTONDOWN, WM_NCLBUTTONUP, WM_NCMOUSELEAVE, WM_NCMOUSEMOVE, WM_NCRBUTTONUP,+ WM_STYLECHANGING, WM_SYSCOMMAND,+ };+ const HT_MAX: LRESULT = HTMAXBUTTON as LRESULT;+ let sizable = data == Drawn::Sizable.data();+ let kind = if sizable {+ Drawn::Sizable+ } else {+ Drawn::Fixed+ };+ let at = |lp: LPARAM| {+ (+ (lp & 0xffff) as i16 as i32,+ ((lp >> 16) & 0xffff) as i16 as i32,+ )+ };+ match msg {+ WM_STYLECHANGING if lparam != 0 => {+ // SAFETY: for WM_STYLECHANGING, lParam points to the STYLESTRUCT of the change,+ // whose styleNew the window may alter (Win32 contract).+ let s = unsafe { &mut *(lparam as *mut STYLESTRUCT) };+ match wparam as i32 {+ GWL_STYLE => s.styleNew = drawn_style(s.styleNew, kind),+ GWL_EXSTYLE => s.styleNew = drawn_ex_style(s.styleNew),+ _ => {}+ }+ return 0;+ }+ // The system's frame for these styles, nothing of ours or winit's: what any reading of+ // the styles finds.+ WM_NCCALCSIZE => return unsafe { DefWindowProcW(hwnd, msg, wparam, lparam) },+ // Activation changes repaint the non-client area: with lParam -1 the system skips that.+ WM_NCACTIVATE => return unsafe { DefSubclassProc(hwnd, msg, wparam, -1) },+ WM_SYSCOMMAND if (wparam & 0xFFF0) == SC_HIDE_TO_TRAY => {+ HIDE_ASKED.with(|s| {+ if let Ok(mut s) = s.try_borrow_mut() {+ s.insert(hwnd as isize);+ }+ });+ return 0;+ }+ // The window menu from the title bar, which the system opens only from a caption.+ WM_NCRBUTTONUP if wparam as LRESULT == HTCAPTION || wparam as LRESULT == HTSYSMENU => {+ let (x, y) = at(lparam);+ window_menu(hwnd, x, y, sizable);+ return 0;+ }+ WM_NCLBUTTONDOWN if wparam as LRESULT == HTSYSMENU => {+ // Under the icon, as the system opens it from its own.+ let hits = CAPTION_HITS.with(|m| {+ m.try_borrow()+ .ok()+ .and_then(|m| m.get(&(hwnd as isize)).map(|h| h.icon))+ });+ let mut pt = match hits {+ Some(icon) => POINT {+ x: icon[0],+ y: icon[3],+ },+ None => POINT { x: 0, y: 0 },+ };+ // SAFETY: our own window; the point is a local.+ unsafe { windows_sys::Win32::Graphics::Gdi::ClientToScreen(hwnd, &mut pt) };+ window_menu(hwnd, pt.x, pt.y, sizable);+ return 0;+ }+ WM_NCLBUTTONDBLCLK if wparam as LRESULT == HTSYSMENU => {+ // SAFETY: our own window, a system command it handles.+ unsafe { PostMessageW(hwnd, WM_SYSCOMMAND, SC_CLOSE as WPARAM, 0) };+ return 0;+ }+ // Maximize: pressed and released on it toggles the window (the system's own button would+ // draw itself over ours).+ WM_NCLBUTTONDOWN | WM_NCLBUTTONDBLCLK if sizable && wparam as LRESULT == HT_MAX => {+ set_max_button(+ hwnd,+ MaxButton {+ hot: true,+ pressed: true,+ },+ );+ return 0;+ }+ WM_NCLBUTTONUP if sizable && wparam as LRESULT == HT_MAX => {+ let was = max_button_state(hwnd as isize);+ set_max_button(+ hwnd,+ MaxButton {+ hot: true,+ pressed: false,+ },+ );+ if was.pressed {+ // SAFETY: our own window, a system command it handles.+ let zoomed = unsafe { IsZoomed(hwnd) } != 0;+ let cmd = if zoomed { SC_RESTORE } else { SC_MAXIMIZE };+ unsafe { SendMessageW(hwnd, WM_SYSCOMMAND, cmd as WPARAM, 0) };+ }+ return 0;+ }+ WM_NCMOUSEMOVE if sizable => {+ let on = wparam as LRESULT == HT_MAX;+ let was = max_button_state(hwnd as isize);+ set_max_button(+ hwnd,+ MaxButton {+ hot: on,+ pressed: was.pressed && on,+ },+ );+ if on {+ let mut t = TRACKMOUSEEVENT {+ cbSize: size_of::<TRACKMOUSEEVENT>() as u32,+ dwFlags: TME_LEAVE | TME_NONCLIENT,+ hwndTrack: hwnd,+ dwHoverTime: 0,+ };+ // SAFETY: a plain call with a filled struct, our own window.+ unsafe { TrackMouseEvent(&mut t) };+ }+ }+ WM_NCMOUSELEAVE if sizable => set_max_button(hwnd, MaxButton::default()),+ WM_GETMINMAXINFO if sizable && lparam != 0 => {+ let res = unsafe { DefSubclassProc(hwnd, msg, wparam, lparam) };+ // SAFETY: for WM_GETMINMAXINFO, lParam points to the MINMAXINFO to fill.+ let info = unsafe {+ &mut *(lparam as *mut windows_sys::Win32::UI::WindowsAndMessaging::MINMAXINFO)+ };+ if let Some((pos, size)) = maximized_to_work_area(hwnd) {+ info.ptMaxPosition = pos;+ info.ptMaxSize = size;+ }+ return res;+ }+ _ => {}+ }+ let res = unsafe { DefSubclassProc(hwnd, msg, wparam, lparam) };+ if msg == WM_NCHITTEST && res == HTCLIENT {+ let (x, y) = at(lparam);+ let mut pt = POINT { x, y };+ // SAFETY: our own window; the point is a local.+ unsafe { ScreenToClient(hwnd, &mut pt) };+ let hit = CAPTION_HITS.with(|m| {+ m.try_borrow().ok().and_then(|m| {+ m.get(&(hwnd as isize)).map(|h| {+ if sizable+ && h.max_button+ .and_then(|i| h.buttons.get(i))+ .is_some_and(|b| inside(b, pt.x, pt.y))+ {+ HT_MAX+ } else {+ frame_hit(pt.x, pt.y, h)+ }+ })+ })+ });+ return hit.unwrap_or(HTCLIENT);+ }+ res+}++/// Where and how large window `hwnd` is when maximized: its monitor's work area (not over the+/// taskbar), with its sizing frame beyond the work area's edges, as a window with a caption+/// style is maximized (Windows maximizes one without a caption over the whole monitor). In the+/// coordinates WM_GETMINMAXINFO takes: from the monitor's top left.+fn maximized_to_work_area(+ hwnd: HWND,+) -> Option<(+ windows_sys::Win32::Foundation::POINT,+ windows_sys::Win32::Foundation::POINT,+)> {+ use windows_sys::Win32::Foundation::POINT;+ use windows_sys::Win32::Graphics::Gdi::{+ GetMonitorInfoW, MONITOR_DEFAULTTONEAREST, MONITORINFO, MonitorFromWindow,+ };+ use windows_sys::Win32::UI::HiDpi::{AdjustWindowRectExForDpi, GetDpiForWindow};+ // SAFETY: plain queries about our own window and its monitor, into locals.+ unsafe {+ let mon = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);+ let mut mi: MONITORINFO = zeroed();+ mi.cbSize = size_of::<MONITORINFO>() as u32;+ if mon.is_null() || GetMonitorInfoW(mon, &mut mi) == 0 {+ return None;+ }+ let style = GetWindowLongPtrW(hwnd, GWL_STYLE) as u32;+ let ex = GetWindowLongPtrW(hwnd, GWL_EXSTYLE) as u32;+ let mut f = RECT {+ left: 0,+ top: 0,+ right: 0,+ bottom: 0,+ };+ AdjustWindowRectExForDpi(&mut f, style, 0, ex, GetDpiForWindow(hwnd).max(96));+ let (w, m) = (mi.rcWork, mi.rcMonitor);+ Some((+ POINT {+ x: w.left - m.left + f.left,+ y: w.top - m.top + f.top,+ },+ POINT {+ x: (w.right - w.left) + f.right - f.left,+ y: (w.bottom - w.top) + f.bottom - f.top,+ },+ ))+ }+}+#[cfg(test)]mod frame_tests {use super::*;@@ -631,6 +1118,192 @@ mod frame_tests {}}+#[cfg(test)]+mod caption_less_tests {+ use super::*;+ use windows_sys::Win32::UI::WindowsAndMessaging::{+ AdjustWindowRectEx, CreateWindowExW, DestroyWindow, GetClientRect, GetWindowRect,+ MINMAXINFO, SendMessageW, WM_GETMINMAXINFO, WS_CAPTION, WS_EX_APPWINDOW, WS_POPUP,+ };++ /// The styles winit gives the progress window (a caption, a sizing frame trimmed by us, a+ /// system menu, Minimize; 0x14CE0000 less WS_VISIBLE) and the main window (Maximize too), and+ /// their extended styles (a window edge, an app window: a taskbar button).+ const WINIT_PROGRESS: u32 = 0x04CE_0000;+ const WINIT_MAIN: u32 = 0x04CF_0000;+ const WINIT_EX: u32 = 0x0004_0100;++ /// The frame (left, top, right, bottom) the styles alone put round a client: what a reader+ /// of the styles, rather than of the window's WM_NCCALCSIZE, takes for it.+ fn styles_frame(style: u32, ex: u32) -> [i32; 4] {+ let mut rc: RECT = unsafe { zeroed() };+ // SAFETY: plain computation on a local rectangle.+ unsafe { AdjustWindowRectEx(&mut rc, style, 0, ex) };+ [-rc.left, -rc.top, rc.right, rc.bottom]+ }++ #[test]+ fn a_drawn_title_bar_s_window_has_no_caption_style() {+ // The caption style the band came from (#208): 31 px above the client, by the styles.+ assert!(styles_frame(WINIT_PROGRESS, WINIT_EX)[1] > 20);+ let fixed = drawn_style(WINIT_PROGRESS, Drawn::Fixed);+ assert_eq!(fixed & WS_CAPTION, 0);+ assert_eq!(fixed & WS_THICKFRAME, 0);+ assert_ne!(fixed & WS_POPUP, 0);+ // The system menu and Minimize stay (Alt+Space, the taskbar's commands).+ assert_eq!(fixed & 0x000A_0000, 0x000A_0000);+ assert_eq!(styles_frame(fixed, drawn_ex_style(WINIT_EX)), [0, 0, 0, 0]);+ // Resizable: the sizing frame, the same on every side (none taller at the top).+ let sizable = drawn_style(WINIT_MAIN, Drawn::Sizable);+ assert_eq!(sizable & WS_CAPTION, 0);+ assert_ne!(sizable & WS_THICKFRAME, 0);+ assert_eq!(sizable & 0x000B_0000, 0x000B_0000);+ let [l, t, r, b] = styles_frame(sizable, WINIT_EX);+ assert!(l > 0 && l == t && t == r && r == b, "{l} {t} {r} {b}");+ // Applied again (winit setting its own styles once more), nothing changes.+ assert_eq!(drawn_style(fixed, Drawn::Fixed), fixed);+ assert_eq!(drawn_style(sizable, Drawn::Sizable), sizable);+ // IDM's dialog frame goes; the taskbar button stays.+ let ex = drawn_ex_style(WINIT_EX | WS_EX_DLGMODALFRAME);+ assert_eq!(ex & WS_EX_DLGMODALFRAME, 0);+ assert_ne!(ex & WS_EX_APPWINDOW, 0);+ }++ /// A real (hidden, off-screen) window with winit's styles, framed by caption_less: its client+ /// is where its styles say, it keeps its styles when they are set again (winit does on every+ /// change of its flags), its edges size it (resizable) or don't (fixed), and maximized it+ /// fills its monitor's work area, not the whole monitor.+ #[test]+ fn caption_less_frames_a_real_window_as_its_styles_say() {+ use windows_sys::Win32::UI::WindowsAndMessaging::{HTBOTTOMRIGHT, HTLEFT, HTTOP};+ for (n, (style, kind)) in [(WINIT_PROGRESS, Drawn::Fixed), (WINIT_MAIN, Drawn::Sizable)]+ .into_iter()+ .enumerate()+ {+ let title = format!("mdm caption-less test {n} {}", std::process::id());+ let wide: Vec<u16> = title.encode_utf16().chain(Some(0)).collect();+ // A class of its own whose procedure is the system's (a STATIC control answers+ // every hit test as transparent).+ let class: Vec<u16> = "mdm-caption-less-test\0".encode_utf16().collect();+ // SAFETY: a class registered with the system's own procedure, and a hidden window+ // of it on this thread, destroyed below; the strings are NUL-terminated; plain+ // queries and messages to it.+ unsafe {+ use windows_sys::Win32::UI::WindowsAndMessaging::{+ DefWindowProcW, RegisterClassW, WNDCLASSW,+ };+ let mut wc: WNDCLASSW = zeroed();+ wc.lpfnWndProc = Some(DefWindowProcW);+ wc.lpszClassName = class.as_ptr();+ // Registered once for both windows (the second call fails: already there).+ RegisterClassW(&wc);+ let hwnd = CreateWindowExW(+ WINIT_EX,+ class.as_ptr(),+ wide.as_ptr(),+ style,+ -20000,+ -20000,+ 548,+ 444,+ std::ptr::null_mut(),+ std::ptr::null_mut(),+ std::ptr::null_mut(),+ std::ptr::null(),+ );+ assert!(!hwnd.is_null());+ caption_less(hwnd, kind);+ let st = GetWindowLongPtrW(hwnd, GWL_STYLE) as u32;+ let ex = GetWindowLongPtrW(hwnd, GWL_EXSTYLE) as u32;+ assert_eq!(st, drawn_style(style, kind));+ // winit setting its caption style again: the subclass keeps ours.+ SetWindowLongPtrW(hwnd, GWL_STYLE, (st | WS_CAPTION) as isize);+ assert_eq!(GetWindowLongPtrW(hwnd, GWL_STYLE) as u32, st);+ // The client is where the styles alone put it: no reading of them can differ.+ let (mut w, mut c): (RECT, RECT) = (zeroed(), zeroed());+ GetWindowRect(hwnd, &mut w);+ GetClientRect(hwnd, &mut c);+ let mut at = windows_sys::Win32::Foundation::POINT { x: 0, y: 0 };+ windows_sys::Win32::Graphics::Gdi::ClientToScreen(hwnd, &mut at);+ let [l, t, r, b] = styles_frame(st, ex);+ assert_eq!([at.x - w.left, at.y - w.top], [l, t]);+ assert_eq!(+ [c.right, c.bottom],+ [w.right - w.left - l - r, w.bottom - w.top - t - b]+ );+ // Its edges: the system's sizing edges where it is resizable, else the client's+ // (the progress window keeps its size).+ let hit = |x: i32, y: i32| {+ SendMessageW(+ hwnd,+ WM_NCHITTEST,+ 0,+ ((y as u16 as u32) << 16 | x as u16 as u32) as LPARAM,+ )+ };+ let mid = (w.top + w.bottom) / 2;+ let sizable = kind == Drawn::Sizable;+ assert_eq!(+ hit(w.left + 1, mid),+ if sizable { HTLEFT as LRESULT } else { HTCLIENT }+ );+ assert_eq!(+ hit((w.left + w.right) / 2, w.top + 1),+ if sizable { HTTOP as LRESULT } else { HTCLIENT }+ );+ assert_eq!(+ hit(w.right - 2, w.bottom - 2),+ if sizable {+ HTBOTTOMRIGHT as LRESULT+ } else {+ HTCLIENT+ }+ );+ // Resized, the client follows as the styles say.+ SetWindowPos(+ hwnd,+ std::ptr::null_mut(),+ 0,+ 0,+ 700,+ 500,+ SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE,+ );+ GetClientRect(hwnd, &mut c);+ assert_eq!([c.right, c.bottom], [700 - l - r, 500 - t - b]);+ // Maximized (asked, not done: that would show it): its monitor's work area.+ if sizable {+ let mut info: MINMAXINFO = zeroed();+ SendMessageW(+ hwnd,+ WM_GETMINMAXINFO,+ 0,+ &mut info as *mut MINMAXINFO as LPARAM,+ );+ // The work area with the sizing frame beyond its edges, from the monitor's+ // top left.+ use windows_sys::Win32::Graphics::Gdi::{+ GetMonitorInfoW, MONITOR_DEFAULTTONEAREST, MONITORINFO, MonitorFromWindow,+ };+ let mut mi: MONITORINFO = zeroed();+ mi.cbSize = size_of::<MONITORINFO>() as u32;+ GetMonitorInfoW(MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST), &mut mi);+ let (wa, m) = (mi.rcWork, mi.rcMonitor);+ assert_eq!(+ [info.ptMaxPosition.x, info.ptMaxPosition.y],+ [wa.left - m.left - l, wa.top - m.top - t]+ );+ assert_eq!(+ [info.ptMaxSize.x, info.ptMaxSize.y],+ [wa.right - wa.left + l + r, wa.bottom - wa.top + t + b]+ );+ }+ DestroyWindow(hwnd);+ }+ }+ }+}+/// The main window's "Minimize to the notification area" (#63): its minimize command (title bar,/// system menu, taskbar) asks the app to hide it instead. The app can't see the window go/// minimized: an idle app runs no frame while minimized, so it would never know.diff --git a/crates/app/src/winui.rs b/crates/app/src/winui.rsindex fa592a41..0c52e667 100644--- a/crates/app/src/winui.rs+++ b/crates/app/src/winui.rs@@ -6008,6 +6008,10 @@ pub mod caption {pub pressed: Option<CaptionButton>,pub buttons: Vec<Rect>,pub icon: Rect,+ /// The colour along the bar's top edge (the sizing frame above the main window's bar+ /// takes it: win32::caption_colour). Only Windows draws title bars (elsewhere unread).+ #[cfg_attr(not(windows), allow(dead_code))]+ pub top: Color32,}/// A title bar we draw ourselves (where the window's frame is ours: win32::thin_frame): the@@ -6030,17 +6034,17 @@ pub mod caption {let p = ui.painter().clone();let st = style();let bar = bar_colour(active);- match st.era {- Some(era) => fill_shaded(- &p,- rect,- 0.0,- &bar_stops(era, bar, crate::theme::colors().gloss),- ),+ let top = match st.era {+ Some(era) => {+ let stops = bar_stops(era, bar, crate::theme::colors().gloss);+ fill_shaded(&p, rect, 0.0, &stops);+ stops.first().map_or(bar, |s| s.1)+ }None => {p.rect_filled(rect, 0.0, bar);+ bar}- }+ };let mut pressed = None;let mut rects = Vec::with_capacity(buttons.len());// From the window's end inwards, each as wide as its kind is.@@ -6163,6 +6167,7 @@ pub mod caption {pressed,buttons: rects,icon,+ top,}}}