mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 07:57:47 +00:00
ui-contract, ui-patterns: Enter in a list manager's name box runs its enabled first action
With no row picked, Enter in a list manager's name box did nothing, while a click on the first action ran it. The name box drives the list, so the key router sent Enter to the list as an activation, and a list with no lead row took it without acting on it. The list manager also turned the dialog's Enter rule off, so the default button could not take the key. - ui-contract: a collection records whether it has a lead row, and a text field that drives a collection without one keeps Enter as its submit. A driven target that records nothing (a menu) still takes Enter. - ui-patterns: `ListManager` turns `close_on_enter` on, so Enter runs the first action while it is enabled, as its doc says. Enter on a lead row the name box no longer names (a name typed after picking a row) runs the first action on the typed name instead of the row's.
This commit is contained in:
@@ -401,6 +401,7 @@ impl<C: Collection> ShapeHooks<C> for ItemsHooks<'_, '_, C> {
|
||||
focus::registration::record_tree(&ctx, id, tree);
|
||||
let focused = focus::is_focused(&ctx, id);
|
||||
let lead = self.lead.key.clone();
|
||||
focus::registration::record_lead(&ctx, id, lead.as_ref().is_some_and(|key| self.index_of(key).is_some()));
|
||||
let mut lead_child = None;
|
||||
let striped = comp.items().striped;
|
||||
let mut stripe: Option<(usize, usize)> = None;
|
||||
|
||||
@@ -273,3 +273,29 @@ pub(crate) fn is_tree(ctx: &Context, collection: Id) -> bool {
|
||||
store::peek_clone::<TreeFact>(ctx, tree_slot(collection))
|
||||
.is_some_and(|fact| fact.tree && fact.pass.saturating_add(1) >= crate::runtime::pointer::pass_of(ctx))
|
||||
}
|
||||
|
||||
/// Whether a collection has a lead row to activate, and the pass that recorded it.
|
||||
#[derive(Clone, Copy)]
|
||||
struct LeadFact {
|
||||
lead: bool,
|
||||
pass: u64,
|
||||
}
|
||||
|
||||
fn lead_slot(collection: Id) -> Id {
|
||||
sub(collection, "nav_lead")
|
||||
}
|
||||
|
||||
/// Records whether the collection `collection` has a lead row in this pass: the item step writes it, and the key
|
||||
/// router reads it for the text field that drives the collection.
|
||||
pub(crate) fn record_lead(ctx: &Context, collection: Id, lead: bool) {
|
||||
let slot = lead_slot(collection);
|
||||
store::put(ctx, slot, LeadFact { lead, pass: crate::runtime::pointer::pass_of(ctx) });
|
||||
crate::runtime::memory::adopt::<LeadFact>(ctx, collection, slot);
|
||||
}
|
||||
|
||||
/// Whether the collection `collection` recorded no lead row in this pass or the one before. A target that records no
|
||||
/// lead fact (a menu a search field drives) never lacks one.
|
||||
pub(crate) fn lacks_lead(ctx: &Context, collection: Id) -> bool {
|
||||
store::peek_clone::<LeadFact>(ctx, lead_slot(collection))
|
||||
.is_some_and(|fact| !fact.lead && fact.pass.saturating_add(1) >= crate::runtime::pointer::pass_of(ctx))
|
||||
}
|
||||
|
||||
@@ -599,7 +599,8 @@ struct Driven {
|
||||
ends: bool,
|
||||
/// Left and Right go to the list too (a tree): a text stop keeps them for its caret while it holds text.
|
||||
tree: bool,
|
||||
/// Enter goes to the list; off, a text opener keeps it (its layer's `FocusPolicy::list_enter`).
|
||||
/// Enter goes to the list; off, a text opener keeps it (its layer's `FocusPolicy::list_enter`), and so does a field
|
||||
/// that drives a collection with no lead row.
|
||||
enter: bool,
|
||||
}
|
||||
|
||||
@@ -624,7 +625,8 @@ fn list_of_field(ctx: &Context, rec: &StopRec) -> Option<Driven> {
|
||||
text: false,
|
||||
ends: drive.empty,
|
||||
tree: drive.empty && focus::registration::is_tree(ctx, drive.list),
|
||||
enter: true,
|
||||
// With no lead row to activate, Enter stays the field's submit, so the dialog's default button takes it.
|
||||
enter: !focus::registration::lacks_lead(ctx, drive.list),
|
||||
}),
|
||||
(None, Some((_, opener))) => {
|
||||
let (layer, list, _) = focus::registration::dropdown_target(ctx, opener)?;
|
||||
|
||||
@@ -505,7 +505,7 @@ impl<'a> ListManager<'a> {
|
||||
buttons.push(FooterButton::new(cancel_label.unwrap_or_else(|| common.cancel.clone()), DialogRole::Cancel));
|
||||
let settled = Rc::clone(&name_box);
|
||||
let mut dialog =
|
||||
Dialog::new(title.clone()).collection_body().close_on_enter(false).button_place(ButtonPlace::Side).buttons(buttons).settle_buttons(
|
||||
Dialog::new(title.clone()).collection_body().close_on_enter(true).button_place(ButtonPlace::Side).buttons(buttons).settle_buttons(
|
||||
move |cx, buttons| {
|
||||
let folder = cx.env.folder();
|
||||
let typed = text_of(&mut settled.borrow_mut(), cx);
|
||||
@@ -586,7 +586,10 @@ impl<'a> ListManager<'a> {
|
||||
for event in events {
|
||||
match event {
|
||||
CollectionEvent::Activate(at) => {
|
||||
text.clone_from(&entries[at].name);
|
||||
// A lead row left unpicked by a name typed after it runs the first action on that name.
|
||||
if picked == Some(at) {
|
||||
text.clone_from(&entries[at].name);
|
||||
}
|
||||
let first = actions.first().filter(|action| is_enabled(action, entries, &folder, &text, name_check));
|
||||
activated.set(first.map(|_| 0));
|
||||
}
|
||||
|
||||
@@ -103,6 +103,76 @@ fn a_bound_name_box_shows_the_callers_writes_and_takes_the_typed_name() {
|
||||
assert!(!enabled(&fixture, "Add"), "an existing name disables Add");
|
||||
}
|
||||
|
||||
/// The choices a list manager reported and whether it closed.
|
||||
type Answers = (Rc<RefCell<Vec<ListManagerChoice>>>, Rc<Cell<bool>>);
|
||||
|
||||
/// A list manager whose first action is `first`, with every choice and close it reports.
|
||||
fn enter_fixture(first: ListManagerAction) -> (Fixture, Answers) {
|
||||
let choices = Rc::new(RefCell::new(Vec::new()));
|
||||
let closed = Rc::new(Cell::new(false));
|
||||
let (seen, ended) = (Rc::clone(&choices), Rc::clone(&closed));
|
||||
let fixture = Fixture::new(move |ui| {
|
||||
if ended.get() {
|
||||
return;
|
||||
}
|
||||
let actions = vec![first.clone(), ListManagerAction::new("rename", "Rename", ActionEnabled::Always).keeps_open()];
|
||||
let entries = vec![ListManagerEntry::new("a", "Alpha")];
|
||||
let out = ListManager::new("Workspaces", "Name:", entries, actions).opened(Bind::given(true)).show(ui);
|
||||
ended.set(out.closed.is_some());
|
||||
seen.borrow_mut().extend(out.choice);
|
||||
});
|
||||
(fixture, (choices, closed))
|
||||
}
|
||||
|
||||
// Enter in the name box with no row picked runs the first action while it is enabled, as a click on it does, and
|
||||
// leaves the dialog open while it is disabled; with a row picked it still answers once.
|
||||
#[trial]
|
||||
fn enter_in_the_name_box_runs_the_first_action_only_while_it_is_enabled() {
|
||||
let ok = ListManagerAction::new("ok", "OK", ActionEnabled::Always);
|
||||
let (mut fixture, (choices, closed)) = enter_fixture(ok.clone());
|
||||
fixture.room(Vec2::new(900.0, 700.0)).idle(10);
|
||||
let name = fixture.by_label("Name:").expect("the name box");
|
||||
fixture.click(&name).idle(2);
|
||||
fixture.type_text("Beta").idle(4);
|
||||
fixture.key(Key::Enter, Modifiers::NONE).idle(6);
|
||||
assert!(closed.get(), "Enter closes the dialog as OK would");
|
||||
{
|
||||
let seen = choices.borrow();
|
||||
let [choice] = seen.as_slice() else { panic!("one choice for Enter, got {seen:?}") };
|
||||
assert_eq!((choice.action.as_str(), choice.entry.as_deref(), choice.name.as_str()), ("ok", None, "Beta"));
|
||||
}
|
||||
|
||||
let (mut fixture, (choices, closed)) = enter_fixture(ok.clone());
|
||||
fixture.room(Vec2::new(900.0, 700.0)).idle(10);
|
||||
let alpha = fixture.by_label("Alpha").expect("the list shows Alpha");
|
||||
fixture.click(&alpha).idle(4);
|
||||
fixture.key(Key::Enter, Modifiers::NONE).idle(6);
|
||||
assert!(closed.get(), "Enter on a picked row closes the dialog");
|
||||
assert_eq!(choices.borrow().len(), 1, "Enter on a picked row answers once: {:?}", choices.borrow());
|
||||
|
||||
let (mut fixture, (choices, closed)) = enter_fixture(ok);
|
||||
fixture.room(Vec2::new(900.0, 700.0)).idle(10);
|
||||
let alpha = fixture.by_label("Alpha").expect("the list shows Alpha");
|
||||
fixture.click(&alpha).idle(4);
|
||||
let name = fixture.by_label("Name:").expect("the name box");
|
||||
fixture.click(&name).idle(2);
|
||||
fixture.key(Key::A, Modifiers::COMMAND).idle(2);
|
||||
fixture.type_text("Beta").idle(4);
|
||||
fixture.key(Key::Enter, Modifiers::NONE).idle(6);
|
||||
assert!(closed.get(), "Enter after retyping the name closes the dialog");
|
||||
assert_eq!(choices.borrow().first().map(|choice| choice.name.as_str()), Some("Beta"), "Enter answers with the typed name");
|
||||
|
||||
let (mut fixture, (choices, closed)) = enter_fixture(ListManagerAction::run(&ListManagerWords::default()));
|
||||
fixture.room(Vec2::new(900.0, 700.0)).idle(10);
|
||||
let name = fixture.by_label("Name:").expect("the name box");
|
||||
fixture.click(&name).idle(2);
|
||||
fixture.type_text("Beta").idle(4);
|
||||
assert!(!enabled(&fixture, "Run"), "a new name leaves an entry action off");
|
||||
fixture.key(Key::Enter, Modifiers::NONE).idle(6);
|
||||
assert!(!closed.get(), "Enter does not run a disabled first action");
|
||||
assert!(choices.borrow().is_empty(), "no choice for a disabled first action: {:?}", choices.borrow());
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_committed_rename_reports_entry_and_name_and_escape_keeps_it() {
|
||||
let rows = Rc::new(RefCell::new(vec![
|
||||
|
||||
Reference in New Issue
Block a user