Something went wrong. Try again.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485//! A full-width block that fills its background and insets its child by one row//! above and below and one cell left and right.
const std = @import("std");const vaxis = @import("vaxis");
const vxfw = vaxis.vxfw;
const testing = std.testing;const cellAt = @import("testing.zig").cellAt;
pub const Block = @This();
style: vaxis.Style = .{},child: vxfw.Widget,
pub fn widget(self: *const Block) vxfw.Widget { return .{ .userdata = @constCast(self), .drawFn = typeErasedDrawFn, };}
fn typeErasedDrawFn(ptr: *anyopaque, ctx: vxfw.DrawContext) std.mem.Allocator.Error!vxfw.Surface { const self: *const Block = @ptrCast(@alignCast(ptr)); return self.draw(ctx);}
pub fn draw(self: *const Block, ctx: vxfw.DrawContext) std.mem.Allocator.Error!vxfw.Surface { const width = ctx.max.width orelse return vxfw.Surface.empty(self.widget()); const inner = width -| 2; const child = try self.child.draw(ctx.withConstraints( .{ .width = inner, .height = 0 }, .{ .width = inner, .height = null }, )); const height = child.size.height + 2; // One buffer for the whole block could pass the vxfw limit of 65535 cells, // so the background is drawn in bands of rows. const band_rows: u16 = @max(1, std.math.maxInt(u16) / @max(width, 1)); const bands = std.math.divCeil(u16, height, band_rows) catch unreachable; const children = try ctx.arena.alloc(vxfw.SubSurface, bands + 1); for (children[0..bands], 0..) |*band, i| { const row: u16 = @intCast(i * band_rows); const surface = try vxfw.Surface.init(ctx.arena, self.widget(), .{ .width = width, .height = @min(band_rows, height - row) }); @memset(surface.buffer, .{ .style = self.style }); band.* = .{ .origin = .{ .row = row, .col = 0 }, .surface = surface }; } children[bands] = .{ .origin = .{ .row = 1, .col = 1 }, .surface = child }; return .{ .size = .{ .width = width, .height = height }, .widget = self.widget(), .buffer = &.{}, .children = children, };}
test "the block pads its child with its background on every side" { var arena_state = std.heap.ArenaAllocator.init(testing.allocator); defer arena_state.deinit(); const arena = arena_state.allocator(); vxfw.DrawContext.init(.wcwidth);
const text: vxfw.Text = .{ .text = "hi", .width_basis = .parent }; const block: Block = .{ .style = .{ .bg = .{ .index = 8 } }, .child = text.widget() }; const surface = try block.widget().draw(.{ .arena = arena, .min = .{}, .max = .{ .width = 6, .height = null }, .cell_size = .{}, });
try testing.expectEqual(@as(u16, 6), surface.size.width); try testing.expectEqual(@as(u16, 3), surface.size.height); for (0..3) |row| for (0..6) |col| { if (row == 1 and col > 0 and col < 5) { continue; } try testing.expectEqual(@as(u8, 8), cellAt(surface, @intCast(col), @intCast(row)).?.style.bg.index); }; try testing.expectEqualStrings(" ", cellAt(surface, 0, 1).?.char.grapheme); try testing.expectEqualStrings("h", cellAt(surface, 1, 1).?.char.grapheme); try testing.expectEqualStrings("i", cellAt(surface, 2, 1).?.char.grapheme); try testing.expectEqualStrings(" ", cellAt(surface, 5, 1).?.char.grapheme);}