From c10470b3f0f8fd37a4022295fc8b0e07df267113 Mon Sep 17 00:00:00 2001 From: aiongg Date: Sat, 21 Nov 2026 03:43:11 +0000 Subject: [PATCH] An OS dock window's chrome is sized at its own display's scale The rule under a floating window's title row takes the divider's width in whole physical pixels on the display the window lays out on. An OS window on another monitor (a laptop at 2x with an external display at 1x) laid its rule out at its own scale while the dock sized it at the main window's, so the window was half a point short and cut its last row. DockSizes keeps the rule's width and the display scale it is for, and at_scale gives the sizes at an OS window's own scale (native::pixels_per_point: its monitor's scale from the window facts with the app's zoom). DockArea::windows sizes each OS window's chrome at that scale and hands it to FloatingGroup; in-app windows keep the main window's. A window's caller place now counts as a change when it moves the window by more than half a physical pixel on its display, not half a point, so a window that changes displays takes a one-pixel correction at 2x. --- .../src/runtime/windows/viewport.rs | 24 +++++++++++------- crates/ui-panels/src/docking/floating.rs | 13 ++++++++++ crates/ui-panels/src/docking/native.rs | 6 +++++ crates/ui-panels/src/docking/targets.rs | 23 ++++++++++++----- crates/ui-patterns/src/patterns/dock/area.rs | 16 ++++++++++-- crates/ui-patterns/tests/it/docking/native.rs | 25 ++++++++++++++++++- 6 files changed, 89 insertions(+), 18 deletions(-) diff --git a/crates/ui-contract/src/runtime/windows/viewport.rs b/crates/ui-contract/src/runtime/windows/viewport.rs index cc337a081..b621430c3 100644 --- a/crates/ui-contract/src/runtime/windows/viewport.rs +++ b/crates/ui-contract/src/runtime/windows/viewport.rs @@ -172,11 +172,10 @@ pub(crate) fn show_popup(ctx: &Context, open: &PopupOpen<'_>, body: impl FnMut(& pub(crate) fn place_popup(ctx: &Context, id: Id, rect: Rect) { let viewport = viewports::viewport_id(id); let known = PerViewport::::with(ctx, viewport, |record| record.known); - let differs = |delta: Vec2| delta.x.abs() > ROUNDING || delta.y.abs() > ROUNDING; - if known.is_none_or(|known| differs(rect.min - known.min)) { + if known.is_none_or(|known| differs(ctx, viewport, rect.min - known.min)) { act(ctx, viewport, WindowAction::SetOuterPosition(rect.min)); } - if known.is_none_or(|known| differs(rect.size() - known.size())) { + if known.is_none_or(|known| differs(ctx, viewport, rect.size() - known.size())) { act(ctx, viewport, WindowAction::SetInnerSize(rect.size())); } PerViewport::::with(ctx, viewport, |record| record.known = Some(rect)); @@ -226,9 +225,17 @@ pub(crate) fn unplaced(ctx: &Context, id: Id) -> bool { && PerViewport::::with(ctx, viewports::viewport_id(id), |record| record.kind.is_some() && record.seen.is_none()) } -/// How far a caller's place may differ from the known place before it is a change: the OS rounds to pixels, and a -/// placement records the position in pixels. -const ROUNDING: f32 = 0.5; +/// How far a caller's place may differ from the known place before it is a change, in physical pixels of the window's +/// display: the OS rounds to pixels, and a placement records the position in pixels. +const ROUNDING_PX: f32 = 0.5; + +/// Whether `delta`, in points, moves the window `viewport` by more than rounding on its display: a whole pixel on +/// a 2x display is half a point. +fn differs(ctx: &Context, viewport: ViewportId, delta: Vec2) -> bool { + let facts = PerViewport::::with(ctx, viewport, |record| record.facts); + let scale = facts.and_then(|facts| super::host::scale_of(ctx, &facts)).unwrap_or(1.0); + delta.x.abs() * scale > ROUNDING_PX || delta.y.abs() * scale > ROUNDING_PX +} /// The place of a window with `facts`: its outer top-left corner and its inner size. fn place_of(facts: &viewports::Facts) -> Option { @@ -257,11 +264,10 @@ fn keep_placed(ctx: &Context, viewport: ViewportId, pos: Option, size: Opt (record.known, state || record.moved_pointer.is_some()) }); let Some(known) = known.filter(|_| !held) else { return }; - let differs = |delta: Vec2| delta.x.abs() > ROUNDING || delta.y.abs() > ROUNDING; - if let Some(pos) = pos.filter(|pos| differs(*pos - known.min)) { + if let Some(pos) = pos.filter(|pos| differs(ctx, viewport, *pos - known.min)) { act(ctx, viewport, WindowAction::SetOuterPosition(pos)); } - if let Some(size) = size.filter(|size| differs(*size - known.size())) { + if let Some(size) = size.filter(|size| differs(ctx, viewport, *size - known.size())) { act(ctx, viewport, WindowAction::SetInnerSize(size)); } } diff --git a/crates/ui-panels/src/docking/floating.rs b/crates/ui-panels/src/docking/floating.rs index 9bc6af0d6..fc6369b3b 100644 --- a/crates/ui-panels/src/docking/floating.rs +++ b/crates/ui-panels/src/docking/floating.rs @@ -277,6 +277,9 @@ pub struct FloatingGroup<'a> { placed: Option, /// The width a resize gave the collapsed column this pass. collapsed_width: Option, + /// The display scale of the OS window the group shows in, which sizes the rule under its title row; unset, the + /// running window's. + scale: Option, /// Starts the window-unit drag; set by `window_of`. start_drag: Option>, /// Takes the drops released on the group, on the group's own layer. @@ -323,6 +326,7 @@ impl<'a> FloatingGroup<'a> { rtl: false, placed: None, collapsed_width: None, + scale: None, start_drag: None, take_drop: None, moving: false, @@ -359,6 +363,14 @@ impl<'a> FloatingGroup<'a> { self } + /// The display scale of the OS window the group shows in (`native::pixels_per_point`), when it differs from the + /// running window's: its chrome is sized at that scale. + #[must_use] + pub fn scale(mut self, pixels_per_point: f32) -> Self { + self.scale = Some(pixels_per_point); + self + } + /// The action parts at the end of the title row, in reading order. #[must_use] pub fn actions(mut self, actions: impl IntoIterator) -> Self { @@ -372,6 +384,7 @@ impl<'a> FloatingGroup<'a> { // A window in the app window keeps the band over its title row that a layer the user moves keeps for its top // grip; an OS window's grips lie over its title row. The rule under the title row takes a band of its own. let sizes = DockSizes::style(&StyleCx::of_env(ui.ctx(), &env)); + let sizes = self.scale.map_or(sizes, |scale| sizes.at_scale(scale)); (self.header, self.chrome, self.rtl) = (sizes.title_row, sizes.window_chrome(self.native.is_none()), env.direction.is_rtl()); let mount = self.mount(); self.props = self.props.mount(mount); diff --git a/crates/ui-panels/src/docking/native.rs b/crates/ui-panels/src/docking/native.rs index 568e3d011..4ef261889 100644 --- a/crates/ui-panels/src/docking/native.rs +++ b/crates/ui-panels/src/docking/native.rs @@ -46,6 +46,12 @@ pub fn opens_os_windows(ctx: &Context, config: &DockConfig) -> bool { config.native_windows && host.opens_windows() && host.positions_reported() } +/// The display scale the OS window `viewport` lays out at: its monitor's scale with the app's zoom, as the window +/// reported it (`WindowHost::facts`). `None` before it reported one. +pub fn pixels_per_point(ctx: &Context, viewport: ViewportId) -> Option { + WindowHost::of(ctx).facts(viewport)?.scale.map(|scale| scale * ctx.zoom_factor()) +} + /// The window a group of panels with `specs` opens: a document window when any panel is a document, else a tool /// window (a palette level, no taskbar entry). pub fn spec_for(specs: &[PanelSpec]) -> WindowSpec { diff --git a/crates/ui-panels/src/docking/targets.rs b/crates/ui-panels/src/docking/targets.rs index c396eb325..634716f87 100644 --- a/crates/ui-panels/src/docking/targets.rs +++ b/crates/ui-panels/src/docking/targets.rs @@ -7,7 +7,7 @@ use super::{DragUnit, PanelKey, PanelSpec, scale_widths, stepped, width_bounds}; use crate::contract::DropMark; use crate::runtime::frame::{self, PassStep, PerViewport, Stamped, Stored}; -use crate::style::{RuleKind, StyleCx}; +use crate::style::{RuleKind, RuleWidth, StyleCx}; use craft_behavior::DockSide; use craft_behavior::dock::{Action, Column, DockEdge, Layout, Node, ZoneSize, node_at, zones}; use egui::{Context, Id, Pos2, Rect, Vec2}; @@ -53,9 +53,12 @@ pub struct DockSizes { pub bar: f32, /// The height of a floating window's title row, which is the bar of the column the window holds. pub title_row: f32, - /// The band of the divider rule a floating window draws under its title row, in points on the running display: - /// the rule's width in whole physical pixels, as the container lays it out in a band of its own. - pub title_rule: f32, + /// The divider rule a floating window draws under its title row, which the container lays out in a band of its + /// own: the rule's width in whole physical pixels on the window's display ([`Self::pixels_per_point`]). + pub title_rule: RuleWidth, + /// The display scale the sizes that depend on it are for: the running window's, or with [`Self::at_scale`] an OS + /// window's own. + pub pixels_per_point: f32, /// The band a floating window in the app window keeps clear over its title row for its top grip /// (`contract::grip_band`); an OS window keeps none. Every height of a window's body or content is the window's /// height less its chrome ([`Self::window_chrome`]). @@ -99,7 +102,8 @@ impl DockSizes { strip: size.panel_header, bar: size.control.sm, title_row: super::floating::title_row(cx.tokens), - title_rule: cx.rule(RuleKind::Divider).width.points(cx.frame_ctx().pixels_per_point()), + title_rule: cx.rule(RuleKind::Divider).width, + pixels_per_point: cx.frame_ctx().pixels_per_point(), top_band: crate::contract::grip_band(cx.tokens), least: size.panel_header, reach: size.hit.target, @@ -113,7 +117,14 @@ impl DockSizes { /// The height above a floating window's body: its title row, the rule under it and, for a window in the app /// window (`in_app`), the band over it. pub fn window_chrome(&self, in_app: bool) -> f32 { - self.title_row + self.title_rule + if in_app { self.top_band } else { 0.0 } + self.title_row + self.title_rule.points(self.pixels_per_point) + if in_app { self.top_band } else { 0.0 } + } + + /// The sizes for a window on a display of `pixels_per_point`: an OS window lays out at its own monitor's scale. + #[must_use] + pub fn at_scale(mut self, pixels_per_point: f32) -> Self { + self.pixels_per_point = pixels_per_point; + self } /// The height of a column whose every group is minimized: their tab strips, as the column stacks them. `None` diff --git a/crates/ui-patterns/src/patterns/dock/area.rs b/crates/ui-patterns/src/patterns/dock/area.rs index 4ace220ce..f5f83891d 100644 --- a/crates/ui-patterns/src/patterns/dock/area.rs +++ b/crates/ui-patterns/src/patterns/dock/area.rs @@ -1222,6 +1222,14 @@ impl Pass<'_, P> { let dock = scene.dock.with(("floating", window.id)); let group_id = dock.with("window"); let opening = native::opening(&ctx, scene.config, window, &specs, group_id); + // An OS window lays out at its own monitor's scale, which sizes its chrome; one in the app window at the + // main window's. + let scale = opening + .spec + .as_ref() + .and_then(|_| WindowHost::of(&ctx).viewport_of(group_id)) + .and_then(|viewport| native::pixels_per_point(&ctx, viewport)); + let window_sizes = scale.map_or(scene.sizes, |scale| scene.sizes.at_scale(scale)); let movable = rights.iter().all(|rights| rights.movable); // The title row is the column's bar: its double arrow and Close; the menu opens from a context request on // its band. @@ -1270,6 +1278,9 @@ impl Pass<'_, P> { .takes_drops(target, &dropped) .command_scope(DockCommands::SCOPE) .id(group_id); + if let Some(scale) = scale { + group = group.scale(scale); + } group = match opening.spec.clone() { Some(window_spec) => group.native(window_spec), None => group.bounds(scene.bounds), @@ -1303,8 +1314,9 @@ impl Pass<'_, P> { cross_window::accept_cross_window(&ctx); self.report.actions.push(dropped.action); } - // The height above the window's body: the title row, and in the app window the band over it. - let chrome = scene.sizes.window_chrome(opening.spec.is_none()); + // The height above the window's body at its scale: the title row, the rule under it, and in the app window + // the band over it. + let chrome = window_sizes.window_chrome(opening.spec.is_none()); // A resize of a window whose column has steps: the steps it sets, and the window's rect at them. let resized = output .placed diff --git a/crates/ui-patterns/tests/it/docking/native.rs b/crates/ui-patterns/tests/it/docking/native.rs index c81605020..6d71e005f 100644 --- a/crates/ui-patterns/tests/it/docking/native.rs +++ b/crates/ui-patterns/tests/it/docking/native.rs @@ -834,9 +834,12 @@ const COLLAPSED_SCALE: f32 = 1.25; /// A native palette collapsed to icons on Windows at 125 %, and the rect its OS window shows the strip at. fn collapsed_palette() -> (NativeDock, Rect) { let mut dock = NativeDock::on(Os::Windows, Arc::new(FakeSystem::new())); + // Both windows report the display at 125 % before the column collapses. dock.windows.root_info(scaled(true, MAIN_AT, COLLAPSED_SCALE)); - dock.workspace.apply(Action::set_iconic(1, true), false, |_| Permissions::ALL).expect("the column collapses"); let mut at = FLOAT_AT; + dock.windows.child_info(scaled(true, at, COLLAPSED_SCALE)); + collapsed_frame(&mut dock, &mut BodyProbe::default(), &mut at, vec![], vec![]); + dock.workspace.apply(Action::set_iconic(1, true), false, |_| Permissions::ALL).expect("the column collapses"); for _ in 0..4 { collapsed_frame(&mut dock, &mut BodyProbe::default(), &mut at, vec![], vec![]); } @@ -993,6 +996,26 @@ fn a_collapsed_native_palette_paints_its_icons_and_no_clipped_text() { } } +/// A native palette on a monitor at 1x while the main window is on one at 2x (a laptop with an external display): +/// collapsed to icons, its window fits the title row, the rule under it and the icons exactly at its own scale. +#[trial] +fn a_collapsed_native_palette_on_a_1x_monitor_beside_a_2x_main_window_fits_its_icons_exactly() { + let mut dock = NativeDock::on(Os::Mac, Arc::new(FakeSystem::new())); + dock.windows.root_info(scaled(true, MAIN_AT, 2.0)); + let mut at = FLOAT_AT; + dock.windows.child_info(scaled(true, at, 1.0)); + dock.workspace.apply(Action::set_iconic(1, true), false, |_| Permissions::ALL).expect("the column collapses"); + for _ in 0..4 { + let commands = dock.frame(vec![]).1; + at = placed_by(&commands, at); + dock.windows.child_info(scaled(true, at, 1.0)); + } + let tree = dock.palette_tree.as_ref().expect("the palette ran a pass"); + if let Err(fit) = fits_exactly(tree, at.size(), Role::BUTTON) { + panic!("the strip's window fits its icons at its own scale: {fit}"); + } +} + /// The tooltip of a collapsed native palette's double arrow opens in a popup OS window below the arrow, in desktop /// points, on one line: the window takes no focus and lets the pointer through to the palette. #[trial]