From dda61ec788ad3224037399d0602e1b98ed026929 Mon Sep 17 00:00:00 2001 From: Janis Date: Wed, 21 Dec 2022 14:17:35 +0100 Subject: [PATCH] can draw dimensions of selection --- box.zig | 20 ++++++++++++++++++++ main.zig | 51 ++++++++++++++++++++++++++++++++------------------- 2 files changed, 52 insertions(+), 19 deletions(-) diff --git a/box.zig b/box.zig index c4b4e44..4441980 100644 --- a/box.zig +++ b/box.zig @@ -161,6 +161,26 @@ pub const Box = struct { return getInner(T, self.position.y); } + /// lowest X-axis value + pub fn getLeftMost(self: *const Self, comptime T: anytype) T { + return getInner(T, self.position.x); + } + + /// highest X-axis value + pub fn getRightMost(self: *const Self, comptime T: anytype) T { + return getInner(T, self.position.x + self.getWidth(isize)); + } + + /// lowest Y-axis value + pub fn getTopMost(self: *const Self, comptime T: anytype) T { + return getInner(T, self.position.y); + } + + /// highest Y-axis value + pub fn getBottomMost(self: *const Self, comptime T: anytype) T { + return getInner(T, self.position.y + self.getHeight(isize)); + } + pub fn getWidth(self: *const Self, comptime T: anytype) T { return getInner(T, self.extents.width); } diff --git a/main.zig b/main.zig index c630c3b..66ba77b 100644 --- a/main.zig +++ b/main.zig @@ -246,8 +246,6 @@ const Output = struct { frame_callback: ?*wl.Callback = null, fn init(self: *Self, state: *State.Init) !void { - std.debug.print("output.init()\n", .{}); - self.state = state; try self.buffers.init(state); @@ -299,6 +297,30 @@ const Output = struct { ); } + fn drawDimensions(self: *Self, c: *Cairo.cairo_t, color: u32, box: *const Box) void { + const opts = self.state.getOptions(); + const font_family = if (opts.@"font-family") |ff| ff.ptr else null; + Cairo.cairo_select_font_face(c, + font_family, + Cairo.CAIRO_FONT_SLANT_NORMAL, + Cairo.CAIRO_FONT_WEIGHT_NORMAL, + ); + Cairo.cairo_set_font_size(c, 14); + setSourceU32(c, color); + var buffer = [_]u8{0} ** 12; + const printed = std.fmt.bufPrint(&buffer, + "{}x{}", + .{box.extents.width, box.extents.height}) catch null; + + if (printed) |str| { + Cairo.cairo_move_to(c, + box.getRightMost(f64) + 10.0, + box.getBottomMost(f64) + 10.0, + ); + Cairo.cairo_show_text(c, @ptrCast([*c]const u8, str.ptr)); + } + } + fn render(self: *Self, buffer: *Buffer) void { const cairo = &buffer.cairo; const opts = self.state.getOptions(); @@ -310,7 +332,6 @@ const Output = struct { // draw in_boxes if (self.state.config.in_boxes) |boxes| { - std.debug.print("in_boxes: {}\n", .{boxes}); var iter = boxes.iter(); while (iter.next()) |box| { if (box.intersects(&self.logical_geometry)) { @@ -318,6 +339,10 @@ const Output = struct { drawRect(buffer, corrected_box, opts.choice); Cairo.cairo_fill(cairo.ctx); + + if (opts.@"display-dimensions") { + self.drawDimensions(cairo.ctx, opts.border, box); + } } } } @@ -343,7 +368,9 @@ const Output = struct { drawRect(buffer, corrected_box, opts.border); Cairo.cairo_stroke(cairo.ctx); - // TODO: draw dimensions + if (opts.@"display-dimensions") { + self.drawDimensions(cairo.ctx, opts.border, &box); + } } } } @@ -411,8 +438,6 @@ const Output = struct { if (self.needs_redraw) { self.commitFrame(); } - - std.debug.print("frame_callback done\n", .{}); }, } } @@ -566,7 +591,6 @@ const Seat = struct { fn init(self: *Self, state: *State.Init) void { self.state = state; - std.debug.print("seat.init()\n", .{}); self.seat.setListener(*Self, seatListener, self); } @@ -719,13 +743,6 @@ const Seat = struct { if (self.keyboard.?.xkb) |xkb_| { const keysym = xkb.xkb_state_key_get_one_sym(xkb_.state, key.key + 8); - // const buf_len = 32; - // const buf = self.state.ally.alloc(u8, buf_len) catch null; - // if (buf) |buffer| { - // _ = xkb.xkb_keysym_get_name(keysym, @ptrCast([*c]u8, buffer), buf_len); - // std.debug.print("key: {s} {s}\n", .{buffer, if (key.state == .pressed) "pressed" else "released"}); - // } - switch (key.state) { .pressed => { switch (keysym) { @@ -1083,7 +1100,6 @@ const State = struct { fn registryListener(registry: *wl.Registry, event: wl.Registry.Event, self: *Self) void { _ = registry; _ = self; - std.debug.print("registryListener called after init", .{}); switch (event) { .global => {}, .global_remove => {}, @@ -1166,7 +1182,7 @@ const Args = struct { selection: u32 = 0x00000000, choice: u32 = 0xFFFFFF40, format: ?[]const u8 = null, - @"font-family": ?[]const u8 = null, + @"font-family": ?[]const u8 = "sans-serif", @"border-weight": u32 = 2, @"single-point": bool = false, @"output-boxes": bool = false, @@ -1251,9 +1267,6 @@ const Input = struct { }; pub fn run(ally: std.mem.Allocator) !void { - // Prints to stderr (it's a shortcut based on `std.io.getStdErr()`) - std.debug.print("All your {s} are belong to us.\n", .{"codebase"}); - const state = try State.Uninit.create(ally); const init = try state.intoInit(); defer init.deinit();