diff --git a/crates/ui-canvas/tests/it/canvas_controls/guides.rs b/crates/ui-canvas/tests/it/canvas_controls/guides.rs index 37853046d..1a2de3e1a 100644 --- a/crates/ui-canvas/tests/it/canvas_controls/guides.rs +++ b/crates/ui-canvas/tests/it/canvas_controls/guides.rs @@ -226,3 +226,27 @@ fn a_dragged_guide_is_removed_off_the_bounds_or_on_a_ruler_and_alt_and_shift_cha let landed: Vec = deep.borrow().iter().map(|guide| guide.position).collect(); assert!(matches!(landed.as_slice(), [tick] if tick.fract() == 0.0), "Shift deep in lands on a whole pixel: {landed:?}"); } + +// Delete on a focused guide removes it, and the focus the removal hands to the next guide selects nothing: no guide is +// selected after a delete, as in Photoshop. +#[trial] +fn a_deleted_guide_hands_focus_on_without_selecting_the_next_guide() { + let guides: Shared> = Rc::new(RefCell::new(vec![guide(1, Axis::Y, 200.0), guide(2, Axis::Y, 400.0)])); + let selected: Shared> = Rc::default(); + let (shown, picked) = (Rc::clone(&guides), Rc::clone(&selected)); + let mut fixture = ToolFixture::with_view("Document", Recorder::default(), view_at(1.0), move |ui, cx| { + let mut built = Guides::new("Guides", canvas_of(cx), Bind::bound(&mut *shown.borrow_mut())).interactive(true); + let current = *picked.borrow(); + if let Some(key) = current { + built = built.selected_guide(key); + } + if let Some(select) = built.show_at(ui, cx.rect).select { + *picked.borrow_mut() = select; + } + }); + fixture.press_at(pos2(200.0, 300.0)).release_at(pos2(200.0, 300.0)); + assert_eq!(*selected.borrow(), Some(GuideKey::new(1)), "a click selects the guide"); + fixture.fixture().key(Key::Delete, Modifiers::NONE).idle(3); + assert!(at(&guides, Axis::Y, 200.0).is_none(), "Delete removes it: {:?}", guides.borrow()); + assert_eq!(*selected.borrow(), None, "and selects no other guide"); +} diff --git a/crates/ui-contract/src/runtime/focus.rs b/crates/ui-contract/src/runtime/focus.rs index 4d0b1ceed..b2c78d9f5 100644 --- a/crates/ui-contract/src/runtime/focus.rs +++ b/crates/ui-contract/src/runtime/focus.rs @@ -632,6 +632,9 @@ pub(crate) enum FocusOrigin { Press, /// The keyboard (Tab, arrows, Page keys) or a focus request. Request, + /// The focused stop went away and handed focus to its neighbour: neither the keyboard nor a request chose it, so + /// it is no arrival by key (a deleted guide selects no other guide). + Removal, } /// What a component reads of focus in one pass. diff --git a/crates/ui-contract/src/runtime/focus/nav.rs b/crates/ui-contract/src/runtime/focus/nav.rs index ee75e05a0..1b4368e7d 100644 --- a/crates/ui-contract/src/runtime/focus/nav.rs +++ b/crates/ui-contract/src/runtime/focus/nav.rs @@ -164,8 +164,13 @@ impl State { /// Moves focus to `rec` and asks the enclosing scroll owner to reveal it: the egui widget is its root, and the stop /// is recorded under that root. pub(super) fn focus_stop(&mut self, rec: StopRec) -> Vec { + self.focus_stop_from(rec, FocusOrigin::Request) + } + + /// [`focus_stop`](Self::focus_stop) with what moved focus there. + fn focus_stop_from(&mut self, rec: StopRec, origin: FocusOrigin) -> Vec { let key = rec.stop.key(); - let acts = self.enter_stop(rec, FocusOrigin::Request); + let acts = self.enter_stop(rec, origin); self.revealed = Some(key); acts } @@ -684,9 +689,12 @@ pub(super) fn end(state: &mut State, focused: Option, on_app_widget: bool, f acts.clear(); } if acts.is_empty() { - let next = state.restored_focus(focused).or_else(|| state.removed_focus(focused)); - if let Some(rec) = next { - acts = state.focus_stop(rec); + let next = state + .restored_focus(focused) + .map(|rec| (rec, FocusOrigin::Request)) + .or_else(|| state.removed_focus(focused).map(|rec| (rec, FocusOrigin::Removal))); + if let Some((rec, origin)) = next { + acts = state.focus_stop_from(rec, origin); } } let now = acts.last().map_or(focused, |act| match act {