diff --git a/src/widget/mod.rs b/src/widget/mod.rs index b57e606..1717495 100755 --- a/src/widget/mod.rs +++ b/src/widget/mod.rs @@ -128,7 +128,10 @@ pub( crate ) fn elide( { return String::new(); } - if canvas.measure_text( text, size ) <= max_w + // Half-pixel slack: a parent that sized itself from this same + // measurement can hand back a width a few ULP short after its + // padding add-then-subtract round-trip. + if canvas.measure_text( text, size ) <= max_w + 0.5 { return text.to_string(); } diff --git a/src/widget/toggle/mod.rs b/src/widget/toggle/mod.rs index d69d3c0..965fc86 100644 --- a/src/widget/toggle/mod.rs +++ b/src/widget/toggle/mod.rs @@ -89,9 +89,10 @@ impl Toggle /// Override the preferred height (default: the theme row height, /// `theme::HEIGHT` design px). Accepts any [`Length`] so dense - /// layouts can tie the row height to the viewport. The resolved - /// value is floored at the track height so the pill never clips; - /// the track keeps its theme size and stays vertically centred. + /// layouts can tie the row height to the viewport. When the resolved + /// value falls below the theme row height, the pill (track and thumb) + /// scales down proportionally so it never clips; it never grows + /// beyond its theme size. pub fn height( mut self, h: impl Into ) -> Self { self.height = Some( h.into() ); @@ -105,7 +106,13 @@ impl Toggle /// label is set. Height is the theme-defined row height. pub fn preferred_size( &self, max_width: f32, canvas: &Canvas ) -> (f32, f32) { - let track_w = canvas.geom_px( theme::TRACK_W ); + let h = match self.height + { + Some( l ) => canvas.resolve_geom( l ), + None => canvas.geom_px( theme::HEIGHT ), + }; + let scale = ( h / canvas.geom_px( theme::HEIGHT ) ).min( 1.0 ); + let track_w = canvas.geom_px( theme::TRACK_W ) * scale; let w = if let Some( ref label ) = self.label { let text_w = canvas.measure_text( label, canvas.font_px( theme::FONT_SIZE ) ); @@ -113,11 +120,6 @@ impl Toggle } else { track_w.min( max_width ) }; - let h = match self.height - { - Some( l ) => canvas.resolve_geom( l ).max( canvas.geom_px( theme::TRACK_H ) ), - None => canvas.geom_px( theme::HEIGHT ), - }; ( w, h ) } @@ -133,9 +135,10 @@ impl Toggle pub fn draw( &self, canvas: &mut Canvas, rect: Rect, focused: bool ) { - let track_w = canvas.geom_px( theme::TRACK_W ); - let track_h = canvas.geom_px( theme::TRACK_H ); - let thumb_size = canvas.geom_px( theme::THUMB_SIZE ); + let scale = ( rect.height / canvas.geom_px( theme::HEIGHT ) ).min( 1.0 ); + let track_w = canvas.geom_px( theme::TRACK_W ) * scale; + let track_h = canvas.geom_px( theme::TRACK_H ) * scale; + let thumb_size = canvas.geom_px( theme::THUMB_SIZE ) * scale; let track_x = if let Some( ref label ) = self.label {