mirror of
https://github.com/ratatui/ratatui.git
synced 2025-09-27 04:50:46 +00:00

This commit introduces new methods for the `Rect` struct that simplify the process of splitting a `Rect` into sub-rects according to a given `Layout`. By putting these methods on the `Rect` struct, we make it a bit more natural that a layout is applied to the `Rect` itself, rather than passing a `Rect` to the `Layout` struct to be split. Adds: - `Rect::layout` and `Rect::try_layout` methods that allow splitting a `Rect` into an array of sub-rects according to a given `Layout`. - `Rect::layout_vec` method that returns a `Vec` of sub-rects. - `Layout::try_areas` method that returns an array of sub-rects, with compile-time checks for the number of constraints. This is added mainly for consistency with the new `Rect` methods. ```rust use ratatui_core::layout::{Layout, Constraint, Rect}; let area = Rect::new(0, 0, 10, 10); let layout = Layout::vertical([Constraint::Fill(1); 2]); // Rect::layout() infers the number of constraints at compile time: let [top, main] = area.layout(&layout); // Rect::try_layout() and Layout::try_areas() do the same, but return a // Result: let [top, main] = area.try_layout(&layout)?; let [top, main] = layout.try_areas(area)?; // Rect::layout_vec() returns a Vec of sub-rects: let areas_vec = area.layout_vec(&layout); // you can also explicitly specify the number of constraints: let areas = area.layout::<2>(&layout); let areas = area.try_layout::<2>(&layout)?; let areas = layout.try_areas::<2>(area)?; ```
Input Form example
This example demonstrates how to handle input across several form fields (2 strings and an number). It uses an enum to track the focused field, and sends keyboard events to one which is current.
Run this example with:
cargo run -p input-form
This example does not handle things like cursor movement within the line (just keys and backspace). Most apps would benefit from using the following crates for text input rather than directly using strings:
Some more ideas for handling focus can be found in:
focusable
(see also Ratatui forum post)rat-focus
- A useful
Bevy
discussion about focus more generally.