From b037b2a6ba1beedfb753d823158b868e9d9bb49f Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 28 Nov 2026 11:08:51 -0800 Subject: [PATCH] canvas chrome: corner widgets sit a fixed inset, the compass clears the scroll bars, the ruler corner shows a plus - Corner widgets sit 10 points inside their corner at every density (CornerWidgets::INSET) instead of space.gap.stack, which changed with the density. A host that hit-tests the widgets reads the constant. - CanvasScrollbars::clear_of returns a canvas rect without the scroll bars' strips, from the sizes the bars draw with, so a canvas passes it to RotateView::compass_rect and the vertical bar does not cover the compass. - The ruler corner shows a "+" that marks the draggable zero point. - ZoomField::status_bar is public, so an app's own status-bar zoom field can use the status bar's look. --- crates/draw/src/ui/corner/style.rs | 4 +-- crates/draw/src/ui/corner/widgets.rs | 4 +++ crates/draw/tests/it/corner.rs | 27 +++++++++++++++ crates/ui-canvas/src/canvas/rotate.rs | 4 ++- crates/ui-canvas/src/canvas/rulers/mod.rs | 20 ++++++++--- crates/ui-canvas/src/canvas/scrollbars/mod.rs | 30 +++++++++++++--- crates/ui-canvas/tests/it/canvas/rulers.rs | 33 ++++++++++++++++++ .../ui-canvas/tests/it/canvas/scrollbars.rs | 34 ++++++++++++++++++- crates/ui-widgets/src/text/zoom_field.rs | 5 +-- crates/ui-widgets/tests/it/text/zoom_field.rs | 26 ++++++++++++++ 10 files changed, 172 insertions(+), 15 deletions(-) diff --git a/crates/draw/src/ui/corner/style.rs b/crates/draw/src/ui/corner/style.rs index 1517afb33d..1401ec4ed1 100644 --- a/crates/draw/src/ui/corner/style.rs +++ b/crates/draw/src/ui/corner/style.rs @@ -19,7 +19,7 @@ pub struct CornerStyle { pub(super) dot: f32, /// The pointer's reach. pub(super) reach: f32, - /// The least distance inside a corner. + /// The least distance inside a corner: [`CornerWidgets::INSET`](super::CornerWidgets::INSET). pub(super) inset: f32, /// The shortest side, on the screen, whose corners show widgets. pub(super) min_side: f32, @@ -38,7 +38,7 @@ impl CornerStyle { widget: tokens.size.handle.md, dot: tokens.size.handle.sm, reach: tokens.size.hit.target, - inset: tokens.space.gap.stack, + inset: super::CornerWidgets::INSET, min_side: MIN_SIDE, fill: cx.color(tokens.color.overlay.handle_fill), color: cx.color(tokens.color.overlay.line), diff --git a/crates/draw/src/ui/corner/widgets.rs b/crates/draw/src/ui/corner/widgets.rs index b2ebaff95f..d2a4ce6f6b 100644 --- a/crates/draw/src/ui/corner/widgets.rs +++ b/crates/draw/src/ui/corner/widgets.rs @@ -101,6 +101,10 @@ impl<'a> CornerWidgets<'a> { /// The widest inner angle, in degrees, a widget shows at unless [`hide_above`](Self::hide_above) says otherwise. pub const HIDE_ABOVE: f64 = 180.0; + + /// The least distance, in screen points along each side of a right angle, a widget sits inside its corner, at every + /// density. A host that hit-tests the widgets itself reads it from here. + pub const INSET: f32 = 10.0; } /// What the widgets report: the edit of the targets and what the widgets ask the host to do. diff --git a/crates/draw/tests/it/corner.rs b/crates/draw/tests/it/corner.rs index 966d6b0650..37654cf4f0 100644 --- a/crates/draw/tests/it/corner.rs +++ b/crates/draw/tests/it/corner.rs @@ -118,3 +118,30 @@ fn a_triangle_apex_rounds_with_the_pointer_travel() { assert!(small > START_RADIUS, "a short pull rounds the apex: {small}"); assert!(large > small, "a longer pull rounds it more: {small} then {large}"); } + +/// A slightly rounded right-angle corner's widget sits `CornerWidgets::INSET` points inside the corner along each side, +/// at every density, so a host that hit-tests the widgets with that constant presses where the widget is drawn. +#[trial] +fn a_widget_sits_the_published_inset_inside_its_corner_at_every_density() { + for density in [craft_theme::Size::Compact, craft_theme::Size::Comfortable] { + let vertex = Rc::new(Cell::new(egui::Pos2::ZERO)); + let seen = Rc::clone(&vertex); + let mut targets = vec![square()]; + let mut fixture = Fixture::new(move |ui| { + let room = ui.available_rect_before_wrap(); + let view = CanvasView::new(room, Point::new(210.0, 200.0), 1.0); + seen.set(view.to_screen(targets[0].corners[0].vertex)); + Canvas::new("Document") + .view(view) + .show(ui, |ui, body| CornerWidgets::new("Corner radius", Bind::bound(&mut targets)).show_at(ui, body.rect)); + }); + fixture.density(density).settle(); + let widget = fixture.by_label("Corner 1 of 4").expect("the first corner has a widget"); + let offset = widget.center() - vertex.get(); + let inset = CornerWidgets::INSET; + assert!( + (offset.x - inset).abs() < 0.5 && (offset.y - inset).abs() < 0.5, + "{density:?}: the widget sits {offset:?} from its corner, not {inset} along each side" + ); + } +} diff --git a/crates/ui-canvas/src/canvas/rotate.rs b/crates/ui-canvas/src/canvas/rotate.rs index f120729fe0..f3015bc351 100644 --- a/crates/ui-canvas/src/canvas/rotate.rs +++ b/crates/ui-canvas/src/canvas/rotate.rs @@ -89,7 +89,9 @@ impl<'a> RotateView<'a> { } /// The compass' rect in the top corner of `canvas` where the reading direction ends, a medium thumbnail side - /// across (`size.thumb.md`, as the old `compass_rect`), inset from the canvas edges by the stack gap. + /// across (`size.thumb.md`, as the old `compass_rect`), inset from the canvas edges by the stack gap. A canvas that + /// shows scroll bars passes [`CanvasScrollbars::clear_of`](super::CanvasScrollbars::clear_of) its rect, so the + /// vertical bar does not cover the compass. pub fn compass_rect(ui: &Ui, canvas: Rect) -> Rect { let env = Env::of(ui); let corner = if env.direction.is_rtl() { Align2::LEFT_TOP } else { Align2::RIGHT_TOP }; diff --git a/crates/ui-canvas/src/canvas/rulers/mod.rs b/crates/ui-canvas/src/canvas/rulers/mod.rs index c6d23ffd33..148d2b37ea 100644 --- a/crates/ui-canvas/src/canvas/rulers/mod.rs +++ b/crates/ui-canvas/src/canvas/rulers/mod.rs @@ -151,6 +151,8 @@ pub struct RulersStyle { cross: StyleColor, /// The halo under the cross hairs, so they read on the paper, the pasteboard and any art. halo: StyleColor, + /// The side of the "+" the corner shows to mark the draggable zero point. + origin_mark: f32, } /// The type role of the tick labels. @@ -197,8 +199,9 @@ enum MenuRow { /// - A drag on the top ruler reports a horizontal guide in [`RulersOutput::guide`] and to the canvas's body /// ([`ruler_guide`](super::ruler_guide)), one on the left ruler a vertical guide; Alt turns it, Shift snaps it to the ticks, and a release over a ruler or off the document reports no /// position, which removes the guide. -/// - A drag out of the corner moves the zero point: cross hairs show where it lands, Shift snaps to the ticks -/// (otherwise the canvas's [`Snap`]), dropped back on the corner it stays, Esc cancels. A double click +/// - The corner shows a "+" that marks the zero point. A drag out of the corner moves the zero point: cross hairs show +/// where it lands, Shift snaps to the ticks (otherwise the canvas's [`Snap`]), dropped back on the corner it stays, +/// Esc cancels. A double click /// on the corner and the menu's Reset Zero Point put the zero point back at the document's origin, one edit through the zero bind. The arrow keys move the focused corner. [`origin_locked`](Rulers::origin_locked) /// turns the drag and the reset off. /// - A secondary click on a strip or the corner, or the Menu key or Shift+F10 on the focused corner, opens the units @@ -388,6 +391,7 @@ impl Component for Rulers<'_> { marker: cx.color(c.text.normal), cross: cx.color(c.overlay.stroke), halo: cx.color(c.overlay.halo), + origin_mark: cx.tokens.size.handle.md, } } @@ -550,8 +554,16 @@ impl Component for Rulers<'_> { draw.fill(&rect(top), style.fill); draw.fill(&rect(left), style.fill); - let Some(space) = space else { + // The corner: its fill and the "+" that marks the zero point a drag from it moves. + let paint_corner = || { draw.fill(&rect(corner), style.fill); + let (c, arm) = (corner.center(), style.origin_mark / 2.0); + for (from, to) in [(c - Vec2::new(arm, 0.0), c + Vec2::new(arm, 0.0)), (c - Vec2::new(0.0, arm), c + Vec2::new(0.0, arm))] { + draw.stroke(&PaintShape::Path(vec![from, to]), StrokeWidth::Thin, style.marker); + } + }; + let Some(space) = space else { + paint_corner(); return; }; let face = style.face; @@ -678,7 +690,7 @@ impl Component for Rulers<'_> { } } - draw.fill(&rect(corner), style.fill); + paint_corner(); draw.edge_rule(RuleKind::Divider, top, RectSide::Bottom); draw.edge_rule(RuleKind::Divider, left, RectSide::Right); } diff --git a/crates/ui-canvas/src/canvas/scrollbars/mod.rs b/crates/ui-canvas/src/canvas/scrollbars/mod.rs index f4e30e5e3f..b53587ccd4 100644 --- a/crates/ui-canvas/src/canvas/scrollbars/mod.rs +++ b/crates/ui-canvas/src/canvas/scrollbars/mod.rs @@ -27,15 +27,15 @@ use crate::display::Text; use crate::primitives::{Draw, PaintShape, ScrollBar, StrokeWidth}; use crate::runtime::access::Numeric; use crate::runtime::commands::FireSource; -use crate::runtime::env::Label; +use crate::runtime::env::{Env, Label}; use crate::runtime::focus::KeyUse; use crate::runtime::frame; -use crate::style::{ComponentStyle, ScrollBarKind, ScrollBarLook, Stateful, StyleColor, StyleCx}; +use crate::style::{ComponentStyle, ScrollBarKind, ScrollBarLook, Stateful, StyleColor, StyleCx, UiTokens}; use craft_behavior::kurbo::{Point, Rect as DocRect}; use craft_behavior::overlay::{Align, Side}; use craft_behavior::scroll::ScrollSpan; use craft_behavior::{Axis, CanvasView, Metrics, PartKey, PartRef, PressPhase, PressStart, Sense}; -use egui::{CornerRadius, Pos2, Rect, Response, Vec2, pos2, vec2}; +use egui::{CornerRadius, Pos2, Rect, Response, Ui, Vec2, pos2, vec2}; use std::ops::{Deref, Range}; const HORIZONTAL: PartKey = PartKey::new("horizontal"); @@ -55,6 +55,13 @@ const KEPT_IN_VIEW: f64 = 100.0; /// The popup of the tip beside a dragged vertical thumb. const TIP: &str = "tip"; +/// A bar's thickness and the margin it keeps from the canvas's edges, from `tokens`: the docked bars keep a tight gap, +/// the document window's bars none. +fn bar_size(tokens: &UiTokens, document_chrome: bool) -> (f32, f32) { + let margin = if document_chrome { 0.0 } else { tokens.space.gap.tight }; + (tokens.size.icon.md, margin) +} + /// The thumb part that `PartsCx::scroll` declares for the bar `bar`. fn thumb_of(bar: PartKey) -> PartRef { bar.at(THUMB) @@ -476,6 +483,19 @@ impl<'a> CanvasScrollbars<'a> { changed } + /// `rect`, a canvas's rect the bars are shown in, without the strips they take: the vertical bar's along the right + /// edge and, unless `vertical_only`, the horizontal bar's along the bottom. `document_chrome` is the bars' own flag. + /// A canvas control placed in a corner of the view (the Rotate View compass) is placed inside this rect so the bars + /// do not cover it. + pub fn clear_of(ui: &Ui, rect: Rect, document_chrome: bool, vertical_only: bool) -> Rect { + let env = Env::of(ui); + let (thickness, margin) = bar_size(&env.tokens, document_chrome); + let strip = thickness + margin; + let right = (rect.right() - strip).max(rect.left()); + let bottom = if vertical_only { rect.bottom() } else { (rect.bottom() - strip).max(rect.top()) }; + Rect::from_min_max(rect.min, pos2(right, bottom)) + } + fn bars(&self, view: &CanvasView, rect: Rect, style: &ScrollbarsStyle, held: Option<&HeldThumb>) -> Bars { // A horizontal thumb held keeps its bar until the press ends, so its release closes the drag. let horizontal = !self.vertical_only || held.is_some_and(|held| held.on(Axis::X)); @@ -491,8 +511,8 @@ impl Component for CanvasScrollbars<'_> { fn style(cx: &StyleCx<'_>, key: CanvasScrollbarsStyleKey) -> ScrollbarsStyle { let mut look = cx.scroll_bar_of(ScrollBarKind::Docked); - look.thickness = cx.tokens.size.icon.md; - let margin = if key.document_chrome { 0.0 } else { cx.tokens.space.gap.tight }; + let (thickness, margin) = bar_size(cx.tokens, key.document_chrome); + look.thickness = thickness; let document = key.document_chrome.then(|| { let color = &cx.tokens.color; look.track = Stateful::new(cx.color(color.surface.sunken)); diff --git a/crates/ui-canvas/tests/it/canvas/rulers.rs b/crates/ui-canvas/tests/it/canvas/rulers.rs index f481eff5ee..b8e0de311e 100644 --- a/crates/ui-canvas/tests/it/canvas/rulers.rs +++ b/crates/ui-canvas/tests/it/canvas/rulers.rs @@ -350,6 +350,39 @@ fn cross_hair_strokes(mode: craft_theme::Mode, contrast: craft_theme::Contrast) (strokes, pasteboard) } +/// The rulers' corner marks the draggable zero point with a "+": a level and an upright line, each centred in the +/// corner and inside it. +#[trial] +fn the_ruler_corner_marks_the_zero_point_with_a_plus() { + let room = Vec2::new(800.0, 600.0); + let view = CanvasView::new(Rect::from_min_size(Pos2::ZERO, room), Point::new(400.0, 300.0), 1.0); + let text = RulerText::new(RulerUnits::pixels(), UnitKind::Pixels); + let mut zero = Point::ZERO; + let mut fixture = Fixture::new(move |ui| { + let strips = Rulers::new("Rulers", Bind::bound(&mut zero)); + let _ = Canvas::new("Document").view(view).with_rulers(Some(strips)).rulers(text, text).show(ui, |_, _| {}); + }); + fixture.room(room); + fixture.settle(); + let corner = fixture.by_label("Ruler origin").expect("the rulers' corner").rect; + let lines: Vec<[Pos2; 2]> = fixture + .frame_shapes() + .iter() + .filter_map(|shape| match shape { + Shape::LineSegment { points, .. } => Some(*points), + Shape::Path(path) if path.points.len() == 2 => Some([path.points[0], path.points[1]]), + _ => None, + }) + .filter(|[a, b]| corner.contains(*a) && corner.contains(*b)) + .collect(); + // The pointer marker crosses the whole corner under its fill; the plus is shorter than the corner's side. + let mark = |length: f32| length > 1.0 && length < corner.width() * 0.75; + let centred = |a: Pos2, b: Pos2| ((a + b.to_vec2()) / 2.0 - corner.center()).length() < 1.0; + let level = lines.iter().any(|[a, b]| (a.y - b.y).abs() < 0.01 && mark((a.x - b.x).abs()) && centred(*a, *b)); + let upright = lines.iter().any(|[a, b]| (a.x - b.x).abs() < 0.01 && mark((a.y - b.y).abs()) && centred(*a, *b)); + assert!(level && upright, "the corner {corner:?} shows a centred plus: lines {lines:?}"); +} + /// With artboards hidden the paper fills the canvas, so the zero point's cross hairs cross the paper as well as the /// pasteboard and dark art. Over each, the top line contrasts with what lies under it (the strokes below it over the /// backdrop) by the minimum the theme sets for overlay strokes on their halo: 3 to 1, and 4.5 to 1 in high contrast. diff --git a/crates/ui-canvas/tests/it/canvas/scrollbars.rs b/crates/ui-canvas/tests/it/canvas/scrollbars.rs index 06fd794e7f..48059e4fe5 100644 --- a/crates/ui-canvas/tests/it/canvas/scrollbars.rs +++ b/crates/ui-canvas/tests/it/canvas/scrollbars.rs @@ -5,7 +5,7 @@ use std::rc::Rc; use craft_behavior::kurbo::{Point, Rect as DocRect, Size}; use craft_behavior::{Axis, CanvasView}; -use craft_ui::canvas::{Canvas, CanvasScrollbars, NavigationOptions, Rulers, canvas_space}; +use craft_ui::canvas::{Canvas, CanvasScrollbars, NavigationOptions, RotateView, Rulers, canvas_space}; use craft_ui::contract::{Bind, Show as _, ShowContainer as _}; use craft_ui::runtime::access::Role; use craft_ui::runtime::env::Label; @@ -504,3 +504,35 @@ fn after_a_thumb_drag_the_bars_follow_a_new_zoom() { let first = shown_range(&view.get(), Axis::X).0; assert!((first - fresh).abs() < 1.0, "after a thumb drag paging reaches the same end: shows from {first}, without the drag {fresh}"); } + +/// The Rotate View compass of a turned view, placed in `CanvasScrollbars::clear_of` the window, sits clear of the +/// vertical bar, and that rect stops above the horizontal bar, with or without the document chrome. +#[trial] +fn the_rotate_view_compass_placed_clear_of_the_bars_is_not_covered_by_them() { + for chrome in [false, true] { + let large = Size::new(4000.0, 3000.0); + let clear = Rc::new(Cell::new(Rect::NOTHING)); + let seen = Rc::clone(&clear); + let mut view = CanvasView::new(DOCUMENT_WINDOW, Point::new(2000.0, 1500.0), 1.0).rotation(30.0); + let mut fixture = Fixture::new(move |ui| { + let mut turn = view.rotation; + Canvas::new("Document").document(large).view(view).show_at(ui, DOCUMENT_WINDOW, |ui, _| { + let room = CanvasScrollbars::clear_of(ui, DOCUMENT_WINDOW, chrome, false); + seen.set(room); + let _ = RotateView::new("Rotation", Bind::bound(&mut turn), false).show_at(ui, RotateView::compass_rect(ui, room)); + }); + let doc = DocRect::from_origin_size(Point::ZERO, large); + let bars = CanvasScrollbars::new("Scroll bars", Bind::bound(&mut view), doc); + let bars = if chrome { bars.document_chrome() } else { bars }; + let _ = bars.show_at(ui, DOCUMENT_WINDOW); + }); + fixture.room(vec2(760.0, 560.0)).settle(); + let compass = fixture.by_label("Rotation compass").expect("the compass").rect; + let vertical = fixture.by_label("Vertical scroll bar").expect("the vertical bar").rect; + let horizontal = fixture.by_label("Horizontal scroll bar").expect("the horizontal bar").rect; + assert!(compass.right() <= vertical.left() + TOLERANCE, "chrome {chrome}: the compass {compass:?} reaches under the bar {vertical:?}"); + let room = clear.get(); + assert!(room.right() <= vertical.left() + TOLERANCE, "chrome {chrome}: {room:?} reaches the vertical bar {vertical:?}"); + assert!(room.bottom() <= horizontal.top() + TOLERANCE, "chrome {chrome}: {room:?} reaches the horizontal bar {horizontal:?}"); + } +} diff --git a/crates/ui-widgets/src/text/zoom_field.rs b/crates/ui-widgets/src/text/zoom_field.rs index 44de970cab..89b25d9a7b 100644 --- a/crates/ui-widgets/src/text/zoom_field.rs +++ b/crates/ui-widgets/src/text/zoom_field.rs @@ -120,9 +120,10 @@ impl ZoomField { self } - /// Uses the status bar's space and field appearance. + /// Uses the status bar's space and field appearance: the whole percentage in an unframed recessed well. An app that + /// shows its own zoom field in a status bar sets it. #[must_use] - pub(crate) fn status_bar(mut self) -> Self { + pub fn status_bar(mut self) -> Self { self.status_bar = true; self } diff --git a/crates/ui-widgets/tests/it/text/zoom_field.rs b/crates/ui-widgets/tests/it/text/zoom_field.rs index 17e7134bb9..6dc0cf6946 100644 --- a/crates/ui-widgets/tests/it/text/zoom_field.rs +++ b/crates/ui-widgets/tests/it/text/zoom_field.rs @@ -398,6 +398,32 @@ fn zoom_field_fits_a_narrow_host_slot() { assert!((kept.zoom() - 200.0).abs() < 0.01, "the field remains editable"); } +/// An app's own status-bar zoom field draws in the status bar's look: unlike a plain zoom field, it shows no outline +/// while it does not have the focus. +#[trial] +fn an_apps_status_bar_zoom_field_draws_without_an_outline() { + let outlines = |status_bar: bool| { + let mut fixture = Fixture::new(move |ui| { + let canvas = Rect::from_min_size(Pos2::ZERO, Vec2::new(800.0, 600.0)); + let field = ZoomField::new(ZOOM, Some(CanvasView::new(canvas, Point::new(1000.0, 750.0), 1.0)), DOCUMENT); + let field = if status_bar { field.status_bar() } else { field }; + field.show(ui); + }); + fixture.settle(); + let field = fixture.by_label(ZOOM).expect("the zoom field").rect; + fixture + .frame_shapes() + .iter() + .filter(|shape| match shape { + egui::Shape::Rect(rect) => rect.stroke.width > 0.0 && rect.stroke.color.a() > 0 && rect.rect.intersects(field), + _ => false, + }) + .count() + }; + assert!(outlines(false) > 0, "a plain zoom field draws its outline"); + assert_eq!(outlines(true), 0, "the status-bar zoom field draws none"); +} + /// A rail-width field shows all of a three-digit zoom and its percent sign. #[trial] fn rail_width_zoom_shows_its_whole_value() {