refactor: deduplicate toggle_panel_view

By allowing any panel to accept arguments.
This commit is contained in:
CJ van den Berg 2026-03-01 21:50:37 +01:00
parent c4f6b6c945
commit 2f5d4ded3c
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
8 changed files with 21 additions and 36 deletions

View file

@ -57,7 +57,7 @@ const Entry = struct {
pos_type: editor.PosType,
};
pub fn create(allocator: Allocator, parent: Plane) !Widget {
pub fn create(allocator: Allocator, parent: Plane, _: command.Context) !Widget {
const self = try allocator.create(Self);
errdefer allocator.destroy(self);
self.* = .{

View file

@ -1,6 +1,7 @@
const std = @import("std");
const Allocator = @import("std").mem.Allocator;
const Plane = @import("renderer").Plane;
const command = @import("command");
const Widget = @import("Widget.zig");
const WidgetList = @import("WidgetList.zig");
const reflow = @import("Buffer").reflow;
@ -19,7 +20,7 @@ widget_type: Widget.Type,
const default_widget_type: Widget.Type = .panel;
pub fn create(allocator: Allocator, parent: Plane) !Widget {
pub fn create(allocator: Allocator, parent: Plane, _: command.Context) !Widget {
return create_widget_type(allocator, parent, default_widget_type);
}

View file

@ -10,6 +10,7 @@ const cbor = @import("cbor");
const Plane = @import("renderer").Plane;
const EventHandler = @import("EventHandler");
const input = @import("input");
const command = @import("command");
const tui = @import("tui.zig");
const Widget = @import("Widget.zig");
@ -33,7 +34,7 @@ const Entry = struct {
};
const Buffer = ArrayList(Entry);
pub fn create(allocator: Allocator, parent: Plane) !Widget {
pub fn create(allocator: Allocator, parent: Plane, _: command.Context) !Widget {
var n = try Plane.init(&(Widget.Box{}).opts_vscroll(@typeName(Self)), parent);
errdefer n.deinit();
const container = try WidgetList.createHStyled(allocator, parent, "panel_frame", .dynamic, widget_type);

View file

@ -9,6 +9,7 @@ const Plane = @import("renderer").Plane;
const style = @import("renderer").style;
const styles = @import("renderer").styles;
const EventHandler = @import("EventHandler");
const command = @import("command");
const tui = @import("tui.zig");
const Widget = @import("Widget.zig");
@ -25,7 +26,7 @@ last_node: usize = 0,
const Self = @This();
const widget_type: Widget.Type = .panel;
pub fn create(allocator: Allocator, parent: Plane) !Widget {
pub fn create(allocator: Allocator, parent: Plane, _: command.Context) !Widget {
const editor = tui.get_active_editor() orelse return error.NotFound;
const self = try allocator.create(Self);
errdefer allocator.destroy(self);

View file

@ -10,6 +10,7 @@ const cbor = @import("cbor");
const Plane = @import("renderer").Plane;
const input = @import("input");
const command = @import("command");
const tui = @import("tui.zig");
const Widget = @import("Widget.zig");
@ -33,7 +34,7 @@ const Entry = struct {
};
const Buffer = ArrayList(Entry);
pub fn create(allocator: Allocator, parent: Plane) !Widget {
pub fn create(allocator: Allocator, parent: Plane, _: command.Context) !Widget {
var n = try Plane.init(&(Widget.Box{}).opts_vscroll(@typeName(Self)), parent);
errdefer n.deinit();
const container = try WidgetList.createHStyled(allocator, parent, "panel_frame", .dynamic, widget_type);

View file

@ -6,6 +6,7 @@ const array_list = @import("std").array_list;
const tp = @import("thespian");
const cbor = @import("cbor");
const command = @import("command");
const Plane = @import("renderer").Plane;
@ -39,7 +40,7 @@ const Level = enum {
err,
};
pub fn create(allocator: Allocator, parent: Plane) !Widget {
pub fn create(allocator: Allocator, parent: Plane, _: command.Context) !Widget {
const self = try allocator.create(Self);
errdefer allocator.destroy(self);
const container = try WidgetList.createHStyled(allocator, parent, "panel_frame", .dynamic, widget_type);

View file

@ -298,7 +298,13 @@ pub fn get_panel_height(self: *Self) usize {
return self.panel_height orelse self.box().h / 5;
}
fn toggle_panel_view(self: *Self, view: anytype, mode: enum { toggle, enable, disable }) !void {
pub const PanelToggleMode = enum { toggle, enable, disable };
fn toggle_panel_view(self: *Self, view: anytype, mode: PanelToggleMode) !void {
return self.toggle_panel_view_with_args(view, mode, .{});
}
fn toggle_panel_view_with_args(self: *Self, view: anytype, mode: PanelToggleMode, ctx: command.Context) !void {
if (self.panels) |panels| {
if (self.get_panel(@typeName(view))) |w| {
if (mode != .enable) {
@ -310,39 +316,17 @@ fn toggle_panel_view(self: *Self, view: anytype, mode: enum { toggle, enable, di
}
} else {
if (mode != .disable)
try panels.add(try view.create(self.allocator, self.widgets.plane));
try panels.add(try view.create(self.allocator, self.widgets.plane, ctx));
}
} else if (mode != .disable) {
const panels = try WidgetList.createH(self.allocator, self.widgets.plane, "panel", .{ .static = self.get_panel_height() });
try self.widgets.add(panels.widget());
try panels.add(try view.create(self.allocator, self.widgets.plane));
try panels.add(try view.create(self.allocator, self.widgets.plane, ctx));
self.panels = panels;
}
tui.resize();
}
fn toggle_panel_view_with_args(self: *Self, view: anytype, mode: enum { toggle, enable, disable }, ctx: command.Context) !void {
if (self.panels) |panels| {
if (self.get_panel(@typeName(view))) |w| {
if (mode != .enable) {
panels.remove(w.*);
if (panels.empty()) {
self.widgets.remove(panels.widget());
self.panels = null;
}
}
} else {
if (mode != .disable)
try panels.add(try view.create_with_args(self.allocator, self.widgets.plane, ctx));
}
} else if (mode != .disable) {
const panels = try WidgetList.createH(self.allocator, self.widgets.plane, "panel", .{ .static = self.get_panel_height() });
try self.widgets.add(panels.widget());
try panels.add(try view.create_with_args(self.allocator, self.widgets.plane, ctx));
self.panels = panels;
}
tui.resize();
}
fn get_panel(self: *Self, name_: []const u8) ?*Widget {
if (self.panels) |panels|
for (panels.widgets.items) |*w|

View file

@ -37,11 +37,7 @@ hover: bool = false,
vt: *Vt,
commands: Commands = undefined,
pub fn create(allocator: Allocator, parent: Plane) !Widget {
return create_with_args(allocator, parent, .{});
}
pub fn create_with_args(allocator: Allocator, parent: Plane, ctx: command.Context) !Widget {
pub fn create(allocator: Allocator, parent: Plane, ctx: command.Context) !Widget {
const container = try WidgetList.createHStyled(
allocator,
parent,