mirror of
https://github.com/fairyglade/ly.git
synced 2026-05-06 07:10:36 +00:00
Make box widget not position-dependent
Signed-off-by: AnErrupTion <anerruption@disroot.org>
This commit is contained in:
@@ -5,7 +5,7 @@ const Position = @import("../Position.zig");
|
||||
const TerminalBuffer = @import("../TerminalBuffer.zig");
|
||||
const Widget = @import("../Widget.zig");
|
||||
|
||||
const CenteredBox = @This();
|
||||
const Box = @This();
|
||||
|
||||
instance: ?Widget = null,
|
||||
buffer: *TerminalBuffer,
|
||||
@@ -20,7 +20,7 @@ bottom_title: ?[]const u8,
|
||||
border_fg: u32,
|
||||
title_fg: u32,
|
||||
bg: u32,
|
||||
update_fn: ?*const fn (*CenteredBox, *anyopaque) anyerror!void,
|
||||
update_fn: ?*const fn (*Box, *anyopaque) anyerror!void,
|
||||
left_pos: Position,
|
||||
right_pos: Position,
|
||||
children_pos: Position,
|
||||
@@ -38,8 +38,8 @@ pub fn init(
|
||||
border_fg: u32,
|
||||
title_fg: u32,
|
||||
bg: u32,
|
||||
update_fn: ?*const fn (*CenteredBox, *anyopaque) anyerror!void,
|
||||
) CenteredBox {
|
||||
update_fn: ?*const fn (*Box, *anyopaque) anyerror!void,
|
||||
) Box {
|
||||
return .{
|
||||
.instance = null,
|
||||
.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;
|
||||
self.instance = Widget.init(
|
||||
"CenteredBox",
|
||||
"Box",
|
||||
null,
|
||||
self,
|
||||
null,
|
||||
@@ -77,30 +77,26 @@ pub fn widget(self: *CenteredBox) *Widget {
|
||||
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;
|
||||
|
||||
self.left_pos = Position.init(
|
||||
(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.left_pos = original_pos;
|
||||
self.right_pos = Position.init(
|
||||
(self.buffer.width + @min(self.buffer.width, self.width)) / 2,
|
||||
(self.buffer.height + @min(self.buffer.height, self.height)) / 2,
|
||||
).add(original_pos);
|
||||
@min(self.buffer.width, self.width),
|
||||
@min(self.buffer.height, self.height),
|
||||
).add(self.left_pos);
|
||||
|
||||
self.children_pos = Position.init(
|
||||
self.left_pos.x + self.horizontal_margin,
|
||||
self.left_pos.y + self.vertical_margin,
|
||||
).add(original_pos);
|
||||
self.horizontal_margin,
|
||||
self.vertical_margin,
|
||||
).add(self.left_pos);
|
||||
}
|
||||
|
||||
pub fn childrenPosition(self: CenteredBox) Position {
|
||||
pub fn childrenPosition(self: Box) Position {
|
||||
return self.children_pos;
|
||||
}
|
||||
|
||||
fn draw(self: *CenteredBox) void {
|
||||
fn draw(self: *Box) void {
|
||||
if (self.show_borders) {
|
||||
var left_up = Cell.init(
|
||||
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| {
|
||||
return @call(
|
||||
.auto,
|
||||
@@ -7,7 +7,7 @@ pub const TerminalBuffer = @import("TerminalBuffer.zig");
|
||||
pub const Widget = @import("Widget.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 Label = @import("components/Label.zig");
|
||||
pub const Text = @import("components/Text.zig");
|
||||
|
||||
12
src/main.zig
12
src/main.zig
@@ -9,7 +9,7 @@ const clap = @import("clap");
|
||||
const ly_ui = @import("ly-ui");
|
||||
const Position = ly_ui.Position;
|
||||
const BigLabel = ly_ui.BigLabel;
|
||||
const CenteredBox = ly_ui.CenteredBox;
|
||||
const Box = ly_ui.Box;
|
||||
const Label = ly_ui.Label;
|
||||
const Text = ly_ui.Text;
|
||||
const TerminalBuffer = ly_ui.TerminalBuffer;
|
||||
@@ -87,7 +87,7 @@ const UiState = struct {
|
||||
password_label: Label,
|
||||
version_label: Label,
|
||||
bigclock_label: BigLabel,
|
||||
box: CenteredBox,
|
||||
box: Box,
|
||||
info_line: InfoLine,
|
||||
animate: bool,
|
||||
session: Session,
|
||||
@@ -524,7 +524,7 @@ pub fn main() !void {
|
||||
);
|
||||
defer state.bigclock_label.deinit();
|
||||
|
||||
state.box = CenteredBox.init(
|
||||
state.box = Box.init(
|
||||
&state.buffer,
|
||||
state.config.margin_box_h,
|
||||
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);
|
||||
}
|
||||
|
||||
fn updateBox(self: *CenteredBox, ptr: *anyopaque) !void {
|
||||
fn updateBox(self: *Box, ptr: *anyopaque) !void {
|
||||
const state: *UiState = @ptrCast(@alignCast(ptr));
|
||||
|
||||
if (state.config.vi_mode) {
|
||||
@@ -1753,7 +1753,9 @@ fn positionWidgets(ptr: *anyopaque) !void {
|
||||
.childrenPosition()
|
||||
.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) {
|
||||
const half_width = state.buffer.width / 2;
|
||||
|
||||
Reference in New Issue
Block a user