diff --git a/crates/ui-contract/src/contract/items.rs b/crates/ui-contract/src/contract/items.rs index 0d2e7d28ac..cabba801f8 100644 --- a/crates/ui-contract/src/contract/items.rs +++ b/crates/ui-contract/src/contract/items.rs @@ -24,6 +24,7 @@ fn with_item_facts(ui: &mut Ui, at: crate::runtime::access::SetPosition, body columns: Vec::new(), toggles: Vec::new(), toggle_keys: Vec::new(), + closed_toggles: Vec::new(), aligns: Vec::new(), pressed: None, menu_open: false, diff --git a/crates/ui-contract/src/contract/items/hooks.rs b/crates/ui-contract/src/contract/items/hooks.rs index a274babf17..f4613b13c6 100644 --- a/crates/ui-contract/src/contract/items/hooks.rs +++ b/crates/ui-contract/src/contract/items/hooks.rs @@ -499,6 +499,7 @@ impl ShapeHooks for ItemsHooks<'_, '_, C> { let column_keys: Vec = cells.iter().map(|(key, _)| key.clone()).collect(); let shown_cells = cells.clone(); let toggle_keys: Vec = self.header.toggle_columns().map(|(key, _)| key).collect(); + let closed_toggles: Vec = toggle_keys.iter().filter(|key| !self.toggle_open(at, key)).cloned().collect(); let mut icx = ItemCx { key: &key, at, @@ -551,6 +552,7 @@ impl ShapeHooks for ItemsHooks<'_, '_, C> { columns: column_keys, toggles: toggle_keys, toggle_keys: self.header.toggle_keys(), + closed_toggles, aligns: aligns.clone(), pressed: state.pressed.clone(), menu_open: state.menu_open, diff --git a/crates/ui-contract/src/contract/items/measure.rs b/crates/ui-contract/src/contract/items/measure.rs index 2a546e8921..3a752f9589 100644 --- a/crates/ui-contract/src/contract/items/measure.rs +++ b/crates/ui-contract/src/contract/items/measure.rs @@ -220,6 +220,7 @@ impl<'s, 'r, C: Collection> ItemsHooks<'s, 'r, C> { columns, toggles: self.header.toggle_columns().map(|(key, _)| key).collect(), toggle_keys: Vec::new(), + closed_toggles: Vec::new(), aligns: self.header.aligns(), pressed: None, menu_open: false, diff --git a/crates/ui-contract/src/runtime/env.rs b/crates/ui-contract/src/runtime/env.rs index 64494b8af4..6b03e835b5 100644 --- a/crates/ui-contract/src/runtime/env.rs +++ b/crates/ui-contract/src/runtime/env.rs @@ -377,6 +377,10 @@ 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)>, + /// The toggle columns whose cell in this item does not flip (`ItemSource::toggle_enabled` is false, or the item is + /// a section): the row paints such a cell inactive. + #[doc(hidden)] + pub closed_toggles: Vec, /// Each column's alignment and whether its figures are tabular, from its `ColumnSpec`: a row places its cells' /// text by them. #[doc(hidden)] diff --git a/crates/ui-widgets/src/list/row.rs b/crates/ui-widgets/src/list/row.rs index 48ebe16a79..f17f070791 100644 --- a/crates/ui-widgets/src/list/row.rs +++ b/crates/ui-widgets/src/list/row.rs @@ -399,6 +399,9 @@ pub struct ListRow<'a> { pub(super) toggle_columns: Vec, /// The keys of those toggle columns that have one, from the same facts: their cells show the key in the tooltip. pub(super) toggle_keys: Vec<(ColumnKey, craft_keymap::Chord)>, + /// The toggle columns whose cell in this row does not flip, from the same facts: their cells paint inactive and + /// show no hover. + pub(super) closed_toggles: Vec, pub(super) toggles: Vec, pub(super) texts: Vec, /// The status cells (`cell_status`), in the order added. @@ -538,6 +541,7 @@ impl<'a> ListRow<'a> { selectable: Vec::new(), toggle_columns: Vec::new(), toggle_keys: Vec::new(), + closed_toggles: Vec::new(), toggles: Vec::new(), texts: Vec::new(), statuses: Vec::new(), diff --git a/crates/ui-widgets/src/list/row/paint.rs b/crates/ui-widgets/src/list/row/paint.rs index 20412f017e..b88e9649d9 100644 --- a/crates/ui-widgets/src/list/row/paint.rs +++ b/crates/ui-widgets/src/list/row/paint.rs @@ -97,7 +97,7 @@ impl ListRow<'_> { ToggleFace::Icons { on, off } => { let shown = match toggle.state { ToggleState::On | ToggleState::Mixed => Some(on), - ToggleState::Off if toggle.shows_off(cx.part(part).hovered) => Some(off), + ToggleState::Off if toggle.shows_off(cx.part(part).hovered && !self.toggle_closed(toggle)) => Some(off), ToggleState::Off => None, }; if let Some(id) = shown { diff --git a/crates/ui-widgets/src/list/row/placement.rs b/crates/ui-widgets/src/list/row/placement.rs index 171cb5d9d0..c7d9671bbd 100644 --- a/crates/ui-widgets/src/list/row/placement.rs +++ b/crates/ui-widgets/src/list/row/placement.rs @@ -228,13 +228,14 @@ impl ListRow<'_> { } } - /// Takes the toggle columns, their keys, the columns' alignments and the press the collection holds on the row from + /// Takes the toggle columns, their keys, the ones that do not flip in this row, the columns' alignments and the press the collection holds on the row from /// the facts it recorded for this row: an end-aligned column's text ends at the cell's end (mirrored in a /// right-to-left interface) and a numeric column's figures are tabular. pub(super) fn adopt_facts(&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); + self.closed_toggles.clone_from(&facts.closed_toggles); self.aligns = facts.aligns.iter().map(|(key, align, tabular)| (key.clone(), halign(*align), *tabular)).collect(); 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 4b403ce195..ff3705d148 100644 --- a/crates/ui-widgets/src/list/row/show.rs +++ b/crates/ui-widgets/src/list/row/show.rs @@ -14,7 +14,7 @@ use crate::contract::{ }; use crate::controls::disclosure::Disclosure; use crate::display::Progress; -use crate::list::toggle::{ToggleFace, ToggleState}; +use crate::list::toggle::{ToggleCell, ToggleFace, ToggleState}; use crate::list::words::ListWords; use crate::primitives::{DocRun, DocText, RectSide, RuleKind, StrokeWidth, TextRun, Wrap, glyph}; use crate::runtime::commands::FireSource; @@ -779,7 +779,13 @@ impl ListRow<'_> { self.cell_bounds.iter().find(|(key, _)| *key == part).map(|(_, rect)| *rect) } - /// The parts of the row's toggle cells: their flags, held state and tooltip with the toggle's key. + /// The collection does not flip `toggle` in this row (`ItemSource::toggle_enabled`): its cell paints in the + /// Disabled state and shows no hover. + pub(super) fn toggle_closed(&self, toggle: &ToggleCell) -> bool { + self.closed_toggles.contains(&toggle.column) + } + + /// The parts of the row's toggle cells: their flags, held state, inactive state and tooltip with the toggle's key. fn toggle_parts(&self, placed: &Placed, cx: &mut PartsCx<'_, Self>) { for (at, cell) in &placed.toggles { let toggle = &self.toggles[*at]; @@ -808,7 +814,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.part(TOGGLE.at(*at as u64), shape).flags(flags).held(held).inactive(self.toggle_closed(toggle)); let _ = match tip { Some(tip) => part.tooltip(tip), None => part, diff --git a/crates/ui-widgets/tests/it/list.rs b/crates/ui-widgets/tests/it/list.rs index 822fc22898..81cbcb6f84 100644 --- a/crates/ui-widgets/tests/it/list.rs +++ b/crates/ui-widgets/tests/it/list.rs @@ -1078,6 +1078,106 @@ fn a_toggle_glyph_rests_muted_and_reads_normal_under_the_pointer() { ); } +/// Two rows whose eye cell is on and whose lock cell is off; the source does not flip the first row's cells. +struct LockedFirst; + +const LOCKED_EYE: craft_behavior::ColumnKey = craft_behavior::ColumnKey::new("eye"); + +impl craft_behavior::DisplayOrder for LockedFirst { + fn len(&self) -> usize { + 2 + } + + fn key(&self, at: usize) -> usize { + at + } +} + +impl craft_behavior::ItemSource for LockedFirst { + fn toggled(&self, _: usize, column: &craft_behavior::ColumnKey) -> bool { + *column == LOCKED_EYE + } + + fn toggle_enabled(&self, at: usize, _: &craft_behavior::ColumnKey) -> bool { + at != 0 + } +} + +/// A toggle cell the source does not flip paints its glyph in `text.disabled` at rest, under the pointer and under a +/// press, shows no off glyph under the pointer, and its node reads disabled; the cells of a row that flips keep +/// `text.muted`, their off glyph under the pointer and enabled nodes. +#[trial] +fn a_toggle_cell_that_does_not_flip_paints_disabled_and_ignores_the_pointer() { + use craft_behavior::{ColumnKey, ColumnWidth}; + use craft_ui::contract::{ColumnSpec, Role, ToggleColumn}; + use craft_ui::list::ToggleCell; + use craft_ui::runtime::env::Env; + use craft_ui::testing::pointer; + use egui::Color32; + use std::cell::Cell; + const LOCK: ColumnKey = ColumnKey::new("lock"); + const NAME: ColumnKey = ColumnKey::new("name"); + // The eye and lock cells of each row. + let seen = Rc::new(Cell::new(([[egui::Rect::NOTHING; 2]; 2], Color32::TRANSPARENT, Color32::TRANSPARENT))); + let held = seen.clone(); + let mut fixture = Fixture::new(move |ui| { + let text = Env::of(ui).tokens.tokens().color.text; + let color = |[r, g, b, a]: [u8; 4]| Color32::from_rgba_unmultiplied(r, g, b, a); + let columns = vec![ + ColumnSpec::toggle(LOCKED_EYE, "", ToggleColumn::sweepable()), + ColumnSpec::toggle(LOCK, "", ToggleColumn::sweepable()), + ColumnSpec::new(NAME, "", ColumnWidth::fill(1.0)), + ]; + let _ = List::new("Layers").columns(columns).show_items(ui, &LockedFirst, &mut |key, icx| { + let (mut cells, ..) = held.get(); + cells[*key] = [LOCKED_EYE, LOCK].map(|column| icx.cell(&column).unwrap_or(egui::Rect::NOTHING)); + held.set((cells, color(text.disabled.0), color(text.muted.0))); + ListRow::new(NAMES[*key]).cells(icx.cells()).toggle(ToggleCell::eye(LOCKED_EYE, true)).toggle(ToggleCell::lock(LOCK, false)) + }); + }); + fixture.room(Vec2::new(300.0, 200.0)); + fixture.settle(); + let tints = |shapes: &[egui::Shape], cell: egui::Rect| -> Vec { + let mut tints: Vec = shapes + .iter() + .filter_map(|shape| match shape { + egui::Shape::Mesh(mesh) if cell.contains(mesh.calc_bounds().center()) => Some(mesh.vertices.iter().map(|vertex| vertex.color)), + _ => None, + }) + .flatten() + .filter(|color| color.a() > 0) + .collect(); + tints.dedup(); + tints + }; + let ([[locked, locked_lock], [open, open_lock]], disabled, muted) = seen.get(); + let shapes = fixture.frame_shapes(); + let (rest, open_rest) = (tints(&shapes, locked), tints(&shapes, open)); + let mut hover = |cell: egui::Rect| { + fixture.frame(vec![pointer::moved(cell.center())]); + fixture.frame(Vec::new()); + tints(&fixture.frame_shapes(), cell) + }; + let (locked_off, open_off) = (hover(locked_lock), hover(open_lock)); + let hovered = hover(locked); + fixture.frame(vec![pointer::press(locked.center())]); + let pressed = tints(&fixture.frame_shapes(), locked); + fixture.frame(vec![pointer::release(locked.center())]); + for (when, found) in [("at rest", &rest), ("under the pointer", &hovered), ("under a press", &pressed)] { + assert!(!found.is_empty() && found.iter().all(|tint| *tint == disabled), "the locked eye reads text.disabled {disabled:?} {when}: {found:?}"); + } + assert!(!open_rest.is_empty() && open_rest.iter().all(|tint| *tint == muted), "the open eye rests in text.muted {muted:?}: {open_rest:?}"); + assert!(locked_off.is_empty(), "the locked row's off lock shows no glyph under the pointer: {locked_off:?}"); + assert!(!open_off.is_empty(), "the open row's off lock shows its glyph under the pointer"); + let tree = fixture.tree(); + let switches: Vec<_> = tree.keys().into_iter().filter(|key| tree.role_of(*key) == Some(Role::SWITCH)).collect(); + let states = |row: &str| -> Vec { + switches.iter().filter(|key| tree.name_of(**key).is_some_and(|name| name.contains(row))).map(|key| tree.is_disabled_key(*key)).collect() + }; + assert_eq!(states(NAMES[0]), [true, true], "the locked row's toggle nodes read disabled"); + assert_eq!(states(NAMES[1]), [false, false], "the open row's toggle nodes read enabled"); +} + /// Every filled rect among `shapes`, nested lists included. fn fills(shapes: &[egui::Shape], out: &mut Vec<(egui::Rect, egui::Color32)>) { for shape in shapes {