| 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 = "[S]", |
| 11 | .arrange_fn = scroll, |
| 12 | }; |
| 13 | |
| 14 | pub const Snap = struct { |
| 15 | client: *Client, |
| 16 | offset: i32, |
| 17 | width: i32, |
| 18 | }; |
| 19 | |
| 20 | pub const Geometry = struct { |
| 21 | outer_h: i32, |
| 22 | outer_v: i32, |
| 23 | inner: i32, |
| 24 | available_w: i32, |
| 25 | available_h: i32, |
| 26 | count: u32, |
| 27 | }; |
| 28 | |
| 29 | pub fn tiledCount(monitor: *Monitor) u32 { |
| 30 | var count: u32 = 0; |
| 31 | var current = client_mod.nextTiled(monitor.clients); |
| 32 | while (current) |client| : (current = client_mod.nextTiled(client.next)) { |
| 33 | count += 1; |
| 34 | } |
| 35 | return count; |
| 36 | } |
| 37 | |
| 38 | pub fn geometry(monitor: *Monitor) Geometry { |
| 39 | const count = tiledCount(monitor); |
| 40 | const smart = monitor.smartgaps_enabled and count == 1; |
| 41 | const outer_h = if (smart) 0 else monitor.gap_outer_h; |
| 42 | const outer_v = if (smart) 0 else monitor.gap_outer_v; |
| 43 | return .{ |
| 44 | .outer_h = outer_h, |
| 45 | .outer_v = outer_v, |
| 46 | .inner = monitor.gap_inner_v, |
| 47 | .available_w = monitor.win_w - 2 * outer_v, |
| 48 | .available_h = monitor.win_h - 2 * outer_h, |
| 49 | .count = count, |
| 50 | }; |
| 51 | } |
| 52 | |
| 53 | pub fn windowWidth(monitor: *Monitor, client: *Client, geo: Geometry) i32 { |
| 54 | const value: f32 = if (client.scroll_width > 0) client.scroll_width else monitor.scroll_default_width; |
| 55 | const available: f32 = @floatFromInt(geo.available_w); |
| 56 | const inner: f32 = @floatFromInt(geo.inner); |
| 57 | const width: i32 = if (value > 1.0) |
| 58 | @intFromFloat(value) |
| 59 | else |
| 60 | @intFromFloat((available + inner) * value - inner); |
| 61 | return @max(1, @min(width, geo.available_w)); |
| 62 | } |
| 63 | |
| 64 | pub fn contentWidth(monitor: *Monitor) i32 { |
| 65 | const geo = geometry(monitor); |
| 66 | var offset: i32 = 0; |
| 67 | var current = client_mod.nextTiled(monitor.clients); |
| 68 | while (current) |client| : (current = client_mod.nextTiled(client.next)) { |
| 69 | offset += windowWidth(monitor, client, geo) + geo.inner; |
| 70 | } |
| 71 | return @max(0, offset - geo.inner); |
| 72 | } |
| 73 | |
| 74 | pub fn maxScroll(monitor: *Monitor) i32 { |
| 75 | const geo = geometry(monitor); |
| 76 | return @max(0, contentWidth(monitor) - geo.available_w); |
| 77 | } |
| 78 | |
| 79 | pub fn snapFor(monitor: *Monitor, target: *Client) ?Snap { |
| 80 | const geo = geometry(monitor); |
| 81 | const max = maxScroll(monitor); |
| 82 | var offset: i32 = 0; |
| 83 | var current = client_mod.nextTiled(monitor.clients); |
| 84 | while (current) |client| : (current = client_mod.nextTiled(client.next)) { |
| 85 | const width = windowWidth(monitor, client, geo); |
| 86 | if (client == target) return .{ .client = client, .offset = @min(offset, max), .width = width }; |
| 87 | offset += width + geo.inner; |
| 88 | } |
| 89 | return null; |
| 90 | } |
| 91 | |
| 92 | pub fn offsetToReveal(monitor: *Monitor, offset: i32, target: *Client) i32 { |
| 93 | const geo = geometry(monitor); |
| 94 | const max = maxScroll(monitor); |
| 95 | var left: i32 = 0; |
| 96 | var current = client_mod.nextTiled(monitor.clients); |
| 97 | while (current) |client| : (current = client_mod.nextTiled(client.next)) { |
| 98 | const width = windowWidth(monitor, client, geo); |
| 99 | if (client == target) { |
| 100 | var result = offset; |
| 101 | if (left < offset) { |
| 102 | result = left; |
| 103 | } else if (left + width > offset + geo.available_w) { |
| 104 | result = left + width - geo.available_w; |
| 105 | } |
| 106 | return @max(0, @min(result, max)); |
| 107 | } |
| 108 | left += width + geo.inner; |
| 109 | } |
| 110 | return @max(0, @min(offset, max)); |
| 111 | } |
| 112 | |
| 113 | pub fn nearestSnap(monitor: *Monitor, position: f64) ?Snap { |
| 114 | const geo = geometry(monitor); |
| 115 | const max = maxScroll(monitor); |
| 116 | var best: ?Snap = null; |
| 117 | var best_dist: f64 = std.math.inf(f64); |
| 118 | var offset: i32 = 0; |
| 119 | var current = client_mod.nextTiled(monitor.clients); |
| 120 | while (current) |client| : (current = client_mod.nextTiled(client.next)) { |
| 121 | const width = windowWidth(monitor, client, geo); |
| 122 | const dist = @abs(@as(f64, @floatFromInt(offset)) - position); |
| 123 | if (dist < best_dist) { |
| 124 | best_dist = dist; |
| 125 | best = .{ .client = client, .offset = @min(offset, max), .width = width }; |
| 126 | } |
| 127 | offset += width + geo.inner; |
| 128 | } |
| 129 | return best; |
| 130 | } |
| 131 | |
| 132 | pub fn scroll(monitor: *Monitor) void { |
| 133 | const geo = geometry(monitor); |
| 134 | if (geo.count == 0) return; |
| 135 | |
| 136 | var x_pos: i32 = monitor.win_x + geo.outer_v - monitor.scroll_offset; |
| 137 | const y_pos: i32 = monitor.win_y + geo.outer_h; |
| 138 | const screen_left = monitor.win_x; |
| 139 | const screen_right = monitor.win_x + monitor.win_w; |
| 140 | |
| 141 | var current = client_mod.nextTiled(monitor.clients); |
| 142 | while (current) |client| : (current = client_mod.nextTiled(client.next)) { |
| 143 | const width = windowWidth(monitor, client, geo); |
| 144 | const border = 2 * client.border_width; |
| 145 | const is_visible = x_pos + width > screen_left and x_pos < screen_right; |
| 146 | |
| 147 | if (is_visible) { |
| 148 | tiling.resize(client, x_pos, y_pos, width - border, geo.available_h - border, false); |
| 149 | } else { |
| 150 | tiling.resizeClient(client, -2 * (width + border), y_pos, width - border, geo.available_h - border); |
| 151 | } |
| 152 | x_pos += width + geo.inner; |
| 153 | } |
| 154 | } |