tony / oxwm

feature : click handler on bar blocks Browse files
Iziram committed 2026-03-07 parent 11c0e3a commit 15a6a021def70a8d14dbdbc9ed3c10efa8fb7696
Showing 8 changed files with 70 additions and 3 deletions. UnifiedSplit
+12 −0 src/bar/bar.zig
@@ -217,6 +217,8 @@ pub const Bar = struct {
217 const content = block.getContent(); 217 const content = block.getContent();
218 const content_width = self.textWidth(display, content); 218 const content_width = self.textWidth(display, content);
219 block_x -= content_width; 219 block_x -= content_width;
220 + block.x_start = block_x;
221 + block.x_end = block_x + content_width;
220 self.drawText(display, block_x, @divTrunc(self.height + self.font_height, 2) - 4, content, block.color()); 222 self.drawText(display, block_x, @divTrunc(self.height + self.font_height, 2) - 4, content, block.color());
221 if (block.underline) { 223 if (block.underline) {
222 self.fillRect(display, block_x, self.height - 2, content_width, 2, block.color()); 224 self.fillRect(display, block_x, self.height - 2, content_width, 2, block.color());
@@ -256,6 +258,16 @@ pub const Bar = struct {
256 return null; 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 /// Updates all blocks and marks the bar dirty if any block changed. 271 /// Updates all blocks and marks the bar dirty if any block changed.
260 pub fn updateBlocks(self: *Bar) void { 272 pub fn updateBlocks(self: *Bar) void {
261 var changed = false; 273 var changed = false;
+4 −0 src/bar/blocks/blocks.zig
@@ -1,4 +1,5 @@
1 const std = @import("std"); 1 const std = @import("std");
2 +const config_mod = @import("../../config/config.zig");
2 3
3 pub const Static = @import("static.zig").Static; 4 pub const Static = @import("static.zig").Static;
4 pub const DateTime = @import("datetime.zig").DateTime; 5 pub const DateTime = @import("datetime.zig").DateTime;
@@ -13,6 +14,9 @@ pub const Block = struct {
13 cached_content: [256]u8, 14 cached_content: [256]u8,
14 cached_len: usize, 15 cached_len: usize,
15 underline: bool, 16 underline: bool,
17 + click: ?config_mod.ClickAction = null,
18 + x_start: i32 = 0,
19 + x_end: i32 = 0,
16 20
17 pub const Data = union(enum) { 21 pub const Data = union(enum) {
18 static: Static, 22 static: Static,
+6 −0 src/config/config.zig
@@ -105,6 +105,11 @@ pub const MouseButton = struct {
105 action: MouseAction, 105 action: MouseAction,
106 }; 106 };
107 107
108 +pub const ClickAction = struct {
109 + command: []const u8,
110 + floating: bool = false,
111 +};
112 +
108 pub const Block = struct { 113 pub const Block = struct {
109 block_type: BlockType, 114 block_type: BlockType,
110 format: []const u8, 115 format: []const u8,
@@ -118,6 +123,7 @@ pub const Block = struct {
118 format_full: ?[]const u8 = null, 123 format_full: ?[]const u8 = null,
119 battery_name: ?[]const u8 = null, 124 battery_name: ?[]const u8 = null,
120 thermal_zone: ?[]const u8 = null, 125 thermal_zone: ?[]const u8 = null,
126 + click: ?ClickAction = null,
121 }; 127 };
122 128
123 pub const ColorScheme = struct { 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 const underline = c.lua_toboolean(state, -1) != 0; 823 const underline = c.lua_toboolean(state, -1) != 0;
824 c.lua_settop(state, -2); 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 var block = Block{ 830 var block = Block{
827 .block_type = .static, 831 .block_type = .static,
828 .format = format, 832 .format = format,
829 .interval = interval, 833 .interval = interval,
830 .color = color, 834 .color = color,
831 .underline = underline, 835 .underline = underline,
836 + .click = click,
832 }; 837 };
833 838
834 if (std.mem.eql(u8, block_type_str, "Ram")) { 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 fn luaBarBlockBattery(state: ?*c.lua_State) callconv(.c) c_int { 954 fn luaBarBlockBattery(state: ?*c.lua_State) callconv(.c) c_int {
950 const s = state orelse return 0; 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 _ = c.lua_pushstring(s, "Battery"); 959 _ = c.lua_pushstring(s, "Battery");
955 c.lua_setfield(s, -2, "__block_type"); 960 c.lua_setfield(s, -2, "__block_type");
@@ -966,6 +971,9 @@ fn luaBarBlockBattery(state: ?*c.lua_State) callconv(.c) c_int {
966 _ = c.lua_getfield(s, 1, "underline"); 971 _ = c.lua_getfield(s, 1, "underline");
967 c.lua_setfield(s, -2, "underline"); 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 c.lua_createtable(s, 0, 4); 977 c.lua_createtable(s, 0, 4);
970 _ = c.lua_getfield(s, 1, "charging"); 978 _ = c.lua_getfield(s, 1, "charging");
971 c.lua_setfield(s, -2, "charging"); 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 fn createBlockTable(state: *c.lua_State, block_type: [*:0]const u8, arg: ?[]const u8) void { 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 _ = c.lua_pushstring(state, block_type); 994 _ = c.lua_pushstring(state, block_type);
987 c.lua_setfield(state, -2, "__block_type"); 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 _ = c.lua_getfield(state, 1, "underline"); 1006 _ = c.lua_getfield(state, 1, "underline");
999 c.lua_setfield(state, -2, "underline"); 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 if (arg) |a| { 1012 if (arg) |a| {
1002 var buf: [256]u8 = undefined; 1013 var buf: [256]u8 = undefined;
1003 if (a.len < buf.len) { 1014 if (a.len < buf.len) {
@@ -1212,6 +1223,26 @@ fn dupeLuaString(state: *c.lua_State, idx: c_int) ?[]const u8 {
1212 return duped; 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 fn parseColor(state: *c.lua_State, idx: c_int) u32 { 1246 fn parseColor(state: *c.lua_State, idx: c_int) u32 {
1216 const lua_type = c.lua_type(state, idx); 1247 const lua_type = c.lua_type(state, idx);
1217 if (lua_type == c.LUA_TNUMBER) { 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 applyRules(client, wm); 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 const monitor = client.monitor orelse return; 49 const monitor = client.monitor orelse return;
45 50
46 if (client.x + client.width > monitor.win_x + monitor.win_w) { 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 if (clicked_tag) |tag_index| { 209 if (clicked_tag) |tag_index| {
210 const tag_mask: u32 = @as(u32, 1) << @intCast(tag_index); 210 const tag_mask: u32 = @as(u32, 1) << @intCast(tag_index);
211 core.view(tag_mask, wm); 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 return; 216 return;
214 } 217 }
+4 −1 src/wm/wm.zig
@@ -84,6 +84,7 @@ pub const WindowManager = struct {
84 animation_config: animations.AnimationConfig, 84 animation_config: animations.AnimationConfig,
85 85
86 running: bool, 86 running: bool,
87 + next_spawn_floating: bool = false,
87 last_motion_monitor: ?*Monitor, 88 last_motion_monitor: ?*Monitor,
88 89
89 /// Initialises the window manager 90 /// Initialises the window manager
@@ -553,7 +554,7 @@ pub const WindowManager = struct {
553 554
554 /// Converts a config block description into a live status bar block. 555 /// Converts a config block description into a live status bar block.
555 pub fn configBlockToBarBlock(cfg: config_mod.Block) blocks_mod.Block { 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 .static => blocks_mod.Block.initStatic(cfg.format, cfg.color, cfg.underline), 558 .static => blocks_mod.Block.initStatic(cfg.format, cfg.color, cfg.underline),
558 .datetime => blocks_mod.Block.initDatetime( 559 .datetime => blocks_mod.Block.initDatetime(
559 cfg.format, 560 cfg.format,
@@ -587,4 +588,6 @@ pub fn configBlockToBarBlock(cfg: config_mod.Block) blocks_mod.Block {
587 cfg.underline, 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 interval = 30, 92 interval = 30,
93 color = colors.green, 93 color = colors.green,
94 underline = true, 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