Make box widget not position-dependent

Signed-off-by: AnErrupTion <anerruption@disroot.org>
This commit is contained in:
AnErrupTion
2026-03-25 22:06:57 +01:00
parent 3758b5da1b
commit 549576aa3e
3 changed files with 25 additions and 27 deletions

View File

@@ -5,7 +5,7 @@ const Position = @import("../Position.zig");
const TerminalBuffer = @import("../TerminalBuffer.zig"); const TerminalBuffer = @import("../TerminalBuffer.zig");
const Widget = @import("../Widget.zig"); const Widget = @import("../Widget.zig");
const CenteredBox = @This(); const Box = @This();
instance: ?Widget = null, instance: ?Widget = null,
buffer: *TerminalBuffer, buffer: *TerminalBuffer,
@@ -20,7 +20,7 @@ bottom_title: ?[]const u8,
border_fg: u32, border_fg: u32,
title_fg: u32, title_fg: u32,
bg: u32, bg: u32,
update_fn: ?*const fn (*CenteredBox, *anyopaque) anyerror!void, update_fn: ?*const fn (*Box, *anyopaque) anyerror!void,
left_pos: Position, left_pos: Position,
right_pos: Position, right_pos: Position,
children_pos: Position, children_pos: Position,
@@ -38,8 +38,8 @@ pub fn init(
border_fg: u32, border_fg: u32,
title_fg: u32, title_fg: u32,
bg: u32, bg: u32,
update_fn: ?*const fn (*CenteredBox, *anyopaque) anyerror!void, update_fn: ?*const fn (*Box, *anyopaque) anyerror!void,
) CenteredBox { ) Box {
return .{ return .{
.instance = null, .instance = null,
.buffer = buffer, .buffer = buffer,
@@ -61,10 +61,10 @@ pub fn init(
}; };
} }
pub fn widget(self: *CenteredBox) *Widget { pub fn widget(self: *Box) *Widget {
if (self.instance) |*instance| return instance; if (self.instance) |*instance| return instance;
self.instance = Widget.init( self.instance = Widget.init(
"CenteredBox", "Box",
null, null,
self, self,
null, null,
@@ -77,30 +77,26 @@ pub fn widget(self: *CenteredBox) *Widget {
return &self.instance.?; return &self.instance.?;
} }
pub fn positionXY(self: *CenteredBox, original_pos: Position) void { pub fn positionXY(self: *Box, original_pos: Position) void {
if (self.buffer.width < 2 or self.buffer.height < 2) return; if (self.buffer.width < 2 or self.buffer.height < 2) return;
self.left_pos = Position.init( self.left_pos = original_pos;
(self.buffer.width - @min(self.buffer.width - 2, self.width)) / 2,
(self.buffer.height - @min(self.buffer.height - 2, self.height)) / 2,
).add(original_pos);
self.right_pos = Position.init( self.right_pos = Position.init(
(self.buffer.width + @min(self.buffer.width, self.width)) / 2, @min(self.buffer.width, self.width),
(self.buffer.height + @min(self.buffer.height, self.height)) / 2, @min(self.buffer.height, self.height),
).add(original_pos); ).add(self.left_pos);
self.children_pos = Position.init( self.children_pos = Position.init(
self.left_pos.x + self.horizontal_margin, self.horizontal_margin,
self.left_pos.y + self.vertical_margin, self.vertical_margin,
).add(original_pos); ).add(self.left_pos);
} }
pub fn childrenPosition(self: CenteredBox) Position { pub fn childrenPosition(self: Box) Position {
return self.children_pos; return self.children_pos;
} }
fn draw(self: *CenteredBox) void { fn draw(self: *Box) void {
if (self.show_borders) { if (self.show_borders) {
var left_up = Cell.init( var left_up = Cell.init(
self.buffer.box_chars.left_up, self.buffer.box_chars.left_up,
@@ -183,7 +179,7 @@ fn draw(self: *CenteredBox) void {
} }
} }
fn update(self: *CenteredBox, ctx: *anyopaque) !void { fn update(self: *Box, ctx: *anyopaque) !void {
if (self.update_fn) |update_fn| { if (self.update_fn) |update_fn| {
return @call( return @call(
.auto, .auto,

View File

@@ -7,7 +7,7 @@ pub const TerminalBuffer = @import("TerminalBuffer.zig");
pub const Widget = @import("Widget.zig"); pub const Widget = @import("Widget.zig");
pub const BigLabel = @import("components/BigLabel.zig"); pub const BigLabel = @import("components/BigLabel.zig");
pub const CenteredBox = @import("components/CenteredBox.zig"); pub const Box = @import("components/Box.zig");
pub const CyclableLabel = @import("components/generic.zig").CyclableLabel; pub const CyclableLabel = @import("components/generic.zig").CyclableLabel;
pub const Label = @import("components/Label.zig"); pub const Label = @import("components/Label.zig");
pub const Text = @import("components/Text.zig"); pub const Text = @import("components/Text.zig");

View File

@@ -9,7 +9,7 @@ const clap = @import("clap");
const ly_ui = @import("ly-ui"); const ly_ui = @import("ly-ui");
const Position = ly_ui.Position; const Position = ly_ui.Position;
const BigLabel = ly_ui.BigLabel; const BigLabel = ly_ui.BigLabel;
const CenteredBox = ly_ui.CenteredBox; const Box = ly_ui.Box;
const Label = ly_ui.Label; const Label = ly_ui.Label;
const Text = ly_ui.Text; const Text = ly_ui.Text;
const TerminalBuffer = ly_ui.TerminalBuffer; const TerminalBuffer = ly_ui.TerminalBuffer;
@@ -87,7 +87,7 @@ const UiState = struct {
password_label: Label, password_label: Label,
version_label: Label, version_label: Label,
bigclock_label: BigLabel, bigclock_label: BigLabel,
box: CenteredBox, box: Box,
info_line: InfoLine, info_line: InfoLine,
animate: bool, animate: bool,
session: Session, session: Session,
@@ -524,7 +524,7 @@ pub fn main() !void {
); );
defer state.bigclock_label.deinit(); defer state.bigclock_label.deinit();
state.box = CenteredBox.init( state.box = Box.init(
&state.buffer, &state.buffer,
state.config.margin_box_h, state.config.margin_box_h,
state.config.margin_box_v, state.config.margin_box_v,
@@ -1681,7 +1681,7 @@ fn calculateBigClockTimeout(_: *BigLabel, ptr: *anyopaque) !?usize {
return @intCast((60 - @rem(time.seconds, 60)) * 1000 - @divTrunc(time.microseconds, 1000) + 1); return @intCast((60 - @rem(time.seconds, 60)) * 1000 - @divTrunc(time.microseconds, 1000) + 1);
} }
fn updateBox(self: *CenteredBox, ptr: *anyopaque) !void { fn updateBox(self: *Box, ptr: *anyopaque) !void {
const state: *UiState = @ptrCast(@alignCast(ptr)); const state: *UiState = @ptrCast(@alignCast(ptr));
if (state.config.vi_mode) { if (state.config.vi_mode) {
@@ -1753,7 +1753,9 @@ fn positionWidgets(ptr: *anyopaque) !void {
.childrenPosition() .childrenPosition()
.removeX(TerminalBuffer.strWidth(state.lang.numlock) + TerminalBuffer.strWidth(state.lang.capslock) + 1)); .removeX(TerminalBuffer.strWidth(state.lang.numlock) + TerminalBuffer.strWidth(state.lang.capslock) + 1));
state.box.positionXY(TerminalBuffer.START_POSITION); state.box.positionXY(TerminalBuffer.START_POSITION
.addX((state.buffer.width - @min(state.buffer.width - 2, state.box.width)) / 2)
.addY((state.buffer.height - @min(state.buffer.height - 2, state.box.height)) / 2));
if (state.config.bigclock != .none) { if (state.config.bigclock != .none) {
const half_width = state.buffer.width / 2; const half_width = state.buffer.width / 2;