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 <tim@timculverhouse.com>
This commit is contained in:
Tim Culverhouse 2024-01-19 10:17:11 -06:00
parent c3964fec43
commit 9029055de0

View file

@ -15,7 +15,6 @@ y_off: usize,
width: usize, width: usize,
height: usize, height: usize,
parent: ?*Window,
screen: *Screen, screen: *Screen,
/// Creates a new window with offset relative to parent and size clamped to the /// Creates a new window with offset relative to parent and size clamped to the
@ -46,11 +45,10 @@ pub fn initChild(
}, },
}; };
return Window{ return Window{
.x_off = x_off, .x_off = x_off + self.x_off,
.y_off = y_off, .y_off = y_off + self.y_off,
.width = resolved_width, .width = resolved_width,
.height = resolved_height, .height = resolved_height,
.parent = self,
.screen = self.screen, .screen = self.screen,
}; };
} }
@ -58,12 +56,8 @@ pub fn initChild(
/// writes a cell to the location in the window /// writes a cell to the location in the window
pub fn writeCell(self: Window, cell: Cell, row: usize, col: usize) void { pub fn writeCell(self: Window, cell: Cell, row: usize, col: usize) void {
if (self.h < row or self.w < col) return; 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" { test "Window size set" {
var parent = Window{ var parent = Window{
@ -71,7 +65,6 @@ test "Window size set" {
.y_off = 0, .y_off = 0,
.width = 20, .width = 20,
.height = 20, .height = 20,
.parent = null,
.screen = undefined, .screen = undefined,
}; };
@ -86,7 +79,6 @@ test "Window size set too big" {
.y_off = 0, .y_off = 0,
.width = 20, .width = 20,
.height = 20, .height = 20,
.parent = null,
.screen = undefined, .screen = undefined,
}; };
@ -101,7 +93,6 @@ test "Window size set too big with offset" {
.y_off = 0, .y_off = 0,
.width = 20, .width = 20,
.height = 20, .height = 20,
.parent = null,
.screen = undefined, .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.width);
try std.testing.expectEqual(10, child.height); 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);
}