diff --git a/crates/ui-canvas/src/canvas/rulers/mod.rs b/crates/ui-canvas/src/canvas/rulers/mod.rs index fc65d8bb7..2a02647b0 100644 --- a/crates/ui-canvas/src/canvas/rulers/mod.rs +++ b/crates/ui-canvas/src/canvas/rulers/mod.rs @@ -281,10 +281,12 @@ impl<'a> Rulers<'a> { PressPhase::Drag { pos, .. } => (GesturePhase::Update, pos), PressPhase::Release { pos, click: false, .. } => (GesturePhase::Commit, pos), PressPhase::Cancel { .. } => (GesturePhase::Cancel, start.origin), + PressPhase::Down { .. } if press.double_pressed() => (GesturePhase::Commit, start.origin), _ => return None, }; let along = if press.mods.alt { across_axis(part.guide_along()) } else { part.guide_along() }; - let at = (phase != GesturePhase::Cancel && content.contains(pos)).then(|| { + // PLAN: a double press lies on the strip itself, outside `content`; the guides' bounds decide whether it lands. + let at = (phase != GesturePhase::Cancel && (press.double_pressed() || content.contains(pos))).then(|| { let across = coordinate_across(along, space.view.to_doc(pos)); if press.mods.shift { let (text, _) = space.rulers().across(along); diff --git a/crates/ui-canvas/src/canvas_controls/grid/mod.rs b/crates/ui-canvas/src/canvas_controls/grid/mod.rs index dbc12d352..89471ee4e 100644 --- a/crates/ui-canvas/src/canvas_controls/grid/mod.rs +++ b/crates/ui-canvas/src/canvas_controls/grid/mod.rs @@ -7,7 +7,7 @@ pub(super) mod stories; use crate::canvas::{CanvasSpace, GuideLine, canvas_space}; use crate::contract::{Access, Common, Component, Cx, MeasureCx, PaintUi, Props as _, Role}; -use crate::primitives::{GridInk, GridLook, GridStroke, grid_lines_with}; +use crate::primitives::{GridInk, GridLook, GridStroke, PaintSlot, grid_lines_with}; use crate::runtime::env::Label; use crate::runtime::memory::Remember as _; use crate::style::{ComponentStyle, DataColor, StyleColor, StyleCx}; @@ -118,7 +118,6 @@ pub struct CanvasGrid { /// The grid's shapes go into the canvas picture slot in show order, so shown before the picture the grid lies /// under the art; outside a slot it paints in place. #[prop(flag, name = "picture")] - #[allow(dead_code)] // P3(canvas-w5): cv.F28 under_pictures: bool, } @@ -227,11 +226,15 @@ impl Component for CanvasGrid { } fn paint(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>) { - if canvas_space(&cx.env).is_none() { - return; - } + let Some(space) = canvas_space(&cx.env) else { return }; let shapes = cx.recall::().shapes; - ui.content_painter(cx).painter.extend(shapes.iter().cloned()); + let painter = ui.content_painter(cx); + let first = (self.under_pictures && space.picture_slot().is_some()).then(|| PaintSlot::reserve(&painter.painter)); + painter.painter.extend(shapes.iter().cloned()); + if let Some(first) = first { + let ctx = painter.painter.ctx().clone(); + space.paint_picture(&ctx, first.take_after(&ctx)); + } } fn access(&self, access: &mut Access, _: &Cx<'_, Self>) { diff --git a/crates/ui-canvas/src/canvas_controls/grid/stories.rs b/crates/ui-canvas/src/canvas_controls/grid/stories.rs index 4a3d5b26d..48a190a70 100644 --- a/crates/ui-canvas/src/canvas_controls/grid/stories.rs +++ b/crates/ui-canvas/src/canvas_controls/grid/stories.rs @@ -1,10 +1,12 @@ //! Stories of `CanvasGrid`. use super::{CanvasGrid, GridLine}; -use crate::canvas::testing::canvas_story::{StoryDocument, on_canvas}; +use crate::canvas::testing::canvas_story::{StoryDocument, StoryPicture, on_canvas, on_canvas_with, story_view}; +use crate::canvas::{Canvas, RasterDisplay, RasterSource as _}; use crate::contract::Show as _; -use crate::testing::{Drive, StoryData, StoryOutput, story}; -use egui::Ui; +use crate::testing::{Drive, StoryData, StoryOutput, stage, story}; +use craft_behavior::kurbo::Point; +use egui::{Color32, Ui}; const MAJOR: f64 = 100.0; const SUBDIVISIONS: u32 = 4; @@ -32,3 +34,18 @@ fn dots(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { fn crossings(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { on_canvas(ui, |ui, at, _| grid().line(GridLine::Crossings).show_at(ui, at)) } + +#[story(CanvasGrid, covers = [Default], drive = Drive::Never)] +fn grid_in_back(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { + let size = StoryDocument::STANDARD.size(); + // A picture over the left half of the document: the grid shows only beside it. + let picture = StoryPicture::new(size.width / 2, size.height, |_| Color32::from_gray(200)); + let room = stage(ui); + let centre = Point::new(f64::from(size.width) / 2.0, f64::from(size.height) / 2.0); + let view = story_view(room, ui.pixels_per_point(), centre, 100.0); + on_canvas_with(ui, room, Canvas::new("Document").view(view), None, |ui, at| { + let shown = grid().picture().show_at(ui, at); + let _ = RasterDisplay::new("Picture", &picture).picture().show_at(ui, at); + shown + }) +} diff --git a/crates/ui-canvas/src/canvas_controls/guides/mod.rs b/crates/ui-canvas/src/canvas_controls/guides/mod.rs index 6a1111409..e8b710842 100644 --- a/crates/ui-canvas/src/canvas_controls/guides/mod.rs +++ b/crates/ui-canvas/src/canvas_controls/guides/mod.rs @@ -95,6 +95,8 @@ pub struct GuideStyle { focus: FocusLook, /// The width of a guide line. width: f32, + /// The width of the key guide's line. + key_width: f32, dash: f32, gap: f32, } @@ -151,7 +153,6 @@ pub struct Guides<'a> { selected: &'a [GuideKey], /// The key guide, stroked at `stroke.medium`. #[prop] - #[allow(dead_code)] // P3(canvas-w5): cv.F27 key_guide: Option, /// Whether the pointer and the keyboard take guides (the Move tool, or the selection tools). Off, the guides only /// draw. @@ -218,7 +219,7 @@ impl<'a> Guides<'a> { /// The first selected guide, the one the keys and the pointer act on. fn lead_selected(&self) -> Option { - self.selected.first().copied() // P3(canvas-w5): cv.F27 + self.selected.first().copied() } /// The positions a guide may be moved to with the keyboard. @@ -472,6 +473,7 @@ impl Component for Guides<'_> { selected: cx.color(tokens.color.accent.solid), focus, width: tokens.stroke.thin, + key_width: tokens.stroke.medium, dash: tokens.stroke.dash, gap: tokens.stroke.dash_gap, } @@ -504,7 +506,7 @@ impl Component for Guides<'_> { .priority(HitPriority::new(HitLevel::Edge, 0)) .cursor(resize_cursor(&space.view, motion_across(guide.along))) .focusable(); - if self.lead_selected() == Some(guide.key) { + if self.selected.contains(&guide.key) { part.selected().flags(Flags::SELECTED); } any = true; @@ -538,15 +540,16 @@ impl Component for Guides<'_> { let focused = cx.focus_visible().then(|| cx.active_part()).flatten().filter(|part| part.key == GUIDE).and_then(|part| part.index); for guide in guides { let Some([a, b]) = place::segment(&space.view, guide.along, guide.position, guide.span, cx.rect) else { continue }; - let color = - if self.lead_selected() == Some(guide.key) { style.selected.egui() } else { guide.color.map_or(style.line.egui(), data_color) }; + let key = self.key_guide == Some(guide.key); + let color = if key || self.selected.contains(&guide.key) { style.selected.egui() } else { guide.color.map_or(style.line.egui(), data_color) }; + let width = if key { style.key_width } else { style.width }; match guide.line { - GuideLine::Solid => painter.painter.extend(Rule::shapes(&RuleLook { color, width: RuleWidth::Points(style.width) }, a, b, ppp)), + GuideLine::Solid => painter.painter.extend(Rule::shapes(&RuleLook { color, width: RuleWidth::Points(width) }, a, b, ppp)), GuideLine::Dashed => painter.dashed(&[a, b], Dashes::new(style.dash, style.gap), StrokeWidth::Thin, color), - GuideLine::Dots => painter.dashed(&[a, b], Dashes::new(style.width, style.gap), StrokeWidth::Thin, color), + GuideLine::Dots => painter.dashed(&[a, b], Dashes::new(width, style.gap), StrokeWidth::Thin, color), } if focused == Some(guide.key.get()) && (b - a).length() > 0.0 { - painter.painter.extend(FocusRing::shapes(&style.focus, FocusKind::Halo, &footprint(&space, a, b, style.width), ppp)); + painter.painter.extend(FocusRing::shapes(&style.focus, FocusKind::Halo, &footprint(&space, a, b, width), ppp)); } } } diff --git a/crates/ui-canvas/src/canvas_controls/guides/stories.rs b/crates/ui-canvas/src/canvas_controls/guides/stories.rs index c78f953f5..68a10ca58 100644 --- a/crates/ui-canvas/src/canvas_controls/guides/stories.rs +++ b/crates/ui-canvas/src/canvas_controls/guides/stories.rs @@ -47,7 +47,7 @@ fn drag_first(fixture: &mut Fixture) { fn guides_over_a_canvas(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let guides = data.value("guides", guides()); on_guides_canvas(ui, |ui, at, canvas| { - Guides::new("Guides", canvas, Bind::bound(&mut *guides)).interactive(true).selected_guides(&[GuideKey::new(2)]).show_at(ui, at) // P3(canvas-w5): cv.F27 + Guides::new("Guides", canvas, Bind::bound(&mut *guides)).interactive(true).selected_guides(&[GuideKey::new(2)]).show_at(ui, at) }) } @@ -85,3 +85,23 @@ fn bounded_guides(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let page = DocRect::new(120.0, 80.0, 520.0, 320.0); on_guides_canvas(ui, |ui, at, canvas| Guides::new("Guides", canvas, Bind::bound(&mut *guides)).interactive(true).bounds(page).show_at(ui, at)) } + +#[story(Guides<'static>, covers = [Default], drive = Drive::Never)] +fn guides_with_several_selected(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { + let guides = data.value("guides", guides()); + on_guides_canvas(ui, |ui, at, canvas| { + Guides::new("Guides", canvas, Bind::bound(&mut *guides)).interactive(true).selected_guides(&[GuideKey::new(1), GuideKey::new(2)]).show_at(ui, at) + }) +} + +#[story(Guides<'static>, covers = [Default], drive = Drive::Never)] +fn guides_with_a_key_guide(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { + let guides = data.value("guides", guides()); + on_guides_canvas(ui, |ui, at, canvas| { + Guides::new("Guides", canvas, Bind::bound(&mut *guides)) + .interactive(true) + .selected_guides(&[GuideKey::new(2)]) + .key_guide(GuideKey::new(1)) + .show_at(ui, at) + }) +} diff --git a/crates/ui-canvas/tests/it/canvas/rulers.rs b/crates/ui-canvas/tests/it/canvas/rulers.rs index 95ca61634..5a7ed8d56 100644 --- a/crates/ui-canvas/tests/it/canvas/rulers.rs +++ b/crates/ui-canvas/tests/it/canvas/rulers.rs @@ -1,9 +1,16 @@ //! The ruler tick rule (`Rulers::ticks`): labelled ticks stay at least `LABEL_STEP_MIN` apart at every zoom. -use craft_behavior::{RulerUnits, Ticks}; -use craft_ui::canvas::{INCH_EIGHTHS, INCH_SIXTEENTHS, LABEL_STEP_MIN, Rulers, StepLadder, TickRule}; +use craft_behavior::kurbo::Point; +use craft_behavior::{CanvasView, RulerUnits, Ticks}; +use craft_ui::canvas::{Canvas, INCH_EIGHTHS, INCH_SIXTEENTHS, LABEL_STEP_MIN, RulerText, Rulers, StepLadder, TickRule}; +use craft_ui::contract::{Bind, Show as _, ShowContainer as _}; use craft_ui::primitives::TickRank; -use craft_ui::testing::trial; +use craft_ui::runtime::env::UnitKind; +use craft_ui::testing::{Fixture, trial}; +use egui::{Pos2, Rect, Vec2}; +use std::cell::Cell; +use std::collections::BTreeSet; +use std::rc::Rc; /// Screen points per document unit, from far out to deep in. const ZOOMS: [f64; 10] = [0.01, 0.0625, 0.25, 1.0 / 3.0, 1.0, 3.7, 16.0, 64.0, 320.0, 3200.0]; @@ -57,3 +64,36 @@ fn a_five_character_vertical_label_fits_the_least_label_step() { assert!(5.0 * pitch + tokens.space.gap.tight < LABEL_STEP_MIN, "{mode:?}: five characters at pitch {pitch}"); } } + +/// The vertical extents of the tick marks the top strip paints over a canvas whose view is turned by `degrees`. +fn top_tick_extents(degrees: f64) -> BTreeSet<(i32, i32)> { + 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).rotation(degrees); + let text = RulerText::new(RulerUnits::pixels(), UnitKind::Pixels); + let content = Rc::new(Cell::new(Rect::NOTHING)); + let seen = Rc::clone(&content); + let mut zero = Point::ZERO; + let mut fixture = Fixture::new(move |ui| { + Canvas::new("Document").view(view).show_rulers(true).rulers(text, text, Point::ZERO).show(ui, |ui, body| { + seen.set(Rulers::new("Rulers", Bind::bound(&mut zero)).show_at(ui, body.rect).content_rect()); + }); + }); + fixture.room(room); + fixture.settle(); + let strip_bottom = content.get().min.y; + fixture + .frame_shapes() + .iter() + .map(|shape| shape.visual_bounding_rect()) + .filter(|bounds| bounds.width() < 3.0 && bounds.height() > 1.0 && bounds.max.y <= strip_bottom + 0.5) + .map(|bounds| ((bounds.min.y * 10.0).round() as i32, (bounds.max.y * 10.0).round() as i32)) + .collect() +} + +#[trial] +fn ticks_stay_level_and_the_zero_label_sits_at_the_zero_point_in_a_turned_view() { + let level = top_tick_extents(0.0); + let turned = top_tick_extents(30.0); + assert!(!turned.is_empty(), "the turned view's top strip has tick marks"); + assert!(turned.is_subset(&level), "tick marks keep the lengths and cross coordinates of the level view: {turned:?} against {level:?}"); +} diff --git a/crates/ui-canvas/tests/it/canvas_controls/grid.rs b/crates/ui-canvas/tests/it/canvas_controls/grid.rs index 3578bf7be..217f5f557 100644 --- a/crates/ui-canvas/tests/it/canvas_controls/grid.rs +++ b/crates/ui-canvas/tests/it/canvas_controls/grid.rs @@ -1,10 +1,14 @@ //! The canvas grid and the pixel grid (20.6): line counts, the per-axis cap, subdivisions, and the pixel grid's maths and //! region cache. -use craft_ui::canvas::PixelRect; -use craft_ui::canvas_controls::{GridSegment, PixelGrid}; -use craft_ui::testing::trial; -use egui::Color32; +use craft_behavior::CanvasView; +use craft_behavior::kurbo::Point; +use craft_ui::canvas::{Canvas, PixelRect, RasterDisplay}; +use craft_ui::canvas_controls::{CanvasGrid, GridSegment, PixelGrid}; +use craft_ui::contract::{Show as _, ShowContainer as _}; +use craft_ui::testing::canvas_story::StoryPicture; +use craft_ui::testing::{Fixture, trial}; +use egui::{Color32, Pos2, Rect, Shape, TextureId, Vec2}; use std::collections::BTreeMap; /// The colour of the pixel at (`x`, `y`) of a document the tests draw: some see-through, the rest in four greys. @@ -65,3 +69,34 @@ fn the_grid_colour_follows_the_pixel_below() { let faint = line(5).expect("a half see-through dark pixel has a line"); assert!(faint.1 < dark.1, "the line follows the pixel's alpha: {faint:?} against {dark:?}"); } + +/// How many shapes a canvas paints before and after its picture, with the grid shown before the picture carrying the +/// `picture` flag when `under` is set. +fn shapes_around_the_picture(under: bool) -> (usize, usize) { + let room = Vec2::new(800.0, 600.0); + let source = StoryPicture::new(40, 30, |_| Color32::WHITE); + let view = CanvasView::new(Rect::from_min_size(Pos2::ZERO, room), Point::new(20.0, 15.0), 4.0); + let mut fixture = Fixture::new(move |ui| { + Canvas::new("Document").view(view).show(ui, |ui, body| { + let grid = CanvasGrid::new("Grid").spacing((10.0, 10.0).into()); + let _ = if under { grid.picture() } else { grid }.show_at(ui, body.rect); + let _ = RasterDisplay::new("Picture", &source).picture().show_at(ui, body.rect); + }); + }); + fixture.room(room); + fixture.settle(); + let shapes = fixture.frame_shapes(); + let at = shapes + .iter() + .position(|shape| matches!(shape, Shape::Mesh(mesh) if mesh.texture_id != TextureId::default())) + .expect("the picture's mesh"); + (at, shapes.len() - at - 1) +} + +#[trial] +fn a_grid_shown_before_a_picture_lies_under_it_and_one_without_the_flag_lies_over_it() { + let (under_before, under_after) = shapes_around_the_picture(true); + let (over_before, over_after) = shapes_around_the_picture(false); + assert!(under_before > over_before, "the flagged grid's lines come before the picture: {under_before} against {over_before}"); + assert!(under_after < over_after, "the grid without the flag comes after it: {under_after} against {over_after}"); +} diff --git a/crates/ui-canvas/tests/it/canvas_controls/guides.rs b/crates/ui-canvas/tests/it/canvas_controls/guides.rs index 0ebfa5946..a57639dd4 100644 --- a/crates/ui-canvas/tests/it/canvas_controls/guides.rs +++ b/crates/ui-canvas/tests/it/canvas_controls/guides.rs @@ -227,6 +227,41 @@ fn a_dragged_guide_is_removed_off_the_bounds_or_on_a_ruler_and_alt_and_shift_cha assert!(matches!(landed.as_slice(), [tick] if tick.fract() == 0.0), "Shift deep in lands on a whole pixel: {landed:?}"); } +// A double press on a strip places a guide at once, at the pointer, with the drag-out's Alt and Shift rules. The +// pointer lies on the strip, so the guide lands only where the bounds take it: Alt turns it to the strip's across +// axis, which runs inside them. A press and drag out of a strip still adds exactly one guide. +#[trial] +fn a_double_press_on_a_ruler_places_a_guide_at_once() { + let placed = |mods: Modifiers, at: &dyn Fn(Rect) -> Pos2| -> Vec { + let guides: Shared> = Rc::new(RefCell::new(Vec::new())); + let (mut fixture, body) = ruled(1.0, &guides); + fixture.fixture().hold(mods); + fixture.double_press_at(at(body.get())); + fixture.fixture().hold(Modifiers::NONE); + let placed = guides.borrow().clone(); + placed + }; + let top = |x: f32| move |rect: Rect| pos2(x, rect.min.y + 4.0); + let left = |y: f32| move |rect: Rect| pos2(rect.min.x + 4.0, y); + + let turned = placed(Modifiers::ALT, &top(300.4)); + assert!( + matches!(turned.as_slice(), [one] if one.along == Axis::Y && (one.position - 300.4).abs() < 1e-3), + "Alt on the top ruler places a vertical guide at the pointer: {turned:?}" + ); + let ticked = placed(Modifiers::ALT | Modifiers::SHIFT, &top(300.4)); + assert!(matches!(ticked.as_slice(), [one] if one.position.fract() == 0.0), "Shift lands on a whole pixel: {ticked:?}"); + let side = placed(Modifiers::ALT, &left(300.4)); + assert!(matches!(side.as_slice(), [one] if one.along == Axis::X), "Alt on the left ruler places a horizontal guide: {side:?}"); + assert!(placed(Modifiers::ALT, &top(650.0)).is_empty(), "a double press outside the bounds adds none"); + + let guides: Shared> = Rc::new(RefCell::new(Vec::new())); + let (mut fixture, body) = ruled(1.0, &guides); + let rect = body.get(); + drag_with(&mut fixture, Modifiers::NONE, pos2(300.0, rect.min.y + 4.0), pos2(300.0, 247.6)); + assert_eq!(guides.borrow().len(), 1, "a press and drag out of a strip adds exactly one guide: {:?}", guides.borrow()); +} + // Delete on a focused guide removes it, and the focus the removal hands to the next guide selects nothing: no guide is // selected after a delete, as in Photoshop. #[trial] @@ -236,7 +271,7 @@ fn a_deleted_guide_hands_focus_on_without_selecting_the_next_guide() { let (shown, picked) = (Rc::clone(&guides), Rc::clone(&selected)); let mut fixture = ToolFixture::with_view("Document", Recorder::default(), view_at(1.0), move |ui, cx| { let mut list = shown.borrow_mut(); - let current: Vec = picked.borrow().iter().copied().collect(); // P3(canvas-w5): cv.F27 + let current: Vec = picked.borrow().iter().copied().collect(); let built = Guides::new("Guides", canvas_of(cx), Bind::bound(&mut *list)).interactive(true).selected_guides(¤t); if let Some(select) = built.show_at(ui, cx.rect).select { *picked.borrow_mut() = select; diff --git a/migration/api-map/craft-ui-canvas-controls.toml b/migration/api-map/craft-ui-canvas-controls.toml index a2997dd58..63c99b4d9 100644 --- a/migration/api-map/craft-ui-canvas-controls.toml +++ b/migration/api-map/craft-ui-canvas-controls.toml @@ -458,7 +458,7 @@ note = "The modifiers held come from the press of the guide's part." [[setter]] type = "Guides" old = "selected(Option)" -new = "selected_guide(GuideKey)" +new = "selected_guides(&[GuideKey])" [[setter]] type = "Guides"