dwindle algo
Browse files
+70 −0 src/layouts/dwindle.zig new file mode 100644
| @@ -0,0 +1,70 @@ | ||
| 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 = dwindle, | |
| 12 | +}; | |
| 13 | + | |
| 14 | +pub fn dwindle(monitor: *Monitor) void { | |
| 15 | + var n: u32 = 0; | |
| 16 | + var counter = client_mod.nextTiled(monitor.clients); | |
| 17 | + while (counter) |c| : (counter = client_mod.nextTiled(c.next)) { | |
| 18 | + n += 1; | |
| 19 | + } | |
| 20 | + if (n == 0) return; | |
| 21 | + | |
| 22 | + const gap_h = if (monitor.smartgaps_enabled and n == 1) 0 else monitor.gap_outer_h; | |
| 23 | + const gap_v = if (monitor.smartgaps_enabled and n == 1) 0 else monitor.gap_outer_v; | |
| 24 | + | |
| 25 | + const work_x: i32 = monitor.win_x + gap_v; | |
| 26 | + const work_y: i32 = monitor.win_y + gap_h; | |
| 27 | + const work_w: i32 = monitor.win_w - 2 * gap_v; | |
| 28 | + const work_h: i32 = monitor.win_h - 2 * gap_h; | |
| 29 | + | |
| 30 | + var nx: i32 = work_x; | |
| 31 | + var ny: i32 = 0; | |
| 32 | + var nw: i32 = work_w; | |
| 33 | + var nh: i32 = work_h; | |
| 34 | + | |
| 35 | + var i: u32 = 0; | |
| 36 | + var current = client_mod.nextTiled(monitor.clients); | |
| 37 | + while (current) |c| : (current = client_mod.nextTiled(c.next)) { | |
| 38 | + const bw = c.border_width; | |
| 39 | + const can_split = (i % 2 == 1 and @divTrunc(nh, 2) > 2 * bw) or | |
| 40 | + (i % 2 == 0 and @divTrunc(nw, 2) > 2 * bw); | |
| 41 | + | |
| 42 | + if (can_split) { | |
| 43 | + if (i + 1 < n) { | |
| 44 | + if (i % 2 == 1) { | |
| 45 | + nh = @divTrunc(nh, 2); | |
| 46 | + } else { | |
| 47 | + nw = @divTrunc(nw, 2); | |
| 48 | + } | |
| 49 | + } | |
| 50 | + switch (i % 4) { | |
| 51 | + 0 => ny += nh, | |
| 52 | + 1 => nx += nw, | |
| 53 | + 2 => ny += nh, | |
| 54 | + 3 => nx += nw, | |
| 55 | + else => unreachable, | |
| 56 | + } | |
| 57 | + if (i == 0) { | |
| 58 | + if (n != 1) { | |
| 59 | + nw = @intFromFloat(@as(f32, @floatFromInt(work_w)) * monitor.mfact); | |
| 60 | + } | |
| 61 | + ny = work_y; | |
| 62 | + } else if (i == 1) { | |
| 63 | + nw = work_w - nw; | |
| 64 | + } | |
| 65 | + i += 1; | |
| 66 | + } | |
| 67 | + | |
| 68 | + tiling.resize(c, nx, ny, nw - 2 * bw, nh - 2 * bw, false); | |
| 69 | + } | |
| 70 | +} |