Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 94 additions & 25 deletions examples/toast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ 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,
Expand All @@ -18,8 +20,12 @@ const Model = struct {
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;

// Initial welcome toast
self.toast.push("Welcome to the Toast demo!", .info, 5000, 0) catch {};
Expand All @@ -38,34 +44,38 @@ const Model = struct {
.char => |c| switch (c) {
'q' => return .quit,
'1' => {
self.msg_counter += 1;
const text = std.fmt.allocPrint(ctx.persistent_allocator, "Info message #{d}", .{self.msg_counter}) catch "Info";
self.toast.push(text, .info, 3000, ctx.elapsed) catch {};
self.pushNumberedToast(ctx, .info, "Info message #{d}", 3000);
},
'2' => {
self.msg_counter += 1;
const text = std.fmt.allocPrint(ctx.persistent_allocator, "Success #{d}!", .{self.msg_counter}) catch "Success";
self.toast.push(text, .success, 3000, ctx.elapsed) catch {};
self.pushNumberedToast(ctx, .success, "Success #{d}!", 3000);
},
'3' => {
self.msg_counter += 1;
const text = std.fmt.allocPrint(ctx.persistent_allocator, "Warning #{d}", .{self.msg_counter}) catch "Warning";
self.toast.push(text, .warning, 4000, ctx.elapsed) catch {};
self.pushNumberedToast(ctx, .warning, "Warning #{d}", 4000);
},
'4' => {
self.msg_counter += 1;
const text = std.fmt.allocPrint(ctx.persistent_allocator, "Error #{d}!", .{self.msg_counter}) catch "Error";
self.toast.push(text, .err, 5000, ctx.elapsed) catch {};
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,
'p' => {
'w' => self.cycleWidthPreset(),
't' => self.cycleBorderStyle(),
'p', 'P' => {
self.toast.position = switch (self.toast.position) {
.top_left => .top_center,
.top_center => .top_right,
Expand All @@ -74,6 +84,11 @@ const Model = struct {
.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;
Expand All @@ -95,26 +110,27 @@ const Model = struct {
title_style = title_style.inline_style(true);
const title = title_style.render(ctx.allocator, "Toast Notifications") catch "Toast";

const pos_name: []const u8 = 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",
};
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 = std.fmt.allocPrint(
ctx.allocator,
"Position: {s} | Order: {s} | Active: {d} | Borders: {s} | Icons: {s} | Countdown: {s}",
"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",
Expand All @@ -126,9 +142,11 @@ const Model = struct {
help_style = help_style.fg(zz.Color.gray(12));
help_style = help_style.inline_style(true);
const help = help_style.render(ctx.allocator,
\\1: info 2: success 3: warning 4: error 5: persistent
\\d: dismiss D: dismiss all b: borders i: icons c: countdown
\\p: cycle position s: stack order q: quit
\\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
) catch "";

// Render toast notifications
Expand All @@ -144,6 +162,57 @@ const Model = struct {
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() !void {
Expand Down
Loading
Loading