Tile: quiet check box (Checkbox::quiet) and an outlined selected band tile (trials)

This commit is contained in:
Dylan M. Taylor
2026-11-22 22:06:49 -05:00
parent ef11aa2431
commit 4b7937c270
5 changed files with 82 additions and 6 deletions
+24 -2
View File
@@ -23,18 +23,31 @@ pub struct CheckboxStyle {
side: f32,
}
/// The side of a quiet check box, against the icon side.
const QUIET_SIDE: f32 = 0.6;
/// A check box over a bool; a `Mixed` binding shows the mixed state and a click makes it checked.
#[derive(crate::contract::Component)]
pub struct Checkbox<'a> {
#[common]
common: Common,
value: Bind<'a, bool>,
/// A small, dark box with a plain white mark in place of the accent fill: the check of a thumbnail tile, which
/// sits over a picture and must not outweigh it.
#[style_key]
quiet: bool,
}
impl<'a> Checkbox<'a> {
/// A check box of `value`.
pub fn new(value: Bind<'a, bool>) -> Self {
Checkbox { common: Common::new(), value }
Checkbox { common: Common::new(), value, quiet: false }
}
/// A small, dark box with a plain mark, for a check that sits over a picture (a thumbnail tile's).
pub fn quiet(mut self, on: bool) -> Self {
self.quiet = on;
self
}
}
@@ -46,8 +59,17 @@ impl Component for Checkbox<'_> {
type Style = CheckboxStyle;
type Output = EditResponse<bool>;
fn style(cx: &StyleCx<'_>, _: CheckboxStyleKey) -> CheckboxStyle {
fn style(cx: &StyleCx<'_>, key: CheckboxStyleKey) -> CheckboxStyle {
let control = cx.control();
if key.quiet {
return CheckboxStyle {
metrics: Metrics::new().min_size(Vec2::new(0.0, control.icon * QUIET_SIDE)),
focus: cx.focus_look(),
well: super::quiet_indicator(cx, cx.surface(SurfaceKind::Well).radius),
mark: super::quiet_mark(cx),
side: control.icon * QUIET_SIDE,
};
}
CheckboxStyle {
metrics: Metrics::new().min_size(Vec2::new(0.0, control.height)),
focus: cx.focus_look(),
+16
View File
@@ -33,6 +33,22 @@ fn indicator(cx: &crate::style::StyleCx<'_>, radius: egui::CornerRadius) -> crat
look
}
/// The well of a quiet check box: dark in either state with a thin faint edge, so the box reads over a picture and
/// its checked state is told by the mark alone.
fn quiet_indicator(cx: &crate::style::StyleCx<'_>, radius: egui::CornerRadius) -> crate::style::SurfaceLook {
use crate::style::{SurfaceKind, SurfaceLook};
use craft_theme::{Stateful, States};
let dark = cx.color(cx.tokens.color.surface.scrim);
let edge = cx.color(cx.tokens.color.text.faint);
let fill = Stateful::new(dark).off(States::new(dark, dark, dark, dark));
SurfaceLook::new(fill).kind(SurfaceKind::Sunken).radius(radius).border(Stateful::new(edge), cx.tokens.stroke.thin)
}
/// The mark of a quiet check box: the ink that reads on an accent, in both states.
fn quiet_mark(cx: &crate::style::StyleCx<'_>) -> craft_theme::Stateful<crate::style::StyleColor> {
craft_theme::Stateful::new(cx.color(cx.tokens.color.text.on_accent))
}
/// The fill of a check box, radio button or switch track while off: the sunken well, the state layer's press fill while
/// pressed, and the panel while disabled, so a disabled off control is an empty frame.
fn well(cx: &crate::style::StyleCx<'_>) -> craft_theme::Stateful<crate::style::StyleColor> {
+8 -3
View File
@@ -782,7 +782,12 @@ impl Component for Tile<'_> {
Stateful::new(0.0).on(States::new(thin, thin, thin, thin)).when(Flags::CURRENT, thin)
};
let radius = UiTokens::corner(tokens.shape.radius.control);
let surface = SurfaceLook::new(fill).radius(radius);
let mut surface = SurfaceLook::new(fill).radius(radius);
if key.action_place == TileActionPlace::Band {
// A band tile has no ring round its picture: the selected tile is outlined as a whole.
let (line, accent) = (c(clear), c(colors.accent.solid));
surface = surface.border(Stateful::new(line).on(States::new(accent, accent, accent, accent)), tokens.stroke.medium);
}
let surface = if key.card { surface.kind(SurfaceKind::Raised) } else { surface };
// A selected tile's name reads in the normal ink: the selection is the ring, not a fill under the name.
let normal = c(text.normal);
@@ -867,7 +872,7 @@ impl Component for Tile<'_> {
let words = cx.words::<TileWords>();
self.pills = self.corner_badges(&words, &cx.env.units());
let faces = Faces {
check: cx.face(Checkbox::new(Bind::given(Mixed::Same(false)))),
check: cx.face(Checkbox::new(Bind::given(Mixed::Same(false))).quiet(true)),
stars: cx.face(Rating::new(Bind::given(0u8), STARS).compact(true)),
pill: cx.face(Badge::new("0")).y,
action: self
@@ -1089,7 +1094,7 @@ impl Component for Tile<'_> {
ToggleState::Mixed => Mixed::Mixed,
state => Mixed::Same(state == ToggleState::On),
};
ui.face(CHECK, Checkbox::new(Bind::given(value)));
ui.face(CHECK, Checkbox::new(Bind::given(value)).quiet(true));
}
self.paint_caption(ui, cx, &placed);
self.caption_rect = match self.caption {
+16
View File
@@ -145,3 +145,19 @@ fn arrows_move_through_a_radio_group_without_picking_and_enter_picks() {
frames.frame(vec![], &mut show);
assert_eq!(picked.get(), 2, "Enter picks the focused radio");
}
/// A quiet check box (a thumbnail tile's) is a smaller box than the standard one.
#[trial]
fn a_quiet_check_box_is_smaller_than_the_standard_one() {
let side = |quiet: bool| {
let mut frames = Frames::new();
let shown = Cell::new(Rect::NOTHING);
let mut show = |ui: &mut Ui| {
shown.set(in_region(ui, REGION, |ui| Checkbox::new(Bind::given(false)).quiet(quiet).show(ui).response.rect));
};
frames.frame(vec![], &mut show);
frames.frame(vec![], &mut show);
shown.get().height()
};
assert!(side(true) < side(false), "quiet {} against standard {}", side(true), side(false));
}
+18 -1
View File
@@ -2,7 +2,7 @@
//! shows are apart and inside the tile, and a part that does not fit is left out.
use craft_theme::Srgba;
use craft_ui::contract::Show as _;
use craft_ui::contract::{Props as _, Show as _};
use craft_ui::testing::layout::in_region;
use craft_ui::testing::{Frames, trial};
use craft_ui::tile_grid::{BadgeVisibility, Tile, TileFlag};
@@ -133,3 +133,20 @@ fn a_named_action_is_exposed_by_its_own_name_while_its_tile_shows_it() {
assert!(fixture.by_label(NAMES[1]).is_some(), "the hovered tile's action is named for its tile");
assert!(fixture.by_label(NAMES[0]).is_none(), "the other tile's action stays hidden");
}
/// A selected band tile (the organize grid's) is outlined as a whole: it paints an edge that the same tile unselected
/// does not.
#[trial]
fn a_selected_band_tile_is_outlined() {
let edges = |selected: bool| {
let mut frames = Frames::new();
let mut shapes = Vec::new();
for _ in 0..3 {
shapes = frames.frame_shapes(|ui| {
Tile::new("Page 1").action_place(craft_ui::tile_grid::TileActionPlace::Band).selected(selected).show(ui);
});
}
shapes.iter().filter(|shape| matches!(shape, Shape::Rect(rect) if rect.stroke.width > 0.0 && rect.stroke.color.a() > 0)).count()
};
assert!(edges(true) > edges(false), "selected {} against unselected {}", edges(true), edges(false));
}