fix(element/scrollable): adjust anchor for scrollable element during resize

This commit is contained in:
2025-05-22 23:39:05 +02:00
parent e9a9c2b680
commit 4cde0640c8

View File

@@ -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;