| 1 | const std = @import("std"); |
| 2 | const client_mod = @import("../client.zig"); |
| 3 | const monitor_mod = @import("../monitor.zig"); |
| 4 | const xlib = @import("../x11/xlib.zig"); |
| 5 | const tiling = @import("tiling.zig"); |
| 6 | |
| 7 | const Client = client_mod.Client; |
| 8 | const Monitor = monitor_mod.Monitor; |
| 9 | |
| 10 | pub const layout = monitor_mod.Layout{ |
| 11 | .symbol = "[S]", |
| 12 | .arrange_fn = scroll, |
| 13 | }; |
| 14 | |
| 15 | pub fn scroll(monitor: *Monitor) void { |
| 16 | var client_count: u32 = 0; |
| 17 | var current = client_mod.nextTiled(monitor.clients); |
| 18 | while (current) |_| : (current = client_mod.nextTiled(current.?.next)) { |
| 19 | client_count += 1; |
| 20 | } |
| 21 | |
| 22 | if (client_count == 0) return; |
| 23 | |
| 24 | const gap_outer_h = if (monitor.smartgaps_enabled and client_count == 1) 0 else monitor.gap_outer_h; |
| 25 | const gap_outer_v = if (monitor.smartgaps_enabled and client_count == 1) 0 else monitor.gap_outer_v; |
| 26 | const gap_inner_v = monitor.gap_inner_v; |
| 27 | |
| 28 | const visible_count: u32 = @intCast(@max(1, monitor.nmaster)); |
| 29 | const available_width = monitor.win_w - 2 * gap_outer_v; |
| 30 | const available_height = monitor.win_h - 2 * gap_outer_h; |
| 31 | |
| 32 | const total_gaps = gap_inner_v * @as(i32, @intCast(if (visible_count > 1) visible_count - 1 else 0)); |
| 33 | const window_width = @divTrunc(available_width - total_gaps, @as(i32, @intCast(visible_count))); |
| 34 | |
| 35 | var x_pos: i32 = monitor.win_x + gap_outer_v - monitor.scroll_offset; |
| 36 | const y_pos: i32 = monitor.win_y + gap_outer_h; |
| 37 | const height = available_height; |
| 38 | |
| 39 | var index: u32 = 0; |
| 40 | current = client_mod.nextTiled(monitor.clients); |
| 41 | while (current) |client| : (current = client_mod.nextTiled(client.next)) { |
| 42 | const window_right = x_pos + window_width; |
| 43 | const screen_left = monitor.win_x; |
| 44 | const screen_right = monitor.win_x + monitor.win_w; |
| 45 | const is_visible = window_right > screen_left and x_pos < screen_right; |
| 46 | |
| 47 | if (is_visible) { |
| 48 | tiling.resize( |
| 49 | client, |
| 50 | x_pos, |
| 51 | y_pos, |
| 52 | window_width - 2 * client.border_width, |
| 53 | height - 2 * client.border_width, |
| 54 | false, |
| 55 | ); |
| 56 | } else { |
| 57 | tiling.resizeClient( |
| 58 | client, |
| 59 | -2 * window_width, |
| 60 | y_pos, |
| 61 | window_width - 2 * client.border_width, |
| 62 | height - 2 * client.border_width, |
| 63 | ); |
| 64 | } |
| 65 | x_pos += window_width + gap_inner_v; |
| 66 | index += 1; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | pub fn getScrollStep(monitor: *Monitor) i32 { |
| 71 | var client_count: u32 = 0; |
| 72 | var current = client_mod.nextTiled(monitor.clients); |
| 73 | while (current) |_| : (current = client_mod.nextTiled(current.?.next)) { |
| 74 | client_count += 1; |
| 75 | } |
| 76 | |
| 77 | const gap_outer_v = if (monitor.smartgaps_enabled and client_count == 1) 0 else monitor.gap_outer_v; |
| 78 | const gap_inner_v = monitor.gap_inner_v; |
| 79 | const visible_count: u32 = @intCast(@max(1, monitor.nmaster)); |
| 80 | const available_width = monitor.win_w - 2 * gap_outer_v; |
| 81 | const total_gaps = gap_inner_v * @as(i32, @intCast(if (visible_count > 1) visible_count - 1 else 0)); |
| 82 | const window_width = @divTrunc(available_width - total_gaps, @as(i32, @intCast(visible_count))); |
| 83 | return window_width + gap_inner_v; |
| 84 | } |
| 85 | |
| 86 | pub fn getMaxScroll(monitor: *Monitor) i32 { |
| 87 | var client_count: u32 = 0; |
| 88 | var current = client_mod.nextTiled(monitor.clients); |
| 89 | while (current) |_| : (current = client_mod.nextTiled(current.?.next)) { |
| 90 | client_count += 1; |
| 91 | } |
| 92 | |
| 93 | const visible_count: u32 = @intCast(@max(1, monitor.nmaster)); |
| 94 | if (client_count <= visible_count) return 0; |
| 95 | |
| 96 | const scroll_step = getScrollStep(monitor); |
| 97 | const scrollable = client_count - visible_count; |
| 98 | return scroll_step * @as(i32, @intCast(scrollable)); |
| 99 | } |
| 100 | |
| 101 | pub fn getWindowIndex(monitor: *Monitor, target: *Client) ?u32 { |
| 102 | var index: u32 = 0; |
| 103 | var current = client_mod.nextTiled(monitor.clients); |
| 104 | while (current) |client| : (current = client_mod.nextTiled(client.next)) { |
| 105 | if (client == target) return index; |
| 106 | index += 1; |
| 107 | } |
| 108 | return null; |
| 109 | } |
| 110 | |
| 111 | pub fn getTargetScrollForWindow(monitor: *Monitor, target: *Client) i32 { |
| 112 | const index = getWindowIndex(monitor, target) orelse return 0; |
| 113 | if (index == 0) return 0; |
| 114 | |
| 115 | const scroll_step = getScrollStep(monitor); |
| 116 | const max_scroll = getMaxScroll(monitor); |
| 117 | |
| 118 | const target_scroll = scroll_step * @as(i32, @intCast(index)); |
| 119 | return @min(target_scroll, max_scroll); |
| 120 | } |