Focus: a removed stop hands focus to its neighbour as a removal, not an arrival by key, so a deleted guide (or stop, pin, anchor) selects no neighbour

This commit is contained in:
aiongg
2026-11-17 03:09:37 +00:00
parent 2970ef8cc0
commit 6eb0fa8cde
3 changed files with 39 additions and 4 deletions
@@ -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<f64> = 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<Vec<Guide>> = Rc::new(RefCell::new(vec![guide(1, Axis::Y, 200.0), guide(2, Axis::Y, 400.0)]));
let selected: Shared<Option<GuideKey>> = 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");
}
+3
View File
@@ -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.
+12 -4
View File
@@ -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<Act> {
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<Act> {
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<Id>, 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 {