tony / oxwm

Branch: 16b90b9
Switch branches/tags
master
tonybtw Added smartgaps, mfact, and nmaster (inc master, dec master) functionality from dwm. Default to attach-aside patch. 16b90b9 2025-12-01
91 lines · 3.1 KB Raw Blame History
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 _master_factor: f32,
22 _num_master: i32,
23 ) -> Vec<WindowGeometry> {
24 let window_count = windows.len();
25 if window_count == 0 {
26 return Vec::new();
27 }
28
29 if window_count == 1 {
30 let x = gaps.outer_horizontal as i32;
31 let y = gaps.outer_vertical as i32;
32 let width = screen_width.saturating_sub(2 * gaps.outer_horizontal);
33 let height = screen_height.saturating_sub(2 * gaps.outer_vertical);
34
35 return vec![WindowGeometry {
36 x_coordinate: x,
37 y_coordinate: y,
38 width,
39 height,
40 }];
41 }
42
43 let cols = (window_count as f64).sqrt().ceil() as usize;
44 let rows = (window_count as f64 / cols as f64).ceil() as usize;
45
46 let mut geometries = Vec::new();
47
48 let total_horizontal_gaps =
49 gaps.outer_horizontal * 2 + gaps.inner_horizontal * (cols as u32 - 1);
50 let total_vertical_gaps = gaps.outer_vertical * 2 + gaps.inner_vertical * (rows as u32 - 1);
51
52 let cell_width = screen_width.saturating_sub(total_horizontal_gaps) / cols as u32;
53 let cell_height = screen_height.saturating_sub(total_vertical_gaps) / rows as u32;
54
55 for (index, _window) in windows.iter().enumerate() {
56 let row = index / cols;
57 let col = index % cols;
58
59 let is_last_row = row == rows - 1;
60 let windows_in_last_row = window_count - (rows - 1) * cols;
61
62 let (x, y, width, height) = if is_last_row && windows_in_last_row < cols {
63 let last_row_col = index % cols;
64 let last_row_cell_width =
65 screen_width.saturating_sub(total_horizontal_gaps.saturating_sub(
66 gaps.inner_horizontal * (cols as u32 - windows_in_last_row as u32),
67 )) / windows_in_last_row as u32;
68
69 let x = gaps.outer_horizontal
70 + 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 let x = gaps.outer_horizontal + col as u32 * (cell_width + gaps.inner_horizontal);
76 let y = gaps.outer_vertical + row as u32 * (cell_height + gaps.inner_vertical);
77
78 (x as i32, y as i32, cell_width, cell_height)
79 };
80
81 geometries.push(WindowGeometry {
82 x_coordinate: x,
83 y_coordinate: y,
84 width,
85 height,
86 });
87 }
88
89 geometries
90 }
91 }