diff --git a/crates/ui-contract/src/contract/collection.rs b/crates/ui-contract/src/contract/collection.rs index 754053cdc..623ac6612 100644 --- a/crates/ui-contract/src/contract/collection.rs +++ b/crates/ui-contract/src/contract/collection.rs @@ -12,7 +12,8 @@ pub use events::{ CollectionEvent, ColumnEvent, ColumnRequest, ContextMenuRequest, HeaderMenuRequest, ListDrag, OpenChange, Rename, ReorderMove, ToggleEdit, }; pub use props::{ - ActivateOn, CollectionProps, DragPayload, ItemKeys, LeadOnOpen, LeadSource, ReorderRule, RowHeight, RowPressed, RowState, SelectMode, SpaceAction, + ActivateOn, CollectionProps, DragPayload, HoverLead, ItemKeys, LeadOnOpen, LeadSource, ReorderRule, RowHeight, RowPressed, RowState, SelectMode, + SpaceAction, }; pub(crate) use response::Gestures; pub(crate) use response::ItemsOut; diff --git a/crates/ui-contract/src/contract/collection/props.rs b/crates/ui-contract/src/contract/collection/props.rs index 3c464e0b4..db45f0d04 100644 --- a/crates/ui-contract/src/contract/collection/props.rs +++ b/crates/ui-contract/src/contract/collection/props.rs @@ -119,6 +119,21 @@ pub enum LeadOnOpen { First, } +/// What the pointer moving over the rows changes. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +#[non_exhaustive] +pub enum HoverLead { + /// Nothing: only a press moves the lead. + #[default] + Off, + /// The lead moves to the row under the pointer; the selection stays until a pick (click, Enter, or a key move): + /// option lists, where the selection is the chosen value. + Lead, + /// The lead moves and picks the row, as the arrow keys do: menus and the undo list, where the hover range is + /// the point. + LeadAndSelection, +} + /// How tall a collection's rows are. Read-only display tables (a metadata table) take text rows; lists the user picks /// from or navigates keep control-high rows, a pointer target as tall as a control. A row with a thumbnail or a /// detail line is as tall as those need either way. @@ -173,9 +188,8 @@ pub struct CollectionProps { pub allow_empty: bool, /// Where the lead starts. pub lead_on_open: LeadOnOpen, - /// The lead, and the selection with it as for the arrow keys, moves to the row under the pointer when the pointer - /// moves: menus, popup lists and the undo list. - pub lead_follows_hover: bool, + /// What the pointer moving over the rows changes: the lead, and for some lists the selection too. + pub hover_lead: HoverLead, /// The arrow keys select the row they move the lead to. Off: they move the lead only, and Space, Enter or a click /// selects it (a swatch table, a tab strip, a preset browser); Enter then activates it as well. pub arrows_select: bool, @@ -299,7 +313,7 @@ impl CollectionProps { striped: false, allow_empty: false, lead_on_open: LeadOnOpen::Selected, - lead_follows_hover: false, + hover_lead: HoverLead::Off, arrows_select: true, focus_on_press: true, rows_stamp: None, @@ -434,10 +448,10 @@ impl CollectionProps { self } - /// The lead follows the pointer over the rows. + /// What the pointer moving over the rows changes. #[must_use] - pub const fn lead_follows_hover(mut self, on: bool) -> Self { - self.lead_follows_hover = on; + pub const fn hover_lead(mut self, hover: HoverLead) -> Self { + self.hover_lead = hover; self } diff --git a/crates/ui-contract/src/contract/items/hooks.rs b/crates/ui-contract/src/contract/items/hooks.rs index a3b6225fd..40671bf6f 100644 --- a/crates/ui-contract/src/contract/items/hooks.rs +++ b/crates/ui-contract/src/contract/items/hooks.rs @@ -8,7 +8,7 @@ use super::{Held, ITEM_NODE, ItemsHooks, LastLayout, Lead, Placement, Renaming, use super::{request, rows}; use crate::contract::ValueOf; use crate::contract::collection::{ - CollectionEvent, ColumnLayout, ContextMenuRequest, DropAt, Gestures, ItemsOut, OpenChange, PendingDrop, Rename, RowState, ShownRow, + CollectionEvent, ColumnLayout, ContextMenuRequest, DropAt, Gestures, HoverLead, ItemsOut, OpenChange, PendingDrop, Rename, RowState, ShownRow, }; use crate::contract::cx::{BodyCx, CxCore, Pointer as _}; use crate::contract::run::{Measuring, ShapeHooks, ShowAt}; @@ -277,8 +277,8 @@ impl ShapeHooks for ItemsHooks<'_, '_, C> { let seen = Placement { offset: ScrollPos { x: self.offset_x_at_read, y: self.offset_at_read }, ..at }; self.edge_step(cx); self.press_step(comp, cx, &props, &layout, seen); - if props.lead_follows_hover { - self.hover_lead(comp, cx, &layout, seen); + if props.hover_lead != HoverLead::Off { + self.hover_lead(comp, cx, &layout, seen, props.hover_lead); } if props.select_under_drag { self.drag_select(comp, cx, &layout, seen); diff --git a/crates/ui-contract/src/contract/items/lead.rs b/crates/ui-contract/src/contract/items/lead.rs index 88e069872..6d69fd2ea 100644 --- a/crates/ui-contract/src/contract/items/lead.rs +++ b/crates/ui-contract/src/contract/items/lead.rs @@ -1,7 +1,7 @@ //! The lead and the selection: picks, ranges, and where the lead starts and follows. use super::{ItemsHooks, Placement}; -use crate::contract::collection::{CollectionEvent, LeadOnOpen, LeadSource, ListDrag, SelectMode}; +use crate::contract::collection::{CollectionEvent, HoverLead, LeadOnOpen, LeadSource, ListDrag, SelectMode}; use crate::contract::{Collection, Cx}; use craft_behavior::{Flow, ItemLayout, PointerEvent, SelectionChange, SelectionModel}; use egui::{Pos2, Rect, Vec2}; @@ -135,10 +135,10 @@ impl ItemsHooks<'_, '_, C> { layout.hit(local, offset).filter(|&row| self.source.enabled(row) && !self.source.is_section(row) && layout.shown(row)) } - /// The lead follows a moving pointer over the rows, and the selection follows the lead the way it does for the - /// arrow keys. A pass moves the pointer when it holds a move event: the pointer entering over a row moves the lead + /// The lead follows a moving pointer over the rows; with `HoverLead::LeadAndSelection` the selection follows the + /// lead the way it does for the arrow keys. A pass moves the pointer when it holds a move event: the pointer entering over a row moves the lead /// with no movement to measure, and a modifier change under a resting pointer leaves the lead where the keys put it. - pub(super) fn hover_lead(&mut self, comp: &C, cx: &Cx<'_, C>, layout: &C::ItemLayout, at: Placement) { + pub(super) fn hover_lead(&mut self, comp: &C, cx: &Cx<'_, C>, layout: &C::ItemLayout, at: Placement, hover: HoverLead) { let moved = crate::runtime::pointer::with_pass_raw(&cx.core.ctx, |pass| pass.events.iter().any(|event| matches!(event, PointerEvent::Moved { .. }))); if !moved { @@ -147,7 +147,9 @@ impl ItemsHooks<'_, '_, C> { let Some(row) = Self::hovered_pos(cx).and_then(|pos| self.row_under(layout, at, pos)) else { return }; if self.lead.key.as_ref() != Some(&self.source.key(row)) { self.move_lead(row, LeadSource::Pointer); - self.pick(comp, row, false, false); + if hover == HoverLead::LeadAndSelection { + self.pick(comp, row, false, false); + } } } diff --git a/crates/ui-contract/src/contract/mod.rs b/crates/ui-contract/src/contract/mod.rs index 14ea466f7..75f800d74 100644 --- a/crates/ui-contract/src/contract/mod.rs +++ b/crates/ui-contract/src/contract/mod.rs @@ -60,8 +60,8 @@ pub use body::{STRIP_BACK, STRIP_FORWARD}; pub use collection::{ ActivateOn, CollectionEvent, CollectionProps, CollectionResponse, ColumnAlign, ColumnEvent, ColumnKind, ColumnLayout, ColumnRemove, ColumnRequest, ColumnSort, ColumnSpec, ContextMenuRequest, DragPayload, DropAt, DropKind, DropPayload, DropPlace, EdgePress, HeaderMenuRequest, - ItemKeys, LeadOnOpen, LeadSource, ListDrag, OpenChange, PendingDrop, Rename, ReorderMove, ReorderRule, RowHeight, RowPressed, RowState, - SelectMode, ShownRow, SpaceAction, ToggleColumn, ToggleEdit, + HoverLead, ItemKeys, LeadOnOpen, LeadSource, ListDrag, OpenChange, PendingDrop, Rename, ReorderMove, ReorderRule, RowHeight, RowPressed, + RowState, SelectMode, ShownRow, SpaceAction, ToggleColumn, ToggleEdit, }; #[doc(hidden)] pub use container::InBar; diff --git a/crates/ui-macros/src/props.rs b/crates/ui-macros/src/props.rs index 158382be1..e334f26f8 100644 --- a/crates/ui-macros/src/props.rs +++ b/crates/ui-macros/src/props.rs @@ -175,7 +175,7 @@ const COLLECTION: &[Forwarded] = &[ ("striped", "bool", "Odd rows are tinted: the item reads `ItemCx::row.striped`."), ("allow_empty", "bool", "A press on the empty space below the rows empties the selection."), ("lead_on_open", "$ui::contract::LeadOnOpen", "Where the lead lands when rows first show."), - ("lead_follows_hover", "bool", "The lead, and the selection with it, moves to the row under the pointer when the pointer moves."), + ("hover_lead", "$ui::contract::HoverLead", "What the pointer moving over the rows changes: nothing, the lead, or the lead and the selection."), ("arrows_select", "bool", "The arrow keys select the row they move the lead to; off, they move the lead only."), ("arrows_wrap", "bool", "The arrow keys wrap from the last item to the first and back."), ("type_ahead", "bool", "Typed letters move the lead to the next item whose text starts with them; off, they stay shortcut candidates."), diff --git a/crates/ui-widgets/src/list/undo.rs b/crates/ui-widgets/src/list/undo.rs index bd77b65e7..06277827e 100644 --- a/crates/ui-widgets/src/list/undo.rs +++ b/crates/ui-widgets/src/list/undo.rs @@ -4,8 +4,8 @@ use super::view_words::UndoWords; use super::{ListRow, RowEvent}; use crate::contract::{ - ActivateOn, Bind, BindMut, Collection, CollectionEvent, CollectionProps, CollectionResponse, Common, Component, ContainerProps, Cx, Props as _, - Role, RowState, SelectMode, Sizing, + ActivateOn, Bind, BindMut, Collection, CollectionEvent, CollectionProps, CollectionResponse, Common, Component, ContainerProps, Cx, HoverLead, + Props as _, Role, RowState, SelectMode, Sizing, }; use crate::runtime::env::Label; use crate::style::{ComponentStyle, StyleCx}; @@ -141,7 +141,7 @@ impl<'a> UndoList<'a> { UndoList { common: Common::new(), props: ContainerProps::new().sizing(Sizing::Fill), - items: CollectionProps::new().select(SelectMode::Single).activate(ActivateOn::Click).lead_follows_hover(true), + items: CollectionProps::new().select(SelectMode::Single).activate(ActivateOn::Click).hover_lead(HoverLead::LeadAndSelection), selection: None, } .label(label) diff --git a/crates/ui-widgets/src/list/view.rs b/crates/ui-widgets/src/list/view.rs index b2a22580d..541af3cb5 100644 --- a/crates/ui-widgets/src/list/view.rs +++ b/crates/ui-widgets/src/list/view.rs @@ -7,7 +7,7 @@ use super::predicate::{DropTest, RowTest, RowText}; use super::{ListRow, RowEvent}; use crate::contract::{ Access, ActivateOn, Bind, BindMut, Collection, CollectionProps, CollectionResponse, ColumnLayout, Common, Component, ContainerProps, Cx, - DropKind, EdgePress, EditResponse, LeadOnOpen, ListDrag, Props as _, Role, RowState, SelectMode, Sizing, + DropKind, EdgePress, EditResponse, HoverLead, LeadOnOpen, ListDrag, Props as _, Role, RowState, SelectMode, Sizing, }; use crate::runtime::env::{Env, Label}; use crate::style::{ComponentStyle, StyleCx}; @@ -119,7 +119,7 @@ impl<'a, K: Clone + Eq + Hash + Send + Sync + 'static> List<'a, K> { .select(SelectMode::Single) .activate(ActivateOn::Click) .lead_on_open(LeadOnOpen::First) - .lead_follows_hover(true) + .hover_lead(HoverLead::Lead) .focus_on_press(false) .type_ahead(true) .sizing(Sizing::Content) diff --git a/crates/ui-widgets/tests/it/pickers/select_list.rs b/crates/ui-widgets/tests/it/pickers/select_list.rs index 801ccefeb..4a129d72a 100644 --- a/crates/ui-widgets/tests/it/pickers/select_list.rs +++ b/crates/ui-widgets/tests/it/pickers/select_list.rs @@ -297,3 +297,27 @@ fn the_wheel_steps_the_open_select_a_line_an_entry_and_commits_when_it_stops() { assert!(!steps.contains(&"cancel"), "and puts nothing back: {steps:?}"); assert_eq!(*value.borrow(), 4); } + +/// The pointer over a row of an open list moves the lead only: the held value stays the selected option in every +/// frame, and Enter then chooses the row the pointer is on. +#[trial] +fn hovering_a_row_moves_the_lead_and_leaves_the_selected_option_held() { + use craft_ui::testing::pointer::moved; + let mut fixture = focused(); + fixture.key(Key::ArrowDown, Modifiers::ALT); + fixture.settle(); + assert!(list_open(&mut fixture), "Alt+Down opens the list"); + let options = fixture.by_role(Role::LIST_BOX_OPTION); + assert_eq!(options.len(), 4, "the list shows its four options"); + let held = options[0].node; + let under_pointer = options[2].center(); + for step in 0..3 { + fixture.frame(if step == 0 { vec![moved(under_pointer)] } else { Vec::new() }); + let selected: Vec<_> = + fixture.by_role(Role::LIST_BOX_OPTION).into_iter().filter(|row| fixture.tree().selected_of(row.node) == Some(true)).collect(); + assert!(selected.len() == 1 && selected[0].node == held, "frame {step}: the held row stays the selected option"); + } + fixture.key(Key::Enter, Modifiers::NONE); + fixture.settle(); + assert_eq!(value(&mut fixture), 3, "the lead followed the pointer, so Enter chooses the row under it"); +}