Files
ltk/tests/layout_stack_spacer.rs
T
admin 8882078743
CI / build + test (push) Canceled after 0s
CI / cargo audit (push) Canceled after 0s
A text field that serves as an editor, a file chooser, ltk::files with callbacks, Text::fit, App::window_title, and a stack that keeps its children inside it
A multiline text field was a box for a few lines: it wrapped its whole value by shaping each character alone on every frame, every click and every caret motion, so a file of some size made it crawl; it drew a tab as the font's glyph for it, had no scroll bar, no page keys and no way to number its lines, kept no record of the view it showed, so once the cursor had left the first screen it stayed on the last row and the arrows moved the text, and an app could neither colour its text, type a tab with Tab, give it a face of its own, ask it to copy, cut, paste or select, nor hear where its cursor was. Notes, the Eydos text editor, needs all of it, and every app that opens or saves the user's files needed a dialog of its own.
Speed. `Canvas::measure_char` measures a character's advance once per font and size and keeps it (`text_shaping::char_advance_cached`, emptied with the shaped lines), and `compute_visual_lines` keeps the last four layouts by the value's hash and length, the row width, the size and the face, so a value is wrapped again only when one of them changes, and drawing, hit testing and caret motion over the same value wrap it once.
The view. `TextEdit::draw` returns the view it showed, which the frame keeps in `text_scroll`, so the view moves only when the cursor leaves it, and by the least; a `TextCommand::Select` no longer resets it. `Window::shown` gives the lines as the last frame showed them, wherever the cursor went since, and a press, the wheel, the scroll bar and the page keys act on them: a press that gave a field the focus, which puts its cursor at the end, landed where the view would have gone to show that cursor, near the end of the text, instead of on the character pressed. The cursor a scroll keeps is taken back to the value's length as the view's is, so a scroll made with the cursor at its end sentinel holds instead of going back to the cursor at once.
Tabs. In a multiline field a tab reaches the next stop, every four digits' widths, as four monospace columns are (`theme::TAB_COLUMNS`, `text_edit/tabs.rs`), when it is drawn, selected, clicked and crossed with the arrows. `TextEdit::tab_text( text )` has Tab type `text` while the field has the focus instead of moving the focus on; Shift+Tab still moves it back and Ctrl+Tab still reaches the app.
The scroll bar. A field whose lines do not all fit shows a scroll bar along its right edge (`Window::scrollbar`): its thumb is held and dragged, or a press on its track brings the thumb there, by pointer or touch, without moving the cursor (`AppData::text_scrollbar_grab`, `handle_text_scrollbar_press`, `handle_text_scrollbar_drag`); the pointer over it is the arrow rather than the text cursor, and a long press that holds it no longer opens the Copy / Cut / Paste menu.
Page Up and Page Down move the cursor of a multiline field by the lines on screen but one, at the same height on screen, and the view with it, to the start or the end from the first or the last page (`cursor_visual_page`, `handle_cursor_page`); Shift extends the selection, and with Ctrl, or in any other widget, the keys still reach the app. A key without Ctrl that types no text, such as F3, was dropped while a text field had the focus; it now goes to `App::on_key_with_modifiers`, as it does with no field focused.
The text as an editor's. `TextEdit::highlights( Arc<Vec<TextHighlight>> )` draws runs of a multiline value, byte ranges in order, in colours of their own, what no run covers in the field's colour and a run off a character boundary left out. `TextEdit::font( face )` lays a multiline field's text out in a face of its own at the field's size: wrapping, drawing, the selection, the cursor, clicks, tab stops and the arrows all measure through one `TextFont`, the size and the face, which `text_input_geometry` now gives the runtime's paths where it gave them the size; `Canvas::measure_char_with_font` is `measure_char` in another face. `ltk::monospace_font()` finds a monospace face, DejaVu Sans Mono from the app's resources, which a page and an APK carry, else the first of the system's installed, loaded once. `TextEdit::line_numbers( true )` numbers the lines in a column at the field's left, in the monospace face when there is one, as wide as the number of the last line needs, a wrapped line numbered on its first row and the cursor's in the text colour while the field has the focus; the text starts right of the column alike when drawn and in every runtime path, since they all lay it out in the rect `gutter::text_rect` gives. `borderless()` now also drops a multiline field's background and border.
Talking to the field. `App::take_text_command` hands a field, by its `WidgetId`, a `TextCommand` after the next frame: `Copy`, `Cut`, `Paste` and `SelectAll` through the paths the keys take, and `Select { start, end }`, which focuses the field, selects the range with its ends taken back to the value and to its characters' starts, and scrolls to it. `Reveal { start, end }` does the same without moving the focus, as a search does while its own field keeps the keys, and a multiline field without the focus now shows its selection dimmed (`theme::selection_inactive`). `TextEdit::on_cursor( |at| … )` sends the cursor's byte offset once it is placed and whenever it moves or the value changes length (`report_text_cursors`, after every frame), as an editor's status bar showing the line needs.
A file chooser. `ltk::FileChooser` (`src/file_chooser/`) picks a file to open or a folder and a name to save one as, over `ltk::files`, alike natively and in a browser. The app holds it, puts its `view()` in its own and hands its `FileChooserMsg`s to `update()`, which returns `Chosen::Path` or `Chosen::Cancelled` once the user chose; `wrap` makes the app's message of the chooser's, so its listings reach it through the app's `ChannelSender`. It offers the home folder, the xdg-user-dirs folders that exist, the root and the folders mounted in a browser, with a press there to mount another; it lists folders first, the hidden entries left out, each with its type's icon, a file with its size and the date of its last access or change, sorted as Crate sorts, by name, last access or size, each key in a direction of its own (`Sort`, `SortKey`, `Directions`). `dir` starts elsewhere, `filter( &[ "text/*" ] )` shows only some types of files, `compact( true )` puts the places behind a button for a phone, `sort` starts in an order kept from `current_sort`, and `take_focus_request` gives the field to focus. Saving asks before replacing a file and can create a folder; its errors are in the user's words, and its strings in ltk's seven locales. `files::mounts()` gives the folders mounted in a browser as the desktop last said, which the web backend now keeps. `docs/widgets.md` describes it and `examples/file_chooser.rs` shows it, in `make examples`.
ltk::files::callback. The operations of `ltk::files` with a function in place of a message (`list`, `stat`, `read`, `read_range`, `write`, `create_dir`, `rename`, `remove`, `remove_all`, `copy`, `copy_all`, `mount` and `unmount`), for code with no run loop's `ChannelSender` to take one, such as Prompt's session daemon, which in a browser runs in the page beside the window. `done` is called on ltk's worker thread natively and on the page's thread in a browser.
Closing. An app that keeps its window open in `on_close_requested`, to ask whether to save, was not drawn again, so its question appeared only at the next event, such as a press. Every close request, from the compositor, the title bar's close button, back and the browser, now goes through `AppData::request_close`, which draws the view again when the app refused it.
`App::window_title`. The main window's title, read when the window is made, which the title bar, the task switcher and the dock show; by default the `app_id`. A `ShellMode::Window` app was titled "ltk" on Wayland, whatever the app, with no way to name it but the deprecated `window_config`; in a browser its title was its `app_id`, and is now this.
`Text::fit` and `Text::valign`. `fit()` draws a text at the largest size up to its own at which it fits the rect the layout gives it, on one line within its width and height or, with `wrap`, all its lines within its height, so a text whose length varies, such as a calculator's display, neither overflows a box the window sizes nor needs its size worked out from the window and its content; its preferred size is still the one at its own size. `valign( VAlign )` places the lines at the top, the centre or the bottom of the rect.
A stack keeps its children inside it. A child that `Stack` aligned to an edge or the centre took its preferred size even when that was larger than the stack, so one aligned to the bottom started above the stack's top and one aligned to the right left of its left edge, drawn over whatever was there. Each child's preferred size is now capped by the stack's, less its margin, before it is aligned; children placed with `push_placed` and the offsets of `push_translated` are as they were.
Tests cover Tab with and without `tab_text`, the text commands, `Reveal` leaving the focus alone, a highlight into a character, `on_cursor` reporting once, a refused close drawing again, a key that types nothing reaching the app from a field, tab stops, a click on a tab, the kept layouts, the scroll bar, a view kept while the cursor moves inside it and after a scroll with the cursor at its sentinel, a click landing on the view shown, the page keys, the width of the line numbers, the file chooser's listing, filters, names, user folders, sizes, sorting and its open, save and replace flows, `Text::fit` and a stack's aligned children. `CHANGELOG.md` and `docs/widgets.md` describe the changes.
2026-10-26 23:00:33 +01:00

