tony / oxwm

Branch: c7d9989
Switch branches/tags
master
tonybanters Zig port. e95e4af 2026-02-02
114 lines · 4 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 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.next_tiled(monitor.clients);
18 while (current) |_| : (current = client_mod.next_tiled(current.?.next)) {
19 client_count += 1;
20 }
21
22 if (client_count == 0) return;
23
24 const gap_outer_h = monitor.gap_outer_h;
25 const gap_outer_v = 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.next_tiled(monitor.clients);
41 while (current) |client| : (current = client_mod.next_tiled(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.resize_client(
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 get_scroll_step(monitor: *Monitor) i32 {
71 const gap_outer_v = monitor.gap_outer_v;
72 const gap_inner_v = monitor.gap_inner_v;
73 const visible_count: u32 = @intCast(@max(1, monitor.nmaster));
74 const available_width = monitor.win_w - 2 * gap_outer_v;
75 const total_gaps = gap_inner_v * @as(i32, @intCast(if (visible_count > 1) visible_count - 1 else 0));
76 const window_width = @divTrunc(available_width - total_gaps, @as(i32, @intCast(visible_count)));
77 return window_width + gap_inner_v;
78 }
79
80 pub fn get_max_scroll(monitor: *Monitor) i32 {
81 var client_count: u32 = 0;
82 var current = client_mod.next_tiled(monitor.clients);
83 while (current) |_| : (current = client_mod.next_tiled(current.?.next)) {
84 client_count += 1;
85 }
86
87 const visible_count: u32 = @intCast(@max(1, monitor.nmaster));
88 if (client_count <= visible_count) return 0;
89
90 const scroll_step = get_scroll_step(monitor);
91 const scrollable = client_count - visible_count;
92 return scroll_step * @as(i32, @intCast(scrollable));
93 }
94
95 pub fn get_window_index(monitor: *Monitor, target: *Client) ?u32 {
96 var index: u32 = 0;
97 var current = client_mod.next_tiled(monitor.clients);
98 while (current) |client| : (current = client_mod.next_tiled(client.next)) {
99 if (client == target) return index;
100 index += 1;
101 }
102 return null;
103 }
104
105 pub fn get_target_scroll_for_window(monitor: *Monitor, target: *Client) i32 {
106 const index = get_window_index(monitor, target) orelse return 0;
107 if (index == 0) return 0;
108
109 const scroll_step = get_scroll_step(monitor);
110 const max_scroll = get_max_scroll(monitor);
111
112 const target_scroll = scroll_step * @as(i32, @intCast(index));
113 return @min(target_scroll, max_scroll);
114 }