╭ 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 // title sits to the left of the tabs 74 { 75 var title_view = try ui.Title.View.init(allocator, &data.title); 76 errdefer title_view.deinit(allocator); 77 title_view.getFocus().focusable = true; 78 title_view.getFocus().kind = .{ .custom = "a:/" }; 79 // shrink the title when there is not enough space 80 try box.children.put(allocator, title_view.getFocus().id, .{ 81 .widget = .{ .title = title_view }, 82 .rect = null, 83 .min_size = null, 84 .shrink = true, 85 }); 86 } 87 88 // spacer 89 { 90 var text = try wgt.Text(ui.Widget).init(allocator, " "); 91 errdefer text.deinit(allocator); 92 try box.children.put(allocator, text.getFocus().id, .{ 93 .widget = .{ .text = text }, 94 .rect = null, 95 .min_size = .{ .width = 1, .height = null }, 96 }); 97 } 98 99 // every tab link is repo-scoped so selecting one stays on this page — 100 // switching its stack and updating the url — instead of navigating to 101 // the global settings or auth pages. the "ai:" prefix makes each an 102 // in-page anchor: crossPageLink ignores it so a wasm click just switches 103 // tabs (the page already holds every tab's content), while the href is 104 // still followed with js off. the files tab routes through the shared 105 // helper so the /repo/.../files url format lives in one place. 106 const identity = try std.fmt.allocPrint(aa, "{s}/{s}", .{ data.owner_name, data.name }); 107 // both tabs link to the ref/oid this page is viewing, so switching tabs 108 // keeps the same ref (the files tab opens at its root directory). 109 const files_route = ui.RoutablePage.repoFilesRoute(identity, data.ref_or_oid, data.ref_or_oid_value, "", 0) orelse return error.RouteTooLong; 110 const files_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try files_route.urlAlloc(session.page_arena)}); 111 const commits_route = ui.RoutablePage.repoCommitsRoute(identity, data.ref_or_oid, data.ref_or_oid_value, 0) orelse return error.RouteTooLong; 112 const commits_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try commits_route.urlAlloc(session.page_arena)}); 113 const refs_link = try std.fmt.allocPrint(aa, "ai:/repo/{s}/{s}/refs", .{ data.owner_name, data.name }); 114 const issues_link = if (data.issues_tag.len == 0) 115 try std.fmt.allocPrint(aa, "ai:/repo/{s}/{s}/issues", .{ data.owner_name, data.name }) 116 else 117 try std.fmt.allocPrint(aa, "ai:/repo/{s}/{s}/issues/tag/{s}", .{ data.owner_name, data.name, data.issues_tag }); 118 const settings_link = try std.fmt.allocPrint(aa, "ai:/repo/{s}/{s}/settings", .{ data.owner_name, data.name }); 119 const auth_link = try std.fmt.allocPrint(aa, "ai:/repo/{s}/{s}/auth", .{ data.owner_name, data.name }); 120 121 // the tab matching the current page is focused initially; matching by 122 // link (rather than position) keeps this robust to tab changes. 123 const current_link: []const u8 = switch (session.data.current_page) { 124 .repo_commits => commits_link, 125 .repo_refs => refs_link, 126 .repo_issues => issues_link, 127 .repo_settings => settings_link, 128 .repo_auth => auth_link, 129 else => files_link, 130 }; 131 var selected_tab: ?usize = null; 132 133 // files tab 134 { 135 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "files", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 136 errdefer text_box.deinit(allocator); 137 text_box.getFocus().focusable = true; 138 text_box.getFocus().kind = .{ .custom = files_link }; 139 try tab_ids.put(allocator, text_box.getFocus().id, {}); 140 if (std.mem.eql(u8, files_link, current_link)) selected_tab = text_box.getFocus().id; 141 try box.children.put(allocator, text_box.getFocus().id, .{ 142 .widget = .{ .text_box = text_box }, 143 .rect = null, 144 .min_size = .{ .width = "files".len + 2, .height = null }, 145 }); 146 } 147 148 // commits tab 149 { 150 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "commits", .{ .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 = commits_link }; 154 try tab_ids.put(allocator, text_box.getFocus().id, {}); 155 if (std.mem.eql(u8, commits_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 = "commits".len + 2, .height = null }, 160 }); 161 } 162 163 // refs tab 164 { 165 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "refs", .{ .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 = refs_link }; 169 try tab_ids.put(allocator, text_box.getFocus().id, {}); 170 if (std.mem.eql(u8, refs_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 = "refs".len + 2, .height = null }, 175 }); 176 } 177 178 // issues tab 179 { 180 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "issues", .{ .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 = issues_link }; 184 try tab_ids.put(allocator, text_box.getFocus().id, {}); 185 if (std.mem.eql(u8, issues_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 = "issues".len + 2, .height = null }, 190 }); 191 } 192 193 // spacer pushes settings + auth to the right 194 { 195 var spacer = try ui.Spacer.init(allocator); 196 errdefer spacer.deinit(allocator); 197 try box.children.put(allocator, spacer.getFocus().id, .{ 198 .widget = .{ .spacer = spacer }, 199 .rect = null, 200 .min_size = null, 201 }); 202 } 203 204 // settings tab 205 { 206 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "settings", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 207 errdefer text_box.deinit(allocator); 208 text_box.getFocus().focusable = true; 209 text_box.getFocus().kind = .{ .custom = settings_link }; 210 try tab_ids.put(allocator, text_box.getFocus().id, {}); 211 if (std.mem.eql(u8, settings_link, current_link)) selected_tab = text_box.getFocus().id; 212 try box.children.put(allocator, text_box.getFocus().id, .{ 213 .widget = .{ .text_box = text_box }, 214 .rect = null, 215 .min_size = .{ .width = "settings".len + 2, .height = null }, 216 }); 217 } 218 219 // auth tab (login / logout). AuthTab defaults to the global ai:/auth 220 // link; repoint its instance at this repo's auth route so it stays local. 221 { 222 var auth_tab = try AuthTab.View.init(allocator, session); 223 errdefer auth_tab.deinit(allocator); 224 auth_tab.text_box.getFocus().kind = .{ .custom = auth_link }; 225 try tab_ids.put(allocator, auth_tab.getFocus().id, {}); 226 if (std.mem.eql(u8, auth_link, current_link)) selected_tab = auth_tab.getFocus().id; 227 try box.children.put(allocator, auth_tab.getFocus().id, .{ 228 .widget = .{ .auth_tab = auth_tab }, 229 .rect = null, 230 .min_size = .{ .width = AuthTab.min_width, .height = null }, 231 }); 232 } 233 234 // quit tab 235 if (session.is_terminal) { 236 var text_box = try wgt.TextBox(ui.Widget).init(allocator, ui.Quit.tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 237 errdefer text_box.deinit(allocator); 238 text_box.getFocus().focusable = true; 239 text_box.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 240 try tab_ids.put(allocator, text_box.getFocus().id, {}); 241 try box.children.put(allocator, text_box.getFocus().id, .{ 242 .widget = .{ .text_box = text_box }, 243 .rect = null, 244 // the label is a single column; +2 for the border 245 .min_size = .{ .width = 1 + 2, .height = null }, 246 }); 247 } 248 249 var self = View{ .box = box, .data = data, .tab_ids = tab_ids }; 250 self.getFocus().child_id = selected_tab orelse self.tab_ids.keys()[0]; 251 return self; 252 } 253 254 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 255 self.box.deinit(allocator); 256 self.tab_ids.deinit(allocator); 257 } 258 259 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 260 self.clearGrid(); 261 // only the selected tab shows its border 262 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 263 const tb: ?*wgt.TextBox(ui.Widget) = switch (child.widget) { 264 .text_box => |*x| x, 265 .auth_tab => |*at| &at.text_box, 266 else => null, 267 }; 268 if (tb) |t| { 269 t.options.border_style = if (self.getFocus().child_id == id) .single else .hidden; 270 } 271 } 272 try self.box.build(allocator, constraint, root_focus); 273 } 274 275 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 276 _ = allocator; 277 const current_tab = self.currentTabIndex() orelse return; 278 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 279 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 280 } 281 } 282 283 pub fn clearGrid(self: *View) void { 284 self.box.clearGrid(); 285 } 286 287 pub fn getGrid(self: View) ?Grid { 288 return self.box.getGrid(); 289 } 290 291 pub fn getFocus(self: *View) *Focus { 292 return self.box.getFocus(); 293 } 294 295 pub fn getSelectedIndex(self: View) ?usize { 296 return self.currentTabIndex(); 297 } 298 299 fn currentTabIndex(self: View) ?usize { 300 const child_id = self.box.focus.child_id orelse return null; 301 return self.tab_ids.getIndex(child_id); 302 } 303 }; 304