281 lines
9.2 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Integration coverage for `Stack` alignment / margin / translation and for
// the way `Spacer` distributes leftover space inside a `Column`.
use ltk::core::Canvas;
use ltk::{ stack, spacer, HAlign, VAlign, Rect };
fn make_canvas() -> Canvas
{
Canvas::new( 800, 600 )
}
fn rect( x: f32, y: f32, w: f32, h: f32 ) -> Rect
{
Rect { x, y, width: w, height: h }
}
// ── Stack: alignment ──────────────────────────────────────────────────────────
#[ test ]
fn stack_fill_child_takes_full_rect()
{
let canvas = make_canvas();
let s = stack::<()>().push( spacer().width( 100.0 ).height( 100.0 ) );
let layout = s.layout( rect( 0.0, 0.0, 400.0, 300.0 ), &canvas );
assert_eq!( layout.len(), 1 );
let ( child_rect, _ ) = layout[ 0 ];
// Fill ignores the spacer's preferred size and stretches across the
// entire stack rect.
assert_eq!( child_rect, rect( 0.0, 0.0, 400.0, 300.0 ) );
}
#[ test ]
fn stack_center_centers_child_inside_rect()
{
let canvas = make_canvas();
let child = spacer().width( 100.0 ).height( 50.0 );
let s = stack::<()>().push_aligned( child, HAlign::Center, VAlign::Center );
let layout = s.layout( rect( 0.0, 0.0, 400.0, 300.0 ), &canvas );
let ( child_rect, _ ) = layout[ 0 ];
assert_eq!( child_rect.x, 150.0 );
assert_eq!( child_rect.y, 125.0 );
assert_eq!( child_rect.width, 100.0 );
assert_eq!( child_rect.height, 50.0 );
}
#[ test ]
fn stack_end_bottom_anchors_to_bottom_right()
{
let canvas = make_canvas();
let child = spacer().width( 80.0 ).height( 40.0 );
let s = stack::<()>().push_aligned( child, HAlign::End, VAlign::Bottom );
let layout = s.layout( rect( 0.0, 0.0, 400.0, 300.0 ), &canvas );
let ( child_rect, _ ) = layout[ 0 ];
assert_eq!( child_rect.x, 320.0 );
assert_eq!( child_rect.y, 260.0 );
}
#[ test ]
fn stack_start_top_anchors_to_top_left()
{
let canvas = make_canvas();
let child = spacer().width( 40.0 ).height( 30.0 );
let s = stack::<()>().push_aligned( child, HAlign::Start, VAlign::Top );
let layout = s.layout( rect( 100.0, 50.0, 400.0, 300.0 ), &canvas );
let ( child_rect, _ ) = layout[ 0 ];
assert_eq!( child_rect.x, 100.0 );
assert_eq!( child_rect.y, 50.0 );
}
#[ test ]
fn stack_child_larger_than_the_stack_stays_inside_it()
{
let canvas = make_canvas();
let child = spacer().width( 500.0 ).height( 500.0 );
let s = stack::<()>().push_aligned( child, HAlign::End, VAlign::Bottom );
let layout = s.layout( rect( 10.0, 20.0, 400.0, 300.0 ), &canvas );
let ( child_rect, _ ) = layout[ 0 ];
// Aligned to the bottom right, it would otherwise start 200 px above the stack and 100 px left of it.
assert_eq!( child_rect, rect( 10.0, 20.0, 400.0, 300.0 ) );
}
// ── Stack: margin ─────────────────────────────────────────────────────────────
#[ test ]
fn stack_margin_insets_fill_child_on_all_sides()
{
let canvas = make_canvas();
let child = spacer().width( 50.0 ).height( 50.0 );
let s = stack::<()>()
.push_aligned_margin( child, HAlign::Fill, VAlign::Fill, 12.0 );
let layout = s.layout( rect( 0.0, 0.0, 400.0, 300.0 ), &canvas );
let ( child_rect, _ ) = layout[ 0 ];
// Fill + margin = original rect inset on every side.
assert_eq!( child_rect, rect( 12.0, 12.0, 376.0, 276.0 ) );
}
#[ test ]
fn stack_margin_pushes_end_anchored_child_inward()
{
let canvas = make_canvas();
let child = spacer().width( 50.0 ).height( 50.0 );
let s = stack::<()>()
.push_aligned_margin( child, HAlign::End, VAlign::Top, 8.0 );
let layout = s.layout( rect( 0.0, 0.0, 400.0, 300.0 ), &canvas );
let ( child_rect, _ ) = layout[ 0 ];
// 400 - 50 - 8 = 342 from left edge; y starts at 8.
assert_eq!( child_rect.x, 342.0 );
assert_eq!( child_rect.y, 8.0 );
}
#[ test ]
fn stack_margin_larger_than_half_rect_clamps_inner_to_zero()
{
let canvas = make_canvas();
let child = spacer().width( 10.0 ).height( 10.0 );
let s = stack::<()>()
.push_aligned_margin( child, HAlign::Fill, VAlign::Fill, 200.0 );
// Stack rect is 100×100; margin 200 → inner is clamped to (0, 0).
let layout = s.layout( rect( 0.0, 0.0, 100.0, 100.0 ), &canvas );
let ( child_rect, _ ) = layout[ 0 ];
assert_eq!( child_rect.width, 0.0 );
assert_eq!( child_rect.height, 0.0 );
}
// ── Stack: translation ────────────────────────────────────────────────────────
#[ test ]
fn stack_translation_offsets_child_after_alignment()
{
let canvas = make_canvas();
let child = spacer().width( 40.0 ).height( 30.0 );
let s = stack::<()>()
.push_translated( child, HAlign::Start, VAlign::Top, 5.0, 7.0 );
let layout = s.layout( rect( 0.0, 0.0, 400.0, 300.0 ), &canvas );
let ( child_rect, _ ) = layout[ 0 ];
assert_eq!( child_rect.x, 5.0 );
assert_eq!( child_rect.y, 7.0 );
}
// ── Stack: order ─────────────────────────────────────────────────────────────
#[ test ]
fn stack_layout_preserves_child_order()
{
let canvas = make_canvas();
let s = stack::<()>()
.push( spacer().width( 10.0 ).height( 10.0 ) )
.push( spacer().width( 20.0 ).height( 20.0 ) )
.push( spacer().width( 30.0 ).height( 30.0 ) );
let layout = s.layout( rect( 0.0, 0.0, 400.0, 300.0 ), &canvas );
assert_eq!( layout.len(), 3 );
assert_eq!( layout[ 0 ].1, 0 );
assert_eq!( layout[ 1 ].1, 1 );
assert_eq!( layout[ 2 ].1, 2 );
}
// ── Stack: preferred_size ─────────────────────────────────────────────────────
#[ test ]
fn stack_preferred_size_returns_max_child_height()
{
let canvas = make_canvas();
let s = stack::<()>()
.push( spacer().width( 40.0 ).height( 30.0 ) )
.push( spacer().width( 60.0 ).height( 80.0 ) );
let ( w, h ) = s.preferred_size( 400.0, &canvas );
// Stack always reports `max_width` for width, and the tallest child for
// height — Stack is meant for overlaying, not flow.
assert_eq!( w, 400.0 );
assert_eq!( h, 80.0 );
}
#[ test ]
fn stack_empty_preferred_size_height_is_zero()
{
let canvas = make_canvas();
let s: ltk::Stack<()> = stack();
let ( _, h ) = s.preferred_size( 400.0, &canvas );
assert_eq!( h, 0.0 );
}
// ── Spacer: builder semantics ─────────────────────────────────────────────────
// Builder-field storage (weight / fixed dimensions) is covered by internal
// unit tests in `src/layout/spacer.rs`; here we exercise only the public
// observable — `preferred_size` — which reflects those fields.
#[ test ]
fn spacer_default_has_no_fixed_size()
{
let canvas = make_canvas();
assert_eq!( spacer().preferred_size( &canvas ), ( 0.0, 0.0 ) );
}
#[ test ]
fn spacer_height_pins_vertical_axis_only()
{
let canvas = make_canvas();
assert_eq!( spacer().height( 24.0 ).preferred_size( &canvas ), ( 0.0, 24.0 ) );
}
#[ test ]
fn spacer_width_pins_horizontal_axis_only()
{
let canvas = make_canvas();
assert_eq!( spacer().width( 12.0 ).preferred_size( &canvas ), ( 12.0, 0.0 ) );
}
// ── Spacer in Column: leftover distribution ───────────────────────────────────
// These use `column().layout(...)` directly (Column re-exports its `layout()`
// method as `pub`). Fixed-height children consume known space; flexible spacers
// must absorb the rest.
#[ test ]
fn flexible_spacer_in_column_consumes_leftover_height()
{
use ltk::column;
let canvas = make_canvas();
let col = column::<()>()
.padding( 0.0 )
.spacing( 0.0 )
.push( spacer().width( 50.0 ).height( 100.0 ) )
.push( spacer() ) // flexible
.push( spacer().width( 50.0 ).height( 100.0 ) );
let layout = col.layout( rect( 0.0, 0.0, 200.0, 500.0 ), &canvas );
assert_eq!( layout.len(), 3 );
// Two fixed children of 100 each + one spacer absorbing the leftover 300.
assert!( ( layout[ 0 ].0.height - 100.0 ).abs() < 1e-3 );
assert!( ( layout[ 1 ].0.height - 300.0 ).abs() < 1e-3 );
assert!( ( layout[ 2 ].0.height - 100.0 ).abs() < 1e-3 );
}
#[ test ]
fn weighted_spacers_split_leftover_proportionally()
{
use ltk::column;
let canvas = make_canvas();
let col = column::<()>()
.padding( 0.0 )
.spacing( 0.0 )
.push( spacer().weight( 1 ) )
.push( spacer().weight( 3 ) );
let layout = col.layout( rect( 0.0, 0.0, 200.0, 400.0 ), &canvas );
// Total weight 4; leftover 400; weight 1 → 100, weight 3 → 300.
assert!( ( layout[ 0 ].0.height - 100.0 ).abs() < 1e-3 );
assert!( ( layout[ 1 ].0.height - 300.0 ).abs() < 1e-3 );
}
#[ test ]
fn fixed_height_spacer_does_not_consume_leftover()
{
use ltk::column;
let canvas = make_canvas();
let col = column::<()>()
.padding( 0.0 )
.spacing( 0.0 )
.push( spacer().height( 50.0 ) ) // fixed — keeps its 50 px
.push( spacer() ); // flexible — absorbs the rest
let layout = col.layout( rect( 0.0, 0.0, 200.0, 500.0 ), &canvas );
assert!( ( layout[ 0 ].0.height - 50.0 ).abs() < 1e-3 );
assert!( ( layout[ 1 ].0.height - 450.0 ).abs() < 1e-3 );
}