Merge pull request #151 from tonybanters/grid-impl
Browse files
Grid impl
+2 −0 src/bar/bar.zig
| @@ -16,6 +16,7 @@ fn get_layout_symbol(layout_index: u32) []const u8 { | |||
| 16 | 1 => conf.layout_monocle_symbol, | 16 | 1 => conf.layout_monocle_symbol, |
| 17 | 2 => conf.layout_floating_symbol, | 17 | 2 => conf.layout_floating_symbol, |
| 18 | 3 => "[S]", | 18 | 3 => "[S]", |
| 19 | + 4 => "[#]", | ||
| 19 | else => "[?]", | 20 | else => "[?]", |
| 20 | }; | 21 | }; |
| 21 | } | 22 | } |
| @@ -24,6 +25,7 @@ fn get_layout_symbol(layout_index: u32) []const u8 { | |||
| 24 | 1 => "[M]", | 25 | 1 => "[M]", |
| 25 | 2 => "><>", | 26 | 2 => "><>", |
| 26 | 3 => "[S]", | 27 | 3 => "[S]", |
| 28 | + 4 => "[#]", | ||
| 27 | else => "[?]", | 29 | else => "[?]", |
| 28 | }; | 30 | }; |
| 29 | } | 31 | } |
+86 −0 src/layouts/grid.zig new file mode 100644
| @@ -0,0 +1,86 @@ | |||
| 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 = grid, | ||
| 12 | +}; | ||
| 13 | + | ||
| 14 | +pub fn grid(monitor: *Monitor) void { | ||
| 15 | + var client_count: u32 = 0; | ||
| 16 | + var current = client_mod.next_tiled(monitor.clients); | ||
| 17 | + while (current) |_| : (current = client_mod.next_tiled(current.?.next)) { | ||
| 18 | + client_count += 1; | ||
| 19 | + } | ||
| 20 | + | ||
| 21 | + if (client_count == 0) return; | ||
| 22 | + | ||
| 23 | + const gap_outer_h = monitor.gap_outer_h; | ||
| 24 | + const gap_outer_v = monitor.gap_outer_v; | ||
| 25 | + const gap_inner_h = monitor.gap_inner_h; | ||
| 26 | + const gap_inner_v = monitor.gap_inner_v; | ||
| 27 | + | ||
| 28 | + if (client_count == 1) { | ||
| 29 | + const client = client_mod.next_tiled(monitor.clients).?; | ||
| 30 | + tiling.resize( | ||
| 31 | + client, | ||
| 32 | + monitor.win_x + gap_outer_v, | ||
| 33 | + monitor.win_y + gap_outer_h, | ||
| 34 | + monitor.win_w - 2 * gap_outer_v - 2 * client.border_width, | ||
| 35 | + monitor.win_h - 2 * gap_outer_h - 2 * client.border_width, | ||
| 36 | + false, | ||
| 37 | + ); | ||
| 38 | + return; | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + const cols: u32 = @intFromFloat(@ceil(@sqrt(@as(f64, @floatFromInt(client_count))))); | ||
| 42 | + const rows: u32 = @intFromFloat(@ceil(@as(f64, @floatFromInt(client_count)) / @as(f64, @floatFromInt(cols)))); | ||
| 43 | + | ||
| 44 | + const total_horizontal_gaps = 2 * gap_outer_v + gap_inner_v * @as(i32, @intCast(cols - 1)); | ||
| 45 | + const total_vertical_gaps = 2 * gap_outer_h + gap_inner_h * @as(i32, @intCast(rows - 1)); | ||
| 46 | + | ||
| 47 | + const available_width = monitor.win_w - total_horizontal_gaps; | ||
| 48 | + const available_height = monitor.win_h - total_vertical_gaps; | ||
| 49 | + const cell_width: i32 = @divTrunc(available_width, @as(i32, @intCast(cols))); | ||
| 50 | + const cell_height: i32 = @divTrunc(available_height, @as(i32, @intCast(rows))); | ||
| 51 | + | ||
| 52 | + var index: u32 = 0; | ||
| 53 | + current = client_mod.next_tiled(monitor.clients); | ||
| 54 | + while (current) |client| : (current = client_mod.next_tiled(client.next)) { | ||
| 55 | + const row = index / cols; | ||
| 56 | + const col = index % cols; | ||
| 57 | + | ||
| 58 | + const is_last_row = row == rows - 1; | ||
| 59 | + const windows_in_last_row = client_count - (rows - 1) * cols; | ||
| 60 | + | ||
| 61 | + var x: i32 = undefined; | ||
| 62 | + var y: i32 = undefined; | ||
| 63 | + var width: i32 = undefined; | ||
| 64 | + var height: i32 = undefined; | ||
| 65 | + | ||
| 66 | + if (is_last_row and windows_in_last_row < cols) { | ||
| 67 | + const last_row_col = index % cols; | ||
| 68 | + const last_row_gaps = 2 * gap_outer_v + gap_inner_v * @as(i32, @intCast(windows_in_last_row - 1)); | ||
| 69 | + const last_row_available = monitor.win_w - last_row_gaps; | ||
| 70 | + const last_row_cell_width = @divTrunc(last_row_available, @as(i32, @intCast(windows_in_last_row))); | ||
| 71 | + | ||
| 72 | + x = monitor.win_x + gap_outer_v + @as(i32, @intCast(last_row_col)) * (last_row_cell_width + gap_inner_v); | ||
| 73 | + y = monitor.win_y + gap_outer_h + @as(i32, @intCast(row)) * (cell_height + gap_inner_h); | ||
| 74 | + width = last_row_cell_width - 2 * client.border_width; | ||
| 75 | + height = cell_height - 2 * client.border_width; | ||
| 76 | + } else { | ||
| 77 | + x = monitor.win_x + gap_outer_v + @as(i32, @intCast(col)) * (cell_width + gap_inner_v); | ||
| 78 | + y = monitor.win_y + gap_outer_h + @as(i32, @intCast(row)) * (cell_height + gap_inner_h); | ||
| 79 | + width = cell_width - 2 * client.border_width; | ||
| 80 | + height = cell_height - 2 * client.border_width; | ||
| 81 | + } | ||
| 82 | + | ||
| 83 | + tiling.resize(client, x, y, width, height, false); | ||
| 84 | + index += 1; | ||
| 85 | + } | ||
| 86 | +} | ||
+8 −1 src/main.zig
| @@ -9,6 +9,7 @@ const tiling = @import("layouts/tiling.zig"); | |||
| 9 | const monocle = @import("layouts/monocle.zig"); | 9 | const monocle = @import("layouts/monocle.zig"); |
| 10 | const floating = @import("layouts/floating.zig"); | 10 | const floating = @import("layouts/floating.zig"); |
| 11 | const scrolling = @import("layouts/scrolling.zig"); | 11 | const scrolling = @import("layouts/scrolling.zig"); |
| 12 | +const grid = @import("layouts/grid.zig"); | ||
| 12 | const animations = @import("animations.zig"); | 13 | const animations = @import("animations.zig"); |
| 13 | const bar_mod = @import("bar/bar.zig"); | 14 | const bar_mod = @import("bar/bar.zig"); |
| 14 | const blocks_mod = @import("bar/blocks/blocks.zig"); | 15 | const blocks_mod = @import("bar/blocks/blocks.zig"); |
| @@ -350,11 +351,13 @@ fn setup_monitors(display: *Display) void { | |||
| 350 | mon.lt[1] = &monocle.layout; | 351 | mon.lt[1] = &monocle.layout; |
| 351 | mon.lt[2] = &floating.layout; | 352 | mon.lt[2] = &floating.layout; |
| 352 | mon.lt[3] = &scrolling.layout; | 353 | mon.lt[3] = &scrolling.layout; |
| 354 | + mon.lt[4] = &grid.layout; | ||
| 353 | for (0..10) |i| { | 355 | for (0..10) |i| { |
| 354 | mon.pertag.ltidxs[i][0] = mon.lt[0]; | 356 | mon.pertag.ltidxs[i][0] = mon.lt[0]; |
| 355 | mon.pertag.ltidxs[i][1] = mon.lt[1]; | 357 | mon.pertag.ltidxs[i][1] = mon.lt[1]; |
| 356 | mon.pertag.ltidxs[i][2] = mon.lt[2]; | 358 | mon.pertag.ltidxs[i][2] = mon.lt[2]; |
| 357 | mon.pertag.ltidxs[i][3] = mon.lt[3]; | 359 | mon.pertag.ltidxs[i][3] = mon.lt[3]; |
| 360 | + mon.pertag.ltidxs[i][4] = mon.lt[4]; | ||
| 358 | } | 361 | } |
| 359 | 362 | ||
| 360 | if (prev_monitor) |prev| { | 363 | if (prev_monitor) |prev| { |
| @@ -388,11 +391,13 @@ fn setup_monitors(display: *Display) void { | |||
| 388 | mon.lt[1] = &monocle.layout; | 391 | mon.lt[1] = &monocle.layout; |
| 389 | mon.lt[2] = &floating.layout; | 392 | mon.lt[2] = &floating.layout; |
| 390 | mon.lt[3] = &scrolling.layout; | 393 | mon.lt[3] = &scrolling.layout; |
| 394 | + mon.lt[4] = &grid.layout; | ||
| 391 | for (0..10) |i| { | 395 | for (0..10) |i| { |
| 392 | mon.pertag.ltidxs[i][0] = mon.lt[0]; | 396 | mon.pertag.ltidxs[i][0] = mon.lt[0]; |
| 393 | mon.pertag.ltidxs[i][1] = mon.lt[1]; | 397 | mon.pertag.ltidxs[i][1] = mon.lt[1]; |
| 394 | mon.pertag.ltidxs[i][2] = mon.lt[2]; | 398 | mon.pertag.ltidxs[i][2] = mon.lt[2]; |
| 395 | mon.pertag.ltidxs[i][3] = mon.lt[3]; | 399 | mon.pertag.ltidxs[i][3] = mon.lt[3]; |
| 400 | + mon.pertag.ltidxs[i][4] = mon.lt[4]; | ||
| 396 | } | 401 | } |
| 397 | monitor_mod.monitors = mon; | 402 | monitor_mod.monitors = mon; |
| 398 | monitor_mod.selected_monitor = mon; | 403 | monitor_mod.selected_monitor = mon; |
| @@ -1468,7 +1473,7 @@ fn setmfact(delta: f32) void { | |||
| 1468 | 1473 | ||
| 1469 | fn cycle_layout() void { | 1474 | fn cycle_layout() void { |
| 1470 | const monitor = monitor_mod.selected_monitor orelse return; | 1475 | const monitor = monitor_mod.selected_monitor orelse return; |
| 1471 | - const new_lt = (monitor.sel_lt + 1) % 4; | 1476 | + const new_lt = (monitor.sel_lt + 1) % 5; |
| 1472 | monitor.sel_lt = new_lt; | 1477 | monitor.sel_lt = new_lt; |
| 1473 | monitor.pertag.sellts[monitor.pertag.curtag] = new_lt; | 1478 | monitor.pertag.sellts[monitor.pertag.curtag] = new_lt; |
| 1474 | if (new_lt != 3) { | 1479 | if (new_lt != 3) { |
| @@ -1493,6 +1498,8 @@ fn set_layout(layout_name: ?[]const u8) void { | |||
| 1493 | 2 | 1498 | 2 |
| 1494 | else if (std.mem.eql(u8, name, "scrolling") or std.mem.eql(u8, name, "[S]")) | 1499 | else if (std.mem.eql(u8, name, "scrolling") or std.mem.eql(u8, name, "[S]")) |
| 1495 | 3 | 1500 | 3 |
| 1501 | + else if (std.mem.eql(u8, name, "grid") or std.mem.eql(u8, name, "[#]")) | ||
| 1502 | + 4 | ||
| 1496 | else { | 1503 | else { |
| 1497 | std.debug.print("set_layout: unknown layout '{s}'\n", .{name}); | 1504 | std.debug.print("set_layout: unknown layout '{s}'\n", .{name}); |
| 1498 | return; | 1505 | return; |
+2 −2 src/monitor.zig
| @@ -13,7 +13,7 @@ pub const Pertag = struct { | |||
| 13 | nmasters: [10]i32 = [_]i32{1} ** 10, | 13 | nmasters: [10]i32 = [_]i32{1} ** 10, |
| 14 | mfacts: [10]f32 = [_]f32{0.55} ** 10, | 14 | mfacts: [10]f32 = [_]f32{0.55} ** 10, |
| 15 | sellts: [10]u32 = [_]u32{0} ** 10, | 15 | sellts: [10]u32 = [_]u32{0} ** 10, |
| 16 | - ltidxs: [10][4]?*const Layout = [_][4]?*const Layout{.{ null, null, null, null }} ** 10, | 16 | + ltidxs: [10][5]?*const Layout = [_][5]?*const Layout{.{ null, null, null, null, null }} ** 10, |
| 17 | showbars: [10]bool = [_]bool{true} ** 10, | 17 | showbars: [10]bool = [_]bool{true} ** 10, |
| 18 | }; | 18 | }; |
| 19 | 19 | ||
| @@ -46,7 +46,7 @@ pub const Monitor = struct { | |||
| 46 | stack: ?*Client = null, | 46 | stack: ?*Client = null, |
| 47 | next: ?*Monitor = null, | 47 | next: ?*Monitor = null, |
| 48 | bar_win: xlib.Window = 0, | 48 | bar_win: xlib.Window = 0, |
| 49 | - lt: [4]?*const Layout = .{ null, null, null, null }, | 49 | + lt: [5]?*const Layout = .{ null, null, null, null, null }, |
| 50 | pertag: Pertag = Pertag{}, | 50 | pertag: Pertag = Pertag{}, |
| 51 | }; | 51 | }; |
| 52 | 52 | ||