tony / oxwm

added grid layout and positional movements. turns out grid layout was easy to add thanks to rusts based idiommatic enum. and positional movements is a solved problem (peeked leftwm for source) Browse files
tonybtw committed 2025-11-07 parent c9efeb4 commit 026dcd91d48373a8d61bc08b752fcf59f06a4bbd
Showing 6 changed files with 271 additions and 25 deletions. UnifiedSplit
+10 −11 resources/test-config.ron
@@ -55,17 +55,16 @@
55 55 (modifiers: [Mod1], key: A, action: ToggleGaps),
56 56 (modifiers: [Mod1, Shift], key: Q, action: Quit),
57 57 (modifiers: [Mod1, Shift], key: R, action: Restart),
58 - (modifiers: [Mod1], key: J, action: FocusStack, arg: -1),
59 - (modifiers: [Mod1], key: K, action: FocusStack, arg: 1),
60 - // Exchange client (vim-style with Mod+Shift)
61 - (modifiers: [Mod1, Control], key: K, action: SmartMoveWin, arg: 0), // UP
62 - (modifiers: [Mod1, Control], key: J, action: SmartMoveWin, arg: 1), // DOWN
63 - (modifiers: [Mod1, Control], key: H, action: SmartMoveWin, arg: 2), // LEFT
64 - (modifiers: [Mod1, Control], key: L, action: SmartMoveWin, arg: 3), // RIGHT
65 - (modifiers: [Mod1, Shift], key: K, action: ExchangeClient, arg: 0), // UP
66 - (modifiers: [Mod1, Shift], key: J, action: ExchangeClient, arg: 1), // DOWN
67 - (modifiers: [Mod1, Shift], key: H, action: ExchangeClient, arg: 2), // LEFT
68 - (modifiers: [Mod1, Shift], key: L, action: ExchangeClient, arg: 3), // RIGHT
58 + // (modifiers: [Mod1], key: J, action: FocusStack, arg: -1),
59 + // (modifiers: [Mod1], key: K, action: FocusStack, arg: 1),
60 + (modifiers: [Mod1], key: H, action: FocusDirection, arg: 2),
61 + (modifiers: [Mod1], key: J, action: FocusDirection, arg: 1),
62 + (modifiers: [Mod1], key: K, action: FocusDirection, arg: 0),
63 + (modifiers: [Mod1], key: L, action: FocusDirection, arg: 3),
64 + (modifiers: [Mod1, Shift], key: H, action: SwapDirection, arg: 2),
65 + (modifiers: [Mod1, Shift], key: J, action: SwapDirection, arg: 1),
66 + (modifiers: [Mod1, Shift], key: K, action: SwapDirection, arg: 0),
67 + (modifiers: [Mod1, Shift], key: L, action: SwapDirection, arg: 3),
69 68 (modifiers: [Mod1], key: Key1, action: ViewTag, arg: 0),
70 69 (modifiers: [Mod1], key: Key2, action: ViewTag, arg: 1),
71 70 (modifiers: [Mod1], key: Key3, action: ViewTag, arg: 2),
+2 −0 src/keyboard/handlers.rs
@@ -14,6 +14,8 @@ pub enum KeyAction {
14 14 Spawn,
15 15 KillClient,
16 16 FocusStack,
17 + FocusDirection,
18 + SwapDirection,
17 19 Quit,
18 20 Restart,
19 21 Recompile,
+92 −0 src/layout/grid.rs new file mode 100644
@@ -0,0 +1,92 @@
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 +}
+7 −1 src/layout/mod.rs
@@ -1,3 +1,4 @@
1 +pub mod grid;
1 2 pub mod normie;
2 3 pub mod tiling;
3 4
@@ -15,6 +16,7 @@ pub struct GapConfig {
15 16 pub enum LayoutType {
16 17 Tiling,
17 18 Normie,
19 + Grid,
18 20 }
19 21
20 22 impl LayoutType {
@@ -22,13 +24,15 @@ impl LayoutType {
22 24 match self {
23 25 Self::Tiling => Box::new(tiling::TilingLayout),
24 26 Self::Normie => Box::new(normie::NormieLayout),
27 + Self::Grid => Box::new(grid::GridLayout),
25 28 }
26 29 }
27 30
28 31 pub fn next(&self) -> Self {
29 32 match self {
30 33 Self::Tiling => Self::Normie,
31 - Self::Normie => Self::Tiling,
34 + Self::Normie => Self::Grid,
35 + Self::Grid => Self::Tiling,
32 36 }
33 37 }
34 38
@@ -36,6 +40,7 @@ impl LayoutType {
36 40 match self {
37 41 Self::Tiling => "tiling",
38 42 Self::Normie => "normie",
43 + Self::Grid => "grid",
39 44 }
40 45 }
41 46
@@ -43,6 +48,7 @@ impl LayoutType {
43 48 match s.to_lowercase().as_str() {
44 49 "tiling" => Ok(Self::Tiling),
45 50 "normie" | "floating" => Ok(Self::Normie),
51 + "grid" => Ok(Self::Grid),
46 52 _ => Err(format!("Invalid Layout Type: {}", s)),
47 53 }
48 54 }
+152 −0 src/window_manager.rs
@@ -977,6 +977,16 @@ impl WindowManager {
977 977 self.cycle_focus(*direction)?;
978 978 }
979 979 }
980 + KeyAction::FocusDirection => {
981 + if let Arg::Int(direction) = arg {
982 + self.focus_direction(*direction)?;
983 + }
984 + }
985 + KeyAction::SwapDirection => {
986 + if let Arg::Int(direction) = arg {
987 + self.swap_direction(*direction)?;
988 + }
989 + }
980 990 KeyAction::Quit | KeyAction::Restart => {
981 991 // Handled in handle_event
982 992 }
@@ -1205,6 +1215,148 @@ impl WindowManager {
1205 1215 Ok(())
1206 1216 }
1207 1217
1218 + pub fn focus_direction(&mut self, direction: i32) -> WmResult<()> {
1219 + let focused = match self
1220 + .monitors
1221 + .get(self.selected_monitor)
1222 + .and_then(|m| m.focused_window)
1223 + {
1224 + Some(win) => win,
1225 + None => return Ok(()),
1226 + };
1227 +
1228 + let visible = self.visible_windows();
1229 + if visible.len() < 2 {
1230 + return Ok(());
1231 + }
1232 +
1233 + let focused_geom = match self.connection.get_geometry(focused)?.reply() {
1234 + Ok(geom) => geom,
1235 + Err(_) => return Ok(()),
1236 + };
1237 +
1238 + let focused_center_x = focused_geom.x + (focused_geom.width as i16 / 2);
1239 + let focused_center_y = focused_geom.y + (focused_geom.height as i16 / 2);
1240 +
1241 + let mut candidates = Vec::new();
1242 +
1243 + for &window in &visible {
1244 + if window == focused {
1245 + continue;
1246 + }
1247 +
1248 + let geom = match self.connection.get_geometry(window)?.reply() {
1249 + Ok(g) => g,
1250 + Err(_) => continue,
1251 + };
1252 +
1253 + let center_x = geom.x + (geom.width as i16 / 2);
1254 + let center_y = geom.y + (geom.height as i16 / 2);
1255 +
1256 + let is_valid_direction = match direction {
1257 + 0 => center_y < focused_center_y,
1258 + 1 => center_y > focused_center_y,
1259 + 2 => center_x < focused_center_x,
1260 + 3 => center_x > focused_center_x,
1261 + _ => false,
1262 + };
1263 +
1264 + if is_valid_direction {
1265 + let dx = (center_x - focused_center_x) as i32;
1266 + let dy = (center_y - focused_center_y) as i32;
1267 + let distance = dx * dx + dy * dy;
1268 + candidates.push((window, distance));
1269 + }
1270 + }
1271 +
1272 + if let Some(&(closest_window, _)) = candidates.iter().min_by_key(|&(_, dist)| dist) {
1273 + self.set_focus(closest_window)?;
1274 + }
1275 +
1276 + Ok(())
1277 + }
1278 +
1279 + pub fn swap_direction(&mut self, direction: i32) -> WmResult<()> {
1280 + let focused = match self
1281 + .monitors
1282 + .get(self.selected_monitor)
1283 + .and_then(|m| m.focused_window)
1284 + {
1285 + Some(win) => win,
1286 + None => return Ok(()),
1287 + };
1288 +
1289 + let visible = self.visible_windows();
1290 + if visible.len() < 2 {
1291 + return Ok(());
1292 + }
1293 +
1294 + let focused_geom = match self.connection.get_geometry(focused)?.reply() {
1295 + Ok(geom) => geom,
1296 + Err(_) => return Ok(()),
1297 + };
1298 +
1299 + let focused_center_x = focused_geom.x + (focused_geom.width as i16 / 2);
1300 + let focused_center_y = focused_geom.y + (focused_geom.height as i16 / 2);
1301 +
1302 + let mut candidates = Vec::new();
1303 +
1304 + for &window in &visible {
1305 + if window == focused {
1306 + continue;
1307 + }
1308 +
1309 + let geom = match self.connection.get_geometry(window)?.reply() {
1310 + Ok(g) => g,
1311 + Err(_) => continue,
1312 + };
1313 +
1314 + let center_x = geom.x + (geom.width as i16 / 2);
1315 + let center_y = geom.y + (geom.height as i16 / 2);
1316 +
1317 + let is_valid_direction = match direction {
1318 + 0 => center_y < focused_center_y,
1319 + 1 => center_y > focused_center_y,
1320 + 2 => center_x < focused_center_x,
1321 + 3 => center_x > focused_center_x,
1322 + _ => false,
1323 + };
1324 +
1325 + if is_valid_direction {
1326 + let dx = (center_x - focused_center_x) as i32;
1327 + let dy = (center_y - focused_center_y) as i32;
1328 + let distance = dx * dx + dy * dy;
1329 + candidates.push((window, distance));
1330 + }
1331 + }
1332 +
1333 + if let Some(&(target_window, _)) = candidates.iter().min_by_key(|&(_, dist)| dist) {
1334 + let focused_pos = self.windows.iter().position(|&w| w == focused);
1335 + let target_pos = self.windows.iter().position(|&w| w == target_window);
1336 +
1337 + if let (Some(f_pos), Some(t_pos)) = (focused_pos, target_pos) {
1338 + self.windows.swap(f_pos, t_pos);
1339 + self.apply_layout()?;
1340 + self.set_focus(focused)?;
1341 +
1342 + if let Ok(geometry) = self.connection.get_geometry(focused)?.reply() {
1343 + self.connection.warp_pointer(
1344 + x11rb::NONE,
1345 + focused,
1346 + 0,
1347 + 0,
1348 + 0,
1349 + 0,
1350 + geometry.width as i16 / 2,
1351 + geometry.height as i16 / 2,
1352 + )?;
1353 + }
1354 + }
1355 + }
1356 +
1357 + Ok(())
1358 + }
1359 +
1208 1360 fn grab_next_keys(&self, candidates: &[usize], keys_pressed: usize) -> WmResult<()> {
1209 1361 use std::collections::HashMap;
1210 1362 use x11rb::protocol::xproto::Keycode;
+8 −13 templates/config.ron
@@ -66,8 +66,10 @@
66 66 (modifiers: [$modkey], key: A, action: ToggleGaps),
67 67 (modifiers: [$modkey, Shift], key: Q, action: Quit),
68 68 (modifiers: [$modkey, Shift], key: R, action: Restart),
69 - (modifiers: [$modkey], key: J, action: FocusStack, arg: -1),
70 - (modifiers: [$modkey], key: K, action: FocusStack, arg: 1),
69 + (modifiers: [$modkey], key: H, action: FocusDirection, arg: 2),
70 + (modifiers: [$modkey], key: J, action: FocusDirection, arg: 1),
71 + (modifiers: [$modkey], key: K, action: FocusDirection, arg: 0),
72 + (modifiers: [$modkey], key: L, action: FocusDirection, arg: 3),
71 73 (modifiers: [$modkey], key: Comma, action: FocusMonitor, arg: -1),
72 74 (modifiers: [$modkey], key: Period, action: FocusMonitor, arg: 1),
73 75 (modifiers: [$modkey], key: Key1, action: ViewTag, arg: 0),
@@ -89,17 +91,10 @@
89 91 (modifiers: [$modkey, Shift], key: Key8, action: MoveToTag, arg: 7),
90 92 (modifiers: [$modkey, Shift], key: Key9, action: MoveToTag, arg: 8),
91 93
92 - // Moving Windows
93 - (modifiers: [$modkey, Control], key: K, action: SmartMoveWin, arg: 0), // UP
94 - (modifiers: [$modkey, Control], key: J, action: SmartMoveWin, arg: 1), // DOWN
95 - (modifiers: [$modkey, Control], key: H, action: SmartMoveWin, arg: 2), // LEFT
96 - (modifiers: [$modkey, Control], key: L, action: SmartMoveWin, arg: 3), // RIGHT
97 -
98 - // Exchanging Clients
99 - (modifiers: [$modkey, Shift], key: K, action: ExchangeClient, arg: 0), // UP
100 - (modifiers: [$modkey, Shift], key: J, action: ExchangeClient, arg: 1), // DOWN
101 - (modifiers: [$modkey, Shift], key: H, action: ExchangeClient, arg: 2), // LEFT
102 - (modifiers: [$modkey, Shift], key: L, action: ExchangeClient, arg: 3), // RIGHT
94 + (modifiers: [$modkey, Shift], key: H, action: SwapDirection, arg: 2),
95 + (modifiers: [$modkey, Shift], key: J, action: SwapDirection, arg: 1),
96 + (modifiers: [$modkey, Shift], key: K, action: SwapDirection, arg: 0),
97 + (modifiers: [$modkey, Shift], key: L, action: SwapDirection, arg: 3),
103 98
104 99 // Example keychord bindings (uncomment to use):
105 100 // KEYCHORDS