diff --git a/crates/ui-egui/src/canvas/document_display.rs b/crates/ui-egui/src/canvas/document_display.rs index e0c6df2c2e..08e063d974 100644 --- a/crates/ui-egui/src/canvas/document_display.rs +++ b/crates/ui-egui/src/canvas/document_display.rs @@ -28,6 +28,10 @@ use photocraft_geom::Rect as DRect; #[path = "layer_edges_drawing_tests.rs"] mod layer_edges_drawing_tests; +#[cfg(test)] +#[path = "hand_scroll_all_windows_tests.rs"] +mod hand_scroll_all_windows_tests; + impl View { /// This view as the framework's canvas view of `rect` at `ppp` device pixels per point, mirrored by View › Flip /// Horizontal and stretched by View › Pixel Aspect Ratio Correction. @@ -146,7 +150,7 @@ pub(crate) fn view_follows_pointer(app: &PhotocraftApp, idx: usize) -> bool { crate::crop_ui::moves_view(app) && app.session.active_index() == Some(idx) } -/// The user's navigation choices from Preferences (General, Tools, Enhanced Controls). +/// The user's navigation preferences and Hand tool option. pub(crate) fn navigation(app: &PhotocraftApp) -> NavigationOptions { let p = app.session.prefs(); let mut options = NavigationOptions::default(); @@ -154,6 +158,7 @@ pub(crate) fn navigation(app: &PhotocraftApp) -> NavigationOptions { options.animated_zoom = p.general.animated_zoom; options.zoom_clicked_point_to_center = p.tools.zoom_clicked_point_to_center; options.flick_panning = p.tools.enable_flick_panning; + options.scroll_all_windows = app.ui.tool_options.hand_scroll_all; options.overscroll = p.tools.overscroll; options.pinch_zooms = p.enhanced_controls.zoom_with_trackpad_pinch; options.trackpad_rotate = p.enhanced_controls.rotate_view_with_trackpad; diff --git a/crates/ui-egui/src/canvas/hand_scroll_all_windows_tests.rs b/crates/ui-egui/src/canvas/hand_scroll_all_windows_tests.rs new file mode 100644 index 0000000000..15888b21cc --- /dev/null +++ b/crates/ui-egui/src/canvas/hand_scroll_all_windows_tests.rs @@ -0,0 +1,145 @@ +//! #12023 exercises the Hand options and gestures through the application window. + +use craft::ui::canvas::ToolHost; +use egui::accesskit::{Role, Toggled}; +use egui::{Event, Modifiers, PointerButton, Vec2, vec2}; +use egui_kittest::Harness; +use egui_kittest::kittest::{NodeT, Queryable}; +use serde_json::json; + +use super::CanvasHost; +use crate::state::{Tool, ToolOptions}; +use crate::{PhotocraftApp, Services}; + +type Window = Harness<'static, Box>; + +/// Two shown 3000 × 2000 documents, at 100% and 50%, with the second one active. +fn window() -> Window { + let mut window = Harness::builder().with_size(vec2(1400.0, 900.0)).with_pixels_per_point(1.0).build_eframe(|cc| { + PhotocraftApp::setup_context(&cc.egui_ctx, Default::default()); + let mut app = PhotocraftApp::with_context(cc.egui_ctx.clone(), photocraft_engine::Session::new(), Services::default()); + app.background_jobs = false; + for name in ["oracle", "second"] { + app.run("file.new", json!({"width": 3000, "height": 2000, "name": name})).unwrap(); + } + app.ui.tool = Tool::Hand; + app.session.prefs.edit(|prefs| { + prefs.tools.enable_flick_panning = false; + prefs.general.animated_zoom = false; + }); + app + }); + window.run_steps(6); + let ctx = window.ctx.clone(); + crate::menus::invoke(window.state_mut(), &ctx, "window.arrange.twoUpVertical", json!({})).unwrap(); + window.run_steps(6); + for (view, zoom) in window.state_mut().ui.views.iter_mut().zip([1.0, 0.5]) { + view.zoom = zoom; + view.center = [1500.0, 1000.0]; + view.clear_pending(); + } + window.run_steps(4); + assert_eq!(window.state().session.active_index(), Some(1)); + assert_eq!(window.ctx.pixels_per_point(), 1.0); + window +} + +fn cameras(window: &Window) -> Vec<(f32, [f32; 2])> { + window.state().ui.views.iter().map(|view| (view.zoom, view.center)).collect() +} + +fn history_lengths(window: &Window) -> Vec { + window.state().session.documents().iter().map(|state| state.history.entries().len()).collect() +} + +fn enable_scroll_all(window: &mut Window) { + let checkbox = window.get_by_role_and_label(Role::CheckBox, "Scroll All Windows"); + assert_eq!(checkbox.accesskit_node().toggled(), Some(Toggled::False)); + checkbox.click(); + window.run_steps(3); + assert!(window.state().ui.tool_options.hand_scroll_all); + assert_eq!(window.get_by_role_and_label(Role::CheckBox, "Scroll All Windows").accesskit_node().toggled(), Some(Toggled::True)); +} + +/// A fixed screen-space gesture, so following a changing camera cannot add a second movement. +fn drag_second(window: &mut Window, modifiers: Modifiers) { + let source = ToolHost::shown_space(&window.ctx, CanvasHost::main(1).canvas_id()).expect("the active tile is shown").view; + let start = source.rect.center(); + let delta = vec2(-100.0, -50.0); + window.event(Event::ModifiersChanged(modifiers)); + window.hover_at(start); + window.run_steps(2); + window.event(Event::PointerButton { pos: start, button: PointerButton::Primary, pressed: true, modifiers }); + window.run_steps(2); + for fraction in [0.25, 0.5, 0.75, 1.0] { + window.event(Event::ModifiersChanged(modifiers)); + window.hover_at(start + delta * fraction); + window.run_steps(1); + } + window.event(Event::PointerButton { pos: start + delta, button: PointerButton::Primary, pressed: false, modifiers }); + window.run_steps(3); + window.event(Event::ModifiersChanged(Modifiers::NONE)); + window.run_steps(1); +} + +fn assert_moved(before: (f32, [f32; 2]), after: (f32, [f32; 2]), expected: Vec2) { + assert_eq!(after.0, before.0, "a Hand drag keeps each view's zoom"); + let delta = vec2(after.1[0] - before.1[0], after.1[1] - before.1[1]); + assert!((delta - expected).length() < 1e-3, "expected {expected:?} document pixels, got {delta:?}"); +} + +#[test] +fn checkbox_is_saved_without_history_and_pans_both_views_in_image_pixels() { + let mut window = window(); + assert!(!window.state().ui.tool_options.hand_scroll_all); + let history = history_lengths(&window); + enable_scroll_all(&mut window); + let saved = serde_json::to_value(&*window.state().ui.tool_options).unwrap(); + let restored: ToolOptions = serde_json::from_value(saved).unwrap(); + assert!(restored.hand_scroll_all, "the option persists with the other tool options"); + assert_eq!(history_lengths(&window), history, "ticking the checkbox adds no history state"); + let before = cameras(&window); + drag_second(&mut window, Modifiers::NONE); + let after = cameras(&window); + assert_moved(before[1], after[1], vec2(200.0, 100.0)); + assert_moved(before[0], after[0], vec2(200.0, 100.0)); + assert_eq!(history_lengths(&window), history, "panning adds no history state"); +} + +#[test] +fn shift_drag_pans_both_views_by_the_same_image_distance() { + let mut window = window(); + let before = cameras(&window); + drag_second(&mut window, Modifiers::SHIFT); + let after = cameras(&window); + assert_moved(before[1], after[1], vec2(200.0, 100.0)); + assert_moved(before[0], after[0], vec2(200.0, 100.0)); + assert!(!window.state().ui.tool_options.hand_scroll_all); +} + +#[test] +fn checkbox_off_without_shift_pans_only_the_dragged_view() { + let mut window = window(); + let before = cameras(&window); + drag_second(&mut window, Modifiers::NONE); + let after = cameras(&window); + assert_moved(before[1], after[1], vec2(200.0, 100.0)); + assert_eq!(after[0], before[0]); +} + +#[test] +fn hand_zoom_buttons_change_only_the_active_view_with_scroll_all_on() { + for label in ["100%", "Fit Screen", "Fill Screen"] { + let mut window = window(); + enable_scroll_all(&mut window); + let before = cameras(&window); + let history = history_lengths(&window); + window.get_by_role_and_label(Role::Button, label).click(); + window.run_steps(5); + let after = cameras(&window); + assert_eq!(after[0], before[0], "{label} leaves the other view unchanged"); + assert_ne!(after[1].0, before[1].0, "{label} changes the active view's zoom"); + assert_eq!(history_lengths(&window), history, "{label} adds no history state"); + assert!(window.state().ui.tool_options.hand_scroll_all); + } +}