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
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:
@@ -49,6 +49,9 @@ pub struct Row<Msg: Clone>
|
||||
pub( crate ) align_right: bool,
|
||||
pub( crate ) align_top: bool,
|
||||
pub( crate ) fill_height: bool,
|
||||
/// When false the row keeps every child at its preferred width even if
|
||||
/// the cluster overflows. Default true.
|
||||
pub( crate ) shrink: bool,
|
||||
}
|
||||
|
||||
impl<Msg: Clone> Row<Msg>
|
||||
@@ -63,6 +66,7 @@ impl<Msg: Clone> Row<Msg>
|
||||
align_right: false,
|
||||
align_top: false,
|
||||
fill_height: false,
|
||||
shrink: true,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,6 +105,19 @@ impl<Msg: Clone> Row<Msg>
|
||||
canvas.resolve_geom( self.padding )
|
||||
}
|
||||
|
||||
/// Keep every child at its preferred width even when the cluster does not
|
||||
/// fit, letting it overflow the row's rect.
|
||||
///
|
||||
/// The default distributes the shortfall instead, which is what a row of
|
||||
/// labelled buttons wants. Opt out for strips that are deliberately wider
|
||||
/// than their viewport — a carousel rail, a tray meant to be scrolled —
|
||||
/// where shrinking to fit would defeat the point.
|
||||
pub fn no_shrink( mut self ) -> Self
|
||||
{
|
||||
self.shrink = false;
|
||||
self
|
||||
}
|
||||
|
||||
/// Push the content block to the right edge of the available width.
|
||||
pub fn align_right( mut self ) -> Self
|
||||
{
|
||||
@@ -231,8 +248,28 @@ impl<Msg: Clone> Row<Msg>
|
||||
} )
|
||||
.sum();
|
||||
|
||||
let inner_w = ( rect.width - pad * 2.0 ).max( 0.0 );
|
||||
let leftover = ( inner_w - fixed_w - gaps ).max( 0.0 );
|
||||
let inner_w = ( rect.width - pad * 2.0 ).max( 0.0 );
|
||||
|
||||
// Deficit distribution: cap the widest children first, so a long
|
||||
// label gives up its width before a neighbouring icon does. A uniform
|
||||
// scale-down would thin a header's back arrow along with its title.
|
||||
// Spacers keep the width they were pinned to — an explicit gap is a
|
||||
// decision, not slack.
|
||||
let mut flexible: Vec<f32> = self.children.iter().zip( sizes.iter() )
|
||||
.filter( |( c, _ )| !matches!( c, Element::Flex( _ ) | Element::Spacer( _ ) ) )
|
||||
.map( |( _, ( w, _ ) )| *w )
|
||||
.collect();
|
||||
let flexible_w: f32 = flexible.iter().sum();
|
||||
let rigid_w = ( fixed_w - flexible_w ).max( 0.0 );
|
||||
let cap = if self.shrink
|
||||
{
|
||||
width_cap( &mut flexible, ( inner_w - gaps - rigid_w ).max( 0.0 ) )
|
||||
} else {
|
||||
f32::INFINITY
|
||||
};
|
||||
|
||||
let laid_w = rigid_w + flexible.iter().map( |w| w.min( cap ) ).sum::<f32>();
|
||||
let leftover = ( inner_w - laid_w - gaps ).max( 0.0 );
|
||||
let has_spacers = total_weight > 0;
|
||||
|
||||
let ( start_x, flex_unit ) = if has_spacers
|
||||
@@ -243,11 +280,11 @@ impl<Msg: Clone> Row<Msg>
|
||||
}
|
||||
else if self.align_right
|
||||
{
|
||||
( rect.x + rect.width - ( fixed_w + gaps ) - pad, 0.0 )
|
||||
( rect.x + rect.width - ( laid_w + gaps ) - pad, 0.0 )
|
||||
}
|
||||
else
|
||||
{
|
||||
( rect.x + ( rect.width - fixed_w - gaps ) / 2.0, 0.0 )
|
||||
( rect.x + ( rect.width - laid_w - gaps ) / 2.0, 0.0 )
|
||||
};
|
||||
|
||||
let mut x = start_x;
|
||||
@@ -262,7 +299,7 @@ impl<Msg: Clone> Row<Msg>
|
||||
None => flex_unit * s.weight as f32,
|
||||
},
|
||||
Element::Flex( f ) => flex_unit * f.weight as f32,
|
||||
_ => w,
|
||||
_ => w.min( cap ),
|
||||
};
|
||||
let ( y, height ) = if self.fill_height && !matches!( child, Element::Spacer( _ ) )
|
||||
{
|
||||
@@ -291,10 +328,40 @@ impl<Msg: Clone> Row<Msg>
|
||||
align_right: self.align_right,
|
||||
align_top: self.align_top,
|
||||
fill_height: self.fill_height,
|
||||
shrink: self.shrink,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Largest per-child width `c` for which `sum( min( w, c ) ) <= available`.
|
||||
///
|
||||
/// Water filling: the widest children give up width first and the narrow ones
|
||||
/// are left alone until the wide ones have been levelled down to them. In a
|
||||
/// header that means the title absorbs the whole shortfall while the back
|
||||
/// arrow beside it keeps its size. Sorts `widths` in place; returns infinity
|
||||
/// when everything already fits.
|
||||
fn width_cap( widths: &mut [f32], available: f32 ) -> f32
|
||||
{
|
||||
if widths.iter().sum::<f32>() <= available
|
||||
{
|
||||
return f32::INFINITY;
|
||||
}
|
||||
widths.sort_by( |a, b| a.partial_cmp( b ).unwrap_or( std::cmp::Ordering::Equal ) );
|
||||
|
||||
let mut prefix = 0.0_f32;
|
||||
for ( i, w ) in widths.iter().enumerate()
|
||||
{
|
||||
let remaining = ( widths.len() - i ) as f32;
|
||||
let cap = ( available - prefix ) / remaining;
|
||||
if cap <= *w
|
||||
{
|
||||
return cap.max( 0.0 );
|
||||
}
|
||||
prefix += *w;
|
||||
}
|
||||
0.0
|
||||
}
|
||||
|
||||
/// Create an empty row layout.
|
||||
pub fn row<Msg: Clone>() -> Row<Msg>
|
||||
{
|
||||
@@ -318,6 +385,75 @@ mod tests
|
||||
|
||||
fn make_canvas() -> Canvas { Canvas::new( 800, 600 ) }
|
||||
|
||||
/// Children whose preferred widths overflow the rect are scaled down to
|
||||
/// fit, rather than spilling symmetrically out of it.
|
||||
#[ test ]
|
||||
fn overflowing_children_shrink_to_fit()
|
||||
{
|
||||
let canvas = make_canvas();
|
||||
let build = || row::<()>()
|
||||
.spacing( 0.0 )
|
||||
.padding( 0.0 )
|
||||
.push( crate::toggle( false ) )
|
||||
.push( crate::toggle( false ) );
|
||||
|
||||
let natural = build().preferred_size( 10_000.0, &canvas ).0;
|
||||
assert!( natural > 0.0, "toggles must report a width for this to test anything" );
|
||||
let rect = Rect { x: 0.0, y: 0.0, width: natural / 2.0, height: 48.0 };
|
||||
|
||||
let laid: f32 = build().layout( rect, &canvas )
|
||||
.iter().map( |( r, _ )| r.width ).sum();
|
||||
assert!( laid <= rect.width + 0.5, "laid out {laid} into {}", rect.width );
|
||||
|
||||
let kept: f32 = build().no_shrink().layout( rect, &canvas )
|
||||
.iter().map( |( r, _ )| r.width ).sum();
|
||||
assert!( kept > rect.width, "no_shrink kept {kept}, expected wider than {}", rect.width );
|
||||
}
|
||||
|
||||
/// The shortfall comes off the widest child first: a narrow neighbour —
|
||||
/// a header's back arrow next to its title — keeps its size.
|
||||
#[ test ]
|
||||
fn narrow_child_survives_a_wide_neighbour()
|
||||
{
|
||||
let mut w = vec![ 20.0_f32, 300.0 ];
|
||||
let cap = width_cap( &mut w, 200.0 );
|
||||
assert_eq!( 20.0_f32.min( cap ), 20.0 );
|
||||
assert_eq!( 300.0_f32.min( cap ), 180.0 );
|
||||
}
|
||||
|
||||
/// Equal children share the shortfall equally, which is what a segmented
|
||||
/// control wants.
|
||||
#[ test ]
|
||||
fn equal_children_shrink_equally()
|
||||
{
|
||||
let mut w = vec![ 100.0_f32, 100.0, 100.0 ];
|
||||
let cap = width_cap( &mut w, 150.0 );
|
||||
assert_eq!( cap, 50.0 );
|
||||
}
|
||||
|
||||
#[ test ]
|
||||
fn nothing_shrinks_when_it_all_fits()
|
||||
{
|
||||
let mut w = vec![ 10.0_f32, 20.0 ];
|
||||
assert_eq!( width_cap( &mut w, 100.0 ), f32::INFINITY );
|
||||
}
|
||||
|
||||
/// Pinned spacers are an explicit gap, not slack, so the shrink pass
|
||||
/// leaves them alone.
|
||||
#[ test ]
|
||||
fn pinned_spacers_keep_their_width_when_shrinking()
|
||||
{
|
||||
let canvas = make_canvas();
|
||||
let r = row::<()>()
|
||||
.spacing( 0.0 )
|
||||
.padding( 0.0 )
|
||||
.push( crate::spacer().width( 60.0 ).height( 10.0 ) )
|
||||
.push( crate::toggle( false ) );
|
||||
let rect = Rect { x: 0.0, y: 0.0, width: 70.0, height: 48.0 };
|
||||
let cells = r.layout( rect, &canvas );
|
||||
assert_eq!( cells[ 0 ].0.width, 60.0 );
|
||||
}
|
||||
|
||||
#[ test ]
|
||||
fn align_right_returns_full_max_width()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user