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)?; ```
Flex demo
This interactive example shows how to use the flex layouts.
To run this demo:
cargo run -p flex