refactor: improve capsulation and safety of tui module public api

This commit is contained in:
CJ van den Berg 2025-01-23 16:45:04 +01:00
parent 4145460012
commit 1d947ab499
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
28 changed files with 239 additions and 198 deletions

View file

@ -28,13 +28,13 @@ pub fn create(allocator: std.mem.Allocator, parent: Plane, event_handler: ?Event
.on_event = event_handler,
.tz = zeit.local(allocator, &env) catch |e| return tp.exit_error(e, @errorReturnTrace()),
};
try tui.current().message_filters.add(MessageFilter.bind(self, receive_tick));
try tui.message_filters().add(MessageFilter.bind(self, receive_tick));
self.update_tick_timer(.init);
return Widget.to(self);
}
pub fn deinit(self: *Self, allocator: std.mem.Allocator) void {
tui.current().message_filters.remove_ptr(self);
tui.message_filters().remove_ptr(self);
if (self.tick_timer) |*t| {
t.cancel() catch {};
t.deinit();

View file

@ -90,7 +90,7 @@ pub fn render(self: *Self, btn: *Button.State(Self), theme: *const Widget.Theme)
btn.plane.fill(" ");
btn.plane.home();
}
if (tui.current().mini_mode) |_|
if (tui.mini_mode()) |_|
render_mini_mode(&btn.plane, theme)
else if (self.detailed)
self.render_detailed(&btn.plane, theme)
@ -102,14 +102,13 @@ pub fn render(self: *Self, btn: *Button.State(Self), theme: *const Widget.Theme)
fn render_mini_mode(plane: *Plane, theme: *const Widget.Theme) void {
plane.off_styles(style.italic);
const tui_ = tui.current();
const mini_mode = tui_.mini_mode orelse return;
const mini_mode = tui.mini_mode() orelse return;
_ = plane.print(" {s}", .{mini_mode.text}) catch {};
if (mini_mode.cursor) |cursor| {
const pos: c_int = @intCast(cursor);
if (tui_.config.enable_terminal_cursor) {
if (tui.config().enable_terminal_cursor) {
const y, const x = plane.rel_yx_to_abs(0, pos + 1);
tui_.rdr.cursor_enable(y, x, tui_.get_cursor_shape()) catch {};
tui.rdr().cursor_enable(y, x, tui.get_cursor_shape()) catch {};
} else {
plane.cursor_move_yx(0, pos + 1) catch return;
var cell = plane.cell_init();
@ -189,7 +188,7 @@ fn render_terminal_title(self: *Self) void {
if (std.mem.eql(u8, self.previous_title, new_title)) return;
@memcpy(self.previous_title_buf[0..new_title.len], new_title);
self.previous_title = self.previous_title_buf[0..new_title.len];
tui.current().rdr.set_terminal_title(new_title);
tui.rdr().set_terminal_title(new_title);
}
pub fn receive(self: *Self, _: *Button.State(Self), _: tp.pid_ref, m: tp.message) error{Exit}!bool {

View file

@ -36,7 +36,7 @@ pub fn create(allocator: Allocator, parent: Plane, _: ?EventHandler) @import("wi
.plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent),
.wipe_after_frames = @divTrunc(frame_rate, 2),
};
try tui.current().input_listeners.add(EventHandler.bind(self, listen));
try tui.input_listeners().add(EventHandler.bind(self, listen));
return self.widget();
}
@ -45,7 +45,7 @@ pub fn widget(self: *Self) Widget {
}
pub fn deinit(self: *Self, allocator: Allocator) void {
tui.current().input_listeners.remove_ptr(self);
tui.input_listeners().remove_ptr(self);
self.plane.deinit();
allocator.destroy(self);
}
@ -171,7 +171,7 @@ pub fn receive(self: *Self, _: tp.pid_ref, m: tp.message) error{Exit}!bool {
return true;
}
if (try m.match(.{ "H", tp.extract(&self.hover) })) {
tui.current().rdr.request_mouse_cursor_pointer(self.hover);
tui.rdr().request_mouse_cursor_pointer(self.hover);
return true;
}

View file

@ -34,7 +34,7 @@ pub fn create(allocator: std.mem.Allocator, parent: Plane, event_handler: ?Event
.on_event = event_handler,
};
logview.init(allocator);
try tui.current().message_filters.add(MessageFilter.bind(self, receive_log));
try tui.message_filters().add(MessageFilter.bind(self, receive_log));
try log.subscribe();
return Widget.to(self);
}
@ -47,7 +47,7 @@ pub fn deinit(self: *Self, allocator: std.mem.Allocator) void {
}
self.msg.deinit();
log.unsubscribe() catch {};
tui.current().message_filters.remove_ptr(self);
tui.message_filters().remove_ptr(self);
self.plane.deinit();
allocator.destroy(self);
}

View file

@ -33,11 +33,11 @@ pub fn layout(_: *void, btn: *Button.State(void)) Widget.Layout {
}
fn is_mini_mode() bool {
return tui.current().mini_mode != null;
return tui.mini_mode() != null;
}
fn is_overlay_mode() bool {
return tui.current().input_mode_outer != null;
return tui.input_mode_outer() != null;
}
pub fn render(_: *void, self: *Button.State(void), theme: *const Widget.Theme) bool {

View file

@ -24,7 +24,7 @@ pub fn create(allocator: Allocator, parent: Plane, _: ?EventHandler) @import("wi
self.* = .{
.plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent),
};
try tui.current().input_listeners.add(EventHandler.bind(self, listen));
try tui.input_listeners().add(EventHandler.bind(self, listen));
return self.widget();
}
@ -33,7 +33,7 @@ pub fn widget(self: *Self) Widget {
}
pub fn deinit(self: *Self, allocator: Allocator) void {
tui.current().input_listeners.remove_ptr(self);
tui.input_listeners().remove_ptr(self);
self.plane.deinit();
allocator.destroy(self);
}