diff --git a/build.zig b/build.zig index 1bc1b39..6c4f459 100644 --- a/build.zig +++ b/build.zig @@ -39,6 +39,7 @@ pub fn build(b: *std.Build) void { "mouse", "accessibility", "wasm_app", + "layers", "virtual_list", "calendar", "heatmap", diff --git a/examples/layers.zig b/examples/layers.zig new file mode 100644 index 0000000..33bce7f --- /dev/null +++ b/examples/layers.zig @@ -0,0 +1,118 @@ +//! ZigZag Layers Example +//! Demonstrates z-ordered layer compositing with overlapping panels. + +const std = @import("std"); +const zz = @import("zigzag"); + +const Model = struct { + show_popup: bool, + show_tooltip: bool, + + pub const Msg = union(enum) { + key: zz.KeyEvent, + }; + + pub fn init(self: *Model, _: *zz.Context) zz.Cmd(Msg) { + self.* = .{ .show_popup = false, .show_tooltip = false }; + return .none; + } + + 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, + 'p' => self.show_popup = !self.show_popup, + 't' => self.show_tooltip = !self.show_tooltip, + else => {}, + }, + .escape => { + if (self.show_popup) { + self.show_popup = false; + } else if (self.show_tooltip) { + self.show_tooltip = false; + } else { + return .quit; + } + }, + else => {}, + }, + } + return .none; + } + + pub fn view(self: *const Model, ctx: *const zz.Context) []const u8 { + const alloc = ctx.allocator; + const w: u16 = @intCast(@min(ctx.width, std.math.maxInt(u16))); + const h: u16 = @intCast(@min(ctx.height, std.math.maxInt(u16))); + + var stack = zz.layout.layer.LayerStack.init(alloc); + stack.setSize(w, h); + + // Background layer (z=0) + var bg_style = zz.Style{}; + bg_style = bg_style.fg(zz.Color.gray(8)); + bg_style = bg_style.inline_style(true); + + var bg_content = std.array_list.Managed(u8).init(alloc); + for (0..h) |row| { + if (row > 0) bg_content.append('\n') catch {}; + for (0..w) |col| { + const c: u8 = if ((row + col) % 2 == 0) '.' else ' '; + bg_content.append(c) catch {}; + } + } + stack.push(.{ .content = bg_content.items, .z = 0, .transparent = false }) catch {}; + + // Main info panel (z=1) + var info_style = zz.Style{}; + info_style = info_style.borderAll(zz.Border.rounded); + info_style = info_style.borderForeground(zz.Color.cyan()); + info_style = info_style.paddingAll(1); + info_style = info_style.width(40); + info_style = info_style.height(8); + + const info_text = "Layer Compositing Demo\n\np: toggle popup\nt: toggle tooltip\nq: quit"; + const info_panel = info_style.render(alloc, info_text) catch info_text; + stack.push(.{ .content = info_panel, .x = 5, .y = 2, .z = 1 }) catch {}; + + // Popup (z=10) + if (self.show_popup) { + var popup_style = zz.Style{}; + popup_style = popup_style.borderAll(zz.Border.double); + popup_style = popup_style.borderForeground(zz.Color.yellow()); + popup_style = popup_style.paddingAll(1); + popup_style = popup_style.width(30); + popup_style = popup_style.height(5); + + const popup_text = "Modal Popup\n\nThis is on top!\nEsc to close"; + const popup = popup_style.render(alloc, popup_text) catch popup_text; + const px: u16 = if (w > 34) (w - 34) / 2 else 0; + const py: u16 = if (h > 9) (h - 9) / 2 else 0; + stack.push(.{ .content = popup, .x = px, .y = py, .z = 10 }) catch {}; + } + + // Tooltip (z=20) + if (self.show_tooltip) { + var tt_style = zz.Style{}; + tt_style = tt_style.borderAll(zz.Border.normal); + tt_style = tt_style.borderForeground(zz.Color.green()); + tt_style = tt_style.width(20); + + const tt = tt_style.render(alloc, "Tooltip z=20") catch "tooltip"; + stack.push(.{ .content = tt, .x = 15, .y = 5, .z = 20 }) catch {}; + } + + return stack.render(alloc); + } +}; + +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/layout/layer.zig b/src/layout/layer.zig new file mode 100644 index 0000000..ad48236 --- /dev/null +++ b/src/layout/layer.zig @@ -0,0 +1,155 @@ +//! Layer compositing system for z-ordered UI overlays. +//! Manages a stack of rendered text layers and composites them +//! into a single output with proper z-ordering and transparency. + +const std = @import("std"); +const measure = @import("measure.zig"); + +/// A single layer in the stack. +pub const Layer = struct { + /// Rendered content string. + content: []const u8, + /// X position (column offset). + x: u16 = 0, + /// Y position (row offset). + y: u16 = 0, + /// Z-index for ordering. Higher = on top. + z: i16 = 0, + /// If true, space characters are transparent (show layer below). + transparent: bool = true, +}; + +/// Composites multiple layers into a single rendered output. +pub const LayerStack = struct { + allocator: std.mem.Allocator, + layers: std.array_list.Managed(Layer), + width: u16 = 80, + height: u16 = 24, + /// Background character for empty cells. + background: u8 = ' ', + + pub fn init(allocator: std.mem.Allocator) LayerStack { + return .{ + .allocator = allocator, + .layers = std.array_list.Managed(Layer).init(allocator), + }; + } + + pub fn deinit(self: *LayerStack) void { + self.layers.deinit(); + } + + pub fn setSize(self: *LayerStack, w: u16, h: u16) void { + self.width = w; + self.height = h; + } + + pub fn push(self: *LayerStack, layer: Layer) !void { + try self.layers.append(layer); + } + + pub fn clear(self: *LayerStack) void { + self.layers.clearRetainingCapacity(); + } + + /// Composite all layers and return the final rendered string. + pub fn render(self: *const LayerStack, allocator: std.mem.Allocator) []const u8 { + const w: usize = self.width; + const h: usize = self.height; + + // Create cell buffer: each cell stores a byte slice (content) and ANSI state + // For simplicity, we use a 2D grid of cells that stores display characters + const grid = allocator.alloc(Cell, w * h) catch return ""; + + // Fill with background + for (grid) |*cell| { + cell.* = .{ .char = self.background, .ansi_prefix = "" }; + } + + // Sort layers by z-index + const sorted = allocator.alloc(Layer, self.layers.items.len) catch return ""; + @memcpy(sorted, self.layers.items); + std.mem.sort(Layer, sorted, {}, struct { + fn lessThan(_: void, a: Layer, b: Layer) bool { + return a.z < b.z; + } + }.lessThan); + + // Paint each layer onto the grid + for (sorted) |layer| { + self.paintLayer(grid, w, h, layer); + } + + // Render grid to string + var result = std.array_list.Managed(u8).init(allocator); + const writer = result.writer(); + + for (0..h) |row| { + if (row > 0) writer.writeByte('\n') catch {}; + for (0..w) |col| { + const cell = grid[row * w + col]; + if (cell.ansi_prefix.len > 0) { + writer.writeAll(cell.ansi_prefix) catch {}; + writer.writeByte(cell.char) catch {}; + writer.writeAll("\x1b[0m") catch {}; + } else { + writer.writeByte(cell.char) catch {}; + } + } + } + + return result.items; + } + + fn paintLayer(self: *const LayerStack, grid: []Cell, w: usize, h: usize, layer: Layer) void { + _ = self; + const content = layer.content; + var row: usize = layer.y; + var col: usize = layer.x; + var i: usize = 0; + var current_ansi: []const u8 = ""; + + while (i < content.len and row < h) { + if (content[i] == '\n') { + row += 1; + col = layer.x; + i += 1; + continue; + } + + // Detect ANSI escape sequence + if (content[i] == 0x1b and i + 1 < content.len and content[i + 1] == '[') { + const seq_start = i; + i += 2; + while (i < content.len and content[i] != 'm' and content[i] != 'H' and content[i] != 'J' and content[i] != 'K') : (i += 1) {} + if (i < content.len) { + i += 1; + // Check if it's a reset sequence + if (content[seq_start + 2 .. i - 1].len == 1 and content[seq_start + 2] == '0') { + current_ansi = ""; + } else { + current_ansi = content[seq_start..i]; + } + } + continue; + } + + if (col < w) { + const is_transparent = layer.transparent and content[i] == ' ' and current_ansi.len == 0; + if (!is_transparent) { + grid[row * w + col] = .{ + .char = content[i], + .ansi_prefix = current_ansi, + }; + } + col += 1; + } + i += 1; + } + } +}; + +const Cell = struct { + char: u8 = ' ', + ansi_prefix: []const u8 = "", +}; diff --git a/src/layout/layout.zig b/src/layout/layout.zig index 4ed552b..70530a4 100644 --- a/src/layout/layout.zig +++ b/src/layout/layout.zig @@ -4,6 +4,7 @@ pub const measure = @import("measure.zig"); pub const join = @import("join.zig"); pub const place = @import("place.zig"); +pub const layer = @import("layer.zig"); pub const flex = @import("flex.zig"); // Re-export common types