tony / oxwm

Branch: afc403f
Switch branches/tags
master
tonybanters Added smartgaps impl c09f004 2026-02-27
86 lines · 3.6 KB Raw Blame History
1 const std = @import("std");
2 const client_mod = @import("../client.zig");
3 const monitor_mod = @import("../monitor.zig");
4 const tiling = @import("tiling.zig");
5
6 const Client = client_mod.Client;
7 const Monitor = monitor_mod.Monitor;
8
9 pub const layout = monitor_mod.Layout{
10 .symbol = "[#]",
11 .arrange_fn = grid,
12 };
13
14 pub fn grid(monitor: *Monitor) void {
15 var client_count: u32 = 0;
16 var current = client_mod.nextTiled(monitor.clients);
17 while (current) |_| : (current = client_mod.nextTiled(current.?.next)) {
18 client_count += 1;
19 }
20
21 if (client_count == 0) return;
22
23 const gap_outer_h = if (monitor.smartgaps_enabled and client_count == 1) 0 else monitor.gap_outer_h;
24 const gap_outer_v = if (monitor.smartgaps_enabled and client_count == 1) 0 else monitor.gap_outer_v;
25 const gap_inner_h = monitor.gap_inner_h;
26 const gap_inner_v = monitor.gap_inner_v;
27
28 if (client_count == 1) {
29 const client = client_mod.nextTiled(monitor.clients).?;
30 tiling.resize(
31 client,
32 monitor.win_x + gap_outer_v,
33 monitor.win_y + gap_outer_h,
34 monitor.win_w - 2 * gap_outer_v - 2 * client.border_width,
35 monitor.win_h - 2 * gap_outer_h - 2 * client.border_width,
36 false,
37 );
38 return;
39 }
40
41 const cols: u32 = @intFromFloat(@ceil(@sqrt(@as(f64, @floatFromInt(client_count)))));
42 const rows: u32 = @intFromFloat(@ceil(@as(f64, @floatFromInt(client_count)) / @as(f64, @floatFromInt(cols))));
43
44 const total_horizontal_gaps = 2 * gap_outer_v + gap_inner_v * @as(i32, @intCast(cols - 1));
45 const total_vertical_gaps = 2 * gap_outer_h + gap_inner_h * @as(i32, @intCast(rows - 1));
46
47 const available_width = monitor.win_w - total_horizontal_gaps;
48 const available_height = monitor.win_h - total_vertical_gaps;
49 const cell_width: i32 = @divTrunc(available_width, @as(i32, @intCast(cols)));
50 const cell_height: i32 = @divTrunc(available_height, @as(i32, @intCast(rows)));
51
52 var index: u32 = 0;
53 current = client_mod.nextTiled(monitor.clients);
54 while (current) |client| : (current = client_mod.nextTiled(client.next)) {
55 const row = index / cols;
56 const col = index % cols;
57
58 const is_last_row = row == rows - 1;
59 const windows_in_last_row = client_count - (rows - 1) * cols;
60
61 var x: i32 = undefined;
62 var y: i32 = undefined;
63 var width: i32 = undefined;
64 var height: i32 = undefined;
65
66 if (is_last_row and windows_in_last_row < cols) {
67 const last_row_col = index % cols;
68 const last_row_gaps = 2 * gap_outer_v + gap_inner_v * @as(i32, @intCast(windows_in_last_row - 1));
69 const last_row_available = monitor.win_w - last_row_gaps;
70 const last_row_cell_width = @divTrunc(last_row_available, @as(i32, @intCast(windows_in_last_row)));
71
72 x = monitor.win_x + gap_outer_v + @as(i32, @intCast(last_row_col)) * (last_row_cell_width + gap_inner_v);
73 y = monitor.win_y + gap_outer_h + @as(i32, @intCast(row)) * (cell_height + gap_inner_h);
74 width = last_row_cell_width - 2 * client.border_width;
75 height = cell_height - 2 * client.border_width;
76 } else {
77 x = monitor.win_x + gap_outer_v + @as(i32, @intCast(col)) * (cell_width + gap_inner_v);
78 y = monitor.win_y + gap_outer_h + @as(i32, @intCast(row)) * (cell_height + gap_inner_h);
79 width = cell_width - 2 * client.border_width;
80 height = cell_height - 2 * client.border_width;
81 }
82
83 tiling.resize(client, x, y, width, height, false);
84 index += 1;
85 }
86 }