-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcheckbox.zig
136 lines (119 loc) · 4.62 KB
/
checkbox.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const std = @import("std");
const tuile = @import("tuile");
const CheckedState = struct {
const labels_on: [3][]const u8 = .{
"State 1 - 1",
"State 2 - 1",
"State 3 - 1",
};
const labels_off: [3][]const u8 = .{
"State 1 - 0",
"State 2 - 0",
"State 3 - 0",
};
checked: [3]bool = .{ false, false, false },
tui: *tuile.Tuile,
ids: [3][]const u8,
pub fn onGroupState(ptr: ?*CheckedState, idx: usize, state: bool) void {
var self = ptr.?;
self.checked[idx] = state;
self.updateLabels();
}
pub fn onState0(ptr: ?*CheckedState, state: bool) void {
var self = ptr.?;
self.checked[0] = state;
self.updateLabels();
}
pub fn onState1(ptr: ?*CheckedState, state: bool) void {
var self = ptr.?;
self.checked[1] = state;
self.updateLabels();
}
pub fn onState2(ptr: ?*CheckedState, state: bool) void {
var self = ptr.?;
self.checked[2] = state;
self.updateLabels();
}
fn updateLabels(self: CheckedState) void {
var labels: [3][]const u8 = undefined;
for (&labels, labels_on, labels_off, self.checked) |*label, on, off, checked| {
if (checked) {
label.* = on;
} else {
label.* = off;
}
}
for (self.ids, labels) |id, text| {
const label = self.tui.findByIdTyped(tuile.Label, id) orelse unreachable;
label.setText(text) catch unreachable;
}
}
};
pub fn main() !void {
var tui = try tuile.Tuile.init(.{});
defer tui.deinit();
var state1 = CheckedState{ .tui = &tui, .ids = [3][]const u8{ "state-1-1", "state-1-2", "state-1-3" } };
var state2 = CheckedState{ .tui = &tui, .ids = [3][]const u8{ "state-2-1", "state-2-2", "state-2-3" } };
const layout = tuile.horizontal(
.{},
.{
tuile.spacer(.{}),
tuile.block(
.{ .border = tuile.border.Border.all(), .border_type = .double },
tuile.vertical(.{}, .{
tuile.label(.{ .text = "Radio" }),
tuile.checkbox_group(
.{ .multiselect = false },
.{
tuile.checkbox(.{
.text = "Option 1",
.on_state_change = .{ .cb = @ptrCast(&CheckedState.onState0), .payload = &state1 },
}),
tuile.checkbox(.{
.text = "Option 2",
.on_state_change = .{ .cb = @ptrCast(&CheckedState.onState1), .payload = &state1 },
}),
tuile.checkbox(.{
.text = "Option 3",
.on_state_change = .{ .cb = @ptrCast(&CheckedState.onState2), .payload = &state1 },
}),
},
),
tuile.vertical(.{}, .{
tuile.label(.{ .id = state1.ids[0], .text = "" }),
tuile.label(.{ .id = state1.ids[1], .text = "" }),
tuile.label(.{ .id = state1.ids[2], .text = "" }),
}),
}),
),
tuile.spacer(.{ .layout = .{ .max_width = 10, .max_height = 1 } }),
tuile.block(
.{ .border = tuile.border.Border.all(), .border_type = .double },
tuile.vertical(.{}, .{
tuile.label(.{ .text = "Multiselect" }),
tuile.checkbox_group(
.{
.multiselect = true,
.on_state_change = .{ .cb = @ptrCast(&CheckedState.onGroupState), .payload = &state2 },
},
.{
tuile.checkbox(.{ .text = "Option 1" }),
tuile.checkbox(.{ .text = "Option 2" }),
tuile.checkbox(.{ .text = "Option 3" }),
},
),
tuile.vertical(.{}, .{
tuile.label(.{ .id = state2.ids[0], .text = "" }),
tuile.label(.{ .id = state2.ids[1], .text = "" }),
tuile.label(.{ .id = state2.ids[2], .text = "" }),
}),
}),
),
tuile.spacer(.{}),
},
);
try tui.add(layout);
state1.updateLabels();
state2.updateLabels();
try tui.run();
}