From 796f916025e80539d39336a21d933de7c480a3b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1t=C3=A9=20M=C3=A9sz=C3=A1ros=20=28Laptop=29?= Date: Mon, 23 Mar 2026 14:21:09 +0100 Subject: [PATCH 1/2] feat: add theming system with palettes (catppuccin, dracula, nord) and example --- build.zig | 2 + examples/theming.zig | 164 ++++++++++++++++++++++ src/root.zig | 4 + src/style/theme.zig | 316 ++++++++++++++++++++++++++++++++++++++++++ tests/theme_tests.zig | 90 ++++++++++++ 5 files changed, 576 insertions(+) create mode 100644 examples/theming.zig create mode 100644 src/style/theme.zig create mode 100644 tests/theme_tests.zig diff --git a/build.zig b/build.zig index ceddb1f..66f28be 100644 --- a/build.zig +++ b/build.zig @@ -26,6 +26,7 @@ pub fn build(b: *std.Build) void { "tooltip", "tabs", "clipboard_osc52", + "theming", }; for (examples) |example_name| { @@ -68,6 +69,7 @@ pub fn build(b: *std.Build) void { "tests/tab_group_tests.zig", "tests/chart_tests.zig", "tests/viewport_tests.zig", + "tests/theme_tests.zig", }; const test_step = b.step("test", "Run unit tests"); diff --git a/examples/theming.zig b/examples/theming.zig new file mode 100644 index 0000000..8af9dd6 --- /dev/null +++ b/examples/theming.zig @@ -0,0 +1,164 @@ +//! ZigZag Theming Example +//! Demonstrates switching between theme palettes at runtime. + +const std = @import("std"); +const zz = @import("zigzag"); + +const ThemeChoice = enum { + default_dark, + default_light, + catppuccin_mocha, + catppuccin_latte, + dracula, + nord, + high_contrast, +}; + +const theme_names = [_][]const u8{ + "Default Dark", + "Default Light", + "Catppuccin Mocha", + "Catppuccin Latte", + "Dracula", + "Nord", + "High Contrast", +}; + +const Model = struct { + current_theme: ThemeChoice, + active_theme: zz.Theme, + progress_val: f64, + + pub const Msg = union(enum) { + key: zz.KeyEvent, + tick: zz.msg.Tick, + }; + + pub fn init(self: *Model, _: *zz.Context) zz.Cmd(Msg) { + self.current_theme = .default_dark; + self.active_theme = zz.Theme.fromPalette(zz.Palette.default_dark); + self.progress_val = 35; + return .{ .tick = .{ .every = 100_000_000 } }; + } + + fn getPalette(choice: ThemeChoice) zz.Palette { + return switch (choice) { + .default_dark => zz.Palette.default_dark, + .default_light => zz.Palette.default_light, + .catppuccin_mocha => zz.Palette.catppuccin_mocha, + .catppuccin_latte => zz.Palette.catppuccin_latte, + .dracula => zz.Palette.dracula, + .nord => zz.Palette.nord, + .high_contrast => zz.Palette.high_contrast, + }; + } + + fn nextTheme(self: *Model) void { + const idx = @intFromEnum(self.current_theme); + const next = if (idx + 1 < theme_names.len) idx + 1 else 0; + self.current_theme = @enumFromInt(next); + self.active_theme = zz.Theme.fromPalette(getPalette(self.current_theme)); + } + + fn prevTheme(self: *Model) void { + const idx = @intFromEnum(self.current_theme); + const prev = if (idx > 0) idx - 1 else theme_names.len - 1; + self.current_theme = @enumFromInt(prev); + self.active_theme = zz.Theme.fromPalette(getPalette(self.current_theme)); + } + + pub fn update(self: *Model, msg: Msg, _: *zz.Context) zz.Cmd(Msg) { + switch (msg) { + .key => |k| { + switch (k.key) { + .char => |c| switch (c) { + 'q' => return .quit, + 'n' => self.nextTheme(), + 'p' => self.prevTheme(), + else => {}, + }, + .right => self.nextTheme(), + .left => self.prevTheme(), + .escape => return .quit, + else => {}, + } + }, + .tick => { + self.progress_val += 0.5; + if (self.progress_val > 100) self.progress_val = 0; + }, + } + return .none; + } + + pub fn view(self: *const Model, ctx: *const zz.Context) []const u8 { + const t = &self.active_theme; + const p = &t.palette; + + // Title + const title_style = zz.Theme.boldStyleWith(p.primary); + const title = title_style.render(ctx.allocator, "Theme Preview") catch "Theme Preview"; + + // Theme name + const name_style = zz.Theme.boldStyleWith(p.accent); + const theme_name = name_style.render(ctx.allocator, theme_names[@intFromEnum(self.current_theme)]) catch "?"; + const theme_line = std.fmt.allocPrint(ctx.allocator, "Current: {s}", .{theme_name}) catch "?"; + + // Color swatches + const primary_s = zz.Theme.boldStyleWith(p.primary).render(ctx.allocator, "██ Primary") catch ""; + const secondary_s = zz.Theme.boldStyleWith(p.secondary).render(ctx.allocator, "██ Secondary") catch ""; + const accent_s = zz.Theme.boldStyleWith(p.accent).render(ctx.allocator, "██ Accent") catch ""; + const success_s = zz.Theme.styleWith(p.success).render(ctx.allocator, "██ Success") catch ""; + const warning_s = zz.Theme.styleWith(p.warning).render(ctx.allocator, "██ Warning") catch ""; + const danger_s = zz.Theme.styleWith(p.danger).render(ctx.allocator, "██ Danger") catch ""; + const info_s = zz.Theme.styleWith(p.info).render(ctx.allocator, "██ Info") catch ""; + + // Text styles + const fg_s = zz.Theme.styleWith(p.foreground).render(ctx.allocator, "Foreground text") catch ""; + const muted_s = zz.Theme.styleWith(p.muted).render(ctx.allocator, "Muted text") catch ""; + const subtle_s = zz.Theme.styleWith(p.subtle).render(ctx.allocator, "Subtle text") catch ""; + + // Border preview + var box_style = zz.Style{}; + box_style = box_style.borderAll(zz.Border.rounded); + box_style = box_style.borderForeground(p.border_focus); + box_style = box_style.paddingAll(1); + box_style = box_style.fg(p.foreground); + const box_content = std.fmt.allocPrint(ctx.allocator, "{s}\n{s}\n{s}", .{ fg_s, muted_s, subtle_s }) catch "?"; + const bordered = box_style.render(ctx.allocator, box_content) catch box_content; + + // Progress bar using theme colors + var prog = zz.Progress.init(); + prog.setValue(self.progress_val); + prog.width = 30; + var full_s = zz.Style{}; + full_s = full_s.fg(p.primary); + full_s = full_s.inline_style(true); + prog.full_style = full_s; + var empty_s = zz.Style{}; + empty_s = empty_s.fg(p.subtle); + empty_s = empty_s.inline_style(true); + prog.empty_style = empty_s; + const prog_view = prog.view(ctx.allocator) catch "error"; + + // Help + const help_style = zz.Theme.styleWith(p.muted); + const help = help_style.render(ctx.allocator, "Left/Right or n/p: switch theme | q: quit") catch ""; + + return std.fmt.allocPrint( + ctx.allocator, + "{s}\n{s}\n\n{s} {s} {s}\n{s} {s} {s} {s}\n\n{s}\n\nProgress: {s}\n\n{s}", + .{ title, theme_line, primary_s, secondary_s, accent_s, success_s, warning_s, danger_s, info_s, bordered, prog_view, help }, + ) catch "Error"; + } +}; + +pub fn main() !void { + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer _ = gpa.deinit(); + + var program = try zz.Program(Model).init(gpa.allocator()); + defer program.deinit(); + + try program.run(); +} diff --git a/src/root.zig b/src/root.zig index 3e5a3e7..9b77632 100644 --- a/src/root.zig +++ b/src/root.zig @@ -85,6 +85,10 @@ pub const color = @import("style/color.zig"); pub const Color = color.Color; pub const border = @import("style/border.zig"); pub const Border = border.Border; +pub const theme = @import("style/theme.zig"); +pub const Theme = theme.Theme; +pub const Palette = theme.Palette; +pub const AdaptivePalette = theme.AdaptivePalette; // Layout pub const layout = @import("layout/layout.zig"); diff --git a/src/style/theme.zig b/src/style/theme.zig new file mode 100644 index 0000000..1b4583a --- /dev/null +++ b/src/style/theme.zig @@ -0,0 +1,316 @@ +//! Theming system for ZigZag. +//! Provides centralized color palettes and component-specific themes. + +const Color = @import("color.zig").Color; +const style_mod = @import("style.zig"); + +/// Semantic color palette. Defines named colors for a theme. +pub const Palette = struct { + // Primary colors + primary: Color, + secondary: Color, + accent: Color, + + // Surfaces + background: Color, + surface: Color, + overlay: Color, + + // Text + foreground: Color, + muted: Color, + subtle: Color, + + // Feedback + success: Color, + warning: Color, + danger: Color, + info: Color, + + // Borders + border_color: Color, + border_focus: Color, + + // Highlight + highlight: Color, + highlight_text: Color, + + // ── Built-in presets ────────────────────────────── + + pub const default_dark = Palette{ + .primary = Color.cyan(), + .secondary = Color.magenta(), + .accent = Color.yellow(), + .background = Color.fromRgb(24, 24, 32), + .surface = Color.fromRgb(32, 32, 42), + .overlay = Color.fromRgb(40, 40, 52), + .foreground = Color.white(), + .muted = Color.gray(14), + .subtle = Color.gray(10), + .success = Color.green(), + .warning = Color.yellow(), + .danger = Color.red(), + .info = Color.cyan(), + .border_color = Color.gray(12), + .border_focus = Color.cyan(), + .highlight = Color.fromRgb(60, 60, 80), + .highlight_text = Color.white(), + }; + + pub const default_light = Palette{ + .primary = Color.fromRgb(0, 120, 180), + .secondary = Color.fromRgb(140, 60, 160), + .accent = Color.fromRgb(180, 120, 0), + .background = Color.fromRgb(250, 250, 250), + .surface = Color.fromRgb(240, 240, 240), + .overlay = Color.fromRgb(230, 230, 230), + .foreground = Color.fromRgb(30, 30, 30), + .muted = Color.fromRgb(100, 100, 100), + .subtle = Color.fromRgb(160, 160, 160), + .success = Color.fromRgb(40, 160, 40), + .warning = Color.fromRgb(200, 150, 0), + .danger = Color.fromRgb(200, 40, 40), + .info = Color.fromRgb(0, 120, 180), + .border_color = Color.fromRgb(180, 180, 180), + .border_focus = Color.fromRgb(0, 120, 180), + .highlight = Color.fromRgb(200, 220, 240), + .highlight_text = Color.fromRgb(30, 30, 30), + }; + + pub const catppuccin_mocha = Palette{ + .primary = Color.fromRgb(137, 180, 250), // blue + .secondary = Color.fromRgb(203, 166, 247), // mauve + .accent = Color.fromRgb(249, 226, 175), // yellow + .background = Color.fromRgb(30, 30, 46), // base + .surface = Color.fromRgb(49, 50, 68), // surface0 + .overlay = Color.fromRgb(69, 71, 90), // surface1 + .foreground = Color.fromRgb(205, 214, 244), // text + .muted = Color.fromRgb(166, 173, 200), // subtext0 + .subtle = Color.fromRgb(127, 132, 156), // overlay1 + .success = Color.fromRgb(166, 227, 161), // green + .warning = Color.fromRgb(249, 226, 175), // yellow + .danger = Color.fromRgb(243, 139, 168), // red + .info = Color.fromRgb(137, 180, 250), // blue + .border_color = Color.fromRgb(88, 91, 112), // overlay0 + .border_focus = Color.fromRgb(137, 180, 250), // blue + .highlight = Color.fromRgb(49, 50, 68), // surface0 + .highlight_text = Color.fromRgb(205, 214, 244), // text + }; + + pub const catppuccin_latte = Palette{ + .primary = Color.fromRgb(30, 102, 245), // blue + .secondary = Color.fromRgb(136, 57, 239), // mauve + .accent = Color.fromRgb(223, 142, 29), // yellow + .background = Color.fromRgb(239, 241, 245), // base + .surface = Color.fromRgb(204, 208, 218), // surface0 + .overlay = Color.fromRgb(188, 192, 204), // surface1 + .foreground = Color.fromRgb(76, 79, 105), // text + .muted = Color.fromRgb(108, 111, 133), // subtext0 + .subtle = Color.fromRgb(140, 143, 161), // overlay1 + .success = Color.fromRgb(64, 160, 43), // green + .warning = Color.fromRgb(223, 142, 29), // yellow + .danger = Color.fromRgb(210, 15, 57), // red + .info = Color.fromRgb(30, 102, 245), // blue + .border_color = Color.fromRgb(156, 160, 176), // overlay0 + .border_focus = Color.fromRgb(30, 102, 245), // blue + .highlight = Color.fromRgb(204, 208, 218), // surface0 + .highlight_text = Color.fromRgb(76, 79, 105), // text + }; + + pub const dracula = Palette{ + .primary = Color.fromRgb(189, 147, 249), // purple + .secondary = Color.fromRgb(255, 121, 198), // pink + .accent = Color.fromRgb(241, 250, 140), // yellow + .background = Color.fromRgb(40, 42, 54), // background + .surface = Color.fromRgb(68, 71, 90), // current line + .overlay = Color.fromRgb(68, 71, 90), // current line + .foreground = Color.fromRgb(248, 248, 242), // foreground + .muted = Color.fromRgb(98, 114, 164), // comment + .subtle = Color.fromRgb(98, 114, 164), // comment + .success = Color.fromRgb(80, 250, 123), // green + .warning = Color.fromRgb(255, 184, 108), // orange + .danger = Color.fromRgb(255, 85, 85), // red + .info = Color.fromRgb(139, 233, 253), // cyan + .border_color = Color.fromRgb(98, 114, 164), // comment + .border_focus = Color.fromRgb(189, 147, 249), // purple + .highlight = Color.fromRgb(68, 71, 90), // current line + .highlight_text = Color.fromRgb(248, 248, 242), // foreground + }; + + pub const nord = Palette{ + .primary = Color.fromRgb(136, 192, 208), // nord8 frost + .secondary = Color.fromRgb(129, 161, 193), // nord9 + .accent = Color.fromRgb(235, 203, 139), // nord13 aurora yellow + .background = Color.fromRgb(46, 52, 64), // nord0 polar night + .surface = Color.fromRgb(59, 66, 82), // nord1 + .overlay = Color.fromRgb(67, 76, 94), // nord2 + .foreground = Color.fromRgb(236, 239, 244), // nord6 snow storm + .muted = Color.fromRgb(216, 222, 233), // nord4 + .subtle = Color.fromRgb(76, 86, 106), // nord3 + .success = Color.fromRgb(163, 190, 140), // nord14 aurora green + .warning = Color.fromRgb(235, 203, 139), // nord13 + .danger = Color.fromRgb(191, 97, 106), // nord11 aurora red + .info = Color.fromRgb(136, 192, 208), // nord8 + .border_color = Color.fromRgb(76, 86, 106), // nord3 + .border_focus = Color.fromRgb(136, 192, 208), // nord8 + .highlight = Color.fromRgb(59, 66, 82), // nord1 + .highlight_text = Color.fromRgb(236, 239, 244), // nord6 + }; + + pub const high_contrast = Palette{ + .primary = Color.fromRgb(0, 200, 255), + .secondary = Color.fromRgb(255, 100, 255), + .accent = Color.fromRgb(255, 255, 0), + .background = Color.fromRgb(0, 0, 0), + .surface = Color.fromRgb(20, 20, 20), + .overlay = Color.fromRgb(40, 40, 40), + .foreground = Color.fromRgb(255, 255, 255), + .muted = Color.fromRgb(200, 200, 200), + .subtle = Color.fromRgb(150, 150, 150), + .success = Color.fromRgb(0, 255, 0), + .warning = Color.fromRgb(255, 255, 0), + .danger = Color.fromRgb(255, 0, 0), + .info = Color.fromRgb(0, 200, 255), + .border_color = Color.fromRgb(200, 200, 200), + .border_focus = Color.fromRgb(0, 200, 255), + .highlight = Color.fromRgb(0, 80, 120), + .highlight_text = Color.fromRgb(255, 255, 255), + }; +}; + +/// Adaptive palette that resolves based on dark/light background. +pub const AdaptivePalette = struct { + light: Palette, + dark: Palette, + + pub fn resolve(self: AdaptivePalette, is_dark: bool) Palette { + return if (is_dark) self.dark else self.light; + } + + pub const catppuccin = AdaptivePalette{ + .light = Palette.catppuccin_latte, + .dark = Palette.catppuccin_mocha, + }; + + pub const default = AdaptivePalette{ + .light = Palette.default_light, + .dark = Palette.default_dark, + }; +}; + +/// Theme contains a palette and derived component styles. +pub const Theme = struct { + palette: Palette, + + // Derived styles for components + text: TextTheme, + list: ListTheme, + progress: ProgressTheme, + modal: ModalTheme, + notification: NotificationTheme, + tab: TabTheme, + + pub const TextTheme = struct { + text_fg: Color, + placeholder_fg: Color, + prompt_fg: Color, + border_fg: Color, + border_focus_fg: Color, + }; + + pub const ListTheme = struct { + item_fg: Color, + selected_fg: Color, + cursor_fg: Color, + filter_fg: Color, + }; + + pub const ProgressTheme = struct { + filled_fg: Color, + empty_fg: Color, + percent_fg: Color, + }; + + pub const ModalTheme = struct { + border_fg: Color, + title_fg: Color, + body_fg: Color, + button_fg: Color, + button_active_bg: Color, + }; + + pub const NotificationTheme = struct { + info_fg: Color, + success_fg: Color, + warning_fg: Color, + err_fg: Color, + }; + + pub const TabTheme = struct { + bar_fg: Color, + active_fg: Color, + active_bg: Color, + inactive_fg: Color, + }; + + /// Create a Theme from a Palette with sensible defaults. + pub fn fromPalette(p: Palette) Theme { + return .{ + .palette = p, + .text = .{ + .text_fg = p.foreground, + .placeholder_fg = p.subtle, + .prompt_fg = p.primary, + .border_fg = p.border_color, + .border_focus_fg = p.border_focus, + }, + .list = .{ + .item_fg = p.foreground, + .selected_fg = p.primary, + .cursor_fg = p.secondary, + .filter_fg = p.accent, + }, + .progress = .{ + .filled_fg = p.primary, + .empty_fg = p.subtle, + .percent_fg = p.foreground, + }, + .modal = .{ + .border_fg = p.border_color, + .title_fg = p.foreground, + .body_fg = p.muted, + .button_fg = p.foreground, + .button_active_bg = p.primary, + }, + .notification = .{ + .info_fg = p.info, + .success_fg = p.success, + .warning_fg = p.warning, + .err_fg = p.danger, + }, + .tab = .{ + .bar_fg = p.muted, + .active_fg = p.foreground, + .active_bg = p.primary, + .inactive_fg = p.subtle, + }, + }; + } + + /// Helper to create a Style with foreground color from the theme. + pub fn styleWith(fg: Color) style_mod.Style { + var s = style_mod.Style{}; + s = s.fg(fg); + s = s.inline_style(true); + return s; + } + + /// Helper to create a bold Style with foreground color. + pub fn boldStyleWith(fg: Color) style_mod.Style { + var s = style_mod.Style{}; + s = s.fg(fg); + s = s.bold(true); + s = s.inline_style(true); + return s; + } +}; diff --git a/tests/theme_tests.zig b/tests/theme_tests.zig new file mode 100644 index 0000000..6956ba9 --- /dev/null +++ b/tests/theme_tests.zig @@ -0,0 +1,90 @@ +const std = @import("std"); +const testing = std.testing; +const zz = @import("zigzag"); + +test "Palette default_dark has valid colors" { + const p = zz.Palette.default_dark; + // Check that colors are set (non-default) + try testing.expect(p.primary.toRgb() != null); + try testing.expect(p.foreground.toRgb() != null); + try testing.expect(p.background.toRgb() != null); +} + +test "Palette all presets are valid" { + const palettes = [_]zz.Palette{ + zz.Palette.default_dark, + zz.Palette.default_light, + zz.Palette.catppuccin_mocha, + zz.Palette.catppuccin_latte, + zz.Palette.dracula, + zz.Palette.nord, + zz.Palette.high_contrast, + }; + + for (palettes) |p| { + try testing.expect(p.primary.toRgb() != null); + try testing.expect(p.danger.toRgb() != null); + } +} + +test "Theme fromPalette derives component themes" { + const t = zz.Theme.fromPalette(zz.Palette.dracula); + + // Text theme inherits from palette + const p = zz.Palette.dracula; + try testing.expectEqual(p.foreground, t.text.text_fg); + try testing.expectEqual(p.subtle, t.text.placeholder_fg); + try testing.expectEqual(p.primary, t.text.prompt_fg); + try testing.expectEqual(p.border_color, t.text.border_fg); + try testing.expectEqual(p.border_focus, t.text.border_focus_fg); + + // List theme + try testing.expectEqual(p.foreground, t.list.item_fg); + try testing.expectEqual(p.primary, t.list.selected_fg); + + // Notification theme + try testing.expectEqual(p.info, t.notification.info_fg); + try testing.expectEqual(p.success, t.notification.success_fg); + try testing.expectEqual(p.danger, t.notification.err_fg); +} + +test "AdaptivePalette resolves correctly" { + const adaptive = zz.AdaptivePalette.catppuccin; + + const dark = adaptive.resolve(true); + try testing.expectEqual(zz.Palette.catppuccin_mocha.primary, dark.primary); + + const light = adaptive.resolve(false); + try testing.expectEqual(zz.Palette.catppuccin_latte.primary, light.primary); +} + +test "Theme styleWith creates inline style" { + const s = zz.Theme.styleWith(zz.Color.red()); + + var arena = std.heap.ArenaAllocator.init(testing.allocator); + defer arena.deinit(); + + const rendered = try s.render(arena.allocator(), "hello"); + try testing.expect(rendered.len > 0); +} + +test "Theme boldStyleWith creates bold inline style" { + const s = zz.Theme.boldStyleWith(zz.Color.green()); + + var arena = std.heap.ArenaAllocator.init(testing.allocator); + defer arena.deinit(); + + const rendered = try s.render(arena.allocator(), "bold"); + try testing.expect(rendered.len > 0); +} + +test "Theme can be overridden per-component" { + var t = zz.Theme.fromPalette(zz.Palette.nord); + + // Override list cursor color + t.list.cursor_fg = zz.Color.red(); + try testing.expectEqual(zz.Color.red(), t.list.cursor_fg); + + // Other fields unchanged + try testing.expectEqual(zz.Palette.nord.foreground, t.list.item_fg); +} From 04a4ca2065d32ce221dcc5fd1f93016d2ec0d6b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1t=C3=A9=20M=C3=A9sz=C3=A1ros=20=28Laptop=29?= Date: Mon, 23 Mar 2026 14:35:04 +0100 Subject: [PATCH 2/2] fix: use correct Cmd syntax for tick in theming example --- examples/theming.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/theming.zig b/examples/theming.zig index 8af9dd6..64fecd1 100644 --- a/examples/theming.zig +++ b/examples/theming.zig @@ -38,7 +38,7 @@ const Model = struct { self.current_theme = .default_dark; self.active_theme = zz.Theme.fromPalette(zz.Palette.default_dark); self.progress_val = 35; - return .{ .tick = .{ .every = 100_000_000 } }; + return .{ .every = 100_000_000 }; } fn getPalette(choice: ThemeChoice) zz.Palette {