diff --git a/src/tui/Button.zig b/src/tui/Button.zig new file mode 100644 index 0000000..7c075f4 --- /dev/null +++ b/src/tui/Button.zig @@ -0,0 +1,81 @@ +const std = @import("std"); +const nc = @import("notcurses"); +const tp = @import("thespian"); +const log = @import("log"); + +const Widget = @import("Widget.zig"); +const command = @import("command.zig"); +const tui = @import("tui.zig"); + +parent: nc.Plane, +plane: nc.Plane, +active: bool = false, +hover: bool = false, +opts: Options, + +const Self = @This(); + +pub const Options = struct { + label: []const u8 = "button", + pos: Widget.Box = .{ .y = 0, .x = 0, .w = 8, .h = 1 }, + ctx: ?*anyopaque = null, + + on_click: *const fn (ctx: ?*anyopaque, button: *Self) void = do_nothing, + on_render: ?*const fn (ctx: ?*anyopaque, button: *Self, theme: *const Widget.Theme) bool = null, + on_layout: ?*const fn (ctx: ?*anyopaque, button: *Self) Widget.Layout = null, +}; + +pub fn create(a: std.mem.Allocator, parent: nc.Plane, opts: Options) !Widget { + const self: *Self = try a.create(Self); + var n = try nc.Plane.init(&opts.pos.opts(@typeName(Self)), parent); + errdefer n.deinit(); + self.* = .{ + .parent = parent, + .plane = n, + .opts = opts, + }; + return Widget.to(self); +} + +pub fn deinit(self: *Self, a: std.mem.Allocator) void { + self.plane.deinit(); + a.destroy(self); +} + +pub fn layout(self: *Self) Widget.Layout { + return if (self.opts.on_layout) |on_layout| + on_layout(self.opts.ctx, self) + else + .{ .static = self.opts.label.len + 2 }; +} + +pub fn render(self: *Self, theme: *const Widget.Theme) bool { + if (self.opts.on_render) |on_render| + return on_render(self.opts.ctx, self, theme); + tui.set_base_style(&self.plane, " ", if (self.active) theme.scrollbar_active else if (self.hover) theme.scrollbar_hover else theme.scrollbar); + self.plane.erase(); + self.plane.home(); + _ = self.plane.print(" {s} ", .{self.opts.label}) catch {}; + return false; +} + +pub fn receive(self: *Self, _: tp.pid_ref, m: tp.message) error{Exit}!bool { + if (try m.match(.{ "B", nc.event_type.PRESS, nc.key.BUTTON1, tp.any, tp.any, tp.any, tp.any, tp.any })) { + self.active = true; + return true; + } else if (try m.match(.{ "B", nc.event_type.RELEASE, nc.key.BUTTON1, tp.any, tp.any, tp.any, tp.any, tp.any })) { + self.opts.on_click(self.opts.ctx, self); + self.active = false; + return true; + } else if (try m.match(.{ "D", nc.event_type.RELEASE, nc.key.BUTTON1, tp.any, tp.any, tp.any, tp.any, tp.any })) { + self.opts.on_click(self.opts.ctx, self); + self.active = false; + return true; + } else if (try m.match(.{ "H", tp.extract(&self.hover) })) { + tui.current().request_mouse_cursor_pointer(self.hover); + return true; + } + return false; +} + +pub fn do_nothing(_: ?*anyopaque, _: *Self) void {} diff --git a/src/tui/Widget.zig b/src/tui/Widget.zig index d1a965e..1af2d31 100644 --- a/src/tui/Widget.zig +++ b/src/tui/Widget.zig @@ -46,7 +46,7 @@ pub const VTable = struct { subscribe: *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, - walk: *const fn (ctx: *anyopaque, walk_ctx: *anyopaque, f: WalkFn) bool, + walk: *const fn (ctx: *anyopaque, walk_ctx: *anyopaque, f: WalkFn, self_widget: *Self) bool, type_name: []const u8, }; @@ -130,8 +130,8 @@ pub fn to(pimpl: anytype) Self { } }.get, .walk = struct { - pub fn walk(ctx: *anyopaque, walk_ctx: *anyopaque, f: WalkFn) bool { - return if (comptime @hasDecl(child, "walk")) child.walk(@as(*child, @ptrCast(@alignCast(ctx))), walk_ctx, f) else false; + pub fn walk(ctx: *anyopaque, walk_ctx: *anyopaque, f: WalkFn, self: *Self) bool { + return if (comptime @hasDecl(child, "walk")) child.walk(@as(*child, @ptrCast(@alignCast(ctx))), walk_ctx, f, self) else false; } }.walk, }, @@ -206,7 +206,7 @@ pub fn get(self: *Self, name_: []const u8) ?*Self { } pub fn walk(self: *Self, walk_ctx: *anyopaque, f: WalkFn) bool { - return if (self.vtable.walk(self.ptr, walk_ctx, f)) 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 empty(a: Allocator, parent: nc.Plane, layout_: Layout) !Self { @@ -264,7 +264,7 @@ pub fn empty(a: Allocator, parent: nc.Plane, layout_: Layout) !Self { } }.get, .walk = struct { - pub fn walk(_: *anyopaque, _: *anyopaque, _: WalkFn) bool { + pub fn walk(_: *anyopaque, _: *anyopaque, _: WalkFn, _: *Self) bool { return false; } }.walk, diff --git a/src/tui/WidgetList.zig b/src/tui/WidgetList.zig index ec36957..311d3b3 100644 --- a/src/tui/WidgetList.zig +++ b/src/tui/WidgetList.zig @@ -28,18 +28,21 @@ box: ?Widget.Box = null, pub fn createH(a: Allocator, parent: Widget, name: [:0]const u8, layout_: Layout) !*Self { const self: *Self = try a.create(Self); self.* = try init(a, parent, name, .horizontal, layout_, Box{}); + self.plane.move_bottom(); return self; } pub fn createV(a: Allocator, parent: Widget, name: [:0]const u8, layout_: Layout) !*Self { const self: *Self = try a.create(Self); self.* = try init(a, parent, name, .vertical, layout_, Box{}); + self.plane.move_bottom(); return self; } pub fn createBox(a: Allocator, parent: Widget, name: [:0]const u8, dir: Direction, layout_: Layout, box: Box) !*Self { const self: *Self = try a.create(Self); self.* = try init(a, parent, name, dir, layout_, box); + self.plane.move_bottom(); return self; } @@ -222,8 +225,8 @@ pub fn get(self: *Self, name_: []const u8) ?*Widget { return null; } -pub fn walk(self: *Self, walk_ctx: *anyopaque, f: Widget.WalkFn) bool { +pub fn walk(self: *Self, ctx: *anyopaque, f: Widget.WalkFn, self_w: *Widget) bool { for (self.widgets.items) |*w| - if (w.widget.walk(walk_ctx, f)) return true; - return false; + if (w.widget.walk(ctx, f)) return true; + return f(ctx, self_w); } diff --git a/src/tui/home.zig b/src/tui/home.zig index 7b437b6..e06e7a8 100644 --- a/src/tui/home.zig +++ b/src/tui/home.zig @@ -3,6 +3,8 @@ const nc = @import("notcurses"); const tp = @import("thespian"); const Widget = @import("Widget.zig"); +const WidgetList = @import("WidgetList.zig"); +const Button = @import("Button.zig"); const tui = @import("tui.zig"); const command = @import("command.zig"); const fonts = @import("fonts.zig"); @@ -12,6 +14,8 @@ plane: nc.Plane, parent: nc.Plane, fire: ?Fire = null, commands: Commands = undefined, +menu: *WidgetList, +menu_widget: Widget, const Self = @This(); @@ -24,20 +28,86 @@ pub fn create(a: std.mem.Allocator, parent: Widget) !Widget { .a = a, .parent = parent.plane.*, .plane = n, + .menu = undefined, + .menu_widget = undefined, }; try self.commands.init(self); + const w = Widget.to(self); + const menu = try WidgetList.createV(a, w, @typeName(Self), .{ .static = 32 }); + self.menu = menu; + self.menu_widget = menu.widget(); + try menu.add(try Button.create(a, parent.plane.*, .{ .on_layout = menu_layout, .label = "Help ······················· :h", .on_click = on_click_help, .on_render = render_menu_item })); + try menu.add(try Button.create(a, parent.plane.*, .{ .on_layout = menu_layout, .label = "Open file ·················· :o", .on_click = on_click_open_file, .on_render = render_menu_item })); + try menu.add(try Button.create(a, parent.plane.*, .{ .on_layout = menu_layout, .label = "Open recent file ····(wip)·· :e", .on_click = on_click_open_recent_file, .on_render = render_menu_item })); + try menu.add(try Button.create(a, parent.plane.*, .{ .on_layout = menu_layout, .label = "Open recent project ·(wip)·· :r", .on_click = on_click_open_recent_project, .on_render = render_menu_item })); + try menu.add(try Button.create(a, parent.plane.*, .{ .on_layout = menu_layout, .label = "Show/Run commands ···(wip)·· :p", .on_click = on_click_show_commands, .on_render = render_menu_item })); + try menu.add(try Button.create(a, parent.plane.*, .{ .on_layout = menu_layout, .label = "Open config file ··········· :c", .on_click = on_click_open_config, .on_render = render_menu_item })); + try menu.add(try Button.create(a, parent.plane.*, .{ .on_layout = menu_layout, .label = "Quit/Close ················· :q", .on_click = on_click_quit, .on_render = render_menu_item })); + menu.resize(.{ .y = 15, .x = 9, .w = 32, .h = menu.widgets.items.len }); command.executeName("enter_mode", command.Context.fmt(.{"home"})) catch {}; - return Widget.to(self); + return w; } pub fn deinit(self: *Self, a: std.mem.Allocator) void { + self.menu.deinit(a); self.commands.deinit(); self.plane.deinit(); if (self.fire) |*fire| fire.deinit(); a.destroy(self); } +pub fn update(self: *Self) void { + self.menu.update(); +} + +pub fn walk(self: *Self, walk_ctx: *anyopaque, f: Widget.WalkFn, w: *Widget) bool { + return self.menu.walk(walk_ctx, f, &self.menu_widget) or f(walk_ctx, w); +} + +pub fn receive(_: *Self, _: tp.pid_ref, m: tp.message) error{Exit}!bool { + var hover: bool = false; + if (try m.match(.{ "H", tp.extract(&hover) })) { + tui.current().request_mouse_cursor_default(hover); + return true; + } + return false; +} + +fn menu_layout(_: ?*anyopaque, _: *Button) Widget.Layout { + return .{ .static = 1 }; +} + +pub fn on_click_help(_: ?*anyopaque, _: *Button) void { + command.executeName("open_help", .{}) catch {}; +} + +pub fn on_click_open_file(_: ?*anyopaque, _: *Button) void { + command.executeName("enter_open_file_mode", .{}) catch {}; +} + +pub fn on_click_open_recent_file(_: ?*anyopaque, _: *Button) void { + tp.self_pid().send(.{ "log", "home", "open recent file not implemented" }) catch {}; +} + +pub fn on_click_open_recent_project(_: ?*anyopaque, _: *Button) void { + tp.self_pid().send(.{ "log", "home", "open recent project not implemented" }) catch {}; +} + +pub fn on_click_show_commands(_: ?*anyopaque, _: *Button) void { + tp.self_pid().send(.{ "log", "home", "open command palette not implemented" }) catch {}; +} + +pub fn on_click_open_config(_: ?*anyopaque, _: *Button) void { + command.executeName("open_config", .{}) catch {}; +} + +pub fn on_click_quit(_: ?*anyopaque, _: *Button) void { + command.executeName("quit", .{}) catch {}; +} + pub fn render(self: *Self, theme: *const Widget.Theme) bool { + const more = self.menu.render(theme); + tui.set_base_style(&self.plane, " ", theme.editor); self.plane.erase(); self.plane.home(); @@ -45,78 +115,58 @@ pub fn render(self: *Self, theme: *const Widget.Theme) bool { const style_title = if (tui.find_scope_style(theme, "function")) |sty| sty.style else theme.editor; const style_subtext = if (tui.find_scope_style(theme, "comment")) |sty| sty.style else theme.editor; - const style_text = if (tui.find_scope_style(theme, "keyword")) |sty| sty.style else theme.editor; - const style_keybind = if (tui.find_scope_style(theme, "entity.name")) |sty| sty.style else theme.editor; const title = "Flow Control"; const subtext = "a programmer's text editor"; if (self.plane.dim_x() > 120 and self.plane.dim_y() > 22) { - self.set_style(style_title); - self.plane.cursor_move_yx(2, 4) catch return false; - fonts.print_string_large(self.plane, title) catch return false; + tui.set_style(&self.plane, style_title); + self.plane.cursor_move_yx(2, 4) catch return more; + fonts.print_string_large(self.plane, title) catch return more; - self.set_style(style_subtext); - self.plane.cursor_move_yx(10, 8) catch return false; - fonts.print_string_medium(self.plane, subtext) catch return false; + tui.set_style(&self.plane, style_subtext); + self.plane.cursor_move_yx(10, 8) catch return more; + fonts.print_string_medium(self.plane, subtext) catch return more; - self.plane.cursor_move_yx(15, 10) catch return false; + self.menu.resize(.{ .y = 15, .x = 10, .w = 32, .h = self.menu.widgets.items.len }); } else if (self.plane.dim_x() > 55 and self.plane.dim_y() > 16) { - self.set_style(style_title); - self.plane.cursor_move_yx(2, 4) catch return false; - fonts.print_string_medium(self.plane, title) catch return false; + tui.set_style(&self.plane, style_title); + self.plane.cursor_move_yx(2, 4) catch return more; + fonts.print_string_medium(self.plane, title) catch return more; - self.set_style(style_subtext); - self.plane.cursor_move_yx(7, 6) catch return false; + tui.set_style(&self.plane, style_subtext); + self.plane.cursor_move_yx(7, 6) catch return more; _ = self.plane.print(subtext, .{}) catch {}; - self.plane.cursor_move_yx(9, 8) catch return false; + self.menu.resize(.{ .y = 9, .x = 8, .w = 32, .h = self.menu.widgets.items.len }); } else { - self.set_style(style_title); - self.plane.cursor_move_yx(1, 4) catch return false; - _ = self.plane.print(title, .{}) catch return false; + tui.set_style(&self.plane, style_title); + self.plane.cursor_move_yx(1, 4) catch return more; + _ = self.plane.print(title, .{}) catch return more; - self.set_style(style_subtext); - self.plane.cursor_move_yx(3, 6) catch return false; + tui.set_style(&self.plane, style_subtext); + self.plane.cursor_move_yx(3, 6) catch return more; _ = self.plane.print(subtext, .{}) catch {}; - self.plane.cursor_move_yx(5, 8) catch return false; + self.menu.resize(.{ .y = 5, .x = 8, .w = 32, .h = self.menu.widgets.items.len }); } - if (self.plane.dim_x() > 48 and self.plane.dim_y() > 12) - self.render_hints(style_subtext, style_text, style_keybind); return true; } -fn render_hints(self: *Self, style_base: Widget.Theme.Style, style_text: Widget.Theme.Style, style_keybind: Widget.Theme.Style) void { - const hint_text: [:0]const u8 = - \\Help ······················· :h - \\Open file ·················· :o - \\Open recent file ··(wip)···· :e - \\Open recent project ··(wip)· :r - \\Show/Run commands ·(wip)···· :p - \\Open config file ··········· :c - \\Quit/Close ················· :q - \\ - ; - - const left: c_int = @intCast(self.plane.cursor_x()); - var pos: usize = 0; - while (std.mem.indexOfScalarPos(u8, hint_text, pos, '\n')) |next| { - const line = hint_text[pos..next]; - const sep = std.mem.indexOfScalar(u8, line, ':') orelse line.len; - self.set_style(style_base); - self.set_style(style_text); - _ = self.plane.print("{s}", .{line[0..sep]}) catch {}; - self.set_style(style_keybind); - _ = self.plane.print("{s}", .{line[sep + 1 ..]}) catch {}; - self.plane.cursor_move_rel(1, 0) catch {}; - self.plane.cursor_move_yx(-1, left) catch {}; - pos = next + 1; - } -} - -fn set_style(self: *Self, style: Widget.Theme.Style) void { - tui.set_style(&self.plane, style); +fn render_menu_item(_: ?*anyopaque, button: *Button, theme: *const Widget.Theme) bool { + tui.set_base_style(&button.plane, " ", if (button.active) theme.editor_cursor else if (button.hover) theme.editor_selection else theme.editor); + button.plane.erase(); + button.plane.home(); + const style_subtext = if (tui.find_scope_style(theme, "comment")) |sty| sty.style else theme.editor; + const style_text = if (tui.find_scope_style(theme, "keyword")) |sty| sty.style else theme.editor; + const style_keybind = if (tui.find_scope_style(theme, "entity.name")) |sty| sty.style else theme.editor; + const sep = std.mem.indexOfScalar(u8, button.opts.label, ':') orelse button.opts.label.len; + tui.set_style(&button.plane, style_subtext); + tui.set_style(&button.plane, style_text); + _ = button.plane.print(" {s}", .{button.opts.label[0..sep]}) catch {}; + tui.set_style(&button.plane, style_keybind); + _ = button.plane.print("{s}", .{button.opts.label[sep + 1 ..]}) catch {}; + return false; } pub fn handle_resize(self: *Self, pos: Widget.Box) void { diff --git a/src/tui/mainview.zig b/src/tui/mainview.zig index b212e8b..1a86ca6 100644 --- a/src/tui/mainview.zig +++ b/src/tui/mainview.zig @@ -21,6 +21,7 @@ const Commands = command.Collection(cmds); a: std.mem.Allocator, plane: nc.Plane, widgets: *WidgetList, +widgets_widget: Widget, floating_views: WidgetStack, commands: Commands = undefined, statusbar: *Widget, @@ -46,6 +47,7 @@ pub fn create(a: std.mem.Allocator, n: nc.Plane) !Widget { .a = a, .plane = n, .widgets = undefined, + .widgets_widget = undefined, .floating_views = WidgetStack.init(a), .statusbar = undefined, .location_history = try location_history.create(), @@ -54,6 +56,7 @@ pub fn create(a: std.mem.Allocator, n: nc.Plane) !Widget { const w = Widget.to(self); const widgets = try WidgetList.createV(a, w, @typeName(Self), .dynamic); self.widgets = widgets; + self.widgets_widget = widgets.widget(); try widgets.add(try Widget.empty(a, n, .dynamic)); self.statusbar = try widgets.addP(try @import("status/statusbar.zig").create(a, w)); self.resize(); @@ -320,8 +323,8 @@ pub fn get_editor(self: *Self) ?*ed.Editor { return self.editor; } -pub fn walk(self: *Self, walk_ctx: *anyopaque, f: Widget.WalkFn) bool { - return if (self.floating_views.walk(walk_ctx, f)) true else self.widgets.walk(walk_ctx, f); +pub fn walk(self: *Self, ctx: *anyopaque, f: Widget.WalkFn, w: *Widget) bool { + return self.floating_views.walk(ctx, f) or self.widgets.walk(ctx, f, &self.widgets_widget) or f(ctx, w); } fn create_editor(self: *Self) tp.result { diff --git a/src/tui/tui.zig b/src/tui/tui.zig index f47b578..236584f 100644 --- a/src/tui/tui.zig +++ b/src/tui/tui.zig @@ -690,7 +690,7 @@ const cmds = struct { var mode: []const u8 = undefined; if (!try ctx.args.match(.{tp.extract(&mode)})) return tp.exit_error(error.InvalidArgument); - if (self.mini_mode) |_| std.debug.panic("attempted to switch mode to {s} while in mini mode {s}", .{ mode, self.input_mode.?.name }); + if (self.mini_mode) |_| try cmds.exit_mini_mode(self, .{}); if (self.input_mode) |*m| m.deinit(); self.input_mode = if (std.mem.eql(u8, mode, "vim/normal")) @import("mode/input/vim/normal.zig").create(self.a) catch |e| return tp.exit_error(e) @@ -831,6 +831,10 @@ pub fn request_mouse_cursor_pointer(self: *const Self, push_or_pop: bool) void { if (push_or_pop) self.mouse_cursor_push("pointer") else self.mouse_cursor_pop(); } +pub fn request_mouse_cursor_default(self: *const Self, push_or_pop: bool) void { + if (push_or_pop) self.mouse_cursor_push("default") else self.mouse_cursor_pop(); +} + fn mouse_cursor_push(self: *const Self, comptime name: []const u8) void { self.write_stdout(OSC22_cursor ++ name ++ ST); }