text_edit font sizing on Length; add Button::width and Text::line_height
Unify TextEdit's font size with the button label. The button resolved its `font_size` Length in font space (`viewport_logical`, so the `× dpi_scale` at raster lands the right physical size), while `TextEdit::font_size` took a raw `f32` that the draw / hit-test paths treated as a logical size. A caller that resolved a Length against the physical surface and passed the result as that `f32` therefore double-counted `dpi_scale` and got a font that rendered too large. `TextEdit::font_size` now takes `impl Into<Length>` and is resolved against `viewport_logical` exactly like the button, so the two paths agree. The field stores `Option<Length>` (`None` follows the widget-scaling mode at the theme default), retiring the old `f32` sentinel (`0.0` = mode, negative = fluid design px). `font_size_fluid` becomes shorthand for `Length::fluid( n )`. The `Option<Length>` flows through the `WidgetHandlers::TextEdit` snapshot and `text_input_geometry` and is resolved at draw / hit-test time, both of which carry a canvas; the inner measure helpers (`wrapping`, `hit_test`) keep `f32` because they receive the already-resolved value. Backward compatible: `f32` call sites still compile via `f32: Into<Length>` (→ `Length::px`), with the same result as before. Add `Button::width( impl Into<Length> )`. Text buttons size to their label plus padding; some layouts need a pinned width instead — a full-width or surface-proportional button. The new builder mirrors `height`: resolved in physical layout space, clamped to the available `max_width`, propagated through `map_msg`, and a no-op for icon buttons. Add `Text::line_height( mult )`. Wrapped multi-line text used the font's declared leading (`new_line_size`), which is tight for some labels; the multiplier scales the gap between wrapped lines (`1.0`, the default, keeps the natural leading — every other `text` is unchanged — and a `0.5` floor keeps lines from overlapping). Applied uniformly in `preferred_size` and `draw` so the reported height and the drawn baselines stay consistent. Tests: `button` gains a pinned-width and a size-to-content case; `text` gains a line-height default / clamp test and a check that doubling the line height doubles a wrapped block's reported height. Docs: `docs/widgets.md` `text` / `button` / `separator` sections updated for the new builders, and a `CHANGELOG.md` "Unreleased" section covering this batch alongside the responsive work already landed.
This commit is contained in:
@@ -51,6 +51,10 @@ pub struct Text
|
||||
/// the active theme's font registry on every draw. `None` keeps
|
||||
/// the canvas default font (Sora Regular in `ltk-theme-default`).
|
||||
pub( crate ) font: Option<( String, u16, FontStyle )>,
|
||||
/// Multiplier applied to the font's natural line height when wrapping
|
||||
/// onto multiple lines. `1.0` (default) uses the font-declared leading;
|
||||
/// values above `1.0` open up the gap between wrapped lines.
|
||||
pub( crate ) line_height: f32,
|
||||
}
|
||||
|
||||
impl Text
|
||||
@@ -68,6 +72,7 @@ impl Text
|
||||
wrap: false,
|
||||
truncate: true,
|
||||
font: None,
|
||||
line_height: 1.0,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,6 +151,15 @@ impl Text
|
||||
self
|
||||
}
|
||||
|
||||
/// Multiply the natural line height for wrapped text. `1.0` keeps the
|
||||
/// font's declared leading; e.g. `1.4` opens the gap between wrapped
|
||||
/// lines. Clamped to a `0.5` floor so lines never overlap.
|
||||
pub fn line_height( mut self, mult: f32 ) -> Self
|
||||
{
|
||||
self.line_height = mult.max( 0.5 );
|
||||
self
|
||||
}
|
||||
|
||||
fn resolve_font( &self, canvas: &Canvas ) -> Option<Arc<Font>>
|
||||
{
|
||||
self.font.as_ref().map( |( family, weight, style )|
|
||||
@@ -193,7 +207,7 @@ impl Text
|
||||
// visibly overlapping when stacked tight in a column.
|
||||
let line_h = canvas.font_line_metrics( size )
|
||||
.map( |m| m.new_line_size )
|
||||
.unwrap_or( size );
|
||||
.unwrap_or( size ) * self.line_height;
|
||||
let font = self.resolve_font( canvas );
|
||||
|
||||
if self.wrap
|
||||
@@ -217,7 +231,7 @@ impl Text
|
||||
.unwrap_or( size * 0.8 );
|
||||
let line_h = canvas.font_line_metrics( size )
|
||||
.map( |m| m.new_line_size )
|
||||
.unwrap_or( size );
|
||||
.unwrap_or( size ) * self.line_height;
|
||||
let font = self.resolve_font( canvas );
|
||||
|
||||
if self.wrap
|
||||
|
||||
Reference in New Issue
Block a user