Bar.zig (3351B)
1 const std = @import("std"); 2 const mem = std.mem; 3 4 const wl = @import("wayland").client.wl; 5 const wp = @import("wayland").client.wp; 6 const zwlr = @import("wayland").client.zwlr; 7 8 const Buffer = @import("Buffer.zig"); 9 const Monitor = @import("Monitor.zig"); 10 const render = @import("render.zig"); 11 const Widget = @import("Widget.zig"); 12 const Bar = @This(); 13 14 const state = &@import("root").state; 15 16 monitor: *Monitor, 17 18 layer_surface: *zwlr.LayerSurfaceV1, 19 background: struct { 20 surface: *wl.Surface, 21 viewport: *wp.Viewport, 22 buffer: *wl.Buffer, 23 }, 24 25 tags: Widget, 26 clock: Widget, 27 modules: Widget, 28 29 configured: bool, 30 width: u16, 31 height: u16, 32 33 pub fn create(monitor: *Monitor) !*Bar { 34 const self = try state.gpa.create(Bar); 35 self.monitor = monitor; 36 self.configured = false; 37 38 const compositor = state.wayland.compositor.?; 39 const viewporter = state.wayland.viewporter.?; 40 const spb_manager = state.wayland.single_pixel_buffer_manager.?; 41 const layer_shell = state.wayland.layer_shell.?; 42 43 self.background.surface = try compositor.createSurface(); 44 self.background.viewport = try viewporter.getViewport(self.background.surface); 45 self.background.buffer = try spb_manager.createU32RgbaBuffer(0, 0, 0, 0xffffffff); 46 47 self.layer_surface = try layer_shell.getLayerSurface(self.background.surface, monitor.output, .top, "stevee"); 48 49 self.tags = try Widget.init(self.background.surface); 50 self.clock = try Widget.init(self.background.surface); 51 self.modules = try Widget.init(self.background.surface); 52 53 // setup layer surface 54 self.layer_surface.setSize(0, state.config.height); 55 self.layer_surface.setAnchor( 56 .{ .top = true, .left = true, .right = true, .bottom = false }, 57 ); 58 self.layer_surface.setExclusiveZone(state.config.height); 59 self.layer_surface.setMargin(0, 0, 0, 0); 60 self.layer_surface.setListener(*Bar, layerSurfaceListener, self); 61 62 self.tags.surface.commit(); 63 self.clock.surface.commit(); 64 self.modules.surface.commit(); 65 self.background.surface.commit(); 66 67 return self; 68 } 69 70 pub fn destroy(self: *Bar) void { 71 self.monitor.bar = null; 72 self.layer_surface.destroy(); 73 74 self.background.surface.destroy(); 75 self.background.viewport.destroy(); 76 self.background.buffer.destroy(); 77 78 self.tags.deinit(); 79 self.clock.deinit(); 80 self.modules.deinit(); 81 82 state.gpa.destroy(self); 83 } 84 85 fn layerSurfaceListener( 86 layerSurface: *zwlr.LayerSurfaceV1, 87 event: zwlr.LayerSurfaceV1.Event, 88 bar: *Bar, 89 ) void { 90 switch (event) { 91 .configure => |data| { 92 bar.configured = true; 93 bar.width = @intCast(data.width); 94 bar.height = @intCast(data.height); 95 96 layerSurface.ackConfigure(data.serial); 97 98 const bg = &bar.background; 99 bg.surface.attach(bg.buffer, 0, 0); 100 bg.surface.damageBuffer(0, 0, bar.width, bar.height); 101 bg.viewport.setDestination(bar.width, bar.height); 102 103 render.renderTags(bar) catch return; 104 render.renderClock(bar) catch return; 105 render.renderModules(bar) catch return; 106 107 bar.tags.surface.commit(); 108 bar.clock.surface.commit(); 109 bar.modules.surface.commit(); 110 bar.background.surface.commit(); 111 }, 112 .closed => bar.destroy(), 113 } 114 }