rat_widget/layout/layout_grid.rs
1use crate::layout::GenericLayout;
2use ratatui::layout::{Constraint, Layout, Rect};
3
4///
5/// Calculates a full grid of rects from the horizontal and vertical components.
6///
7/// ```
8/// use ratatui::layout::{Constraint, Layout, Rect};
9/// use rat_widget::layout::layout_grid;
10///
11/// let area = Rect::new(0,0,100,100);
12///
13/// let layout = layout_grid::<3, 5>(area,
14/// Layout::horizontal([
15/// Constraint::Length(5),
16/// Constraint::Fill(1),
17/// Constraint::Length(5)
18/// ]),
19/// Layout::vertical([
20/// Constraint::Length(1),
21/// Constraint::Length(3),
22/// Constraint::Fill(1),
23/// Constraint::Length(3),
24/// Constraint::Length(1),
25/// ])
26/// );
27///
28/// // middle column, second block
29/// let a_1_2 = layout.widget(layout.try_index_of((1,2)).expect("fine"));
30/// ```
31#[allow(clippy::needless_range_loop)]
32#[deprecated(since = "1.2.0", note = "use layout_as_grid() instead")]
33pub fn layout_grid<const X: usize, const Y: usize>(
34 area: Rect,
35 horizontal: Layout,
36 vertical: Layout,
37) -> GenericLayout<(usize, usize)> {
38 let mut gen_layout = GenericLayout::new();
39 gen_layout.set_area(area);
40 gen_layout.set_page_size(area.as_size());
41
42 let hori = horizontal.areas::<X>(Rect::new(area.x, 0, area.width, 0));
43 let vert = vertical.areas::<Y>(Rect::new(0, area.y, 0, area.height));
44
45 for x in 0..X {
46 for y in 0..Y {
47 let grid_area = Rect::new(hori[x].x, vert[y].y, hori[x].width, vert[y].height);
48 gen_layout.add((x, y), grid_area, None, Rect::default());
49 }
50 }
51
52 gen_layout
53}
54
55///
56/// Calculates a full grid of rects from the horizontal and vertical components.
57///
58/// ```
59/// use ratatui::layout::{Constraint, Layout, Rect};
60/// use rat_widget::layout::layout_as_grid;
61///
62/// let area = Rect::new(0,0,100,100);
63///
64/// let layout = layout_as_grid(area,
65/// Layout::horizontal([
66/// Constraint::Length(5),
67/// Constraint::Fill(1),
68/// Constraint::Length(5)
69/// ]),
70/// Layout::vertical([
71/// Constraint::Length(1),
72/// Constraint::Length(3),
73/// Constraint::Fill(1),
74/// Constraint::Length(3),
75/// Constraint::Length(1),
76/// ])
77/// );
78///
79/// // middle column, second block
80/// let a_1_2 = layout.widget(layout.try_index_of((1,2)).expect("fine"));
81/// ```
82pub fn layout_as_grid(
83 area: Rect,
84 horizontal: Layout,
85 vertical: Layout,
86) -> GenericLayout<(usize, usize)> {
87 let mut gen_layout = GenericLayout::new();
88 gen_layout.set_area(area);
89 gen_layout.set_page_size(area.as_size());
90
91 let hori = horizontal.split(Rect::new(area.x, 0, area.width, 0));
92 let vert = vertical.split(Rect::new(0, area.y, 0, area.height));
93
94 for x in 0..hori.len() {
95 for y in 0..vert.len() {
96 let grid_area = Rect::new(hori[x].x, vert[y].y, hori[x].width, vert[y].height);
97 gen_layout.add((x, y), grid_area, None, Rect::default());
98 }
99 }
100
101 gen_layout
102}
103
104/// Create a basic grid of areas using the given Constraints.
105pub fn simple_grid<const X: usize, const Y: usize>(
106 area: Rect,
107 horizontal: [Constraint; X],
108 vertical: [Constraint; Y],
109) -> [[Rect; Y]; X] {
110 let mut layout = [[Rect::default(); Y]; X];
111
112 let hori = Layout::horizontal(horizontal).split(Rect::new(area.x, 0, area.width, 0));
113 let vert = Layout::vertical(vertical).split(Rect::new(0, area.y, 0, area.height));
114
115 for x in 0..X {
116 for y in 0..Y {
117 let grid_area = Rect::new(hori[x].x, vert[y].y, hori[x].width, vert[y].height);
118 layout[x][y] = grid_area;
119 }
120 }
121
122 layout
123}