From fe271bf5745f71bbdfb7e26a5ae9ab450ea8d4e0 Mon Sep 17 00:00:00 2001 From: aiongg Date: Sun, 22 Nov 2026 18:59:40 +0000 Subject: [PATCH 1/2] Dropdowns name the list that takes their opener's keys A dropdown's opener (a picker's field or button) hands Up, Down, Page keys, Enter and Tab to a collection in its dropdown. The framework took the first type-ahead collection it found in the layer. The font picker's dropdown holds the class column before the family list, so with filters shown Enter and the arrows went to the class column: typing a name and pressing Enter picked nothing. Overlay::list (FocusPolicy::list) names the collection instead, and every dropdown names its list: Select, ComboBox, TreePicker, completions, number presets, PopupList (new PopupList::list) and the font picker, which names its family list while it shows one. The type-ahead guess is gone. --- crates/font-manager/src/picker/rows.rs | 10 +++- crates/font-manager/tests/it/picker.rs | 56 +++++++++++++++++++ .../ui-contract/src/contract/overlay/props.rs | 12 +++- .../ui-contract/src/runtime/focus/policy.rs | 12 ++++ .../src/runtime/focus/registration.rs | 18 ++---- .../src/patterns/panels/palette.rs | 2 +- crates/ui-widgets/src/pickers/combo_box.rs | 4 +- crates/ui-widgets/src/pickers/select.rs | 4 +- crates/ui-widgets/src/pickers/tree_picker.rs | 4 +- crates/ui-widgets/src/popover/popup_list.rs | 16 +++++- .../src/popover/popup_list/stories.rs | 6 +- crates/ui-widgets/src/text/completions.rs | 7 ++- crates/ui-widgets/src/text/number/popups.rs | 4 +- docs/contract-api.md | 5 +- 14 files changed, 128 insertions(+), 32 deletions(-) diff --git a/crates/font-manager/src/picker/rows.rs b/crates/font-manager/src/picker/rows.rs index bd1e58f9c..5bbdb8fdd 100644 --- a/crates/font-manager/src/picker/rows.rs +++ b/crates/font-manager/src/picker/rows.rs @@ -389,7 +389,14 @@ pub(super) fn popup<'a>(picker: &mut FontPicker<'a>, ui: &mut OverlayUi<'_>, cx: let mut stars: Vec<(String, bool)> = Vec::new(); let mut over_rows = false; let mut under_pointer: Option = None; - let events = ui.show(popup, Overlay::dropdown().dismiss(Dismiss::new().focus_left(true)), |ui, _| { + // The field's list keys go to the family list, not to the class column beside it. Without matches no list shows, + // and the field keeps Enter (a plain box applies the typed name). + let families = cx.id.with("families"); + let mut overlay = Overlay::dropdown().dismiss(Dismiss::new().focus_left(true)); + if !slots.is_empty() { + overlay = overlay.list(families); + } + let events = ui.show(popup, overlay, |ui, _| { let mut events = Vec::new(); Stack::new().show(ui, |ui, _| { if let Some(text) = banner_text { @@ -434,6 +441,7 @@ pub(super) fn popup<'a>(picker: &mut FontPicker<'a>, ui: &mut OverlayUi<'_>, cx: let selection = current.filter(|_| query.trim().is_empty()).map_or_else(Selection::default, |at| Selection::single(slots[at].key())); let rows = List::::options(name.clone()) + .id(families) .tree(true) .tree_leaves(leaves) .columns(columns.clone()) diff --git a/crates/font-manager/tests/it/picker.rs b/crates/font-manager/tests/it/picker.rs index 5f18ee494..141ff5730 100644 --- a/crates/font-manager/tests/it/picker.rs +++ b/crates/font-manager/tests/it/picker.rs @@ -198,6 +198,62 @@ fn a_click_on_the_focused_closed_field_selects_the_name_so_typing_replaces_it() } } +/// A picker as the character controls host it: a given value, the filters (so the class column shows beside the +/// families) and a preview text. The field's list keys go to the family list: Enter on a typed part of a name picks the +/// best match, and Down moves the family row, which previews, before Enter picks it. +#[trial] +fn with_the_class_column_shown_the_field_keys_move_and_pick_in_the_family_list() { + #[derive(Default)] + struct Reported { + picks: Vec, + previews: Vec, + } + let reported = Rc::new(RefCell::new(Reported::default())); + let seen = Rc::clone(&reported); + let catalog = catalog_of(&[INTER_REGULAR, epaint_default_fonts::HACK_REGULAR, epaint_default_fonts::UBUNTU_LIGHT]); + let mut settings = FontPickerSettings::default(); + let mut filters = FontFilters::default(); + let mut fx = Fixture::new(move |ui| { + let out = FontPicker::new("Font family", Bind::given("Inter".to_owned()), &catalog) + .settings(Bind::bound(&mut settings)) + .filters(Bind::bound(&mut filters)) + .preview_text("Sample") + .show(ui); + let mut seen = seen.borrow_mut(); + if let Preview::Shows(family) = out.preview { + seen.previews.push(family); + } + seen.picks.extend(out.picked); + }); + fx.in_room(Room::Window); + fx.settle(); + let words = FontPickerWords::default(); + let type_in = |fx: &mut Fixture, text: &str| { + let field = fx.by_label("Font family").expect("the family field"); + fx.click(&field); + fx.settle(); + fx.type_text(text); + fx.settle(); + }; + + type_in(&mut fx, "Ha"); + assert!(present(&fx, words.all_classes.as_str()), "the class column shows"); + fx.key(Key::Enter, Modifiers::NONE); + fx.settle(); + assert_eq!(reported.borrow().picks, ["Hack"], "Enter on a partial name picks the best match"); + + // "n" matches Inter and Ubuntu. + type_in(&mut fx, "n"); + reported.borrow_mut().previews.clear(); + fx.key(Key::ArrowDown, Modifiers::NONE); + fx.settle(); + let keyed = reported.borrow().previews.last().cloned().expect("Down moves the family row, which previews its family"); + assert!(["Inter", "Ubuntu"].contains(&keyed.as_str()), "the row is a family that matches: {keyed}"); + fx.key(Key::Enter, Modifiers::NONE); + fx.settle(); + assert_eq!(reported.borrow().picks.last(), Some(&keyed), "Enter picks the family row the keys reached"); +} + /// A `Plain` picker over `family` with `recent` families, `actions` rows and the face `shown_as`. fn plain_rig( catalog: Arc, diff --git a/crates/ui-contract/src/contract/overlay/props.rs b/crates/ui-contract/src/contract/overlay/props.rs index f452d113a..abfd74cff 100644 --- a/crates/ui-contract/src/contract/overlay/props.rs +++ b/crates/ui-contract/src/contract/overlay/props.rs @@ -10,7 +10,7 @@ use crate::runtime::windows::ResizeEdge; use craft_behavior::overlay::{Align, MinWidth, Side}; use craft_behavior::{GesturePhase, OwnerClass}; use egui::emath::TSTransform; -use egui::{Align2, Pos2, Rect, Response, Vec2}; +use egui::{Align2, Id, Pos2, Rect, Response, Vec2}; use std::borrow::Cow; /// What a press outside the layer and its anchor does. @@ -215,7 +215,8 @@ impl Overlay { } /// A dropdown list: from its content, at least as wide as its anchor and capped, focus stays on the anchor. It - /// closes when a row is activated or the focus leaves the anchor. + /// closes when a row is activated or the focus leaves the anchor. The opener's list keys go to the collection + /// [`Overlay::list`] names. pub fn dropdown() -> Overlay { let focus = FocusPolicy::POPUP.trap(false).restore(false).initial(Initial::KeepAnchor); let mut overlay = Overlay::base(Rank::Popup, Overlay::below(), OverlayFit::ContentCapped(DROPDOWN_SHARE), OwnerClass::PassivePopup, focus); @@ -451,6 +452,13 @@ impl Overlay { self } + /// The collection in a dropdown that takes its opener's list keys (`FocusPolicy::list`). A dropdown that holds a + /// list names it each pass it is shown; the opener keeps keys no layer names a list for. + pub fn list(mut self, list: Id) -> Overlay { + self.focus = self.focus.list(list); + self + } + /// Sets the dismissal rules. pub fn dismiss(mut self, dismiss: Dismiss) -> Overlay { self.dismiss = dismiss; diff --git a/crates/ui-contract/src/runtime/focus/policy.rs b/crates/ui-contract/src/runtime/focus/policy.rs index fcddf7c38..f5077026c 100644 --- a/crates/ui-contract/src/runtime/focus/policy.rs +++ b/crates/ui-contract/src/runtime/focus/policy.rs @@ -53,6 +53,9 @@ pub struct FocusPolicy { /// In a `KeepAnchor` layer, Enter on a text opener activates the layer's list; off, the opener keeps Enter and /// Tab alone takes the list's lead (a completion list that leaves Enter to the field's own commit). pub list_enter: bool, + /// In a `KeepAnchor` layer, the collection the opener hands its list keys to (Up, Down, Page keys, Home and End, + /// Enter and Tab as the layer's rules say). Without one the opener keeps its keys. + pub list: Option, } impl FocusPolicy { @@ -66,6 +69,7 @@ impl FocusPolicy { cancel: CancelRule::Ignore, pages: false, list_enter: true, + list: None, }; /// A pane: an F6 stop. pub const PANE: FocusPolicy = FocusPolicy { kind: ScopeKind::Pane, ..FocusPolicy::NONE }; @@ -79,6 +83,7 @@ impl FocusPolicy { cancel: CancelRule::Close, pages: false, list_enter: true, + list: None, }; /// A popup: focus moves in, Escape closes and focus returns to the opener. pub const POPUP: FocusPolicy = FocusPolicy { @@ -90,6 +95,7 @@ impl FocusPolicy { cancel: CancelRule::Close, pages: false, list_enter: true, + list: None, }; /// A menu: like a popup. pub const MENU: FocusPolicy = FocusPolicy { kind: ScopeKind::Menu, ..FocusPolicy::POPUP }; @@ -127,6 +133,12 @@ impl FocusPolicy { self } + /// The collection in the layer that takes the opener's list keys. + pub const fn list(mut self, list: Id) -> Self { + self.list = Some(list); + self + } + /// Whether Ctrl+Tab cycles pages. pub const fn pages(mut self, on: bool) -> Self { self.pages = on; diff --git a/crates/ui-contract/src/runtime/focus/registration.rs b/crates/ui-contract/src/runtime/focus/registration.rs index d251d2eda..cc2ed5cc3 100644 --- a/crates/ui-contract/src/runtime/focus/registration.rs +++ b/crates/ui-contract/src/runtime/focus/registration.rs @@ -180,27 +180,17 @@ impl Group { } } -/// The innermost open `KeepAnchor` layer opened from the stop `opener`, the collection its opener hands list keys -/// to (the stop with the collection's key role that is registered inside the layer's focus scope) and the layer's -/// policy. `None` while no such layer is open or it has no collection. +/// The innermost open `KeepAnchor` layer opened from the stop `opener`, the collection the layer names for its +/// opener's list keys (`FocusPolicy::list`) and the layer's policy. `None` while no such layer is open or it names no +/// collection. pub(crate) fn dropdown_target(ctx: &Context, opener: StopKey) -> Option<(Id, Id, FocusPolicy)> { with(ctx, |state| { let layer = *state.last_scope_order.iter().rev().find(|id| { state.last_scopes.get(*id).is_some_and(|scope| scope.policy.initial == Initial::KeepAnchor && scope.opener == Some(opener)) })?; - let inside = |rec: &super::StopRec| { - let mut at = rec.scope; - while let Some(scope) = at { - if scope == layer { - return true; - } - at = state.last_scopes.get(&scope).and_then(|record| record.parent); - } - false - }; let policy = state.last_scopes.get(&layer)?.policy; - state.last_stops.iter().find(|rec| rec.stop.role.type_ahead && inside(rec)).map(|rec| (layer, rec.stop.id, policy)) + policy.list.map(|list| (layer, list, policy)) }) } diff --git a/crates/ui-patterns/src/patterns/panels/palette.rs b/crates/ui-patterns/src/patterns/panels/palette.rs index 07a2e19c9..9f7e50f68 100644 --- a/crates/ui-patterns/src/patterns/panels/palette.rs +++ b/crates/ui-patterns/src/patterns/panels/palette.rs @@ -252,7 +252,7 @@ impl<'a> CommandPalette<'a> { let session = inner.memory::(); let field = TextField::search(label.clone(), Bind::bound(&mut session.query)).placeholder(face_hint.clone()).id(field_id).show(ui).response; - let popup = PopupList::new(label.clone(), open.reborrowed()).opened_by(&field).show(ui, |ui, popup| { + let popup = PopupList::new(label.clone(), open.reborrowed()).opened_by(&field).list(list_id).show(ui, |ui, popup| { let recent = recent_of(&mut recents, popup); let input = RowsInput { ctx: &ctx, diff --git a/crates/ui-widgets/src/pickers/combo_box.rs b/crates/ui-widgets/src/pickers/combo_box.rs index 3b52582d9..2b99a1836 100644 --- a/crates/ui-widgets/src/pickers/combo_box.rs +++ b/crates/ui-widgets/src/pickers/combo_box.rs @@ -263,9 +263,11 @@ impl Component for ComboBox<'_> { let rows = EntryRows::new(options, &labels); let mut over_rows = false; let mut under_pointer: Option = None; - let events = ui.show(popup, Overlay::dropdown(), |ui, _| { + let list = cx.id.with("options"); + let events = ui.show(popup, Overlay::dropdown().list(list), |ui, _| { let selection = current.filter(|_| query.trim().is_empty()).map_or_else(Selection::default, Selection::single); let shown = List::::options(name.clone()) + .id(list) .selection(Bind::given(selection)) .filter(&query, |key| labels[*key].clone()) .ranked(true) diff --git a/crates/ui-widgets/src/pickers/select.rs b/crates/ui-widgets/src/pickers/select.rs index 4418fcf29..9453007c7 100644 --- a/crates/ui-widgets/src/pickers/select.rs +++ b/crates/ui-widgets/src/pickers/select.rs @@ -315,8 +315,8 @@ impl Component for Select<'_, T> { let held = self.position(cx.value()); let (searchable, dress, picture) = (self.searchable, self.decorate.as_ref(), self.picture.as_ref()); let mut header_menu = self.header_menu.as_mut(); - let events = ui.show(popup, Overlay::dropdown(), |ui, body| { - let list = ui.scope_id().with("options"); + let list = cx.id.with("options"); + let events = ui.show(popup, Overlay::dropdown().list(list), |ui, body| { let mut query = String::new(); if searchable || header_menu.is_some() { let _ = Row::new().show(ui, |ui, _| { diff --git a/crates/ui-widgets/src/pickers/tree_picker.rs b/crates/ui-widgets/src/pickers/tree_picker.rs index 3d6d0fb97..2ffff5b37 100644 --- a/crates/ui-widgets/src/pickers/tree_picker.rs +++ b/crates/ui-widgets/src/pickers/tree_picker.rs @@ -357,10 +357,10 @@ impl Component for TreePicker<'_, K> { } }; // Folders open and close inside the popup, so activating one must not close it: a pick closes it below. - let overlay = Overlay::dropdown().dismiss(Dismiss::new()); + let list_id = cx.id.with("tree"); + let overlay = Overlay::dropdown().dismiss(Dismiss::new()).list(list_id); let events = ui.show(popup, overlay, |ui, body| { let fresh = Popup::opening(popup, cx); - let list_id = ui.scope_id().with("tree"); let query = search::show(ui, body, popup, cx, &name, list_id); if fresh { cx.memory::>().open = tree.opened_for(chosen.as_ref()); diff --git a/crates/ui-widgets/src/popover/popup_list.rs b/crates/ui-widgets/src/popover/popup_list.rs index 4fcf84c16..b43ff11f7 100644 --- a/crates/ui-widgets/src/popover/popup_list.rs +++ b/crates/ui-widgets/src/popover/popup_list.rs @@ -6,7 +6,7 @@ use crate::runtime::env::Label; use crate::runtime::overlay::CloseReason; use crate::style::{ComponentStyle, StyleCx, SurfaceKind}; use craft_behavior::{Metrics, Sense}; -use egui::Response; +use egui::{Id, Response}; #[cfg(feature = "suite")] pub(crate) mod stories; @@ -23,7 +23,8 @@ pub struct PopupListStyle { /// /// - The popup is at least as wide as its opener and capped to a share of the viewport. /// - The opener keeps the keyboard focus. While the popup is open the host hands the opener's list keys (Up, Down, -/// Page, Home, End, Enter and type-ahead on an opener that is not a text field) to the list. +/// Page, Home, End, Enter and type-ahead on an opener that is not a text field) to the list named with +/// [`PopupList::list`]. /// - Activating a row closes the popup with `CloseReason::Activated`; Escape and a press outside close it too. /// - Open state: the caller's binding. The output's `closed` says why it closed in this pass. /// - The popup is a group; the caller's `List::options` is the listbox inside it. @@ -57,6 +58,17 @@ impl<'a> PopupList<'a> { self.props = self.props.mount(Mount::Layer(overlay.opened_by(opener, Trigger::Manual))); self } + + /// The caller's `List`, by the id it is shown with: it takes the opener's list keys while the popup is open. + #[must_use] + pub fn list(mut self, list: Id) -> Self { + let overlay = match std::mem::take(&mut self.props.mount) { + Mount::Layer(overlay) => overlay, + _ => Overlay::dropdown(), + }; + self.props = self.props.mount(Mount::Layer(overlay.list(list))); + self + } } impl Component for PopupList<'_> { diff --git a/crates/ui-widgets/src/popover/popup_list/stories.rs b/crates/ui-widgets/src/popover/popup_list/stories.rs index 3f2e79f44..7e5a909d5 100644 --- a/crates/ui-widgets/src/popover/popup_list/stories.rs +++ b/crates/ui-widgets/src/popover/popup_list/stories.rs @@ -2,7 +2,7 @@ use super::PopupList; use crate::buttons::Button; -use crate::contract::{Show as _, ShowCollection as _, ShowContainer as _}; +use crate::contract::{Props as _, Show as _, ShowCollection as _, ShowContainer as _}; use crate::layout_components::Stack; use crate::list::{List, ListRow}; use crate::testing::{Drive, StoryData, StoryOutput, story}; @@ -38,10 +38,12 @@ fn popup_list(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let names = ["Round", "Square", "Chisel", "Fan"]; let shown = Stack::new().fill().show(ui, |ui, _| { let opener = Button::new("Brush tip").show(ui); + let list = opener.id.with("tips"); PopupList::new("Brush tips", data.bind("open", true)) .opened_by(&opener) + .list(list) .show(ui, |ui, _| { - List::::options("Brush tips").show_items(ui, &Names(&names), &mut |key, _| ListRow::new(names[*key]).option()); + List::::options("Brush tips").id(list).show_items(ui, &Names(&names), &mut |key, _| ListRow::new(names[*key]).option()); }) .output .response diff --git a/crates/ui-widgets/src/text/completions.rs b/crates/ui-widgets/src/text/completions.rs index 79095b70d..d9828ce14 100644 --- a/crates/ui-widgets/src/text/completions.rs +++ b/crates/ui-widgets/src/text/completions.rs @@ -2,7 +2,7 @@ //! on a row hands the pick back to the adapter, which accepts it in its next run. use crate::contract::{ - CollectionEvent, Component, Cx, Overlay, OverlayUi, Overlays as _, Show as _, ShowCollection as _, WordSetCx as _, lead_position, + CollectionEvent, Component, Cx, Overlay, OverlayUi, Overlays as _, Props as _, Show as _, ShowCollection as _, WordSetCx as _, lead_position, }; use crate::display::{Divider, Text}; use crate::list::{List, ListRow}; @@ -59,10 +59,11 @@ pub(crate) fn show(ui: &mut OverlayUi<'_>, cx: &mut Cx labels.iter().for_each(|label| label.as_str().hash(&mut hasher)); hasher.finish() }; - let focus = Overlay::dropdown().focus.list_enter(enter); + let list_id = cx.id.with("completions"); + let focus = Overlay::dropdown().focus.list_enter(enter).list(list_id); let overlay = Overlay::dropdown().focus(focus); let events = ui.show(popup, overlay, |ui, _| { - let list = List::options(name.clone()).rows_stamp(stamp).show_items(ui, &Candidates { labels: &labels }, &mut |at, _| { + let list = List::options(name.clone()).id(list_id).rows_stamp(stamp).show_items(ui, &Candidates { labels: &labels }, &mut |at, _| { let row = ListRow::new(labels[*at].clone()).option().role(TypeRole::Mono).lead_role(TypeRole::Mono); match (&marks[*at], &widest) { (Some(mark), Some(widest)) => row.lead_text(mark.clone(), widest.clone()), diff --git a/crates/ui-widgets/src/text/number/popups.rs b/crates/ui-widgets/src/text/number/popups.rs index e9626f7c6..90a7a1be6 100644 --- a/crates/ui-widgets/src/text/number/popups.rs +++ b/crates/ui-widgets/src/text/number/popups.rs @@ -26,10 +26,12 @@ impl<'a, V: NumberValue + 'static> NumberField<'a, V> { }; let marked = (0..rows.len()).find(|at| held(*at)); let name = cx.label(); - let events = ui.show(popup, Overlay::dropdown(), |ui, _| { + let list = cx.id.with("presets"); + let events = ui.show(popup, Overlay::dropdown().list(list), |ui, _| { let selection = marked.map_or_else(Selection::default, |at| Selection::new(vec![at], Some(at), Some(at))); let source = Presets { rows, labels: &labels }; List::::options(name.clone()) + .id(list) .selection(Bind::given(selection)) .show_items(ui, &source, &mut |at, _| { if rows[*at].is_separator() { diff --git a/docs/contract-api.md b/docs/contract-api.md index a5d810da1..29fb7ce0c 100644 --- a/docs/contract-api.md +++ b/docs/contract-api.md @@ -2389,6 +2389,7 @@ The component traits, context types, `PaintUi`, `Looks`, the pipeline and its st - `pub fn moves_window(mut self, on: bool) -> Overlay`. Sets whether the move band moves the OS window the layer is shown in, in place of the layer. - `pub fn moved_by_app(mut self) -> Overlay`. The app moves the OS window from the move band ([`Overlay::moved_by_app`]). - `pub fn focus(mut self, focus: FocusPolicy) -> Overlay`. Sets the focus scope. + - `pub fn list(mut self, list: Id) -> Overlay`. The collection in a dropdown that takes its opener's list keys (`FocusPolicy::list`). A dropdown that holds a - `pub fn dismiss(mut self, dismiss: Dismiss) -> Overlay`. Sets the dismissal rules. - `pub fn motion(mut self, motion: MotionSpec) -> Overlay`. Sets the open and close motion. - `pub fn opened_by(mut self, anchor: &Response, trigger: Trigger) -> Overlay`. Records an opener. An anchored layer hangs from the rect of the opener that opened it, on the side and @@ -2702,6 +2703,7 @@ Chapter 15: the `Overlay` data, its presets, popup handles, `OverlayUi` and the - `pub fn moves_window(mut self, on: bool) -> Overlay`. Sets whether the move band moves the OS window the layer is shown in, in place of the layer. - `pub fn moved_by_app(mut self) -> Overlay`. The app moves the OS window from the move band ([`Overlay::moved_by_app`]). - `pub fn focus(mut self, focus: FocusPolicy) -> Overlay`. Sets the focus scope. + - `pub fn list(mut self, list: Id) -> Overlay`. The collection in a dropdown that takes its opener's list keys (`FocusPolicy::list`). A dropdown that holds a - `pub fn dismiss(mut self, dismiss: Dismiss) -> Overlay`. Sets the dismissal rules. - `pub fn motion(mut self, motion: MotionSpec) -> Overlay`. Sets the open and close motion. - `pub fn opened_by(mut self, anchor: &Response, trigger: Trigger) -> Overlay`. Records an opener. An anchored layer hangs from the rect of the opener that opened it, on the side and @@ -3889,7 +3891,7 @@ The focus service: stops, scopes and roving groups registered per pass, the focu - `pub const fn contains(self, command: EditCommand) -> bool`. Whether the set has `command`. - `craft_ui::runtime::focus::EndKey`: `pub enum EndKey { LastItem, EndMode }`. What a bare End means for a focused component. - `craft_ui::runtime::focus::EnterRule`: `pub enum EnterRule { Pass, Submit }`. What Enter does in a scope after the focused component declined it. -- `craft_ui::runtime::focus::FocusPolicy`: `pub struct FocusPolicy { pub kind: ScopeKind, pub trap: bool, pub restore: bool, pub initial: Initial, pub enter: EnterRule, pub cancel: CancelRule, pub pages: bool, pub list_enter: bool }`. A focus scope's rules. +- `craft_ui::runtime::focus::FocusPolicy`: `pub struct FocusPolicy { pub kind: ScopeKind, pub trap: bool, pub restore: bool, pub initial: Initial, pub enter: EnterRule, pub cancel: CancelRule, pub pages: bool, pub list_enter: bool, pub list: Option }`. A focus scope's rules. - `pub const NONE: FocusPolicy`. A scope that changes nothing. - `pub const PANE: FocusPolicy`. A pane: an F6 stop. - `pub const DIALOG: FocusPolicy`. A dialog: Tab stays inside, Enter submits, Escape closes. @@ -3901,6 +3903,7 @@ The focus service: stops, scopes and roving groups registered per pass, the focu - `pub const fn initial(mut self, initial: Initial) -> Self`. Where focus goes first. - `pub const fn cancel(mut self, cancel: CancelRule) -> Self`. What Escape does. - `pub const fn list_enter(mut self, on: bool) -> Self`. Whether Enter on a text opener activates the layer's list. + - `pub const fn list(mut self, list: Id) -> Self`. The collection in the layer that takes the opener's list keys. - `pub const fn pages(mut self, on: bool) -> Self`. Whether Ctrl+Tab cycles pages. - `craft_ui::runtime::focus::FocusTarget`: `pub enum FocusTarget { Id(Id), Part(Id, PartRef), Doc(DocKey) }`. What `runtime::focus::request` focuses. - `craft_ui::runtime::focus::Initial`: `pub enum Initial { Keep, First, FirstField, Default, Key(&'static str), KeepAnchor }`. Where focus goes the first time a scope is pushed. From 49988e9187eccc21686097c259649419f98826d0 Mon Sep 17 00:00:00 2001 From: aiongg Date: Sun, 22 Nov 2026 19:10:03 +0000 Subject: [PATCH 2/2] A dropdown's named list takes the keys only while it is shown dropdown_target hands the opener's list keys to the named collection only when it registered a stop in the last pass, so a dropdown that shows no list (the font picker with no matching family) leaves Enter with the field, which commits the typed name. The font picker names its family list on every pass. The trial covers the no-match case with the class column shown. --- crates/font-manager/src/picker/rows.rs | 8 ++----- crates/font-manager/tests/it/picker.rs | 23 +++++++++++++++---- .../ui-contract/src/contract/overlay/props.rs | 3 ++- .../src/runtime/focus/registration.rs | 7 +++--- 4 files changed, 26 insertions(+), 15 deletions(-) diff --git a/crates/font-manager/src/picker/rows.rs b/crates/font-manager/src/picker/rows.rs index 5bbdb8fdd..2bd3298cc 100644 --- a/crates/font-manager/src/picker/rows.rs +++ b/crates/font-manager/src/picker/rows.rs @@ -389,13 +389,9 @@ pub(super) fn popup<'a>(picker: &mut FontPicker<'a>, ui: &mut OverlayUi<'_>, cx: let mut stars: Vec<(String, bool)> = Vec::new(); let mut over_rows = false; let mut under_pointer: Option = None; - // The field's list keys go to the family list, not to the class column beside it. Without matches no list shows, - // and the field keeps Enter (a plain box applies the typed name). + // The field's list keys go to the family list, not to the class column beside it. let families = cx.id.with("families"); - let mut overlay = Overlay::dropdown().dismiss(Dismiss::new().focus_left(true)); - if !slots.is_empty() { - overlay = overlay.list(families); - } + let overlay = Overlay::dropdown().dismiss(Dismiss::new().focus_left(true)).list(families); let events = ui.show(popup, overlay, |ui, _| { let mut events = Vec::new(); Stack::new().show(ui, |ui, _| { diff --git a/crates/font-manager/tests/it/picker.rs b/crates/font-manager/tests/it/picker.rs index 141ff5730..c1c4b8160 100644 --- a/crates/font-manager/tests/it/picker.rs +++ b/crates/font-manager/tests/it/picker.rs @@ -9,7 +9,7 @@ use craft_behavior::{PartKey, Preview}; use craft_font_manager::craft_font_catalog::{Limits, Os, Source}; use craft_font_manager::missing::MissingFontsWords; use craft_font_manager::picker::{ - FamilyInfos, FontFilters, FontPicker, FontPickerKind, FontPickerSettings, FontPickerWords, MissingAdvice, SIMILAR_COUNT, family_rows, + FamilyInfos, FontClass, FontFilters, FontPicker, FontPickerKind, FontPickerSettings, FontPickerWords, MissingAdvice, SIMILAR_COUNT, family_rows, }; use craft_font_manager::{Catalog, FontEnv}; use craft_ui::buttons::ControlPlace; @@ -200,7 +200,8 @@ fn a_click_on_the_focused_closed_field_selects_the_name_so_typing_replaces_it() /// A picker as the character controls host it: a given value, the filters (so the class column shows beside the /// families) and a preview text. The field's list keys go to the family list: Enter on a typed part of a name picks the -/// best match, and Down moves the family row, which previews, before Enter picks it. +/// best match, and Down moves the family row, which previews, before Enter picks it. While no family row shows, Enter is +/// the field's: it commits a typed family name and leaves the class column as it was. #[trial] fn with_the_class_column_shown_the_field_keys_move_and_pick_in_the_family_list() { #[derive(Default)] @@ -212,11 +213,13 @@ fn with_the_class_column_shown_the_field_keys_move_and_pick_in_the_family_list() let seen = Rc::clone(&reported); let catalog = catalog_of(&[INTER_REGULAR, epaint_default_fonts::HACK_REGULAR, epaint_default_fonts::UBUNTU_LIGHT]); let mut settings = FontPickerSettings::default(); - let mut filters = FontFilters::default(); + let filters = Rc::new(RefCell::new(FontFilters::default())); + let held = Rc::clone(&filters); let mut fx = Fixture::new(move |ui| { - let out = FontPicker::new("Font family", Bind::given("Inter".to_owned()), &catalog) + let mut filters = held.borrow_mut(); + let out = FontPicker::new("Font family", Bind::given("Ubuntu".to_owned()), &catalog) .settings(Bind::bound(&mut settings)) - .filters(Bind::bound(&mut filters)) + .filters(Bind::bound(&mut *filters)) .preview_text("Sample") .show(ui); let mut seen = seen.borrow_mut(); @@ -252,6 +255,16 @@ fn with_the_class_column_shown_the_field_keys_move_and_pick_in_the_family_list() fx.key(Key::Enter, Modifiers::NONE); fx.settle(); assert_eq!(reported.borrow().picks.last(), Some(&keyed), "Enter picks the family row the keys reached"); + + // The host's filters list monospaced families only, Hack alone, so the exact name "Inter" matches no row. + filters.borrow_mut().class = Some(FontClass::Monospace); + type_in(&mut fx, "Inter"); + assert!(present(&fx, words.all_classes.as_str()), "the class column shows"); + assert!(fx.by_label("Favorite Inter").is_none() && fx.by_label("Favorite Hack").is_none(), "no family row shows"); + fx.key(Key::Enter, Modifiers::NONE); + fx.settle(); + assert_eq!(reported.borrow().picks.last().map(String::as_str), Some("Inter"), "the field commits the typed name"); + assert_eq!(filters.borrow().class, Some(FontClass::Monospace), "the class column is unchanged"); } /// A `Plain` picker over `family` with `recent` families, `actions` rows and the face `shown_as`. diff --git a/crates/ui-contract/src/contract/overlay/props.rs b/crates/ui-contract/src/contract/overlay/props.rs index abfd74cff..22fec6b94 100644 --- a/crates/ui-contract/src/contract/overlay/props.rs +++ b/crates/ui-contract/src/contract/overlay/props.rs @@ -453,7 +453,8 @@ impl Overlay { } /// The collection in a dropdown that takes its opener's list keys (`FocusPolicy::list`). A dropdown that holds a - /// list names it each pass it is shown; the opener keeps keys no layer names a list for. + /// list names it each pass it is shown; the opener keeps the keys while the named list is not shown, and in a + /// dropdown that names none. pub fn list(mut self, list: Id) -> Overlay { self.focus = self.focus.list(list); self diff --git a/crates/ui-contract/src/runtime/focus/registration.rs b/crates/ui-contract/src/runtime/focus/registration.rs index cc2ed5cc3..d03d2bb75 100644 --- a/crates/ui-contract/src/runtime/focus/registration.rs +++ b/crates/ui-contract/src/runtime/focus/registration.rs @@ -181,8 +181,8 @@ impl Group { } /// The innermost open `KeepAnchor` layer opened from the stop `opener`, the collection the layer names for its -/// opener's list keys (`FocusPolicy::list`) and the layer's policy. `None` while no such layer is open or it names no -/// collection. +/// opener's list keys (`FocusPolicy::list`) and the layer's policy. `None` while no such layer is open, or it names no +/// collection or one that registered no stop in the last pass (a list not shown leaves the keys with the opener). pub(crate) fn dropdown_target(ctx: &Context, opener: StopKey) -> Option<(Id, Id, FocusPolicy)> { with(ctx, |state| { let layer = @@ -190,7 +190,8 @@ pub(crate) fn dropdown_target(ctx: &Context, opener: StopKey) -> Option<(Id, Id, state.last_scopes.get(*id).is_some_and(|scope| scope.policy.initial == Initial::KeepAnchor && scope.opener == Some(opener)) })?; let policy = state.last_scopes.get(&layer)?.policy; - policy.list.map(|list| (layer, list, policy)) + let shown = |list: &Id| state.last_stops.iter().any(|rec| rec.stop.id == *list); + policy.list.filter(shown).map(|list| (layer, list, policy)) }) }