add(PopupMenu): menu to enable space key binding groupings
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 32s

Some minor layout changes of the application
This commit is contained in:
2024-10-09 11:10:40 +02:00
parent 63bef849ec
commit 93ecfbeda0
4 changed files with 115 additions and 46 deletions

80
src/widget/PopupMenu.zig Normal file
View File

@@ -0,0 +1,80 @@
//! Pop-up Menu widget to show the available keybindings
const std = @import("std");
const vaxis = @import("vaxis");
const widget = @import("../widget.zig");
const Event = widget.Event;
allocator: std.mem.Allocator = undefined,
unicode: *const vaxis.Unicode = undefined,
pub fn init(allocator: std.mem.Allocator, unicode: *const vaxis.Unicode) @This() {
return .{
.allocator = allocator,
.unicode = unicode,
};
}
pub fn deinit(this: *@This()) void {
this.* = undefined;
}
/// Update loop for a given widget to react to the provided `Event`. It may
/// change its internal state, update variables, react to user input, etc.
pub fn update(this: *@This(), event: Event) ?Event {
_ = this;
switch (event) {
.key_press => |key| {
if (key.matches('a', .{})) {
// About
return .{ .path = "./doc/about.md" };
}
if (key.matches('h', .{})) {
// Home
return .{ .path = "./doc/home.md" };
}
},
else => {},
}
return null;
}
/// Draw a given widget using the provided `vaxis.Window`. The window controls
/// the dimension one widget may take on the screen. The widget itself has no
/// control over this.
pub fn draw(this: *@This(), win: vaxis.Window) void {
var view = vaxis.widgets.View.init(this.allocator, this.unicode, .{ .width = win.width, .height = win.height }) catch @panic("OOM");
defer view.deinit();
const msg =
\\# Goto
\\
\\*a* about
\\*h* home
;
var col: usize = 0;
var row: usize = 0;
for (msg, 0..) |_, i| {
const cell: vaxis.Cell = .{
// each cell takes a _grapheme_ as opposed to a single
// codepoint. This allows Vaxis to handle emoji properly,
// particularly with terminals that the Unicode Core extension
// (IE Mode 2027)
.char = .{ .grapheme = msg[i .. i + 1] },
.style = .{
.fg = .{ .index = 6 },
.bold = true,
},
};
view.writeCell(col, row, cell);
if (std.mem.eql(u8, cell.char.grapheme, "\n")) {
col = 0;
row += 1;
} else {
col += 1;
}
}
view.draw(win, .{});
}