╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
48 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 49 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .hidden, .rounded_corners = true, .direction = .horiz }); 50 errdefer box.deinit(allocator); 51 52 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 53 errdefer tab_ids.deinit(allocator); 54 55 const aa = session.page_arena.allocator(); 56 57 // the user's name (local mode has no user pages to link to) 58 if (!session.data.is_local) { 59 const text = try std.fmt.allocPrint(aa, "{s}/", .{data.owner_name}); 60 const link = try std.fmt.allocPrint(aa, "a:/user/{s}", .{data.owner_name}); 61 62 var text_box = try wgt.TextBox(ui.Widget).init(allocator, text, .{ .border_style = .hidden, .wrap_kind = .none }); 63 errdefer text_box.deinit(allocator); 64 text_box.getFocus().focusable = true; 65 text_box.getFocus().kind = .{ .custom = link }; 66 try box.children.put(allocator, text_box.getFocus().id, .{ 67 .widget = .{ .text_box = text_box }, 68 .rect = null, 69 .min_size = null, 70 .shrink = true, 71 }); 72 } 73 74 // local routes carry no identity, so their urls come out elided 75 const identity = if (session.data.is_local) "" else try std.fmt.allocPrint(aa, "{s}/{s}", .{ data.owner_name, data.name }); 76 77 // title sits to the left of the tabs; it links to the repo's files root 78 // (the bare route, so it resolves to the default branch). 79 { 80 const files_root_route = ui.RoutablePage.repoFilesRoute(identity, null, "", "", 0) orelse return error.RouteTooLong; 81 const title_link = try std.fmt.allocPrint(aa, "a:{s}", .{try files_root_route.toUrl(session.page_arena)}); 82 var title_view = try ui.Title.View.init(allocator, &data.title); 83 errdefer title_view.deinit(allocator); 84 title_view.getFocus().focusable = true; 85 title_view.getFocus().kind = .{ .custom = title_link }; 86 // shrink the title when there is not enough space 87 try box.children.put(allocator, title_view.getFocus().id, .{ 88 .widget = .{ .title = title_view }, 89 .rect = null, 90 .min_size = null, 91 .shrink = true, 92 }); 93 } 94 95 // spacer 96 { 97 var text = try wgt.Text(ui.Widget).init(allocator, " "); 98 errdefer text.deinit(allocator); 99 try box.children.put(allocator, text.getFocus().id, .{ 100 .widget = .{ .text = text }, 101 .rect = null, 102 .min_size = .{ .width = 1, .height = null }, 103 }); 104 } 105 106 // every tab link is repo-scoped so selecting one stays on this page — 107 // switching its stack and updating the url — instead of navigating to 108 // the global settings or auth pages. the "ai:" prefix makes each an 109 // in-page anchor: crossPageLink ignores it so a wasm click just switches 110 // tabs (the page already holds every tab's content), while the href is 111 // still followed with js off. the files tab routes through the shared 112 // helper so the /repo/.../files url format lives in one place. 113 // both tabs link to the ref/oid this page is viewing, so switching tabs 114 // keeps the same ref (the files tab opens at its root directory). 115 const files_route = ui.RoutablePage.repoFilesRoute(identity, data.ref_or_oid, data.ref_or_oid_value, "", 0) orelse return error.RouteTooLong; 116 const files_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try files_route.toUrl(session.page_arena)}); 117 const commits_route = ui.RoutablePage.repoCommitsRoute(identity, data.ref_or_oid, data.ref_or_oid_value, 0, "") orelse return error.RouteTooLong; 118 const commits_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try commits_route.toUrl(session.page_arena)}); 119 const refs_route = ui.RoutablePage.repoRefsRoute(identity, .branch, "") orelse return error.RouteTooLong; 120 const refs_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try refs_route.toUrl(session.page_arena)}); 121 const issues_route = ui.RoutablePage.repoIssuesRoute(identity, .open, data.issues_tag, "") orelse return error.RouteTooLong; 122 const issues_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try issues_route.toUrl(session.page_arena)}); 123 const settings_route = ui.RoutablePage{ .repo_settings = Array.from(identity) orelse return error.RouteTooLong }; 124 const settings_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try settings_route.toUrl(session.page_arena)}); 125 const auth_route = ui.RoutablePage{ .repo_auth = Array.from(identity) orelse return error.RouteTooLong }; 126 const auth_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try auth_route.toUrl(session.page_arena)}); 127 128 // the tab matching the current page is focused initially; matching by 129 // link (rather than position) keeps this robust to tab changes. 130 const current_link: []const u8 = switch (session.data.current_page) { 131 .repo_commits => commits_link, 132 .repo_refs => refs_link, 133 .repo_issues => issues_link, 134 .repo_settings => settings_link, 135 .repo_auth => auth_link, 136 else => files_link, 137 }; 138 var selected_tab: ?usize = null; 139 140 // files tab 141 { 142 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "files", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 143 errdefer text_box.deinit(allocator); 144 text_box.getFocus().focusable = true; 145 text_box.getFocus().kind = .{ .custom = files_link }; 146 try tab_ids.put(allocator, text_box.getFocus().id, {}); 147 if (std.mem.eql(u8, files_link, current_link)) selected_tab = text_box.getFocus().id; 148 try box.children.put(allocator, text_box.getFocus().id, .{ 149 .widget = .{ .text_box = text_box }, 150 .rect = null, 151 .min_size = .{ .width = "files".len + 2, .height = null }, 152 }); 153 } 154 155 // commits tab 156 { 157 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "commits", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 158 errdefer text_box.deinit(allocator); 159 text_box.getFocus().focusable = true; 160 text_box.getFocus().kind = .{ .custom = commits_link }; 161 try tab_ids.put(allocator, text_box.getFocus().id, {}); 162 if (std.mem.eql(u8, commits_link, current_link)) selected_tab = text_box.getFocus().id; 163 try box.children.put(allocator, text_box.getFocus().id, .{ 164 .widget = .{ .text_box = text_box }, 165 .rect = null, 166 .min_size = .{ .width = "commits".len + 2, .height = null }, 167 }); 168 } 169 170 // refs tab 171 { 172 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "refs", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 173 errdefer text_box.deinit(allocator); 174 text_box.getFocus().focusable = true; 175 text_box.getFocus().kind = .{ .custom = refs_link }; 176 try tab_ids.put(allocator, text_box.getFocus().id, {}); 177 if (std.mem.eql(u8, refs_link, current_link)) selected_tab = text_box.getFocus().id; 178 try box.children.put(allocator, text_box.getFocus().id, .{ 179 .widget = .{ .text_box = text_box }, 180 .rect = null, 181 .min_size = .{ .width = "refs".len + 2, .height = null }, 182 }); 183 } 184 185 // issues tab 186 { 187 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "issues", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 188 errdefer text_box.deinit(allocator); 189 text_box.getFocus().focusable = true; 190 text_box.getFocus().kind = .{ .custom = issues_link }; 191 try tab_ids.put(allocator, text_box.getFocus().id, {}); 192 if (std.mem.eql(u8, issues_link, current_link)) selected_tab = text_box.getFocus().id; 193 try box.children.put(allocator, text_box.getFocus().id, .{ 194 .widget = .{ .text_box = text_box }, 195 .rect = null, 196 .min_size = .{ .width = "issues".len + 2, .height = null }, 197 }); 198 } 199 200 // spacer pushes settings + auth to the right 201 { 202 var spacer = try ui.Spacer.init(allocator); 203 errdefer spacer.deinit(allocator); 204 try box.children.put(allocator, spacer.getFocus().id, .{ 205 .widget = .{ .spacer = spacer }, 206 .rect = null, 207 .min_size = null, 208 }); 209 } 210 211 // settings tab. settings are account preferences, so it needs a login 212 // (which also rules out local mode, which has no accounts). 213 if (session.data.user_id != null) { 214 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "settings", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 215 errdefer text_box.deinit(allocator); 216 text_box.getFocus().focusable = true; 217 text_box.getFocus().kind = .{ .custom = settings_link }; 218 try tab_ids.put(allocator, text_box.getFocus().id, {}); 219 if (std.mem.eql(u8, settings_link, current_link)) selected_tab = text_box.getFocus().id; 220 try box.children.put(allocator, text_box.getFocus().id, .{ 221 .widget = .{ .text_box = text_box }, 222 .rect = null, 223 .min_size = .{ .width = "settings".len + 2, .height = null }, 224 }); 225 } 226 227 // auth tab (login / logout). AuthTab defaults to the global ai:/auth 228 // link; repoint its instance at this repo's auth route so it stays on 229 // this page. local mode has no accounts, so it has no auth tab. 230 if (!session.data.is_local) { 231 var auth_tab = try AuthTab.View.init(allocator, session); 232 errdefer auth_tab.deinit(allocator); 233 auth_tab.text_box.getFocus().kind = .{ .custom = auth_link }; 234 try tab_ids.put(allocator, auth_tab.getFocus().id, {}); 235 if (std.mem.eql(u8, auth_link, current_link)) selected_tab = auth_tab.getFocus().id; 236 try box.children.put(allocator, auth_tab.getFocus().id, .{ 237 .widget = .{ .auth_tab = auth_tab }, 238 .rect = null, 239 .min_size = .{ .width = auth_tab.minWidth(), .height = null }, 240 }); 241 } 242 243 // quit tab 244 if (session.is_terminal) { 245 var text_box = try wgt.TextBox(ui.Widget).init(allocator, ui.Quit.tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 246 errdefer text_box.deinit(allocator); 247 text_box.getFocus().focusable = true; 248 text_box.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 249 try tab_ids.put(allocator, text_box.getFocus().id, {}); 250 try box.children.put(allocator, text_box.getFocus().id, .{ 251 .widget = .{ .text_box = text_box }, 252 .rect = null, 253 // the label is a single column; +2 for the border 254 .min_size = .{ .width = 1 + 2, .height = null }, 255 }); 256 } 257 258 var self = View{ .box = box, .data = data, .tab_ids = tab_ids }; 259 self.getFocus().child_id = selected_tab orelse self.tab_ids.keys()[0]; 260 return self; 261 } 262 263 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 264 self.box.deinit(allocator); 265 self.tab_ids.deinit(allocator); 266 } 267 268 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 269 self.clearGrid(); 270 // only the selected tab shows its border 271 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 272 const tb: ?*wgt.TextBox(ui.Widget) = switch (child.widget) { 273 .text_box => |*x| x, 274 .auth_tab => |*at| blk: { 275 // the label tracks login state per frame, so the width must too 276 child.min_size = .{ .width = at.minWidth(), .height = null }; 277 break :blk &at.text_box; 278 }, 279 else => null, 280 }; 281 if (tb) |t| { 282 t.options.border_style = if (self.getFocus().child_id == id) .single else .hidden; 283 } 284 } 285 try self.box.build(allocator, constraint, root_focus); 286 } 287 288 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 289 _ = allocator; 290 const current_tab = self.currentTabIndex() orelse return; 291 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 292 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 293 } 294 } 295 296 pub fn clearGrid(self: *View) void { 297 self.box.clearGrid(); 298 } 299 300 pub fn getGrid(self: View) ?Grid { 301 return self.box.getGrid(); 302 } 303 304 pub fn getFocus(self: *View) *Focus { 305 return self.box.getFocus(); 306 } 307 308 pub fn getSelectedIndex(self: View) ?usize { 309 return self.currentTabIndex(); 310 } 311 312 fn currentTabIndex(self: View) ?usize { 313 const child_id = self.box.focus.child_id orelse return null; 314 return self.tab_ids.getIndex(child_id); 315 } 316 }; 317