From 4cde0640c8d804e438d174b6a49159dac09a3475 Mon Sep 17 00:00:00 2001 From: Yves Biener Date: Thu, 22 May 2025 23:39:05 +0200 Subject: [PATCH] fix(element/scrollable): adjust anchor for scrollable element during resize --- src/element.zig | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/element.zig b/src/element.zig index 8e74454..3beb1ff 100644 --- a/src/element.zig +++ b/src/element.zig @@ -109,20 +109,28 @@ pub fn Scrollable(Event: type) type { fn resize(ctx: *anyopaque, size: Point) void { const this: *@This() = @ptrCast(@alignCast(ctx)); - this.size = size; + const last_max_anchor_x = this.container_size.x -| this.size.x; + const last_max_anchor_y = this.container_size.y -| this.size.y; + this.size = size; this.container.resize(size); if (this.configuration.scrollbar) { if (this.container.properties.size.dim.x > this.size.x or this.container.size.x > this.size.x) this.configuration.y_axis = true; if (this.container.properties.size.dim.y > this.size.y or this.container.size.y > this.size.y) this.configuration.x_axis = true; - if (this.configuration.x_axis or this.configuration.y_axis) - this.container.resize(.{ - .x = this.size.x - if (this.configuration.x_axis) @as(u16, 1) else @as(u16, 0), - .y = this.size.y - if (this.configuration.y_axis) @as(u16, 1) else @as(u16, 0), - }); + if (this.configuration.x_axis or this.configuration.y_axis) this.container.resize(.{ + .x = this.size.x - if (this.configuration.x_axis) @as(u16, 1) else @as(u16, 0), + .y = this.size.y - if (this.configuration.y_axis) @as(u16, 1) else @as(u16, 0), + }); } + const new_max_anchor_x = this.container.size.x -| size.x; + const new_max_anchor_y = this.container.size.y -| size.y; + + // correct anchor if necessary + if (new_max_anchor_x < last_max_anchor_x and this.anchor.x > new_max_anchor_x) this.anchor.x = new_max_anchor_x; + if (new_max_anchor_y < last_max_anchor_y and this.anchor.y > new_max_anchor_y) this.anchor.y = new_max_anchor_y; + this.container_size = this.container.size; } @@ -140,8 +148,8 @@ pub fn Scrollable(Event: type) type { this.anchor.y -|= 1; }, Mouse.Button.wheel_down => if (this.container_size.y > this.size.y) { - const max_origin_y = this.container_size.y -| this.size.y; - this.anchor.y = @min(this.anchor.y + 1, max_origin_y); + const max_anchor_y = this.container_size.y -| this.size.y; + this.anchor.y = @min(this.anchor.y + 1, max_anchor_y); }, Mouse.Button.wheel_left => if (this.container_size.x > this.size.x) { this.anchor.x -|= 1;