╭ 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 name: []const u8, 14 title: ui.Title, 15 auth_tab: AuthTab, 16 17 const Self = @This(); 18 19 pub fn init(arena: *std.heap.ArenaAllocator, name: []const u8) !Self { 20 return .{ 21 .name = name, 22 .title = try ui.Title.init(arena, name), 23 .auth_tab = AuthTab.init(), 24 }; 25 } 26 27 pub const View = struct { 28 box: wgt.Box(ui.Widget), 29 data: *const Self, 30 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 31 32 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 33 var box = wgt.Box(ui.Widget).init(.{ .border_style = .hidden, .rounded_corners = true, .direction = .horiz }); 34 errdefer box.deinit(allocator); 35 36 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 37 errdefer tab_ids.deinit(allocator); 38 39 // title sits to the left of the tabs 40 { 41 var title_view = try ui.Title.View.init(allocator, &data.title); 42 errdefer title_view.deinit(allocator); 43 title_view.getFocus().focusable = true; 44 title_view.getFocus().kind = .{ .custom = "a:/" }; 45 try box.children.put(allocator, title_view.getFocus().id, .{ 46 .widget = .{ .title = title_view }, 47 .rect = null, 48 .min_size = .{ .width = data.title.width, .height = null }, 49 }); 50 } 51 52 // spacer 53 { 54 var text = wgt.Text(ui.Widget).init(" "); 55 errdefer text.deinit(allocator); 56 try box.children.put(allocator, text.getFocus().id, .{ 57 .widget = .{ .text = text }, 58 .rect = null, 59 .min_size = .{ .width = 1, .height = null }, 60 }); 61 } 62 63 // every tab link is user-scoped so selecting one stays on this page — 64 // switching its stack and updating the url — instead of navigating to 65 // the global settings or auth pages. allocated in the page arena so the 66 // focus kinds that borrow them live as long as this page's widget tree. 67 const aa = session.page_arena.allocator(); 68 const repos_link = try std.fmt.allocPrint(aa, "a:/user/{s}", .{data.name}); 69 const settings_link = try std.fmt.allocPrint(aa, "a:/user/{s}/settings", .{data.name}); 70 const auth_link = try std.fmt.allocPrint(aa, "a:/user/{s}/auth", .{data.name}); 71 72 // repos tab 73 { 74 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "repos", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 75 errdefer text_box.deinit(allocator); 76 text_box.getFocus().focusable = true; 77 text_box.getFocus().kind = .{ .custom = repos_link }; 78 try tab_ids.put(allocator, text_box.getFocus().id, {}); 79 try box.children.put(allocator, text_box.getFocus().id, .{ 80 .widget = .{ .text_box = text_box }, 81 .rect = null, 82 .min_size = .{ .width = "repos".len + 2, .height = null }, 83 }); 84 } 85 86 // spacer pushes settings + auth to the right 87 { 88 var spacer = ui.Spacer.init(); 89 errdefer spacer.deinit(allocator); 90 try box.children.put(allocator, spacer.getFocus().id, .{ 91 .widget = .{ .spacer = spacer }, 92 .rect = null, 93 .min_size = .{ .width = 1, .height = null }, 94 }); 95 } 96 97 // settings tab 98 { 99 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "settings", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 100 errdefer text_box.deinit(allocator); 101 text_box.getFocus().focusable = true; 102 text_box.getFocus().kind = .{ .custom = settings_link }; 103 try tab_ids.put(allocator, text_box.getFocus().id, {}); 104 try box.children.put(allocator, text_box.getFocus().id, .{ 105 .widget = .{ .text_box = text_box }, 106 .rect = null, 107 .min_size = .{ .width = "settings".len + 2, .height = null }, 108 }); 109 } 110 111 // auth tab (login / logout). AuthTab defaults to the global a:/auth link; 112 // repoint its instance at this user's auth route so it stays local too. 113 { 114 var auth_tab = try AuthTab.View.init(allocator, &data.auth_tab, session); 115 errdefer auth_tab.deinit(allocator); 116 auth_tab.text_box.getFocus().kind = .{ .custom = auth_link }; 117 try tab_ids.put(allocator, auth_tab.getFocus().id, {}); 118 try box.children.put(allocator, auth_tab.getFocus().id, .{ 119 .widget = .{ .auth_tab = auth_tab }, 120 .rect = null, 121 .min_size = .{ .width = AuthTab.min_width, .height = null }, 122 }); 123 } 124 125 // quit tab 126 if (session.is_terminal) { 127 var text_box = try wgt.TextBox(ui.Widget).init(allocator, ui.Quit.tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 128 errdefer text_box.deinit(allocator); 129 text_box.getFocus().focusable = true; 130 text_box.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 131 try tab_ids.put(allocator, text_box.getFocus().id, {}); 132 try box.children.put(allocator, text_box.getFocus().id, .{ 133 .widget = .{ .text_box = text_box }, 134 .rect = null, 135 // the label is a single column; +2 for the border 136 .min_size = .{ .width = 1 + 2, .height = null }, 137 }); 138 } 139 140 var self = View{ .box = box, .data = data, .tab_ids = tab_ids }; 141 // open on the tab named by the current route 142 self.getFocus().child_id = self.tab_ids.keys()[ 143 switch (session.data.current_page) { 144 .user_settings => 1, 145 .user_auth => 2, 146 else => 0, 147 } 148 ]; 149 return self; 150 } 151 152 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 153 self.box.deinit(allocator); 154 self.tab_ids.deinit(allocator); 155 } 156 157 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 158 self.clearGrid(); 159 // only the selected tab shows its border 160 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 161 const tb: ?*wgt.TextBox(ui.Widget) = switch (child.widget) { 162 .text_box => |*x| x, 163 .auth_tab => |*at| &at.text_box, 164 else => null, 165 }; 166 if (tb) |t| { 167 t.options.border_style = if (self.getFocus().child_id == id) .single else .hidden; 168 } 169 } 170 try self.box.build(allocator, constraint, root_focus); 171 } 172 173 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 174 _ = allocator; 175 const current_tab = self.currentTabIndex() orelse return; 176 var new_tab = current_tab; 177 switch (key) { 178 .arrow_left => new_tab -|= 1, 179 .arrow_right => if (new_tab + 1 < self.tab_ids.count()) { 180 new_tab += 1; 181 }, 182 else => {}, 183 } 184 if (new_tab != current_tab) { 185 try root_focus.setFocus(self.tab_ids.keys()[new_tab]); 186 } 187 } 188 189 pub fn clearGrid(self: *View) void { 190 self.box.clearGrid(); 191 } 192 193 pub fn getGrid(self: View) ?Grid { 194 return self.box.getGrid(); 195 } 196 197 pub fn getFocus(self: *View) *Focus { 198 return self.box.getFocus(); 199 } 200 201 pub fn getSelectedIndex(self: View) ?usize { 202 return self.currentTabIndex(); 203 } 204 205 fn currentTabIndex(self: View) ?usize { 206 const child_id = self.box.focus.child_id orelse return null; 207 return self.tab_ids.getIndex(child_id); 208 } 209 }; 210