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 | needs_redraw: bool, | 44 | needs_redraw: bool, |
| 45 | next: ?*Bar, | 45 | next: ?*Bar, |
| 46 | systray: ?*Systray, | 46 | systray: ?*Systray, |
| 47 | + systray_underline: bool, | ||
| 48 | + systray_color: u32, | ||
| 47 | 49 | ||
| 48 | /// Creates a bar window for `monitor` using the given config. | 50 | /// Creates a bar window for `monitor` using the given config. |
| 49 | /// Returns null on allocation failure or if the font cannot be loaded. | 51 | /// Returns null on allocation failure or if the font cannot be loaded. |
| @@ -133,6 +135,8 @@ pub const Bar = struct { | |||
| 133 | .needs_redraw = true, | 135 | .needs_redraw = true, |
| 134 | .next = null, | 136 | .next = null, |
| 135 | .systray = systray, | 137 | .systray = systray, |
| 138 | + .systray_underline = false, | ||
| 139 | + .systray_color = 0xffffff, | ||
| 136 | }; | 140 | }; |
| 137 | 141 | ||
| 138 | monitor.bar_win = window; | 142 | monitor.bar_win = window; |
| @@ -160,6 +164,11 @@ pub const Bar = struct { | |||
| 160 | self.blocks.append(self.allocator, block) catch {}; | 164 | self.blocks.append(self.allocator, block) catch {}; |
| 161 | } | 165 | } |
| 162 | 166 | ||
| 167 | + pub fn setSystrayConfig(self: *Bar, underline: bool, col: u32) void { | ||
| 168 | + self.systray_underline = underline; | ||
| 169 | + self.systray_color = col; | ||
| 170 | + } | ||
| 171 | + | ||
| 163 | pub fn clearBlocks(self: *Bar) void { | 172 | pub fn clearBlocks(self: *Bar) void { |
| 164 | self.blocks.clearRetainingCapacity(); | 173 | self.blocks.clearRetainingCapacity(); |
| 165 | } | 174 | } |
| @@ -230,7 +239,11 @@ pub const Bar = struct { | |||
| 230 | } | 239 | } |
| 231 | 240 | ||
| 232 | if (self.systray) |tray| { | 241 | if (self.systray) |tray| { |
| 233 | - tray.updatePosition(self.width - systray_width - padding, 0); | 242 | + const systray_x = self.width - systray_width - padding; |
| 243 | + tray.updatePosition(systray_x, 0); | ||
| 244 | + if (self.systray_underline and systray_width > 0) { | ||
| 245 | + self.fillRect(display, systray_x, self.height - 2, systray_width, 2, self.systray_color); | ||
| 246 | + } | ||
| 234 | } | 247 | } |
| 235 | 248 | ||
| 236 | _ = xlib.XCopyArea(display, self.pixmap, self.window, self.graphics_context, 0, 0, @intCast(self.width), @intCast(self.height), 0, 0); | 249 | _ = 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 | self.display = display; | 56 | self.display = display; |
| 57 | self.screen = screen; | 57 | self.screen = screen; |
| 58 | self.bar_height = bar_height; | 58 | self.bar_height = bar_height; |
| 59 | - self.icon_size = bar_height - 4; | 59 | + self.icon_size = bar_height - 8; |
| 60 | self.icons = .empty; | 60 | self.icons = .empty; |
| 61 | 61 | ||
| 62 | self.net_system_tray_opcode = xlib.XInternAtom(display, "_NET_SYSTEM_TRAY_OPCODE", xlib.False); | 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 | return false; | 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
| @@ -66,6 +66,7 @@ pub const BlockType = enum { | |||
| 66 | shell, | 66 | shell, |
| 67 | battery, | 67 | battery, |
| 68 | cpu_temp, | 68 | cpu_temp, |
| 69 | + systray, | ||
| 69 | }; | 70 | }; |
| 70 | 71 | ||
| 71 | pub const ClickTarget = enum { | 72 | pub const ClickTarget = enum { |
+11 −0 src/config/lua.zig
| @@ -291,6 +291,9 @@ fn registerBarModule(state: *c.lua_State) void { | |||
| 291 | c.lua_pushcfunction(state, luaBarBlockBattery); | 291 | c.lua_pushcfunction(state, luaBarBlockBattery); |
| 292 | c.lua_setfield(state, -2, "battery"); | 292 | c.lua_setfield(state, -2, "battery"); |
| 293 | 293 | ||
| 294 | + c.lua_pushcfunction(state, luaBarBlockSystray); | ||
| 295 | + c.lua_setfield(state, -2, "systray"); | ||
| 296 | + | ||
| 294 | c.lua_setfield(state, -2, "block"); | 297 | c.lua_setfield(state, -2, "block"); |
| 295 | 298 | ||
| 296 | c.lua_setfield(state, -2, "bar"); | 299 | c.lua_setfield(state, -2, "bar"); |
| @@ -853,6 +856,8 @@ fn parseBlockConfig(state: *c.lua_State, idx: c_int) ?Block { | |||
| 853 | c.lua_settop(state, -2); | 856 | c.lua_settop(state, -2); |
| 854 | } | 857 | } |
| 855 | c.lua_settop(state, -2); | 858 | c.lua_settop(state, -2); |
| 859 | + } else if (std.mem.eql(u8, block_type_str, "Systray")) { | ||
| 860 | + block.block_type = .systray; | ||
| 856 | } else { | 861 | } else { |
| 857 | return null; | 862 | return null; |
| 858 | } | 863 | } |
| @@ -928,6 +933,12 @@ fn luaBarBlockStatic(state: ?*c.lua_State) callconv(.c) c_int { | |||
| 928 | return 1; | 933 | return 1; |
| 929 | } | 934 | } |
| 930 | 935 | ||
| 936 | +fn luaBarBlockSystray(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 937 | + const s = state orelse return 0; | ||
| 938 | + createBlockTable(s, "Systray", null); | ||
| 939 | + return 1; | ||
| 940 | +} | ||
| 941 | + | ||
| 931 | fn luaBarBlockBattery(state: ?*c.lua_State) callconv(.c) c_int { | 942 | fn luaBarBlockBattery(state: ?*c.lua_State) callconv(.c) c_int { |
| 932 | const s = state orelse return 0; | 943 | const s = state orelse return 0; |
| 933 | 944 | ||
+11 −0 src/wm/handlers.zig
| @@ -30,6 +30,7 @@ pub fn handleEvent(event: *xlib.XEvent, wm: *WindowManager) void { | |||
| 30 | .motion_notify => handleMotionNotify(&event.xmotion, wm), | 30 | .motion_notify => handleMotionNotify(&event.xmotion, wm), |
| 31 | .client_message => handleClientMessage(&event.xclient, wm), | 31 | .client_message => handleClientMessage(&event.xclient, wm), |
| 32 | .button_press => handleButtonPress(&event.xbutton, wm), | 32 | .button_press => handleButtonPress(&event.xbutton, wm), |
| 33 | + .button_release => handleButtonRelease(&event.xbutton, wm), | ||
| 33 | .expose => handleExpose(&event.xexpose, wm), | 34 | .expose => handleExpose(&event.xexpose, wm), |
| 34 | .property_notify => handlePropertyNotify(&event.xproperty, wm), | 35 | .property_notify => handlePropertyNotify(&event.xproperty, wm), |
| 35 | .mapping_notify => handleMappingNotify(&event.xmapping, wm), | 36 | .mapping_notify => handleMappingNotify(&event.xmapping, wm), |
| @@ -208,6 +209,10 @@ fn handleExpose(event: *xlib.XExposeEvent, wm: *WindowManager) void { | |||
| 208 | fn handleButtonPress(event: *xlib.XButtonEvent, wm: *WindowManager) void { | 209 | fn handleButtonPress(event: *xlib.XButtonEvent, wm: *WindowManager) void { |
| 209 | std.debug.print("button_press: window=0x{x} subwindow=0x{x}\n", .{ event.window, event.subwindow }); | 210 | std.debug.print("button_press: window=0x{x} subwindow=0x{x}\n", .{ event.window, event.subwindow }); |
| 210 | 211 | ||
| 212 | + if (wm.getSystray()) |tray| { | ||
| 213 | + if (tray.handleButtonPress(event)) return; | ||
| 214 | + } | ||
| 215 | + | ||
| 211 | const clicked_monitor = monitor_mod.windowToMonitor(wm, event.window); | 216 | const clicked_monitor = monitor_mod.windowToMonitor(wm, event.window); |
| 212 | if (clicked_monitor) |monitor| { | 217 | if (clicked_monitor) |monitor| { |
| 213 | if (monitor != wm.selected_monitor) { | 218 | if (monitor != wm.selected_monitor) { |
| @@ -436,3 +441,9 @@ fn handleResizeRequest(event: *xlib.XResizeRequestEvent, wm: *WindowManager) voi | |||
| 436 | } | 441 | } |
| 437 | } | 442 | } |
| 438 | } | 443 | } |
| 444 | + | ||
| 445 | +fn handleButtonRelease(event: *xlib.XButtonEvent, wm: *WindowManager) void { | ||
| 446 | + if (wm.getSystray()) |tray| { | ||
| 447 | + _ = tray.handleButtonRelease(event); | ||
| 448 | + } | ||
| 449 | +} | ||
+6 −1 src/wm/wm.zig
| @@ -301,7 +301,11 @@ pub const WindowManager = struct { | |||
| 301 | pub fn populateBarBlocks(self: *WindowManager, bar: *Bar) void { | 301 | pub fn populateBarBlocks(self: *WindowManager, bar: *Bar) void { |
| 302 | if (self.config.blocks.items.len > 0) { | 302 | if (self.config.blocks.items.len > 0) { |
| 303 | for (self.config.blocks.items) |cfg_block| { | 303 | for (self.config.blocks.items) |cfg_block| { |
| 304 | - bar.addBlock(configBlockToBarBlock(cfg_block)); | 304 | + if (cfg_block.block_type == .systray) { |
| 305 | + bar.setSystrayConfig(cfg_block.underline, cfg_block.color); | ||
| 306 | + } else { | ||
| 307 | + bar.addBlock(configBlockToBarBlock(cfg_block)); | ||
| 308 | + } | ||
| 305 | } | 309 | } |
| 306 | } else { | 310 | } else { |
| 307 | bar.addBlock(blocks_mod.Block.initRam("", 5, 0x7aa2f7, true)); | 311 | bar.addBlock(blocks_mod.Block.initRam("", 5, 0x7aa2f7, true)); |
| @@ -597,5 +601,6 @@ pub fn configBlockToBarBlock(cfg: config_mod.Block) blocks_mod.Block { | |||
| 597 | cfg.color, | 601 | cfg.color, |
| 598 | cfg.underline, | 602 | cfg.underline, |
| 599 | ), | 603 | ), |
| 604 | + .systray => blocks_mod.Block.initStatic("", 0, false), | ||
| 600 | }; | 605 | }; |
| 601 | } | 606 | } |