diff --git a/apps/vectorcraft/Cargo.toml b/apps/vectorcraft/Cargo.toml index 58b9741ac5..0fe5804a16 100644 --- a/apps/vectorcraft/Cargo.toml +++ b/apps/vectorcraft/Cargo.toml @@ -46,6 +46,8 @@ winreg = "0.55" # Which GPU drives which display (EnumDisplayDevices, src/gpu.rs), so the window is drawn on the # GPU that shows it: drawing on a GPU without a monitor reset the display driver. Safe wrappers over # Win32 (MIT); the `user` feature is User32.dll only. Only the wgpu renderer chooses an adapter. +# It also lists each monitor's work area for floating panels (src/window.rs), so a build without +# the `wgpu` feature keeps the panels to the monitors' whole rects. winsafe = { version = "0.0.29", default-features = false, features = ["user"], optional = true } # Waits for Direct3D 12's adapter list before the window starts (src/gpu.rs, #4150). MIT OR # Apache-2.0, already in the build through eframe. @@ -73,7 +75,7 @@ vectorcraft-render = { workspace = true } # with default features off). Pinned exactly: review the diff before bumping. muda = { version = "=0.21.2", default-features = false } objc2 = "0.6" -objc2-app-kit = { version = "0.3", default-features = false, features = ["std", "block2", "objc2-core-foundation", "objc2-quartz-core", "NSApplication", "NSCursor", "NSImage", "NSImageRep", "NSBitmapImageRep", "NSButton", "NSColor", "NSColorSampler", "NSColorSpace", "NSControl", "NSMenu", "NSEvent", "NSPasteboard", "NSPopUpButton", "NSResponder", "NSSavePanel", "NSTextField", "NSTextInputContext", "NSView", "NSWindow"] } +objc2-app-kit = { version = "0.3", default-features = false, features = ["std", "block2", "objc2-core-foundation", "objc2-quartz-core", "NSApplication", "NSCursor", "NSImage", "NSImageRep", "NSBitmapImageRep", "NSButton", "NSColor", "NSColorSampler", "NSColorSpace", "NSControl", "NSMenu", "NSEvent", "NSPasteboard", "NSPopUpButton", "NSResponder", "NSSavePanel", "NSScreen", "NSTextField", "NSTextInputContext", "NSView", "NSWindow"] } # Files opened from Finder and the Dock (src/open_documents.rs), pasted bitmaps (src/clipboard.rs) and the control channel's App Nap activity (src/control_server.rs); both already come with winit, muda and rfd. objc2-foundation = { version = "0.3", default-features = false, features = ["std", "block2", "NSArray", "NSData", "NSGeometry", "NSNotification", "NSOperation", "NSProcessInfo", "NSString", "NSURL"] } block2 = "0.6" diff --git a/apps/vectorcraft/src/window.rs b/apps/vectorcraft/src/window.rs index ea10fecad0..ce224584fd 100644 --- a/apps/vectorcraft/src/window.rs +++ b/apps/vectorcraft/src/window.rs @@ -11,26 +11,51 @@ use winit::dpi::{LogicalSize, PhysicalPosition, PhysicalSize}; pub const DEFAULT_SIZE: [f32; 2] = [1440.0, 900.0]; /// The smallest window, in points. pub const MIN_SIZE: [f32; 2] = [800.0, 500.0]; -/// Room the first window leaves around itself for a taskbar, Dock or menu bar (the system doesn't -/// say how much of the screen they take): a monitor with less room opens it maximized. +/// Room the first window leaves around itself for a taskbar, Dock or menu bar when it decides +/// whether to open maximized: a monitor with less room opens it maximized. This check uses the +/// fixed allowance on every platform, including where the system reports a work area. const SCREEN_ROOM: [f32; 2] = [64.0, 128.0]; /// Seconds between readings of the monitor list, which picks up a monitor connected while the app runs. const MONITORS_EVERY: f64 = 1.0; -/// A monitor: its top-left corner and size in physical pixels, and its scale factor. +/// A monitor: its top-left corner and size in physical pixels, its scale factor, and the part +/// windows may use where the system reports one. #[derive(Clone, Copy, Debug, PartialEq)] pub struct Monitor { pos: [i64; 2], size: [i64; 2], scale: f32, + /// `[x, y, width, height]` in physical pixels, without the taskbar, Dock or menu bar. + work: Option<[i64; 4]>, +} + +/// A monitor's rect and its work area as the system lists them, `[x, y, width, height]` in +/// physical pixels. Only Windows and macOS list them. +#[derive(Clone, Copy, Debug, PartialEq)] +#[cfg_attr(not(any(all(windows, feature = "wgpu"), target_os = "macos")), allow(dead_code))] +pub struct WorkArea { + monitor: [i64; 4], + work: [i64; 4], } impl Monitor { pub fn new(pos: PhysicalPosition, size: PhysicalSize, scale: f64) -> Self { let scale = scale as f32; let scale = if scale.is_finite() && scale > 0.0 { scale } else { 1.0 }; - Self { pos: [pos.x.into(), pos.y.into()], size: [size.width.into(), size.height.into()], scale } + Self { pos: [pos.x.into(), pos.y.into()], size: [size.width.into(), size.height.into()], scale, work: None } + } + + /// This monitor with the work area of the entry in `areas` whose monitor rect matches its own, + /// to the pixel either way to allow for rounding. Without a match, or when the matching work + /// area has no width or height, it has none. + fn with_work_area(self, areas: &[WorkArea]) -> Self { + let [x, y] = self.pos; + let [w, h] = self.size; + let rect = [x, y, w, h]; + let work = + areas.iter().find(|a| a.monitor.iter().zip(rect).all(|(a, b)| a.abs_diff(b) <= 1)).map(|a| a.work).filter(|&[_, _, w, h]| w > 0 && h > 0); + Self { work, ..self } } /// Its size in points. @@ -38,6 +63,19 @@ impl Monitor { self.size.map(|s| s as f32 / self.scale) } + /// This monitor with its work area made smaller by a window frame of `frame` physical pixels + /// (outer size less inner size), so a window whose inner size fits the result fits the work area + /// with its frame. + fn less_frame(self, frame: [i64; 2]) -> Self { + let work = self.work.map(|[x, y, w, h]| [x, y, (w - frame[0].max(0)).max(1), (h - frame[1].max(0)).max(1)]); + Self { work, ..self } + } + + /// The size of its work area in points, or of the whole monitor when it has none. + fn work_points(&self) -> [f32; 2] { + self.work.map_or_else(|| self.points(), |[_, _, w, h]| [w, h].map(|s| s as f32 / self.scale)) + } + /// Its size in points less room for a taskbar, Dock or menu bar. fn usable(&self) -> [f32; 2] { let points = self.points(); @@ -61,24 +99,31 @@ impl Monitor { } /// The top-left corner nearest to `pos` (centred when none) that keeps a window of `size` - /// points on this monitor. + /// points on this monitor, inside its work area when it has one. A window bigger than the area + /// gets the area's top-left corner. fn keep_on(&self, pos: Option<[i32; 2]>, size: [f32; 2]) -> [i32; 2] { let px = self.pixels(size); + let [x, y] = self.pos; + let [w, h] = self.size; + let [ax, ay, aw, ah] = self.work.unwrap_or([x, y, w, h]); + let (start, extent) = ([ax, ay], [aw, ah]); std::array::from_fn(|i| { - let lo = self.pos[i]; - let hi = lo + self.size[i].saturating_sub(px[i]).max(0); + let lo = start[i]; + let hi = lo + extent[i].saturating_sub(px[i]).max(0); let want = pos.map_or(lo + (hi - lo) / 2, |p| i64::from(p[i])); i32::try_from(want.max(lo).min(hi)).unwrap_or_default() }) } /// This monitor for floating panels, in the points egui reports for a window on it: physical - /// pixels divided by the monitor's scale factor and the UI zoom. + /// pixels divided by the monitor's scale factor and the UI zoom. The work area is converted the + /// same way. fn docking(&self, name: Option, zoom: f32) -> DockMonitor { let scale = self.scale * zoom; let [x, y] = self.pos; let [w, h] = self.size; - DockMonitor { name, rect: [x, y, w, h].map(|v| v as f32 / scale), scale, work_area: None } + let points = |rect: [i64; 4]| rect.map(|v| v as f32 / scale); + DockMonitor { name, rect: points([x, y, w, h]), scale, work_area: self.work.map(points) } } } @@ -108,7 +153,10 @@ pub fn place(saved: Option, monitors: &[Monitor], home: Option room[i]); - let size = fit(saved.size, if too_big { m.usable() } else { room }); + // No bigger than the work area either, so the window fits below or beside the taskbar. + let work = m.work_points(); + let room = if too_big { m.usable() } else { room }; + let size = fit(saved.size, std::array::from_fn(|i| room[i].min(work[i]))); WindowGeometry { // Moved fully onto its monitor, or centred on the home monitor. pos: saved.pos.map(|p| m.keep_on(shown.map(|_| p), size)), @@ -128,12 +176,94 @@ pub fn monitors_due(listed: Option<(f64, f32)>, now: f64, zoom: f32) -> bool { listed.is_none_or(|(at, listed_zoom)| listed_zoom != zoom || now - at >= MONITORS_EVERY) } -/// The connected monitors for floating panels (`Services::dock_monitors`). +/// The connected monitors for floating panels (`Services::dock_monitors`), with the work area the +/// system reports for each (Windows and macOS). pub fn dock_monitors(window: &winit::window::Window, zoom: f32) -> Vec { - let monitors: Vec<_> = window.available_monitors().map(|m| (Monitor::new(m.position(), m.size(), m.scale_factor()), m.name())).collect(); + let areas = work_areas::list(); + let monitors: Vec<_> = + window.available_monitors().map(|m| (Monitor::new(m.position(), m.size(), m.scale_factor()).with_work_area(&areas), m.name())).collect(); docking_list(&monitors, zoom, cfg!(target_os = "macos")) } +/// A rect in macOS screen coordinates (points, origin at the bottom-left of the primary screen, +/// y up) in the physical pixels winit gives a monitor's position and size: points from the top-left +/// of the primary screen, `primary_height` points tall, times `scale`. +#[cfg(any(target_os = "macos", test))] +fn from_bottom_left([x, y, w, h]: [f64; 4], primary_height: f64, scale: f64) -> Option<[i64; 4]> { + let rect = [x, primary_height - (y + h), w, h].map(|v| (v * scale).round()); + // `as` saturates; a coordinate of 1e12 pixels or more is not a monitor's. + rect.iter().all(|v| v.is_finite() && v.abs() < 1e12).then(|| rect.map(|v| v as i64)) +} + +/// Each monitor's rect and work area as the system reports them. +mod work_areas { + pub use platform::list; + + /// `GetMonitorInfo` for every monitor: `rcMonitor` and `rcWork`, in the physical pixels winit + /// reports for a per-monitor DPI aware process. + #[cfg(all(windows, feature = "wgpu"))] + mod platform { + use super::super::WorkArea; + use winsafe::prelude::Handle; + use winsafe::{HDC, RECT}; + + pub fn list() -> Vec { + let rect = |r: RECT| { + let [left, top, right, bottom] = [r.left, r.top, r.right, r.bottom].map(i64::from); + [left, top, right - left, bottom - top] + }; + let mut areas = Vec::new(); + // Without a list the panels keep to the monitors' whole rects, as before. + let _ = HDC::NULL.EnumDisplayMonitors(None, |monitor, _, _| { + if let Ok(info) = monitor.GetMonitorInfo() { + areas.push(WorkArea { monitor: rect(info.rcMonitor), work: rect(info.rcWork) }); + } + true + }); + areas + } + } + + /// `NSScreen`'s `frame` and `visibleFrame` for every screen, turned into the top-left origin + /// and physical pixels winit uses for its monitors. + #[cfg(target_os = "macos")] + mod platform { + use super::super::{WorkArea, from_bottom_left}; + use objc2_app_kit::NSScreen; + use objc2_foundation::{MainThreadMarker, NSRect}; + + pub fn list() -> Vec { + // `NSScreen::screens` needs the main thread, where the UI frame runs. + let Some(mtm) = MainThreadMarker::new() else { + return Vec::new(); + }; + let screens: Vec<_> = NSScreen::screens(mtm).iter().collect(); + // The first screen holds the menu bar and is the origin of the screen coordinates. + let Some(primary) = screens.first().map(|s| s.frame().size.height) else { + return Vec::new(); + }; + let rect = |r: NSRect| [r.origin.x, r.origin.y, r.size.width, r.size.height]; + screens + .iter() + .filter_map(|s| { + let scale = s.backingScaleFactor(); + let monitor = from_bottom_left(rect(s.frame()), primary, scale)?; + let work = from_bottom_left(rect(s.visibleFrame()), primary, scale)?; + Some(WorkArea { monitor, work }) + }) + .collect() + } + } + + /// No work areas: the panels keep to the monitors' whole rects. + #[cfg(not(any(all(windows, feature = "wgpu"), target_os = "macos")))] + mod platform { + pub fn list() -> Vec { + Vec::new() + } + } +} + /// The list for floating panels, empty when `monitors` have different scales and `shared_points` is /// false. macOS has one desktop of points. Elsewhere a new window multiplies its saved position by the /// scale of the monitor it is created on, which can put a window saved on another scale off every @@ -154,7 +284,19 @@ fn docking_list(monitors: &[(Monitor, Option)], zoom: f32, shared_points /// until the host reports maximized (fixes #3635). pub fn restore(window: &winit::window::Window, saved: Option) -> WindowGeometry { let handles: Vec<_> = window.available_monitors().collect(); - let monitors: Vec = handles.iter().map(|m| Monitor::new(m.position(), m.size(), m.scale_factor())).collect(); + let areas = work_areas::list(); + // The system title bar and border, which the inner size leaves out, in points: each monitor + // draws them at its own scale. + let (outer, inner, scale) = (window.outer_size(), window.inner_size(), window.scale_factor()); + let scale = if scale.is_finite() && scale > 0.0 { scale } else { 1.0 }; + let frame = [outer.width.saturating_sub(inner.width), outer.height.saturating_sub(inner.height)].map(|px| f64::from(px) / scale); + let monitors: Vec = handles + .iter() + .map(|m| { + let px = frame.map(|f| (f * m.scale_factor()).ceil().min(1e6) as i64); + Monitor::new(m.position(), m.size(), m.scale_factor()).with_work_area(&areas).less_frame(px) + }) + .collect(); let home = window.current_monitor().or_else(|| window.primary_monitor()).and_then(|c| handles.iter().position(|m| *m == c)); let g = place(saved, &monitors, home); if let Some([x, y]) = g.pos { @@ -257,6 +399,64 @@ mod tests { assert_eq!(zoomed, DockMonitor { name: Some("m".into()), rect: [-2048.0, 0.0, 2048.0, 1152.0], scale: 1.25, work_area: None }); } + #[test] + fn floating_panels_get_each_monitors_work_area_in_the_same_points() { + // A 4K monitor at 200% with a 96-pixel taskbar along its bottom, and one at 100% to its + // right with a 48-pixel taskbar along its left edge. + let areas = [ + WorkArea { monitor: [0, 0, 3840, 2160], work: [0, 0, 3840, 2064] }, + WorkArea { monitor: [3840, 0, 1920, 1080], work: [3888, 0, 1872, 1080] }, + ]; + let left = monitor(0, 0, 3840, 2160, 2.0).with_work_area(&areas); + assert_eq!(left.docking(None, 1.0).work_area, Some([0.0, 0.0, 1920.0, 1032.0])); + // The UI zoom divides the work area as it divides the rect. + assert_eq!(left.docking(None, 1.25).work_area, Some([0.0, 0.0, 1536.0, 825.6])); + // Rounding of a fractional scale can move a rect by a pixel. + let right = monitor(3841, 0, 1919, 1080, 1.0).with_work_area(&areas); + assert_eq!(right.docking(None, 1.0).work_area, Some([3888.0, 0.0, 1872.0, 1080.0])); + // A monitor the system did not list keeps to its whole rect. + let other = monitor(5760, 0, 1920, 1080, 1.0).with_work_area(&areas); + assert_eq!(other.docking(None, 1.0).work_area, None); + assert_eq!(monitor(0, 0, 3840, 2160, 2.0).with_work_area(&[]).docking(None, 1.0).work_area, None); + // A work area with no height is left out, so the monitor keeps to its whole rect. + let flat = [WorkArea { monitor: [0, 0, 1920, 1080], work: [0, 1080, 1920, 0] }]; + assert_eq!(monitor(0, 0, 1920, 1080, 1.0).with_work_area(&flat).docking(None, 1.0).work_area, None); + } + + #[test] + fn a_restored_window_keeps_its_title_bar_out_of_a_taskbar_along_the_top() { + // A 1920 × 1080 monitor with a 48-pixel taskbar along its top edge. + let areas = [WorkArea { monitor: [0, 0, 1920, 1080], work: [0, 48, 1920, 1032] }]; + let m = [monitor(0, 0, 1920, 1080, 1.0).with_work_area(&areas)]; + // A window saved at the top edge moves down below the taskbar. + let g = place(saved(Some([100, 0]), [1000.0, 700.0], false), &m, Some(0)); + assert_eq!(g.pos, Some([100, 48])); + // A window that fits below the taskbar keeps its place. + assert_eq!(place(saved(Some([100, 200]), [1000.0, 700.0], false), &m, Some(0)).pos, Some([100, 200])); + // A window taller than the work area is made as tall as the work area and placed in it. + let tall = place(saved(Some([0, 0]), [1900.0, 1060.0], false), &m, Some(0)); + assert_eq!((tall.pos, tall.size), (Some([0, 48]), [1900.0, 1032.0])); + // With a system frame of 16 × 39 pixels the inner size leaves room for it. + let framed = [m[0].less_frame([16, 39])]; + let tall = place(saved(Some([0, 0]), [1900.0, 1060.0], false), &framed, Some(0)); + assert_eq!((tall.pos, tall.size), (Some([0, 48]), [1900.0, 993.0])); + // Without a work area the frame leaves the monitor's rect as it was. + assert_eq!(monitor(0, 0, 1920, 1080, 1.0).less_frame([16, 39]).work, None); + } + + #[test] + fn macos_screen_rects_turn_into_winits_top_left_pixels() { + // A 1512 × 982 point primary screen at 2× with a 37-point menu bar and an 80-point Dock, + // and a 1920 × 1080 screen at 1× above it, its bottom-left 200 points right of the + // primary's top-left. + let primary = 982.0; + assert_eq!(from_bottom_left([0.0, 0.0, 1512.0, 982.0], primary, 2.0), Some([0, 0, 3024, 1964])); + assert_eq!(from_bottom_left([0.0, 80.0, 1512.0, 865.0], primary, 2.0), Some([0, 74, 3024, 1730])); + assert_eq!(from_bottom_left([200.0, 982.0, 1920.0, 1080.0], primary, 1.0), Some([200, -1080, 1920, 1080])); + assert_eq!(from_bottom_left([f64::NAN, 0.0, 1.0, 1.0], primary, 1.0), None); + assert_eq!(from_bottom_left([0.0, 0.0, 1e300, 1.0], primary, 1.0), None); + } + #[test] fn monitors_of_different_scales_are_listed_only_on_a_desktop_of_shared_points() { let named = |m: Monitor, name: &str| (m, Some(name.to_string())); diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index 0616b700f0..125b9f2019 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1107,6 +1107,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur - A floating panel's OS window hides while a tool group's flyout or the All Tools drawer is open in the main window, as it does under a modal dialog or a menu, and shows again when the flyout or the drawer closes (#5813; `a_tool_flyout_hides_the_palette_windows_until_it_closes`). - Focus moving between the app's own windows passes through a frame where none has it (a press on a panel's bar takes it from the main window): the panels' OS windows keep their level then, and leave the top level only once the app has had no focus for 0.25 s. Before, that frame lowered them, so with two floating panels a press on one panel's bar handed the keyboard to the other (the main window's tool cursor did not come back) and could leave both behind the main window (#6986; `focus_moving_between_the_apps_windows_leaves_the_palettes_on_top`). - On the desktop a floating panel in an OS window stores the monitor it is on and reopens there at the next launch. When that monitor is no longer connected, the panel opens on the main window's monitor at the same offset (#5582). Outside macOS, a desktop whose monitors have different scale factors keeps opening floating panels on the main window's monitor. Test: `panel_docking::native_tests::a_floating_panel_reopens_on_the_monitor_where_it_was_left`. + - The monitors given to floating panels carry each monitor's work area (the part without the taskbar, Dock or menu bar) on macOS, and on Windows when the monitors share one scale factor (a build without the `wgpu` feature reads none). A floating panel that reopens without enough of its title bar inside any work area to grab moves into the work area of the nearest monitor. A restored main window is kept inside its monitor's work area the same way. Tests: `floating_panels_get_each_monitors_work_area_in_the_same_points`, `macos_screen_rects_turn_into_winits_top_left_pixels` and `a_restored_window_keeps_its_title_bar_out_of_a_taskbar_along_the_top`. - A menu in a floating panel's OS window stops at that window's bottom edge and scrolls past it, and is never taller than the window (#5347; `a_long_panel_menu_scrolls_inside_a_short_native_window`). A panel's long submenus (Digits, Stylistic Sets, Save Selection, Move to Group), the libraries menu, the Color Guide's Limit to Library menu and the Layers panel's Filter popup scroll the same way. - The dock's double arrow collapses Properties, Layers and Libraries to icons at the top of the icon column, popping out like the other icon panels, and expands them again (`window.collapseDock`, kept with the preferences and in saved workspaces). The icon column's own arrow (expanding the icon panels into a full column) is not done. - Window › Toolbars › New Toolbar… and New Toolbar… in the All Tools drawer's menu ask for a name and make a blank toolbar that floats on its own, moved by its title bar and listed under Window › Toolbars, whose item shows or hides it. The title bar's × hides it, and its double arrow shows its tools in one column or two. Its ••• button opens the All Tools drawer beside it, and tools dragged from the drawer fill it. The toolbars are kept with the workspace they were made in: switching workspaces shows that workspace's toolbars, Reset Workspace keeps them, and New Workspace starts the new workspace with a copy of them (; `window.toolbar.new`, `window.toolbar.custom`, the `toolbar` param of `window.editToolbar` and `toolbar.addTool`/`removeTool`/`reset`; `tools_added_to_a_new_toolbar_survive_a_workspace_reload`).