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