| 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_outer_h = if (monitor.smartgaps_enabled and n == 1) 0 else monitor.gap_outer_h; |
| 23 | const gap_outer_v = if (monitor.smartgaps_enabled and n == 1) 0 else monitor.gap_outer_v; |
| 24 | const gap_inner_h = monitor.gap_inner_h; |
| 25 | const gap_inner_v = monitor.gap_inner_v; |
| 26 | |
| 27 | const work_x: i32 = monitor.win_x + gap_outer_v; |
| 28 | const work_y: i32 = monitor.win_y + gap_outer_h; |
| 29 | const work_w: i32 = monitor.win_w - 2 * gap_outer_v; |
| 30 | const work_h: i32 = monitor.win_h - 2 * gap_outer_h; |
| 31 | |
| 32 | var nx: i32 = work_x; |
| 33 | var ny: i32 = 0; |
| 34 | var nw: i32 = work_w; |
| 35 | var nh: i32 = work_h; |
| 36 | |
| 37 | var i: u32 = 0; |
| 38 | var current = client_mod.nextTiled(monitor.clients); |
| 39 | while (current) |c| : (current = client_mod.nextTiled(c.next)) { |
| 40 | const bw = c.border_width; |
| 41 | const can_split = (i % 2 == 1 and @divTrunc(nh, 2) > 2 * bw) or |
| 42 | (i % 2 == 0 and @divTrunc(nw, 2) > 2 * bw); |
| 43 | |
| 44 | if (can_split) { |
| 45 | if (i + 1 < n) { |
| 46 | if (i % 2 == 1) { |
| 47 | nh = @divTrunc(nh - gap_inner_h, 2); |
| 48 | } else { |
| 49 | nw = @divTrunc(nw - gap_inner_v, 2); |
| 50 | } |
| 51 | } |
| 52 | switch (i % 4) { |
| 53 | 0 => ny += nh + gap_inner_h, |
| 54 | 1 => nx += nw + gap_inner_v, |
| 55 | 2 => ny += nh + gap_inner_h, |
| 56 | 3 => nx += nw + gap_inner_v, |
| 57 | else => unreachable, |
| 58 | } |
| 59 | if (i == 0) { |
| 60 | if (n != 1) { |
| 61 | nw = @intFromFloat(@as(f32, @floatFromInt(work_w - gap_inner_v)) * monitor.mfact); |
| 62 | } |
| 63 | ny = work_y; |
| 64 | } else if (i == 1) { |
| 65 | nw = work_w - nw - gap_inner_v; |
| 66 | } |
| 67 | i += 1; |
| 68 | } |
| 69 | |
| 70 | tiling.resize(c, nx, ny, nw - 2 * bw, nh - 2 * bw, false); |
| 71 | } |
| 72 | } |