╭ 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 29 const Self = @This(); 30 31 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) !Self { 32 return .{ 33 .name = name, 34 .owner_name = owner_name, 35 .title = try ui.Title.init(arena, name), 36 .auth_tab = AuthTab.init(), 37 .ref_or_oid = ref_or_oid, 38 .ref_or_oid_value = ref_or_oid_value, 39 .issues_tag = issues_tag, 40 }; 41 } 42 43 pub const View = struct { 44 box: wgt.Box(ui.Widget), 45 data: *const Self, 46 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 47 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 events_route = ui.RoutablePage.repoEventsRoute(identity, null, "") orelse return error.RouteTooLong; 124 const events_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try events_route.toUrl(session.page_arena)}); 125 const settings_route = ui.RoutablePage{ .repo_settings = Array.from(identity) orelse return error.RouteTooLong }; 126 const settings_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try settings_route.toUrl(session.page_arena)}); 127 const auth_route = ui.RoutablePage{ .repo_auth = Array.from(identity) orelse return error.RouteTooLong }; 128 const auth_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try auth_route.toUrl(session.page_arena)}); 129 130 // the tab matching the current page is focused initially; matching by 131 // link (rather than position) keeps this robust to tab changes. 132 const current_link: []const u8 = switch (session.data.current_page) { 133 .repo_commits => commits_link, 134 .repo_refs => refs_link, 135 .repo_issues => issues_link, 136 .repo_events => events_link, 137 .repo_settings => settings_link, 138 .repo_auth => auth_link, 139 else => files_link, 140 }; 141 var selected_tab: ?usize = null; 142 143 // files tab 144 { 145 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "files", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 146 errdefer text_box.deinit(allocator); 147 text_box.getFocus().focusable = true; 148 text_box.getFocus().kind = .{ .custom = files_link }; 149 try tab_ids.put(allocator, text_box.getFocus().id, {}); 150 if (std.mem.eql(u8, files_link, current_link)) selected_tab = text_box.getFocus().id; 151 try box.children.put(allocator, text_box.getFocus().id, .{ 152 .widget = .{ .text_box = text_box }, 153 .rect = null, 154 .min_size = .{ .width = "files".len + 2, .height = null }, 155 }); 156 } 157 158 // commits tab 159 { 160 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "commits", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 161 errdefer text_box.deinit(allocator); 162 text_box.getFocus().focusable = true; 163 text_box.getFocus().kind = .{ .custom = commits_link }; 164 try tab_ids.put(allocator, text_box.getFocus().id, {}); 165 if (std.mem.eql(u8, commits_link, current_link)) selected_tab = text_box.getFocus().id; 166 try box.children.put(allocator, text_box.getFocus().id, .{ 167 .widget = .{ .text_box = text_box }, 168 .rect = null, 169 .min_size = .{ .width = "commits".len + 2, .height = null }, 170 }); 171 } 172 173 // refs tab 174 { 175 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "refs", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 176 errdefer text_box.deinit(allocator); 177 text_box.getFocus().focusable = true; 178 text_box.getFocus().kind = .{ .custom = refs_link }; 179 try tab_ids.put(allocator, text_box.getFocus().id, {}); 180 if (std.mem.eql(u8, refs_link, current_link)) selected_tab = text_box.getFocus().id; 181 try box.children.put(allocator, text_box.getFocus().id, .{ 182 .widget = .{ .text_box = text_box }, 183 .rect = null, 184 .min_size = .{ .width = "refs".len + 2, .height = null }, 185 }); 186 } 187 188 // issues tab 189 { 190 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "issues", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 191 errdefer text_box.deinit(allocator); 192 text_box.getFocus().focusable = true; 193 text_box.getFocus().kind = .{ .custom = issues_link }; 194 try tab_ids.put(allocator, text_box.getFocus().id, {}); 195 if (std.mem.eql(u8, issues_link, current_link)) selected_tab = text_box.getFocus().id; 196 try box.children.put(allocator, text_box.getFocus().id, .{ 197 .widget = .{ .text_box = text_box }, 198 .rect = null, 199 .min_size = .{ .width = "issues".len + 2, .height = null }, 200 }); 201 } 202 203 // events 204 { 205 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "events", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 206 errdefer text_box.deinit(allocator); 207 text_box.getFocus().focusable = true; 208 text_box.getFocus().kind = .{ .custom = events_link }; 209 try tab_ids.put(allocator, text_box.getFocus().id, {}); 210 if (std.mem.eql(u8, events_link, current_link)) selected_tab = text_box.getFocus().id; 211 try box.children.put(allocator, text_box.getFocus().id, .{ 212 .widget = .{ .text_box = text_box }, 213 .rect = null, 214 .min_size = .{ .width = "events".len + 2, .height = null }, 215 }); 216 } 217 218 // spacer pushes settings + auth to the right 219 { 220 var spacer = try ui.Spacer.init(allocator); 221 errdefer spacer.deinit(allocator); 222 try box.children.put(allocator, spacer.getFocus().id, .{ 223 .widget = .{ .spacer = spacer }, 224 .rect = null, 225 .min_size = null, 226 }); 227 } 228 229 // settings tab. settings are account preferences, so it needs a login 230 // (which also rules out local mode, which has no accounts). 231 if (session.data.user_id != null) { 232 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "settings", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 233 errdefer text_box.deinit(allocator); 234 text_box.getFocus().focusable = true; 235 text_box.getFocus().kind = .{ .custom = settings_link }; 236 try tab_ids.put(allocator, text_box.getFocus().id, {}); 237 if (std.mem.eql(u8, settings_link, current_link)) selected_tab = text_box.getFocus().id; 238 try box.children.put(allocator, text_box.getFocus().id, .{ 239 .widget = .{ .text_box = text_box }, 240 .rect = null, 241 .min_size = .{ .width = "settings".len + 2, .height = null }, 242 }); 243 } 244 245 // auth tab (login / logout). AuthTab defaults to the global ai:/auth 246 // link; repoint its instance at this repo's auth route so it stays on 247 // this page. local mode has no accounts, so it has no auth tab. 248 if (!session.data.is_local) { 249 var auth_tab = try AuthTab.View.init(allocator, session); 250 errdefer auth_tab.deinit(allocator); 251 auth_tab.text_box.getFocus().kind = .{ .custom = auth_link }; 252 try tab_ids.put(allocator, auth_tab.getFocus().id, {}); 253 if (std.mem.eql(u8, auth_link, current_link)) selected_tab = auth_tab.getFocus().id; 254 try box.children.put(allocator, auth_tab.getFocus().id, .{ 255 .widget = .{ .auth_tab = auth_tab }, 256 .rect = null, 257 .min_size = .{ .width = auth_tab.minWidth(), .height = null }, 258 }); 259 } 260 261 // quit tab 262 if (session.is_terminal) { 263 var text_box = try wgt.TextBox(ui.Widget).init(allocator, ui.Quit.tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 264 errdefer text_box.deinit(allocator); 265 text_box.getFocus().focusable = true; 266 text_box.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 267 try tab_ids.put(allocator, text_box.getFocus().id, {}); 268 try box.children.put(allocator, text_box.getFocus().id, .{ 269 .widget = .{ .text_box = text_box }, 270 .rect = null, 271 // the label is a single column; +2 for the border 272 .min_size = .{ .width = 1 + 2, .height = null }, 273 }); 274 } 275 276 var self = View{ .box = box, .data = data, .tab_ids = tab_ids }; 277 self.getFocus().child_id = selected_tab orelse self.tab_ids.keys()[0]; 278 return self; 279 } 280 281 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 282 self.box.deinit(allocator); 283 self.tab_ids.deinit(allocator); 284 } 285 286 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 287 self.clearGrid(); 288 // only the selected tab shows its border 289 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 290 const tb: ?*wgt.TextBox(ui.Widget) = switch (child.widget) { 291 .text_box => |*x| x, 292 .auth_tab => |*at| blk: { 293 // the label tracks login state per frame, so the width must too 294 child.min_size = .{ .width = at.minWidth(), .height = null }; 295 break :blk &at.text_box; 296 }, 297 else => null, 298 }; 299 if (tb) |t| { 300 t.options.border_style = if (self.getFocus().child_id == id) .single else .hidden; 301 } 302 } 303 try self.box.build(allocator, constraint, root_focus); 304 } 305 306 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 307 _ = allocator; 308 const current_tab = self.currentTabIndex() orelse return; 309 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 310 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 311 } 312 } 313 314 pub fn clearGrid(self: *View) void { 315 self.box.clearGrid(); 316 } 317 318 pub fn getGrid(self: View) ?Grid { 319 return self.box.getGrid(); 320 } 321 322 pub fn getFocus(self: *View) *Focus { 323 return self.box.getFocus(); 324 } 325 326 pub fn getSelectedIndex(self: View) ?usize { 327 return self.currentTabIndex(); 328 } 329 330 fn currentTabIndex(self: View) ?usize { 331 const child_id = self.box.focus.child_id orelse return null; 332 return self.tab_ids.getIndex(child_id); 333 } 334 }; 335