row, button, widget: distribute a row's width shortfall instead of overflowing, elide button labels into the rect they are granted, icon_size takes a Length
CI / build + test (push) Has been cancelled
CI / cargo audit (push) Has been cancelled

Row deficit distribution (layout/row.rs). `Row` knew how to hand out leftover width but had no notion of a shortfall: `leftover` was floored at zero and every child was laid out at its preferred width, so a cluster wider than its rect simply overflowed — and symmetrically, because the no-spacer branch centres the block, which is why both end labels of a segmented control were clipped at once rather than only the trailing one. The shortfall now comes off the widest children first, by water filling: `width_cap` sorts the flexible widths and returns the largest per-child cap `c` for which `sum( min( w, c ) ) <= available`, so a long title absorbs the whole deficit while an icon button beside it keeps its size, and equal siblings — a segmented control — share it evenly. A uniform scale-down, which was the first attempt, was wrong precisely there: it thinned a header's back arrow along with the title it sat next to. Spacers and flex children stay out of the flexible set, so a pinned gap keeps the width it was given — an explicit spacer is a decision, not slack — and `Row::no_shrink()` opts a row out entirely, for strips deliberately wider than their viewport such as a carousel rail meant to be scrolled. `align_right` and the centring branch now measure the laid width rather than the preferred one, so a shrunken row is positioned against what it actually occupies.
Button label elision (widget/button/mod.rs, widget/mod.rs, widget/list_item/mod.rs). Shrinking a row is only half the fix: a leaf handed less width than it asked for still painted its full string, so the deficit came out clipped instead of truncated. `draw_text_button` now elides the label against the rect the layout granted, less the horizontal padding, for all three variants. The truncation rule moves out of `ListItem`, where it was a private helper, into a single crate-internal `widget::elide` the two share. `Text` keeps its own inline copy for now: it measures through an optional font override that this signature does not carry, and folding the two together is a separate change.
icon_size as a Length (widget/button/mod.rs). `Button::icon_size` was the one geometry setter that ignored the widget-scaling mode — it took a bare `f32`, pinned it, and used `0.0` as the "unset" sentinel. It now takes `impl Into<Length>` behind an `Option` and resolves through `Canvas::resolve_geom`, the way `height`, `width` and `font_size` already do. A bare number still means `Length::px`, so every existing call keeps its exact size; what it adds is `Length::widget( n )`, which follows the active mode the way stock icons do. Without it a back arrow pinned at 21 px sat next to a `list_item` chevron that fluid sizing had grown well past 21, and read as visibly smaller on the same row.
This commit is contained in:
2026-08-05 12:37:03 +02:00
parent 530a5696b9
commit 1290f9400e
6 changed files with 228 additions and 50 deletions
+30 -15
View File
@@ -69,7 +69,7 @@ pub struct Button<Msg: Clone>
/// Width and height in pixels for icon buttons. The `0.0` default
/// follows the process [`crate::WidgetScaling`] mode (via
/// [`crate::Canvas::geom_px`]); any positive value pins an explicit size.
pub( crate ) icon_size: f32,
pub( crate ) icon_size: Option<Length>,
/// Optional label font size for text buttons. `None` uses the theme's
/// default (`theme::FONT_SIZE`); a [`Length`] scales the label with the
/// surface (e.g. `Length::vmin( 2.2 ).clamp( 14.0, 22.0 )`).
@@ -115,7 +115,7 @@ impl<Msg: Clone> Button<Msg>
on_long_press: None,
on_drag_start: None,
variant: ButtonVariant::Primary,
icon_size: 0.0,
icon_size: None,
font_size: None,
height: None,
width: None,
@@ -153,7 +153,7 @@ impl<Msg: Clone> Button<Msg>
on_long_press: None,
on_drag_start: None,
variant: ButtonVariant::Tertiary,
icon_size: 0.0,
icon_size: None,
font_size: None,
height: None,
width: None,
@@ -237,10 +237,16 @@ impl<Msg: Clone> Button<Msg>
self
}
/// Set the display size (width = height) for icon buttons in pixels.
pub fn icon_size( mut self, size: f32 ) -> Self
/// Set the display size (width = height) for icon buttons.
///
/// A bare `f32` is `Length::px`, which pins the icon at that many canvas
/// pixels. Pass `Length::widget( n )` to have it follow the active
/// widget-scaling mode, which is what stock icons — a `list_item`
/// disclosure arrow, for one — already do, so the two match on a surface
/// where fluid sizing has grown them.
pub fn icon_size( mut self, size: impl Into<Length> ) -> Self
{
self.icon_size = size;
self.icon_size = Some( size.into() );
self
}
@@ -293,11 +299,17 @@ impl<Msg: Clone> Button<Msg>
.unwrap_or_else( || canvas.geom_px( theme::HEIGHT ) )
}
/// Resolve the icon-button size: a positive [`Self::icon_size`] pins it,
/// the `0.0` sentinel follows the widget-scaling mode.
/// Resolve the icon-button size. Unset follows the widget-scaling mode
/// through `theme::HEIGHT`; an explicit value resolves like any other
/// geometry `Length`, so `Length::px` pins a physical size while
/// `Length::widget` tracks the surface the way stock icons do.
fn resolved_icon_size( &self, canvas: &Canvas ) -> f32
{
if self.icon_size > 0.0 { self.icon_size } else { canvas.geom_px( theme::HEIGHT ) }
match self.icon_size
{
Some( l ) => canvas.resolve_geom( l ),
None => canvas.geom_px( theme::HEIGHT ),
}
}
/// Assign a stable identifier for focus management.
@@ -389,6 +401,9 @@ impl<Msg: Clone> Button<Msg>
let is_disabled = self.on_press.is_none();
let fs = self.label_font_size( canvas );
let text_y = rect.y + (rect.height + fs) / 2.0 - 2.0;
// `rect` is what the layout granted, which is not always the
// preferred width: a row that cannot fit its children shrinks them.
let label = super::elide( canvas, label, fs, rect.width - canvas.geom_px( theme::PAD_H ) * 2.0 );
match self.variant
{
@@ -412,9 +427,9 @@ impl<Msg: Clone> Button<Msg>
theme::RADIUS + theme::FOCUS_W + 2.0,
);
}
let text_w = canvas.measure_text( label, fs );
let text_w = canvas.measure_text( &label, fs );
canvas.draw_text(
label,
&label,
rect.x + (rect.width - text_w) / 2.0,
text_y,
fs,
@@ -438,9 +453,9 @@ impl<Msg: Clone> Button<Msg>
theme::RADIUS + theme::FOCUS_W + 2.0,
);
}
let text_w = canvas.measure_text( label, fs );
let text_w = canvas.measure_text( &label, fs );
canvas.draw_text(
label,
&label,
rect.x + (rect.width - text_w) / 2.0,
text_y,
fs,
@@ -455,9 +470,9 @@ impl<Msg: Clone> Button<Msg>
let ring = rect.expand( 2.0 );
canvas.stroke_rect( ring, theme::focus_color(), theme::FOCUS_W, theme::RADIUS );
}
let text_w = canvas.measure_text( label, fs );
let text_w = canvas.measure_text( &label, fs );
canvas.draw_text(
label,
&label,
rect.x + (rect.width - text_w) / 2.0,
text_y,
fs,