Add compact action card tiles

This commit is contained in:
Dylan M. Taylor
2026-11-21 14:20:24 -05:00
parent e84a6b5dec
commit a8aeb9858b
3 changed files with 73 additions and 0 deletions
@@ -107,6 +107,12 @@ fn tile_hue(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
Tile::new("Quarterly report.pdf").icon(IconId::new("file")).hue(DataColor::new(Color32::from_rgb(196, 64, 52))).show(ui).into()
}
/// A compact action card: icon and name, a description, and the action that activates the whole card.
#[story(Tile<'static>, covers = [], drive = Drive::Activate)]
fn tile_summary_card(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
Tile::new("Arrange pages").icon(IconId::new("layout_grid")).detail("Move · Rotate · Delete").summary_action("Use now").card().show(ui).into()
}
fn click_check(fixture: &mut Fixture) {
if let Some(check) = fixture.by_label("Select IMG_0003.jpg") {
fixture.click(&check);
+52
View File
@@ -344,6 +344,10 @@ pub struct Tile<'a> {
/// A second line under the name.
#[prop(into)]
detail: Option<Label>,
/// Compact action card: the title sits beside the icon, above the detail and action text. An empty action
/// shows a centered icon and title for a simple destination card. Clicking anywhere activates the tile.
#[prop(into)]
summary_action: Option<Label>,
/// What a screen reader reads as the tile's name, in place of the name with its details; the marks' tips still
/// join it.
#[prop(into)]
@@ -424,6 +428,7 @@ impl<'a> Tile<'a> {
detail_line: false,
card: false,
detail: None,
summary_action: None,
accessible_name: None,
rating: None,
flag: None,
@@ -800,6 +805,10 @@ impl Component for Tile<'_> {
fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 {
let sizes = &cx.style.sizes;
if self.summary_action.is_some() {
let width = cx.room.map_or(sizes.thumbs[1], |room| room.min(sizes.thumbs[1]));
return Vec2::new(width, sizes.face + sizes.name_line + 2.0 * sizes.detail_line + 3.0 * sizes.tight);
}
let ratio = self.aspect.of(1.0);
let caption = sizes.caption(self.caption, self.has_detail_line());
let column = sizes.column(self.caption);
@@ -840,6 +849,9 @@ impl Component for Tile<'_> {
_ => cx.hovered() || cx.resolved.selected == Some(true) || self.has_bar_values(),
};
let mut placed = self.place(&sizes, cx.content_rect, rtl, bar_shown, &faces, &pill_widths);
if self.summary_action.is_some() {
placed.picture = cx.content_rect;
}
let _ = cx.part(PICTURE, placed.picture).sense(Sense::HOVER);
if let (Some(state), Some((at, _))) = (self.check, &placed.check) {
let mut flags = Flags::empty();
@@ -930,6 +942,46 @@ impl Component for Tile<'_> {
let Some(placed) = self.placed.take() else { return };
let style = cx.style;
let picture = placed.picture;
if let Some(action) = &self.summary_action {
let flow = Flow::new(picture, cx.flow.is_rtl());
let draw = ui.draw();
let icon_side = style.face.side.min(picture.width()).min(picture.height());
let ink = match self.hue {
Some(hue) => ui.with_style(cx.resolved.variant, |scx| scx.ink_of(hue, Rgba::from(style.placeholder.egui()))),
None => cx.resolve(&style.face.color),
};
let centered = action.as_str().is_empty();
let icon = if centered {
Rect::from_center_size(egui::pos2(picture.center().x, picture.center().y - icon_side / 2.0), Vec2::splat(icon_side))
} else {
let cell = flow.span(0.0, icon_side);
Rect::from_min_size(cell.min, Vec2::splat(icon_side))
};
if let Some(face) = self.icon.take() {
draw.icon(icon, &face.color(ink));
}
let title = TextRun::new(cx.resolved.label).role(style.name.role).color(cx.resolve(&style.name.color)).elide();
let title_area = if centered {
Rect::from_min_max(egui::pos2(picture.left(), icon.bottom() + style.sizes.tight), picture.max)
} else {
flow.take_start(icon_side + style.sizes.tight).1.rect()
};
let title_layout = draw.layout(&title, title_area.width());
let title_x = if centered { title_area.center().x - title_layout.size().x / 2.0 } else { self.text_x(title_area, title_layout.size().x, cx) };
draw.galley(egui::pos2(title_x, title_area.top()), &title_layout);
if !centered {
if let Some(detail) = &self.detail {
let run = TextRun::new(detail.as_str()).role(style.detail.role).color(cx.resolve(&style.detail.color)).elide();
let layout = draw.layout(&run, picture.width());
draw.galley(egui::pos2(self.text_x(picture, layout.size().x, cx), icon.bottom() + style.sizes.tight), &layout);
}
let run = TextRun::new(action.as_str()).role(style.detail.role).color(style.range).elide();
let layout = draw.layout(&run, picture.width());
draw.galley(egui::pos2(self.text_x(picture, layout.size().x, cx), picture.bottom() - layout.size().y), &layout);
}
self.caption_rect = Some(title_area);
return;
}
let flat = CornerRadius::ZERO;
{
let draw = ui.draw();
@@ -45,6 +45,21 @@ fn overlap(a: &Rect, b: &Rect) -> bool {
both.width() > 0.01 && both.height() > 0.01
}
#[trial]
fn summary_card_paints_title_description_and_action() {
let mut frames = Frames::new();
let mut shapes = Vec::new();
for _ in 0..3 {
shapes = frames.frame_shapes(|ui| {
Tile::new("Arrange").icon(craft_behavior::IconId::new("grid")).detail("Move pages").summary_action("Use now").card().show(ui);
});
}
let texts: Vec<_> = shapes.iter().filter_map(|shape| match shape { Shape::Text(text) => Some(text.galley.job.text.as_str()), _ => None }).collect();
for text in ["Arrange", "Move pages", "Use now"] {
assert!(texts.contains(&text), "missing {text}: {texts:?}");
}
}
#[trial]
fn the_badge_bar_leaves_out_what_does_not_fit_and_never_overlaps() {
let (_, glyphs, dots) = badges(400.0);