forked from akarpovskii/tuile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.zig
245 lines (216 loc) · 10.7 KB
/
demo.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
const std = @import("std");
const tuile = @import("tuile");
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
pub const tuile_allocator = gpa.allocator();
fn generateStyles() !tuile.Span {
var span = tuile.Span.init(tuile_allocator);
try span.appendPlain("Styles: ");
try span.append(.{ .text = "bold", .style = .{ .add_effect = .{ .bold = true } } });
try span.appendPlain(", ");
try span.append(.{ .text = "italic", .style = .{ .add_effect = .{ .italic = true } } });
try span.appendPlain(", ");
try span.append(.{ .text = "underline", .style = .{ .add_effect = .{ .underline = true } } });
try span.appendPlain(", ");
try span.append(.{ .text = "dim", .style = .{ .add_effect = .{ .dim = true } } });
try span.appendPlain(", ");
try span.append(.{ .text = "blink", .style = .{ .add_effect = .{ .blink = true } } });
try span.appendPlain(", ");
try span.append(.{ .text = "reverse", .style = .{ .add_effect = .{ .reverse = true } } });
try span.appendPlain(", ");
try span.append(.{ .text = "highlight", .style = .{ .add_effect = .{ .highlight = true } } });
return span;
}
fn generateMultilineSpan() !tuile.Span {
var span = tuile.Span.init(tuile_allocator);
try span.append(.{ .style = .{ .fg = tuile.color("red") }, .text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n" });
try span.append(.{ .style = .{ .fg = tuile.color("green") }, .text = "Nullam aliquam mollis sapien, eget pretium dui.\n" });
try span.append(.{ .style = .{ .fg = tuile.color("blue") }, .text = "Nam lobortis turpis ac nunc vehicula cursus in vitae leo.\n" });
try span.append(.{ .style = .{ .fg = tuile.color("magenta") }, .text = "Donec malesuada accumsan tortor at porta." });
return span;
}
const CustomThemeState = struct {
theme: usize = 0,
border: tuile.Border = tuile.Border.all(),
border_type: tuile.BorderType = .solid,
tui: *tuile.Tuile,
pub fn onThemeChange(ptr: ?*anyopaque, idx: usize, state: bool) void {
var self: *CustomThemeState = @ptrCast(@alignCast(ptr.?));
if (state) {
self.theme = idx;
self.updateTheme();
}
}
pub fn onBorderChange(ptr: ?*anyopaque, idx: usize, state: bool) void {
var self: *CustomThemeState = @ptrCast(@alignCast(ptr.?));
switch (idx) {
0 => self.border.top = state,
1 => self.border.right = state,
2 => self.border.bottom = state,
3 => self.border.left = state,
else => unreachable,
}
self.updateBorder();
}
pub fn onBorderTypeChange(ptr: ?*anyopaque, idx: usize, state: bool) void {
var self: *CustomThemeState = @ptrCast(@alignCast(ptr.?));
if (state) {
switch (idx) {
0 => self.border_type = .simple,
1 => self.border_type = .solid,
2 => self.border_type = .rounded,
3 => self.border_type = .double,
else => unreachable,
}
self.updateBorder();
}
}
fn updateTheme(self: CustomThemeState) void {
var theme = switch (self.theme) {
0 => tuile.Theme.amber(),
1 => tuile.Theme.lime(),
2 => tuile.Theme.sky(),
else => unreachable,
};
theme.background_primary = theme.background_secondary;
const themed = self.tui.findByIdTyped(tuile.Themed, "themed") orelse unreachable;
themed.setTheme(theme);
}
fn updateBorder(self: CustomThemeState) void {
const block = self.tui.findByIdTyped(tuile.Block, "borders") orelse unreachable;
block.border = self.border;
block.border_type = self.border_type;
}
};
const UserInputState = struct {
input: []const u8 = "",
tui: *tuile.Tuile,
pub fn onPress(ptr: ?*anyopaque) void {
const self: *UserInputState = @ptrCast(@alignCast(ptr.?));
if (self.input.len > 0) {
const label = self.tui.findByIdTyped(tuile.Label, "user-input") orelse unreachable;
label.setText(self.input) catch unreachable;
}
}
pub fn inputChanged(ptr: ?*anyopaque, value: []const u8) void {
const self: *UserInputState = @ptrCast(@alignCast(ptr.?));
self.input = value;
}
};
pub fn main() !void {
defer _ = gpa.deinit();
var tui = try tuile.Tuile.init(.{});
defer tui.deinit();
var styles = try generateStyles();
defer styles.deinit();
var multiline_span = try generateMultilineSpan();
defer multiline_span.deinit();
var custom_theme_state = CustomThemeState{ .tui = &tui };
var user_input_state = UserInputState{ .tui = &tui };
const layout = tuile.vertical(
.{ .layout = .{ .flex = 1 } },
.{
tuile.label(.{ .span = styles.view() }),
tuile.themed(
.{ .id = "themed", .theme = tuile.Theme.amber() },
tuile.block(
.{
.id = "borders",
.border = custom_theme_state.border,
.border_type = custom_theme_state.border_type,
.padding = .{ .top = 1, .bottom = 1 },
},
tuile.horizontal(.{}, .{
tuile.spacer(.{}),
tuile.vertical(.{}, .{
tuile.label(.{ .text = "Customizable themes:" }),
tuile.checkbox_group(
.{ .multiselect = false, .on_state_change = .{ .cb = @ptrCast(&CustomThemeState.onThemeChange), .payload = &custom_theme_state } },
.{
tuile.checkbox(.{ .text = "Amber", .checked = true, .role = .radio }),
tuile.checkbox(.{ .text = "Lime", .role = .radio }),
tuile.checkbox(.{ .text = "Sky", .role = .radio }),
},
),
}),
tuile.spacer(.{}),
tuile.vertical(.{}, .{
tuile.label(.{ .text = "Borders:" }),
tuile.horizontal(.{}, .{
tuile.checkbox_group(
.{ .multiselect = true, .on_state_change = .{ .cb = @ptrCast(&CustomThemeState.onBorderChange), .payload = &custom_theme_state } },
.{
tuile.checkbox(.{ .text = "Top", .checked = custom_theme_state.border.top }),
tuile.checkbox(.{ .text = "Right", .checked = custom_theme_state.border.right }),
tuile.checkbox(.{ .text = "Bottom", .checked = custom_theme_state.border.bottom }),
tuile.checkbox(.{ .text = "Left", .checked = custom_theme_state.border.left }),
},
),
tuile.spacer(.{ .layout = .{ .max_height = 3, .max_width = 3 } }),
tuile.checkbox_group(
.{ .multiselect = false, .on_state_change = .{ .cb = @ptrCast(&CustomThemeState.onBorderTypeChange), .payload = &custom_theme_state } },
.{
tuile.checkbox(.{ .text = "Simple", .checked = (custom_theme_state.border_type == .simple), .role = .radio }),
tuile.checkbox(.{ .text = "Solid", .checked = (custom_theme_state.border_type == .solid), .role = .radio }),
tuile.checkbox(.{ .text = "Rounded", .checked = (custom_theme_state.border_type == .rounded), .role = .radio }),
tuile.checkbox(.{ .text = "Double", .checked = (custom_theme_state.border_type == .double), .role = .radio }),
},
),
}),
}),
tuile.spacer(.{}),
}),
),
),
tuile.block(
.{
.border = tuile.border.Border.all(),
.border_type = .rounded,
.padding = .{ .top = 1, .bottom = 1, .left = 1, .right = 1 },
},
tuile.vertical(.{}, .{
tuile.label(.{ .text = "Multiline text:" }),
tuile.label(.{ .span = multiline_span.view() }),
}),
),
tuile.label(.{ .text = "Alignment" }),
tuile.horizontal(.{}, .{
tuile.block(
.{ .layout = .{ .flex = 1, .max_height = 6 }, .border = tuile.Border.all() },
tuile.label(.{ .text = "TL", .layout = .{ .alignment = tuile.Align.topLeft() } }),
),
tuile.block(
.{ .layout = .{ .flex = 1, .max_height = 6 }, .border = tuile.Border.all() },
tuile.label(.{ .text = "TR", .layout = .{ .alignment = tuile.Align.topRight() } }),
),
tuile.block(
.{ .layout = .{ .flex = 1, .max_height = 6 }, .border = tuile.Border.all() },
tuile.label(.{ .text = "BL", .layout = .{ .alignment = tuile.Align.bottomLeft() } }),
),
tuile.block(
.{ .layout = .{ .flex = 1, .max_height = 6 }, .border = tuile.Border.all() },
tuile.label(.{ .text = "BR", .layout = .{ .alignment = tuile.Align.bottomRight() } }),
),
}),
tuile.label(.{ .text = "User inputs" }),
tuile.block(
.{ .layout = .{ .flex = 2, .max_height = 5 }, .border = tuile.Border.all() },
tuile.label(.{ .id = "user-input", .text = "" }),
),
tuile.horizontal(.{}, .{
tuile.input(.{
.placeholder = "placeholder",
.layout = .{ .flex = 1 },
.on_value_changed = .{ .cb = UserInputState.inputChanged, .payload = &user_input_state },
}),
tuile.spacer(.{ .layout = .{ .max_width = 1, .max_height = 1 } }),
tuile.button(.{ .text = "Submit", .on_press = .{ .cb = UserInputState.onPress, .payload = &user_input_state } }),
}),
tuile.spacer(.{}),
tuile.label(.{ .text = "Tab/Shift+Tab to move between elements, Space to interract" }),
},
);
try tui.add(layout);
custom_theme_state.updateBorder();
custom_theme_state.updateTheme();
try tui.run();
}