ui-contract: a focused stop that is no longer shown leaves focus nowhere; only a removed child of a collection row hands it back to the row

behavior: after_removal goes with the rule it served. The spec trials state the new rule.
This commit is contained in:
bflatastic
2026-11-22 18:15:04 +07:00
parent 4e76008c1e
commit 8206a076a2
7 changed files with 34 additions and 58 deletions
+2 -10
View File
@@ -1,5 +1,5 @@
//! Pure focus rules (spec 9.3, 9.4, 8.3): key ownership, focus after removal, the Escape order and the
//! focus-on-press kinds. The runtime's focus service feeds them facts and applies the result.
//! Pure focus rules (spec 9.3, 9.4, 8.3): key ownership, the Escape order and the focus-on-press kinds. The runtime's
//! focus service feeds them facts and applies the result.
use crate::sense::Sense;
@@ -86,14 +86,6 @@ pub fn key_owner(blocked: bool, present: &[OwnerClass]) -> KeyOwner {
.map_or(KeyOwner::Class(OwnerClass::App), |class| KeyOwner::Class(*class))
}
/// The stop that takes focus when `focused` is no longer registered: the one after it in last pass's order
/// that is still registered, else the one before. `None` leaves the scope's initial stop to decide.
pub fn after_removal<'a, T: PartialEq>(last_order: &'a [T], focused: &T, registered: impl Fn(&T) -> bool) -> Option<&'a T> {
let at = last_order.iter().position(|stop| stop == focused)?;
let after = last_order[at + 1..].iter().find(|stop| registered(stop));
after.or_else(|| last_order[..at].iter().rev().find(|stop| registered(stop)))
}
/// What the Escape of one pass reaches first.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
+2 -3
View File
@@ -784,9 +784,8 @@ pub(crate) enum FocusOrigin {
/// A scope shown for the first time moved focus to its initial stop (a dialog's first field): a request too, so
/// it counts as an arrival by key, and a text part selects its whole text so typing replaces it.
Open,
/// 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) and no scroll owner reveals it (a closed canvas
/// text editor does not pan the canvas to the stop before it).
/// The focused child of a collection row went away and handed focus back to its row: neither the keyboard nor a
/// request chose it, so it is no arrival by key and no scroll owner reveals it.
Removal,
}
+15 -30
View File
@@ -4,7 +4,6 @@
use super::{Act, BeginIn, FocusOrigin, FocusTarget, Initial, PickedIn, State, StopKey, StopRec, TabReach};
use crate::runtime::egui::focus as egui_focus;
use crate::runtime::egui::memory::FastMap;
use craft_behavior::focus::after_removal;
use craft_behavior::nav::step;
use craft_behavior::{Arrows, MoveMods, NavMove, NavOutline, NavStep, ScopeKind};
use egui::{Context, Id};
@@ -168,8 +167,8 @@ impl State {
self.focus_stop_from(rec, FocusOrigin::Request)
}
/// [`focus_stop`](Self::focus_stop) with what moved focus there. A stop that inherits the focus from a removed one
/// is not revealed: nothing chose to show it.
/// [`focus_stop`](Self::focus_stop) with what moved focus there. A row that takes the focus back from its removed
/// child is not revealed: nothing chose to show it.
fn focus_stop_from(&mut self, rec: StopRec, origin: FocusOrigin) -> Vec<Act> {
let key = rec.stop.key();
let acts = self.enter_stop(rec, origin);
@@ -463,33 +462,22 @@ impl State {
Some(self.focus_stop(member))
}
/// The stop that takes focus when the focused stop was not registered this pass.
fn removed_focus(&self, focused: Option<Id>) -> Option<StopRec> {
/// The focus move when the focused stop was not registered this pass: a removed child of a collection row hands
/// focus back to the row, as Escape does; any other removed stop leaves focus nowhere, whatever stops are left
/// around it.
fn removed_focus(&mut self, focused: Option<Id>) -> Option<Vec<Act>> {
let root = focused?;
let key = *self.focused.get(&root)?;
if self.stops.iter().any(|rec| rec.stop.key() == key) {
return None;
}
let gone = self.last_stops.iter().find(|rec| rec.stop.key() == key)?;
// A removed child of a collection row hands focus back to the row, as Escape does, not to the next stop.
let row = gone.stop.group.and_then(|group| self.group(group)).filter(|group| group.row);
if let Some(owner) =
row.and_then(|group| self.row_owner(&group)).filter(|owner| self.stops.iter().any(|rec| rec.stop.key() == owner.stop.key()))
{
return Some(owner);
}
// Press-only stops (selectable text) are outside the Tab order, so focus never falls to them either.
let order: Vec<StopKey> = self
.last_stops
.iter()
.filter(|rec| rec.scope == gone.scope && (rec.stop.key() == key || !rec.stop.press_only))
.map(|rec| rec.stop.key())
.collect();
let registered = |candidate: &StopKey| self.stops.iter().any(|rec| rec.stop.key() == *candidate);
match after_removal(&order, &key, registered) {
Some(next) => self.stops.iter().find(|rec| rec.stop.key() == *next).cloned(),
None => self.stops.iter().find(|rec| rec.scope == gone.scope && !rec.stop.press_only).cloned(),
}
let owner = row.and_then(|group| self.row_owner(&group)).filter(|owner| self.stops.iter().any(|rec| rec.stop.key() == owner.stop.key()));
Some(match owner {
Some(owner) => self.focus_stop_from(owner, FocusOrigin::Removal),
None => vec![Act::Surrender(root)],
})
}
/// The opener of a scope that went away while focus was inside it.
@@ -719,13 +707,10 @@ 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)
.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);
}
acts = match state.restored_focus(focused) {
Some(rec) => state.focus_stop(rec),
None => state.removed_focus(focused).unwrap_or_default(),
};
}
let now = acts.last().map_or(focused, |act| match act {
Act::Focus(id, _) => Some(*id),
+6 -7
View File
@@ -12,12 +12,11 @@ use std::cell::{Cell, RefCell};
const ORIGIN: Pos2 = Pos2::new(10.0, 10.0);
const VIEW: Vec2 = Vec2::new(200.0, 100.0);
/// The focused stop goes away and hands the focus to the next stop in its scope, far below a scrolling body's view:
/// the stop takes the focus and the body stays where it is. A neighbour that inherits the focus was not shown by
/// anyone, so nothing reveals it (#15555: the rulers' corner panned the canvas when a closed text editor handed it the
/// focus).
/// The focused stop goes away while the next stop in its scope lies far below a scrolling body's view: the focus goes
/// nowhere and the body stays where it is (#15555: the rulers' corner panned the canvas when a closed text editor
/// handed it the focus).
#[trial]
fn a_stop_that_inherits_the_focus_from_a_removed_one_is_not_revealed() {
fn a_removed_stop_hands_its_focus_to_no_neighbour_and_the_body_stays() {
let (first, next) = (RefCell::new(Seen::default()), RefCell::new(Seen::default()));
let shown = Cell::new(true);
let top = Cell::new(0.0f32);
@@ -42,7 +41,7 @@ fn a_stop_that_inherits_the_focus_from_a_removed_one_is_not_revealed() {
let start = top.get();
shown.set(false);
frames.idle(6, &mut show);
let next_id = next.borrow().id.expect("the next stop ran");
assert_eq!(craft_ui::testing::focus::egui_focused(&frames.ctx), Some(next_id), "the next stop inherits the focus");
assert!(next.borrow().id.is_some(), "the next stop ran");
assert_eq!(craft_ui::testing::focus::egui_focused(&frames.ctx), None, "no neighbour inherits the focus");
assert!((top.get() - start).abs() < 0.5, "the body keeps its offset: content top {} from {start}", top.get());
}
+7 -5
View File
@@ -5,10 +5,10 @@ use craft_ui::testing::{Frames, trial};
use egui::Ui;
use std::cell::Cell;
/// Spec test row "when the focused component is not shown, focus moves to the next stop in its scope, else
/// the previous".
/// Spec test row "when the focused component is not shown, focus goes nowhere": no stop inherits it, whether stops
/// are left after the removed one or only before it.
#[trial]
fn focus_moves_to_the_next_stop_when_the_focused_one_is_not_shown_else_the_previous() {
fn focus_goes_nowhere_when_the_focused_stop_is_not_shown() {
let mut frames = Frames::new();
let hidden = Cell::new(None);
let mut show = |ui: &mut Ui| {
@@ -23,8 +23,10 @@ fn focus_moves_to_the_next_stop_when_the_focused_one_is_not_shown_else_the_previ
frames.idle(1, &mut show);
hidden.set(Some("two"));
frames.idle(2, &mut show);
assert_eq!(focused(&frames.ctx), Some(id("three")), "the stop after the removed one");
assert_eq!(focused(&frames.ctx), None, "a stop after the removed one does not take the focus");
focus(&mut frames, "three", &mut show);
frames.idle(1, &mut show);
hidden.set(Some("three"));
frames.idle(2, &mut show);
assert_eq!(focused(&frames.ctx), Some(id("one")), "no stop after it: the one before");
assert_eq!(focused(&frames.ctx), None, "no stop after it: the one before does not take the focus either");
}
+1 -1
View File
@@ -263,7 +263,7 @@ Grouped by gallery level, then shape, then name.
| `craft_behavior::eviction` | When a component's remembered state is due for eviction |
| `craft_behavior::fit` | The fit rules: which tabs, bar items or titles stay in a strip and which go to an overflow menu |
| `craft_behavior::flow` | `Insets`, `Flow` and `Axis`: start and end geometry resolved once for the layout direction |
| `craft_behavior::focus` | Pure focus rules (spec 9.3, 9.4, 8.3): key ownership, focus after removal, the Escape order and the focus-on-press kinds |
| `craft_behavior::focus` | Pure focus rules (spec 9.3, 9.4, 8.3): key ownership, the Escape order and the focus-on-press kinds |
| `craft_behavior::grid` | The `Grid` cursor: columns resolved from last pass's measurements, and the cell each child takes |
| `craft_behavior::hit` | Hit shapes, their priority and the arena's resolve rule (8.3) |
| `craft_behavior::input` | What a component takes from the user, which decides its default key use |
+1 -2
View File
@@ -716,7 +716,7 @@ The fit rules: which tabs, bar items or titles stay in a strip and which go to a
## `craft_behavior::focus`
Pure focus rules (spec 9.3, 9.4, 8.3): key ownership, focus after removal, the Escape order and the
Pure focus rules (spec 9.3, 9.4, 8.3): key ownership, the Escape order and the focus-on-press kinds. The runtime's
- `craft_behavior::focus::CancelRule`: `pub enum CancelRule { Close, ClearThenClose, Ignore }`. What Escape does in a scope that has no more specific taker.
- `craft_behavior::focus::EscapeFacts`: `pub struct EscapeFacts { pub press_captured: bool, pub component_undoable: bool, pub in_row_child: bool, pub scope_clearable: bool }`. The facts the Escape order reads.
@@ -731,7 +731,6 @@ Pure focus rules (spec 9.3, 9.4, 8.3): key ownership, focus after removal, the E
- `craft_behavior::focus::PressFocus`: `pub enum PressFocus { Take, Keep, Release, Unclaimed }`. How a primary press moves key focus (8.3).
- `pub const fn commits_draft(self) -> bool`. Whether a focused text part with a draft commits it in the press pass.
- `craft_behavior::focus::ScopeKind`: `pub enum ScopeKind { Window, Pane, Dialog, Popup, Menu, Group }`. What a focus scope is.
- `craft_behavior::focus::after_removal`: `pub fn after_removal<'a, T: PartialEq>(last_order: &'a [T], focused: &T, registered: impl Fn(&T) -> bool) -> Option<&'a T>`. The stop that takes focus when `focused` is no longer registered: the one after it in last pass's order
- `craft_behavior::focus::classify_press`: `pub fn classify_press(winner: Option<Sense>) -> PressFocus`. The kind of a press from the sense of the shape that wins it, `None` when no shape senses it. A shape that
- `craft_behavior::focus::escape_step`: `pub fn escape_step(facts: EscapeFacts, rule: CancelRule) -> EscapeStep`. The order of Escape for the top egui layer that is not exiting: a captured press, the focused component,
- `craft_behavior::focus::key_owner`: `pub fn key_owner(blocked: bool, present: &[OwnerClass]) -> KeyOwner`. Key ownership, in the order blocked, `Menu`, `Recorder`, `Dialog`, `Text`, `Popup` and `PassivePopup`,