1use super::PanelRect;
6
7pub const PANEL_GAP: f32 = 0.0;
9
10#[derive(Clone, Copy, Debug, PartialEq, Default)]
14pub enum SplitterPolicy {
15 #[default]
22 Cascade,
23 RejectSnapBack,
30 Clamp {
37 min_frac: f64,
40 },
41}
42
43#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
50pub enum SplitKind {
51 SplitRight,
53 SplitBottom,
55 Grid2x2,
57 OneLeftTwoRight,
59 TwoLeftOneRight,
61 OneTopTwoBottom,
63 TwoTopOneBottom,
65 ThreeColumns,
67 ThreeRows,
69 OneBig3Small,
71}
72
73impl SplitKind {
74 #[deprecated(note = "use SplitKind::SplitRight — clearer about new panel placement")]
76 #[allow(non_upper_case_globals)]
77 pub const Horizontal: SplitKind = SplitKind::SplitRight;
78 #[deprecated(note = "use SplitKind::SplitBottom — clearer about new panel placement")]
80 #[allow(non_upper_case_globals)]
81 pub const Vertical: SplitKind = SplitKind::SplitBottom;
82}
83
84#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
86pub enum WindowLayout {
87 #[default]
89 Single,
90 SplitHorizontal,
92 SplitVertical,
94 Grid2x2,
96 TwoLeftOneRight,
98 OneLeftTwoRight,
100 TwoTopOneBottom,
102 OneTopTwoBottom,
104 ThreeColumns,
106 ThreeRows,
108 Custom,
110}
111
112impl WindowLayout {
113 pub fn panel_count(&self) -> usize {
115 match self {
116 WindowLayout::Single => 1,
117 WindowLayout::SplitHorizontal | WindowLayout::SplitVertical => 2,
118 WindowLayout::Grid2x2 => 4,
119 WindowLayout::TwoLeftOneRight | WindowLayout::OneLeftTwoRight |
120 WindowLayout::TwoTopOneBottom | WindowLayout::OneTopTwoBottom |
121 WindowLayout::ThreeColumns | WindowLayout::ThreeRows => 3,
122 WindowLayout::Custom => usize::MAX, }
124 }
125
126 pub fn calculate_rects(&self, total_width: f32, total_height: f32, panel_count: usize) -> Vec<PanelRect> {
128 self.calculate_rects_with_gap(total_width, total_height, panel_count, PANEL_GAP)
129 }
130
131 pub fn calculate_rects_with_gap(&self, total_width: f32, total_height: f32, panel_count: usize, gap: f32) -> Vec<PanelRect> {
133 let mut rects = Vec::new();
134
135 match self {
136 WindowLayout::Single => {
137 rects.push(PanelRect::new(0.0, 0.0, total_width, total_height));
138 }
139 WindowLayout::SplitHorizontal => {
140 let half = (total_width - gap) / 2.0;
141 rects.push(PanelRect::new(0.0, 0.0, half, total_height));
142 if panel_count > 1 {
143 rects.push(PanelRect::new(half + gap, 0.0, half, total_height));
144 }
145 }
146 WindowLayout::SplitVertical => {
147 let half = (total_height - gap) / 2.0;
148 rects.push(PanelRect::new(0.0, 0.0, total_width, half));
149 if panel_count > 1 {
150 rects.push(PanelRect::new(0.0, half + gap, total_width, half));
151 }
152 }
153 WindowLayout::Grid2x2 => {
154 let half_w = (total_width - gap) / 2.0;
155 let half_h = (total_height - gap) / 2.0;
156 rects.push(PanelRect::new(0.0, 0.0, half_w, half_h));
157 if panel_count > 1 { rects.push(PanelRect::new(half_w + gap, 0.0, half_w, half_h)); }
158 if panel_count > 2 { rects.push(PanelRect::new(0.0, half_h + gap, half_w, half_h)); }
159 if panel_count > 3 { rects.push(PanelRect::new(half_w + gap, half_h + gap, half_w, half_h)); }
160 }
161 WindowLayout::TwoLeftOneRight => {
162 let left_w = (total_width - gap) * 0.4;
163 let right_w = (total_width - gap) * 0.6;
164 let half_h = (total_height - gap) / 2.0;
165 rects.push(PanelRect::new(0.0, 0.0, left_w, half_h));
166 if panel_count > 1 { rects.push(PanelRect::new(0.0, half_h + gap, left_w, half_h)); }
167 if panel_count > 2 { rects.push(PanelRect::new(left_w + gap, 0.0, right_w, total_height)); }
168 }
169 WindowLayout::OneLeftTwoRight => {
170 let left_w = (total_width - gap) * 0.6;
171 let right_w = (total_width - gap) * 0.4;
172 let half_h = (total_height - gap) / 2.0;
173 rects.push(PanelRect::new(0.0, 0.0, left_w, total_height));
174 if panel_count > 1 { rects.push(PanelRect::new(left_w + gap, 0.0, right_w, half_h)); }
175 if panel_count > 2 { rects.push(PanelRect::new(left_w + gap, half_h + gap, right_w, half_h)); }
176 }
177 WindowLayout::TwoTopOneBottom => {
178 let top_h = (total_height - gap) * 0.4;
179 let bottom_h = (total_height - gap) * 0.6;
180 let half_w = (total_width - gap) / 2.0;
181 rects.push(PanelRect::new(0.0, 0.0, half_w, top_h));
182 if panel_count > 1 { rects.push(PanelRect::new(half_w + gap, 0.0, half_w, top_h)); }
183 if panel_count > 2 { rects.push(PanelRect::new(0.0, top_h + gap, total_width, bottom_h)); }
184 }
185 WindowLayout::OneTopTwoBottom => {
186 let top_h = (total_height - gap) * 0.6;
187 let bottom_h = (total_height - gap) * 0.4;
188 let half_w = (total_width - gap) / 2.0;
189 rects.push(PanelRect::new(0.0, 0.0, total_width, top_h));
190 if panel_count > 1 { rects.push(PanelRect::new(0.0, top_h + gap, half_w, bottom_h)); }
191 if panel_count > 2 { rects.push(PanelRect::new(half_w + gap, top_h + gap, half_w, bottom_h)); }
192 }
193 WindowLayout::ThreeColumns => {
194 let col_w = (total_width - gap * 2.0) / 3.0;
195 rects.push(PanelRect::new(0.0, 0.0, col_w, total_height));
196 if panel_count > 1 { rects.push(PanelRect::new(col_w + gap, 0.0, col_w, total_height)); }
197 if panel_count > 2 { rects.push(PanelRect::new(col_w * 2.0 + gap * 2.0, 0.0, col_w, total_height)); }
198 }
199 WindowLayout::ThreeRows => {
200 let row_h = (total_height - gap * 2.0) / 3.0;
201 rects.push(PanelRect::new(0.0, 0.0, total_width, row_h));
202 if panel_count > 1 { rects.push(PanelRect::new(0.0, row_h + gap, total_width, row_h)); }
203 if panel_count > 2 { rects.push(PanelRect::new(0.0, row_h * 2.0 + gap * 2.0, total_width, row_h)); }
204 }
205 WindowLayout::Custom => {
206 if panel_count == 0 {
208 return rects;
209 }
210 let cols = (panel_count as f32).sqrt().ceil() as usize;
211 let rows = panel_count.div_ceil(cols); let cell_w = (total_width - gap * (cols as f32 - 1.0).max(0.0)) / cols as f32;
213 let cell_h = (total_height - gap * (rows as f32 - 1.0).max(0.0)) / rows as f32;
214
215 if cell_w <= 0.0 || cell_h <= 0.0 {
217 rects.push(PanelRect::new(0.0, 0.0, total_width, total_height));
219 return rects;
220 }
221
222 for i in 0..panel_count {
223 let col = i % cols;
224 let row = i / cols;
225 let x = col as f32 * (cell_w + gap);
226 let y = row as f32 * (cell_h + gap);
227 rects.push(PanelRect::new(x, y, cell_w, cell_h));
228 }
229 }
230 }
231
232 rects
233 }
234}
235
236#[derive(Clone, Debug, PartialEq)]
249pub struct GridSpec {
250 pub rows: usize,
252 pub cols: usize,
254 pub row_ratios: Vec<f64>,
256 pub col_ratios: Vec<f64>,
258}
259
260impl GridSpec {
261 pub fn new(rows: usize, cols: usize) -> Option<Self> {
263 if rows == 0 || cols == 0 {
264 return None;
265 }
266 Some(Self {
267 rows,
268 cols,
269 row_ratios: vec![1.0 / rows as f64; rows],
270 col_ratios: vec![1.0 / cols as f64; cols],
271 })
272 }
273
274 pub fn with_ratios(row_ratios: Vec<f64>, col_ratios: Vec<f64>) -> Option<Self> {
278 if !Self::valid_block(&row_ratios) || !Self::valid_block(&col_ratios) {
279 return None;
280 }
281 Some(Self {
282 rows: row_ratios.len(),
283 cols: col_ratios.len(),
284 row_ratios,
285 col_ratios,
286 })
287 }
288
289 pub(crate) fn valid_block(ratios: &[f64]) -> bool {
291 !ratios.is_empty() && ratios.iter().all(|r| r.is_finite() && *r > 0.0)
292 }
293
294 pub fn cell_count(&self) -> usize {
296 self.rows * self.cols
297 }
298
299 pub fn cell_index(&self, row: usize, col: usize) -> usize {
301 row * self.cols + col
302 }
303
304 pub fn normalized_rows(&self) -> Vec<f64> {
307 Self::normalize(&self.row_ratios, self.rows)
308 }
309
310 pub fn normalized_cols(&self) -> Vec<f64> {
313 Self::normalize(&self.col_ratios, self.cols)
314 }
315
316 fn normalize(ratios: &[f64], n: usize) -> Vec<f64> {
317 if n == 0 {
318 return Vec::new();
319 }
320 if ratios.len() != n || !Self::valid_block(ratios) {
321 return vec![1.0 / n as f64; n];
322 }
323 let sum: f64 = ratios.iter().sum();
324 ratios.iter().map(|r| r / sum).collect()
325 }
326
327 pub fn cell_rects(&self, area: PanelRect, gap: f32) -> Vec<PanelRect> {
330 let (xs, ws) = Self::tracks(&self.normalized_cols(), area.x, area.width, gap);
331 let (ys, hs) = Self::tracks(&self.normalized_rows(), area.y, area.height, gap);
332 let mut out = Vec::with_capacity(self.cell_count());
333 for r in 0..self.rows {
334 for c in 0..self.cols {
335 out.push(PanelRect::new(xs[c], ys[r], ws[c], hs[r]));
336 }
337 }
338 out
339 }
340
341 pub(crate) fn tracks(fracs: &[f64], start: f32, extent: f32, gap: f32) -> (Vec<f32>, Vec<f32>) {
345 let n = fracs.len();
346 let available = (extent - gap * n.saturating_sub(1) as f32).max(0.0) as f64;
347 let mut starts = Vec::with_capacity(n);
348 let mut sizes = Vec::with_capacity(n);
349 let mut acc = 0.0_f64;
350 for (i, f) in fracs.iter().enumerate() {
351 let a = start + (acc * available) as f32 + gap * i as f32;
352 acc += f;
353 let b = if i + 1 == n {
354 start + extent
355 } else {
356 start + (acc * available) as f32 + gap * i as f32
357 };
358 starts.push(a);
359 sizes.push((b - a).max(0.0));
360 }
361 (starts, sizes)
362 }
363}