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