feat: add mouse resizing of panel views

This commit is contained in:
CJ van den Berg 2024-08-13 19:38:35 +02:00
parent 95bf2ac9a7
commit 50d47a73f2
15 changed files with 138 additions and 65 deletions

View file

@ -23,6 +23,7 @@ pub fn Options(context: type) type {
on_render: *const fn (ctx: *context, button: *State(Context), theme: *const Widget.Theme) bool = on_render_default, on_render: *const fn (ctx: *context, button: *State(Context), theme: *const Widget.Theme) bool = on_render_default,
on_layout: *const fn (ctx: *context, button: *State(Context)) Widget.Layout = on_layout_default, on_layout: *const fn (ctx: *context, button: *State(Context)) Widget.Layout = on_layout_default,
on_receive: *const fn (ctx: *context, button: *State(Context), from: tp.pid_ref, m: tp.message) error{Exit}!bool = on_receive_default, on_receive: *const fn (ctx: *context, button: *State(Context), from: tp.pid_ref, m: tp.message) error{Exit}!bool = on_receive_default,
on_event: ?Widget.EventHandler = null,
pub const Context = context; pub const Context = context;
pub fn do_nothing(_: *context, _: *State(Context)) void {} pub fn do_nothing(_: *context, _: *State(Context)) void {}
@ -109,7 +110,17 @@ pub fn State(ctx_type: type) type {
self.call_click_handler(btn); self.call_click_handler(btn);
tui.need_render(); tui.need_render();
return true; return true;
} else if (try m.match(.{ "D", event_type.PRESS, tp.extract(&btn), tp.more })) {
if (self.opts.on_event) |h| {
self.active = false;
h.send(from, m) catch {};
}
return true;
} else if (try m.match(.{ "D", event_type.RELEASE, tp.extract(&btn), tp.more })) { } else if (try m.match(.{ "D", event_type.RELEASE, tp.extract(&btn), tp.more })) {
if (self.opts.on_event) |h| {
self.active = false;
h.send(from, m) catch {};
}
self.call_click_handler(btn); self.call_click_handler(btn);
tui.need_render(); tui.need_render();
return true; return true;
@ -122,7 +133,10 @@ pub fn State(ctx_type: type) type {
} }
fn call_click_handler(self: *Self, btn: u32) void { fn call_click_handler(self: *Self, btn: u32) void {
if (btn == key.BUTTON1) self.active = false; if (btn == key.BUTTON1) {
if (!self.active) return;
self.active = false;
}
if (!self.hover) return; if (!self.hover) return;
switch (btn) { switch (btn) {
key.BUTTON1 => self.opts.on_click(&self.opts.ctx, self), key.BUTTON1 => self.opts.on_click(&self.opts.ctx, self),

View file

@ -49,6 +49,7 @@ pub const VTable = struct {
unsubscribe: *const fn (ctx: *anyopaque, h: EventHandler) error{NotSupported}!void, unsubscribe: *const fn (ctx: *anyopaque, h: EventHandler) error{NotSupported}!void,
get: *const fn (ctx: *anyopaque, name_: []const u8) ?*Self, get: *const fn (ctx: *anyopaque, name_: []const u8) ?*Self,
walk: *const fn (ctx: *anyopaque, walk_ctx: *anyopaque, f: WalkFn, self_widget: *Self) bool, walk: *const fn (ctx: *anyopaque, walk_ctx: *anyopaque, f: WalkFn, self_widget: *Self) bool,
hover: *const fn (ctx: *anyopaque) bool,
type_name: []const u8, type_name: []const u8,
}; };
@ -136,6 +137,11 @@ pub fn to(pimpl: anytype) Self {
return if (comptime @hasDecl(child, "walk")) child.walk(@as(*child, @ptrCast(@alignCast(ctx))), walk_ctx, f, self) else false; return if (comptime @hasDecl(child, "walk")) child.walk(@as(*child, @ptrCast(@alignCast(ctx))), walk_ctx, f, self) else false;
} }
}.walk, }.walk,
.hover = struct {
pub fn hover(ctx: *anyopaque) bool {
return if (comptime @hasField(child, "hover")) @as(*child, @ptrCast(@alignCast(ctx))).hover else false;
}
}.hover,
}, },
}; };
} }
@ -211,6 +217,10 @@ pub fn walk(self: *Self, walk_ctx: *anyopaque, f: WalkFn) bool {
return if (self.vtable.walk(self.ptr, walk_ctx, f, self)) true else f(walk_ctx, self); return if (self.vtable.walk(self.ptr, walk_ctx, f, self)) true else f(walk_ctx, self);
} }
pub fn hover(self: *Self) bool {
return self.vtable.hover(self.ptr);
}
pub fn empty(a: Allocator, parent: Plane, layout_: Layout) !Self { pub fn empty(a: Allocator, parent: Plane, layout_: Layout) !Self {
const child: type = struct { plane: Plane, layout: Layout }; const child: type = struct { plane: Plane, layout: Layout };
const widget = try a.create(child); const widget = try a.create(child);
@ -270,6 +280,11 @@ pub fn empty(a: Allocator, parent: Plane, layout_: Layout) !Self {
return false; return false;
} }
}.walk, }.walk,
.hover = struct {
pub fn hover(_: *anyopaque) bool {
return false;
}
}.hover,
}, },
}; };
} }

