diff --git a/src/container.zig b/src/container.zig index dd2cef4..badd11f 100644 --- a/src/container.zig +++ b/src/container.zig @@ -595,7 +595,7 @@ pub fn Container(comptime Event: type) type { const len: u16 = @truncate(this.elements.items.len); if (len > 0) { for (this.elements.items) |element| { - size = size.merge(element.minSize()); + size = size.add(element.minSize()); } var gap = this.properties.layout.gap; if (this.properties.layout.separator.enabled) gap += 1; @@ -605,10 +605,7 @@ pub fn Container(comptime Event: type) type { .vertical => size.rows += gap * (len - 1), } } - return .{ - .cols = @max(size.cols, this.properties.min_size.cols), - .rows = @max(size.rows, this.properties.min_size.rows), - }; + return size.max(this.properties.min_size); } pub fn handle(this: *@This(), event: Event) !void { diff --git a/src/element.zig b/src/element.zig index b33b647..d99da56 100644 --- a/src/element.zig +++ b/src/element.zig @@ -86,12 +86,8 @@ pub fn Scrollable(Event: type) type { .resize => |size| { this.size = size; // TODO: scrollbar space - depending on configuration and only if necessary? - const min_size = this.container.minSize(); - this.container_size = .{ - .anchor = size.anchor, - .cols = @max(min_size.cols, size.cols), - .rows = @max(min_size.rows, size.rows), - }; + this.container_size = size.max(this.container.minSize()); + this.container_size.anchor = size.anchor; try this.container.handle(.{ .resize = this.container_size }); }, // TODO: other means to scroll except with the mouse? (i.e. Ctrl-u/d, k/j, etc.?) diff --git a/src/size.zig b/src/size.zig index 2147e29..51a219e 100644 --- a/src/size.zig +++ b/src/size.zig @@ -3,12 +3,61 @@ pub const Size = packed struct { cols: u16 = 0, rows: u16 = 0, - pub fn merge(this: @This(), other: @This()) Size { + pub fn add(this: @This(), other: @This()) Size { return .{ .cols = this.cols + other.cols, .rows = this.rows + other.rows, }; } + + pub fn max(this: @This(), other: @This()) Size { + return .{ + .cols = @max(this.cols, other.cols), + .rows = @max(this.rows, other.rows), + }; + } + + test "adding" { + const testing = @import("std").testing; + + const a: @This() = .{ + .anchor = .{ .col = 1, .row = 2 }, + .cols = 10, + .rows = 20, + }; + + const b: @This() = .{ + .anchor = .{ .col = 5, .row = 20 }, + .cols = 20, + .rows = 10, + }; + + try testing.expectEqual(@This(){ + .cols = 30, + .rows = 30, + }, a.add(b)); + } + + test "maximum" { + const testing = @import("std").testing; + + const a: @This() = .{ + .anchor = .{ .col = 1, .row = 2 }, + .cols = 10, + .rows = 20, + }; + + const b: @This() = .{ + .anchor = .{ .col = 5, .row = 20 }, + .cols = 20, + .rows = 10, + }; + + try testing.expectEqual(@This(){ + .cols = 20, + .rows = 20, + }, a.max(b)); + } }; pub const Position = packed struct {