diff --git a/crates/draw/src/ui/crop/tool.rs b/crates/draw/src/ui/crop/tool.rs index d39356acd..4b6308fd0 100644 --- a/crates/draw/src/ui/crop/tool.rs +++ b/crates/draw/src/ui/crop/tool.rs @@ -26,11 +26,9 @@ use super::words::CropWords; /// The key of the Crop To... command form. const CROP_TO_KEY: &str = "draw.crop.cropTo"; -/// The chords the tool takes while a finite frame stands and no drag runs: Front Image, Swap, the -/// overlay and its orientation. Every other key stays the app's. -const FRAME_KEYS: &[Chord] = &[Chord::plain(Key::I), Chord::plain(Key::X), Chord::plain(Key::O), Chord::new(Mods::SHIFT, Key::O)]; - -/// [`FRAME_KEYS`] and Show Cropped Area and Classic Mode, which act only while a crop is pending. +/// The chords the tool takes while a crop is pending and no drag runs: Front Image, Swap, the overlay and its +/// orientation, Show Cropped Area and Classic Mode. Over the untouched default frame (also a selection's) every key +/// stays the app's, as in Photoshop: O and I pick their tools, X swaps the colours. const PENDING_KEYS: &[Chord] = &[Chord::plain(Key::I), Chord::plain(Key::X), Chord::plain(Key::O), Chord::new(Mods::SHIFT, Key::O), Chord::plain(Key::H), Chord::plain(Key::P)]; @@ -182,8 +180,9 @@ impl<'a> CropInput<'a> { /// picked part when one has focus) 1 unit, Shift 10 (in image mode they move the image). While the /// canvas has the keyboard: X swaps portrait and landscape (and W with H in W x H x Resolution); O /// cycles the overlay, Shift+O its orientation, H toggles Show Cropped Area (taken but inert in -/// Classic Mode), P toggles Classic Mode; I is Front Image. The tool keeps the plain keys only while a -/// frame exists and no drag runs. +/// Classic Mode), P toggles Classic Mode; I is Front Image. The tool keeps these keys only while a crop +/// is pending (a frame drawn or changed) and no drag runs: over the untouched default frame they stay +/// the app's, as in Photoshop. /// - Straighten: [`toggle_straighten`](Self::toggle_straighten) (the bar's button) arms one line; /// Cmd (Ctrl) held is a temporary Straighten. /// - Box Undo and Redo: the app routes Edit > Undo and Redo to [`undo`](Self::undo) and @@ -709,14 +708,14 @@ impl CanvasTool for CropToolPass<'_> { } } - /// The chords the tool takes while a finite frame stands and no gesture runs: I, X, O and Shift+O, and H - /// and P while a crop is pending. Every other key stays the app's. + /// The chords the tool takes while a crop is pending with a finite frame and no gesture runs: I, X, O, + /// Shift+O, H and P. Every other key, and every key over the untouched default frame, stays the app's. fn claims(&self) -> ToolClaims { - let open = !self.tool.dragging && self.tool.session.frame().is_some_and(|frame| frame.is_finite()); + let open = !self.tool.dragging && self.tool.session.pending() && self.tool.session.frame().is_some_and(|frame| frame.is_finite()); if !open { return ToolClaims::NONE; } - ToolClaims::NONE.keys(if self.tool.session.pending() { PENDING_KEYS } else { FRAME_KEYS }) + ToolClaims::NONE.keys(PENDING_KEYS) } /// A press that reaches the tool is on no part of the frame: the overlay owns the whole canvas. @@ -730,28 +729,26 @@ impl CanvasTool for CropToolPass<'_> { fn cancel(&mut self, _: CancelReason, _: &mut ToolContext<'_>) {} - /// O, Shift+O, H, P, X and I as the type's doc says; the frame's overlay takes Enter, Esc and the arrows before - /// the tool. Returns `false` for a key the tool leaves to the app. + /// O, Shift+O, H, P, X and I as the type's doc says, only while a crop is pending; the frame's overlay takes + /// Enter, Esc and the arrows before the tool. Returns `false` for a key the tool leaves to the app. fn key(&mut self, key: &ToolKey, cx: &mut ToolContext<'_>) -> bool { - if self.tool.dragging { + if self.tool.dragging || !self.tool.session.pending() { return false; } + let Some(frame) = self.tool.session.frame().filter(CropBox::is_finite) else { return false }; let (settings, document) = (self.input.settings, self.input.document); - let frame = self.tool.session.frame().filter(CropBox::is_finite); - let pending = self.tool.session.pending(); let mut next = settings.clone(); match key { ToolKey::Kept(chord) => { let plain = chord.mods.is_none(); match chord.key { Key::I if plain => next.front_image(document), - Key::O if frame.is_some() && plain => next.cycle_guide(), - Key::O if frame.is_some() && chord.mods == Mods::SHIFT => next.cycle_orientation(), + Key::O if plain => next.cycle_guide(), + Key::O if chord.mods == Mods::SHIFT => next.cycle_orientation(), // Show Cropped Area is taken but inert in Classic Mode, where it acts as on. - Key::H if frame.is_some() && pending && plain => next.shield.show_cropped_area ^= next.image_mode(), - Key::P if frame.is_some() && pending && plain => next.shield.classic_mode = !next.shield.classic_mode, + Key::H if plain => next.shield.show_cropped_area ^= next.image_mode(), + Key::P if plain => next.shield.classic_mode = !next.shield.classic_mode, Key::X if plain => { - let Some(frame) = frame else { return false }; self.swap(frame, cx); return true; } diff --git a/crates/draw/tests/it/crop_tool.rs b/crates/draw/tests/it/crop_tool.rs index 3bbf5e4bf..e615b8a40 100644 --- a/crates/draw/tests/it/crop_tool.rs +++ b/crates/draw/tests/it/crop_tool.rs @@ -105,6 +105,8 @@ struct Host { past_edge_passes: usize, /// The passes whose host response carried snap lines. snap_passes: usize, + /// The canvas's id, from its last pass. + canvas: Option, } struct Rig { @@ -135,6 +137,7 @@ impl Rig { snap: None, past_edge_passes: 0, snap_passes: 0, + canvas: None, })); let shown = Rc::clone(&host); let mut fixture = Fixture::new(move |ui| { @@ -157,7 +160,7 @@ impl Rig { let tool = &mut host.tool; let (ctx, settings, document) = (&host.ctx, &host.settings, &host.document); let mut hosted = None; - let _ = canvas.show(ui, |ui, body| { + let output = canvas.show(ui, |ui, body| { let input = CropInput::new(ctx, settings, document); let frame = showing.then(|| tool.show_frame(ui, body.rect, &input)); if let Some(canvas) = tool_pass(&body.env) { @@ -166,6 +169,7 @@ impl Rig { hosted = Some((response, pass.finish().events)); } }); + host.canvas = Some(output.response.id); let (response, events) = hosted.unwrap_or_default(); host.past_edge_passes += usize::from(response.past_edge.is_some()); host.snap_passes += usize::from(!response.snapped.is_empty()); @@ -200,6 +204,14 @@ impl Rig { self.host.borrow().tool.frame() } + /// Gives the canvas the keyboard without a press, as a document window the app shows has it. + fn focus_canvas(&mut self) { + let canvas = self.host.borrow().canvas.expect("the canvas ran"); + craft_ui::testing::wheel::focus(self.fixture.ctx(), canvas); + self.run(); + assert_eq!(self.fixture.focused(), Some(canvas), "the canvas has the keyboard"); + } + /// The turn of the view the canvas shows, in degrees; `None` before the tool set one. fn view_turn(&self) -> Option { self.host.borrow().view.map(|view| view.rotation) @@ -355,15 +367,51 @@ fn the_default_frame_fits_the_ratio_and_follows_the_selection() { } #[trial] -fn p_and_h_belong_to_the_crop_only_while_a_frame_is_pending() { - // The untouched default frame leaves P and H to the app. +fn the_crop_keys_belong_to_the_crop_only_while_a_frame_is_pending() { + // The untouched default frame (400 x 300, so X would turn it) leaves every key to the app, as Photoshop does: + // P, H, O and I pick their tools, Shift+O cycles a tool group, X swaps the colours (photocraft#6536). let mut rig = Rig::new(classic()); rig.run(); + rig.focus_canvas(); + let before = rig.host.borrow().settings.clone(); rig.host.borrow_mut().events.clear(); - rig.fixture.key(Key::P, Modifiers::NONE); - rig.fixture.key(Key::H, Modifiers::NONE); + let keys = [ + (Key::P, Modifiers::NONE), + (Key::H, Modifiers::NONE), + (Key::O, Modifiers::NONE), + (Key::O, Modifiers::SHIFT), + (Key::I, Modifiers::NONE), + (Key::X, Modifiers::NONE), + ]; + for (key, mods) in keys { + rig.fixture.key(key, mods); + rig.run(); + let host = rig.host.borrow(); + assert!(host.events.is_empty(), "{key:?} with {mods:?} was the crop's: {:?}", host.events); + assert_eq!(host.settings, before, "{key:?} with {mods:?} left the options alone"); + assert!(host.tool.session().is_default(), "{key:?} with {mods:?} left the untouched default frame"); + } + + // A pending frame takes O (the overlay), I (Front Image) and X (portrait and landscape). + rig.drag((100.0, 80.0), (300.0, 220.0)); + rig.run(); + assert!(rig.host.borrow().tool.session().pending()); + rig.fixture.key(Key::O, Modifiers::NONE); + rig.run(); + assert_ne!(rig.host.borrow().settings.guide, before.guide, "O cycles the overlay"); + rig.fixture.key(Key::I, Modifiers::NONE); + rig.run(); + assert_ne!(rig.host.borrow().settings.ratio, before.ratio, "I is Front Image"); + let drawn = rig.frame().expect("a frame").rect.abs(); + rig.fixture.key(Key::X, Modifiers::NONE); + rig.run(); + let swapped = rig.frame().expect("a frame").rect.abs(); + let turned = (swapped.width() - drawn.height()).abs() < 1.0 && (swapped.height() - drawn.width()).abs() < 1.0; + assert!(turned, "X swaps {drawn:?} into {swapped:?}"); + + // P and H on a pending frame. + let mut rig = Rig::new(classic()); rig.run(); - assert!(!rig.host.borrow().events.iter().any(|event| matches!(event, CropEvent::Settings(_))), "neither key was the crop's"); // A pending frame takes P (Classic Mode) and H (Show Cropped Area, inert in Classic Mode). rig.drag((100.0, 80.0), (300.0, 220.0));