Skip to content

Commit

Permalink
window!: use nullable width and height
Browse files Browse the repository at this point in the history
Use a nullable width and height when creating a child window
  • Loading branch information
rockorager committed Oct 21, 2024
1 parent 7023889 commit b2a15a7
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 67 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ pub fn main() !void {
const child = win.child(.{
.x_off = win.width / 2 - 20,
.y_off = win.height / 2 - 3,
.width = .{ .limit = 40 },
.height = .{ .limit = 3 },
.width = 40 ,
.height = 3 ,
.border = .{
.where = .all,
.style = style,
Expand Down
16 changes: 8 additions & 8 deletions examples/table.zig
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ pub fn main() !void {
const see_win = win.child(.{
.x_off = 0,
.y_off = 1,
.width = .{ .limit = win.width },
.height = .{ .limit = 4 },
.width = win.width,
.height = 4,
});
see_win.fill(.{ .style = .{ .bg = ctx.bg } });
const content_logo =
Expand Down Expand Up @@ -256,8 +256,8 @@ pub fn main() !void {
const top_bar = win.child(.{
.x_off = 0,
.y_off = 0,
.width = .{ .limit = win.width },
.height = .{ .limit = win.height / top_div },
.width = win.width,
.height = win.height / top_div,
});
for (title_segs[0..]) |*title_seg|
title_seg.style.bg = if (active == .top) selected_bg else other_bg;
Expand All @@ -275,8 +275,8 @@ pub fn main() !void {
const middle_bar = win.child(.{
.x_off = 0,
.y_off = win.height / top_div,
.width = .{ .limit = win.width },
.height = .{ .limit = win.height - (top_bar.height + 1) },
.width = win.width,
.height = win.height - (top_bar.height + 1),
});
if (user_list.items.len > 0) {
demo_tbl.active = active == .mid;
Expand All @@ -294,8 +294,8 @@ pub fn main() !void {
const bottom_bar = win.child(.{
.x_off = 0,
.y_off = win.height - 1,
.width = .{ .limit = win.width },
.height = .{ .limit = 1 },
.width = win.width,
.height = 1,
});
if (active == .btm) bottom_bar.fill(.{ .style = .{ .bg = active_bg } });
cmd_input.draw(bottom_bar);
Expand Down
4 changes: 2 additions & 2 deletions examples/text_input.zig
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ pub fn main() !void {
const child = win.child(.{
.x_off = win.width / 2 - 20,
.y_off = win.height / 2 - 3,
.width = .{ .limit = 40 },
.height = .{ .limit = 3 },
.width = 40,
.height = 3,
.border = .{
.where = .all,
.style = style,
Expand Down
6 changes: 3 additions & 3 deletions examples/view.zig
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ pub fn main() !void {
win.clear();

const controls_win = win.child(.{
.height = .{ .limit = 1 },
.height = 1,
});
_ = controls_win.print(
if (win.width >= 112) &.{
Expand All @@ -155,8 +155,8 @@ pub fn main() !void {
win.child(.{
.y_off = controls_win.height,
.border = .{ .where = .top },
.width = .{ .limit = 45 },
.height = .{ .limit = 15 },
.width = 45,
.height = 15,
})
else
win.child(.{
Expand Down
4 changes: 2 additions & 2 deletions examples/vt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ pub fn main() !void {
const child = win.child(.{
.x_off = 4,
.y_off = 2,
.width = .{ .limit = win.width - 8 },
.height = .{ .limit = win.width - 6 },
.width = 8,
.height = 6,
.border = .{
.where = .all,
},
Expand Down
50 changes: 18 additions & 32 deletions src/Window.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ const gw = @import("gwidth.zig");

const Window = @This();

pub const Size = union(enum) {
expand,
limit: u16,
};

/// horizontal offset from the screen
x_off: u16,
/// vertical offset from the screen
Expand All @@ -32,27 +27,18 @@ fn initChild(
self: Window,
x_off: u16,
y_off: u16,
width: Size,
height: Size,
maybe_width: ?u16,
maybe_height: ?u16,
) Window {
const resolved_width: u16 = switch (width) {
.expand => self.width -| x_off,
.limit => |w| blk: {
if (w + x_off > self.width) {
break :blk self.width -| x_off;
}
break :blk w;
},
};
const resolved_height: u16 = switch (height) {
.expand => self.height -| y_off,
.limit => |h| blk: {
if (h + y_off > self.height) {
break :blk self.height -| y_off;
}
break :blk h;
},
};
const resolved_width: u16 = if (maybe_width) |width|
@min(width, self.width -| x_off)
else
self.width -| x_off;

const resolved_height: u16 = if (maybe_height) |height|
@min(height, self.height -| y_off)
else
self.height -| y_off;
return Window{
.x_off = x_off + self.x_off,
.y_off = y_off + self.y_off,
Expand All @@ -66,9 +52,9 @@ pub const ChildOptions = struct {
x_off: u16 = 0,
y_off: u16 = 0,
/// the width of the resulting child, including any borders
width: Size = .expand,
width: ?u16 = null,
/// the height of the resulting child, including any borders
height: Size = .expand,
height: ?u16 = null,
border: BorderOptions = .{},
};

Expand Down Expand Up @@ -180,7 +166,7 @@ pub fn child(self: Window, opts: ChildOptions) Window {
const w_delt: u16 = if (loc.right) 1 else 0;
const h_ch: u16 = h -| y_off -| h_delt;
const w_ch: u16 = w -| x_off -| w_delt;
return result.initChild(x_off, y_off, .{ .limit = w_ch }, .{ .limit = h_ch });
return result.initChild(x_off, y_off, w_ch, h_ch);
}

/// writes a cell to the location in the window
Expand Down Expand Up @@ -479,7 +465,7 @@ test "Window size set" {
.screen = undefined,
};

const ch = parent.initChild(1, 1, .expand, .expand);
const ch = parent.initChild(1, 1, null, null);
try std.testing.expectEqual(19, ch.width);
try std.testing.expectEqual(19, ch.height);
}
Expand All @@ -493,7 +479,7 @@ test "Window size set too big" {
.screen = undefined,
};

const ch = parent.initChild(0, 0, .{ .limit = 21 }, .{ .limit = 21 });
const ch = parent.initChild(0, 0, 21, 21);
try std.testing.expectEqual(20, ch.width);
try std.testing.expectEqual(20, ch.height);
}
Expand All @@ -507,7 +493,7 @@ test "Window size set too big with offset" {
.screen = undefined,
};

const ch = parent.initChild(10, 10, .{ .limit = 21 }, .{ .limit = 21 });
const ch = parent.initChild(10, 10, 21, 21);
try std.testing.expectEqual(10, ch.width);
try std.testing.expectEqual(10, ch.height);
}
Expand All @@ -521,7 +507,7 @@ test "Window size nested offsets" {
.screen = undefined,
};

const ch = parent.initChild(10, 10, .{ .limit = 21 }, .{ .limit = 21 });
const ch = parent.initChild(10, 10, 21, 21);
try std.testing.expectEqual(11, ch.x_off);
try std.testing.expectEqual(11, ch.y_off);
}
Expand Down
4 changes: 2 additions & 2 deletions src/widgets/CodeView.zig
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ pub fn draw(self: *@This(), win: vaxis.Window, buffer: Buffer, opts: DrawOptions
nl.draw(win.child(.{
.x_off = 0,
.y_off = 0,
.width = .{ .limit = pad_left },
.height = .{ .limit = win.height },
.width = pad_left,
.height = win.height,
}), self.scroll_view.scroll.y);
}
self.drawCode(win.child(.{ .x_off = pad_left }), buffer, opts);
Expand Down
8 changes: 4 additions & 4 deletions src/widgets/ScrollView.zig
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ pub fn draw(self: *@This(), parent: vaxis.Window, content_size: struct {
};
const bg = parent.child(.{
.x_off = parent.width -| opts.character.width,
.width = .{ .limit = opts.character.width },
.height = .{ .limit = parent.height },
.width = opts.character.width,
.height = parent.height,
});
bg.fill(.{ .char = opts.character, .style = opts.bg });
vbar.draw(bg);
Expand Down Expand Up @@ -115,14 +115,14 @@ pub fn bounds(self: *@This(), parent: vaxis.Window) BoundingBox {
pub fn writeCell(self: *@This(), parent: vaxis.Window, col: usize, row: usize, cell: vaxis.Cell) void {
const b = self.bounds(parent);
if (!b.inside(col, row)) return;
const win = parent.child(.{ .width = .{ .limit = b.x2 - b.x1 }, .height = .{ .limit = b.y2 - b.y1 } });
const win = parent.child(.{ .width = b.x2 - b.x1, .height = b.y2 - b.y1 });
win.writeCell(col -| self.scroll.x, row -| self.scroll.y, cell);
}

/// Use this function instead of `Window.readCell` to read the correct cell in scrolling context.
pub fn readCell(self: *@This(), parent: vaxis.Window, col: usize, row: usize) ?vaxis.Cell {
const b = self.bounds(parent);
if (!b.inside(col, row)) return;
const win = parent.child(.{ .width = .{ .limit = b.width }, .height = .{ .limit = b.height } });
const win = parent.child(.{ .width = b.width, .height = b.height });
return win.readCell(col -| self.scroll.x, row -| self.scroll.y);
}
16 changes: 8 additions & 8 deletions src/widgets/Table.zig
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ pub fn drawTable(

const table_win = win.child(.{
.y_off = table_ctx.y_off,
.width = .{ .limit = win.width },
.height = .{ .limit = win.height },
.width = win.width,
.height = win.height,
});

// Headers
Expand All @@ -237,8 +237,8 @@ pub fn drawTable(
const hdr_win = table_win.child(.{
.x_off = col_start,
.y_off = 0,
.width = .{ .limit = col_width },
.height = .{ .limit = 1 },
.width = col_width,
.height = 1,
.border = .{ .where = if (table_ctx.header_borders and idx > 0) .left else .none },
});
var hdr = switch (table_ctx.header_align) {
Expand Down Expand Up @@ -297,8 +297,8 @@ pub fn drawTable(
var row_win = table_win.child(.{
.x_off = 0,
.y_off = @intCast(1 + row + table_ctx.active_y_off),
.width = .{ .limit = table_win.width },
.height = .{ .limit = 1 },
.width = table_win.width,
.height = 1,
//.border = .{ .where = if (table_ctx.row_borders) .top else .none },
});
if (table_ctx.start + row == table_ctx.row) {
Expand Down Expand Up @@ -328,8 +328,8 @@ pub fn drawTable(
const item_win = row_win.child(.{
.x_off = col_start,
.y_off = 0,
.width = .{ .limit = col_width },
.height = .{ .limit = 1 },
.width = col_width,
.height = 1,
.border = .{ .where = if (table_ctx.col_borders and col_idx > 0) .left else .none },
});
const item_txt = switch (ItemT) {
Expand Down
4 changes: 2 additions & 2 deletions src/widgets/View.zig
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ pub fn toWin(self: *View, win: Window, config: RenderConfig) !struct { u16, u16
x = @min(x, self.screen.width -| width);
y = @min(y, self.screen.height -| height);
const child = win.child(.{
.width = .{ .limit = width },
.height = .{ .limit = height },
.width = width,
.height = height,
});
self.draw(child, .{ .x_off = x, .y_off = y });
return .{ x, y };
Expand Down
4 changes: 2 additions & 2 deletions src/widgets/alignment.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub fn center(parent: Window, cols: u16, rows: u16) Window {
return parent.child(.{
.x_off = x_off,
.y_off = y_off,
.width = .{ .limit = cols },
.height = .{ .limit = rows },
.width = cols,
.height = rows,
});
}

0 comments on commit b2a15a7

Please sign in to comment.