View file

@ -28,6 +28,7 @@ direction: Direction,
box: ?Widget.Box = null, box: ?Widget.Box = null,
ctx: ?*anyopaque = null, ctx: ?*anyopaque = null,
on_render: *const fn (ctx: ?*anyopaque, theme: *const Widget.Theme) void = on_render_default, on_render: *const fn (ctx: ?*anyopaque, theme: *const Widget.Theme) void = on_render_default,
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, on_resize: *const fn (ctx: ?*anyopaque, self: *Self, pos_: Widget.Box) void = on_resize_default,
pub fn createH(a: Allocator, parent: Widget, name: [:0]const u8, layout_: Layout) !*Self { pub fn createH(a: Allocator, parent: Widget, name: [:0]const u8, layout_: Layout) !*Self {
@ -135,6 +136,8 @@ pub fn render(self: *Self, theme: *const Widget.Theme) bool {
if (w.widget.render(theme)) { if (w.widget.render(theme)) {
more = true; more = true;
}; };
self.after_render(self.ctx, theme);
return more; return more;
} }
@ -190,6 +193,8 @@ fn on_resize_default(_: ?*anyopaque, self: *Self, pos: Widget.Box) void {
pub fn resize(self: *Self, pos_: Widget.Box) void { pub fn resize(self: *Self, pos_: Widget.Box) void {
self.box = pos_; self.box = pos_;
var pos = pos_; var pos = pos_;
self.plane.move_yx(@intCast(pos.y), @intCast(pos.x)) catch return;
self.plane.resize_simple(@intCast(pos.h), @intCast(pos.w)) catch return;
const total = self.get_size_a(&pos).*; const total = self.get_size_a(&pos).*;
var avail = total; var avail = total;
var statics: usize = 0; var statics: usize = 0;
@ -243,3 +248,8 @@ pub fn walk(self: *Self, ctx: *anyopaque, f: Widget.WalkFn, self_w: *Widget) boo
if (w.widget.walk(ctx, f)) return true; if (w.widget.walk(ctx, f)) return true;
return f(ctx, self_w); return f(ctx, self_w);
} }
pub fn hover(self: *Self) bool {
for (self.widgets.items) |*w| if (w.widget.hover()) return true;
return false;
}

View file

@ -140,9 +140,6 @@ fn handle_render_menu(self: *Self, button: *Button.State(*Menu.State(*Self)), th
const style_info: Widget.Theme.Style = .{ .fg = theme.editor_information.fg, .fs = theme.editor_information.fs, .bg = style_base.bg }; const style_info: Widget.Theme.Style = .{ .fg = theme.editor_information.fg, .fs = theme.editor_information.fs, .bg = style_base.bg };
const style_separator: Widget.Theme.Style = .{ .fg = theme.editor_selection.bg, .bg = style_base.bg }; const style_separator: Widget.Theme.Style = .{ .fg = theme.editor_selection.bg, .bg = style_base.bg };
// const style_error: Widget.Theme.Style = .{ .fg = theme.editor_error.fg, .fs = theme.editor_error.fs, .bg = style_base.bg }; // const style_error: Widget.Theme.Style = .{ .fg = theme.editor_error.fg, .fs = theme.editor_error.fs, .bg = style_base.bg };
button.plane.set_base_style(" ", style_base);
button.plane.erase();
button.plane.home();
var idx: usize = undefined; var idx: usize = undefined;
var iter = button.opts.label; // label contains cbor, just the index var iter = button.opts.label; // label contains cbor, just the index
if (!(cbor.matchValue(&iter, cbor.extract(&idx)) catch false)) { if (!(cbor.matchValue(&iter, cbor.extract(&idx)) catch false)) {
@ -155,6 +152,9 @@ fn handle_render_menu(self: *Self, button: *Button.State(*Menu.State(*Self)), th
if (idx >= self.entries.items.len) { if (idx >= self.entries.items.len) {
return false; return false;
} }
button.plane.set_base_style(" ", style_base);
button.plane.erase();
button.plane.home();
const entry = &self.entries.items[idx]; const entry = &self.entries.items[idx];
const pointer = if (selected) "" else " "; const pointer = if (selected) "" else " ";
_ = button.plane.print("{s} ", .{pointer}) catch {}; _ = button.plane.print("{s} ", .{pointer}) catch {};
@ -229,10 +229,7 @@ fn handle_menu_action(menu: **Menu.State(*Self), button: *Button.State(*Menu.Sta
return; return;
} }
idx += self.view_pos; idx += self.view_pos;
if (idx >= self.entries.items.len) { if (idx >= self.entries.items.len) return;
self.logger.print_err(name, "table entry index out of range: {d}/{d}", .{ idx, self.entries.items.len });
return;
}
self.selected = idx; self.selected = idx;
self.update_selected(); self.update_selected();
const entry = &self.entries.items[idx]; const entry = &self.entries.items[idx];

View file

@ -8,6 +8,8 @@ const location_history = @import("location_history");
const project_manager = @import("project_manager"); const project_manager = @import("project_manager");
const Plane = @import("renderer").Plane; const Plane = @import("renderer").Plane;
const key = @import("renderer").input.key;
const event_type = @import("renderer").input.event_type;
const tui = @import("tui.zig"); const tui = @import("tui.zig");
const command = @import("command.zig"); const command = @import("command.zig");
@ -35,6 +37,7 @@ last_match_text: ?[]const u8 = null,
location_history: location_history, location_history: location_history,
file_stack: std.ArrayList([]const u8), file_stack: std.ArrayList([]const u8),
find_in_files_done: bool = false, find_in_files_done: bool = false,
panel_height: ?usize = null,
const NavState = struct { const NavState = struct {
time: i64 = 0, time: i64 = 0,
@ -45,11 +48,11 @@ const NavState = struct {
matches: usize = 0, matches: usize = 0,
}; };
pub fn create(a: std.mem.Allocator, n: Plane) !Widget { pub fn create(a: std.mem.Allocator) !Widget {
const self = try a.create(Self); const self = try a.create(Self);
self.* = .{ self.* = .{
.a = a, .a = a,
.plane = n, .plane = tui.current().stdplane(),
.widgets = undefined, .widgets = undefined,
.widgets_widget = undefined, .widgets_widget = undefined,
.floating_views = WidgetStack.init(a), .floating_views = WidgetStack.init(a),
@ -62,8 +65,8 @@ pub fn create(a: std.mem.Allocator, n: Plane) !Widget {
const widgets = try WidgetList.createV(a, w, @typeName(Self), .dynamic); const widgets = try WidgetList.createV(a, w, @typeName(Self), .dynamic);
self.widgets = widgets; self.widgets = widgets;
self.widgets_widget = widgets.widget(); self.widgets_widget = widgets.widget();
try widgets.add(try Widget.empty(a, n, .dynamic)); try widgets.add(try Widget.empty(a, self.widgets_widget.plane.*, .dynamic));
self.statusbar = try widgets.addP(try @import("status/statusbar.zig").create(a, w)); self.statusbar = try widgets.addP(try @import("status/statusbar.zig").create(a, w, EventHandler.bind(self, handle_statusbar_event)));
if (tp.env.get().is("show-input")) if (tp.env.get().is("show-input"))
self.toggle_inputview_async(); self.toggle_inputview_async();
if (tp.env.get().is("show-log")) if (tp.env.get().is("show-log"))
@ -125,6 +128,10 @@ pub fn render(self: *Self, theme: *const Widget.Theme) bool {
} }
pub fn handle_resize(self: *Self, pos: Box) void { pub fn handle_resize(self: *Self, pos: Box) void {
self.plane = tui.current().stdplane();
if (self.panel_height) |h| if (h >= self.box().h) {
self.panel_height = null;
};
self.widgets.handle_resize(pos); self.widgets.handle_resize(pos);
self.floating_views.resize(pos); self.floating_views.resize(pos);
} }
@ -133,6 +140,22 @@ pub fn box(self: *const Self) Box {
return Box.from(self.plane); return Box.from(self.plane);
} }
pub fn handle_statusbar_event(self: *Self, _: tp.pid_ref, m: tp.message) tp.result {
var y: usize = undefined;
if (try m.match(.{ "D", event_type.PRESS, key.BUTTON1, tp.any, tp.any, tp.extract(&y), tp.any, tp.any }))
return self.statusbar_primary_drag(y);
}
fn statusbar_primary_drag(self: *Self, y: usize) tp.result {
const panels = self.panels orelse blk: {
cmds.toggle_panel(self, .{}) catch return;
break :blk self.panels.?;
};
const h = self.plane.dim_y();
self.panel_height = @max(2, h - @min(h, y + 1));
panels.layout = .{ .static = self.panel_height.? };
}
fn toggle_panel_view(self: *Self, view: anytype, enable_only: bool) !bool { fn toggle_panel_view(self: *Self, view: anytype, enable_only: bool) !bool {
var enabled = true; var enabled = true;
if (self.panels) |panels| { if (self.panels) |panels| {
@ -149,7 +172,7 @@ fn toggle_panel_view(self: *Self, view: anytype, enable_only: bool) !bool {
try panels.add(try view.create(self.a, self.widgets.plane)); try panels.add(try view.create(self.a, self.widgets.plane));
} }
} else { } else {
const panels = try WidgetList.createH(self.a, self.widgets.widget(), "panel", .{ .static = self.box().h / 5 }); const panels = try WidgetList.createH(self.a, self.widgets.widget(), "panel", .{ .static = self.panel_height orelse self.box().h / 5 });
try self.widgets.add(panels.widget()); try self.widgets.add(panels.widget());
try panels.add(try view.create(self.a, self.widgets.plane)); try panels.add(try view.create(self.a, self.widgets.plane));
self.panels = panels; self.panels = panels;

View file

@ -19,7 +19,7 @@ rendered: [:0]const u8 = "",
const Self = @This(); const Self = @This();
pub fn create(a: Allocator, parent: Plane) !Widget { pub fn create(a: Allocator, parent: Plane, event_handler: ?Widget.EventHandler) !Widget {
return Button.create_widget(Self, a, parent, .{ return Button.create_widget(Self, a, parent, .{
.ctx = .{}, .ctx = .{},
.label = "", .label = "",
@ -27,6 +27,7 @@ pub fn create(a: Allocator, parent: Plane) !Widget {
.on_layout = layout, .on_layout = layout,
.on_render = render, .on_render = render,
.on_receive = receive, .on_receive = receive,
.on_event = event_handler,
}); });
} }

View file

@ -34,7 +34,7 @@ file: bool = false,
const project_icon = ""; const project_icon = "";
const Self = @This(); const Self = @This();
pub fn create(a: Allocator, parent: Plane) !Widget { pub fn create(a: Allocator, parent: Plane, event_handler: ?Widget.EventHandler) !Widget {
const btn = try Button.create(Self, a, parent, .{ const btn = try Button.create(Self, a, parent, .{
.ctx = .{ .ctx = .{
.a = a, .a = a,
@ -52,6 +52,7 @@ pub fn create(a: Allocator, parent: Plane) !Widget {
.on_layout = layout, .on_layout = layout,
.on_render = render, .on_render = render,
.on_receive = receive, .on_receive = receive,
.on_event = event_handler,
}); });
return Widget.to(btn); return Widget.to(btn);
} }

View file

@ -15,7 +15,6 @@ const EventHandler = @import("../EventHandler.zig");
const history = 8; const history = 8;
parent: Plane,
plane: Plane, plane: Plane,
frame: u64 = 0, frame: u64 = 0,
idle_frame: u64 = 0, idle_frame: u64 = 0,
@ -33,23 +32,15 @@ const idle_msg = "🐶";
pub const width = idle_msg.len + 20; pub const width = idle_msg.len + 20;
pub fn create(a: Allocator, parent: Plane) !Widget { pub fn create(a: Allocator, parent: Plane) !Widget {
const self: *Self = try a.create(Self);
self.* = try init(parent);
try tui.current().input_listeners.add(EventHandler.bind(self, listen));
return self.widget();
}
fn init(parent: Plane) !Self {
var n = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent);
errdefer n.deinit();
var frame_rate = tp.env.get().num("frame-rate"); var frame_rate = tp.env.get().num("frame-rate");
if (frame_rate == 0) frame_rate = 60; if (frame_rate == 0) frame_rate = 60;
const self: *Self = try a.create(Self);
return .{ self.* = .{
.parent = parent, .plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent),
.plane = n,
.wipe_after_frames = @divTrunc(frame_rate, 2), .wipe_after_frames = @divTrunc(frame_rate, 2),
}; };
try tui.current().input_listeners.add(EventHandler.bind(self, listen));
return self.widget();
} }
pub fn widget(self: *Self) Widget { pub fn widget(self: *Self) Widget {

View file

@ -18,7 +18,7 @@ rendered: [:0]const u8 = "",
const Self = @This(); const Self = @This();
pub fn create(a: Allocator, parent: Plane) !Widget { pub fn create(a: Allocator, parent: Plane, event_handler: ?Widget.EventHandler) !Widget {
return Button.create_widget(Self, a, parent, .{ return Button.create_widget(Self, a, parent, .{
.ctx = .{}, .ctx = .{},
.label = "", .label = "",
@ -26,6 +26,7 @@ pub fn create(a: Allocator, parent: Plane) !Widget {
.on_layout = layout, .on_layout = layout,
.on_render = render, .on_render = render,
.on_receive = receive, .on_receive = receive,
.on_event = event_handler,
}); });
} }

View file

@ -10,12 +10,12 @@ const tui = @import("../tui.zig");
const mainview = @import("../mainview.zig"); const mainview = @import("../mainview.zig");
const logview = @import("../logview.zig"); const logview = @import("../logview.zig");
parent: Plane,
plane: Plane, plane: Plane,
msg: std.ArrayList(u8), msg: std.ArrayList(u8),
msg_counter: usize = 0, msg_counter: usize = 0,
clear_timer: ?tp.Cancellable = null, clear_timer: ?tp.Cancellable = null,
level: Level = .info, level: Level = .info,
on_event: ?Widget.EventHandler,
const message_display_time_seconds = 2; const message_display_time_seconds = 2;
const error_display_time_seconds = 4; const error_display_time_seconds = 4;
@ -26,12 +26,12 @@ const Level = enum {
err, err,
}; };
pub fn create(a: std.mem.Allocator, parent: Plane) !Widget { pub fn create(a: std.mem.Allocator, parent: Plane, event_handler: ?Widget.EventHandler) !Widget {
const self: *Self = try a.create(Self); const self: *Self = try a.create(Self);
self.* = .{ self.* = .{
.parent = parent,
.plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent), .plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent),
.msg = std.ArrayList(u8).init(a), .msg = std.ArrayList(u8).init(a),
.on_event = event_handler,
}; };
logview.init(a); logview.init(a);
try tui.current().message_filters.add(MessageFilter.bind(self, receive_log)); try tui.current().message_filters.add(MessageFilter.bind(self, receive_log));
@ -52,6 +52,15 @@ pub fn deinit(self: *Self, a: std.mem.Allocator) void {
a.destroy(self); a.destroy(self);
} }
pub fn receive(self: *Self, from: tp.pid_ref, m: tp.message) error{Exit}!bool {
var btn: u32 = 0;
if (try m.match(.{ "D", tp.any, tp.extract(&btn), tp.more })) {
if (self.on_event) |h| h.send(from, m) catch {};
return true;
}
return false;
}
pub fn layout(self: *Self) Widget.Layout { pub fn layout(self: *Self) Widget.Layout {
return .{ .static = if (self.msg.items.len > 0) self.msg.items.len + 2 else 1 }; return .{ .static = if (self.msg.items.len > 0) self.msg.items.len + 2 else 1 };
} }
@ -92,7 +101,7 @@ fn process_log(self: *Self, m: tp.message) !void {
const err_stop = "error.Stop"; const err_stop = "error.Stop";
if (std.mem.eql(u8, msg, err_stop)) if (std.mem.eql(u8, msg, err_stop))
return; return;
if (msg.len >= err_stop.len + 1 and std.mem.eql(u8, msg[0..err_stop.len + 1], err_stop ++ "\n")) if (msg.len >= err_stop.len + 1 and std.mem.eql(u8, msg[0 .. err_stop.len + 1], err_stop ++ "\n"))
return; return;
try self.set(msg, .err); try self.set(msg, .err);
} else if (try m.match(.{ "log", tp.extract(&src), tp.more })) { } else if (try m.match(.{ "log", tp.extract(&src), tp.more })) {

View file

@ -14,13 +14,14 @@ const command = @import("../command.zig");
const ed = @import("../editor.zig"); const ed = @import("../editor.zig");
const tui = @import("../tui.zig"); const tui = @import("../tui.zig");
pub fn create(a: Allocator, parent: Plane) !Widget { pub fn create(a: Allocator, parent: Plane, event_handler: ?Widget.EventHandler) !Widget {
return Button.create_widget(void, a, parent, .{ return Button.create_widget(void, a, parent, .{
.ctx = {}, .ctx = {},
.label = tui.get_mode(), .label = tui.get_mode(),
.on_click = on_click, .on_click = on_click,
.on_layout = layout, .on_layout = layout,
.on_render = render, .on_render = render,
.on_event = event_handler,
}); });
} }

View file

@ -25,19 +25,13 @@ pub const width = 5;
pub fn create(a: Allocator, parent: Plane) !Widget { pub fn create(a: Allocator, parent: Plane) !Widget {
const self: *Self = try a.create(Self); const self: *Self = try a.create(Self);
self.* = try init(parent); self.* = .{
.plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent),
};
try tui.current().input_listeners.add(EventHandler.bind(self, listen)); try tui.current().input_listeners.add(EventHandler.bind(self, listen));
return self.widget(); return self.widget();
} }
fn init(parent: Plane) !Self {
var n = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent);
errdefer n.deinit();
return .{
.plane = n,
};
}
pub fn widget(self: *Self) Widget { pub fn widget(self: *Self) Widget {
return Widget.to(self); return Widget.to(self);
} }

View file

@ -15,22 +15,17 @@ cursels: usize = 0,
selection: ?ed.Selection = null, selection: ?ed.Selection = null,
buf: [256]u8 = undefined, buf: [256]u8 = undefined,
rendered: [:0]const u8 = "", rendered: [:0]const u8 = "",
on_event: ?Widget.EventHandler,
const Self = @This(); const Self = @This();
pub fn create(a: Allocator, parent: Plane) !Widget { pub fn create(a: Allocator, parent: Plane, event_handler: ?Widget.EventHandler) !Widget {
const self: *Self = try a.create(Self); const self: *Self = try a.create(Self);
self.* = try init(parent); self.* = .{
return Widget.to(self); .plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent),
} .on_event = event_handler,
fn init(parent: Plane) !Self {
var n = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent);
errdefer n.deinit();
return .{
.plane = n,
}; };
return Widget.to(self);
} }
pub fn deinit(self: *Self, a: Allocator) void { pub fn deinit(self: *Self, a: Allocator) void {
@ -81,7 +76,12 @@ fn format(self: *Self) void {
self.buf[self.rendered.len] = 0; self.buf[self.rendered.len] = 0;
} }
pub fn receive(self: *Self, _: tp.pid_ref, m: tp.message) error{Exit}!bool { pub fn receive(self: *Self, from: tp.pid_ref, m: tp.message) error{Exit}!bool {
var btn: u32 = 0;
if (try m.match(.{ "D", tp.any, tp.extract(&btn), tp.more })) {
if (self.on_event) |h| h.send(from, m) catch {};
return true;
}
if (try m.match(.{ "E", "match", tp.extract(&self.matches) })) if (try m.match(.{ "E", "match", tp.extract(&self.matches) }))
self.format(); self.format();
if (try m.match(.{ "E", "cursels", tp.extract(&self.cursels) })) if (try m.match(.{ "E", "cursels", tp.extract(&self.cursels) }))

View file

@ -6,15 +6,28 @@ const tui = @import("../tui.zig");
const Self = @This(); const Self = @This();
pub fn create(a: std.mem.Allocator, parent: Widget) !Widget { pub fn create(a: std.mem.Allocator, parent: Widget, event_handler: ?Widget.EventHandler) !Widget {
var w = try WidgetList.createH(a, parent, "statusbar", .{ .static = 1 }); var w = try WidgetList.createH(a, parent, "statusbar", .{ .static = 1 });
if (tui.current().config.modestate_show) try w.add(try @import("modestate.zig").create(a, w.plane)); w.after_render = render_grip;
try w.add(try @import("filestate.zig").create(a, w.plane)); w.ctx = w;
try w.add(try @import("minilog.zig").create(a, w.plane)); if (tui.current().config.modestate_show) try w.add(try @import("modestate.zig").create(a, w.plane, event_handler));
if (tui.current().config.selectionstate_show) try w.add(try @import("selectionstate.zig").create(a, w.plane)); try w.add(try @import("filestate.zig").create(a, w.plane, event_handler));
try w.add(try @import("diagstate.zig").create(a, w.plane)); try w.add(try @import("minilog.zig").create(a, w.plane, event_handler));
try w.add(try @import("linenumstate.zig").create(a, w.plane)); if (tui.current().config.selectionstate_show) try w.add(try @import("selectionstate.zig").create(a, w.plane, event_handler));
try w.add(try @import("diagstate.zig").create(a, w.plane, event_handler));
try w.add(try @import("linenumstate.zig").create(a, w.plane, event_handler));
if (tui.current().config.modstate_show) try w.add(try @import("modstate.zig").create(a, w.plane)); if (tui.current().config.modstate_show) try w.add(try @import("modstate.zig").create(a, w.plane));
if (tui.current().config.keystate_show) try w.add(try @import("keystate.zig").create(a, w.plane)); if (tui.current().config.keystate_show) try w.add(try @import("keystate.zig").create(a, w.plane));
return w.widget(); return w.widget();
} }
fn render_grip(ctx: ?*anyopaque, theme: *const Widget.Theme) void {
const w: *WidgetList = @ptrCast(@alignCast(ctx.?));
if (w.hover()) {
w.plane.set_style(theme.statusbar_hover);
const width = w.plane.dim_x();
const grip_pos = width / 2;
w.plane.cursor_move_yx(0, @intCast(grip_pos)) catch {};
_ = w.plane.putstr("") catch {};
}
}

View file

@ -119,7 +119,6 @@ fn init(a: Allocator) !*Self {
self.rdr.dispatch_mouse_drag = dispatch_mouse_drag; self.rdr.dispatch_mouse_drag = dispatch_mouse_drag;
self.rdr.dispatch_event = dispatch_event; self.rdr.dispatch_event = dispatch_event;
try self.rdr.run(); try self.rdr.run();
const n = self.rdr.stdplane();
try frame_clock.start(); try frame_clock.start();
try self.commands.init(self); try self.commands.init(self);
@ -132,7 +131,7 @@ fn init(a: Allocator) !*Self {
try self.listen_sigwinch(); try self.listen_sigwinch();
}, },
} }
self.mainview = try mainview.create(a, n); self.mainview = try mainview.create(a);
self.resize(); self.resize();
self.rdr.set_terminal_style(self.theme.editor); self.rdr.set_terminal_style(self.theme.editor);
try self.rdr.render(); try self.rdr.render();
@ -701,6 +700,10 @@ pub fn resize(self: *Self) void {
need_render(); need_render();
} }
pub fn stdplane(self: *Self) renderer.Plane {
return self.rdr.stdplane();
}
pub fn screen(self: *Self) Widget.Box { pub fn screen(self: *Self) Widget.Box {
return Widget.Box.from(self.rdr.stdplane()); return Widget.Box.from(self.rdr.stdplane());
} }