window!: use nullable width and height
Use a nullable width and height when creating a child window Fixes: #84
This commit is contained in:
parent
70238897bc
commit
207103e652
11 changed files with 53 additions and 67 deletions
|
@ -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,
|
||||
|
|
|
@ -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 =
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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) &.{
|
||||
|
@ -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(.{
|
||||
|
|
|
@ -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,
|
||||
},
|
||||
|
|
|
@ -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
|
||||
|
@ -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,
|
||||
|
@ -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 = .{},
|
||||
};
|
||||
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
@ -115,7 +115,7 @@ 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);
|
||||
}
|
||||
|
||||
|
@ -123,6 +123,6 @@ pub fn writeCell(self: *@This(), parent: vaxis.Window, col: usize, row: usize, c
|
|||
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);
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
@ -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) {
|
||||
|
@ -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) {
|
||||
|
@ -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) {
|
||||
|
|
|
@ -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 };
|
||||
|
|
|
@ -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,
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue