diff --git a/crates/ui-canvas/src/canvas/navigator/preview.rs b/crates/ui-canvas/src/canvas/navigator/preview.rs index b11216082..22cb170c9 100644 --- a/crates/ui-canvas/src/canvas/navigator/preview.rs +++ b/crates/ui-canvas/src/canvas/navigator/preview.rs @@ -113,8 +113,9 @@ pub struct NavigatorStyle { /// - With Cmd (macOS) or Ctrl held at the press, the drag draws a rectangle and the release zooms the view to it. /// - The wheel zooms about the view centre, one notch at a time, whether or not Zoom with Scroll Wheel is on; a pinch /// zooms the same way when the navigation options allow it. -/// - Focused, the arrow keys move the view by a tenth of its span (Shift: half); Page Up, Page Down, Home and End -/// scroll as on the canvas ([`PageKey`]); `+` and `-` step the zoom ladder, and a step past a limit changes nothing. +/// - Focused, the arrow keys move the view by a tenth of its span (Shift: half); Page Up and Page Down scroll as on +/// the canvas ([`PageKey`]); Home centres the document; `+` and `-` step the zoom ladder, and a step past a limit +/// changes nothing. /// - With Animated Zoom on, a zoom (the wheel, `+` and `-`, the zoom rectangle) eases on the canvas whose view the /// navigator edits: the bound view takes the target, and the canvas that the view commands reach (the one the zoom /// row's Zoom In steps) shows the way there in its one zoom ease. @@ -229,6 +230,10 @@ fn take_navigation(cx: &mut Cx<'_, Navigator<'_>>, next: &mut CanvasView, runnin NavMove::Forward => delta.x += side * span.x * share, NavMove::Up => delta.y -= span.y * share, NavMove::Down => delta.y += span.y * share, + NavMove::Start if !mods.extend && !mods.lead && !mods.fine => { + *next = centred(next, Point::new(document.width / 2.0, document.height / 2.0), document); + } + NavMove::Start | NavMove::End => {} _ => { if let Some(key) = PageKey::of(mv, mods, span) { key.apply(next, document); diff --git a/crates/ui-canvas/tests/it/canvas/mod.rs b/crates/ui-canvas/tests/it/canvas/mod.rs index 9bdcb4d28..8c05eb1a2 100644 --- a/crates/ui-canvas/tests/it/canvas/mod.rs +++ b/crates/ui-canvas/tests/it/canvas/mod.rs @@ -4,6 +4,8 @@ mod crop; mod navigate; +mod navigator; + mod opacity_keys; mod pasteboard; diff --git a/crates/ui-canvas/tests/it/canvas/navigator.rs b/crates/ui-canvas/tests/it/canvas/navigator.rs new file mode 100644 index 000000000..46f2054d5 --- /dev/null +++ b/crates/ui-canvas/tests/it/canvas/navigator.rs @@ -0,0 +1,32 @@ +//! The Navigator's preview keys: Home centres the document. + +use craft_behavior::CanvasView; +use craft_behavior::kurbo::{Point, Size}; +use craft_ui::canvas::Navigator; +use craft_ui::contract::{Bind, Show as _}; +use craft_ui::testing::{Fixture, trial}; +use egui::{Key, Modifiers, Rect, pos2, vec2}; +use std::cell::Cell; +use std::rc::Rc; + +const DOCUMENT: Size = Size::new(2000.0, 1500.0); + +#[trial] +fn home_on_the_focused_preview_centres_the_document_and_end_leaves_the_view() { + let canvas = Rect::from_min_size(pos2(0.0, 0.0), vec2(800.0, 600.0)); + let view = Rc::new(Cell::new(CanvasView::new(canvas, Point::new(400.0, 300.0), 1.0))); + let shown = Rc::clone(&view); + let mut fixture = Fixture::new(move |ui| { + let mut given = shown.get(); + let _ = Navigator::new("Navigator", Bind::bound(&mut given), DOCUMENT).show(ui); + shown.set(given); + }); + fixture.settle(); + fixture.key(Key::Tab, Modifiers::NONE); + assert!(fixture.focused_stop().is_some(), "Tab reaches the preview"); + let before = view.get().center; + fixture.key(Key::End, Modifiers::NONE); + assert_eq!(view.get().center, before, "End leaves the view where it is"); + fixture.key(Key::Home, Modifiers::NONE); + assert_eq!(view.get().center, Point::new(DOCUMENT.width / 2.0, DOCUMENT.height / 2.0), "Home centres the document"); +}