refactor: simplify Plane/Widget usage

This commit is contained in:
CJ van den Berg 2025-01-23 16:12:56 +01:00
parent 62cd53c0f3
commit 4145460012
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
15 changed files with 64 additions and 50 deletions

View file

@ -5,6 +5,7 @@ const Widget = @import("Widget.zig");
const WidgetList = @import("WidgetList.zig");
const Button = @import("Button.zig");
const scrollbar_v = @import("scrollbar_v.zig");
const Plane = @import("renderer").Plane;
pub const Container = WidgetList;
pub const scroll_lines = 3;
@ -52,12 +53,12 @@ pub fn Options(context: type) type {
};
}
pub fn create(ctx_type: type, allocator: std.mem.Allocator, parent: Widget, opts: Options(ctx_type)) !*State(ctx_type) {
pub fn create(ctx_type: type, allocator: std.mem.Allocator, parent: Plane, opts: Options(ctx_type)) !*State(ctx_type) {
const self = try allocator.create(State(ctx_type));
const container = try WidgetList.createH(allocator, parent, @typeName(@This()), .dynamic);
self.* = .{
.allocator = allocator,
.menu = try WidgetList.createV(allocator, container.widget(), @typeName(@This()), .dynamic),
.menu = try WidgetList.createV(allocator, container.plane, @typeName(@This()), .dynamic),
.container = container,
.container_widget = container.widget(),
.scrollbar = if (opts.on_scroll) |on_scroll| (try scrollbar_v.create(allocator, parent, null, on_scroll)).dynamic_cast(scrollbar_v).? else null,

View file

@ -31,31 +31,31 @@ on_render: *const fn (ctx: ?*anyopaque, theme: *const Widget.Theme) void = on_re
after_render: *const fn (ctx: ?*anyopaque, theme: *const Widget.Theme) void = on_render_default,
on_resize: *const fn (ctx: ?*anyopaque, self: *Self, pos_: Widget.Box) void = on_resize_default,
pub fn createH(allocator: Allocator, parent: Widget, name: [:0]const u8, layout_: Layout) !*Self {
pub fn createH(allocator: Allocator, parent: Plane, name: [:0]const u8, layout_: Layout) !*Self {
const self: *Self = try allocator.create(Self);
self.* = try init(allocator, parent, name, .horizontal, layout_, Box{});
self.plane.hide();
return self;
}
pub fn createV(allocator: Allocator, parent: Widget, name: [:0]const u8, layout_: Layout) !*Self {
pub fn createV(allocator: Allocator, parent: Plane, name: [:0]const u8, layout_: Layout) !*Self {
const self: *Self = try allocator.create(Self);
self.* = try init(allocator, parent, name, .vertical, layout_, Box{});
self.plane.hide();
return self;
}
pub fn createBox(allocator: Allocator, parent: Widget, name: [:0]const u8, dir: Direction, layout_: Layout, box: Box) !*Self {
pub fn createBox(allocator: Allocator, parent: Plane, name: [:0]const u8, dir: Direction, layout_: Layout, box: Box) !*Self {
const self: *Self = try allocator.create(Self);
self.* = try init(allocator, parent, name, dir, layout_, box);
self.plane.hide();
return self;
}
fn init(allocator: Allocator, parent: Widget, name: [:0]const u8, dir: Direction, layout_: Layout, box: Box) !Self {
fn init(allocator: Allocator, parent: Plane, name: [:0]const u8, dir: Direction, layout_: Layout, box: Box) !Self {
return .{
.plane = try Plane.init(&box.opts(name), parent.plane.*),
.parent = parent.plane.*,
.plane = try Plane.init(&box.opts(name), parent),
.parent = parent,
.allocator = allocator,
.widgets = ArrayList(WidgetState).init(allocator),
.layout = layout_,

View file

@ -4783,7 +4783,7 @@ pub const Editor = struct {
pub const set_file_type_meta = .{ .arguments = &.{.string} };
};
pub fn create(allocator: Allocator, parent: Widget, buffer_manager: *Buffer.Manager) !Widget {
pub fn create(allocator: Allocator, parent: Plane, buffer_manager: *Buffer.Manager) !Widget {
return EditorWidget.create(allocator, parent, buffer_manager);
}
@ -4806,24 +4806,24 @@ pub const EditorWidget = struct {
const Self = @This();
const Commands = command.Collection(Editor);
fn create(allocator: Allocator, parent: Widget, buffer_manager: *Buffer.Manager) !Widget {
fn create(allocator: Allocator, parent: Plane, buffer_manager: *Buffer.Manager) !Widget {
const container = try WidgetList.createH(allocator, parent, "editor.container", .dynamic);
const self: *Self = try allocator.create(Self);
try self.init(allocator, container.widget(), buffer_manager);
try self.init(allocator, container.plane, buffer_manager);
try self.commands.init(&self.editor);
const editorWidget = Widget.to(self);
try container.add(try editor_gutter.create(allocator, container.widget(), editorWidget, &self.editor));
try container.add(editorWidget);
try container.add(try scrollbar_v.create(allocator, container.widget(), editorWidget, EventHandler.to_unowned(container)));
try container.add(try scrollbar_v.create(allocator, container.plane, editorWidget, EventHandler.to_unowned(container)));
return container.widget();
}
fn init(self: *Self, allocator: Allocator, parent: Widget, buffer_manager: *Buffer.Manager) !void {
var n = try Plane.init(&(Widget.Box{}).opts("editor"), parent.plane.*);
fn init(self: *Self, allocator: Allocator, parent: Plane, buffer_manager: *Buffer.Manager) !void {
var n = try Plane.init(&(Widget.Box{}).opts("editor"), parent);
errdefer n.deinit();
self.* = .{
.parent = parent.plane.*,
.parent = parent,
.plane = n,
.editor = undefined,
};

View file

@ -53,7 +53,7 @@ pub fn create(allocator: Allocator, parent: Plane) !Widget {
.plane = try Plane.init(&(Widget.Box{}).opts(name), parent),
.logger = log.logger(@typeName(Self)),
.entries = std.ArrayList(Entry).init(allocator),
.menu = try Menu.create(*Self, allocator, tui.current().mainview, .{
.menu = try Menu.create(*Self, allocator, tui.plane(), .{
.ctx = self,
.on_render = handle_render_menu,
.on_scroll = EventHandler.bind(self, Self.handle_scroll),

View file

@ -65,7 +65,7 @@ pub fn create(allocator: std.mem.Allocator, parent: Widget) !Widget {
.allocator = allocator,
.parent = parent.plane.*,
.plane = n,
.menu = try Menu.create(*Self, allocator, w, .{ .ctx = self, .on_render = menu_on_render }),
.menu = try Menu.create(*Self, allocator, w.plane.*, .{ .ctx = self, .on_render = menu_on_render }),
.input_namespace = keybind.get_namespace(),
};
try self.commands.init(self);

View file

@ -64,7 +64,7 @@ pub fn create(allocator: std.mem.Allocator) !Widget {
const self = try allocator.create(Self);
self.* = .{
.allocator = allocator,
.plane = tui.current().stdplane(),
.plane = tui.plane(),
.widgets = undefined,
.widgets_widget = undefined,
.floating_views = WidgetStack.init(allocator),
@ -77,13 +77,13 @@ pub fn create(allocator: std.mem.Allocator) !Widget {
try self.commands.init(self);
const w = Widget.to(self);
const widgets = try WidgetList.createV(allocator, w, @typeName(Self), .dynamic);
const widgets = try WidgetList.createV(allocator, self.plane, @typeName(Self), .dynamic);
self.widgets = widgets;
self.widgets_widget = widgets.widget();
if (tui.current().config.top_bar.len > 0)
self.top_bar = try widgets.addP(try @import("status/bar.zig").create(allocator, w, tui.current().config.top_bar, .none, null));
self.top_bar = try widgets.addP(try @import("status/bar.zig").create(allocator, self.plane, tui.current().config.top_bar, .none, null));
const views = try WidgetList.createH(allocator, self.widgets_widget, @typeName(Self), .dynamic);
const views = try WidgetList.createH(allocator, self.plane, @typeName(Self), .dynamic);
self.views = views;
self.views_widget = views.widget();
try views.add(try Widget.empty(allocator, self.views_widget.plane.*, .dynamic));
@ -91,7 +91,7 @@ pub fn create(allocator: std.mem.Allocator) !Widget {
try widgets.add(self.views_widget);
if (tui.current().config.bottom_bar.len > 0) {
self.bottom_bar = try widgets.addP(try @import("status/bar.zig").create(allocator, w, tui.current().config.bottom_bar, .grip, EventHandler.bind(self, handle_bottom_bar_event)));
self.bottom_bar = try widgets.addP(try @import("status/bar.zig").create(allocator, self.plane, tui.current().config.bottom_bar, .grip, EventHandler.bind(self, handle_bottom_bar_event)));
}
if (tp.env.get().is("show-input"))
self.toggle_inputview_async();
@ -161,7 +161,7 @@ pub fn render(self: *Self, theme: *const Widget.Theme) bool {
}
pub fn handle_resize(self: *Self, pos: Box) void {
self.plane = tui.current().stdplane();
self.plane = tui.plane();
if (self.panel_height) |h| if (h >= self.box().h) {
self.panel_height = null;
};
@ -207,7 +207,7 @@ fn toggle_panel_view(self: *Self, view: anytype, enable_only: bool) !void {
try panels.add(try view.create(self.allocator, self.widgets.plane));
}
} else {
const panels = try WidgetList.createH(self.allocator, self.widgets.widget(), "panel", .{ .static = self.panel_height orelse self.box().h / 5 });
const panels = try WidgetList.createH(self.allocator, self.widgets.plane, "panel", .{ .static = self.panel_height orelse self.box().h / 5 });
try self.widgets.add(panels.widget());
try panels.add(try view.create(self.allocator, self.widgets.plane));
self.panels = panels;
@ -879,7 +879,7 @@ fn create_editor(self: *Self) !void {
if (self.get_active_file_path()) |file_path| self.push_file_stack(file_path) catch {};
try self.delete_active_view();
command.executeName("enter_mode_default", .{}) catch {};
var editor_widget = try ed.create(self.allocator, Widget.to(self), &self.buffer_manager);
var editor_widget = try ed.create(self.allocator, self.plane, &self.buffer_manager);
errdefer editor_widget.deinit(self.allocator);
const editor = editor_widget.get("editor") orelse @panic("mainview editor not found");
if (self.top_bar) |bar| editor.subscribe(EventHandler.to_unowned(bar)) catch @panic("subscribe unsupported");

View file

@ -115,7 +115,7 @@ pub fn Create(options: type) type {
}
if (tui.current().mini_mode) |*mini_mode| {
mini_mode.text = self.file_path.items;
mini_mode.cursor = tui.current().stdplane().egc_chunk_width(self.file_path.items, 0, 8);
mini_mode.cursor = tui.egc_chunk_width(self.file_path.items, 0, 8);
}
return;
}
@ -139,7 +139,7 @@ pub fn Create(options: type) type {
defer {
if (tui.current().mini_mode) |*mini_mode| {
mini_mode.text = self.file_path.items;
mini_mode.cursor = tui.current().stdplane().egc_chunk_width(self.file_path.items, 0, 8);
mini_mode.cursor = tui.egc_chunk_width(self.file_path.items, 0, 8);
}
}
var count: usize = undefined;
@ -243,7 +243,7 @@ pub fn Create(options: type) type {
fn update_mini_mode_text(self: *Self) void {
if (tui.current().mini_mode) |*mini_mode| {
mini_mode.text = self.file_path.items;
mini_mode.cursor = tui.current().stdplane().egc_chunk_width(self.file_path.items, 0, 8);
mini_mode.cursor = tui.egc_chunk_width(self.file_path.items, 0, 8);
}
}
@ -273,7 +273,7 @@ pub fn Create(options: type) type {
pub fn mini_mode_delete_backwards(self: *Self, _: Ctx) Result {
if (self.file_path.items.len > 0) {
self.complete_trigger_count = 0;
self.file_path.shrinkRetainingCapacity(self.file_path.items.len - tui.current().stdplane().egc_last(self.file_path.items).len);
self.file_path.shrinkRetainingCapacity(self.file_path.items.len - tui.egc_last(self.file_path.items).len);
}
self.update_mini_mode_text();
}

View file

@ -142,7 +142,7 @@ fn load_history(self: *Self, pos: usize) void {
fn update_mini_mode_text(self: *Self) void {
if (tui.current().mini_mode) |*mini_mode| {
mini_mode.text = self.input.items;
mini_mode.cursor = tui.current().stdplane().egc_chunk_width(self.input.items, 0, 8);
mini_mode.cursor = tui.egc_chunk_width(self.input.items, 0, 8);
}
}
@ -187,7 +187,7 @@ const cmds = struct {
pub const mini_mode_insert_bytes_meta = .{ .arguments = &.{.string} };
pub fn mini_mode_delete_backwards(self: *Self, _: Ctx) Result {
self.input.resize(self.input.items.len - tui.current().stdplane().egc_last(self.input.items).len) catch {};
self.input.resize(self.input.items.len - tui.egc_last(self.input.items).len) catch {};
self.update_mini_mode_text();
}
pub const mini_mode_delete_backwards_meta = .{ .description = "Delete backwards" };

View file

@ -83,7 +83,7 @@ fn start_query(self: *Self) !void {
fn update_mini_mode_text(self: *Self) void {
if (tui.current().mini_mode) |*mini_mode| {
mini_mode.text = self.input;
mini_mode.cursor = tui.current().stdplane().egc_chunk_width(self.input, 0, 8);
mini_mode.cursor = tui.egc_chunk_width(self.input, 0, 8);
}
}
@ -128,7 +128,7 @@ const cmds = struct {
pub const mini_mode_insert_bytes_meta = .{ .arguments = &.{.string} };
pub fn mini_mode_delete_backwards(self: *Self, _: Ctx) Result {
self.input = self.input[0 .. self.input.len - tui.current().stdplane().egc_last(self.input).len];
self.input = self.input[0 .. self.input.len - tui.egc_last(self.input).len];
self.update_mini_mode_text();
}
pub const mini_mode_delete_backwards_meta = .{ .description = "Delete backwards" };

View file

@ -54,7 +54,7 @@ fn update_mini_mode_text(self: *Self) void {
(fmt.bufPrint(&self.buf, "{d}", .{linenum}) catch "")
else
"";
mini_mode.cursor = tui.current().stdplane().egc_chunk_width(mini_mode.text, 0, 8);
mini_mode.cursor = tui.egc_chunk_width(mini_mode.text, 0, 8);
}
}

View file

@ -43,7 +43,7 @@ pub fn create(allocator: std.mem.Allocator) !tui.Mode {
self.* = .{
.allocator = allocator,
.modal = try ModalBackground.create(*Self, allocator, tui.current().mainview, .{ .ctx = self }),
.menu = try Menu.create(*Self, allocator, tui.current().mainview, .{
.menu = try Menu.create(*Self, allocator, tui.plane(), .{
.ctx = self,
.on_render = on_render_menu,
.on_resize = on_resize_menu,
@ -231,14 +231,14 @@ fn delete_word(self: *Self) !void {
} else {
self.inputbox.text.shrinkRetainingCapacity(0);
}
self.inputbox.cursor = tui.current().stdplane().egc_chunk_width(self.inputbox.text.items, 0, 8);
self.inputbox.cursor = tui.egc_chunk_width(self.inputbox.text.items, 0, 8);
return self.start_query();
}
fn delete_code_point(self: *Self) !void {
if (self.inputbox.text.items.len > 0) {
self.inputbox.text.shrinkRetainingCapacity(self.inputbox.text.items.len - tui.current().stdplane().egc_last(self.inputbox.text.items).len);
self.inputbox.cursor = tui.current().stdplane().egc_chunk_width(self.inputbox.text.items, 0, 8);
self.inputbox.text.shrinkRetainingCapacity(self.inputbox.text.items.len - tui.egc_last(self.inputbox.text.items).len);
self.inputbox.cursor = tui.egc_chunk_width(self.inputbox.text.items, 0, 8);
}
return self.start_query();
}
@ -247,13 +247,13 @@ fn insert_code_point(self: *Self, c: u32) !void {
var buf: [6]u8 = undefined;
const bytes = try input.ucs32_to_utf8(&[_]u32{c}, &buf);
try self.inputbox.text.appendSlice(buf[0..bytes]);
self.inputbox.cursor = tui.current().stdplane().egc_chunk_width(self.inputbox.text.items, 0, 8);
self.inputbox.cursor = tui.egc_chunk_width(self.inputbox.text.items, 0, 8);
return self.start_query();
}
fn insert_bytes(self: *Self, bytes: []const u8) !void {
try self.inputbox.text.appendSlice(bytes);
self.inputbox.cursor = tui.current().stdplane().egc_chunk_width(self.inputbox.text.items, 0, 8);
self.inputbox.cursor = tui.egc_chunk_width(self.inputbox.text.items, 0, 8);
return self.start_query();
}

View file

@ -55,7 +55,7 @@ pub fn Create(options: type) type {
.ctx = self,
.on_click = mouse_palette_menu_cancel,
}),
.menu = try Menu.create(*Self, allocator, tui.current().mainview, .{
.menu = try Menu.create(*Self, allocator, tui.plane(), .{
.ctx = self,
.on_render = if (@hasDecl(options, "on_render_menu")) options.on_render_menu else on_render_menu,
.on_resize = on_resize_menu,
@ -307,15 +307,15 @@ pub fn Create(options: type) type {
} else {
self.inputbox.text.shrinkRetainingCapacity(0);
}
self.inputbox.cursor = tui.current().stdplane().egc_chunk_width(self.inputbox.text.items, 0, 8);
self.inputbox.cursor = tui.egc_chunk_width(self.inputbox.text.items, 0, 8);
self.view_pos = 0;
return self.start_query(0);
}
fn delete_code_point(self: *Self) !void {
if (self.inputbox.text.items.len > 0) {
self.inputbox.text.shrinkRetainingCapacity(self.inputbox.text.items.len - tui.current().stdplane().egc_last(self.inputbox.text.items).len);
self.inputbox.cursor = tui.current().stdplane().egc_chunk_width(self.inputbox.text.items, 0, 8);
self.inputbox.text.shrinkRetainingCapacity(self.inputbox.text.items.len - tui.egc_last(self.inputbox.text.items).len);
self.inputbox.cursor = tui.egc_chunk_width(self.inputbox.text.items, 0, 8);
}
self.view_pos = 0;
return self.start_query(0);
@ -325,14 +325,14 @@ pub fn Create(options: type) type {
var buf: [6]u8 = undefined;
const bytes = try input.ucs32_to_utf8(&[_]u32{c}, &buf);
try self.inputbox.text.appendSlice(buf[0..bytes]);
self.inputbox.cursor = tui.current().stdplane().egc_chunk_width(self.inputbox.text.items, 0, 8);
self.inputbox.cursor = tui.egc_chunk_width(self.inputbox.text.items, 0, 8);
self.view_pos = 0;
return self.start_query(0);
}
fn insert_bytes(self: *Self, bytes: []const u8) !void {
try self.inputbox.text.appendSlice(bytes);
self.inputbox.cursor = tui.current().stdplane().egc_chunk_width(self.inputbox.text.items, 0, 8);
self.inputbox.cursor = tui.egc_chunk_width(self.inputbox.text.items, 0, 8);
self.view_pos = 0;
return self.start_query(0);
}

View file

@ -26,10 +26,10 @@ style_factory: ?*const fn (self: *Self, theme: *const Widget.Theme) Widget.Theme
const Self = @This();
pub fn create(allocator: Allocator, parent: Widget, event_source: ?Widget, event_sink: EventHandler) !Widget {
pub fn create(allocator: Allocator, parent: Plane, event_source: ?Widget, event_sink: EventHandler) !Widget {
const self: *Self = try allocator.create(Self);
self.* = .{
.plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent.plane.*),
.plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent),
.event_sink = event_sink,
};

View file

@ -4,10 +4,11 @@ const EventHandler = @import("EventHandler");
const status_widget = @import("widget.zig");
const Widget = @import("../Widget.zig");
const WidgetList = @import("../WidgetList.zig");
const Plane = @import("renderer").Plane;
pub const Style = enum { none, grip };
pub fn create(allocator: std.mem.Allocator, parent: Widget, config: []const u8, style: Style, event_handler: ?EventHandler) !Widget {
pub fn create(allocator: std.mem.Allocator, parent: Plane, config: []const u8, style: Style, event_handler: ?EventHandler) !Widget {
var w = try WidgetList.createH(allocator, parent, "statusbar", .{ .static = 1 });
if (style == .grip) w.after_render = render_grip;
w.ctx = w;

View file

@ -1037,10 +1037,22 @@ pub fn resize(self: *Self) void {
need_render();
}
pub fn stdplane(self: *Self) renderer.Plane {
pub fn plane() renderer.Plane {
return current().rdr.stdplane();
}
fn stdplane(self: *Self) renderer.Plane {
return self.rdr.stdplane();
}
pub fn egc_chunk_width(chunk: []const u8, abs_col: usize, tab_width: usize) usize {
return plane().egc_chunk_width(chunk, abs_col, tab_width);
}
pub fn egc_last(egcs: []const u8) []const u8 {
return plane().egc_last(egcs);
}
pub fn screen(self: *Self) Widget.Box {
return Widget.Box.from(self.rdr.stdplane());
}