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