Files
ltk/tests/tab_navigation.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

110 lines
3.7 KiB
Rust

#![ cfg( feature = "test-support" ) ]
mod common;
use common::{ lw_none, rect };
use ltk::test_support::next_focusable_index;
// ── Empty / single-element edge cases ─────────────────────────────────────────
#[ test ]
fn empty_widget_list_returns_none()
{
let widgets: Vec<_> = vec![];
assert_eq!( next_focusable_index::<()>( &widgets, None, false ), None );
assert_eq!( next_focusable_index::<()>( &widgets, Some( 42 ), false ), None );
assert_eq!( next_focusable_index::<()>( &widgets, None, true ), None );
}
#[ test ]
fn single_widget_wraps_to_itself()
{
let widgets = vec![ lw_none( 7, rect( 0.0, 0.0, 10.0, 10.0 ) ) ];
assert_eq!( next_focusable_index( &widgets, Some( 7 ), false ), Some( 7 ) );
assert_eq!( next_focusable_index( &widgets, Some( 7 ), true ), Some( 7 ) );
}
// ── Forward (Tab) ─────────────────────────────────────────────────────────────
#[ test ]
fn forward_advances_to_next_in_slice_order()
{
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 ) ),
];
assert_eq!( next_focusable_index( &widgets, Some( 10 ), false ), Some( 20 ) );
assert_eq!( next_focusable_index( &widgets, Some( 20 ), false ), Some( 30 ) );
}
#[ test ]
fn forward_wraps_around_at_end()
{
let widgets = vec![
lw_none( 1, rect( 0.0, 0.0, 10.0, 10.0 ) ),
lw_none( 2, rect( 0.0, 0.0, 10.0, 10.0 ) ),
];
assert_eq!( next_focusable_index( &widgets, Some( 2 ), false ), Some( 1 ) );
}
#[ test ]
fn forward_with_no_focus_targets_first_widget()
{
let widgets = vec![
lw_none( 100, rect( 0.0, 0.0, 10.0, 10.0 ) ),
lw_none( 200, rect( 0.0, 0.0, 10.0, 10.0 ) ),
];
assert_eq!( next_focusable_index( &widgets, None, false ), Some( 100 ) );
}
// ── Reverse (Shift+Tab) ───────────────────────────────────────────────────────
#[ test ]
fn reverse_steps_to_previous()
{
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 ) ),
];
assert_eq!( next_focusable_index( &widgets, Some( 30 ), true ), Some( 20 ) );
assert_eq!( next_focusable_index( &widgets, Some( 20 ), true ), Some( 10 ) );
}
#[ test ]
fn reverse_wraps_around_at_start()
{
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 ) ),
];
assert_eq!( next_focusable_index( &widgets, Some( 10 ), true ), Some( 20 ) );
}
#[ test ]
fn reverse_with_no_focus_targets_last_widget()
{
let widgets = vec![
lw_none( 100, rect( 0.0, 0.0, 10.0, 10.0 ) ),
lw_none( 200, rect( 0.0, 0.0, 10.0, 10.0 ) ),
];
assert_eq!( next_focusable_index( &widgets, None, true ), Some( 200 ) );
}
// ── Stale focus index ─────────────────────────────────────────────────────────
#[ test ]
fn stale_focus_idx_falls_back_to_first()
{
// Common after a re-layout removes the previously-focused widget: the
// stored focus idx no longer maps to any entry, and the walker falls
// back to the same behaviour as `None`.
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 ) ),
];
assert_eq!( next_focusable_index( &widgets, Some( 999 ), false ), Some( 10 ) );
assert_eq!( next_focusable_index( &widgets, Some( 999 ), true ), Some( 20 ) );
}