Toggle cells and link glyphs rest in text.muted and read in text.normal under the pointer, through one quiet ink rule shared with Disclosure

This commit is contained in:
aiongg
2026-11-12 15:42:15 +00:00
parent ac9dd13cdc
commit dbe14adf38
6 changed files with 90 additions and 19 deletions
+8
View File
@@ -252,6 +252,14 @@ impl<'a> StyleCx<'a> {
self.host_muted.cloned().unwrap_or(base)
}
/// The ink of a glyph that acts on a click without a face of its own (a disclosure, a list row's toggle cell):
/// `text.muted` at rest, `text.normal` under the pointer and while pressed, `text.disabled` when disabled, on or off.
pub fn quiet_ink(&self) -> Stateful<StyleColor> {
let text = &self.tokens.color.text;
let (muted, normal, off) = (self.color(text.muted), self.color(text.normal), self.color(text.disabled));
Stateful::new(muted).off(States::new(muted, normal, normal, off))
}
/// The fill of an accent control that is on or filled: the strong accent at rest, with its hover and pressed
/// shades, and the disabled fill when disabled.
pub fn accent_fill(&self) -> Stateful<StyleColor> {
+2 -11
View File
@@ -4,10 +4,9 @@ use crate::contract::{
Animate as _, Bind, BindMut, Common, Component, Cx, Edit as _, EditResponse, MeasureCx, PaintUi, Pointer as _, Role, Styled as _, spec,
};
use crate::primitives::{Glyph, PaintShape, StrokeWidth};
use crate::style::{ComponentStyle, IconLook, Stateful, StyleCx, UiTokens, ink_color, rgba_color};
use crate::style::{ComponentStyle, IconLook, StyleCx, UiTokens, ink_color, rgba_color};
use craft_behavior::{Metrics, Sense};
use craft_i18n::Direction;
use craft_theme::States;
use egui::emath::GuiRounding as _;
use egui::{Color32, Painter, Pos2, Rect, Shape, Stroke, Vec2};
@@ -114,14 +113,6 @@ impl DisclosureGlyph {
fn tone(&self, tokens: &UiTokens, ink: Color32) -> Color32 {
if !self.filled && ink == ink_color(tokens, crate::style::Ink::OnPanel) { rgba_color(tokens.color.text.muted) } else { ink }
}
/// The colour a disclosure component shows its glyph in: muted at rest, the normal text colour under the pointer and
/// while pressed.
fn ink(cx: &StyleCx<'_>) -> Stateful<crate::style::StyleColor> {
let text = &cx.tokens.color.text;
let (muted, normal, off) = (cx.color(text.muted), cx.color(text.normal), cx.color(text.disabled));
Stateful::new(muted).off(States::new(muted, normal, normal, off))
}
}
impl Glyph for DisclosureGlyph {
@@ -190,7 +181,7 @@ impl Component for Disclosure<'_> {
fn style(cx: &StyleCx<'_>, _: DisclosureStyleKey) -> DisclosureStyle {
let side = cx.control().icon;
DisclosureStyle { metrics: Metrics::new().min_size(Vec2::splat(side)), glyph: IconLook::new(side, DisclosureGlyph::ink(cx)) }
DisclosureStyle { metrics: Metrics::new().min_size(Vec2::splat(side)), glyph: IconLook::new(side, cx.quiet_ink()) }
}
fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 {
+14 -6
View File
@@ -2,7 +2,7 @@
use super::ListRow;
use super::placement::Placed;
use super::show::{TEXT_CELL, TOGGLE};
use super::show::{LINK, TEXT_CELL, TOGGLE};
use super::style::ListRowStyle;
use super::thumb::{RowThumb, target_at};
use super::trailing::{TrailingFace, TrailingItem, cell_key, field_key, trailing_run};
@@ -12,8 +12,8 @@ use crate::contract::{Bind, Cx, PaintUi, Pointer as _, Props as _, Styled as _,
use crate::controls::disclosure::Disclosure;
use crate::list::toggle::{ToggleFace, ToggleState};
use crate::primitives::{Draw, HAlign, IconSpec, PaintShape, Span, StrokeWidth, TextRun, Wrap};
use crate::style::{Flags, Ink, StyleColor};
use craft_behavior::{Flow, IconId, Mixed};
use crate::style::{Flags, Ink, StateMix, StateSet, StyleColor};
use craft_behavior::{Flow, IconId, Mixed, PartRef};
use craft_theme::ControlSize;
use egui::{CornerRadius, Pos2, Rect, Vec2};
@@ -85,8 +85,9 @@ impl ListRow<'_> {
ToggleState::Off => None,
};
if let Some(id) = shown {
let ink = if toggle.fill.is_some() { Ink::OnDark } else { Ink::OnPanel };
ui.draw().icon(Rect::from_center_size(cell.center(), Vec2::splat(icon)), &IconSpec::svg(id.clone()).ink(ink));
let spec = IconSpec::svg(id.clone());
let spec = if toggle.fill.is_some() { spec.ink(Ink::OnDark) } else { spec.color(glyph_ink(cx, part)) };
ui.draw().icon(Rect::from_center_size(cell.center(), Vec2::splat(icon)), &spec);
}
}
}
@@ -132,7 +133,8 @@ impl ListRow<'_> {
let (Some(slot), Some((linked, _))) = (link, &self.extras[at].link) else { continue };
let id = IconId::new(if *linked { "link" } else { "unlink" });
let side = Rect::from_center_size(slot.center(), Vec2::splat(style.sizes.icon));
ui.draw().icon(side, &IconSpec::svg(id));
let color = glyph_ink(cx, LINK.at(at as u64));
ui.draw().icon(side, &IconSpec::svg(id).color(color));
}
}
@@ -259,6 +261,12 @@ impl ListRow<'_> {
}
}
/// The ink of a toggle or link glyph: its own part's hover and press, over the row's selection and lead.
fn glyph_ink(cx: &Cx<'_, ListRow<'_>>, part: PartRef) -> StyleColor {
let (row, cell) = (cx.state(), cx.part_state(part));
StateSet::new(cell.state, row.flags, StateMix::new(cell.t.hover, cell.t.press, row.t.on)).resolve(&cx.style.toggle.color)
}
/// The down-right clipping mark: a stem down from the top, then a leg toward the reading end with an arrowhead.
pub(super) fn draw_clip_arrow(draw: &Draw<'_>, slot: Rect, rtl: bool, ink: StyleColor) {
let side = slot.width().min(slot.height());
+3 -2
View File
@@ -4,7 +4,7 @@ use super::ListRowStyleKey;
use super::output::{RowEvent, RowOutput};
use super::paint::draw_clip_arrow;
use super::placement::{Sizes, lead_run};
use super::style::{FillKey, ListRowStyle, RowForm, fill_of, row_ink_of, selected_outline};
use super::style::{FillKey, ListRowStyle, RowForm, fill_of, quiet_row_ink, row_ink_of, selected_outline};
use super::thumb::{RowThumb, target_at};
use super::trailing::{HostedSize, TrailingFace, TrailingItem, TrailingPart, below_key, cell_key, field_key};
use super::{ListRow, RowDragSpot, RowInk};
@@ -32,7 +32,7 @@ const TOGGLE_CELL: PartKey = PartKey::new("toggle-cell");
const LABEL_CELL: PartKey = PartKey::new("label-cell");
const DISCLOSURE: PartKey = PartKey::new("disclosure");
const LINK: PartKey = PartKey::new("link");
pub(super) const LINK: PartKey = PartKey::new("link");
const PROGRESS: PartKey = PartKey::new("progress");
const LINES: PartKey = PartKey::new("lines");
pub(super) const TRAILING: PartKey = PartKey::new("trailing");
@@ -104,6 +104,7 @@ impl Component for ListRow<'_> {
tone => row_ink_of(cx, cx.tone(tone), form),
},
),
toggle: IconLook::new(sizes.icon, quiet_row_ink(cx, form)),
detail: TextLook::new(TypeRole::Caption, ink(text.faint)),
cell: TextLook::new(role, ink(base)),
cell_muted: TextLook::new(role, ink(muted)),
+11
View File
@@ -26,6 +26,9 @@ pub struct ListRowStyle {
/// The row's trailing icons.
#[style(icon)]
pub(super) trailing_icon: IconLook,
/// The glyph of a toggle cell and of the link between thumbnails: quiet at rest, the normal ink under the pointer.
#[style(icon)]
pub(super) toggle: IconLook,
/// The detail line.
#[style(text)]
pub(super) detail: TextLook,
@@ -205,6 +208,14 @@ impl RowStyleCx for StyleCx<'_> {
}
}
/// The ink of a row's toggle and link glyphs: `row_ink_of` on `text.muted`, whose off states are the quiet ink's, so
/// the glyph reads in `text.normal` under the pointer and while pressed. The flag rules keep every fill's paired ink.
pub(super) fn quiet_row_ink(cx: &StyleCx<'_>, form: RowForm) -> Stateful<StyleColor> {
let mut ink = row_ink_of(cx, cx.color(cx.tokens.color.text.muted), form);
ink.off = cx.quiet_ink().off;
ink
}
/// `row_ink` for a base that is already a style colour (a tone). A disabled row's ink is `text.disabled`.
pub(super) fn row_ink_of(cx: &StyleCx<'_>, base: StyleColor, form: RowForm) -> Stateful<StyleColor> {
let on_selected = cx.color(cx.tokens.color.text.on_selected);
+52
View File
@@ -556,6 +556,58 @@ fn a_toggle_column_holds_one_toggle_with_room_on_each_side() {
assert!(cell.width() < 2.0 * cell.height(), "the toggle column holds one toggle: {cell:?}");
}
/// A toggle cell's glyph rests in `text.muted` and reads in `text.normal` under the pointer.
#[trial]
fn a_toggle_glyph_rests_muted_and_reads_normal_under_the_pointer() {
use craft_behavior::{ColumnKey, ColumnWidth};
use craft_ui::contract::{ColumnSpec, 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 EYE: ColumnKey = ColumnKey::new("eye");
const NAME: ColumnKey = ColumnKey::new("name");
let seen = Rc::new(Cell::new((egui::Rect::NOTHING, 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(EYE, "", ToggleColumn::sweepable()), ColumnSpec::new(NAME, "", ColumnWidth::fill(1.0))];
let keys = [0_usize];
let source = keyed(&keys, |key: &usize| *key);
let _ = List::new("Layers").columns(columns).show_items(ui, &source, &mut |key, icx| {
held.set((icx.cell(&EYE).unwrap_or(egui::Rect::NOTHING), color(text.muted.0), color(text.normal.0)));
ListRow::new(NAMES[*key]).cells(icx.cells()).toggle(ToggleCell::eye(EYE, true))
});
});
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 (cell, muted, normal) = seen.get();
let rest = tints(&fixture.frame_shapes(), cell);
fixture.frame(vec![pointer::moved(cell.center())]);
fixture.frame(Vec::new());
let hovered = tints(&fixture.frame_shapes(), cell);
assert!(!rest.is_empty() && rest.iter().all(|tint| *tint == muted), "the eye rests in text.muted {muted:?}: {rest:?}");
assert!(
!hovered.is_empty() && hovered.iter().all(|tint| *tint == normal),
"the eye reads in text.normal {normal:?} under the pointer: {hovered:?}"
);
}
/// Every filled rect among `shapes`, nested lists included.
fn fills(shapes: &[egui::Shape], out: &mut Vec<(egui::Rect, egui::Color32)>) {
for shape in shapes {