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