Updated systray to support underlines in config.lua
Browse files
+14 −1 src/bar/bar.zig
| @@ -44,6 +44,8 @@ pub const Bar = struct { | ||
| 44 | 44 | needs_redraw: bool, |
| 45 | 45 | next: ?*Bar, |
| 46 | 46 | systray: ?*Systray, |
| 47 | + systray_underline: bool, | |
| 48 | + systray_color: u32, | |
| 47 | 49 | |
| 48 | 50 | /// Creates a bar window for `monitor` using the given config. |
| 49 | 51 | /// Returns null on allocation failure or if the font cannot be loaded. |
| @@ -145,6 +147,8 @@ pub const Bar = struct { | ||
| 145 | 147 | .needs_redraw = true, |
| 146 | 148 | .next = null, |
| 147 | 149 | .systray = systray, |
| 150 | + .systray_underline = false, | |
| 151 | + .systray_color = 0xffffff, | |
| 148 | 152 | }; |
| 149 | 153 | |
| 150 | 154 | monitor.bar_win = window; |
| @@ -171,6 +175,11 @@ pub const Bar = struct { | ||
| 171 | 175 | self.blocks.append(self.allocator, block) catch {}; |
| 172 | 176 | } |
| 173 | 177 | |
| 178 | + pub fn setSystrayConfig(self: *Bar, underline: bool, col: u32) void { | |
| 179 | + self.systray_underline = underline; | |
| 180 | + self.systray_color = col; | |
| 181 | + } | |
| 182 | + | |
| 174 | 183 | pub fn clearBlocks(self: *Bar) void { |
| 175 | 184 | self.blocks.clearRetainingCapacity(); |
| 176 | 185 | } |
| @@ -243,7 +252,11 @@ pub const Bar = struct { | ||
| 243 | 252 | } |
| 244 | 253 | |
| 245 | 254 | if (self.systray) |tray| { |
| 246 | - tray.updatePosition(self.width - systray_width - padding, 0); | |
| 255 | + const systray_x = self.width - systray_width - padding; | |
| 256 | + tray.updatePosition(systray_x, 0); | |
| 257 | + if (self.systray_underline and systray_width > 0) { | |
| 258 | + self.fillRect(display, systray_x, self.height - 2, systray_width, 2, self.systray_color); | |
| 259 | + } | |
| 247 | 260 | } |
| 248 | 261 | |
| 249 | 262 | _ = xlib.XCopyArea(display, self.pixmap, self.window, self.graphics_context, 0, 0, @intCast(self.width), @intCast(self.height), 0, 0); |
+76 −1 src/bar/systray.zig
| @@ -56,7 +56,7 @@ pub const Systray = struct { | ||
| 56 | 56 | self.display = display; |
| 57 | 57 | self.screen = screen; |
| 58 | 58 | self.bar_height = bar_height; |
| 59 | - self.icon_size = bar_height - 4; | |
| 59 | + self.icon_size = bar_height - 8; | |
| 60 | 60 | self.icons = .empty; |
| 61 | 61 | |
| 62 | 62 | self.net_system_tray_opcode = xlib.XInternAtom(display, "_NET_SYSTEM_TRAY_OPCODE", xlib.False); |
| @@ -450,4 +450,79 @@ pub const Systray = struct { | ||
| 450 | 450 | } |
| 451 | 451 | return false; |
| 452 | 452 | } |
| 453 | + | |
| 454 | + pub fn handleButtonPress(self: *Systray, ev: *xlib.XButtonEvent) bool { | |
| 455 | + if (ev.window != self.window) { | |
| 456 | + for (self.icons.items) |icon| { | |
| 457 | + if (icon.window == ev.window) { | |
| 458 | + self.sendButtonEvent(icon.window, ev, true); | |
| 459 | + return true; | |
| 460 | + } | |
| 461 | + } | |
| 462 | + return false; | |
| 463 | + } | |
| 464 | + | |
| 465 | + const spacing: i32 = 4; | |
| 466 | + var x: i32 = 0; | |
| 467 | + for (self.icons.items) |icon| { | |
| 468 | + if (icon.mapped) { | |
| 469 | + if (ev.x >= x and ev.x < x + icon.width) { | |
| 470 | + self.sendButtonEvent(icon.window, ev, true); | |
| 471 | + return true; | |
| 472 | + } | |
| 473 | + x += icon.width + spacing; | |
| 474 | + } | |
| 475 | + } | |
| 476 | + return false; | |
| 477 | + } | |
| 478 | + | |
| 479 | + pub fn handleButtonRelease(self: *Systray, ev: *xlib.XButtonEvent) bool { | |
| 480 | + if (ev.window != self.window) { | |
| 481 | + for (self.icons.items) |icon| { | |
| 482 | + if (icon.window == ev.window) { | |
| 483 | + self.sendButtonEvent(icon.window, ev, false); | |
| 484 | + return true; | |
| 485 | + } | |
| 486 | + } | |
| 487 | + return false; | |
| 488 | + } | |
| 489 | + | |
| 490 | + const spacing: i32 = 4; | |
| 491 | + var x: i32 = 0; | |
| 492 | + for (self.icons.items) |icon| { | |
| 493 | + if (icon.mapped) { | |
| 494 | + if (ev.x >= x and ev.x < x + icon.width) { | |
| 495 | + self.sendButtonEvent(icon.window, ev, false); | |
| 496 | + return true; | |
| 497 | + } | |
| 498 | + x += icon.width + spacing; | |
| 499 | + } | |
| 500 | + } | |
| 501 | + return false; | |
| 502 | + } | |
| 503 | + | |
| 504 | + fn sendButtonEvent(self: *Systray, win: xlib.Window, ev: *xlib.XButtonEvent, is_press: bool) void { | |
| 505 | + var event: xlib.c.XButtonEvent = undefined; | |
| 506 | + event.type = if (is_press) xlib.ButtonPress else xlib.ButtonRelease; | |
| 507 | + event.window = win; | |
| 508 | + event.root = xlib.XRootWindow(self.display, self.screen); | |
| 509 | + event.subwindow = xlib.None; | |
| 510 | + event.time = ev.time; | |
| 511 | + event.x = ev.x; | |
| 512 | + event.y = ev.y; | |
| 513 | + event.x_root = ev.x_root; | |
| 514 | + event.y_root = ev.y_root; | |
| 515 | + event.state = ev.state; | |
| 516 | + event.button = ev.button; | |
| 517 | + event.same_screen = xlib.True; | |
| 518 | + | |
| 519 | + _ = xlib.XSendEvent( | |
| 520 | + self.display, | |
| 521 | + win, | |
| 522 | + xlib.False, | |
| 523 | + xlib.c.ButtonPressMask | xlib.c.ButtonReleaseMask, | |
| 524 | + @ptrCast(&event), | |
| 525 | + ); | |
| 526 | + _ = xlib.XSync(self.display, xlib.False); | |
| 527 | + } | |
| 453 | 528 | }; |
+1 −0 src/config/config.zig
| @@ -67,6 +67,7 @@ pub const BlockType = enum { | ||
| 67 | 67 | shell, |
| 68 | 68 | battery, |
| 69 | 69 | cpu_temp, |
| 70 | + systray, | |
| 70 | 71 | }; |
| 71 | 72 | |
| 72 | 73 | pub const ClickTarget = enum { |
+11 −0 src/config/lua.zig
| @@ -294,6 +294,9 @@ fn registerBarModule(state: *c.lua_State) void { | ||
| 294 | 294 | c.lua_pushcfunction(state, luaBarBlockBattery); |
| 295 | 295 | c.lua_setfield(state, -2, "battery"); |
| 296 | 296 | |
| 297 | + c.lua_pushcfunction(state, luaBarBlockSystray); | |
| 298 | + c.lua_setfield(state, -2, "systray"); | |
| 299 | + | |
| 297 | 300 | c.lua_setfield(state, -2, "block"); |
| 298 | 301 | |
| 299 | 302 | c.lua_setfield(state, -2, "bar"); |
| @@ -882,6 +885,8 @@ fn parseBlockConfig(state: *c.lua_State, idx: c_int) ?Block { | ||
| 882 | 885 | c.lua_settop(state, -2); |
| 883 | 886 | } |
| 884 | 887 | c.lua_settop(state, -2); |
| 888 | + } else if (std.mem.eql(u8, block_type_str, "Systray")) { | |
| 889 | + block.block_type = .systray; | |
| 885 | 890 | } else { |
| 886 | 891 | return null; |
| 887 | 892 | } |
| @@ -957,6 +962,12 @@ fn luaBarBlockStatic(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 957 | 962 | return 1; |
| 958 | 963 | } |
| 959 | 964 | |
| 965 | +fn luaBarBlockSystray(state: ?*c.lua_State) callconv(.c) c_int { | |
| 966 | + const s = state orelse return 0; | |
| 967 | + createBlockTable(s, "Systray", null); | |
| 968 | + return 1; | |
| 969 | +} | |
| 970 | + | |
| 960 | 971 | fn luaBarBlockBattery(state: ?*c.lua_State) callconv(.c) c_int { |
| 961 | 972 | const s = state orelse return 0; |
| 962 | 973 |
+11 −0 src/wm/handlers.zig
| @@ -31,6 +31,7 @@ pub fn handleEvent(event: *xlib.XEvent, wm: *WindowManager) void { | ||
| 31 | 31 | .motion_notify => handleMotionNotify(&event.xmotion, wm), |
| 32 | 32 | .client_message => handleClientMessage(&event.xclient, wm), |
| 33 | 33 | .button_press => handleButtonPress(&event.xbutton, wm), |
| 34 | + .button_release => handleButtonRelease(&event.xbutton, wm), | |
| 34 | 35 | .expose => handleExpose(&event.xexpose, wm), |
| 35 | 36 | .property_notify => handlePropertyNotify(&event.xproperty, wm), |
| 36 | 37 | .mapping_notify => handleMappingNotify(&event.xmapping, wm), |
| @@ -230,6 +231,10 @@ fn handleConfigureNotify(event: *xlib.XConfigureEvent, wm: *WindowManager) void | ||
| 230 | 231 | fn handleButtonPress(event: *xlib.XButtonEvent, wm: *WindowManager) void { |
| 231 | 232 | std.debug.print("button_press: window=0x{x} subwindow=0x{x}\n", .{ event.window, event.subwindow }); |
| 232 | 233 | |
| 234 | + if (wm.getSystray()) |tray| { | |
| 235 | + if (tray.handleButtonPress(event)) return; | |
| 236 | + } | |
| 237 | + | |
| 233 | 238 | const clicked_monitor = monitor_mod.windowToMonitor(wm, event.window); |
| 234 | 239 | if (clicked_monitor) |monitor| { |
| 235 | 240 | if (monitor != wm.selected_monitor) { |
| @@ -462,3 +467,9 @@ fn handleResizeRequest(event: *xlib.XResizeRequestEvent, wm: *WindowManager) voi | ||
| 462 | 467 | } |
| 463 | 468 | } |
| 464 | 469 | } |
| 470 | + | |
| 471 | +fn handleButtonRelease(event: *xlib.XButtonEvent, wm: *WindowManager) void { | |
| 472 | + if (wm.getSystray()) |tray| { | |
| 473 | + _ = tray.handleButtonRelease(event); | |
| 474 | + } | |
| 475 | +} |
+6 −1 src/wm/wm.zig
| @@ -456,7 +456,11 @@ pub const WindowManager = struct { | ||
| 456 | 456 | pub fn populateBarBlocks(self: *WindowManager, bar: *Bar) void { |
| 457 | 457 | if (self.config.blocks.items.len > 0) { |
| 458 | 458 | for (self.config.blocks.items) |cfg_block| { |
| 459 | - bar.addBlock(configBlockToBarBlock(cfg_block)); | |
| 459 | + if (cfg_block.block_type == .systray) { | |
| 460 | + bar.setSystrayConfig(cfg_block.underline, cfg_block.color); | |
| 461 | + } else { | |
| 462 | + bar.addBlock(configBlockToBarBlock(cfg_block)); | |
| 463 | + } | |
| 460 | 464 | } |
| 461 | 465 | } else { |
| 462 | 466 | bar.addBlock(blocks_mod.Block.initRam("", 5, 0x7aa2f7, true)); |
| @@ -752,6 +756,7 @@ pub fn configBlockToBarBlock(cfg: config_mod.Block) blocks_mod.Block { | ||
| 752 | 756 | cfg.color, |
| 753 | 757 | cfg.underline, |
| 754 | 758 | ), |
| 759 | + .systray => blocks_mod.Block.initStatic("", 0, false), | |
| 755 | 760 | }; |
| 756 | 761 | block.click = cfg.click; |
| 757 | 762 | return block; |