╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const ui = @import("../../ui.zig"); 3 const inp = @import("../input.zig"); 4 const xit = @import("xit"); 5 const xitui = xit.xitui; 6 const wgt = xitui.widget; 7 const layout = xitui.layout; 8 const Key = xitui.input.Key; 9 const Grid = xitui.grid.Grid; 10 const Focus = xitui.focus.Focus; 11 12 pub const AuthTab = @import("./../AuthTab.zig"); 13 14 name: []const u8, 15 title: ui.Title, 16 auth_tab: AuthTab, 17 18 const Self = @This(); 19 20 pub fn init(arena: *std.heap.ArenaAllocator, name: []const u8) !Self { 21 return .{ 22 .name = name, 23 .title = try ui.Title.init(arena, name), 24 .auth_tab = AuthTab.init(), 25 }; 26 } 27 28 pub const View = struct { 29 scroll: wgt.Scroll(ui.Widget), 30 data: *const Self, 31 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 32 33 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 34 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .hidden, .rounded_corners = true, .direction = .horiz }); 35 errdefer box.deinit(allocator); 36 37 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 38 errdefer tab_ids.deinit(allocator); 39 40 // title sits to the left of the tabs 41 { 42 var title_view = try ui.Title.View.init(allocator, &data.title); 43 errdefer title_view.deinit(allocator); 44 title_view.getFocus().focusable = true; 45 title_view.getFocus().kind = .{ .custom = "a:/" }; 46 // shrink the title when there is not enough space 47 try box.children.put(allocator, title_view.getFocus().id, .{ 48 .widget = .{ .title = title_view }, 49 .rect = null, 50 .min_size = null, 51 .flex = .shrink, 52 }); 53 } 54 55 // spacer 56 { 57 var text = try wgt.Text(ui.Widget).init(allocator, " "); 58 errdefer text.deinit(allocator); 59 try box.children.put(allocator, text.getFocus().id, .{ 60 .widget = .{ .text = text }, 61 .rect = null, 62 .min_size = .{ .width = 1, .height = null }, 63 }); 64 } 65 66 // every tab link is user-scoped so selecting one stays on this page — 67 // switching its stack and updating the url — instead of navigating to 68 // the global settings or auth pages. the "ai:" prefix makes each an 69 // in-page anchor: crossPageLink ignores it so a wasm click just switches 70 // tabs (the page already holds every tab's content), while the href is 71 // still followed with js off. allocated in the page arena so the focus 72 // kinds that borrow them live as long as this page's widget tree. 73 const aa = session.page_arena.allocator(); 74 const repos_link = try std.fmt.allocPrint(aa, "ai:/user/{s}", .{data.name}); 75 const forks_link = try std.fmt.allocPrint(aa, "ai:/user/{s}/forks", .{data.name}); 76 const settings_link = try std.fmt.allocPrint(aa, "ai:/user/{s}/settings", .{data.name}); 77 const auth_link = try std.fmt.allocPrint(aa, "ai:/user/{s}/auth", .{data.name}); 78 79 // the tab matching the current page is focused initially; matching by 80 // link (rather than position) keeps this robust to tab changes. 81 const current_link: []const u8 = switch (session.data.current_page) { 82 .user_settings => settings_link, 83 .user_auth => auth_link, 84 .user_forks => forks_link, 85 else => repos_link, 86 }; 87 var selected_tab: ?usize = null; 88 89 // repos tab 90 { 91 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "repos", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 92 errdefer text_box.deinit(allocator); 93 text_box.getFocus().focusable = true; 94 text_box.getFocus().kind = .{ .custom = repos_link }; 95 try tab_ids.put(allocator, text_box.getFocus().id, {}); 96 if (std.mem.eql(u8, repos_link, current_link)) selected_tab = text_box.getFocus().id; 97 try box.children.put(allocator, text_box.getFocus().id, .{ 98 .widget = .{ .text_box = text_box }, 99 .rect = null, 100 .min_size = .{ .width = "repos".len + 2, .height = null }, 101 }); 102 } 103 104 // forks tab 105 { 106 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "forks", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 107 errdefer text_box.deinit(allocator); 108 text_box.getFocus().focusable = true; 109 text_box.getFocus().kind = .{ .custom = forks_link }; 110 try tab_ids.put(allocator, text_box.getFocus().id, {}); 111 if (std.mem.eql(u8, forks_link, current_link)) selected_tab = text_box.getFocus().id; 112 try box.children.put(allocator, text_box.getFocus().id, .{ 113 .widget = .{ .text_box = text_box }, 114 .rect = null, 115 .min_size = .{ .width = "forks".len + 2, .height = null }, 116 }); 117 } 118 119 // spacer pushes settings + auth to the right 120 { 121 var spacer = try ui.Spacer.init(allocator); 122 errdefer spacer.deinit(allocator); 123 try box.children.put(allocator, spacer.getFocus().id, .{ 124 .widget = .{ .spacer = spacer }, 125 .rect = null, 126 .min_size = null, 127 .flex = .grow, 128 }); 129 } 130 131 // settings tab. settings are account preferences, so it needs a login. 132 if (session.data.user_id != null) { 133 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "settings", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 134 errdefer text_box.deinit(allocator); 135 text_box.getFocus().focusable = true; 136 text_box.getFocus().kind = .{ .custom = settings_link }; 137 try tab_ids.put(allocator, text_box.getFocus().id, {}); 138 if (std.mem.eql(u8, settings_link, current_link)) selected_tab = text_box.getFocus().id; 139 try box.children.put(allocator, text_box.getFocus().id, .{ 140 .widget = .{ .text_box = text_box }, 141 .rect = null, 142 .min_size = .{ .width = "settings".len + 2, .height = null }, 143 }); 144 } 145 146 // auth tab (login / logout). AuthTab defaults to the global ai:/auth 147 // link; repoint its instance at this user's auth route so it stays local. 148 { 149 var auth_tab = try AuthTab.View.init(allocator, session); 150 errdefer auth_tab.deinit(allocator); 151 auth_tab.text_box.getFocus().kind = .{ .custom = auth_link }; 152 try tab_ids.put(allocator, auth_tab.getFocus().id, {}); 153 if (std.mem.eql(u8, auth_link, current_link)) selected_tab = auth_tab.getFocus().id; 154 try box.children.put(allocator, auth_tab.getFocus().id, .{ 155 .widget = .{ .auth_tab = auth_tab }, 156 .rect = null, 157 .min_size = .{ .width = auth_tab.minWidth(), .height = null }, 158 }); 159 } 160 161 // quit tab 162 if (session.is_terminal) { 163 var text_box = try wgt.TextBox(ui.Widget).init(allocator, ui.Quit.tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 164 errdefer text_box.deinit(allocator); 165 text_box.getFocus().focusable = true; 166 text_box.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 167 try tab_ids.put(allocator, text_box.getFocus().id, {}); 168 try box.children.put(allocator, text_box.getFocus().id, .{ 169 .widget = .{ .text_box = text_box }, 170 .rect = null, 171 // the label is a single column; +2 for the border 172 .min_size = .{ .width = 1 + 2, .height = null }, 173 }); 174 } 175 176 var self = View{ 177 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = box }, .{ .direction = .horiz, .show_bar = false, .web_native = !session.is_terminal }), 178 .data = data, 179 .tab_ids = tab_ids, 180 }; 181 self.getFocus().child_id = selected_tab orelse self.tab_ids.keys()[0]; 182 return self; 183 } 184 185 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 186 self.scroll.deinit(allocator); 187 self.tab_ids.deinit(allocator); 188 } 189 190 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 191 self.clearGrid(); 192 // only the selected tab shows its border 193 const box = &self.scroll.child.box; 194 for (box.children.keys(), box.children.values()) |id, *child| { 195 const tb: ?*wgt.TextBox(ui.Widget) = switch (child.widget) { 196 .text_box => |*x| x, 197 .auth_tab => |*at| blk: { 198 // the label tracks login state per frame, so the width must too 199 child.min_size = .{ .width = at.minWidth(), .height = null }; 200 break :blk &at.text_box; 201 }, 202 else => null, 203 }; 204 if (tb) |t| { 205 t.options.border_style = if (self.getFocus().child_id == id) .single else .hidden; 206 } 207 } 208 var scroll_constraint = constraint; 209 scroll_constraint.min_size.width = constraint.max_size.width orelse constraint.min_size.width; 210 try self.scroll.build(allocator, scroll_constraint, root_focus); 211 } 212 213 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 214 _ = allocator; 215 const current_tab = self.currentTabIndex() orelse return; 216 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 217 const tab_id = self.tab_ids.keys()[new_tab]; 218 root_focus.setFocus(tab_id); 219 const tab = self.scroll.child.box.children.get(tab_id) orelse return; 220 if (tab.rect) |rect| self.scroll.scrollToRect(rect); 221 } 222 } 223 224 pub fn clearGrid(self: *View) void { 225 self.scroll.clearGrid(); 226 } 227 228 pub fn getGrid(self: View) ?Grid { 229 return self.scroll.getGrid(); 230 } 231 232 pub fn getFocus(self: *View) *Focus { 233 return self.scroll.getFocus(); 234 } 235 236 pub fn getSelectedIndex(self: View) ?usize { 237 return self.currentTabIndex(); 238 } 239 240 fn currentTabIndex(self: View) ?usize { 241 const child_id = self.scroll.child.box.focus.child_id orelse return null; 242 return self.tab_ids.getIndex(child_id); 243 } 244 }; 245