feature : set default layout
Browse files
+9 −0 src/config/config.zig
| @@ -80,6 +80,14 @@ pub const MouseAction = enum { | ||
| 80 | 80 | toggle_floating, |
| 81 | 81 | }; |
| 82 | 82 | |
| 83 | +pub const Layouts = enum(u32) { | |
| 84 | + tiling, | |
| 85 | + monocle, | |
| 86 | + floating, | |
| 87 | + scrolling, | |
| 88 | + grid, | |
| 89 | +}; | |
| 90 | + | |
| 83 | 91 | pub const MouseButton = struct { |
| 84 | 92 | click: ClickTarget, |
| 85 | 93 | mod_mask: u32, |
| @@ -115,6 +123,7 @@ pub const Config = struct { | ||
| 115 | 123 | terminal: []const u8 = "st", |
| 116 | 124 | font: []const u8 = "monospace:size=10", |
| 117 | 125 | tags: [9][]const u8 = .{ "1", "2", "3", "4", "5", "6", "7", "8", "9" }, |
| 126 | + layout: []const u8 = "tiling", | |
| 118 | 127 | |
| 119 | 128 | border_width: i32 = 2, |
| 120 | 129 | border_focused: u32 = 0x6dade3, |
+12 −0 src/config/lua.zig
| @@ -300,6 +300,9 @@ fn registerMiscFunctions(state: *c.lua_State) void { | ||
| 300 | 300 | c.lua_pushcfunction(state, luaSetTerminal); |
| 301 | 301 | c.lua_setfield(state, -2, "set_terminal"); |
| 302 | 302 | |
| 303 | + c.lua_pushcfunction(state, luaSetLayout); | |
| 304 | + c.lua_setfield(state, -2, "set_layout"); | |
| 305 | + | |
| 303 | 306 | c.lua_pushcfunction(state, luaSetModkey); |
| 304 | 307 | c.lua_setfield(state, -2, "set_modkey"); |
| 305 | 308 | |
| @@ -997,6 +1000,15 @@ fn luaSetTerminal(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 997 | 1000 | return 0; |
| 998 | 1001 | } |
| 999 | 1002 | |
| 1003 | +fn luaSetLayout(state: ?*c.lua_State) callconv(.c) c_int { | |
| 1004 | + const cfg = config orelse return 0; | |
| 1005 | + const s = state orelse return 0; | |
| 1006 | + if (dupeLuaString(s, 1)) |layout| { | |
| 1007 | + cfg.layout = layout; | |
| 1008 | + } | |
| 1009 | + return 0; | |
| 1010 | +} | |
| 1011 | + | |
| 1000 | 1012 | fn luaSetModkey(state: ?*c.lua_State) callconv(.c) c_int { |
| 1001 | 1013 | const cfg = config orelse return 0; |
| 1002 | 1014 | const s = state orelse return 0; |
+12 −21 src/wm/actions.zig
| @@ -342,15 +342,15 @@ pub fn setmfact(delta: f32, wm: *WindowManager) void { | ||
| 342 | 342 | |
| 343 | 343 | pub fn cycleLayout(wm: *WindowManager) void { |
| 344 | 344 | const monitor = wm.selected_monitor orelse return; |
| 345 | - const new_lt = (monitor.sel_lt + 1) % 5; | |
| 345 | + const new_lt = (monitor.sel_lt + 1) % @as(u32, @intCast(std.meta.fields(config_mod.Layouts).len)); | |
| 346 | 346 | monitor.sel_lt = new_lt; |
| 347 | 347 | monitor.pertag.sellts[monitor.pertag.curtag] = new_lt; |
| 348 | - if (new_lt != 3) { | |
| 348 | + if (new_lt != @intFromEnum(config_mod.Layouts.scrolling)) { | |
| 349 | 349 | monitor.scroll_offset = 0; |
| 350 | 350 | } |
| 351 | 351 | core.arrange(monitor, wm); |
| 352 | 352 | wm.invalidateBars(); |
| 353 | - if (monitor.lt[monitor.sel_lt]) |layout| { | |
| 353 | + if (monitor.lt[@as(usize, monitor.sel_lt)]) |layout| { | |
| 354 | 354 | std.debug.print("cycle_layout: {s}\n", .{layout.symbol}); |
| 355 | 355 | } |
| 356 | 356 | } |
| @@ -358,44 +358,35 @@ pub fn cycleLayout(wm: *WindowManager) void { | ||
| 358 | 358 | pub fn setLayout(layout_name: ?[]const u8, wm: *WindowManager) void { |
| 359 | 359 | const monitor = wm.selected_monitor orelse return; |
| 360 | 360 | const name = layout_name orelse return; |
| 361 | - | |
| 362 | - const new_lt: u32 = if (std.mem.eql(u8, name, "tiling") or std.mem.eql(u8, name, "[]=")) | |
| 363 | - 0 | |
| 364 | - else if (std.mem.eql(u8, name, "monocle") or std.mem.eql(u8, name, "[M]")) | |
| 365 | - 1 | |
| 366 | - else if (std.mem.eql(u8, name, "floating") or std.mem.eql(u8, name, "><>")) | |
| 367 | - 2 | |
| 368 | - else if (std.mem.eql(u8, name, "scrolling") or std.mem.eql(u8, name, "[S]")) | |
| 369 | - 3 | |
| 370 | - else if (std.mem.eql(u8, name, "grid") or std.mem.eql(u8, name, "[#]")) | |
| 371 | - 4 | |
| 361 | + const new_lt: u32 = if (std.meta.stringToEnum(config_mod.Layouts, name)) |value| | |
| 362 | + @intFromEnum(value) | |
| 372 | 363 | else { |
| 373 | 364 | std.debug.print("set_layout: unknown layout '{s}'\n", .{name}); |
| 374 | 365 | return; |
| 375 | 366 | }; |
| 376 | - | |
| 377 | 367 | monitor.sel_lt = new_lt; |
| 378 | 368 | monitor.pertag.sellts[monitor.pertag.curtag] = new_lt; |
| 379 | - if (new_lt != 3) { | |
| 369 | + if (new_lt != @intFromEnum(config_mod.Layouts.scrolling)) { | |
| 380 | 370 | monitor.scroll_offset = 0; |
| 381 | 371 | } |
| 382 | 372 | core.arrange(monitor, wm); |
| 383 | 373 | wm.invalidateBars(); |
| 384 | - if (monitor.lt[monitor.sel_lt]) |layout| { | |
| 374 | + if (monitor.lt[@as(usize, monitor.sel_lt)]) |layout| { | |
| 385 | 375 | std.debug.print("set_layout: {s}\n", .{layout.symbol}); |
| 386 | 376 | } |
| 387 | 377 | } |
| 388 | 378 | |
| 389 | 379 | pub fn setLayoutIndex(index: u32, wm: *WindowManager) void { |
| 390 | 380 | const monitor = wm.selected_monitor orelse return; |
| 391 | - monitor.sel_lt = index; | |
| 392 | - monitor.pertag.sellts[monitor.pertag.curtag] = index; | |
| 393 | - if (index != 3) { | |
| 381 | + const lt_index = index % @as(u32, @intCast(std.meta.fields(config_mod.Layouts).len)); | |
| 382 | + monitor.sel_lt = lt_index; | |
| 383 | + monitor.pertag.sellts[monitor.pertag.curtag] = lt_index; | |
| 384 | + if (lt_index != @intFromEnum(config_mod.Layouts.scrolling)) { | |
| 394 | 385 | monitor.scroll_offset = 0; |
| 395 | 386 | } |
| 396 | 387 | core.arrange(monitor, wm); |
| 397 | 388 | wm.invalidateBars(); |
| 398 | - if (monitor.lt[monitor.sel_lt]) |layout| { | |
| 389 | + if (monitor.lt[@as(usize, monitor.sel_lt)]) |layout| { | |
| 399 | 390 | std.debug.print("set_layout_index: {s}\n", .{layout.symbol}); |
| 400 | 391 | } |
| 401 | 392 | } |
+20 −10 src/wm/wm.zig
| @@ -180,11 +180,16 @@ pub const WindowManager = struct { | ||
| 180 | 180 | mon.win_y = screen.y_org; |
| 181 | 181 | mon.win_w = screen.width; |
| 182 | 182 | mon.win_h = screen.height; |
| 183 | - mon.lt[0] = &tiling.layout; | |
| 184 | - mon.lt[1] = &monocle.layout; | |
| 185 | - mon.lt[2] = &floating.layout; | |
| 186 | - mon.lt[3] = &scrolling.layout; | |
| 187 | - mon.lt[4] = &grid.layout; | |
| 183 | + | |
| 184 | + mon.lt[@intFromEnum(config_mod.Layouts.tiling)] = &tiling.layout; | |
| 185 | + mon.lt[@intFromEnum(config_mod.Layouts.monocle)] = &monocle.layout; | |
| 186 | + mon.lt[@intFromEnum(config_mod.Layouts.floating)] = &floating.layout; | |
| 187 | + mon.lt[@intFromEnum(config_mod.Layouts.scrolling)] = &scrolling.layout; | |
| 188 | + mon.lt[@intFromEnum(config_mod.Layouts.grid)] = &grid.layout; | |
| 189 | + | |
| 190 | + if (std.meta.stringToEnum(config_mod.Layouts, self.config.layout)) |value| { | |
| 191 | + mon.sel_lt = @intFromEnum(value); | |
| 192 | + } | |
| 188 | 193 | |
| 189 | 194 | for (0..10) |i| { |
| 190 | 195 | mon.pertag.ltidxs[i][0] = mon.lt[0]; |
| @@ -221,11 +226,16 @@ pub const WindowManager = struct { | ||
| 221 | 226 | mon.win_y = 0; |
| 222 | 227 | mon.win_w = mon.mon_w; |
| 223 | 228 | mon.win_h = mon.mon_h; |
| 224 | - mon.lt[0] = &tiling.layout; | |
| 225 | - mon.lt[1] = &monocle.layout; | |
| 226 | - mon.lt[2] = &floating.layout; | |
| 227 | - mon.lt[3] = &scrolling.layout; | |
| 228 | - mon.lt[4] = &grid.layout; | |
| 229 | + | |
| 230 | + mon.lt[@intFromEnum(config_mod.Layouts.tiling)] = &tiling.layout; | |
| 231 | + mon.lt[@intFromEnum(config_mod.Layouts.monocle)] = &monocle.layout; | |
| 232 | + mon.lt[@intFromEnum(config_mod.Layouts.floating)] = &floating.layout; | |
| 233 | + mon.lt[@intFromEnum(config_mod.Layouts.scrolling)] = &scrolling.layout; | |
| 234 | + mon.lt[@intFromEnum(config_mod.Layouts.grid)] = &grid.layout; | |
| 235 | + | |
| 236 | + if (std.meta.stringToEnum(config_mod.Layouts, self.config.layout)) |value| { | |
| 237 | + mon.sel_lt = @intFromEnum(value); | |
| 238 | + } | |
| 229 | 239 | |
| 230 | 240 | for (0..10) |i| { |
| 231 | 241 | mon.pertag.ltidxs[i][0] = mon.lt[0]; |
+3 −0 templates/config.lua
| @@ -102,6 +102,9 @@ oxwm.set_terminal(terminal) | ||
| 102 | 102 | oxwm.set_modkey(modkey) -- This is for Mod + mouse binds, such as drag/resize |
| 103 | 103 | oxwm.set_tags(tags) |
| 104 | 104 | |
| 105 | +-- Set default layout (tiling by default) | |
| 106 | +-- oxwm.set_layout("tiling") | |
| 107 | + | |
| 105 | 108 | ------------------------------------------------------------------------------- |
| 106 | 109 | -- Layouts |
| 107 | 110 | ------------------------------------------------------------------------------- |