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