add(ViewPort): wrapper for vaxis.widgets.ScrollView
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 31s
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 31s
This commit is contained in:
46
src/main.zig
46
src/main.zig
@@ -61,6 +61,9 @@ pub fn main() !void {
|
||||
var header = try widget.Header.init(alloc, &vx.unicode);
|
||||
defer header.deinit();
|
||||
|
||||
var view_port = widget.ViewPort.init(alloc, &vx.unicode);
|
||||
defer view_port.deinit();
|
||||
|
||||
// Sends queries to terminal to detect certain features. This should always
|
||||
// be called after entering the alt screen, if you are using the alt screen
|
||||
try vx.queryTerminal(tty.anyWriter(), 1 * std.time.ns_per_s);
|
||||
@@ -69,6 +72,7 @@ pub fn main() !void {
|
||||
const event = loop.nextEvent();
|
||||
// update widgets
|
||||
header.update(event);
|
||||
view_port.update(event);
|
||||
|
||||
switch (event) {
|
||||
.key_press => |key| {
|
||||
@@ -81,9 +85,15 @@ pub fn main() !void {
|
||||
} else if (key.matches('l', .{ .ctrl = true })) {
|
||||
vx.queueRefresh();
|
||||
} else if (key.matches(vaxis.Key.enter, .{})) {
|
||||
const title = alloc.alloc(u8, text_input.buf.buffer.len) catch @panic("OOM");
|
||||
@memcpy(title, text_input.buf.buffer);
|
||||
if (loop.tryPostEvent(.{ .title = title })) {
|
||||
var len: usize = 0;
|
||||
for (text_input.buf.buffer) |c| {
|
||||
if (c == 0xaa or c == 0)
|
||||
break;
|
||||
len += 1;
|
||||
}
|
||||
const path = alloc.alloc(u8, len) catch @panic("OOM");
|
||||
@memcpy(path, text_input.buf.buffer[0..len]);
|
||||
if (loop.tryPostEvent(.{ .path = path })) {
|
||||
text_input.clearAndFree();
|
||||
}
|
||||
} else {
|
||||
@@ -91,8 +101,8 @@ pub fn main() !void {
|
||||
}
|
||||
},
|
||||
.winsize => |ws| try vx.resize(alloc, tty.anyWriter(), ws),
|
||||
.title => |*title| {
|
||||
alloc.free(title.*);
|
||||
.path => |*path| {
|
||||
alloc.free(path.*);
|
||||
},
|
||||
}
|
||||
|
||||
@@ -108,25 +118,25 @@ pub fn main() !void {
|
||||
.border = .{ .where = .all },
|
||||
}));
|
||||
|
||||
// Create a style
|
||||
const style: vaxis.Style = .{
|
||||
.fg = .{ .index = color_idx },
|
||||
};
|
||||
|
||||
// Create a bordered child window
|
||||
const child = root_window.child(.{
|
||||
.x_off = root_window.width / 2 - 20,
|
||||
.y_off = root_window.height / 2 - 3,
|
||||
text_input.draw(root_window.child(.{
|
||||
.x_off = 20,
|
||||
.y_off = 3,
|
||||
.width = .{ .limit = 40 },
|
||||
.height = .{ .limit = 3 },
|
||||
.border = .{
|
||||
.where = .all,
|
||||
.style = style,
|
||||
.style = .{ .fg = .{ .index = color_idx } },
|
||||
},
|
||||
});
|
||||
}));
|
||||
|
||||
// Draw the text_input in the child window
|
||||
text_input.draw(child);
|
||||
view_port.draw(root_window.child(.{
|
||||
.x_off = root_window.width / 4,
|
||||
.y_off = 3,
|
||||
.width = .{ .limit = root_window.width / 2 },
|
||||
.border = .{
|
||||
.where = .{ .other = .{ .right = true, .left = true } },
|
||||
},
|
||||
}));
|
||||
|
||||
// Render the screen. Using a buffered writer will offer much better
|
||||
// performance, but is not required
|
||||
|
||||
Reference in New Issue
Block a user