refactor: simplify getting the active editor and selection

This commit is contained in:
CJ van den Berg 2024-12-11 20:54:53 +01:00
parent 4b3904d5f2
commit 038ed4da2b
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
10 changed files with 138 additions and 143 deletions

View file

@ -11,7 +11,6 @@ const EventHandler = @import("EventHandler");
const tui = @import("tui.zig");
const Widget = @import("Widget.zig");
const mainview = @import("mainview.zig");
const ed = @import("editor.zig");
pub const name = @typeName(Self);
@ -25,18 +24,15 @@ last_node: usize = 0,
const Self = @This();
pub fn create(allocator: Allocator, parent: Plane) !Widget {
if (tui.current().mainview.dynamic_cast(mainview)) |mv_| if (mv_.get_editor()) |editor| {
const self: *Self = try allocator.create(Self);
self.* = .{
.plane = try Plane.init(&(Widget.Box{}).opts_vscroll(name), parent),
.editor = editor,
.pos_cache = try ed.PosToWidthCache.init(allocator),
};
try editor.handlers.add(EventHandler.bind(self, ed_receive));
return Widget.to(self);
const editor = tui.get_active_editor() orelse return error.NotFound;
const self: *Self = try allocator.create(Self);
self.* = .{
.plane = try Plane.init(&(Widget.Box{}).opts_vscroll(name), parent),
.editor = editor,
.pos_cache = try ed.PosToWidthCache.init(allocator),
};
return error.NotFound;
try editor.handlers.add(EventHandler.bind(self, ed_receive));
return Widget.to(self);
}
pub fn deinit(self: *Self, allocator: Allocator) void {