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