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`.
147 lines
5.0 KiB
Rust
147 lines
5.0 KiB
Rust
// SPDX-License-Identifier: LGPL-2.1-only
|
||
// Copyright (C) 2026 Liberux Labs, S. L. <info@liberux.net>
|
||
|
||
use super::*;
|
||
use crate::render::Canvas;
|
||
|
||
fn solid_rgba( w: u32, h: u32 ) -> Arc<Vec<u8>>
|
||
{
|
||
Arc::new( vec![ 255u8; ( w * h * 4 ) as usize ] )
|
||
}
|
||
|
||
fn dummy_canvas() -> Canvas
|
||
{
|
||
Canvas::new( 800, 600 )
|
||
}
|
||
|
||
// ── builders / defaults ───────────────────────────────────────────────────
|
||
|
||
#[ test ]
|
||
fn new_uses_documented_defaults()
|
||
{
|
||
let img = Image::new( solid_rgba( 4, 4 ), 4, 4 );
|
||
assert_eq!( img.width, 4 );
|
||
assert_eq!( img.height, 4 );
|
||
assert!( !img.cover );
|
||
assert!( img.display_size.is_none() );
|
||
assert_eq!( img.opacity, 1.0 );
|
||
}
|
||
|
||
#[ test ]
|
||
fn cover_builder_sets_cover_flag()
|
||
{
|
||
let img = Image::new( solid_rgba( 4, 4 ), 4, 4 ).cover();
|
||
assert!( img.cover );
|
||
}
|
||
|
||
#[ test ]
|
||
fn size_builder_sets_explicit_display_size()
|
||
{
|
||
let img = Image::new( solid_rgba( 4, 4 ), 4, 4 ).size( 120.0, 80.0 );
|
||
assert_eq!( img.display_size, Some( ( Length::px( 120.0 ), Length::px( 80.0 ) ) ) );
|
||
}
|
||
|
||
#[ test ]
|
||
fn size_builder_keeps_length_values_for_later_viewport_resolution()
|
||
{
|
||
let img = Image::new( solid_rgba( 4, 4 ), 4, 4 ).size( -10.0, -20.0 );
|
||
assert_eq!( img.display_size, Some( ( Length::px( -10.0 ), Length::px( -20.0 ) ) ) );
|
||
}
|
||
|
||
#[ test ]
|
||
fn opacity_clamps_to_unit_interval()
|
||
{
|
||
assert_eq!( Image::new( solid_rgba( 1, 1 ), 1, 1 ).opacity( -0.5 ).opacity, 0.0 );
|
||
assert_eq!( Image::new( solid_rgba( 1, 1 ), 1, 1 ).opacity( 1.5 ).opacity, 1.0 );
|
||
assert_eq!( Image::new( solid_rgba( 1, 1 ), 1, 1 ).opacity( 0.5 ).opacity, 0.5 );
|
||
}
|
||
|
||
// ── preferred_size ────────────────────────────────────────────────────────
|
||
|
||
#[ test ]
|
||
fn preferred_size_default_scales_height_to_match_width_aspect()
|
||
{
|
||
// 200×100 source image at max_width = 400 → scale = 2 → height 200.
|
||
let img = Image::new( solid_rgba( 200, 100 ), 200, 100 );
|
||
let ( w, h ) = img.preferred_size( 400.0, &dummy_canvas() );
|
||
assert_eq!( w, 400.0 );
|
||
assert_eq!( h, 200.0 );
|
||
}
|
||
|
||
#[ test ]
|
||
fn preferred_size_with_explicit_size_wins_over_aspect_logic()
|
||
{
|
||
let img = Image::new( solid_rgba( 200, 100 ), 200, 100 ).size( 50.0, 25.0 );
|
||
assert_eq!( img.preferred_size( 1000.0, &dummy_canvas() ), ( 50.0, 25.0 ) );
|
||
}
|
||
|
||
#[ test ]
|
||
fn preferred_size_resolves_explicit_viewport_lengths()
|
||
{
|
||
let img = Image::new( solid_rgba( 200, 100 ), 200, 100 ).size( Length::vw( 10.0 ), Length::vh( 10.0 ) );
|
||
let canvas = Canvas::new( 480, 900 );
|
||
assert_eq!( img.preferred_size( 1000.0, &canvas ), ( 48.0, 90.0 ) );
|
||
}
|
||
|
||
#[ test ]
|
||
fn preferred_size_cover_mode_keeps_aspect_ratio()
|
||
{
|
||
// 100×50 source in cover mode at max_width = 300 → height = 300 * (50/100) = 150.
|
||
let img = Image::new( solid_rgba( 100, 50 ), 100, 50 ).cover();
|
||
let ( w, h ) = img.preferred_size( 300.0, &dummy_canvas() );
|
||
assert_eq!( w, 300.0 );
|
||
assert_eq!( h, 150.0 );
|
||
}
|
||
|
||
#[ test ]
|
||
fn preferred_size_cover_and_default_match_for_uniform_scaling()
|
||
{
|
||
// When the source aspect matches the requested width, cover and the
|
||
// default fit-width path produce identical sizes — they only diverge
|
||
// when the source is taller than wide.
|
||
let normal = Image::new( solid_rgba( 100, 50 ), 100, 50 );
|
||
let covered = Image::new( solid_rgba( 100, 50 ), 100, 50 ).cover();
|
||
let c = dummy_canvas();
|
||
assert_eq!( normal.preferred_size( 200.0, &c ), covered.preferred_size( 200.0, &c ) );
|
||
}
|
||
|
||
#[ test ]
|
||
fn short_side_sizes_by_width_in_portrait_keeping_aspect()
|
||
{
|
||
// 200×100 source, portrait 480×900 → short side is the width.
|
||
// orient( 40, 5 ) portrait → 40 % of 480 = 192 width, height 192*(100/200)=96.
|
||
let img = Image::new( solid_rgba( 200, 100 ), 200, 100 ).short_side( Length::orient( 40.0, 5.0 ) );
|
||
let canvas = Canvas::new( 480, 900 );
|
||
assert_eq!( img.preferred_size( 1000.0, &canvas ), ( 192.0, 96.0 ) );
|
||
}
|
||
|
||
#[ test ]
|
||
fn short_side_sizes_by_height_in_landscape_keeping_aspect()
|
||
{
|
||
// 200×100 source, landscape 900×480 → short side is the height.
|
||
// orient( 40, 5 ) landscape → 5 % of 480 = 24 height, width 24*(200/100)=48.
|
||
let img = Image::new( solid_rgba( 200, 100 ), 200, 100 ).short_side( Length::orient( 40.0, 5.0 ) );
|
||
let canvas = Canvas::new( 900, 480 );
|
||
assert_eq!( img.preferred_size( 1000.0, &canvas ), ( 48.0, 24.0 ) );
|
||
}
|
||
|
||
// ── Arc sharing ───────────────────────────────────────────────────────────
|
||
|
||
#[ test ]
|
||
fn rgba_buffer_is_shared_via_arc_not_cloned_per_image()
|
||
{
|
||
let bytes = solid_rgba( 8, 8 );
|
||
let strong_before = Arc::strong_count( &bytes );
|
||
|
||
let img = Image::new( bytes.clone(), 8, 8 );
|
||
assert_eq!( Arc::strong_count( &bytes ), strong_before + 1 );
|
||
|
||
// Same buffer goes into a second image — strong count rises again.
|
||
let img2 = Image::new( bytes.clone(), 8, 8 );
|
||
assert_eq!( Arc::strong_count( &bytes ), strong_before + 2 );
|
||
|
||
drop( img );
|
||
drop( img2 );
|
||
assert_eq!( Arc::strong_count( &bytes ), strong_before );
|
||
}
|