2 Commits

Author SHA1 Message Date
54c7e19939 fix(container): growth options to dynamically size Containers
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 29s
2025-03-06 22:31:00 +01:00
5457e91b37 fix(container/grow_size): respect vertical / horizontal provided dimensions 2025-03-06 22:07:39 +01:00
2 changed files with 32 additions and 15 deletions

View File

@@ -71,7 +71,12 @@ pub fn main() !void {
.rectangle = .{ .fill = .yellow }, .rectangle = .{ .fill = .yellow },
}, .{})); }, .{}));
} else { } else {
try column.append(try App.Container.init(allocator, .{}, .{})); try column.append(try App.Container.init(allocator, .{
.size = .{
.dim = .{ .y = 4 },
.grow = .horizontal,
},
}, .{}));
} }
try column.append(try App.Container.init(allocator, .{ try column.append(try App.Container.init(allocator, .{
.rectangle = .{ .fill = .blue }, .rectangle = .{ .fill = .blue },

View File

@@ -690,10 +690,10 @@ pub fn Container(comptime Event: type) type {
.fixed => this.properties.size.dim, .fixed => this.properties.size.dim,
.horizontal => .{ .horizontal => .{
.x = @max(size.x, this.properties.size.dim.x), .x = @max(size.x, this.properties.size.dim.x),
.y = size.y, .y = this.properties.size.dim.y,
}, },
.vertical => .{ .vertical => .{
.x = size.x, .x = this.properties.size.dim.x,
.y = @max(size.y, this.properties.size.dim.y), .y = @max(size.y, this.properties.size.dim.y),
}, },
}; };
@@ -750,18 +750,30 @@ pub fn Container(comptime Event: type) type {
var growable_children: usize = 0; var growable_children: usize = 0;
var first_growable_child: *@This() = undefined; var first_growable_child: *@This() = undefined;
for (this.elements.items) |*child| { for (this.elements.items) |*child| {
if (child.properties.size.grow != .fixed) { // layout direction side growth
switch (child.properties.size.grow) {
.fixed => continue,
.both => {
if (growable_children == 0) first_growable_child = child; if (growable_children == 0) first_growable_child = child;
growable_children += 1; growable_children += 1;
switch (layout.direction) {
.horizontal => if (child.properties.size.grow != .vertical) {
child.size.y = available;
}, },
.vertical => if (child.properties.size.grow != .horizontal) { .horizontal => if (layout.direction == .horizontal) {
child.size.x = available; if (growable_children == 0) first_growable_child = child;
growable_children += 1;
},
.vertical => if (layout.direction == .vertical) {
if (growable_children == 0) first_growable_child = child;
growable_children += 1;
}, },
} }
// non layout direction side growth
switch (layout.direction) {
.horizontal => if (child.properties.size.grow == .vertical or child.properties.size.grow == .both) {
child.size.y = available;
},
.vertical => if (child.properties.size.grow == .horizontal or child.properties.size.grow == .both) {
child.size.x = available;
},
} }
} }