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

@ -5,7 +5,6 @@ const command = @import("command");
const cmd = command.executeName;
const tui = @import("../tui.zig");
const mainview = @import("../mainview.zig");
var commands: Commands = undefined;
@ -59,7 +58,7 @@ const cmds_ = struct {
const logger = log.logger("helix-mode");
defer logger.deinit();
logger.print("saved location", .{});
const mv = tui.current().mainview.dynamic_cast(mainview) orelse return;
const mv = tui.mainview() orelse return;
const file_path = mv.get_active_file_path() orelse return;
const primary = (mv.get_active_editor() orelse return).get_primary();
const sel: ?location_history.Selection = if (primary.selection) |sel| .{

View file

@ -44,7 +44,7 @@ pub fn Create(options: type) type {
.entries = std.ArrayList(Entry).init(allocator),
};
try self.commands.init(self);
try tui.current().message_filters.add(MessageFilter.bind(self, receive_path_entry));
try tui.message_filters().add(MessageFilter.bind(self, receive_path_entry));
try options.load_entries(self);
if (@hasDecl(options, "restore_state"))
options.restore_state(self) catch {};
@ -57,7 +57,7 @@ pub fn Create(options: type) type {
pub fn deinit(self: *Self) void {
self.commands.deinit();
tui.current().message_filters.remove_ptr(self);
tui.message_filters().remove_ptr(self);
self.clear_entries();
self.entries.deinit();
self.match.deinit();
@ -113,7 +113,7 @@ pub fn Create(options: type) type {
} else {
try self.file_path.appendSlice(self.query.items);
}
if (tui.current().mini_mode) |*mini_mode| {
if (tui.mini_mode()) |mini_mode| {
mini_mode.text = self.file_path.items;
mini_mode.cursor = tui.egc_chunk_width(self.file_path.items, 0, 8);
}
@ -137,7 +137,7 @@ pub fn Create(options: type) type {
fn process_project_manager(self: *Self, m: tp.message) MessageFilter.Error!void {
defer {
if (tui.current().mini_mode) |*mini_mode| {
if (tui.mini_mode()) |mini_mode| {
mini_mode.text = self.file_path.items;
mini_mode.cursor = tui.egc_chunk_width(self.file_path.items, 0, 8);
}
@ -241,7 +241,7 @@ pub fn Create(options: type) type {
}
fn update_mini_mode_text(self: *Self) void {
if (tui.current().mini_mode) |*mini_mode| {
if (tui.mini_mode()) |mini_mode| {
mini_mode.text = self.file_path.items;
mini_mode.cursor = tui.egc_chunk_width(self.file_path.items, 0, 8);
}

View file

@ -140,7 +140,7 @@ fn load_history(self: *Self, pos: usize) void {
}
fn update_mini_mode_text(self: *Self) void {
if (tui.current().mini_mode) |*mini_mode| {
if (tui.mini_mode()) |mini_mode| {
mini_mode.text = self.input.items;
mini_mode.cursor = tui.egc_chunk_width(self.input.items, 0, 8);
}

View file

@ -81,7 +81,7 @@ fn start_query(self: *Self) !void {
}
fn update_mini_mode_text(self: *Self) void {
if (tui.current().mini_mode) |*mini_mode| {
if (tui.mini_mode()) |mini_mode| {
mini_mode.text = self.input;
mini_mode.cursor = tui.egc_chunk_width(self.input, 0, 8);
}

View file

@ -49,7 +49,7 @@ pub fn receive(self: *Self, _: tp.pid_ref, _: tp.message) error{Exit}!bool {
}
fn update_mini_mode_text(self: *Self) void {
if (tui.current().mini_mode) |*mini_mode| {
if (tui.mini_mode()) |mini_mode| {
mini_mode.text = if (self.input) |linenum|
(fmt.bufPrint(&self.buf, "{d}", .{linenum}) catch "")
else

View file

@ -4,7 +4,6 @@ const root = @import("root");
const command = @import("command");
const tui = @import("../../tui.zig");
const mainview = @import("../../mainview.zig");
pub const Type = @import("file_browser.zig").Create(@This());

View file

@ -20,7 +20,7 @@ pub const Entry = struct {
};
pub fn load_entries(palette: *Type) !usize {
const hints = if (tui.current().input_mode) |m| m.keybind_hints else @panic("no keybind hints");
const hints = if (tui.input_mode()) |m| m.keybind_hints else @panic("no keybind hints");
var longest_hint: usize = 0;
for (command.commands.items) |cmd_| if (cmd_) |p| {
if (p.meta.description.len > 0) {

View file

@ -33,10 +33,10 @@ pub fn deinit(palette: *Type) void {
pub fn load_entries(palette: *Type) !usize {
var idx: usize = 0;
previous_fontface = try palette.allocator.dupe(u8, tui.current().fontface);
const fontfaces = tui.current().fontfaces orelse return 0;
tui.current().fontfaces = null;
for (fontfaces.items) |fontface| {
previous_fontface = try palette.allocator.dupe(u8, tui.fontface());
const fontfaces = try tui.fontfaces(palette.allocator);
defer palette.allocator.free(fontfaces);
for (fontfaces) |fontface| {
idx += 1;
(try palette.entries.addOne()).* = .{ .label = fontface };
if (previous_fontface) |previous_fontface_| if (std.mem.eql(u8, fontface, previous_fontface_)) {

View file

@ -23,7 +23,7 @@ pub fn deinit(palette: *Type) void {
}
pub fn load_entries(palette: *Type) !usize {
const hints = if (tui.current().input_mode) |m| m.keybind_hints else @panic("no keybind hints");
const hints = if (tui.input_mode()) |m| m.keybind_hints else @panic("no keybind hints");
var longest_hint: usize = 0;
for (command.commands.items) |cmd_| if (cmd_) |p| {
var label_ = std.ArrayList(u8).init(palette.allocator);

View file

@ -18,7 +18,6 @@ const Button = @import("../../Button.zig");
const InputBox = @import("../../InputBox.zig");
const Menu = @import("../../Menu.zig");
const Widget = @import("../../Widget.zig");
const mainview = @import("../../mainview.zig");
const ModalBackground = @import("../../ModalBackground.zig");
const Self = @This();
@ -38,11 +37,11 @@ commands: Commands = undefined,
buffer_manager: ?*BufferManager,
pub fn create(allocator: std.mem.Allocator) !tui.Mode {
const mv = tui.current().mainview.dynamic_cast(mainview) orelse return error.NotFound;
const mv = tui.mainview() orelse return error.NotFound;
const self: *Self = try allocator.create(Self);
self.* = .{
.allocator = allocator,
.modal = try ModalBackground.create(*Self, allocator, tui.current().mainview, .{ .ctx = self }),
.modal = try ModalBackground.create(*Self, allocator, tui.mainview_widget(), .{ .ctx = self }),
.menu = try Menu.create(*Self, allocator, tui.plane(), .{
.ctx = self,
.on_render = on_render_menu,
@ -56,7 +55,7 @@ pub fn create(allocator: std.mem.Allocator) !tui.Mode {
.buffer_manager = tui.get_buffer_manager(),
};
try self.commands.init(self);
try tui.current().message_filters.add(MessageFilter.bind(self, receive_project_manager));
try tui.message_filters().add(MessageFilter.bind(self, receive_project_manager));
self.query_pending = true;
try project_manager.request_recent_files(max_recent_files);
self.menu.resize(.{ .y = 0, .x = self.menu_pos_x(), .w = max_menu_width() + 2 });
@ -72,8 +71,8 @@ pub fn create(allocator: std.mem.Allocator) !tui.Mode {
pub fn deinit(self: *Self) void {
self.commands.deinit();
tui.current().message_filters.remove_ptr(self);
if (tui.current().mainview.dynamic_cast(mainview)) |mv| {
tui.message_filters().remove_ptr(self);
if (tui.mainview()) |mv| {
mv.floating_views.remove(self.menu.container_widget);
mv.floating_views.remove(self.modal.widget());
}
@ -86,13 +85,13 @@ inline fn menu_width(self: *Self) usize {
}
inline fn menu_pos_x(self: *Self) usize {
const screen_width = tui.current().screen().w;
const screen_width = tui.screen().w;
const width = self.menu_width();
return if (screen_width <= width) 0 else (screen_width - width) / 2;
}
inline fn max_menu_width() usize {
const width = tui.current().screen().w;
const width = tui.screen().w;
return @max(15, width - (width / 5));
}

View file

@ -14,7 +14,6 @@ const tui = @import("../../tui.zig");
const Button = @import("../../Button.zig");
const InputBox = @import("../../InputBox.zig");
const Widget = @import("../../Widget.zig");
const mainview = @import("../../mainview.zig");
const scrollbar_v = @import("../../scrollbar_v.zig");
const ModalBackground = @import("../../ModalBackground.zig");
@ -47,11 +46,11 @@ pub fn Create(options: type) type {
pub const ButtonState = Button.State(*Menu.State(*Self));
pub fn create(allocator: std.mem.Allocator) !tui.Mode {
const mv = tui.current().mainview.dynamic_cast(mainview) orelse return error.NotFound;
const mv = tui.mainview() orelse return error.NotFound;
const self: *Self = try allocator.create(Self);
self.* = .{
.allocator = allocator,
.modal = try ModalBackground.create(*Self, allocator, tui.current().mainview, .{
.modal = try ModalBackground.create(*Self, allocator, tui.mainview_widget(), .{
.ctx = self,
.on_click = mouse_palette_menu_cancel,
}),
@ -68,7 +67,7 @@ pub fn Create(options: type) type {
.ctx = self,
.label = options.label,
}))).dynamic_cast(InputBox.State(*Self)) orelse unreachable,
.view_rows = get_view_rows(tui.current().screen()),
.view_rows = get_view_rows(tui.screen()),
.entries = std.ArrayList(Entry).init(allocator),
};
self.menu.scrollbar.?.style_factory = scrollbar_style;
@ -92,8 +91,8 @@ pub fn Create(options: type) type {
if (@hasDecl(options, "deinit"))
options.deinit(self);
self.entries.deinit();
tui.current().message_filters.remove_ptr(self);
if (tui.current().mainview.dynamic_cast(mainview)) |mv| {
tui.message_filters().remove_ptr(self);
if (tui.mainview()) |mv| {
mv.floating_views.remove(self.menu.container_widget);
mv.floating_views.remove(self.modal.widget());
}
@ -160,7 +159,7 @@ pub fn Create(options: type) type {
}
fn do_resize(self: *Self) void {
const screen = tui.current().screen();
const screen = tui.screen();
const w = @min(self.longest, max_menu_width) + 2 + 1 + self.longest_hint;
const x = if (screen.w > w) (screen.w - w) / 2 else 0;
self.view_rows = get_view_rows(screen);
@ -246,7 +245,7 @@ pub fn Create(options: type) type {
while (i > 0) : (i -= 1)
self.menu.select_down();
self.do_resize();
tui.current().refresh_hover();
tui.refresh_hover();
self.selection_updated();
}
}

View file

@ -27,7 +27,7 @@ var previous_theme: ?[]const u8 = null;
pub fn load_entries(palette: *Type) !usize {
var longest_hint: usize = 0;
var idx: usize = 0;
previous_theme = tui.current().theme.name;
previous_theme = tui.theme().name;
for (Widget.themes) |theme| {
idx += 1;
(try palette.entries.addOne()).* = .{
@ -76,7 +76,7 @@ pub fn updated(palette: *Type, button_: ?*Type.ButtonState) !void {
}
pub fn cancel(palette: *Type) !void {
if (previous_theme) |name_| if (!std.mem.eql(u8, name_, tui.current().theme.name)) {
if (previous_theme) |name_| if (!std.mem.eql(u8, name_, tui.theme().name)) {
previous_theme = null;
tp.self_pid().send(.{ "cmd", "set_theme", .{name_} }) catch |e| palette.logger.err("theme_palette cancel", e);
};