diff --git a/crates/draw/tests/it/transform_box.rs b/crates/draw/tests/it/transform_box.rs index 4712c9ff8..1890837fd 100644 --- a/crates/draw/tests/it/transform_box.rs +++ b/crates/draw/tests/it/transform_box.rs @@ -160,7 +160,7 @@ fn harness(state: BoxState, setup: impl FnOnce(&mut Config)) -> Harness { canvas = canvas.snap(snap); } if let Some(ruler) = config.rulers { - canvas = canvas.rulers(ruler, ruler, Point::ZERO); + canvas = canvas.rulers(ruler, ruler); } let canvas = canvas.show(ui, |ui, body| { let mut guide_edits = 0; diff --git a/crates/ui-bench/src/bin/foundation/main.rs b/crates/ui-bench/src/bin/foundation/main.rs index 1295c3093..c4a5d79ef 100644 --- a/crates/ui-bench/src/bin/foundation/main.rs +++ b/crates/ui-bench/src/bin/foundation/main.rs @@ -475,9 +475,9 @@ impl Scene for CanvasViewScene { let text = RulerText::new(RulerUnits::pixels(), UnitKind::Pixels); let canvas = ui.scope_id().with("canvas scene"); let mut hosted = None; - let shown = Canvas::new("Document").id(canvas).view(*view).rulers(text, text, *zero).show_rulers(true).show(ui, |ui, body| { + let strips = Rulers::new("Rulers", Bind::bound(&mut *zero)); + let shown = Canvas::new("Document").id(canvas).view(*view).rulers(text, text).with_rulers(Some(strips)).show(ui, |ui, body| { let _ = Guides::new("Guides", canvas, Bind::bound(&mut *guides)).show_at(ui, body.rect); - let _ = Rulers::new("Rulers", Bind::bound(&mut *zero)).show_at(ui, body.rect); if let Some(pass) = tool_pass(&body.env) { hosted = Some(ToolHost::new().run(ui, &pass, &mut *hand)); } diff --git a/crates/ui-canvas/src/canvas/mod.rs b/crates/ui-canvas/src/canvas/mod.rs index 2439b191d..54731e538 100644 --- a/crates/ui-canvas/src/canvas/mod.rs +++ b/crates/ui-canvas/src/canvas/mod.rs @@ -30,7 +30,7 @@ pub use crop::{CropOverlay, CropOverlayOutput, CropShield, CropStyle, ShieldColo pub use frame_step::{FRAME_STEP_MAX, frame_step}; pub(crate) use march::{MARCH_SECONDS, MARCH_TICK, march_shift}; pub use readout::{ShowReadouts, hud, readout}; -pub use space::{CanvasSpace, RulerText, canvas_space, pixel_aligned}; +pub use space::{CanvasSpace, RulerText, canvas_space, pixel_aligned, ruler_guide}; pub use words::CanvasWords; #[doc(hidden)] @@ -120,6 +120,9 @@ use zoom_motion::{Steer, ViewMotion}; pub(crate) const BACKGROUND: PartKey = PartKey::new("background"); +/// The key the canvas shows its rulers under. +const RULERS: &str = "rulers"; + /// The areas the active tool claims over the canvas controls ([`Canvas::tool_regions`]): `TOOL_REGION.at(i)` is the /// region at `i`. const TOOL_REGION: PartKey = PartKey::new("toolRegion"); @@ -188,9 +191,13 @@ pub struct Canvas<'a> { /// eases (Animated Zoom) and whether the view may leave the document (Overscroll). #[prop] navigation: NavigationOptions, - /// Whether the rulers show along the top and left edges (View > Rulers): the view lies inside them. - #[prop] - show_rulers: bool, + /// The rulers along the top and left edges (View > Rulers), which the canvas shows itself: the view lies inside + /// them ([`with_rulers`](Self::with_rulers)). + strips: Option>, + /// Whether the strips show; the rulers' own output of this pass is [`CanvasOutput::rulers`]. + strips_shown: bool, + /// What the rulers reported in this pass, from the paint step. + ruler_output: Option, /// Whether the canvas leaves Page Up, Page Down, Home, End and the arrows to its body: it applies no page keys /// and gives no tool or session control the arrows, and its body reads them in [`CanvasSpace::keys`]. For a /// body that pages its document itself (a page view). @@ -198,6 +205,8 @@ pub struct Canvas<'a> { keys_to_body: bool, /// The rulers' units and zero point, published to the body in its [`CanvasSpace`]. rulers: Option, + /// The zero point of the rulers given to `with_rulers`, which the space publishes in `rulers`. + ruler_zero: Point, /// The snapping of the coordinates the pointer sets on this canvas (View > Snap To), as data over the document's /// targets: the rulers snap their zero point with it, the guides snap a guide with it and the tool host hands it to /// the tool ([`ToolContext::snap`]). The canvas sets its tolerance each pass from the view it shows. Without it @@ -272,11 +281,14 @@ impl<'a> Canvas<'a> { layer_bounds: None, queued: &[], navigation: NavigationOptions::default(), - show_rulers: false, + strips: None, + strips_shown: false, + ruler_output: None, keys_to_body: false, pasteboard: PasteboardColor::Default, precise_cursors: false, rulers: None, + ruler_zero: Point::ZERO, snap: None, settled: None, context_key: false, @@ -294,7 +306,7 @@ impl<'a> Canvas<'a> { /// (`Bind::given`, or a plain [`CanvasView`]) is only read: the app copies what a tool changed from /// [`ToolHostResponse::view`] (or [`CanvasOutput::view`] without a tool host) every pass. /// - /// The canvas sets the view's rect to its own rect, inside the rulers while they show ([`show_rulers`](Self::show_rulers)), + /// The canvas sets the view's rect to its own rect, inside the rulers while they show ([`with_rulers`](Self::with_rulers)), /// so the body and the reported view carry this pass's rect. A view whose rect has no area (`CanvasView::new(Rect::NOTHING, ..)`, /// a document never shown) opens fitted: Fit on Screen in the first pass the canvas has area and a document. A /// canvas with no area (a minimized window) leaves the view as it is, so a restore does not refit. @@ -379,11 +391,6 @@ impl<'a> Canvas<'a> { self.document.map(|target| target.document) } - /// The rect the view lies in: the canvas's own, inside the rulers while they show. - fn view_rect(&self, rect: Rect, style: &CanvasStyle) -> Rect { - if self.show_rulers { Rect::from_min_max(rect.min + style.rulers, rect.max) } else { rect } - } - /// The view of this pass from the app's `given` view: this pass's rect, the first fit, the wheel, a middle-button /// pan, the view commands, the Overscroll clamp, and the zoom ease. fn settle(&self, cx: &mut Cx<'_, Self>, given: CanvasView) -> Settled { @@ -393,7 +400,7 @@ impl<'a> Canvas<'a> { let pixels_per_point = cx.env.scale().0; let size = self.document_size().unwrap_or(Size::ZERO); let mut view = given; - let area = self.view_rect(cx.content_rect, cx.style); + let area = cx.content_rect; if area.width() > 0.0 && area.height() > 0.0 { let never_shown = !(view.rect.width() > 0.0 && view.rect.height() > 0.0); view.rect = area; @@ -484,13 +491,25 @@ impl<'a> Canvas<'a> { Settled { target, shown, easing, keys } } - /// Measures the document's axes in `x` and `y`, counted from `zero` (document units). The rulers, the guides and - /// the measures of the body read the units and the zero from the canvas's [`CanvasSpace`], so the app states them - /// once. A canvas that publishes none measures in pixels from the document's origin. Whether the strips show, and - /// so where the view lies, is [`show_rulers`](Self::show_rulers). + /// Shows `rulers` along the top and left edges of the canvas, which the view lies inside (View > Rulers); none shows + /// no strips. The canvas hands them its space and snap, and the body (guides) reads the guide a drag out of them + /// places with [`ruler_guide`]. What they report comes back in [`CanvasOutput::rulers`]. #[must_use] - pub fn rulers(mut self, x: RulerText, y: RulerText, zero: Point) -> Self { - self.rulers = Some(CanvasRulers::new(x, y, zero)); + pub fn with_rulers(mut self, rulers: Option>) -> Self { + self.ruler_zero = rulers.as_ref().map_or(Point::ZERO, Rulers::zero); + self.strips_shown = rulers.is_some(); + self.strips = rulers; + self + } + + /// Measures the document's axes in `x` and `y`, counted from the zero point of the [`Rulers`] given to + /// [`with_rulers`](Self::with_rulers) (the document's origin without them). The rulers, the guides and the + /// measures of the body read the units and the zero from the canvas's [`CanvasSpace`], so the app states the zero + /// once. A canvas that publishes none measures in pixels from the document's origin. Whether the strips show, and + /// so where the view lies, is [`with_rulers`](Self::with_rulers). + #[must_use] + pub fn rulers(mut self, x: RulerText, y: RulerText) -> Self { + self.rulers = Some(CanvasRulers::new(x, y, Point::ZERO)); self } @@ -508,7 +527,7 @@ impl<'a> Canvas<'a> { let space = space .with_keys(keys) .with_document(self.document_size()) - .with_rulers(self.rulers) + .with_rulers(self.rulers.map(|rulers| CanvasRulers::new(rulers.x, rulers.y, self.ruler_zero))) .with_pasteboard(self.pasteboard) .with_background(background) .with_precise(self.precise_cursors != caps_lock) @@ -583,6 +602,8 @@ pub struct CanvasOutput { /// target; the canvas shows the way there itself. [`ToolHost::run`] carries it in [`ToolHostResponse::view`] /// when no tool set a view, so an app with a tool host copies that one view. pub view: Option, + /// What the rulers given to [`Canvas::with_rulers`] reported in this pass. + pub rulers: Option, } impl CanvasOutput { @@ -655,9 +676,19 @@ impl Component for Canvas<'_> { WheelNotch::Points(WheelNavigation::LINE) } - /// Reserves the slot the picture paints into, under every canvas control of the body (`CanvasSpace::paint_picture`). + /// The rulers' strips lie outside the view the body is clipped to. + fn padding(&self, _rect: Rect, style: &CanvasStyle) -> craft_behavior::Insets { + if self.strips_shown { craft_behavior::Insets::new(style.rulers.x, 0.0, style.rulers.y, 0.0) } else { style.metrics().padding } + } + + /// Reserves the slot the picture paints into, under every canvas control of the body (`CanvasSpace::paint_picture`), + /// and shows the rulers over the canvas's rect. fn paint(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>) { self.picture = Some(ui.reserve_slot(cx.content_rect)); + if let Some(rulers) = self.strips.take() { + let pass = self.tool_pass(cx); + self.ruler_output = Some(ui.child(RULERS, cx.rect, rulers.on_canvas(pass.space, pass.snap))); + } } /// The view of this pass, kept for the body and `finish`. @@ -689,6 +720,7 @@ impl Component for Canvas<'_> { let pass = self.tool_pass(cx); space::publish(values, pass.space); snap::publish(values, pass.snap.clone()); + space::publish_ruler_guide(values, self.ruler_output.as_ref().and_then(|output| output.guide)); tool_host::publish(values, (!measure::active(&cx.response.ctx)).then_some(pass)); } @@ -723,7 +755,7 @@ impl Component for Canvas<'_> { fn finish(self, cx: Cx<'_, Self>) -> CanvasOutput { let pass = self.tool_pass(&cx); - CanvasOutput { response: cx.response.clone(), press: pass.press, space: pass.space, view: pass.view } + CanvasOutput { response: cx.response.clone(), press: pass.press, space: pass.space, view: pass.view, rulers: self.ruler_output } } } diff --git a/crates/ui-canvas/src/canvas/rulers/mod.rs b/crates/ui-canvas/src/canvas/rulers/mod.rs index f30d57f3a..469831bb5 100644 --- a/crates/ui-canvas/src/canvas/rulers/mod.rs +++ b/crates/ui-canvas/src/canvas/rulers/mod.rs @@ -9,7 +9,7 @@ mod ticks; pub use ticks::{INCH_EIGHTHS, INCH_SIXTEENTHS, LABEL_STEP_MIN, RulerTick, StepLadder, TickRule}; use super::words::CanvasWords; -use super::{CanvasRulers, CanvasSpace, GuideDrag, RulerText, Snap, canvas_snap, canvas_space, context_menu, readout}; +use super::{CanvasRulers, CanvasSpace, GuideDrag, RulerText, Snap, context_menu, readout}; use crate::contract::overlay::Anchor; use crate::contract::{ Access, Bind, BindMut, Common, Component, Cx, Edit as _, EditResponse, Entry, Focus as _, MeasureCx, Name, Outcome, OverlayUi, Overlays as _, @@ -17,7 +17,7 @@ use crate::contract::{ }; use crate::menu::Menu; use crate::primitives::{PaintShape, RectSide, StrokeWidth, TextRun, Tick, TickEdge, TickRank}; -use crate::runtime::env::{EnvView, Label, UnitKind}; +use crate::runtime::env::{Label, UnitKind}; use crate::runtime::focus::KeyUse; use crate::runtime::memory::Remember as _; use crate::style::{ComponentStyle, RuleKind, StyleColor, StyleCx, TextLook, TypeRole, UiTokens}; @@ -178,11 +178,14 @@ enum MenuRow { /// The rulers of a canvas: ticks and labels counted from a zero point, level whatever the view's rotation, a pointer /// marker, the guide drag-out and the zero point's corner. /// -/// - Show it with `show_at` over the canvas's rect inside the `Canvas` body, and lay the view out in -/// [`Rulers::content_rect`] of that rect. The units come from the canvas ([`Canvas::rulers`](super::Canvas::rulers)); -/// the rulers hold the zero point's edit. -/// - A drag on the top ruler reports a horizontal guide in [`RulersOutput::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 +/// Rulers show only through [`Canvas::with_rulers`](super::Canvas::with_rulers): the canvas hands them its space and +/// snap, and shown anywhere else they take no input. +/// +/// - Give it to the canvas ([`Canvas::with_rulers`](super::Canvas::with_rulers)), which shows it along the top and +/// left of its rect and lays its view out inside the strips. The units come from the canvas +/// ([`Canvas::rulers`](super::Canvas::rulers)); the rulers hold the zero point's edit. +/// - 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 @@ -209,14 +212,17 @@ pub struct Rulers<'a> { /// no menu. #[prop] units: Option<&'a [UnitKind]>, + /// The canvas's space and snap in this pass, which the canvas hands to the rulers it shows. + space: Option, + snap: Option, guide: Option, picked: Option, context: Option, } impl<'a> Rulers<'a> { - /// Rulers named `label` that edit the zero point `zero` (document units). The units and the zero they count from - /// are the canvas's ([`Canvas::rulers`](super::Canvas::rulers)); the app passes the same zero to both. + /// Rulers named `label` that edit the zero point `zero` (document units). The canvas reads its zero from here; + /// the units are the canvas's ([`Canvas::rulers`](super::Canvas::rulers)). pub fn new(label: impl Into