Files
ltk/tests/tree_lookup.rs
Pedro M. de Echanove Pasquin ce893ac776
Some checks are pending
CI / build + test (push) Waiting to run
CI / cargo audit (push) Waiting to run
responsive fluid/physical scaling, widget-API stabilization, and perf guardrails
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`.
2026-07-07 17:40:33 +02:00

98 lines
3.0 KiB
Rust

#![ cfg( feature = "test-support" ) ]
mod common;
use common::{ lw_none, lw_button, rect };
use ltk::test_support::{ find_widget_at, find_widget, find_handlers, WidgetHandlers };
use ltk::Point;
fn pt( x: f32, y: f32 ) -> Point
{
Point { x, y }
}
// ── find_widget_at ────────────────────────────────────────────────────────────
#[ test ]
fn hit_test_misses_when_empty()
{
let widgets: Vec<_> = vec![];
assert_eq!( find_widget_at::<()>( &widgets, pt( 10.0, 10.0 ) ), None );
}
#[ test ]
fn hit_test_misses_outside_all_rects()
{
let widgets = vec![
lw_none( 1, rect( 0.0, 0.0, 50.0, 50.0 ) ),
lw_none( 2, rect( 100.0, 100.0, 50.0, 50.0 ) ),
];
assert_eq!( find_widget_at( &widgets, pt( 75.0, 75.0 ) ), None );
}
#[ test ]
fn hit_test_returns_widget_under_point()
{
let widgets = vec![
lw_none( 7, rect( 0.0, 0.0, 100.0, 100.0 ) ),
lw_none( 8, rect( 200.0, 0.0, 100.0, 100.0 ) ),
];
assert_eq!( find_widget_at( &widgets, pt( 50.0, 50.0 ) ), Some( 7 ) );
assert_eq!( find_widget_at( &widgets, pt( 250.0, 50.0 ) ), Some( 8 ) );
}
#[ test ]
fn hit_test_returns_topmost_when_overlapping()
{
// Topmost wins. Layout pushes parents before children, so iteration in
// reverse must return the child (drawn last, last in the slice).
let widgets = vec![
lw_none( 1, rect( 0.0, 0.0, 200.0, 200.0 ) ),
lw_none( 2, rect( 50.0, 50.0, 100.0, 100.0 ) ),
];
assert_eq!( find_widget_at( &widgets, pt( 100.0, 100.0 ) ), Some( 2 ) );
assert_eq!( find_widget_at( &widgets, pt( 10.0, 10.0 ) ), Some( 1 ) );
}
// ── find_widget ───────────────────────────────────────────────────────────────
#[ test ]
fn find_widget_returns_match_by_flat_idx()
{
let widgets = vec![
lw_none( 10, rect( 0.0, 0.0, 10.0, 10.0 ) ),
lw_none( 20, rect( 0.0, 0.0, 10.0, 10.0 ) ),
lw_none( 30, rect( 0.0, 0.0, 10.0, 10.0 ) ),
];
let w = find_widget( &widgets, 20 ).expect( "widget 20 should be found" );
assert_eq!( w.flat_idx, 20 );
}
#[ test ]
fn find_widget_returns_none_for_missing_idx()
{
let widgets = vec![ lw_none( 1, rect( 0.0, 0.0, 10.0, 10.0 ) ) ];
assert!( find_widget( &widgets, 999 ).is_none() );
}
// ── find_handlers ─────────────────────────────────────────────────────────────
#[ test ]
fn find_handlers_returns_button_handler()
{
let widgets = vec![
lw_button( 5, rect( 0.0, 0.0, 10.0, 10.0 ) ),
];
let h = find_handlers( &widgets, 5 ).expect( "handler should be present" );
assert!( matches!( h, WidgetHandlers::Button { on_press: Some( () ), .. } ) );
}
#[ test ]
fn find_handlers_returns_none_for_missing_idx()
{
let widgets = vec![
lw_button( 5, rect( 0.0, 0.0, 10.0, 10.0 ) ),
];
assert!( find_handlers( &widgets, 6 ).is_none() );
}