diff --git a/crates/automation/src/ui/actions/body.rs b/crates/automation/src/ui/actions/body.rs index e77cda82d..49113002b 100644 --- a/crates/automation/src/ui/actions/body.rs +++ b/crates/automation/src/ui/actions/body.rs @@ -185,7 +185,7 @@ fn show_tree(ui: &mut Ui, body: &Body<'_>, state: &mut ActionsPanelState, sessio } let shown: &ActionsPanelState = state; let out = widget.show_items(ui, &rows, &mut |key, item| { - let mut row = build_row(body, shown, key).cells(item.cells()).state(item.row).depth(rows.depth_at(item.at)); + let mut row = build_row(body, shown, key).cells(item.cells()).state(item.row.clone()).depth(rows.depth_at(item.at)); for cell in cells(body, key) { row = row.toggle(cell); } diff --git a/crates/draw/src/ui/brush_settings/sections.rs b/crates/draw/src/ui/brush_settings/sections.rs index 0151b4fb4..6e72b16b9 100644 --- a/crates/draw/src/ui/brush_settings/sections.rs +++ b/crates/draw/src/ui/brush_settings/sections.rs @@ -221,7 +221,8 @@ impl ItemSource> for Rows<'_, K> { /// row and precedes the first switch row, taking room and having no AccessKit node. The link row has the brush /// icon. /// - **Pointer.** A click on a pane row reports `Select` and, if its box is clear, `Enable { on: true }`; the box -/// toggles only the flag; the lock toggles only the lock; a click on a switch row toggles it (`Switch`). +/// toggles only the flag; the lock toggles only the lock; a click on a switch row toggles it (`Switch`). A press on the row (not its lock) +/// shows the box pressed. /// - **Keys.** One Tab stop. The list's own keys (Up, Down, Home, End, Page Up and Page Down) move the lead; onto a /// pane row it also reports `Select`. Space toggles the lead's box or switch, Shift+Space its lock; Enter selects a /// pane row and turns it on. @@ -299,12 +300,12 @@ impl<'a, K: Value + Eq + Hash> BrushSectionList<'a, K> { BrushSectionKind::Switch => { uses.insert(0, CHECK); let state = ToggleState::of(section.enabled == Some(true)); - row = row.toggle(ToggleCell::check(CHECK, state, switch_name.clone())); + row = row.toggle(ToggleCell::check(CHECK, state, switch_name.clone()).row_press()); } BrushSectionKind::Pane => { if let Some(on) = section.enabled { uses.insert(0, CHECK); - row = row.toggle(ToggleCell::check(CHECK, ToggleState::of(on), enable_name.clone())); + row = row.toggle(ToggleCell::check(CHECK, ToggleState::of(on), enable_name.clone()).row_press()); } if let Some(locked) = section.locked { uses.push(LOCK); diff --git a/crates/imaging/src/ui/channels/panel.rs b/crates/imaging/src/ui/channels/panel.rs index 2fb806a3c..9239c69c5 100644 --- a/crates/imaging/src/ui/channels/panel.rs +++ b/crates/imaging/src/ui/channels/panel.rs @@ -205,7 +205,7 @@ impl<'a, K: Clone + Eq + Hash + Debug + Send + Sync + 'static> ChannelsPanel<'a, }) .show_items(ui, &order, &mut |_key, item| { let at = item.at; - channel_row(&row_cx, at).cells(item.cells()).state(item.row) + channel_row(&row_cx, at).cells(item.cells()).state(item.row.clone()) }); let mut found = Vec::new(); diff --git a/crates/layers/src/ui/panel/body.rs b/crates/layers/src/ui/panel/body.rs index 51dcc8c4e..afad509ff 100644 --- a/crates/layers/src/ui/panel/body.rs +++ b/crates/layers/src/ui/panel/body.rs @@ -75,7 +75,7 @@ pub(super) fn show_body( list = list.reveal(key); } let out = list.show_items(ui, rows, &mut |key, item| { - let row = build_row(cx, key).cells(item.cells()).state(item.row); + let row = build_row(cx, key).cells(item.cells()).state(item.row.clone()); match rows.rows.get(item.at) { Some(shown) if shown.has_children => row.depth(shown.paint_depth).group(shown.open), Some(shown) => row.depth(shown.paint_depth), diff --git a/crates/ui-contract/src/contract/collection.rs b/crates/ui-contract/src/contract/collection.rs index 48de584cb..3f1c25bcb 100644 --- a/crates/ui-contract/src/contract/collection.rs +++ b/crates/ui-contract/src/contract/collection.rs @@ -7,6 +7,8 @@ mod response; pub use columns::{ColumnAlign, ColumnKind, ColumnSort, ColumnSpec, ToggleColumn}; pub use events::{CollectionEvent, ColumnEvent, ContextMenuRequest, HeaderMenuRequest, ListDrag, OpenChange, Rename, ReorderMove, ToggleEdit}; -pub use props::{ActivateOn, CollectionProps, DragPayload, ItemKeys, LeadOnOpen, LeadSource, ReorderRule, RowState, SelectMode, SpaceAction}; +pub use props::{ + ActivateOn, CollectionProps, DragPayload, ItemKeys, LeadOnOpen, LeadSource, ReorderRule, RowPressed, RowState, SelectMode, SpaceAction, +}; pub(crate) use response::Gestures; pub use response::{CollectionResponse, ShownRow}; diff --git a/crates/ui-contract/src/contract/collection/props.rs b/crates/ui-contract/src/contract/collection/props.rs index c754c0b9c..ed9252890 100644 --- a/crates/ui-contract/src/contract/collection/props.rs +++ b/crates/ui-contract/src/contract/collection/props.rs @@ -489,8 +489,18 @@ impl CollectionProps { } } +/// Where the collection holds a primary press on a row, within slop. +#[derive(Clone, Debug, PartialEq, Eq)] +#[non_exhaustive] +pub enum RowPressed { + /// On the row's own area. + Row, + /// On the cell of this toggle column, which the press flips. + Cell(ColumnKey), +} + /// What an item reads of its place in the collection; the item step joins it to the item's flags at step 12. -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +#[derive(Clone, Debug, Default, PartialEq, Eq)] pub struct RowState { /// The row is selected. pub selected: bool, @@ -512,4 +522,6 @@ pub struct RowState { pub open: bool, /// The pointer is over the row. pub hovered: bool, + /// The collection holds a press on the row, which the item shows on the part it acts on. + pub pressed: Option, } diff --git a/crates/ui-contract/src/contract/cx/parts.rs b/crates/ui-contract/src/contract/cx/parts.rs index 2973bf8e8..7fe70100e 100644 --- a/crates/ui-contract/src/contract/cx/parts.rs +++ b/crates/ui-contract/src/contract/cx/parts.rs @@ -132,6 +132,13 @@ impl PartBuilder<'_> { self } + /// The component holds a press on the part that the part does not sense itself (a collection's press on a row's + /// toggle cell): the part paints pressed. + pub fn held(self, held: bool) -> Self { + self.decl.held = held; + self + } + /// The part is the arrival of the component's part roving group: Tab and Shift+Tab into the group land here, not /// on the first member. The last part declared `selected` wins. pub fn selected(self) -> Self { diff --git a/crates/ui-contract/src/contract/cx/styled.rs b/crates/ui-contract/src/contract/cx/styled.rs index ea654997e..1d3f5ad8d 100644 --- a/crates/ui-contract/src/contract/cx/styled.rs +++ b/crates/ui-contract/src/contract/cx/styled.rs @@ -68,7 +68,7 @@ impl CxCore<'_> { flags |= Flags::FOCUS_VISIBLE; } } - let pressed = result.press.phase.is_press(); + let pressed = result.pressed(); let state = if !self.resolved.enabled || result.inactive { craft_theme::State::Disabled } else if pressed { diff --git a/crates/ui-contract/src/contract/items.rs b/crates/ui-contract/src/contract/items.rs index e9a330328..f7b870fe3 100644 --- a/crates/ui-contract/src/contract/items.rs +++ b/crates/ui-contract/src/contract/items.rs @@ -171,6 +171,8 @@ pub(super) struct ItemsHooks<'s, 'r, C: Collection> { drags: Drags, /// The row a key move brought to the lead in a collection without its own scroll: the page's scroll owner shows it. page_reveal: Option, + /// The row the collection's own press is held on this pass, and where on it. + pressed: Option<(usize, crate::contract::collection::RowPressed)>, } /// The primary-ness, click count, position in the layout's coordinates and scroll offset of a press on a row. @@ -224,6 +226,7 @@ impl<'s, 'r, C: Collection> ItemsHooks<'s, 'r, C> { header: Header::default(), drags: Drags::default(), page_reveal: None, + pressed: None, } } } diff --git a/crates/ui-contract/src/contract/items/hooks.rs b/crates/ui-contract/src/contract/items/hooks.rs index 6b1941a33..b91349702 100644 --- a/crates/ui-contract/src/contract/items/hooks.rs +++ b/crates/ui-contract/src/contract/items/hooks.rs @@ -462,6 +462,7 @@ impl ShapeHooks for ItemsHooks<'_, '_, C> { current: current.as_ref() == Some(&key), open: self.source.has_children(at) && self.source.open(at), hovered: hover_at.is_some_and(|pos| row.contains(pos)), + pressed: self.pressed.as_ref().filter(|(held, _)| *held == at).map(|(_, pressed)| pressed.clone()), ..RowState::default() }; let mut flags = crate::style::Flags::empty(); @@ -495,7 +496,7 @@ impl ShapeHooks for ItemsHooks<'_, '_, C> { key: &key, at, rect: row, - row: state, + row: state.clone(), depth: self.source.depth(at), set: position, row_index: at, @@ -539,6 +540,7 @@ impl ShapeHooks for ItemsHooks<'_, '_, C> { columns: column_keys, toggles: toggle_keys, toggle_keys: self.header.toggle_keys(), + pressed: state.pressed.clone(), })); }), ); diff --git a/crates/ui-contract/src/contract/items/measure.rs b/crates/ui-contract/src/contract/items/measure.rs index 216f6abc1..b9f12f8eb 100644 --- a/crates/ui-contract/src/contract/items/measure.rs +++ b/crates/ui-contract/src/contract/items/measure.rs @@ -112,6 +112,7 @@ impl<'s, 'r, C: Collection> ItemsHooks<'s, 'r, C> { columns: columns.clone(), toggles: toggles.clone(), toggle_keys: Vec::new(), + pressed: None, }; let installed = Env::install_for( ui, diff --git a/crates/ui-contract/src/contract/items/press.rs b/crates/ui-contract/src/contract/items/press.rs index 4ffd7f9fd..0a51ded60 100644 --- a/crates/ui-contract/src/contract/items/press.rs +++ b/crates/ui-contract/src/contract/items/press.rs @@ -2,7 +2,7 @@ use super::drag::SlowClick; use super::{ItemsHooks, Placement, RowPress, SECTION}; -use crate::contract::collection::{ActivateOn, CollectionEvent, CollectionProps, ContextMenuRequest, LeadSource, SelectMode}; +use crate::contract::collection::{ActivateOn, CollectionEvent, CollectionProps, ContextMenuRequest, LeadSource, RowPressed, SelectMode}; use crate::contract::{Collection, Cx, ItemOpener}; use crate::runtime::egui::response; use crate::runtime::env::ArenaId; @@ -53,6 +53,20 @@ impl ItemsHooks<'_, '_, C> { if layout.mirrors() && rtl { flow.to_physical(Rect::from_min_size(at, Vec2::ZERO)).min } else { at } }; let mods_now = crate::runtime::pointer::frame(&ctx).mods; + self.pressed = match cx.core.press.phase { + PressPhase::Down { start, .. } | PressPhase::Held { start, .. } + if start.button == craft_behavior::PointerButton::Primary && local(start.origin).y >= 0.0 => + { + let origin = local(start.origin); + layout.hit(origin, offset).filter(|&at| self.source.enabled(at) && layout.shown(at)).and_then(|at| { + match self.toggle_cell(props, layout, at, offset, origin) { + Some((column, _)) => self.toggle_open(at, &column).then_some((at, RowPressed::Cell(column))), + None => Some((at, RowPressed::Row)), + } + }) + } + _ => None, + }; if let PressPhase::Down { start, .. } = cx.core.press.phase && local(start.origin).y >= 0.0 { diff --git a/crates/ui-contract/src/contract/mod.rs b/crates/ui-contract/src/contract/mod.rs index a2ebef7e4..23841839e 100644 --- a/crates/ui-contract/src/contract/mod.rs +++ b/crates/ui-contract/src/contract/mod.rs @@ -50,7 +50,7 @@ pub use body::pad_of; pub use collection::{ ActivateOn, CollectionEvent, CollectionProps, CollectionResponse, ColumnAlign, ColumnEvent, ColumnKind, ColumnSort, ColumnSpec, ContextMenuRequest, DragPayload, HeaderMenuRequest, ItemKeys, LeadOnOpen, LeadSource, ListDrag, OpenChange, Rename, ReorderMove, ReorderRule, - RowState, SelectMode, ShownRow, SpaceAction, ToggleColumn, ToggleEdit, + RowPressed, RowState, SelectMode, ShownRow, SpaceAction, ToggleColumn, ToggleEdit, }; #[doc(hidden)] pub use container::InBar; diff --git a/crates/ui-contract/src/contract/run/typed.rs b/crates/ui-contract/src/contract/run/typed.rs index 233cea1b4..d177539df 100644 --- a/crates/ui-contract/src/contract/run/typed.rs +++ b/crates/ui-contract/src/contract/run/typed.rs @@ -739,7 +739,8 @@ pub(crate) fn member_show( let resolved = steps::resolved(id, &step1, &common); let enabled = node.enabled && step1.enabled; let hovered = sensed.response.hovered(); - let result = crate::primitives::PartResult { rect: row, press: sensed.press, hovered, hover_pos: None, flags: node.flags, inactive: false }; + let result = + crate::primitives::PartResult { rect: row, press: sensed.press, hovered, hover_pos: None, flags: node.flags, inactive: false, held: false }; let part_state = steps::part_state(&result, enabled, None); let mut flags = part_state.flags | item.flags(); if (shown.as_deref().map(|value| value as &dyn std::any::Any)).and_then(|value| value.downcast_ref::()) == Some(&true) { diff --git a/crates/ui-contract/src/contract/steps/sense.rs b/crates/ui-contract/src/contract/steps/sense.rs index 5594977b2..e28579b7f 100644 --- a/crates/ui-contract/src/contract/steps/sense.rs +++ b/crates/ui-contract/src/contract/steps/sense.rs @@ -29,7 +29,7 @@ pub(crate) fn part_state(result: &crate::primitives::PartResult, enabled: bool, flags |= Flags::FOCUS_VISIBLE; } } - let pressed = result.press.phase.is_press(); + let pressed = result.pressed(); let state = if !enabled || result.inactive { State::Disabled } else if pressed { diff --git a/crates/ui-contract/src/primitives/sensing.rs b/crates/ui-contract/src/primitives/sensing.rs index a44ccaa9f..5b29e1c33 100644 --- a/crates/ui-contract/src/primitives/sensing.rs +++ b/crates/ui-contract/src/primitives/sensing.rs @@ -41,12 +41,26 @@ pub struct PartResult { /// The component declared the part inactive: it paints with the Disabled state and `PaintUi::draw_part` draws data /// colours with the disabled fill, and it keeps sensing. pub inactive: bool, + /// The component holds a press on the part that the part does not sense itself: it paints pressed. + pub held: bool, } impl PartResult { /// What a read returns for a part with no press history: face mode, or a part first registered this pass. - pub const IDLE: PartResult = - PartResult { rect: Rect::NOTHING, press: PressState::IDLE, hovered: false, hover_pos: None, flags: Flags::empty(), inactive: false }; + pub const IDLE: PartResult = PartResult { + rect: Rect::NOTHING, + press: PressState::IDLE, + hovered: false, + hover_pos: None, + flags: Flags::empty(), + inactive: false, + held: false, + }; + + /// The part paints pressed: its own press, or one its component holds on it. + pub fn pressed(&self) -> bool { + self.press.phase.is_press() || self.held + } } /// A text part's result this pass. @@ -105,6 +119,8 @@ pub(crate) struct PartDecl { pub flags: Flags, /// The part paints with the Disabled state and its data colours take the theme's disabled fill. It keeps sensing. pub inactive: bool, + /// The component holds a press on the part (`PartBuilder::held`). + pub held: bool, } impl PartDecl { @@ -120,6 +136,7 @@ impl PartDecl { over: false, flags: Flags::default(), inactive: false, + held: false, } } @@ -831,6 +848,7 @@ fn part_results_in( hover_pos: read.hover_pos, flags: decl.flags, inactive: decl.inactive, + held: decl.held, }, ) }) diff --git a/crates/ui-contract/src/runtime/env.rs b/crates/ui-contract/src/runtime/env.rs index 37866d842..6c3e77d56 100644 --- a/crates/ui-contract/src/runtime/env.rs +++ b/crates/ui-contract/src/runtime/env.rs @@ -237,6 +237,9 @@ pub struct ItemFacts { /// The keys of the toggle columns that have one (`ToggleColumn::key`), which their cells show in the tooltip. #[doc(hidden)] pub toggle_keys: Vec<(craft_behavior::ColumnKey, craft_keymap::Chord)>, + /// Where the collection holds a press on the row (`RowState::pressed`). + #[doc(hidden)] + pub pressed: Option, } impl Scoped { diff --git a/crates/ui-contract/src/testing/story.rs b/crates/ui-contract/src/testing/story.rs index 553b8aec0..138a2547e 100644 --- a/crates/ui-contract/src/testing/story.rs +++ b/crates/ui-contract/src/testing/story.rs @@ -534,6 +534,7 @@ where columns: Vec::new(), toggles: Vec::new(), toggle_keys: Vec::new(), + pressed: None, }; let installed = crate::runtime::env::Env::install_for( ui, diff --git a/crates/ui-widgets/src/list/row.rs b/crates/ui-widgets/src/list/row.rs index cae164301..e738afd63 100644 --- a/crates/ui-widgets/src/list/row.rs +++ b/crates/ui-widgets/src/list/row.rs @@ -9,7 +9,7 @@ use super::toggle::ToggleCell; use super::words::ListWords; -use crate::contract::{ColumnSpec, Common, OpenChange, RowState, Show}; +use crate::contract::{ColumnSpec, Common, OpenChange, RowPressed, RowState, Show}; use crate::primitives::{ContentPainterCx, HAlign, IconSpec}; use crate::runtime::env::Label; use craft_behavior::ColumnKey; @@ -148,6 +148,8 @@ pub struct ListRow<'a> { pub(super) rule: bool, /// The rename editor replaces the label; `state` sets it from the collection's row state. pub(super) renaming: bool, + /// Where the collection holds a press on the row, from its row state: the toggle cell it acts on shows it. + pub(super) pressed: Option, /// The thumbnails that drag instead of the row, each with what starts its drag session. pub(super) drags: Vec<(ThumbTarget, ThumbPayload)>, /// The icon or thumbnails come before the disclosure column instead of after it. @@ -244,6 +246,7 @@ impl<'a> ListRow<'a> { clipped: false, rule: false, renaming: false, + pressed: None, drags: Vec::new(), leading_first: false, detail: None, @@ -295,10 +298,12 @@ impl<'a> ListRow<'a> { self } - /// The row state the collection gives the item: the rename editor, the stripe and the app's mark. + /// The row state the collection gives the item: the rename editor, the stripe, the app's mark and the press the + /// collection holds on the row. #[must_use] pub fn state(mut self, row: RowState) -> Self { self.renaming = row.renaming; + self.pressed = row.pressed; self.striped = row.striped; self.highlight |= row.emphasized; self diff --git a/crates/ui-widgets/src/list/row/placement.rs b/crates/ui-widgets/src/list/row/placement.rs index 20e19d981..0d992d034 100644 --- a/crates/ui-widgets/src/list/row/placement.rs +++ b/crates/ui-widgets/src/list/row/placement.rs @@ -155,11 +155,15 @@ impl ListRow<'_> { } } - /// Takes the toggle columns and their keys from the facts the collection recorded for this row. + /// Takes the toggle columns, their keys and the press the collection holds on the row from the facts it recorded + /// for this row. pub(super) fn adopt_toggle_columns(&mut self, env: &EnvView<'_>, id: egui::Id) { if let Some(facts) = env.facts_of(id) { self.toggle_columns.clone_from(&facts.toggles); self.toggle_keys.clone_from(&facts.toggle_keys); + if self.pressed.is_none() { + self.pressed.clone_from(&facts.pressed); + } } } diff --git a/crates/ui-widgets/src/list/row/show.rs b/crates/ui-widgets/src/list/row/show.rs index efa125631..31bb63fca 100644 --- a/crates/ui-widgets/src/list/row/show.rs +++ b/crates/ui-widgets/src/list/row/show.rs @@ -9,7 +9,7 @@ use super::thumb::{RowThumb, target_at}; use super::trailing::{TrailingFace, TrailingItem}; use super::{ListRow, RowInk}; use crate::contract::{ - Access, Bind, Component, Cx, DragPreview, MeasureCx, Name, OpenChange, PaintUi, PartsCx, Pointer as _, Props as _, Role, Styled as _, + Access, Bind, Component, Cx, DragPreview, MeasureCx, Name, OpenChange, PaintUi, PartsCx, Pointer as _, Props as _, Role, RowPressed, Styled as _, WordSetCx as _, }; use crate::controls::disclosure::Disclosure; @@ -188,7 +188,12 @@ impl Component for ListRow<'_> { } (None, None) => None, }; - let part = cx.part(TOGGLE.at(*at as u64), shape).flags(flags); + let held = match &self.pressed { + Some(RowPressed::Cell(column)) => *column == toggle.column, + Some(RowPressed::Row) => toggle.row_press, + _ => false, + }; + let part = cx.part(TOGGLE.at(*at as u64), shape).flags(flags).held(held); let _ = match tip { Some(tip) => part.tooltip(tip), None => part, diff --git a/crates/ui-widgets/src/list/toggle.rs b/crates/ui-widgets/src/list/toggle.rs index 34eb24453..3cfeee149 100644 --- a/crates/ui-widgets/src/list/toggle.rs +++ b/crates/ui-widgets/src/list/toggle.rs @@ -83,13 +83,25 @@ pub struct ToggleCell { /// A screen reader announces a check box instead of a toggle button: for a setting the row carries rather than /// an action on the row. pub check_box: bool, + /// A press on the row's own area shows on the cell: the row's click acts on what the cell shows. + pub row_press: bool, } impl ToggleCell { /// A cell of `column` showing `state` with `face`, named `name`: a whole pattern with `{row}` for the row's label /// ("Keep {row}"). pub fn new(column: ColumnKey, face: ToggleFace, state: ToggleState, name: impl Into