diff --git a/crates/behavior/src/keys.rs b/crates/behavior/src/keys.rs index 29761aa46..26a18cddb 100644 --- a/crates/behavior/src/keys.rs +++ b/crates/behavior/src/keys.rs @@ -26,8 +26,8 @@ pub struct KeyRole { /// The component reads arrow nudges: arrows are not moves. One that deletes (a canvas control) takes only the /// plain Delete and Backspace; with a modifier held they are the app's. pub nudge: bool, - /// A document canvas or a control on one (`KeyUse.chrome_key`): Page Up and Page Down with Cmd (Ctrl off macOS) - /// scroll it sideways, as Photoshop's do, instead of switching pages. + /// A canvas with a view or a stop in its body: Page Up and Page Down with Cmd (Ctrl off macOS) scroll the view + /// sideways, as Photoshop's do, instead of switching pages. pub document: bool, /// Typed letters reach the component as `Intent::Text` (type-ahead). pub type_ahead: bool, diff --git a/crates/draw/tests/it/bezier.rs b/crates/draw/tests/it/bezier.rs index 632f5b174..f05ebb2c4 100644 --- a/crates/draw/tests/it/bezier.rs +++ b/crates/draw/tests/it/bezier.rs @@ -1,6 +1,6 @@ //! `BezierOverlay`: the pure path operations, and the overlay's pointer, keyboard and tools through the public API. -use craft_behavior::kurbo::{ParamCurve as _, Point, Vec2}; +use craft_behavior::kurbo::{ParamCurve as _, Point, Size, Vec2}; use craft_behavior::{CanvasView, EditEvent}; use craft_draw::ui::{ AnchorsShown, BezierHit, BezierOverlay, BezierTool, EditPath, EditSubpath, HandleSide, HandlesShown, Knot, KnotRef, PathEdit, PathSelection, @@ -156,6 +156,8 @@ struct State { segment_mark: SegmentMark, rotation: f64, flip: bool, + /// The canvas shows a document and keeps the view it reports, so page keys move it. + pans: bool, view: Option, id: Id, seen: Vec, @@ -204,6 +206,10 @@ fn kind(event: &EditEvent>) -> Seen { } fn harness(paths: Vec) -> Harness { + harness_with(paths, false) +} + +fn harness_with(paths: Vec, pans: bool) -> Harness { let state = Rc::new(RefCell::new(State { paths, selection: PathSelection::default(), @@ -214,6 +220,7 @@ fn harness(paths: Vec) -> Harness { segment_mark: SegmentMark::HandlesOnly, rotation: 0.0, flip: false, + pans, view: None, id: Id::NULL, seen: Vec::new(), @@ -229,10 +236,20 @@ fn harness(paths: Vec) -> Harness { let mut held = shown.borrow_mut(); let st = &mut *held; let room = ui.available_rect_before_wrap(); - let view = CanvasView::new(room, p(f64::from(room.width()) / 2.0, f64::from(room.height()) / 2.0), 1.0).rotation(st.rotation).flip_x(st.flip); + let view = match st.view.filter(|_| st.pans) { + Some(mut kept) => { + kept.rect = room; + kept + } + None => { + CanvasView::new(room, p(f64::from(room.width()) / 2.0, f64::from(room.height()) / 2.0), 1.0).rotation(st.rotation).flip_x(st.flip) + } + }; st.view = Some(view); let selection = st.selection.clone(); - let canvas = Canvas::new("Document").view(view).show(ui, |ui, body| { + let canvas = Canvas::new("Document").view(view); + let canvas = if st.pans { canvas.document(Size::new(4000.0, 4000.0)) } else { canvas }; + let canvas = canvas.show(ui, |ui, body| { // The guides lie under the paths: a stack position hides the levels of the ones below it. if !st.guides.is_empty() { Guides::new("Guides", Bind::bound(&mut st.guides)).interactive(true).show_at(ui, body.rect); @@ -247,6 +264,9 @@ fn harness(paths: Vec) -> Harness { if canvas.output.click_at().is_some() { st.tool_clicks += 1; } + if st.pans { + st.view = canvas.output.view.or(st.view); + } let Some(output) = canvas.inner else { return }; st.id = output.edit.response.id; st.seen.extend(output.edit.events.iter().map(kind)); @@ -358,6 +378,24 @@ fn hit_test_matches_the_overlays_press_priority() { // Arrows, select all and Escape belong to Direct, Convert and Curvature; Add, Delete and Auto Add/Delete have none. +#[trial] +fn cmd_or_ctrl_with_page_down_on_a_focused_anchor_scrolls_the_canvas_and_picks_no_part() { + let mut h = harness_with(vec![path_a(), path_b()], true); + // The canvas, then the overlay; Home picks the first anchor of path 1. + h.fixture + .key(egui::Key::Tab, egui::Modifiers::NONE) + .key(egui::Key::Tab, egui::Modifiers::NONE) + .key(egui::Key::Home, egui::Modifiers::NONE) + .settle(); + let picked = h.state.borrow().selection.clone(); + assert!(!picked.anchors.is_empty(), "Home picks an anchor"); + let before = h.state.borrow().view.expect("the canvas showed").center; + h.fixture.key(egui::Key::PageDown, egui::Modifiers::COMMAND).settle(); + let after = h.state.borrow().view.expect("the canvas showed").center; + assert!(after.x > before.x && after.y == before.y, "the overlay leaves Cmd/Ctrl+Page Down to the canvas: {before:?} -> {after:?}"); + assert_eq!(h.state.borrow().selection, picked, "the page key picks no part"); +} + #[trial] fn insert_on_a_picked_anchor_adds_one_halfway_along_the_segment_leaving_it() { let mut h = two_paths(); diff --git a/crates/ui-canvas/src/canvas_controls/guides/mod.rs b/crates/ui-canvas/src/canvas_controls/guides/mod.rs index 50c1ea2c1..dee09cbce 100644 --- a/crates/ui-canvas/src/canvas_controls/guides/mod.rs +++ b/crates/ui-canvas/src/canvas_controls/guides/mod.rs @@ -351,7 +351,8 @@ impl<'a> Guides<'a> { let step = if mods.shift { self.big_step } else { self.step }; position += place::nudge_sign(&space.view, guide.along, direction) * step; } - while let Some((mv, _)) = cx.intents().take_move() { + // Only the steps along the guide; the page and end moves go on to the canvas, which pages its view. + while let Some((mv, _)) = cx.intents().take_move_where(|mv, _| matches!(mv, NavMove::Forward | NavMove::Back)) { match mv { NavMove::Forward => position += self.step, NavMove::Back => position -= self.step, diff --git a/crates/ui-canvas/tests/it/canvas/navigate.rs b/crates/ui-canvas/tests/it/canvas/navigate.rs index 18771414f..2ea53772a 100644 --- a/crates/ui-canvas/tests/it/canvas/navigate.rs +++ b/crates/ui-canvas/tests/it/canvas/navigate.rs @@ -1,11 +1,10 @@ //! Navigation rules (wheel, page keys, flick, autoscroll), the zoom motion and the momentary tool order. -use craft_behavior::geometry::CropBox; -use craft_behavior::kurbo::Rect; use craft_behavior::kurbo::{Point, Size}; -use craft_behavior::{CanvasView, Wheel, WheelPhase, WheelUnit}; +use craft_behavior::{Axis, CanvasView, Wheel, WheelPhase, WheelUnit}; use craft_keymap::{Mods, Platform}; -use craft_ui::canvas::{Canvas, CropOverlay, Momentary, MomentaryCommands, MomentaryTool, NavigationOptions, TouchpadGlide}; +use craft_ui::canvas::{Canvas, Guide, GuideKey, Momentary, MomentaryCommands, MomentaryTool, NavigationOptions, TouchpadGlide}; +use craft_ui::canvas_controls::Guides; use craft_ui::contract::{Bind, Show as _, ShowContainer as _}; use craft_ui::runtime::access::Role; use craft_ui::runtime::commands::FirePhase; @@ -82,23 +81,27 @@ fn cmd_or_ctrl_with_page_down_scrolls_a_focused_canvas_sideways_on_every_os() { fn cmd_or_ctrl_with_page_down_on_a_focused_canvas_control_scrolls_the_canvas_sideways() { let view = Rc::new(Cell::new(CanvasView::new(egui::Rect::ZERO, Point::new(2000.0, 2000.0), 1.0))); let shown = Rc::clone(&view); - let mut crop = CropBox::upright(Rect::new(1900.0, 1900.0, 2100.0, 2100.0)); + // A horizontal guide through the document's centre, where the view starts: it stays in view as the view moves + // sideways. Its use sets no document canvas key. + let mut guides = vec![Guide::new(GuideKey::new(1), Axis::X, 2000.0)]; let mut fixture = Fixture::new(move |ui| { let mut given = shown.get(); given.rect = ui.available_rect_before_wrap(); let canvas = Canvas::new("Document") .document(Size::new(4000.0, 4000.0)) .view(given) - .show(ui, |ui, body| CropOverlay::new("Crop", Bind::bound(&mut crop)).show_at(ui, body.rect)); + .show(ui, |ui, body| Guides::new("Guides", Bind::bound(&mut guides)).interactive(true).show_at(ui, body.rect)); shown.set(canvas.output.view.unwrap_or(given)); }); fixture.settle(); - // The crop box lies around the canvas's centre: a click inside it focuses the crop. let canvas = fixture.by_role(Role::CANVAS)[0]; fixture.click(&canvas); - assert!(fixture.focused_stop().is_some_and(|(id, _)| Some(id) != fixture.root_id()), "the click focuses the crop, not the canvas"); + let guide = fixture.focused_stop(); + assert!(guide.is_some_and(|(id, part)| Some(id) != fixture.root_id() && part.is_some()), "the click focuses the guide, not the canvas"); + assert!(fixture.focused_role().is_some_and(|role| role.document), "a stop in the body of a canvas with a view is on a document"); let before = view.get().center; fixture.key(Key::PageDown, Modifiers::COMMAND).settle(); let sideways = view.get().center; - assert!(sideways.x > before.x && sideways.y == before.y, "the crop leaves Cmd/Ctrl+Page Down to the canvas: {before:?} -> {sideways:?}"); + assert!(sideways.x > before.x && sideways.y == before.y, "the guide leaves Cmd/Ctrl+Page Down to the canvas: {before:?} -> {sideways:?}"); + assert_eq!(fixture.focused_stop(), guide, "focus stays on the guide"); } diff --git a/crates/ui-contract/src/contract/body/hooks.rs b/crates/ui-contract/src/contract/body/hooks.rs index 06f078386..5cfa7eb31 100644 --- a/crates/ui-contract/src/contract/body/hooks.rs +++ b/crates/ui-contract/src/contract/body/hooks.rs @@ -112,7 +112,8 @@ impl ShapeHooks for BodyHooks<'_> { } fn key_role(&self, comp: &C) -> KeyRole { - KeyRole::new().reorderable(comp.props(Internal::new()).header.is_some_and(|header| header.band == DragBand::Reorder)) + let props = comp.props(Internal::new()); + KeyRole::new().reorderable(props.header.is_some_and(|header| header.band == DragBand::Reorder)).document(props.scroll_owner) } fn position(&self, comp: &C) -> Option { diff --git a/crates/ui-contract/src/contract/body/inherit.rs b/crates/ui-contract/src/contract/body/inherit.rs index 1eb9ab60e..df2743747 100644 --- a/crates/ui-contract/src/contract/body/inherit.rs +++ b/crates/ui-contract/src/contract/body/inherit.rs @@ -4,7 +4,7 @@ use super::BodyHooks; use super::reorder::REORDER_SCOPE; use crate::contract::{Container, ContainerProps, Cx, DragBand, Mount, PaneEdge, Scroll, Sizing}; use crate::runtime::commands::{self, CommandSet as _, WindowCommand}; -use crate::runtime::env::{AppValues, ArenaId, Scoped}; +use crate::runtime::env::{AppValues, ArenaId, Scoped, ScrollOwner}; use crate::style::{ComponentStyle as _, SurfaceKind}; use craft_i18n::Direction; use egui::{Context, Id, Vec2}; @@ -106,7 +106,7 @@ impl BodyHooks<'_> { (offered - label_width).max(0.0) }); if scroll.is_some() || props.scroll_owner { - scoped.scroll_owner = Some(id); + scoped.scroll_owner = Some(ScrollOwner { id, view: scroll.is_none() }); } match scroll { Some(scroll) => scoped.unbounded = (scroll.horizontal_on(), scroll.vertical_on()), diff --git a/crates/ui-contract/src/contract/run.rs b/crates/ui-contract/src/contract/run.rs index 800b93bf7..7d1f5b812 100644 --- a/crates/ui-contract/src/contract/run.rs +++ b/crates/ui-contract/src/contract/run.rs @@ -673,7 +673,9 @@ fn stops_step(at: StopsStep<'_>, e: &mut dyn ErasedPass<'_>, facts: &TypeFacts, let resettable = !facts.text() && resets; let text = holder_widget.is_some(); let keys = text_keys.unwrap_or_else(|| e.keys()); - let role = e.key_role().nudge(keys.nudge).document(keys.chrome_key); + // A stop in the body of a canvas with a view is on a document; the canvas's own role says it itself. + let role = e.key_role().nudge(keys.nudge); + let role = role.document(role.document || env.in_view_body()); let role = role.type_ahead(role.type_ahead || keys.type_ahead); // A bound component that keeps the arrows reads them as value steps; a text value sets `steps_value` in its text options. let arrows = keys.keep == crate::runtime::focus::KeepKeys::BlockArrows; diff --git a/crates/ui-contract/src/runtime/env.rs b/crates/ui-contract/src/runtime/env.rs index 8cbb4fc44..f29e8a54b 100644 --- a/crates/ui-contract/src/runtime/env.rs +++ b/crates/ui-contract/src/runtime/env.rs @@ -133,7 +133,7 @@ pub struct Scoped { pub(crate) offered: Option, /// The innermost scrolling body that encloses the scope: a collection under it owns no scroll of its own, and /// page and end moves a component leaves untaken go to it. - pub(crate) scroll_owner: Option, + pub(crate) scroll_owner: Option, /// The axes (horizontal, vertical) the innermost scrolling body scrolls on: the room along them is unbounded, so /// a child that fills there takes its content size. pub(crate) unbounded: (bool, bool), @@ -245,7 +245,22 @@ pub struct ItemFacts { pub menu_open: bool, } +/// The innermost body that owns the scroll of a scope (`Scoped::scroll_owner`). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) struct ScrollOwner { + pub(crate) id: Id, + /// The owner shows its body through a view of its own (a canvas with a view, `ContainerProps::scroll_owner`), + /// not through a scroll region: a stop in the body is on a document, with its key role. + pub(crate) view: bool, +} + impl Scoped { + /// Whether the scope is the body of a container with a view of its own, as the innermost scroll owner: its stops + /// take the document key role (`KeyRole::document`). + pub(crate) fn in_view_body(&self) -> bool { + self.scroll_owner.is_some_and(|owner| owner.view) + } + /// The height, icon side, padding and gap of a control of `size`, with the line ratio of this scope. pub(crate) fn control(&self, size: craft_theme::ControlSize) -> craft_behavior::ControlMetrics { crate::style::control_metrics(&self.tokens, size, self.line_ratio) diff --git a/crates/ui-contract/src/runtime/env/view.rs b/crates/ui-contract/src/runtime/env/view.rs index 8193fd175..bb36e6e98 100644 --- a/crates/ui-contract/src/runtime/env/view.rs +++ b/crates/ui-contract/src/runtime/env/view.rs @@ -40,7 +40,7 @@ impl<'a> EnvView<'a> { /// The innermost scrolling body around the component. pub(crate) fn scroll_owner(&self) -> Option { - self.scoped.scroll_owner + self.scoped.scroll_owner.map(|owner| owner.id) } /// The view of the innermost scroll area around the scope. diff --git a/crates/ui-contract/src/runtime/focus/policy.rs b/crates/ui-contract/src/runtime/focus/policy.rs index 59ec25a76..4cee37c30 100644 --- a/crates/ui-contract/src/runtime/focus/policy.rs +++ b/crates/ui-contract/src/runtime/focus/policy.rs @@ -269,7 +269,8 @@ pub struct KeyUse { /// shortcut never shares them. A control that does not (a button) leaves them to the commands around the focus. pub moves: bool, /// The component is a document canvas: a window that registers one this pass shows a document, and a bare Tab - /// toggles that window's panels with nothing focused or the canvas focused (`window.togglePanels`). + /// toggles that window's panels with nothing focused or the canvas focused (`window.togglePanels`). The page keys' + /// document role comes from the canvas's view instead (`ContainerProps::scroll_owner`), for its body's stops too. pub chrome_key: bool, /// The arrows are nudges for the component's content (a canvas tool or control), not moves: they arrive as /// `Intents::take_nudge`.