mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-27 09:19:28 +00:00
Merge pull request #3537 from dotCooCoo/fix/dialog-initial-focus
ui-contract: a dialog with no field and no default button opens on its first body stop
This commit is contained in:
@@ -640,6 +640,9 @@ impl<C: Container> ShapeHooks<C> for BodyHooks<'_> {
|
||||
self.end_unfit = Some(short);
|
||||
}
|
||||
self.after_body(ui, env, &mut plan);
|
||||
if plan.real {
|
||||
crate::runtime::focus::end_body(&plan.ctx, plan.id);
|
||||
}
|
||||
if plan.real && props.end.is_some() {
|
||||
self.end_step(comp, ui, cx, &mut plan);
|
||||
}
|
||||
@@ -647,6 +650,9 @@ impl<C: Container> ShapeHooks<C> for BodyHooks<'_> {
|
||||
// A collapsed body keeps its last full size in layout memory, so opening it again places by that size
|
||||
// and asks for no discard pass.
|
||||
self.body_size = if props.collapse.is_some() { None } else { Some(Vec2::ZERO) };
|
||||
if plan.real {
|
||||
crate::runtime::focus::end_body(&plan.ctx, plan.id);
|
||||
}
|
||||
}
|
||||
self.close_label(&mut plan);
|
||||
// B4 footer: under the control column of a label-scoped container, which starts where the label step put
|
||||
@@ -1294,7 +1300,7 @@ impl BodyHooks<'_> {
|
||||
plan.footer_env = plan.region_env.derive_local(|scoped| scoped.content_column = true);
|
||||
}
|
||||
if plan.real {
|
||||
crate::runtime::focus::begin_body(&plan.ctx);
|
||||
crate::runtime::focus::begin_body(&plan.ctx, plan.id);
|
||||
}
|
||||
// A stack is a content column where its width comes from its content: a content-sized stack in flow, or a
|
||||
// held fill-width container. A stack given its width (a rect it is shown at, or a column's width it passes
|
||||
|
||||
@@ -160,8 +160,11 @@ pub(crate) struct State {
|
||||
pub(crate) scope_order: Vec<Id>,
|
||||
pub(crate) last_scope_order: Vec<Id>,
|
||||
pub(super) open: Vec<(Id, usize)>,
|
||||
/// Where a scope's body begins in `stops`; the header's stops come before it, the footer's after the body's.
|
||||
pub(super) body_from: FastMap<Id, usize>,
|
||||
/// Where a scope's body begins in `stops`, and the container whose body it is; the header's stops come before it,
|
||||
/// the footer's after the body's.
|
||||
pub(super) body_from: FastMap<Id, (usize, Id)>,
|
||||
/// Where the body of the container that marked a scope's body ends in `stops`: its end region and footer follow.
|
||||
pub(super) body_end: FastMap<Id, usize>,
|
||||
pub(crate) groups: FastMap<Id, Group>,
|
||||
pub(crate) last_groups: FastMap<Id, Group>,
|
||||
pub(crate) row_sets: Vec<RowGroups>,
|
||||
@@ -581,13 +584,28 @@ pub(crate) fn focus_in(ctx: &Context, range: std::ops::Range<usize>) -> bool {
|
||||
|
||||
/// Marks the stops registered from here on in the innermost open scope as its body's: they come first in Tab
|
||||
/// order, then the footer's, then the header's, and initial focus goes to the first of them, not to the title
|
||||
/// row's close action. The first mark of a pass stands.
|
||||
pub(crate) fn begin_body(ctx: &Context) {
|
||||
/// row's close action. The first mark of a pass stands; `container` is the container whose body it is.
|
||||
pub(crate) fn begin_body(ctx: &Context, container: Id) {
|
||||
take_app_widgets(ctx, None);
|
||||
with(ctx, |state| {
|
||||
if let Some((scope, _)) = state.open.last().copied() {
|
||||
let at = state.stops.len();
|
||||
state.body_from.entry(scope).or_insert(at);
|
||||
state.body_from.entry(scope).or_insert((at, container));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Ends the body that `container` marked in the innermost open scope (`begin_body`): the stops registered from here on
|
||||
/// are its end region's and footer's. A dialog with no field and no default stop starts on its first body stop, never
|
||||
/// on one of theirs. A container nested in the body ends nothing.
|
||||
pub(crate) fn end_body(ctx: &Context, container: Id) {
|
||||
take_app_widgets(ctx, None);
|
||||
with(ctx, |state| {
|
||||
if let Some((scope, _)) = state.open.last().copied()
|
||||
&& state.body_from.get(&scope).is_some_and(|(_, owner)| *owner == container)
|
||||
{
|
||||
let at = state.stops.len();
|
||||
state.body_end.entry(scope).or_insert(at);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -682,8 +700,8 @@ pub(crate) fn pop_scope(ctx: &Context) {
|
||||
take_app_widgets(ctx, None);
|
||||
let frame = crate::runtime::egui::pass::frame_nr(ctx, viewport(ctx));
|
||||
let acts = with(ctx, |state| {
|
||||
nav::order_regions(state);
|
||||
nav::initial_focus(state, frame)
|
||||
let body = nav::order_regions(state);
|
||||
nav::initial_focus(state, frame, body)
|
||||
});
|
||||
apply(ctx, acts);
|
||||
}
|
||||
|
||||
@@ -192,6 +192,7 @@ impl State {
|
||||
self.last_docs = std::mem::take(&mut self.docs);
|
||||
self.open.clear();
|
||||
self.body_from.clear();
|
||||
self.body_end.clear();
|
||||
self.entered = false;
|
||||
self.asked = false;
|
||||
self.candidates_seen = 0;
|
||||
@@ -769,17 +770,22 @@ fn cycle(entries: &[Entry], current: Option<&StopRec>, top: Option<Id>, forward:
|
||||
}
|
||||
|
||||
/// Puts the stops of the innermost open scope in Tab order: the body's, then the footer's, then the header's.
|
||||
/// Inner scopes closed already and lie within one region, so they move with it.
|
||||
pub(super) fn order_regions(state: &mut State) {
|
||||
let Some((scope, first)) = state.open.last().copied() else { return };
|
||||
let Some(body) = state.body_from.remove(&scope) else { return };
|
||||
/// Inner scopes closed already and lie within one region, so they move with it. Returns how many stops the body holds,
|
||||
/// now the scope's first ones; `None` when the scope marked no body.
|
||||
pub(super) fn order_regions(state: &mut State) -> Option<usize> {
|
||||
let (scope, first) = state.open.last().copied()?;
|
||||
let body_end = state.body_end.remove(&scope);
|
||||
let (body, _) = state.body_from.remove(&scope)?;
|
||||
let end = state.stops.len();
|
||||
let first = first.min(end);
|
||||
state.stops[first..end].rotate_left(body.clamp(first, end) - first);
|
||||
let body = body.clamp(first, end);
|
||||
state.stops[first..end].rotate_left(body - first);
|
||||
Some(body_end.map_or(end, |body_end| body_end.clamp(body, end)) - body)
|
||||
}
|
||||
|
||||
/// Closes the innermost open scope; a scope shown for the first time moves focus to its initial stop.
|
||||
pub(super) fn initial_focus(state: &mut State, frame: u64) -> Vec<Act> {
|
||||
/// Closes the innermost open scope; a scope shown for the first time moves focus to its initial stop. `body` is how
|
||||
/// many of the scope's first stops are its body's (`order_regions`), `None` for all of them.
|
||||
pub(super) fn initial_focus(state: &mut State, frame: u64, body: Option<usize>) -> Vec<Act> {
|
||||
let Some((scope, first)) = state.open.pop() else { return Vec::new() };
|
||||
let Some((policy, entering)) = state.scopes.get(&scope).map(|rec| (rec.policy, rec.entering)) else { return Vec::new() };
|
||||
let new = match entering {
|
||||
@@ -804,10 +810,18 @@ pub(super) fn initial_focus(state: &mut State, frame: u64) -> Vec<Act> {
|
||||
.and_then(|group| lists.arrival(&group))
|
||||
.unwrap_or_else(|| rec.clone())
|
||||
});
|
||||
// The body's first Tab entry, at its arrival member. The footer's and the header's buttons with no default take no
|
||||
// focus, so Enter does not answer one that was never chosen.
|
||||
let in_body = (policy.initial == Initial::FirstField)
|
||||
.then(|| {
|
||||
let body = Lists { stops: &stops[..body.unwrap_or(stops.len()).min(stops.len())], ..lists };
|
||||
body.entries(|_| true, true).into_iter().next().map(|entry| entry.arrival().clone())
|
||||
})
|
||||
.flatten();
|
||||
let pick = match policy.initial {
|
||||
Initial::First => entry.as_ref(),
|
||||
Initial::Default => stops.iter().find(|rec| rec.stop.default).or(entry.as_ref()),
|
||||
Initial::FirstField => stops.iter().find(|rec| rec.stop.text).or_else(|| stops.iter().find(|rec| rec.stop.default)),
|
||||
Initial::FirstField => stops.iter().find(|rec| rec.stop.text).or_else(|| stops.iter().find(|rec| rec.stop.default)).or(in_body.as_ref()),
|
||||
Initial::Key(name) => stops.iter().find(|rec| rec.stop.name == Some(name)),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
@@ -12,7 +12,8 @@ pub enum Initial {
|
||||
Keep,
|
||||
/// The scope's first stop.
|
||||
First,
|
||||
/// The scope's first text or number field, else its default stop, else nothing: a dialog's default.
|
||||
/// The scope's first text or number field, else its default stop, else the first stop of its body, else nothing:
|
||||
/// a dialog's default. A footer's or a header's buttons are not taken by the last rule.
|
||||
FirstField,
|
||||
/// The stop registered as the default.
|
||||
Default,
|
||||
|
||||
@@ -77,7 +77,13 @@ pub fn push_scope(ctx: &Context, id: Id, policy: FocusPolicy, label: Option<Arc<
|
||||
|
||||
/// Marks the stops registered from here on in the innermost open scope as its body's.
|
||||
pub fn begin_body(ctx: &Context) {
|
||||
focus::begin_body(ctx);
|
||||
focus::begin_body(ctx, Id::NULL);
|
||||
}
|
||||
|
||||
/// Ends the body [`begin_body`] marked: the stops registered from here on in the innermost open scope are its end
|
||||
/// region's and footer's.
|
||||
pub fn end_body(ctx: &Context) {
|
||||
focus::end_body(ctx, Id::NULL);
|
||||
}
|
||||
|
||||
/// Closes the innermost open scope.
|
||||
|
||||
@@ -8,18 +8,19 @@ use super::{
|
||||
};
|
||||
use crate::buttons::Button;
|
||||
use crate::choice::Checkbox;
|
||||
use crate::contract::{Bind, FooterSlot, Props as _, Role, Show as _, ShowContainer as _};
|
||||
use crate::contract::{Bind, FooterSlot, Props as _, Role, Show as _, ShowCollection as _, ShowContainer as _};
|
||||
use crate::display::Text;
|
||||
use crate::layout::GridFit;
|
||||
use crate::layout_components::Grid;
|
||||
use crate::layout_components::Row;
|
||||
use crate::list::{List, ListRow};
|
||||
use crate::runtime::overlay::CloseReason;
|
||||
use crate::sections::{DialogRole, FooterButton};
|
||||
use crate::testing::input::key_press;
|
||||
use crate::testing::relations::{aligned_left, apart_above, grows_with, inside, off_foot, same_column};
|
||||
use crate::testing::{Fixture, Frames, trial};
|
||||
use crate::text::TextField;
|
||||
use craft_behavior::ColumnWidth;
|
||||
use craft_behavior::{ColumnWidth, keyed};
|
||||
use craft_quit::{CloseAnswer, CloseScope};
|
||||
use egui::{Key, Modifiers, Rect, Ui, Vec2};
|
||||
use std::cell::{Cell, RefCell};
|
||||
@@ -285,7 +286,42 @@ fn a_dialog_opens_on_its_first_field_else_its_default_button() {
|
||||
};
|
||||
assert_eq!(focus_on_open(true, true), Some("Name"), "the first field takes the focus though a check box comes first");
|
||||
assert_eq!(focus_on_open(false, true), Some("OK"), "without a field the default button takes it");
|
||||
assert_eq!(focus_on_open(false, false), None, "without either nothing takes it");
|
||||
assert_eq!(focus_on_open(false, false), Some("Snap"), "without either the first control takes it");
|
||||
}
|
||||
|
||||
/// A dialog that holds a list and two buttons, with no field and no default button, opens with a control inside it
|
||||
/// focused, and Escape gives the focus back to the button that opened it.
|
||||
#[trial]
|
||||
fn a_dialog_with_no_field_or_default_takes_the_focus_and_escape_returns_it_to_the_opener() {
|
||||
const ROWS: [&str; 2] = ["First", "Second"];
|
||||
let open = Rc::new(Cell::new(false));
|
||||
let opener = Rc::new(Cell::new(egui::Id::NULL));
|
||||
let (seen, shown) = (Rc::clone(&open), Rc::clone(&opener));
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
shown.set(Button::new("Open Presets").show(ui).response.id);
|
||||
let mut now = seen.get();
|
||||
Dialog::new("Presets").opened(Bind::bound(&mut now)).show(ui, |ui, _| {
|
||||
List::new("Presets list").natural_height(true).show_items(ui, &keyed(&ROWS, |row| *row), &mut |key, _| ListRow::new(key.to_string()));
|
||||
Row::new().show(ui, |ui, _| {
|
||||
Button::new("New").show(ui);
|
||||
Button::new("Delete").show(ui);
|
||||
});
|
||||
});
|
||||
seen.set(now);
|
||||
});
|
||||
fixture.settle();
|
||||
crate::runtime::focus::request(fixture.ctx(), crate::runtime::focus::FocusTarget::Id(opener.get()));
|
||||
fixture.settle();
|
||||
assert!(crate::testing::focus::is_focused(fixture.ctx(), opener.get()), "the opener has the focus");
|
||||
open.set(true);
|
||||
fixture.settle();
|
||||
let focused = crate::testing::access::focused_key(fixture.tree());
|
||||
let inside = ["Presets list", "First", "Second", "New", "Delete"];
|
||||
let on = inside.into_iter().find(|label| fixture.by_label(label).is_some_and(|target| target.node == focused));
|
||||
assert!(on.is_some(), "a control inside the dialog has the focus");
|
||||
fixture.key(Key::Escape, Modifiers::NONE).settle();
|
||||
assert!(!open.get(), "Escape closed the dialog");
|
||||
assert!(crate::testing::focus::is_focused(fixture.ctx(), opener.get()), "the focus is back on the opener");
|
||||
}
|
||||
|
||||
/// A dialog host showing `dialog` in a fixture, once the dialog settled.
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
//! Where focus goes when a dialog with no text field and no default stop opens.
|
||||
|
||||
use super::{control, focused, id};
|
||||
use craft_behavior::{Input, NavShape, Roving};
|
||||
use craft_ui::runtime::focus::{FocusPolicy, KeyUse};
|
||||
use craft_ui::testing::focus::{Group, Stop, begin_body, end_body, pop_scope, push_scope, register_group, register_stop, widget};
|
||||
use craft_ui::testing::{Frames, trial};
|
||||
use egui::Ui;
|
||||
|
||||
/// The stop a dialog with no field and no default stop focuses when it opens: `body` names its body's stops, `end`
|
||||
/// the stops after its body (an end region's and a footer's buttons). A name starting with "text" is read-only text
|
||||
/// that only a press focuses, and one starting with "row" is a child of a collection's row.
|
||||
fn opens_on(body: &[&str], end: &[&str]) -> Option<egui::Id> {
|
||||
let mut frames = Frames::new();
|
||||
let mut show = |ui: &mut Ui| {
|
||||
push_scope(ui.ctx(), id("dialog"), FocusPolicy::DIALOG, None);
|
||||
begin_body(ui.ctx());
|
||||
for name in body {
|
||||
if name.starts_with("text") {
|
||||
widget(ui, id(name));
|
||||
register_stop(ui.ctx(), Stop::new(id(name), KeyUse::of(Input::Text)).press_only());
|
||||
} else if name.starts_with("row") {
|
||||
widget(ui, id(name));
|
||||
register_stop(ui.ctx(), Stop::new(id(name), KeyUse::of(Input::Pointer)).in_group(id("row")));
|
||||
register_group(ui.ctx(), Group::new(id("row"), Roving::new(NavShape::Flat)).row(id("rows")));
|
||||
} else {
|
||||
control(ui, name);
|
||||
}
|
||||
}
|
||||
end_body(ui.ctx());
|
||||
for name in end {
|
||||
control(ui, name);
|
||||
}
|
||||
pop_scope(ui.ctx());
|
||||
};
|
||||
frames.idle(3, &mut show);
|
||||
focused(&frames.ctx)
|
||||
}
|
||||
|
||||
/// A dialog with no field and no default stop opens on the first body stop that Tab reaches, past read-only text, and
|
||||
/// leaves the buttons after its body unfocused when the body has none, so Enter answers nothing nobody chose.
|
||||
#[trial]
|
||||
fn a_dialog_with_no_field_or_default_opens_on_its_first_reachable_body_stop() {
|
||||
assert_eq!(opens_on(&["list", "new"], &["close"]), Some(id("list")), "the body's first stop takes the focus");
|
||||
assert_eq!(opens_on(&["text", "list"], &["close"]), Some(id("list")), "read-only text that Tab passes over does not");
|
||||
assert_eq!(opens_on(&["row child", "list"], &["close"]), Some(id("list")), "nor does a row's child, which Tab passes over");
|
||||
assert_eq!(opens_on(&[], &["action", "close"]), None, "with nothing in the body, no button after it takes the focus");
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
//! The focus service and the key stage, driven through registered stops over real egui widgets.
|
||||
|
||||
pub(crate) mod dialog_open;
|
||||
pub(crate) mod dialog_tab;
|
||||
pub(crate) mod keys;
|
||||
pub(crate) mod press;
|
||||
|
||||
Reference in New Issue
Block a user