| 1 | use super::{GapConfig, Layout, WindowGeometry}; |
| 2 | use x11rb::protocol::xproto::Window; |
| 3 | |
| 4 | pub struct GridLayout; |
| 5 | |
| 6 | impl Layout for GridLayout { |
| 7 | fn name(&self) -> &'static str { |
| 8 | super::LayoutType::Grid.as_str() |
| 9 | } |
| 10 | |
| 11 | fn symbol(&self) -> &'static str { |
| 12 | "[#]" |
| 13 | } |
| 14 | |
| 15 | fn arrange( |
| 16 | &self, |
| 17 | windows: &[Window], |
| 18 | screen_width: u32, |
| 19 | screen_height: u32, |
| 20 | gaps: &GapConfig, |
| 21 | ) -> Vec<WindowGeometry> { |
| 22 | let window_count = windows.len(); |
| 23 | if window_count == 0 { |
| 24 | return Vec::new(); |
| 25 | } |
| 26 | |
| 27 | // Single window takes full screen |
| 28 | if window_count == 1 { |
| 29 | let x = gaps.outer_horizontal as i32; |
| 30 | let y = gaps.outer_vertical as i32; |
| 31 | let width = screen_width.saturating_sub(2 * gaps.outer_horizontal); |
| 32 | let height = screen_height.saturating_sub(2 * gaps.outer_vertical); |
| 33 | |
| 34 | return vec![WindowGeometry { |
| 35 | x_coordinate: x, |
| 36 | y_coordinate: y, |
| 37 | width, |
| 38 | height, |
| 39 | }]; |
| 40 | } |
| 41 | |
| 42 | // Calculate grid dimensions using "favor rows" approach |
| 43 | // cols = ceil(sqrt(n)) |
| 44 | // rows = ceil(n / cols) |
| 45 | let cols = (window_count as f64).sqrt().ceil() as usize; |
| 46 | let rows = (window_count as f64 / cols as f64).ceil() as usize; |
| 47 | |
| 48 | let mut geometries = Vec::new(); |
| 49 | |
| 50 | // Calculate dimensions with gaps |
| 51 | let total_horizontal_gaps = gaps.outer_horizontal * 2 + gaps.inner_horizontal * (cols as u32 - 1); |
| 52 | let total_vertical_gaps = gaps.outer_vertical * 2 + gaps.inner_vertical * (rows as u32 - 1); |
| 53 | |
| 54 | let cell_width = screen_width.saturating_sub(total_horizontal_gaps) / cols as u32; |
| 55 | let cell_height = screen_height.saturating_sub(total_vertical_gaps) / rows as u32; |
| 56 | |
| 57 | for (index, _window) in windows.iter().enumerate() { |
| 58 | let row = index / cols; |
| 59 | let col = index % cols; |
| 60 | |
| 61 | // Check if this is the last row |
| 62 | let is_last_row = row == rows - 1; |
| 63 | let windows_in_last_row = window_count - (rows - 1) * cols; |
| 64 | |
| 65 | let (x, y, width, height) = if is_last_row && windows_in_last_row < cols { |
| 66 | // Last row with fewer windows - make them wider |
| 67 | let last_row_col = index % cols; |
| 68 | let last_row_cell_width = screen_width.saturating_sub(total_horizontal_gaps.saturating_sub(gaps.inner_horizontal * (cols as u32 - windows_in_last_row as u32))) / windows_in_last_row as u32; |
| 69 | |
| 70 | let x = gaps.outer_horizontal + last_row_col as u32 * (last_row_cell_width + gaps.inner_horizontal); |
| 71 | let y = gaps.outer_vertical + row as u32 * (cell_height + gaps.inner_vertical); |
| 72 | |
| 73 | (x as i32, y as i32, last_row_cell_width, cell_height) |
| 74 | } else { |
| 75 | // Normal grid cell |
| 76 | let x = gaps.outer_horizontal + col as u32 * (cell_width + gaps.inner_horizontal); |
| 77 | let y = gaps.outer_vertical + row as u32 * (cell_height + gaps.inner_vertical); |
| 78 | |
| 79 | (x as i32, y as i32, cell_width, cell_height) |
| 80 | }; |
| 81 | |
| 82 | geometries.push(WindowGeometry { |
| 83 | x_coordinate: x, |
| 84 | y_coordinate: y, |
| 85 | width, |
| 86 | height, |
| 87 | }); |
| 88 | } |
| 89 | |
| 90 | geometries |
| 91 | } |
| 92 | } |