diff --git a/crates/timeline/src/ui/stack.rs b/crates/timeline/src/ui/stack.rs index 8211a767f1..3287119450 100644 --- a/crates/timeline/src/ui/stack.rs +++ b/crates/timeline/src/ui/stack.rs @@ -55,8 +55,6 @@ const LABEL: PartKey = PartKey::new("label"); const HEADER: PartKey = PartKey::new("header"); const EDGE_BOTTOM: PartKey = PartKey::new("heightBottom"); const EDGE_TOP: PartKey = PartKey::new("heightTop"); -/// A switch's face, at its place in `SWITCHES`. The collection's toggle cell beneath takes the presses. -const SWITCH: PartKey = PartKey::new("switch"); /// The switch columns in header order, each with its icon while on and off. const SWITCHES: [(ColumnKey, &str, &str); 6] = [ @@ -535,11 +533,12 @@ impl Component for TrackRow<'_> { cx.part(HEADER, name).sense(Sense::CLICK).pointer_only("the host's track height command"); } let side = cx.tokens.size.control.sm; - for (at, (key, _, _)) in SWITCHES.iter().enumerate() { + // A switch's face; the collection's toggle cell beneath takes the presses. + for (key, _, _) in &SWITCHES { let (Some(cell), Some(on)) = (self.cell(key), self.switch(key)) else { continue }; let face = Rect::from_center_size(cell.center(), Vec2::splat(side)).intersect(cell); let flags = if on { Flags::CHECKED } else { Flags::empty() }; - let _ = cx.part(SWITCH.at(at as u64), face).flags(flags); + let _ = cx.toggle_cell(key, face).flags(flags); } let row = self.row(); let reach = cx.style.edge; @@ -656,14 +655,14 @@ impl Component for TrackRow<'_> { draw.text(text, &craft_ui::primitives::TextRun::new(self.track.name).role(NAME_ROLE).elide()); } let words = cx.words::(); - for (at, (key, on_icon, off_icon)) in SWITCHES.iter().enumerate() { + for (key, on_icon, off_icon) in &SWITCHES { let (Some(_), Some(on)) = (self.cell(key), self.switch(key)) else { continue }; let face = ToggleButton::new(switch_name(&words, key), Bind::given(on)) .icon(IconSpec::svg(IconId::new(off_icon))) .on_icon(IconSpec::svg(IconId::new(on_icon))) .icon_only() .size(ControlSize::Sm); - ui.face(SWITCH.at(at as u64), face); + ui.face(craft_ui::contract::toggle_cell_part(key), face); } if let (Some(lane), Some(axis)) = (self.cell(&LANE), time_axis(&cx.env)) { self.lane.paint(ui, lane, &axis, &style.clip, cx); diff --git a/crates/ui-contract/src/contract/cx/parts.rs b/crates/ui-contract/src/contract/cx/parts.rs index b41c6cd84a..816527a2d0 100644 --- a/crates/ui-contract/src/contract/cx/parts.rs +++ b/crates/ui-contract/src/contract/cx/parts.rs @@ -91,6 +91,8 @@ pub(crate) struct PartExtras { pub after_header: bool, /// The part reports an access node of this role and name though no face is painted for it. pub node: Option<(crate::runtime::access::Role, String)>, + /// The part shows a collection's toggle cell (`PartsCx::toggle_cell`): its node declares AccessKit `Click`. + pub toggle_cell: bool, } impl PartBuilder<'_> { @@ -398,6 +400,15 @@ impl PartsCx<'_, C> { PartBuilder { decl, declared, window } } + /// Declares the part that shows this item's cell of the collection's toggle column `column`, at + /// `toggle_cell_part(column)`. The collection takes the pointer's presses on the cell itself, so the part senses + /// none; its node declares AccessKit `Click`, and the collection flips the cell on that click as on a press. + pub fn toggle_cell(&mut self, column: &craft_behavior::ColumnKey, shape: impl Into) -> PartBuilder<'_> { + let builder = self.part(crate::contract::toggle_cell_part(column), shape); + builder.declared.toggle_cell = true; + builder + } + /// Declares `rect`, the slot where the component shows its inside label (`EnvView::inside_label`), as the label's /// part (`InsideLabel::PART`): it shows the label's tooltip and, for a scrubbing label, takes the press a /// `Scrub::by_label` scrubs with, in edit too. Declares nothing without an inside label. diff --git a/crates/ui-contract/src/contract/items.rs b/crates/ui-contract/src/contract/items.rs index 92e5800c55..03c2280644 100644 --- a/crates/ui-contract/src/contract/items.rs +++ b/crates/ui-contract/src/contract/items.rs @@ -68,8 +68,7 @@ mod request; pub use drag::{SweepApply, SweepStep}; pub use header::ColumnCommands; pub(crate) use header::HeaderFacts; -#[doc(hidden)] -pub use header::put_toggle_names; +pub use header::toggle_cell_part; pub use press::item_part_open; #[doc(hidden)] pub use request::lead_position; diff --git a/crates/ui-contract/src/contract/items/access.rs b/crates/ui-contract/src/contract/items/access.rs index ea461da220..0e31fc152a 100644 --- a/crates/ui-contract/src/contract/items/access.rs +++ b/crates/ui-contract/src/contract/items/access.rs @@ -10,41 +10,16 @@ use craft_behavior::ColumnKey; use egui::{Context, Id, Rect}; impl ItemsHooks<'_, '_, C> { - /// The access sub-node of each toggle cell of row `(at, key)`, named by its cell, else by its column's header, and carrying its state, - /// below the row's node `item`; an AccessKit click on it reaches `toggle_by_access`. - pub(super) fn report_toggle_cells( - &self, - ctx: &Context, - (id, item): (Id, Id), - (at, key): (usize, &C::Key), - (cells, enabled): (&[(ColumnKey, Rect)], bool), - env: &crate::runtime::env::EnvView<'_>, - ) { - for (column, toggle) in self.header.toggle_columns() { - let Some((_, rect)) = cells.iter().find(|(held, _)| *held == column) else { continue }; - let Some((name, mixed)) = self.header.cell_name(ctx, item, &column, env) else { continue }; - let mut node = - access::SubNode::new(TOGGLE_CELL.at(key_hash(&(key, &column))), if toggle.exclusive { Role::RADIO_BUTTON } else { Role::SWITCH }); - node.name = access::Name::Text(name); - node.flags.set(crate::style::Flags::CHECKED, self.source.toggled(at, &column)); - node.flags.set(crate::style::Flags::MIXED, mixed); - if let Some(chord) = toggle.key { - node.shortcut(chord.display(crate::runtime::windows::os_of(ctx))); + /// Hands the collection the AccessKit requests on the node of each toggle cell the shown row `key` declared + /// (`PartsCx::toggle_cell`) below its node `item`: a click on one reaches `toggle_by_access`. A row that declared + /// no part for a column leaves nothing to redirect. + pub(super) fn redirect_toggle_cells(&self, ctx: &Context, (id, item): (Id, Id), key: &C::Key, cells: &[(ColumnKey, Rect)]) { + for (column, _) in self.header.toggle_columns() { + if !cells.iter().any(|(held, _)| *held == column) { + continue; } - let decl = access::SubDecl { - owner: id, - component: "", - node: &node, - rect: *rect, - enabled: enabled && self.source.enabled(at) && self.toggle_open(at, &column), - focusable: false, - clickable: true, - active: false, - face: None, - label: "", - parent: item, - }; - access::report_sub(ctx, &decl); + let node = access::sub_node_id(item, crate::contract::toggle_cell_part(&column)); + crate::runtime::keys::redirect_node(ctx, node, id, TOGGLE_CELL.at(key_hash(&(key, &column)))); } } @@ -134,7 +109,7 @@ impl ItemsHooks<'_, '_, C> { } } - /// An AccessKit click on a toggle cell's sub-node flips that cell, as a click on it does. + /// An AccessKit click on a toggle cell's node (`redirect_toggle_cells`) flips that cell, as a click on it does. pub(super) fn toggle_by_access(&mut self, cx: &mut Cx<'_, C>, layout: &C::ItemLayout, place: Placement) { let wanted = |request: &craft_behavior::AccessRequest| { request.action == craft_behavior::AccessAction::Click && request.target.is_some_and(|part| part.key == TOGGLE_CELL) diff --git a/crates/ui-contract/src/contract/items/header.rs b/crates/ui-contract/src/contract/items/header.rs index 881ed1426c..08373b6799 100644 --- a/crates/ui-contract/src/contract/items/header.rs +++ b/crates/ui-contract/src/contract/items/header.rs @@ -91,19 +91,13 @@ impl DropTarget for ColumnGap { } } -/// The names the toggle cells of one row gave themselves this pass, by column. -#[derive(Clone, Default)] -struct ToggleNames(std::sync::Arc<[(ColumnKey, String, bool)]>); +/// The part of an item that shows its cell of a toggle column; the index is the hash of the column. +const ITEM_TOGGLE: PartKey = PartKey::new("item_toggle"); -/// Records the toggle cells of the row `item` with their accessible names (`{row}` filled; empty when the cell gives -/// none) and whether each shows the mixed state, for the collection's cell nodes of the same pass. A row that has none records an empty list. -#[doc(hidden)] -pub fn put_toggle_names(ctx: &Context, item: Id, names: Vec<(ColumnKey, String, bool)>) { - crate::runtime::egui::memory::put(ctx, sub(item, "toggle_names"), ToggleNames(names.into())); -} - -fn recorded_toggle_names(ctx: &Context, item: Id) -> Option> { - crate::runtime::egui::memory::peek_clone::(ctx, sub(item, "toggle_names")).map(|ToggleNames(names)| names) +/// The part an item declares its cell of the toggle column `column` at (`PartsCx::toggle_cell`): its face and its +/// access node name this part. +pub fn toggle_cell_part(column: &ColumnKey) -> PartRef { + ITEM_TOGGLE.at(hash_of(column)) } /// The id the widest cell of the `Content` column `key` of the collection `id` is recorded under. @@ -349,29 +343,6 @@ impl Header { self.toggle_columns().filter_map(|(column, toggle)| Some((column, toggle.key?))).collect() } - /// The name of the cell of toggle column `key` in the row `item` and whether it shows the mixed state: the name the - /// row's cell gave (`put_toggle_names`), else the column's header text, else its icon's name, resolved in `env`. - /// `None` when the row recorded its toggle cells and has none in this column: the collection reports no node there. - pub(super) fn cell_name(&self, ctx: &Context, item: Id, key: &ColumnKey, env: &crate::runtime::env::EnvView<'_>) -> Option<(String, bool)> { - let recorded = recorded_toggle_names(ctx, item); - let (given, mixed) = match &recorded { - Some(names) => names.iter().find(|(held, ..)| held == key).map(|(_, name, mixed)| (name.as_str(), *mixed))?, - None => ("", false), - }; - if !given.is_empty() { - return Some((given.to_owned(), mixed)); - } - let Some(spec) = self.specs.iter().find(|spec| spec.key == *key) else { return Some((String::new(), mixed)) }; - let header = env.resolve(&spec.header); - Some(( - match (header.as_str(), &spec.icon) { - ("", Some(icon)) => icon.name().to_owned(), - (text, _) => text.to_owned(), - }, - mixed, - )) - } - /// The key of the column at position `at`. pub(super) fn key_at(&self, at: usize) -> Option<&ColumnKey> { self.specs.get(at).map(|spec| &spec.key) diff --git a/crates/ui-contract/src/contract/items/hooks.rs b/crates/ui-contract/src/contract/items/hooks.rs index eeec8c4273..2b6e9c9b47 100644 --- a/crates/ui-contract/src/contract/items/hooks.rs +++ b/crates/ui-contract/src/contract/items/hooks.rs @@ -653,8 +653,8 @@ impl ShapeHooks for ItemsHooks<'_, '_, C> { layout_memory::ask_discard(&ctx); } } - // After the row is shown: its toggle cells have recorded their names by then. - self.report_toggle_cells(&ctx, (id, item_id), (at, &key), (shown_cells.as_slice(), cx.core.resolved.enabled), &cx.core.env); + // After the row is shown: its toggle cells' nodes are registered by then. + self.redirect_toggle_cells(&ctx, (id, item_id), &key, shown_cells.as_slice()); let label = out.label_rect().unwrap_or(leaf); self.slow_click_label(&key, label); crate::runtime::keys::redirect_node(&ctx, item_id, id, ITEM_NODE.at(key_hash(&key))); diff --git a/crates/ui-contract/src/contract/mod.rs b/crates/ui-contract/src/contract/mod.rs index bf12dda0cf..d62971d38c 100644 --- a/crates/ui-contract/src/contract/mod.rs +++ b/crates/ui-contract/src/contract/mod.rs @@ -89,8 +89,7 @@ pub use held::Held; pub use hosted::{Hosted, HostedKey, Hosting}; pub use items::item_node; pub use items::lead_position; -#[doc(hidden)] -pub use items::put_toggle_names; +pub use items::toggle_cell_part; // PLAN: assumed items::cancel_rename(ctx: &Context, collection: Id) (panels-w12) pub use items::{ColumnCommands, SweepApply, SweepStep, cancel_rename, item_part_open, request_rename, section_part}; pub use items::{measure_item, show_item, show_item_in_flow}; diff --git a/crates/ui-contract/src/contract/steps/report.rs b/crates/ui-contract/src/contract/steps/report.rs index cc0157e0e3..46e42fdff5 100644 --- a/crates/ui-contract/src/contract/steps/report.rs +++ b/crates/ui-contract/src/contract/steps/report.rs @@ -303,7 +303,7 @@ fn report_sub(core: &CxCore<'_>, decls: &Decls, node: &SubNode, face: Option<&Fa focusable: takes_focus(core.resolved) && node.enabled && declared.is_some_and(|(_, extras)| extras.focusable), clickable: core.resolved.enabled && node.enabled - && declared.is_some_and(|(decl, extras)| decl.sense.contains(Sense::CLICK) && !extras.no_access_click), + && declared.is_some_and(|(decl, extras)| (decl.sense.contains(Sense::CLICK) && !extras.no_access_click) || extras.toggle_cell), active: core.active == Some(node.part), face: face.map(|face| FaceFacts { name: &face.name, diff --git a/crates/ui-widgets/src/list/row/paint.rs b/crates/ui-widgets/src/list/row/paint.rs index 20412f017e..89ba20395a 100644 --- a/crates/ui-widgets/src/list/row/paint.rs +++ b/crates/ui-widgets/src/list/row/paint.rs @@ -1,7 +1,7 @@ //! Painting a row's trailing items, toggles, thumbnails, text and cells. use super::placement::{Placed, StatusMeasure, action_link}; -use super::show::{LINK, STATUS_ACTION, TEXT_CELL, TOGGLE}; +use super::show::{LINK, STATUS_ACTION, TEXT_CELL}; use super::style::{ListRowStyle, RowForm, row_ink_of}; use super::thumb::{RowThumb, target_at}; use super::trailing::{TrailingFace, TrailingItem, cell_key, field_key, trailing_run}; @@ -82,7 +82,7 @@ impl ListRow<'_> { let icon = cx.style.sizes.mark; for (at, cell) in &placed.toggles { let toggle = &self.toggles[*at]; - let part = TOGGLE.at(*at as u64); + let part = crate::contract::toggle_cell_part(&toggle.column); if let Some(fill) = toggle.fill { ui.draw().data_fill(&PaintShape::Rect(*cell, CornerRadius::ZERO), fill); } diff --git a/crates/ui-widgets/src/list/row/placement.rs b/crates/ui-widgets/src/list/row/placement.rs index 4f5045178a..48d5df4a87 100644 --- a/crates/ui-widgets/src/list/row/placement.rs +++ b/crates/ui-widgets/src/list/row/placement.rs @@ -243,18 +243,6 @@ impl ListRow<'_> { } } - /// Hands the collection the names of this row's toggle cells, so its cell nodes read them. - pub(super) fn record_toggle_names(&self, cx: &PartsCx<'_, Self>) { - let words = cx.words::(); - let label = cx.resolved.label; - let names = self - .toggles - .iter() - .map(|toggle| (toggle.column.clone(), named(toggle.name_pattern(&words), label), toggle.state == ToggleState::Mixed)) - .collect(); - crate::contract::put_toggle_names(cx.ui.ctx(), cx.resolved.id, names); - } - /// How the wrapped lines fit: all of them, or cut after `line_limit`. pub(super) fn lines_wrap(&self) -> Wrap { self.line_limit.map_or(Wrap::Wrap, Wrap::Clamp) diff --git a/crates/ui-widgets/src/list/row/show.rs b/crates/ui-widgets/src/list/row/show.rs index b336ccf48c..e88d35a5be 100644 --- a/crates/ui-widgets/src/list/row/show.rs +++ b/crates/ui-widgets/src/list/row/show.rs @@ -26,7 +26,6 @@ use craft_theme::variant::Tone; use craft_theme::{Rgba, Stateful, TypeRole, over}; use egui::{CornerRadius, Pos2, Rect, Vec2}; -pub(super) const TOGGLE: PartKey = PartKey::new("toggle"); pub(super) const TEXT_CELL: PartKey = PartKey::new("text-cell"); const TOGGLE_CELL: PartKey = PartKey::new("toggle-cell"); const LABEL_CELL: PartKey = PartKey::new("label-cell"); @@ -203,7 +202,6 @@ impl Component for ListRow<'_> { } fn parts(&mut self, rect: Rect, cx: &mut PartsCx<'_, Self>) { - self.record_toggle_names(cx); if self.spacer || self.rule_head { return; } @@ -486,9 +484,12 @@ impl Component for ListRow<'_> { cell.grid.column_index = column_of(&toggle.column); cell.flags.set(Flags::SELECTED, selected); let role = if toggle.check_box { Role::CHECK_BOX } else { Role::BUTTON }; - let node = access.node(TOGGLE.at(at as u64), role); + let node = access.node(crate::contract::toggle_cell_part(&toggle.column), role); node.name = Name::Text(named(toggle.name_pattern(&words), label)); node.under(TOGGLE_CELL.at(at as u64)); + if let Some((_, chord)) = self.toggle_keys.iter().find(|(column, _)| *column == toggle.column) { + node.shortcut(chord.display(crate::runtime::windows::os_of(&cx.ctx))); + } } for text in &self.texts { let Some(position) = self.cells.iter().position(|(column, _)| *column == text.column) else { continue }; @@ -778,7 +779,7 @@ impl ListRow<'_> { Some(RowPressed::Row) => toggle.row_press, _ => false, }; - let part = cx.part(TOGGLE.at(*at as u64), shape).flags(flags).held(held); + let part = cx.toggle_cell(&toggle.column, shape).flags(flags).held(held); let _ = match tip { Some(tip) => part.tooltip(tip), None => part,