╭ 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 pub const AuthTab = @import("./../AuthTab.zig"); 12 13 title: ui.Title, 14 auth_tab: AuthTab, 15 16 const Self = @This(); 17 18 pub fn init(arena: *std.heap.ArenaAllocator) !Self { 19 return .{ 20 .title = try ui.Title.init(arena, "haxy"), 21 .auth_tab = AuthTab.init(), 22 }; 23 } 24 25 pub const View = struct { 26 box: wgt.Box(ui.Widget), 27 data: *const Self, 28 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 29 30 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 31 var box = wgt.Box(ui.Widget).init(.{ .border_style = .hidden, .rounded_corners = true, .direction = .horiz }); 32 errdefer box.deinit(allocator); 33 34 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 35 errdefer tab_ids.deinit(allocator); 36 37 // title sits to the left of the tabs 38 { 39 var title_view = try ui.Title.View.init(allocator, &data.title); 40 errdefer title_view.deinit(allocator); 41 // shrink the title when there is not enough space 42 try box.children.put(allocator, title_view.getFocus().id, .{ 43 .widget = .{ .title = title_view }, 44 .rect = null, 45 .min_size = null, 46 .shrink = true, 47 }); 48 } 49 50 // spacer 51 { 52 var text = wgt.Text(ui.Widget).init(" "); 53 errdefer text.deinit(allocator); 54 try box.children.put(allocator, text.getFocus().id, .{ 55 .widget = .{ .text = text }, 56 .rect = null, 57 .min_size = .{ .width = 1, .height = null }, 58 }); 59 } 60 61 // tabs 62 for ( 63 [_][]const u8{ "users", "repos", "", "settings", "" }, 64 [_][]const u8{ "a:/users", "a:/repos", "", "a:/settings", "a:/auth" }, 65 ) |name, focus_name| { 66 // spacer 67 if (focus_name.len == 0) { 68 var spacer = ui.Spacer.init(); 69 errdefer spacer.deinit(allocator); 70 try box.children.put(allocator, spacer.getFocus().id, .{ 71 .widget = .{ .spacer = spacer }, 72 .rect = null, 73 .min_size = null, 74 }); 75 } 76 // auth 77 else if (std.mem.eql(u8, "a:/auth", focus_name)) { 78 var auth_tab = try AuthTab.View.init(allocator, &data.auth_tab, session); 79 errdefer auth_tab.deinit(allocator); 80 try tab_ids.put(allocator, auth_tab.getFocus().id, {}); 81 try box.children.put(allocator, auth_tab.getFocus().id, .{ 82 .widget = .{ .auth_tab = auth_tab }, 83 .rect = null, 84 .min_size = .{ .width = AuthTab.min_width, .height = null }, 85 }); 86 } 87 // other tabs 88 else { 89 var text_box = try wgt.TextBox(ui.Widget).init(allocator, name, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 90 errdefer text_box.deinit(allocator); 91 text_box.getFocus().focusable = true; 92 text_box.getFocus().kind = .{ .custom = focus_name }; 93 try tab_ids.put(allocator, text_box.getFocus().id, {}); 94 try box.children.put(allocator, text_box.getFocus().id, .{ 95 .widget = .{ .text_box = text_box }, 96 .rect = null, 97 .min_size = .{ .width = name.len + 2, .height = null }, 98 }); 99 } 100 } 101 102 // quit tab 103 if (session.is_terminal) { 104 var text_box = try wgt.TextBox(ui.Widget).init(allocator, ui.Quit.tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 105 errdefer text_box.deinit(allocator); 106 text_box.getFocus().focusable = true; 107 text_box.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 108 try tab_ids.put(allocator, text_box.getFocus().id, {}); 109 try box.children.put(allocator, text_box.getFocus().id, .{ 110 .widget = .{ .text_box = text_box }, 111 .rect = null, 112 // the label is a single column; +2 for the border 113 .min_size = .{ .width = 1 + 2, .height = null }, 114 }); 115 } 116 117 var self = View{ .box = box, .data = data, .tab_ids = tab_ids }; 118 // initial selected tab 119 self.getFocus().child_id = self.tab_ids.keys()[ 120 switch (session.data.current_page) { 121 .home_users => 0, 122 .home_repos => 1, 123 .home_settings => 2, 124 .home_auth => 3, 125 else => 0, 126 } 127 ]; 128 return self; 129 } 130 131 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 132 self.box.deinit(allocator); 133 self.tab_ids.deinit(allocator); 134 } 135 136 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 137 self.clearGrid(); 138 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 139 const tb: ?*wgt.TextBox(ui.Widget) = switch (child.widget) { 140 .text_box => |*x| x, 141 .auth_tab => |*at| &at.text_box, 142 else => null, 143 }; 144 if (tb) |t| { 145 t.options.border_style = if (self.getFocus().child_id == id) .single else .hidden; 146 } 147 } 148 try self.box.build(allocator, constraint, root_focus); 149 } 150 151 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 152 _ = allocator; 153 const current_tab = self.currentTabIndex() orelse return; 154 var new_tab = current_tab; 155 switch (key) { 156 .arrow_left => new_tab -|= 1, 157 .arrow_right => if (new_tab + 1 < self.tab_ids.count()) { 158 new_tab += 1; 159 }, 160 else => {}, 161 } 162 if (new_tab != current_tab) { 163 try root_focus.setFocus(self.tab_ids.keys()[new_tab]); 164 } 165 } 166 167 pub fn clearGrid(self: *View) void { 168 self.box.clearGrid(); 169 } 170 171 pub fn getGrid(self: View) ?Grid { 172 return self.box.getGrid(); 173 } 174 175 pub fn getFocus(self: *View) *Focus { 176 return self.box.getFocus(); 177 } 178 179 pub fn getSelectedIndex(self: View) ?usize { 180 return self.currentTabIndex(); 181 } 182 183 fn currentTabIndex(self: View) ?usize { 184 const child_id = self.box.focus.child_id orelse return null; 185 return self.tab_ids.getIndex(child_id); 186 } 187 }; 188