From a4ee6e59616d513341bc3680ead0bf3e850b6293 Mon Sep 17 00:00:00 2001 From: Cameron Reed Date: Mon, 14 Oct 2024 17:05:56 -0600 Subject: [PATCH] Refactored the way layouts are created --- src/button.zig | 32 +++--- src/line.zig | 29 +++--- src/main.zig | 107 +++++++++---------- src/menu.zig | 86 +++++++--------- src/pane.zig | 274 ++++++++++++++++++++++++++++--------------------- src/stack.zig | 263 +++++++++++++++++++++++++++++++++++++++++++++++ src/tabs.zig | 38 +++---- src/term.zig | 6 -- 8 files changed, 553 insertions(+), 282 deletions(-) create mode 100644 src/stack.zig diff --git a/src/button.zig b/src/button.zig index fdeee21..503b910 100644 --- a/src/button.zig +++ b/src/button.zig @@ -1,32 +1,33 @@ const std = @import("std"); -const log = @import("log.zig"); const dim = @import("dimensions.zig"); const pane = @import("pane.zig"); const term = @import("term.zig"); const Pane = pane.Pane; const TermIO = term.TermIO; +pub const ButtonConfig = struct { + text: []const u8, + callback: *const fn () void, +}; + pub const Button = struct { pane: Pane, text: []const u8, callback: *const fn () void, - pub fn init(parent: ?*Pane, dimensions: dim.Dimensions, text: []const u8, callback: *const fn () void) Button { - return .{ - .pane = .{ - .parent = parent, - .children = null, - .dimensions = dimensions, - .focusable = true, - .vtable = .{ - .draw = draw, - .focus = focus, - .update = update, - }, + pub fn create(mem: *Button, config: ButtonConfig) *Pane { + mem.text = config.text; + mem.callback = config.callback; + mem.pane = Pane{ + .focusable = true, + .vtable = .{ + .draw = draw, + .update = update, + .focus = focus, }, - .text = text, - .callback = callback, }; + + return &mem.pane; } fn draw(pane_ptr: *Pane, term_io: *TermIO) !void { @@ -37,7 +38,6 @@ pub const Button = struct { self.pane.moveCursor(term_io); try writer.writeAll(self.text); - term_io.flush(); } fn update(pane_ptr: *Pane, term_io: *TermIO, key: term.Key) !bool { diff --git a/src/line.zig b/src/line.zig index 7856543..0477940 100644 --- a/src/line.zig +++ b/src/line.zig @@ -2,37 +2,36 @@ const dim = @import("dimensions.zig"); const pane = @import("pane.zig"); const term = @import("term.zig"); const borders = @import("borders.zig"); + +const Pane = pane.Pane; const TermIO = term.TermIO; pub const HorizontalLine = struct { - pane: pane.Pane, + pane: Pane, border: borders.Border, - pub fn create(parent: *pane.Pane, dimensions: dim.Dimensions, border: borders.Border) HorizontalLine { - return .{ - .pane = pane.Pane{ - .parent = parent, - .children = null, - .dimensions = dimensions, - .vtable = .{ - .draw = draw, - }, + pub fn create(mem: *HorizontalLine, border: borders.Border) *Pane { + mem.border = border; + mem.pane = Pane{ + .vtable = .{ + .draw = draw, }, - .border = border, }; + + return &mem.pane; } - fn draw(pane_ptr: *pane.Pane, term_io: *TermIO) !void { + fn draw(pane_ptr: *Pane, term_io: *TermIO) !void { const self: *HorizontalLine = @fieldParentPtr("pane", pane_ptr); - const y = pane_ptr.calcDims.size.height / 2; + const y = pane_ptr.dimensions.size.height / 2; pane_ptr.cursor = .{ .x = 0, .y = y }; pane_ptr.moveCursor(term_io); - for (0..pane_ptr.calcDims.size.width) |_| { + for (0..pane_ptr.dimensions.size.width) |_| { term_io.print("{u}", .{self.border.top}); } } }; pub const VerticalLine = struct { - pane: pane.Pane, + pane: Pane, }; diff --git a/src/main.zig b/src/main.zig index a3a4926..83b0183 100644 --- a/src/main.zig +++ b/src/main.zig @@ -26,28 +26,19 @@ fn on_click() void { fn on_side_select(item: usize) void { log.debug("Side menu: Item {d} was selected", .{item}); - if (item == 1) { + if (item == 0) { pane.focus(&m.pane, &term_io); } } fn on_select(item: usize) void { log.debug("Main menu: Item {d} was selected", .{item}); - if (item == 1) { + if (item == 0) { pane.focus(&side_menu.pane, &term_io); } } pub fn main() !void { - var gpa = std.heap.GeneralPurposeAllocator(.{}){}; - const allocator = gpa.allocator(); - defer { - if (gpa.deinit() == .leak) { - log.warn("Memory was leaked D:", .{}); - std.debug.print("Memory was leaked D:\n", .{}); - } - } - try log.init("panes.log", .Debug); defer log.deinit(); @@ -55,60 +46,58 @@ pub fn main() !void { var stdout = std.io.bufferedWriter(std.io.getStdOut().writer().any()); term_io = try term.getTermIO(stdin.any(), stdout.writer()); - var top = pane.Pane{ - .dimensions = .{ - .x = dim.Fill, - .y = dim.Fill, - }, - .border = borders.BoldBorder, - .children = std.ArrayList(*pane.Pane).init(allocator), - .background = color.RGB(30, 30, 30), - .foreground = color.RGB(0, 255, 0), - }; + var top: stack.Stack = undefined; + var child: stack.Stack = undefined; + var button: btn.Button = undefined; + var tabbar: tabs.TabBar = undefined; + + var items = [_]menu.MenuItem{ .{ .name = "Item 1" }, .{ .name = "Item ab" } }; + var items2 = [_]menu.MenuItem{ .{ .name = "Item 1" }, .{ .name = "Item ab" } }; + var tabs_ = [_]tabs.Tab{ .{ .name = "Tab 1", .pane = &side_menu.pane }, .{ .name = "Tab 2", .pane = &child.pane }, .{ .name = "Tab 3", .pane = &child.pane }, .{ .name = "Tab 4", .pane = &child.pane }, .{ .name = "Tab 5", .pane = &child.pane } }; + + _ = stack.Stack.create(&top, .Horizontal, &[_]stack.StackedPane{ + .{ + .pane = menu.Menu.create(&side_menu, .{ .title = "Menu", .align_text = .Center, .expand_highlight = true, .on_select = on_side_select }, &items2), + .dimensions = .{ .width = .{ .type = .Fill, .value = 1 }, .height = .{ .type = .Fill, .value = 100 } }, + }, + .{ + .pane = stack.Stack.create(&child, .Vertical, &[_]stack.StackedPane{ + .{ + .pane = tabs.TabBar.create(&tabbar, .{ .highlight_color = color.RGB(0, 0, 255) }, &tabs_), + .dimensions = .{ .width = .{ .type = .Fill, .value = 100 }, .height = .{ .type = .Absolute, .value = 1 } }, + }, + .{ + .pane = menu.Menu.create(&m, .{ .title = "Test", .align_text = .Left, .expand_highlight = true, .on_select = on_select }, &items), + .dimensions = .{ .width = .{ .type = .Fill, .value = 100 }, .height = .{ .type = .Fill, .value = 1 } }, + }, + .{ + .pane = btn.Button.create(&button, .{ .text = "