diff --git a/crates/ui-contract/src/contract/items/hooks.rs b/crates/ui-contract/src/contract/items/hooks.rs index a274babf1..e69d2b436 100644 --- a/crates/ui-contract/src/contract/items/hooks.rs +++ b/crates/ui-contract/src/contract/items/hooks.rs @@ -401,6 +401,7 @@ impl ShapeHooks 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; diff --git a/crates/ui-contract/src/runtime/focus/registration.rs b/crates/ui-contract/src/runtime/focus/registration.rs index d5f175b52..c504c221d 100644 --- a/crates/ui-contract/src/runtime/focus/registration.rs +++ b/crates/ui-contract/src/runtime/focus/registration.rs @@ -277,3 +277,29 @@ pub(crate) fn is_tree(ctx: &Context, collection: Id) -> bool { store::peek_clone::(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::(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::(ctx, lead_slot(collection)) + .is_some_and(|fact| !fact.lead && fact.pass.saturating_add(1) >= crate::runtime::pointer::pass_of(ctx)) +} diff --git a/crates/ui-contract/src/runtime/keys/route.rs b/crates/ui-contract/src/runtime/keys/route.rs index 877cfcd70..f78178bfb 100644 --- a/crates/ui-contract/src/runtime/keys/route.rs +++ b/crates/ui-contract/src/runtime/keys/route.rs @@ -610,7 +610,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, } @@ -635,7 +636,8 @@ fn list_of_field(ctx: &Context, rec: &StopRec) -> Option { 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)?; diff --git a/crates/ui-patterns/src/patterns/form_dialogs/list_manager.rs b/crates/ui-patterns/src/patterns/form_dialogs/list_manager.rs index 22b61638b..e55e0b01b 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/list_manager.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/list_manager.rs @@ -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)); } diff --git a/crates/ui-patterns/tests/it/form_dialogs/list_manager.rs b/crates/ui-patterns/tests/it/form_dialogs/list_manager.rs index 788eae225..c49254dd4 100644 --- a/crates/ui-patterns/tests/it/form_dialogs/list_manager.rs +++ b/crates/ui-patterns/tests/it/form_dialogs/list_manager.rs @@ -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>>, Rc>); + +/// 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![