From 9029055de00084ba24307a89eafd7f0f2323d717 Mon Sep 17 00:00:00 2001 From: Tim Culverhouse Date: Fri, 19 Jan 2024 10:17:11 -0600 Subject: [PATCH] window: remove reference to parent We don't need to keep references to the parent. We only need to calculate offsets when we initialize a new window. Signed-off-by: Tim Culverhouse --- src/Window.zig | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/Window.zig b/src/Window.zig index 224693e..de1d128 100644 --- a/src/Window.zig +++ b/src/Window.zig @@ -15,7 +15,6 @@ y_off: usize, width: usize, height: usize, -parent: ?*Window, screen: *Screen, /// Creates a new window with offset relative to parent and size clamped to the @@ -46,11 +45,10 @@ pub fn initChild( }, }; return Window{ - .x_off = x_off, - .y_off = y_off, + .x_off = x_off + self.x_off, + .y_off = y_off + self.y_off, .width = resolved_width, .height = resolved_height, - .parent = self, .screen = self.screen, }; } @@ -58,11 +56,7 @@ pub fn initChild( /// writes a cell to the location in the window pub fn writeCell(self: Window, cell: Cell, row: usize, col: usize) void { if (self.h < row or self.w < col) return; - if (self.parent) |p| { - p.writeCell(cell, row + self.y_off, col + self.x_off); - } else { - self.screen.writeCell(cell, row + self.y_off, col + self.x_off); - } + self.screen.writeCell(cell, row + self.y_off, col + self.x_off); } test "Window size set" { @@ -71,7 +65,6 @@ test "Window size set" { .y_off = 0, .width = 20, .height = 20, - .parent = null, .screen = undefined, }; @@ -86,7 +79,6 @@ test "Window size set too big" { .y_off = 0, .width = 20, .height = 20, - .parent = null, .screen = undefined, }; @@ -101,7 +93,6 @@ test "Window size set too big with offset" { .y_off = 0, .width = 20, .height = 20, - .parent = null, .screen = undefined, }; @@ -109,3 +100,17 @@ test "Window size set too big with offset" { try std.testing.expectEqual(10, child.width); try std.testing.expectEqual(10, child.height); } + +test "Window size nested offsets" { + var parent = Window{ + .x_off = 1, + .y_off = 1, + .width = 20, + .height = 20, + .screen = undefined, + }; + + const child = parent.initChild(10, 10, .{ .limit = 21 }, .{ .limit = 21 }); + try std.testing.expectEqual(11, child.x_off); + try std.testing.expectEqual(11, child.y_off); +}