refactor: move from_client_box and to_client_box to Widget.Box

This commit is contained in:
CJ van den Berg 2025-11-30 22:49:05 +01:00
parent a6e1087773
commit a1455e8e3d
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
4 changed files with 34 additions and 33 deletions

View file

@ -1,5 +1,6 @@
const Plane = @import("renderer").Plane;
const Layer = @import("renderer").Layer;
const WidgetStyle = @import("WidgetStyle.zig");
const Self = @This();
@ -36,6 +37,30 @@ pub fn from(n: Plane) Self {
};
}
pub fn from_client_box(self: Self, padding: WidgetStyle.Margin) Self {
const total_y_padding = padding.top + padding.bottom;
const total_x_padding = padding.left + padding.right;
const y = if (self.y < padding.top) padding.top else self.y;
const x = if (self.x < padding.left) padding.left else self.x;
var box = self;
box.y = y - padding.top;
box.h += total_y_padding;
box.x = x - padding.left;
box.w += total_x_padding;
return box;
}
pub fn to_client_box(self: Self, padding: WidgetStyle.Margin) Self {
const total_y_padding = padding.top + padding.bottom;
const total_x_padding = padding.left + padding.right;
var box = self;
box.y += padding.top;
box.h -= if (box.h > total_y_padding) total_y_padding else box.h;
box.x += padding.left;
box.w -= if (box.w > total_x_padding) total_x_padding else box.w;
return box;
}
pub fn to_layer(self: Self) Layer.Options {
return .{
.y = @intCast(self.y),

View file

@ -81,7 +81,7 @@ fn init(allocator: Allocator, parent: Plane, name: [:0]const u8, dir: Direction,
.deco_box = undefined,
};
const padding = tui.get_widget_style(self.widget_type).padding;
self.deco_box = self.from_client_box(box_, padding);
self.deco_box = box_.from_client_box(padding);
self.plane = try Plane.init(&self.deco_box.opts(name), parent);
return self;
}
@ -186,7 +186,7 @@ pub fn render(self: *Self, theme: *const Widget.Theme) bool {
self.on_render(self.ctx, theme);
if (self.render_decoration) |render_decoration| render_decoration(self, theme, widget_style);
const client_box = self.to_client_box(self.deco_box, padding);
const client_box = self.deco_box.to_client_box(padding);
var more = false;
for (self.widgets.items) |*w| {
@ -306,39 +306,15 @@ fn get_loc_b(self: *Self, pos: *Widget.Box) *usize {
}
fn refresh_layout(self: *Self, padding: Widget.Style.Margin) void {
return self.handle_resize(self.to_client_box(self.deco_box, padding));
return self.handle_resize(self.deco_box.to_client_box(padding));
}
pub fn handle_resize(self: *Self, box: Widget.Box) void {
const padding = tui.get_widget_style(self.widget_type).padding;
const client_box_ = self.prepare_resize(self.ctx, self, self.to_client_box(box, padding));
self.deco_box = self.from_client_box(client_box_, padding);
const client_box_ = self.prepare_resize(self.ctx, self, box.to_client_box(padding));
self.deco_box = client_box_.from_client_box(padding);
self.do_resize(padding);
self.after_resize(self.ctx, self, self.to_client_box(self.deco_box, padding));
}
pub inline fn to_client_box(_: *const Self, box_: Widget.Box, padding: Widget.Style.Margin) Widget.Box {
const total_y_padding = padding.top + padding.bottom;
const total_x_padding = padding.left + padding.right;
var box = box_;
box.y += padding.top;
box.h -= if (box.h > total_y_padding) total_y_padding else box.h;
box.x += padding.left;
box.w -= if (box.w > total_x_padding) total_x_padding else box.w;
return box;
}
inline fn from_client_box(_: *const Self, box_: Widget.Box, padding: Widget.Style.Margin) Widget.Box {
const total_y_padding = padding.top + padding.bottom;
const total_x_padding = padding.left + padding.right;
const y = if (box_.y < padding.top) padding.top else box_.y;
const x = if (box_.x < padding.left) padding.left else box_.x;
var box = box_;
box.y = y - padding.top;
box.h += total_y_padding;
box.x = x - padding.left;
box.w += total_x_padding;
return box;
self.after_resize(self.ctx, self, self.deco_box.to_client_box(padding));
}
fn prepare_resize_default(_: ?*anyopaque, _: *Self, box: Widget.Box) Widget.Box {
@ -356,7 +332,7 @@ pub fn resize(self: *Self, box: Widget.Box) void {
}
fn do_resize(self: *Self, padding: Widget.Style.Margin) void {
const client_box = self.to_client_box(self.deco_box, padding);
const client_box = self.deco_box.to_client_box(padding);
const deco_box = self.deco_box;
self.plane.move_yx(@intCast(deco_box.y), @intCast(deco_box.x)) catch return;
self.plane.resize_simple(@intCast(deco_box.h), @intCast(deco_box.w)) catch return;

View file

@ -95,7 +95,7 @@ pub fn handle_resize(self: *Self, pos: Widget.Box) void {
self.plane.resize_simple(@intCast(pos.h), @intCast(pos.w)) catch return;
self.box = pos;
self.menu.container.resize(self.box);
const client_box = self.menu.container.to_client_box(pos, padding);
const client_box = pos.to_client_box(padding);
self.view_rows = client_box.h;
self.view_cols = client_box.w;
self.update_scrollbar();

View file

@ -238,7 +238,7 @@ pub fn Create(options: type) type {
fn do_resize(self: *Self, padding: Widget.Style.Margin) void {
const box = self.prepare_resize();
self.menu.resize(self.menu.container.to_client_box(box, padding));
self.menu.resize(box.to_client_box(padding));
self.after_resize();
}