Repository navigation
Expand file tree
/
Copy pathtoast.zig
More file actions
223 lines (202 loc) · 8.83 KB
/
Copy pathtoast.zig
File metadata and controls
223 lines (202 loc) · 8.83 KB
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
//! ZigZag Toast Example
//! Demonstrates the enhanced toast notification system with positioning and styles.
const std = @import("std");
const zz = @import("zigzag");
const Model = struct {
toast: zz.Toast,
msg_counter: usize,
last_elapsed: u64,
width_preset_idx: usize,
border_style_idx: usize,
pub const Msg = union(enum) {
key: zz.KeyEvent,
tick: zz.msg.Tick,
};
pub fn init(self: *Model, ctx: *zz.Context) !zz.Cmd(Msg) {
self.toast = zz.Toast.init(ctx.persistent_allocator);
self.toast.position = .top_right;
self.toast.show_countdown = true;
self.toast.min_width = 24;
self.toast.max_width = 50;
self.msg_counter = 0;
self.last_elapsed = 0;
self.width_preset_idx = 1;
self.border_style_idx = 0;
try // Initial welcome toast
self.toast.push("Welcome to the Toast demo!", .info, 5000, 0);
return .{ .every = 100_000_000 };
}
pub fn update(self: *Model, msg: Msg, ctx: *zz.Context) zz.Cmd(Msg) {
switch (msg) {
.tick => {
self.last_elapsed = ctx.elapsed;
self.toast.update(ctx.elapsed);
},
.key => |k| {
switch (k.key) {
.char => |c| switch (c) {
'q' => return .quit,
'1' => {
self.pushNumberedToast(ctx, .info, "Info message #{d}", 3000);
},
'2' => {
self.pushNumberedToast(ctx, .success, "Success #{d}!", 3000);
},
'3' => {
self.pushNumberedToast(ctx, .warning, "Warning #{d}", 4000);
},
'4' => {
self.pushNumberedToast(ctx, .err, "Error #{d}!", 5000);
},
'5' => {
self.toast.pushPersistent("Persistent notification (press d to dismiss)", .info, ctx.elapsed) catch {};
},
'6' => {
self.msg_counter += 1;
const text = std.fmt.allocPrint(
ctx.allocator,
"Long toast #{d}: right-side stacks now flush correctly, messages are owned by the component, and long lines are trimmed to the configured width instead of spilling awkwardly across the layout.",
.{self.msg_counter},
) catch "Long toast";
self.toast.push(text, .info, 5500, ctx.elapsed) catch {};
},
'd' => self.toast.dismiss(),
'D' => self.toast.dismissAll(),
'x' => self.toast.dismissOldest(),
'b' => self.toast.show_border = !self.toast.show_border,
'i' => self.toast.show_icons = !self.toast.show_icons,
'c' => self.toast.show_countdown = !self.toast.show_countdown,
'w' => self.cycleWidthPreset(),
't' => self.cycleBorderStyle(),
'p', 'P' => {
self.toast.position = switch (self.toast.position) {
.top_left => .top_center,
.top_center => .top_right,
.top_right => .bottom_right,
.bottom_right => .bottom_center,
.bottom_center => .bottom_left,
.bottom_left => .top_left,
};
self.announceSetting(
ctx,
"Toast position: {s}",
.{self.positionName()},
);
},
's' => {
self.toast.stack_order = if (self.toast.stack_order == .newest_first) .oldest_first else .newest_first;
},
else => {},
},
.escape => return .quit,
else => {},
}
},
}
return .none;
}
pub fn view(self: *const Model, ctx: *const zz.Context) ![]const u8 {
var title_style = zz.Style{};
title_style = title_style.bold(true);
title_style = title_style.fg(zz.Color.magenta);
title_style = title_style.inline_style(true);
const title = try title_style.render(ctx.allocator, "Toast Notifications");
const pos_name = self.positionName();
const order_name: []const u8 = if (self.toast.stack_order == .newest_first) "newest first" else "oldest first";
const border_name: []const u8 = switch (self.border_style_idx) {
0 => "rounded",
1 => "normal",
else => "thick",
};
var info_style = zz.Style{};
info_style = info_style.fg(zz.Color.cyan);
info_style = info_style.inline_style(true);
const info = try std.fmt.allocPrint(
ctx.allocator,
"Position: {s} | Order: {s} | Active: {d} | Width: {d}-{d} | Border style: {s} | Borders: {s} | Icons: {s} | Countdown: {s}",
.{
pos_name,
order_name,
self.toast.count(),
self.toast.min_width,
self.toast.max_width,
border_name,
if (self.toast.show_border) "on" else "off",
if (self.toast.show_icons) "on" else "off",
if (self.toast.show_countdown) "on" else "off",
},
);
const styled_info = try info_style.render(ctx.allocator, info);
var help_style = zz.Style{};
help_style = help_style.fg(zz.Color.gray(12));
help_style = help_style.inline_style(true);
const help = try help_style.render(ctx.allocator,
\\Quick start: press p to move the toast around the screen.
\\1: info 2: success 3: warning 4: error 5: persistent 6: long toast
\\d: dismiss newest x: dismiss oldest D: dismiss all
\\b: borders i: icons c: countdown w: width preset t: border style
\\p/P: switch position s: stack order q: quit
);
// Render toast notifications
const toast_view = try self.toast.viewPositioned(ctx.allocator, ctx.width, ctx.height -| 8, self.last_elapsed);
return std.fmt.allocPrint(
ctx.allocator,
"{s}\n{s}\n\n{s}\n\n{s}",
.{ title, styled_info, help, toast_view },
);
}
pub fn deinit(self: *Model) void {
self.toast.deinit();
}
fn pushNumberedToast(self: *Model, ctx: *zz.Context, level: zz.ToastLevel, comptime fmt: []const u8, duration_ms: u64) void {
self.msg_counter += 1;
const text = std.fmt.allocPrint(ctx.allocator, fmt, .{self.msg_counter}) catch return;
self.toast.push(text, level, duration_ms, ctx.elapsed) catch {};
}
fn cycleWidthPreset(self: *Model) void {
self.width_preset_idx = (self.width_preset_idx + 1) % 3;
switch (self.width_preset_idx) {
0 => {
self.toast.min_width = 18;
self.toast.max_width = 34;
},
1 => {
self.toast.min_width = 24;
self.toast.max_width = 50;
},
2 => {
self.toast.min_width = 32;
self.toast.max_width = 72;
},
else => unreachable,
}
}
fn cycleBorderStyle(self: *Model) void {
self.border_style_idx = (self.border_style_idx + 1) % 3;
self.toast.border_chars = switch (self.border_style_idx) {
0 => zz.Border.rounded,
1 => zz.Border.normal,
2 => zz.Border.thick,
else => unreachable,
};
}
fn positionName(self: *const Model) []const u8 {
return switch (self.toast.position) {
.top_left => "top-left",
.top_center => "top-center",
.top_right => "top-right",
.bottom_left => "bottom-left",
.bottom_center => "bottom-center",
.bottom_right => "bottom-right",
};
}
fn announceSetting(self: *Model, ctx: *zz.Context, comptime fmt: []const u8, args: anytype) void {
const text = std.fmt.allocPrint(ctx.allocator, fmt, args) catch return;
self.toast.push(text, .success, 1800, ctx.elapsed) catch {};
}
};
pub fn main(init: std.process.Init) !void {
var program = zz.Program(Model).init(init.gpa, init.io, init.environ_map);
defer program.deinit();
try program.run();
}