╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const ui = @import("../../ui.zig"); 3 const xit = @import("xit"); 4 const xitui = xit.xitui; 5 const wgt = xitui.widget; 6 const layout = xitui.layout; 7 const inp = xitui.input; 8 const Grid = xitui.grid.Grid; 9 const Focus = xitui.focus.Focus; 10 11 const Self = @This(); 12 13 const Tab = enum { users, repos }; 14 15 pub fn init() Self { 16 return .{}; 17 } 18 19 pub const View = struct { 20 box: wgt.Box(ui.Widget), 21 data: *const Self, 22 23 pub fn init(allocator: std.mem.Allocator, data: *const Self) !View { 24 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 25 errdefer box.deinit(); 26 27 inline for (@typeInfo(Tab).@"enum".fields) |tab_field| { 28 const tab: Tab = @enumFromInt(tab_field.value); 29 const name = switch (tab) { 30 .users => "users", 31 .repos => "repos", 32 }; 33 var text_box = try wgt.TextBox(ui.Widget).init(allocator, name, .{ .border_style = .single, .wrap_kind = .none }); 34 errdefer text_box.deinit(); 35 text_box.getFocus().focusable = true; 36 try box.children.put(box.allocator, text_box.getFocus().id, .{ .widget = .{ .text_box = text_box }, .rect = null, .min_size = null }); 37 } 38 39 var self = View{ .box = box, .data = data }; 40 self.getFocus().child_id = box.children.keys()[0]; 41 return self; 42 } 43 44 pub fn deinit(self: *View) void { 45 self.box.deinit(); 46 } 47 48 pub fn build(self: *View, constraint: layout.Constraint, root_focus: *Focus) !void { 49 self.clearGrid(); 50 for (self.box.children.keys(), self.box.children.values()) |id, *tab| { 51 tab.widget.text_box.options.border_style = if (self.getFocus().child_id == id) 52 (if (root_focus.grandchild_id == id) .double else .single) 53 else 54 .hidden; 55 } 56 try self.box.build(constraint, root_focus); 57 } 58 59 pub fn input(self: *View, key: inp.Key, root_focus: *Focus) !void { 60 if (self.getFocus().child_id) |child_id| { 61 const children = &self.box.children; 62 if (children.getIndex(child_id)) |current_index| { 63 var index = current_index; 64 65 switch (key) { 66 .arrow_left => { 67 index -|= 1; 68 }, 69 .arrow_right => { 70 if (index + 1 < children.count()) { 71 index += 1; 72 } 73 }, 74 else => {}, 75 } 76 77 if (index != current_index) { 78 try root_focus.setFocus(children.keys()[index]); 79 } 80 } 81 } 82 } 83 84 pub fn clearGrid(self: *View) void { 85 self.box.clearGrid(); 86 } 87 88 pub fn getGrid(self: View) ?Grid { 89 return self.box.getGrid(); 90 } 91 92 pub fn getFocus(self: *View) *Focus { 93 return self.box.getFocus(); 94 } 95 96 pub fn getSelectedIndex(self: View) ?usize { 97 if (self.box.focus.child_id) |child_id| { 98 return self.box.children.getIndex(child_id); 99 } else { 100 return null; 101 } 102 } 103 }; 104