responsive fluid/physical scaling, widget-API stabilization, and perf guardrails
CI / build + test (push) Has been cancelled
CI / cargo audit (push) Has been cancelled

Responsive scaling. ltk now offers two first-class ways to size a UI so it adapts across screens, chosen per process via `WidgetScaling { Fluid, Physical }` (`set_widget_scaling` / `widget_scaling`, default `Fluid`). Fluid sizing (`Length::fluid( px )`) makes a design pixel a proportion of the surface's smaller side, calibrated against a reference width (`set_fluid_reference` / `fluid_reference`, 412 px default) and bounded by `FLUID_MIN` / `FLUID_MAX`; physical sizing (`Length::dp( px )`) is a constant-physical-size pixel scaled by display density (`set_density` / `density`). `Length` gains `orient( portrait, landscape )` — resolve one value in portrait, another in landscape — plus `widget( px )`, which picks fluid or dp per the active mode. Canvas exposes `geom_px` (geometry, resolved in physical layout space) and `font_px` (font size, bridging logical / physical per mode) so widgets and apps share one resolution path. Note the rename: `set_design_reference` / `design_reference` became `set_fluid_reference` / `fluid_reference`, and `Length::dp` changed meaning — the old surface-proportional behaviour now lives on `Length::fluid`.
Widgets. Every stock widget resolves its default geometry and font through the widget-scaling mode instead of frozen pixels, so a whole UI scales coherently without per-call units. New size builders where they were missing: `button` gains `font_size` / `height`, `text_edit` gains `height` / `font_size_fluid`, `separator` gains `pad_v`, and assorted widgets accept a `Length` where they previously took only `f32`.
Overlays. `OverlaySpec::size` is now `( Length, Length )` instead of `( u32, u32 )`, resolved against the main surface when the overlay is materialized, so overlays can scale with the display; `Length::px( … )` reproduces the old fixed sizing.
API stabilization (toward 1.0). Widget struct fields are now `pub( crate )` — they are configured through builders, not field access — except the value / state types apps genuinely read or construct (`Time`, `Date`, `ComboState`), which stay public. The internal `test_support` helpers move behind a `test-support` Cargo feature (off by default, so third-party builds never see them; ltk's own `make test` enables it). `Separator` drops its `0.0`-means-mode sentinel for `Option<Length>`, so an explicit `pad_v( 0.0 )` is a real flush divider distinct from the mode-following default.
Performance guardrails. Opt-in diagnostics via `LTK_PERF_WARN=1` warn about stuck animations, sustained software-render animation, and low `poll_interval`; software-rendered animation is capped near 30 Hz to spare CPU on machines that fall back off EGL. Apps can override the cap with `App::cap_software_animation`.
Docs and build. The two scaling modes are documented in README, onboarding and architecture, with the earlier gradient / backdrop doc drift cleaned up. The Makefile now ships the `locales/` directory into the packaged crate (fixing i18n keys rendering raw for downstreams), builds the new `responsive` example, and runs tests with `--features test-support`.
This commit is contained in:
2026-07-07 17:40:33 +02:00
parent d4d7ee742e
commit ce893ac776
83 changed files with 1850 additions and 526 deletions
+46 -14
View File
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: LGPL-2.1-only
// Copyright (C) 2026 Liberux Labs, S. L. <info@liberux.net>
use crate::types::{ Color, Rect };
use crate::types::{ Color, Length, Rect };
use crate::render::Canvas;
use super::Element;
@@ -30,13 +30,16 @@ mod theme;
pub struct Separator
{
/// Stroke colour. Defaults to the theme's `divider` palette slot.
pub color: Color,
/// Line thickness in logical pixels.
pub thickness: f32,
pub( crate ) color: Color,
/// Line thickness. `None` follows the process [`crate::WidgetScaling`]
/// mode; `Some( len )` pins it (including `Length::px( 0.0 )`).
pub( crate ) thickness: Option<Length>,
/// Vertical padding above and below the line, baked into
/// `preferred_size` so the divider visually centres in the row a
/// parent column allocates.
pub pad_v: f32,
/// parent column allocates. `None` follows the
/// [`crate::WidgetScaling`] mode; `Some( len )` pins it — pass
/// `Length::px( 0.0 )` for a flush, padding-less divider.
pub( crate ) pad_v: Option<Length>,
}
impl Separator
@@ -48,11 +51,29 @@ impl Separator
Self
{
color: theme::color(),
thickness: theme::THICKNESS,
pad_v: theme::PAD_V,
thickness: None,
pad_v: None,
}
}
/// Resolve the line thickness: an explicit value wins, otherwise the
/// widget-scaling default.
fn resolved_thickness( &self, canvas: &Canvas ) -> f32
{
self.thickness
.map( |l| l.resolve( canvas.viewport_layout(), Length::EM_BASE_DEFAULT ) )
.unwrap_or_else( || canvas.geom_px( theme::THICKNESS ) )
}
/// Resolve the vertical padding: an explicit value wins, otherwise the
/// widget-scaling default.
fn resolved_pad_v( &self, canvas: &Canvas ) -> f32
{
self.pad_v
.map( |l| l.resolve( canvas.viewport_layout(), Length::EM_BASE_DEFAULT ) )
.unwrap_or_else( || canvas.geom_px( theme::PAD_V ) )
}
/// Override the stroke colour. Useful for emphasised dividers between
/// destructive actions.
pub fn color( mut self, color: Color ) -> Self
@@ -61,25 +82,36 @@ impl Separator
self
}
/// Override the line thickness. Defaults to 1 logical pixel.
pub fn thickness( mut self, t: f32 ) -> Self
/// Override the line thickness. Accepts logical `f32` pixels or any
/// [`Length`]. Without this it follows the widget-scaling mode.
pub fn thickness( mut self, t: impl Into<Length> ) -> Self
{
self.thickness = t;
self.thickness = Some( t.into() );
self
}
/// Override the vertical padding above and below the line. Accepts
/// logical `f32` pixels or any [`Length`]; pass `0.0` for a flush,
/// padding-less divider. Without this it follows the widget-scaling mode.
pub fn pad_v( mut self, p: impl Into<Length> ) -> Self
{
self.pad_v = Some( p.into() );
self
}
/// Return the preferred `(width, height)`. Width is `max_width`;
/// height is `thickness + 2 × pad_v`.
pub fn preferred_size( &self, max_width: f32 ) -> (f32, f32)
pub fn preferred_size( &self, max_width: f32, canvas: &Canvas ) -> (f32, f32)
{
( max_width, self.thickness + self.pad_v * 2.0 )
( max_width, self.resolved_thickness( canvas ) + self.resolved_pad_v( canvas ) * 2.0 )
}
/// Draw the divider line into `canvas` at `rect`.
pub fn draw( &self, canvas: &mut Canvas, rect: Rect )
{
let y = rect.y + rect.height / 2.0;
canvas.draw_line( rect.x, y, rect.x + rect.width, y, self.color, self.thickness );
let thickness = self.resolved_thickness( canvas );
canvas.draw_line( rect.x, y, rect.x + rect.width, y, self.color, thickness );
}
}
+18 -4
View File
@@ -4,16 +4,30 @@
use super::*;
#[test]
fn default_thickness()
fn default_thickness_and_pad_follow_the_mode()
{
let s = separator();
assert_eq!( s.thickness, theme::THICKNESS );
assert_eq!( s.thickness, None ); // None → follows the widget-scaling mode
assert_eq!( s.pad_v, None );
}
#[test]
fn explicit_zero_pad_v_is_flush_not_the_mode_default()
{
// The Option repr lets `0.0` mean a real flush divider, distinct from
// the mode-following default.
let _g = crate::TEST_GLOBALS_LOCK.lock().unwrap_or_else( |e| e.into_inner() );
let s = separator().pad_v( 0.0 );
let canvas = crate::render::Canvas::new( 412, 900 );
assert_eq!( s.preferred_size( 200.0, &canvas ).1, canvas.geom_px( theme::THICKNESS ) );
}
#[test]
fn preferred_height_includes_padding()
{
let _g = crate::TEST_GLOBALS_LOCK.lock().unwrap_or_else( |e| e.into_inner() );
let s = separator();
let ( _, h ) = s.preferred_size( 200.0 );
assert_eq!( h, theme::THICKNESS + theme::PAD_V * 2.0 );
let canvas = crate::render::Canvas::new( 412, 900 );
let ( _, h ) = s.preferred_size( 200.0, &canvas );
assert_eq!( h, canvas.geom_px( theme::THICKNESS ) + canvas.geom_px( theme::PAD_V ) * 2.0 );
}