mirror of
https://github.com/storytold/craft-libs.git
synced 2026-12-01 02:36:54 +01:00
Focus: the stop a Tab leaves, and a restoring scope's opener, after an unshown Tab target
Review (CodeRabbit), nav.rs:612 and nav.rs:602. Two loose ends in the recovery that moves focus on from a Tab target this pass does not show: - `tabbed_from` recorded the widget egui held at the begin hook, so a focus request the same pass applied (which moves the focus before the Tab) was not the stop the Tab left, and a text part that lost the focus by Tab read it as a blur. It now records the stop focus is on as the Tab applies. - The recovery moved the focus with no source, so entering a scope that restores the focus made the stop that went away its opener; closing that scope would return the focus to a stop that is gone. The Tab's own source is the opener now. Trial: text::tab_source::a_pending_focus_request_moves_the_stop_a_tab_leaves.
This commit is contained in:
1 parent
a5cafbf4ff
commit
693b97c792
3 files changed
+80
-4
No files matched your search
@@ -248,8 +248,14 @@ impl State {
|
||||
/// [`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> {
|
||||
self.focus_stop_from_source(rec, origin, None)
|
||||
}
|
||||
|
||||
/// [`focus_stop_from`](Self::focus_stop_from) with the stop the focus came from, for a move past a stop that is no
|
||||
/// longer shown: a restoring scope's opener is that stop, which is still there, not the one that went away.
|
||||
fn focus_stop_from_source(&mut self, rec: StopRec, origin: FocusOrigin, source: Option<&StopRec>) -> Vec<Act> {
|
||||
let key = rec.stop.key();
|
||||
let acts = self.enter_stop(rec, origin);
|
||||
let acts = self.enter_stop_from(rec, origin, source);
|
||||
if origin != FocusOrigin::Removal {
|
||||
self.revealed = Some(key);
|
||||
}
|
||||
@@ -263,7 +269,16 @@ impl State {
|
||||
}
|
||||
|
||||
fn enter_stop(&mut self, rec: StopRec, origin: FocusOrigin) -> Vec<Act> {
|
||||
self.note_entry(&rec);
|
||||
self.enter_stop_from(rec, origin, None)
|
||||
}
|
||||
|
||||
/// [`enter_stop`](Self::enter_stop) with the stop the focus came from, when it is not the stop focus is on: the Tab
|
||||
/// source a recovery moves the focus on from.
|
||||
fn enter_stop_from(&mut self, rec: StopRec, origin: FocusOrigin, source: Option<&StopRec>) -> Vec<Act> {
|
||||
match source {
|
||||
Some(from) => self.note_entry_from(&rec, from),
|
||||
None => self.note_entry(&rec),
|
||||
}
|
||||
let stop = &rec.stop;
|
||||
let moved = self.focus_now != Some(stop.root) || self.focused.get(&stop.root) != Some(&stop.key());
|
||||
if moved {
|
||||
@@ -279,6 +294,12 @@ impl State {
|
||||
/// returns there.
|
||||
fn note_entry(&mut self, rec: &StopRec) {
|
||||
let Some(from) = self.stop_of_focus() else { return };
|
||||
self.note_entry_from(rec, &from);
|
||||
}
|
||||
|
||||
/// [`note_entry`](Self::note_entry) with the stop the move came from, which is not the stop focus is on when a
|
||||
/// recovery moves it past a stop that is no longer shown.
|
||||
fn note_entry_from(&mut self, rec: &StopRec, from: &StopRec) {
|
||||
let mut at = rec.scope;
|
||||
while let Some(id) = at {
|
||||
let Some(scope) = self.scopes.get(&id).or_else(|| self.last_scopes.get(&id)) else { return };
|
||||
@@ -599,7 +620,9 @@ impl State {
|
||||
// only while it is focused), never held the focus: it goes on to the next stop that is, in the direction of the
|
||||
// Tab, else back to the stop the Tab left.
|
||||
if let Some(next) = self.arrived_unshown(key) {
|
||||
return Some(self.focus_stop_from(next, FocusOrigin::Request));
|
||||
// The move comes from the stop the Tab left, which is still shown, not from the stop it landed on.
|
||||
let source = self.press.tabbed_from.and_then(|(from, _)| self.stop_by_root(from));
|
||||
return Some(self.focus_stop_from_source(next, FocusOrigin::Request, source.as_ref()));
|
||||
}
|
||||
Some(vec![Act::Surrender(root)])
|
||||
}
|
||||
@@ -627,6 +650,13 @@ impl State {
|
||||
.map(|index| entries[index].arrival().clone())
|
||||
}
|
||||
|
||||
/// The stop focus was on when `root` held egui focus: the stop of that root, or the member recorded for it.
|
||||
fn stop_by_root(&self, root: Id) -> Option<StopRec> {
|
||||
let key = self.focused.get(&root).copied();
|
||||
key.and_then(|key| self.stops.iter().chain(&self.last_stops).find(|rec| rec.stop.key() == key).cloned())
|
||||
.or_else(|| self.stops.iter().chain(&self.last_stops).find(|rec| rec.stop.root == root).cloned())
|
||||
}
|
||||
|
||||
/// Whether the scope `scope` of this pass, or one around it, is a layer playing its exit: its stops take no focus.
|
||||
fn fading(&self, scope: Option<Id>) -> bool {
|
||||
let mut at = scope;
|
||||
@@ -869,9 +899,12 @@ pub(super) fn begin(state: &mut State, input: BeginIn) -> (Vec<Act>, Vec<String>
|
||||
acts.extend(state.apply_target(target));
|
||||
}
|
||||
for forward in input.nav.tabs {
|
||||
// The stop the Tab leaves is the one focus is on now: a request this pass applied above may have moved it on
|
||||
// from the widget egui held at the begin hook, and the components read this to tell a Tab from a blur.
|
||||
let from = state.focus_now;
|
||||
let moved = state.tab(forward, input.tab_all_controls, input.dialog_tab);
|
||||
state.press.by_keys |= !moved.is_empty();
|
||||
if let Some(from) = input.focused.filter(|_| !moved.is_empty()) {
|
||||
if let Some(from) = from.filter(|_| !moved.is_empty()) {
|
||||
state.press.tabbed_from = Some((from, forward));
|
||||
}
|
||||
acts.extend(moved);
|
||||
|
||||
@@ -18,6 +18,7 @@ pub(crate) mod rtl_paragraph;
|
||||
pub(crate) mod selectable;
|
||||
pub(crate) mod step_arrows;
|
||||
pub(crate) mod submit_key;
|
||||
pub(crate) mod tab_source;
|
||||
pub(crate) mod text_field;
|
||||
pub(crate) mod widths;
|
||||
pub(crate) mod word_run;
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
//! The stop a Tab leaves is the one focus is on when the Tab applies: a focus request the same pass moved the focus to
|
||||
//! the second field means the Tab left the second field, so the first, which lost the focus to the request, does not
|
||||
//! report a Tab departure.
|
||||
|
||||
use craft_ui::contract::{Bind, Show as _};
|
||||
use craft_ui::runtime::focus::{FocusTarget, request};
|
||||
use craft_ui::testing::focus::key_release;
|
||||
use craft_ui::testing::input::key_press;
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
use craft_ui::text::TextField;
|
||||
use egui::{Id, Key, Modifiers};
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::rc::Rc;
|
||||
|
||||
#[trial]
|
||||
fn a_pending_focus_request_moves_the_stop_a_tab_leaves() {
|
||||
let first = Rc::new(RefCell::new("one".to_owned()));
|
||||
let second = Rc::new(RefCell::new("two".to_owned()));
|
||||
let second_id = Rc::new(Cell::new(Id::NULL));
|
||||
let first_left = Rc::new(Cell::new(None::<Option<bool>>));
|
||||
let (held_first, held_second, held_id, held_left) = (first.clone(), second.clone(), second_id.clone(), first_left.clone());
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let one = TextField::new("First", Bind::bound(&mut *held_first.borrow_mut())).show(ui);
|
||||
// The first field lost the focus to a request this pass, which is no Tab departure.
|
||||
if let Some(Some(left)) = one.left_by_tab.map(Some) {
|
||||
held_left.set(Some(Some(left)));
|
||||
}
|
||||
let two = TextField::new("Second", Bind::bound(&mut *held_second.borrow_mut())).show(ui);
|
||||
held_id.set(two.response.id);
|
||||
});
|
||||
fixture.settle();
|
||||
let first_field = fixture.by_label("First").expect("the first field");
|
||||
fixture.click(&first_field).settle();
|
||||
assert!(fixture.focused_stop().is_some(), "the first field has the focus");
|
||||
// One pass: the request for the second field, then the Tab that leaves it; the first field must not claim the Tab.
|
||||
request(fixture.ctx(), FocusTarget::Id(second_id.get()));
|
||||
fixture.frame(vec![key_press(Key::Tab, Modifiers::NONE)]);
|
||||
fixture.frame(vec![key_release(Key::Tab, Modifiers::NONE)]);
|
||||
fixture.frame(Vec::new());
|
||||
assert_eq!(first_left.get(), None, "the first field lost the focus to the request, not to the Tab");
|
||||
assert!(fixture.by_label("Second").is_some(), "both fields are still shown");
|
||||
}
|
||||
Reference in new issue
Block a user