diff --git a/crates/ui-egui/src/canvas/ruler_tests.rs b/crates/ui-egui/src/canvas/ruler_tests.rs index b34fe7de4..e20cada86 100644 --- a/crates/ui-egui/src/canvas/ruler_tests.rs +++ b/crates/ui-egui/src/canvas/ruler_tests.rs @@ -181,3 +181,21 @@ fn tabs_dialog_sets_clears_and_applies_on_ok() { h.state_mut().session.execute("edit.undo", &json!({})).unwrap(); assert_eq!(ruler(&mut h), before, "one undo restores both the stops and the default interval"); } + +/// The slide rulers label whole inches and centimetres, never closer than a label needs at any zoom, and write a +/// minus sign left of and above the slide's edge. +#[test] +fn slide_rulers_label_whole_units_with_room_and_sign_the_far_side() { + use craft::ui::canvas::CENTIMETRE_LABEL_STEP_MIN; + use deckcraft_geom::Unit; + for unit in [Unit::Inches, Unit::Centimeters] { + let units = super::rulers::ruler_text(unit).units; + for zoom in [0.05, 0.2, 0.5, 1.0, 2.0, 8.0] { + let (step, _) = units.step(zoom, 0.0); + assert_eq!(step.fract(), 0.0, "{unit:?} at zoom {zoom}: labels on whole units, step {step}"); + let apart = step / units.per_doc * zoom; + assert!(apart >= f64::from(CENTIMETRE_LABEL_STEP_MIN), "{unit:?} at zoom {zoom}: labels {apart} points apart"); + } + assert!(units.label_value(-1.0, 1.0) < 0.0, "{unit:?}: a label left of the slide is negative"); + } +} diff --git a/crates/ui-egui/src/canvas/rulers.rs b/crates/ui-egui/src/canvas/rulers.rs index 4bbfd1978..ad51e35f5 100644 --- a/crates/ui-egui/src/canvas/rulers.rs +++ b/crates/ui-egui/src/canvas/rulers.rs @@ -1,8 +1,8 @@ use craft::behavior::kurbo::Point; -use craft::behavior::{EditEvent, RulerUnits, Ticks}; +use craft::behavior::{EditEvent, RulerUnits, TickLabels, Ticks}; use craft::text::ui::DocUnit; use craft::text::ui::ruler::{ParagraphModel, RulerNumbers, RulerValues, TabAlign, TabLeader, TabStop, TextRuler}; -use craft::ui::canvas::{CanvasSpace, RulerText, Rulers}; +use craft::ui::canvas::{CENTIMETRE_LABEL_STEP_MIN, CanvasSpace, RulerText, Rulers}; use craft::ui::contract::{Bind, Show as _}; use craft::ui::runtime::env::UnitKind; use deckcraft_geom::Unit; @@ -10,16 +10,36 @@ use serde_json::json; use crate::SlideApp; -/// The document's unit scale and tick rule for both canvas rulers. +/// The document's unit scale and tick rule for both canvas rulers, numbered from the slide's edge with a minus sign +/// before it. // PLAN: plan error: canvas-w6 must supply ruler_text(prefs.units) to Canvas::rulers; Rulers owns no unit prop. pub(super) fn ruler_text(unit: Unit) -> RulerText { - let kind = match unit { - Unit::Inches => UnitKind::Inches, - Unit::Centimeters => UnitKind::Centimetres, - Unit::Points => UnitKind::Points, - Unit::Pixels => UnitKind::Pixels, + let (kind, ticks) = match unit { + Unit::Inches => (UnitKind::Inches, Ticks::Custom(inch_steps)), + Unit::Centimeters => (UnitKind::Centimetres, Ticks::Custom(centimetre_steps)), + Unit::Points => (UnitKind::Points, Ticks::decimal(false)), + Unit::Pixels => (UnitKind::Pixels, Ticks::decimal(true)), }; - RulerText::new(RulerUnits::new(1.0 / unit.points_per_unit(), Ticks::decimal(unit == Unit::Pixels)), kind) + RulerText::new(RulerUnits::new(1.0 / unit.points_per_unit(), ticks).with_labels(TickLabels::Signed), kind) +} + +/// A label every inch with quarter-inch ticks. +fn inch_steps(points_per_inch: f64) -> (f64, u32) { + whole_steps(points_per_inch, 4) +} + +/// A label every centimetre with half-centimetre ticks. +fn centimetre_steps(points_per_cm: f64) -> (f64, u32) { + whole_steps(points_per_cm, 2) +} + +/// Every whole unit labelled with `subdivisions` ticks to it; where a unit is too narrow for its label, every 2, 5, 10 +/// and so on units, halved. +fn whole_steps(points_per_unit: f64, subdivisions: u32) -> (f64, u32) { + const STEPS: [f64; 7] = [1.0, 2.0, 5.0, 10.0, 20.0, 50.0, 100.0]; + let room = f64::from(CENTIMETRE_LABEL_STEP_MIN); + let step = STEPS.into_iter().find(|step| step * points_per_unit >= room).unwrap_or(1000.0); + (step, if step > 1.0 { 2 } else { subdivisions }) } /// The document's unit as the text controls take it: the ruler's, and the Tabs dialog it opens.