From 54c7e199390e3d35f5da1a57911368bf65a511af Mon Sep 17 00:00:00 2001 From: Yves Biener Date: Thu, 6 Mar 2025 22:31:00 +0100 Subject: [PATCH] fix(container): growth options to dynamically size `Container`s --- examples/layouts/mixed.zig | 4 ++-- src/container.zig | 36 ++++++++++++++++++++++++------------ 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/examples/layouts/mixed.zig b/examples/layouts/mixed.zig index e4251cc..8774dae 100644 --- a/examples/layouts/mixed.zig +++ b/examples/layouts/mixed.zig @@ -73,8 +73,8 @@ pub fn main() !void { } else { try column.append(try App.Container.init(allocator, .{ .size = .{ - .dim = .{ .y = 5 }, - // .grow = .horizontal, + .dim = .{ .y = 4 }, + .grow = .horizontal, }, }, .{})); } diff --git a/src/container.zig b/src/container.zig index adef5d5..12acc6b 100644 --- a/src/container.zig +++ b/src/container.zig @@ -750,18 +750,30 @@ pub fn Container(comptime Event: type) type { var growable_children: usize = 0; var first_growable_child: *@This() = undefined; for (this.elements.items) |*child| { - if (child.properties.size.grow != .fixed) { - if (growable_children == 0) first_growable_child = child; - growable_children += 1; - - switch (layout.direction) { - .horizontal => if (child.properties.size.grow != .vertical) { - child.size.y = available; - }, - .vertical => if (child.properties.size.grow != .horizontal) { - child.size.x = available; - }, - } + // layout direction side growth + switch (child.properties.size.grow) { + .fixed => continue, + .both => { + if (growable_children == 0) first_growable_child = child; + growable_children += 1; + }, + .horizontal => if (layout.direction == .horizontal) { + 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; + }, } }