ui-widgets: a toggle cell the list does not flip paints disabled

A collection already refuses a press, a sweep and the Space key on a
toggle cell whose source says it does not flip
(`ItemSource::toggle_enabled` false, or a section row), and reports its
node disabled. The row still painted that cell with the normal toggle
ink, brightened it under the pointer and showed the off glyph on hover,
so a locked cell looked clickable.

- `ItemFacts` carries `closed_toggles`, the toggle columns whose cell in
  the item does not flip, filled from `toggle_open`.
- `ListRow` reads them with the other facts and declares such a cell's
  part inactive, so its glyph or check box paints in the Disabled state
  at rest, under the pointer and under a press.
- An off cell there shows no off glyph under the pointer.
This commit is contained in:
dotCooCoo committed 2026-11-25 20:34:51 -08:00
1 parent 73743edd08
commit 3af616be85
9 files changed
+124 -5

No files matched your search

+1
View File
@@ -24,6 +24,7 @@ fn with_item_facts<R>(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,
@@ -499,6 +499,7 @@ impl<C: Collection> ShapeHooks<C> for ItemsHooks<'_, '_, C> {
let column_keys: Vec<craft_behavior::ColumnKey> = cells.iter().map(|(key, _)| key.clone()).collect();
let shown_cells = cells.clone();
let toggle_keys: Vec<craft_behavior::ColumnKey> = self.header.toggle_columns().map(|(key, _)| key).collect();
let closed_toggles: Vec<craft_behavior::ColumnKey> = toggle_keys.iter().filter(|key| !self.toggle_open(at, key)).cloned().collect();
let mut icx = ItemCx {
key: &key,
at,
@@ -551,6 +552,7 @@ impl<C: Collection> ShapeHooks<C> 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,
@@ -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,
+4
View File
@@ -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<craft_behavior::ColumnKey>,
/// Each column's alignment and whether its figures are tabular, from its `ColumnSpec`: a row places its cells'
/// text by them.
#[doc(hidden)]
+4
View File
@@ -399,6 +399,9 @@ pub struct ListRow<'a> {
pub(super) toggle_columns: Vec<ColumnKey>,
/// 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<ColumnKey>,
pub(super) toggles: Vec<ToggleCell>,
pub(super) texts: Vec<TextCell>,
/// 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(),
+1 -1
View File
@@ -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 {
+2 -1
View File
@@ -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);
+9 -3
View File
@@ -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,
+100
View File
@@ -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<usize> for LockedFirst {
fn len(&self) -> usize {
2
}
fn key(&self, at: usize) -> usize {
at
}
}
impl craft_behavior::ItemSource<usize> 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<Color32> {
let mut tints: Vec<Color32> = 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<bool> {
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 {