Skip to content

Commit

Permalink
fix layouting
Browse files Browse the repository at this point in the history
  • Loading branch information
zenith391 committed Nov 10, 2024
1 parent 0b32562 commit bbdeb32
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion examples/border-layout.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const capy = @import("capy");
pub usingnamespace capy.cross_platform;

pub fn main() !void {
try capy.backend.init();
try capy.init();

var window = try capy.Window.init();
try window.set(capy.column(.{}, .{
Expand Down
4 changes: 2 additions & 2 deletions src/components/Alignment.zig
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ pub const Alignment = struct {
const preferred_size = self.child.get().getPreferredSize(available);
const final_size = Size.intersect(preferred_size, available);

const x: u32 = @intFromFloat(alignX * available.width - final_size.width);
const y: u32 = @intFromFloat(alignY * available.height - final_size.height);
const x: u32 = @intFromFloat(alignX * @max(0, available.width - final_size.width));
const y: u32 = @intFromFloat(alignY * @max(0, available.height - final_size.height));

peer.move(widget_peer, x, y);
peer.resize(widget_peer, @intFromFloat(final_size.width), @intFromFloat(final_size.height));
Expand Down
8 changes: 4 additions & 4 deletions src/containers.zig
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub fn ColumnLayout(peer: Callbacks, widgets: []*Widget) void {
const config = peer.getLayoutConfig(ColumnRowConfig);
const spacing: f32 = @floatFromInt(config.spacing);

const totalAvailableHeight = peer.getSize(peer.userdata).height - @as(f32, @floatFromInt((widgets.len -| 1) * config.spacing));
const totalAvailableHeight: f32 = @max(0, peer.getSize(peer.userdata).height - @as(f32, @floatFromInt((widgets.len -| 1) * config.spacing)));

var childHeight = if (expandedCount == 0) 0 else totalAvailableHeight / @as(f32, @floatFromInt(expandedCount));
for (widgets) |widget| {
Expand Down Expand Up @@ -81,7 +81,7 @@ pub fn ColumnLayout(peer: Callbacks, widgets: []*Widget) void {

const available = Size{
.width = peer.getSize(peer.userdata).width,
.height = if (widget.container_expanded) childHeight else peer.getSize(peer.userdata).height - childY,
.height = if (widget.container_expanded) childHeight else @max(0, peer.getSize(peer.userdata).height - childY),
};
const preferred = widget.getPreferredSize(available);
const size = blk: {
Expand Down Expand Up @@ -124,7 +124,7 @@ pub fn RowLayout(peer: Callbacks, widgets: []*Widget) void {
const config = peer.getLayoutConfig(ColumnRowConfig);
const spacing: f32 = @floatFromInt(config.spacing);

const totalAvailableWidth = peer.getSize(peer.userdata).width - @as(f32, @floatFromInt((widgets.len -| 1) * config.spacing));
const totalAvailableWidth: f32 = @max(0, peer.getSize(peer.userdata).width - @as(f32, @floatFromInt((widgets.len -| 1) * config.spacing)));

var childWidth = if (expandedCount == 0) 0 else totalAvailableWidth / @as(f32, @floatFromInt(expandedCount));
for (widgets) |widget| {
Expand Down Expand Up @@ -156,7 +156,7 @@ pub fn RowLayout(peer: Callbacks, widgets: []*Widget) void {
}

const available = Size{
.width = if (widget.container_expanded) childWidth else peer.getSize(peer.userdata).width - childX,
.width = if (widget.container_expanded) childWidth else @max(0, peer.getSize(peer.userdata).width - childX),
.height = peer.getSize(peer.userdata).height,
};
const preferred = widget.getPreferredSize(available);
Expand Down

0 comments on commit bbdeb32

Please sign in to comment.