forked from akarpovskii/tuile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.zig
67 lines (57 loc) · 1.81 KB
/
input.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const std = @import("std");
const tuile = @import("tuile");
const ListApp = struct {
input: ?[]const u8 = null,
tui: *tuile.Tuile,
pub fn onPress(opt_self: ?*ListApp) void {
const self = opt_self.?;
if (self.input) |input| {
if (input.len > 0) {
const list = self.tui.findByIdTyped(tuile.StackLayout, "list-id") orelse unreachable;
list.addChild(tuile.label(.{ .text = input })) catch unreachable;
}
}
}
pub fn inputChanged(opt_self: ?*ListApp, value: []const u8) void {
const self = opt_self.?;
self.input = value;
}
};
pub fn main() !void {
var tui = try tuile.Tuile.init(.{});
defer tui.deinit();
var list_app = ListApp{ .tui = &tui };
const layout = tuile.vertical(
.{ .layout = .{ .flex = 1 } },
.{
tuile.block(
.{ .border = tuile.border.Border.all(), .layout = .{ .flex = 1 } },
tuile.vertical(
.{ .id = "list-id" },
.{},
),
),
tuile.horizontal(
.{},
.{
tuile.input(.{
.layout = .{ .flex = 1 },
.on_value_changed = .{
.cb = @ptrCast(&ListApp.inputChanged),
.payload = &list_app,
},
}),
tuile.button(.{
.text = "Submit",
.on_press = .{
.cb = @ptrCast(&ListApp.onPress),
.payload = &list_app,
},
}),
},
),
},
);
try tui.add(layout);
try tui.run();
}