mirror of
https://github.com/storytold/craft-libs.git
synced 2026-12-01 02:36:54 +01:00
buttons: a stacked button or toggle takes the control corners, so a tall pressed toggle is no oval
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d4dd00eae1
commit
2022e8bc14
4 files changed
+37
-1
No files matched your search
@@ -307,6 +307,7 @@ impl Component for Button<'_> {
|
||||
};
|
||||
if key.arrangement == Arrangement::Stacked {
|
||||
style.metrics = face::stacked_metrics(cx, &cx.variant_look());
|
||||
style.surface = style.surface.radius(face::stacked_corners(cx));
|
||||
}
|
||||
if let Some(least) = key.least_width {
|
||||
style.metrics.min_size.x = style.metrics.min_size.x.max(least.points(cx.tokens));
|
||||
|
||||
@@ -392,6 +392,12 @@ pub(super) fn on_surface(cx: &StyleCx<'_>, off: &VariantParts, on: &VariantParts
|
||||
look
|
||||
}
|
||||
|
||||
/// The corners of a stacked face's surface: the control corners. A capsule is the shape of a one-line control; over a
|
||||
/// face as tall as an icon over two label lines it would stretch into an oval.
|
||||
pub(super) fn stacked_corners(cx: &StyleCx<'_>) -> egui::CornerRadius {
|
||||
UiTokens::corner(cx.tokens.shape.radius.control)
|
||||
}
|
||||
|
||||
/// The look of a button that shows only its icon: an icon button, or a toggle button that is `icon_only`.
|
||||
pub(crate) struct IconFace {
|
||||
pub surface: SurfaceLook,
|
||||
|
||||
@@ -152,7 +152,9 @@ impl Component for ToggleButton<'_> {
|
||||
(face.surface, face.metrics)
|
||||
} else {
|
||||
icon.color = icon.color.on(on.text.off);
|
||||
(face::on_surface(cx, &off, &on), face::metrics_of(cx, &off, key.arrangement))
|
||||
let surface = face::on_surface(cx, &off, &on);
|
||||
let surface = if key.arrangement == Arrangement::Stacked { surface.radius(face::stacked_corners(cx)) } else { surface };
|
||||
(surface, face::metrics_of(cx, &off, key.arrangement))
|
||||
};
|
||||
// A mixed value shows the off look, as Photoshop's style toggles do; the access state tells it from off.
|
||||
surface.fill = surface.fill.when(Flags::MIXED, off.fill.off.rest);
|
||||
|
||||
@@ -428,6 +428,33 @@ fn in_ribbon(ui: &mut Ui, show: impl FnOnce(&mut Ui)) {
|
||||
);
|
||||
}
|
||||
|
||||
/// A stacked button or toggle takes the control corners: the capsule an inline button may take, stretched over an
|
||||
/// icon and two label lines, drew a pressed ribbon pen as an oval.
|
||||
#[trial]
|
||||
fn a_stacked_buttons_surface_keeps_control_corners_and_never_turns_oval() {
|
||||
let corner = |type_name: &'static str, stacked: bool| {
|
||||
let arrangement = if stacked { Arrangement::Stacked } else { Arrangement::Inline };
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
in_ribbon(ui, |ui| {
|
||||
let mut on = true;
|
||||
let _ = ToggleButton::new("Red Pen", Bind::bound(&mut on)).icon(IconId::new("copy")).arrangement(arrangement).show(ui);
|
||||
let _ = Button::new("Pictures").icon(IconId::new("copy")).arrangement(arrangement).show(ui);
|
||||
});
|
||||
});
|
||||
fixture.settle();
|
||||
let style = fixture.styles_of(type_name).into_iter().next().expect("the control resolved its style");
|
||||
let surface = style.looks.surface.clone().expect("the control has a surface");
|
||||
let label = if type_name.contains("Toggle") { "Red Pen" } else { "Pictures" };
|
||||
(f32::from(surface.radius.nw), fixture.by_label(label).expect("the control").rect)
|
||||
};
|
||||
for type_name in [std::any::type_name::<ToggleButton<'static>>(), std::any::type_name::<Button<'static>>()] {
|
||||
let (radius, rect) = corner(type_name, true);
|
||||
assert!(radius < rect.height() / 4.0, "{type_name}: a stacked face's corners stay small against its height {rect:?}: {radius}");
|
||||
let (inline, _) = corner(type_name, false);
|
||||
assert!(radius <= inline, "{type_name}: a stacked face is never rounder than an inline one: {radius} against {inline}");
|
||||
}
|
||||
}
|
||||
|
||||
/// In a ribbon a push or split button that asks for no variant is flat at rest: it draws the ghost surface, and its
|
||||
/// sides take the control's padding, so it is narrower than a ghost button elsewhere. Anywhere else a button keeps its
|
||||
/// outlined default, and in a ribbon a variant it asks for stays.
|
||||
|
||||
Reference in new issue
Block a user