diff --git a/crates/behavior/src/geometry/draft.rs b/crates/behavior/src/geometry/draft.rs index 506e027925..9c1525a2c8 100644 --- a/crates/behavior/src/geometry/draft.rs +++ b/crates/behavior/src/geometry/draft.rs @@ -28,7 +28,8 @@ pub enum DraftKind { /// again; releasing ⌥ with the button up closes. ⌥ held at the press means subtract (the /// tool's operation), not straight segments, until it is released and pressed again; with /// nothing selected it starts straight segments at once. ⇧ puts each clicked vertex on the - /// horizontal, vertical or 45° line through the previous one, and holds a freehand drag to one such line. + /// horizontal, vertical or 45° line through the previous one, and a draft built with + /// [`locking_shift_drags`](OutlineDraft::locking_shift_drags) (the Freeform Pen) holds a freehand drag to one such line. #[default] Mixed, /// The Polygonal Lasso: each click adds a vertex, ⇧ (without ⌥) puts it in the nearest of @@ -152,6 +153,9 @@ pub struct OutlineDraft { document: Option, /// A ⇧ freehand drag's line: where it began and the unit direction it locked to (the nearest of eight). lock: Option<(Point, Vec2)>, + /// Whether ⇧ holds a freehand drag to one line (the Freeform Pen); the Lasso's ⇧ at the press adds to the + /// selection and its drag stays free. + locks_shift_drag: bool, } impl OutlineDraft { @@ -168,9 +172,16 @@ impl OutlineDraft { alt_from_press: false, document: None, lock: None, + locks_shift_drag: false, } } + /// This draft with ⇧ holding a freehand drag to one of eight directions (see [`DraftKind::Mixed`]). + pub fn locking_shift_drags(mut self) -> Self { + self.locks_shift_drag = true; + self + } + /// The rules this draft follows. pub fn kind(&self) -> DraftKind { self.kind @@ -274,12 +285,12 @@ impl OutlineDraft { Some(DraftEvent::Changed) } - /// Where a freehand point of `at` goes. With ⇧ held a Mixed draft's drag follows one line: once the pointer has + /// Where a freehand point of `at` goes. With ⇧ held a [`locking_shift_drags`](Self::locking_shift_drags) Mixed draft's drag follows one line: once the pointer has /// travelled [`LOCK_DISTANCE`] screen points from the last vertex, the nearest of eight directions from there /// is locked and every later point is the pointer's projection onto it, however the pointer wanders, until ⇧ is /// released. Before the lock the point stays at the vertex. Otherwise [`shift_step`](Self::shift_step). fn drag_step(&mut self, at: DraftPoint) -> Point { - if self.kind != DraftKind::Mixed || !at.mods.shift || self.polygonal { + if !self.locks_shift_drag || self.kind != DraftKind::Mixed || !at.mods.shift || self.polygonal { self.lock = None; return self.shift_step(at); } diff --git a/crates/behavior/tests/geometry/draft.rs b/crates/behavior/tests/geometry/draft.rs index f26dd85248..2aec75ab61 100644 --- a/crates/behavior/tests/geometry/draft.rs +++ b/crates/behavior/tests/geometry/draft.rs @@ -72,7 +72,7 @@ fn shift_steps_keep_the_shorter_diagonal_offset() { #[test] fn shift_holds_a_freehand_drag_to_one_line() { let shift = |x: f64, y: f64| DraftPoint::new(Point::new(x, y), Mods::SHIFT, 1.0); - let mut draft = OutlineDraft::new(DraftKind::Mixed, REACH); + let mut draft = OutlineDraft::new(DraftKind::Mixed, REACH).locking_shift_drags(); draft.press(at(10.0, 60.0, 1.0), true); for (x, y) in [(30.0, 63.0), (50.0, 55.0), (80.0, 68.0), (100.0, 52.0)] { draft.drag(shift(x, y)); diff --git a/crates/keymap/src/import.rs b/crates/keymap/src/import.rs index 13f031e579..66bb172d08 100644 --- a/crates/keymap/src/import.rs +++ b/crates/keymap/src/import.rs @@ -639,14 +639,12 @@ pub fn plan(set: &ForeignSet, keymap: &Keymap, options: &ImportOptions) -> Impor } } - // A key the command already has by default stays its main key: the keys of the set follow the defaults in their order. + // The command's own main default key, when the set lists it, stays the main key; the set's other keys (default alternates included) keep their order. for p in &mut commands { let Some(home) = keymap.bindable(&p.command).map(|b| b.scope.clone()) else { continue }; let defaults = keymap.default_bindings(&p.command); - let rank = |b: &PlannedBinding| { - defaults.iter().position(|d| d.trigger == b.binding.trigger && d.scope_or(&home) == b.binding.scope_or(&home)).unwrap_or(usize::MAX) - }; - p.bindings.sort_by_key(rank); + let Some(main) = defaults.first() else { continue }; + p.bindings.sort_by_key(|b| !(main.trigger == b.binding.trigger && main.scope_or(&home) == b.binding.scope_or(&home))); } if !to_suggest.is_empty() { diff --git a/crates/keymap/tests/import.rs b/crates/keymap/tests/import.rs index 93e1383ed3..6aaa67047e 100644 --- a/crates/keymap/tests/import.rs +++ b/crates/keymap/tests/import.rs @@ -284,9 +284,9 @@ fn tool_keys_match_by_name_or_id_and_take_one_letter() { assert_eq!(unmatched, [("Copy", UnmatchedReason::NoMatch), ("Imaginary Tool", UnmatchedReason::NoMatch)]); } -/// A key the command has by default stays its main key, whichever order the set lists its keys in. +/// A command's main default key stays its main key, whichever order the set lists its keys in. #[test] -fn a_default_key_among_the_sets_keys_stays_first() { +fn the_main_default_key_among_the_sets_keys_stays_first() { let km = keymap(Platform::Mac); let set = ForeignSet::new(vec![win("File", &["Save"], &[("Default", "Ctrl+Alt+S"), ("Default", "Ctrl+S")])]); let planned = plan(&set, &km, &windows_options()); diff --git a/crates/text/src/ui/editor.rs b/crates/text/src/ui/editor.rs index efdd48af28..ddc86013a9 100644 --- a/crates/text/src/ui/editor.rs +++ b/crates/text/src/ui/editor.rs @@ -32,7 +32,7 @@ use craft_behavior::kurbo::Point; use craft_behavior::{ CancelReason, CanvasView, CursorIcon, CursorPriority, EditCommand, EditorInput, GesturePhase, Metrics, PressPhase, Sense, TextPos, }; -use craft_ui::canvas::canvas_space; +use craft_ui::canvas::{canvas_space, context_click}; use craft_ui::contract::{ Animate as _, Bind, BindMut, CommandSet as _, Commands as _, Common, Component, Cx, Edit as _, EditResponse, Focus as _, MeasureCx, Outcome, PaintUi, PartsCx, Pointer as _, Props as _, Remember as _, Role, @@ -300,6 +300,9 @@ pub struct TextEditorOutput { pub ended: Option, /// The Character panel command was pressed: the host shows or hides the panel. Set for the one pass. pub character_panel: bool, + /// Where a secondary press on the editor was released, for the host's context menu (the type edit menu, which acts + /// on the live edit). The editor's part takes the press, so the canvas's tool never sees it. Set for the one pass. + pub context: Option, } impl Outcome for TextEditorOutput { @@ -352,6 +355,7 @@ pub struct TextEditor<'a> { changed: bool, selection_changed: bool, character_panel: bool, + context: Option, ended: Option, } @@ -367,6 +371,7 @@ impl<'a> TextEditor<'a> { changed: false, selection_changed: false, character_panel: false, + context: None, ended: None, } .label(label) @@ -471,7 +476,7 @@ impl Component for TextEditor<'_> { } fn sense(&self) -> Sense { - Sense::CLICK | Sense::DRAG | Sense::FOCUS | Sense::TAKE_FOCUS + Sense::CLICK | Sense::DRAG | Sense::CONTEXT_CLICK | Sense::FOCUS | Sense::TAKE_FOCUS } fn measure(&mut self, _cx: &MeasureCx<'_, Self>) -> Vec2 { @@ -638,6 +643,7 @@ impl Component for TextEditor<'_> { self.changed = done.changed; self.selection_changed = selection != before; self.character_panel = character_panel; + self.context = context_click(&press); } fn paint(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>) { @@ -724,6 +730,7 @@ impl Component for TextEditor<'_> { selection_changed: self.selection_changed, edit: cx.edit_response(), character_panel: self.character_panel, + context: self.context, ended: self.ended, } } diff --git a/crates/text/tests/it/editor.rs b/crates/text/tests/it/editor.rs index abd349df49..f23eb21f19 100644 --- a/crates/text/tests/it/editor.rs +++ b/crates/text/tests/it/editor.rs @@ -92,6 +92,8 @@ struct Session { cancels: Rc>, /// Every session end the editor reported (`TextEditorOutput::ended`). ends: Rc>>, + /// Every context click the editor reported (`TextEditorOutput::context`). + contexts: Rc>>, /// The next pass asks egui for a second pass of its frame. discard: Rc>, } @@ -110,6 +112,8 @@ fn session_on(host: Host, quotes: Option, beside: bool) -> Session { let (shown_cancels, shown_discard) = (Rc::clone(&cancels), Rc::clone(&discard)); let ends = Rc::new(RefCell::new(Vec::new())); let shown_ends = Rc::clone(&ends); + let contexts = Rc::new(RefCell::new(Vec::new())); + let shown_contexts = Rc::clone(&contexts); let mut other = (Host { text: "xyz".to_owned(), ..Host::default() }, TextSelection::caret_at(0)); let mut fixture = Fixture::new(move |ui| { if shown_discard.take() { @@ -128,6 +132,7 @@ fn session_on(host: Host, quotes: Option, beside: bool) -> Session { shown_commits.set(shown_commits.get() + commits); let cancels = output.edit.events.iter().filter(|event| matches!(event, craft_behavior::EditEvent::Cancel { .. })).count() as u32; shown_cancels.set(shown_cancels.get() + cancels); + shown_contexts.borrow_mut().extend(output.context); shown_ends.borrow_mut().extend(output.ended); if beside { let (host, selection) = (&mut other.0, &mut other.1); @@ -136,7 +141,7 @@ fn session_on(host: Host, quotes: Option, beside: bool) -> Session { }); }); fixture.settle(); - Session { fixture, host, selection, commits, cancels, ends, discard } + Session { fixture, host, selection, commits, cancels, ends, contexts, discard } } impl Session { @@ -188,6 +193,23 @@ fn a_primary_press_on_the_canvas_background_commits_the_session_with_its_marked_ assert_eq!(editor.text(), "abcdkzk", "the marked text stays as typed"); } +/// A secondary click on the text is reported once as a context click at its release, and neither moves the caret, +/// selects nor ends the session: the host's menu acts on the live edit. +#[trial] +fn a_secondary_click_on_the_text_reports_a_context_click_and_starts_no_drag() { + let mut editor = session("abc def", None); + editor.focus(); + let before = *editor.selection.borrow(); + let at = pos2(60.0, 10.0); + editor.fixture.frame(vec![pointer::moved(at)]).frame(vec![pointer::secondary(at, true)]).settle(); + assert!(editor.contexts.borrow().is_empty(), "nothing is reported before the button is released"); + editor.fixture.frame(vec![pointer::secondary(at, false)]).settle(); + assert_eq!(editor.contexts.borrow().len(), 1, "one context click"); + assert!(editor.contexts.borrow()[0].distance(at) < 1.0); + assert_eq!(*editor.selection.borrow(), before, "the caret and selection stay"); + assert_eq!(editor.commits.get() + editor.cancels.get(), 0, "the session stays open"); +} + fn preedit(text: &str) -> Event { Event::Ime(ImeEvent::Preedit { text: text.to_owned(), active_range_chars: None }) } diff --git a/crates/ui-canvas/src/canvas/crop/mod.rs b/crates/ui-canvas/src/canvas/crop/mod.rs index 99c985eaf9..f2a9256ac7 100644 --- a/crates/ui-canvas/src/canvas/crop/mod.rs +++ b/crates/ui-canvas/src/canvas/crop/mod.rs @@ -385,8 +385,11 @@ impl CropOverlay<'_> { if cx.intents().take(Intent::Submit) { self.report.apply = true; } - // Escape discards a started crop; the untouched default frame has nothing to discard and leaves it to the window. - if self.started(None) && cx.intents().take(Intent::Cancel) { + // Escape drops an armed Straighten first; else it discards a started crop. The untouched default frame has + // nothing to discard and leaves Escape to the window. + if self.straighten && cx.intents().take(Intent::Cancel) { + self.report.straighten_ended = true; + } else if self.started(None) && cx.intents().take(Intent::Cancel) { self.report.discard = true; } // A hidden frame is not shown to move. diff --git a/crates/ui-canvas/tests/it/canvas/crop.rs b/crates/ui-canvas/tests/it/canvas/crop.rs index 79451c4b88..b618876c16 100644 --- a/crates/ui-canvas/tests/it/canvas/crop.rs +++ b/crates/ui-canvas/tests/it/canvas/crop.rs @@ -626,6 +626,21 @@ fn a_default_turn_shows_the_turn_grid_and_release_restores_the_guide() { assert_eq!(rig.seen.borrow().straighten_ended, 1, "the release frame's two passes end Straighten once"); } +// Escape ends an armed Straighten and leaves the crop; with none armed it discards the started crop. +#[trial] +fn escape_ends_an_armed_straighten_before_it_discards_the_crop() { + let mut setup = Setup::new(); + setup.straighten = true; + let mut rig = Rig::new(CropBox::upright(RECT), setup); + rig.frame(vec![input::key_press(egui::Key::Escape, Modifiers::NONE)]); + assert!(rig.seen.borrow().straighten_ended >= 1, "Escape ends Straighten"); + assert!(!rig.seen.borrow().discarded, "and keeps the crop"); + let mut rig = Rig::new(CropBox::upright(RECT), Setup::new()); + rig.frame(vec![input::key_press(egui::Key::Escape, Modifiers::NONE)]); + assert!(rig.seen.borrow().discarded, "with no Straighten armed Escape discards the started crop"); + assert_eq!(rig.seen.borrow().straighten_ended, 0); +} + #[trial] fn a_default_turn_keeps_the_frame_inside_the_turned_image() { let image = Rect::new(0.0, 0.0, 400.0, 300.0);