menu/ribbon: render framed Gallery with sunken well surface and restore stepper button contrast

Framed galleries now render with the sunken well surface (`colors.surface.sunken`)
and provide a dedicated stepper track (`colors.surface.panel`) with divider definition
for the stepper buttons column to preserve contrast and hierarchy in both light and dark modes.
This commit is contained in:
batistalucasv committed 2026-11-21 23:31:00 -03:00
1 parent 750a9bdd57
commit ebba2b5d30
2 files changed
+76 -2

No files matched your search

+23 -2
View File
@@ -85,6 +85,8 @@ pub struct Gallery<'a, K: Clone + Eq + Hash + Send + Sync + 'static> {
caption: CaptionPlace,
/// Each shown tile's rect.
laid: Vec<Rect>,
/// The column of stepper buttons, if shown.
column: Rect,
picked: Option<K>,
menu: Option<ContextMenuRequest<K>>,
}
@@ -118,6 +120,7 @@ impl<'a, K: Clone + Eq + Hash + Send + Sync + 'static> Gallery<'a, K> {
thumb_size: ThumbSize::Medium,
caption: CaptionPlace::None,
laid: Vec::new(),
column: Rect::NOTHING,
picked: None,
menu: None,
}
@@ -131,7 +134,7 @@ impl<'a, K: Clone + Eq + Hash + Send + Sync + 'static> Gallery<'a, K> {
self
}
/// One rounded frame on the raised surface around the tiles and the buttons, as the old Word-style ribbon drew
/// One rounded frame on the sunken well surface around the tiles and the buttons, as the old Word-style ribbon drew
/// its Styles gallery.
pub fn framed(mut self) -> Self {
self.framed = true;
@@ -509,6 +512,9 @@ pub struct GalleryStyle {
frame: (StyleColor, StyleColor, f32),
/// The room inside a frame.
inset: f32,
/// The stepper buttons column's track fill and divider.
track: StyleColor,
divider: StyleColor,
}
/// The sizes the gallery's faces measure.
@@ -527,8 +533,10 @@ impl<K: Clone + Eq + Hash + Send + Sync + 'static> Component for Gallery<'_, K>
let colors = &cx.tokens.color;
GalleryStyle {
metrics: Metrics::new().gap(cx.tokens.space.gap.inline),
frame: (cx.color(colors.surface.raised), cx.color(colors.line.control_edge), cx.tokens.shape.radius.control),
frame: (cx.color(colors.surface.sunken), cx.color(colors.line.control_edge), cx.tokens.shape.radius.control),
inset: cx.tokens.space.gap.inline,
track: cx.color(colors.surface.panel),
divider: cx.color(colors.line.divider),
}
}
@@ -566,6 +574,7 @@ impl<K: Clone + Eq + Hash + Send + Sync + 'static> Component for Gallery<'_, K>
let buttons = self.buttons(level);
let rect = rect.shrink2(Vec2::new(self.inset(cx.style), 0.0));
let (column, rest) = Flow::new(rect, cx.flow.is_rtl()).take_end(if buttons { sizes.button.x } else { 0.0 });
self.column = if buttons { column } else { Rect::NOTHING };
self.laid.clear();
for at in 0..shown {
let cell = rest.span(at as f32 * (sizes.tile.x + sizes.gap), sizes.tile.x);
@@ -702,6 +711,18 @@ impl<K: Clone + Eq + Hash + Send + Sync + 'static> Component for Gallery<'_, K>
if !self.buttons(level) {
return;
}
if self.framed && self.column.is_positive() {
let draw = ui.draw();
let radius = cx.style.frame.2.round().clamp(0.0, 255.0) as u8;
let track_shape = PaintShape::Rect(self.column, CornerRadius::same(radius));
draw.fill(&track_shape, cx.style.track);
draw.stroke(&track_shape, craft_ui::primitives::StrokeWidth::Thin, cx.style.divider);
let height = self.column.height() / 3.0;
let y1 = self.column.min.y + height;
let y2 = self.column.min.y + 2.0 * height;
draw.rule(craft_ui::style::RuleKind::Divider, egui::pos2(self.column.left(), y1), egui::pos2(self.column.right(), y1));
draw.rule(craft_ui::style::RuleKind::Divider, egui::pos2(self.column.left(), y2), egui::pos2(self.column.right(), y2));
}
ui.face(UP, IconButton::new(IconId::new("chevron_up")).label(words.up.clone()).enabled(start > 0));
ui.face(DOWN, IconButton::new(IconId::new("chevron_down")).label(words.down.clone()).enabled(start < last));
ui.face(MORE, IconButton::new(IconId::new("more")).label(words.more.clone()));
+53
View File
@@ -138,3 +138,56 @@ fn a_wide_captioned_gallery_names_its_tiles_under_wide_pictures() {
assert!(name.min.y >= wide.min.y + square.width() / 2.0, "the name {name:?} is under the picture in {wide:?}");
assert!(!plain.iter().any(|(text, _)| text == "Tile 0"), "a plain tile paints no name: {plain:?}");
}
/// A framed gallery paints a sunken well fill over its bounds, and a panel fill over its stepper button track.
#[trial]
fn a_framed_gallery_paints_sunken_well_and_stepper_track() {
use craft_ui::runtime::env::Env;
use egui::Color32;
fn fills(shapes: &[egui::Shape], out: &mut Vec<(egui::Rect, egui::Color32)>) {
for shape in shapes {
match shape {
egui::Shape::Vec(inner) => fills(inner, out),
egui::Shape::Rect(rect) => out.push((rect.rect, rect.fill)),
_ => {}
}
}
}
let colors_and_rect = Rc::new(Cell::new((Rect::NOTHING, Color32::TRANSPARENT, Color32::TRANSPARENT)));
let held = colors_and_rect.clone();
let mut fixture = Fixture::new(move |ui| {
let [sr, sg, sb, sa] = Env::of(ui).tokens.tokens().color.surface.sunken.0;
let [pr, pg, pb, pa] = Env::of(ui).tokens.tokens().color.surface.panel.0;
let sunken = Color32::from_rgba_unmultiplied(sr, sg, sb, sa);
let panel = Color32::from_rgba_unmultiplied(pr, pg, pb, pa);
let paint = |_: &ContentPainterCx, _: Rect| {};
let items: Vec<GalleryItem<'_, usize>> =
(0..10).map(|at| GalleryItem { key: at, label: Label::from(format!("Tile {at}")), face: GalleryFace::Paint(&paint) }).collect();
let out = Gallery::new("Styles", &items, None).tiles(6, 3).framed().show(ui);
held.set((out.response.rect, sunken, panel));
});
fixture.settle();
let (gallery_rect, sunken, panel) = colors_and_rect.get();
assert!(gallery_rect.is_positive(), "the gallery paints a positive rect");
let mut seen = Vec::new();
fills(&fixture.frame_shapes(), &mut seen);
assert!(
seen.iter()
.any(|(rect, fill)| *fill == sunken && rect.contains_rect(gallery_rect.shrink(1.0)) && gallery_rect.expand(1.0).contains_rect(*rect)),
"a sunken fill covers the gallery {gallery_rect:?}: {seen:?}"
);
let up = fixture.by_label("Previous row").expect("the up button shows");
let down = fixture.by_label("Next row").expect("the down button shows");
let more = fixture.by_label("More").expect("the more button shows");
let column = up.rect.union(down.rect).union(more.rect);
let stepper_track =
seen.iter().find(|(rect, fill)| *fill == panel && rect.contains_rect(column.shrink(1.0)) && gallery_rect.contains_rect(*rect));
assert!(stepper_track.is_some(), "a panel fill covers the stepper track column {column:?}: {seen:?}");
}