From c1eea0352e79f7a052cf45f815f7e4bafc5a75f4 Mon Sep 17 00:00:00 2001 From: Cameron Reed Date: Mon, 14 Oct 2024 17:35:47 -0600 Subject: [PATCH] Better styling --- src/button.zig | 18 +++++++---- src/line.zig | 23 +++++++++++++ src/main.zig | 35 ++++++++------------ src/menu.zig | 24 ++++++++------ src/pane.zig | 88 ++++++++++++++++++++++++++------------------------ src/stack.zig | 33 ++++++------------- src/tabs.zig | 11 ++++--- 7 files changed, 124 insertions(+), 108 deletions(-) diff --git a/src/button.zig b/src/button.zig index 503b910..539ede9 100644 --- a/src/button.zig +++ b/src/button.zig @@ -2,24 +2,28 @@ const std = @import("std"); 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, -}; +const Pane = pane.Pane; +const Style = pane.Style; +const TermIO = term.TermIO; pub const Button = struct { pane: Pane, text: []const u8, callback: *const fn () void, - pub fn create(mem: *Button, config: ButtonConfig) *Pane { + pub const Config = struct { + text: []const u8, + callback: *const fn () void, + style: Style = .{}, + }; + + pub fn create(mem: *Button, config: Config) *Pane { mem.text = config.text; mem.callback = config.callback; mem.pane = Pane{ .focusable = true, + .style = config.style, .vtable = .{ .draw = draw, .update = update, diff --git a/src/line.zig b/src/line.zig index 0477940..68638c5 100644 --- a/src/line.zig +++ b/src/line.zig @@ -34,4 +34,27 @@ pub const HorizontalLine = struct { pub const VerticalLine = struct { pane: Pane, + border: borders.Border, + + pub fn create(mem: *VerticalLine, border: borders.Border) *Pane { + mem.border = border; + mem.pane = Pane{ + .vtable = .{ + .draw = draw, + }, + }; + + return &mem.pane; + } + + fn draw(pane_ptr: *Pane, term_io: *TermIO) !void { + const self: *VerticalLine = @fieldParentPtr("pane", pane_ptr); + const x = pane_ptr.dimensions.size.width / 2; + + for (0..pane_ptr.dimensions.size.height) |h| { + pane_ptr.cursor = .{ .x = x, .y = h }; + pane_ptr.moveCursor(term_io); + term_io.print("{u}", .{self.border.left}); + } + } }; diff --git a/src/main.zig b/src/main.zig index 83b0183..e4e82ac 100644 --- a/src/main.zig +++ b/src/main.zig @@ -50,28 +50,36 @@ pub fn main() !void { var child: stack.Stack = undefined; var button: btn.Button = undefined; var tabbar: tabs.TabBar = undefined; + var lineV: line.VerticalLine = undefined; + var lineH: line.HorizontalLine = 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{ + const green = color.RGB(0, 255, 0); + const purple = color.RGB(125, 0, 125); + const orange = color.RGB(255, 125, 10); + + _ = stack.Stack.create(&top, .{ .direction = .Horizontal, .style = .{ .border = borders.BoldBorder, .background = color.RGB(30, 30, 30), .foreground = green } }, &[_]stack.StackedPane{ .{ - .pane = menu.Menu.create(&side_menu, .{ .title = "Menu", .align_text = .Center, .expand_highlight = true, .on_select = on_side_select }, &items2), + .pane = menu.Menu.create(&side_menu, .{ .title = "Menu", .style = .{ .background = color.RGB(80, 80, 80), .foreground = green }, .align_text = .Center, .expand_highlight = true, .on_select = on_side_select }, &items2), .dimensions = .{ .width = .{ .type = .Fill, .value = 1 }, .height = .{ .type = .Fill, .value = 100 } }, }, + .{ .pane = line.VerticalLine.create(&lineV, borders.BoldBorder), .dimensions = .{ .width = .{ .type = .Absolute, .value = 1 }, .height = .{ .type = .Fill, .value = 100 } } }, .{ - .pane = stack.Stack.create(&child, .Vertical, &[_]stack.StackedPane{ + .pane = stack.Stack.create(&child, .{ .direction = .Vertical, .style = .{ .border = borders.BoldBorder, .background = purple, .foreground = orange } }, &[_]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), + .pane = menu.Menu.create(&m, .{ .title = "Test", .style = .{ .background = purple, .foreground = orange }, .align_text = .Left, .expand_highlight = true, .on_select = on_select }, &items), .dimensions = .{ .width = .{ .type = .Fill, .value = 100 }, .height = .{ .type = .Fill, .value = 1 } }, }, + .{ .pane = line.HorizontalLine.create(&lineH, borders.BoldBorder), .dimensions = .{ .width = .{ .type = .Fill, .value = 100 }, .height = .{ .type = .Absolute, .value = 1 } } }, .{ - .pane = btn.Button.create(&button, .{ .text = "