feature : click handler on bar blocks
Browse files
+12 −0 src/bar/bar.zig
| @@ -217,6 +217,8 @@ pub const Bar = struct { | ||
| 217 | 217 | const content = block.getContent(); |
| 218 | 218 | const content_width = self.textWidth(display, content); |
| 219 | 219 | block_x -= content_width; |
| 220 | + block.x_start = block_x; | |
| 221 | + block.x_end = block_x + content_width; | |
| 220 | 222 | self.drawText(display, block_x, @divTrunc(self.height + self.font_height, 2) - 4, content, block.color()); |
| 221 | 223 | if (block.underline) { |
| 222 | 224 | self.fillRect(display, block_x, self.height - 2, content_width, 2, block.color()); |
| @@ -256,6 +258,16 @@ pub const Bar = struct { | ||
| 256 | 258 | return null; |
| 257 | 259 | } |
| 258 | 260 | |
| 261 | + /// Returns the click action of the block the user clicked on, or null. | |
| 262 | + pub fn handleBlockClick(self: *Bar, click_x: i32) ?config_mod.ClickAction { | |
| 263 | + for (self.blocks.items) |*block| { | |
| 264 | + if (block.click != null and click_x >= block.x_start and click_x < block.x_end) { | |
| 265 | + return block.click; | |
| 266 | + } | |
| 267 | + } | |
| 268 | + return null; | |
| 269 | + } | |
| 270 | + | |
| 259 | 271 | /// Updates all blocks and marks the bar dirty if any block changed. |
| 260 | 272 | pub fn updateBlocks(self: *Bar) void { |
| 261 | 273 | var changed = false; |
+4 −0 src/bar/blocks/blocks.zig
| @@ -1,4 +1,5 @@ | ||
| 1 | 1 | const std = @import("std"); |
| 2 | +const config_mod = @import("../../config/config.zig"); | |
| 2 | 3 | |
| 3 | 4 | pub const Static = @import("static.zig").Static; |
| 4 | 5 | pub const DateTime = @import("datetime.zig").DateTime; |
| @@ -13,6 +14,9 @@ pub const Block = struct { | ||
| 13 | 14 | cached_content: [256]u8, |
| 14 | 15 | cached_len: usize, |
| 15 | 16 | underline: bool, |
| 17 | + click: ?config_mod.ClickAction = null, | |
| 18 | + x_start: i32 = 0, | |
| 19 | + x_end: i32 = 0, | |
| 16 | 20 | |
| 17 | 21 | pub const Data = union(enum) { |
| 18 | 22 | static: Static, |
+6 −0 src/config/config.zig
| @@ -105,6 +105,11 @@ pub const MouseButton = struct { | ||
| 105 | 105 | action: MouseAction, |
| 106 | 106 | }; |
| 107 | 107 | |
| 108 | +pub const ClickAction = struct { | |
| 109 | + command: []const u8, | |
| 110 | + floating: bool = false, | |
| 111 | +}; | |
| 112 | + | |
| 108 | 113 | pub const Block = struct { |
| 109 | 114 | block_type: BlockType, |
| 110 | 115 | format: []const u8, |
| @@ -118,6 +123,7 @@ pub const Block = struct { | ||
| 118 | 123 | format_full: ?[]const u8 = null, |
| 119 | 124 | battery_name: ?[]const u8 = null, |
| 120 | 125 | thermal_zone: ?[]const u8 = null, |
| 126 | + click: ?ClickAction = null, | |
| 121 | 127 | }; |
| 122 | 128 | |
| 123 | 129 | pub const ColorScheme = struct { |
+33 −2 src/config/lua.zig
| @@ -823,12 +823,17 @@ fn parseBlockConfig(state: *c.lua_State, idx: c_int) ?Block { | ||
| 823 | 823 | const underline = c.lua_toboolean(state, -1) != 0; |
| 824 | 824 | c.lua_settop(state, -2); |
| 825 | 825 | |
| 826 | + _ = c.lua_getfield(state, idx, "click"); | |
| 827 | + const click = parseClickAction(state, -1); | |
| 828 | + c.lua_settop(state, -2); | |
| 829 | + | |
| 826 | 830 | var block = Block{ |
| 827 | 831 | .block_type = .static, |
| 828 | 832 | .format = format, |
| 829 | 833 | .interval = interval, |
| 830 | 834 | .color = color, |
| 831 | 835 | .underline = underline, |
| 836 | + .click = click, | |
| 832 | 837 | }; |
| 833 | 838 | |
| 834 | 839 | if (std.mem.eql(u8, block_type_str, "Ram")) { |
| @@ -949,7 +954,7 @@ fn luaBarBlockStatic(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 949 | 954 | fn luaBarBlockBattery(state: ?*c.lua_State) callconv(.c) c_int { |
| 950 | 955 | const s = state orelse return 0; |
| 951 | 956 | |
| 952 | - c.lua_createtable(s, 0, 6); | |
| 957 | + c.lua_createtable(s, 0, 7); | |
| 953 | 958 | |
| 954 | 959 | _ = c.lua_pushstring(s, "Battery"); |
| 955 | 960 | c.lua_setfield(s, -2, "__block_type"); |
| @@ -966,6 +971,9 @@ fn luaBarBlockBattery(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 966 | 971 | _ = c.lua_getfield(s, 1, "underline"); |
| 967 | 972 | c.lua_setfield(s, -2, "underline"); |
| 968 | 973 | |
| 974 | + _ = c.lua_getfield(s, 1, "click"); | |
| 975 | + c.lua_setfield(s, -2, "click"); | |
| 976 | + | |
| 969 | 977 | c.lua_createtable(s, 0, 4); |
| 970 | 978 | _ = c.lua_getfield(s, 1, "charging"); |
| 971 | 979 | c.lua_setfield(s, -2, "charging"); |
| @@ -981,7 +989,7 @@ fn luaBarBlockBattery(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 981 | 989 | } |
| 982 | 990 | |
| 983 | 991 | fn createBlockTable(state: *c.lua_State, block_type: [*:0]const u8, arg: ?[]const u8) void { |
| 984 | - c.lua_createtable(state, 0, 6); | |
| 992 | + c.lua_createtable(state, 0, 7); | |
| 985 | 993 | |
| 986 | 994 | _ = c.lua_pushstring(state, block_type); |
| 987 | 995 | c.lua_setfield(state, -2, "__block_type"); |
| @@ -998,6 +1006,9 @@ fn createBlockTable(state: *c.lua_State, block_type: [*:0]const u8, arg: ?[]cons | ||
| 998 | 1006 | _ = c.lua_getfield(state, 1, "underline"); |
| 999 | 1007 | c.lua_setfield(state, -2, "underline"); |
| 1000 | 1008 | |
| 1009 | + _ = c.lua_getfield(state, 1, "click"); | |
| 1010 | + c.lua_setfield(state, -2, "click"); | |
| 1011 | + | |
| 1001 | 1012 | if (arg) |a| { |
| 1002 | 1013 | var buf: [256]u8 = undefined; |
| 1003 | 1014 | if (a.len < buf.len) { |
| @@ -1212,6 +1223,26 @@ fn dupeLuaString(state: *c.lua_State, idx: c_int) ?[]const u8 { | ||
| 1212 | 1223 | return duped; |
| 1213 | 1224 | } |
| 1214 | 1225 | |
| 1226 | +fn parseClickAction(state: *c.lua_State, idx: c_int) ?config_mod.ClickAction { | |
| 1227 | + const lua_type = c.lua_type(state, idx); | |
| 1228 | + if (lua_type == c.LUA_TSTRING) { | |
| 1229 | + const cmd = dupeLuaString(state, idx) orelse return null; | |
| 1230 | + return .{ .command = cmd }; | |
| 1231 | + } else if (lua_type == c.LUA_TTABLE) { | |
| 1232 | + _ = c.lua_getfield(state, idx, "command"); | |
| 1233 | + const cmd = dupeLuaString(state, -1); | |
| 1234 | + c.lua_settop(state, -2); | |
| 1235 | + if (cmd == null) return null; | |
| 1236 | + | |
| 1237 | + _ = c.lua_getfield(state, idx, "floating"); | |
| 1238 | + const floating = c.lua_toboolean(state, -1) != 0; | |
| 1239 | + c.lua_settop(state, -2); | |
| 1240 | + | |
| 1241 | + return .{ .command = cmd.?, .floating = floating }; | |
| 1242 | + } | |
| 1243 | + return null; | |
| 1244 | +} | |
| 1245 | + | |
| 1215 | 1246 | fn parseColor(state: *c.lua_State, idx: c_int) u32 { |
| 1216 | 1247 | const lua_type = c.lua_type(state, idx); |
| 1217 | 1248 | if (lua_type == c.LUA_TNUMBER) { |
+5 −0 src/wm/core.zig
| @@ -41,6 +41,11 @@ pub fn manage(win: xlib.Window, window_attrs: *xlib.XWindowAttributes, wm: *Wind | ||
| 41 | 41 | applyRules(client, wm); |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | + if (wm.next_spawn_floating) { | |
| 45 | + client.is_floating = true; | |
| 46 | + wm.next_spawn_floating = false; | |
| 47 | + } | |
| 48 | + | |
| 44 | 49 | const monitor = client.monitor orelse return; |
| 45 | 50 | |
| 46 | 51 | if (client.x + client.width > monitor.win_x + monitor.win_w) { |
+3 −0 src/wm/handlers.zig
| @@ -209,6 +209,9 @@ fn handleButtonPress(event: *xlib.XButtonEvent, wm: *WindowManager) void { | ||
| 209 | 209 | if (clicked_tag) |tag_index| { |
| 210 | 210 | const tag_mask: u32 = @as(u32, 1) << @intCast(tag_index); |
| 211 | 211 | core.view(tag_mask, wm); |
| 212 | + } else if (bar.handleBlockClick(event.x)) |click_action| { | |
| 213 | + wm.next_spawn_floating = click_action.floating; | |
| 214 | + actions.spawnCommand(wm, click_action.command); | |
| 212 | 215 | } |
| 213 | 216 | return; |
| 214 | 217 | } |
+4 −1 src/wm/wm.zig
| @@ -84,6 +84,7 @@ pub const WindowManager = struct { | ||
| 84 | 84 | animation_config: animations.AnimationConfig, |
| 85 | 85 | |
| 86 | 86 | running: bool, |
| 87 | + next_spawn_floating: bool = false, | |
| 87 | 88 | last_motion_monitor: ?*Monitor, |
| 88 | 89 | |
| 89 | 90 | /// Initialises the window manager |
| @@ -553,7 +554,7 @@ pub const WindowManager = struct { | ||
| 553 | 554 | |
| 554 | 555 | /// Converts a config block description into a live status bar block. |
| 555 | 556 | pub fn configBlockToBarBlock(cfg: config_mod.Block) blocks_mod.Block { |
| 556 | - return switch (cfg.block_type) { | |
| 557 | + var block = switch (cfg.block_type) { | |
| 557 | 558 | .static => blocks_mod.Block.initStatic(cfg.format, cfg.color, cfg.underline), |
| 558 | 559 | .datetime => blocks_mod.Block.initDatetime( |
| 559 | 560 | cfg.format, |
| @@ -587,4 +588,6 @@ pub fn configBlockToBarBlock(cfg: config_mod.Block) blocks_mod.Block { | ||
| 587 | 588 | cfg.underline, |
| 588 | 589 | ), |
| 589 | 590 | }; |
| 591 | + block.click = cfg.click; | |
| 592 | + return block; | |
| 590 | 593 | } |
+3 −0 templates/config.lua
| @@ -92,6 +92,9 @@ local blocks = { | ||
| 92 | 92 | interval = 30, |
| 93 | 93 | color = colors.green, |
| 94 | 94 | underline = true, |
| 95 | + -- click: run a command when the block is clicked | |
| 96 | + -- click = "alacritty -e btop", | |
| 97 | + -- click = { command = "bluetui", floating = true }, | |
| 95 | 98 | }), |
| 96 | 99 | }; |
| 97 | 100 |