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