╭ 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 settings_link = try std.fmt.allocPrint(aa, "ai:/repo/{s}/{s}/settings", .{ data.owner_name, data.name }); 111 const auth_link = try std.fmt.allocPrint(aa, "ai:/repo/{s}/{s}/auth", .{ data.owner_name, data.name }); 112 113 // the tab matching the current page is focused initially; matching by 114 // link (rather than position) keeps this robust to tab changes. 115 const current_link: []const u8 = switch (session.data.current_page) { 116 .repo_commits => commits_link, 117 .repo_refs => refs_link, 118 .repo_settings => settings_link, 119 .repo_auth => auth_link, 120 else => files_link, 121 }; 122 var selected_tab: ?usize = null; 123 124 // files tab 125 { 126 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "files", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 127 errdefer text_box.deinit(allocator); 128 text_box.getFocus().focusable = true; 129 text_box.getFocus().kind = .{ .custom = files_link }; 130 try tab_ids.put(allocator, text_box.getFocus().id, {}); 131 if (std.mem.eql(u8, files_link, current_link)) selected_tab = text_box.getFocus().id; 132 try box.children.put(allocator, text_box.getFocus().id, .{ 133 .widget = .{ .text_box = text_box }, 134 .rect = null, 135 .min_size = .{ .width = "files".len + 2, .height = null }, 136 }); 137 } 138 139 // commits tab 140 { 141 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "commits", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 142 errdefer text_box.deinit(allocator); 143 text_box.getFocus().focusable = true; 144 text_box.getFocus().kind = .{ .custom = commits_link }; 145 try tab_ids.put(allocator, text_box.getFocus().id, {}); 146 if (std.mem.eql(u8, commits_link, current_link)) selected_tab = text_box.getFocus().id; 147 try box.children.put(allocator, text_box.getFocus().id, .{ 148 .widget = .{ .text_box = text_box }, 149 .rect = null, 150 .min_size = .{ .width = "commits".len + 2, .height = null }, 151 }); 152 } 153 154 // refs tab 155 { 156 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "refs", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 157 errdefer text_box.deinit(allocator); 158 text_box.getFocus().focusable = true; 159 text_box.getFocus().kind = .{ .custom = refs_link }; 160 try tab_ids.put(allocator, text_box.getFocus().id, {}); 161 if (std.mem.eql(u8, refs_link, current_link)) selected_tab = text_box.getFocus().id; 162 try box.children.put(allocator, text_box.getFocus().id, .{ 163 .widget = .{ .text_box = text_box }, 164 .rect = null, 165 .min_size = .{ .width = "refs".len + 2, .height = null }, 166 }); 167 } 168 169 // spacer pushes settings + auth to the right 170 { 171 var spacer = try ui.Spacer.init(allocator); 172 errdefer spacer.deinit(allocator); 173 try box.children.put(allocator, spacer.getFocus().id, .{ 174 .widget = .{ .spacer = spacer }, 175 .rect = null, 176 .min_size = null, 177 }); 178 } 179 180 // settings tab 181 { 182 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "settings", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 183 errdefer text_box.deinit(allocator); 184 text_box.getFocus().focusable = true; 185 text_box.getFocus().kind = .{ .custom = settings_link }; 186 try tab_ids.put(allocator, text_box.getFocus().id, {}); 187 if (std.mem.eql(u8, settings_link, current_link)) selected_tab = text_box.getFocus().id; 188 try box.children.put(allocator, text_box.getFocus().id, .{ 189 .widget = .{ .text_box = text_box }, 190 .rect = null, 191 .min_size = .{ .width = "settings".len + 2, .height = null }, 192 }); 193 } 194 195 // auth tab (login / logout). AuthTab defaults to the global ai:/auth 196 // link; repoint its instance at this repo's auth route so it stays local. 197 { 198 var auth_tab = try AuthTab.View.init(allocator, session); 199 errdefer auth_tab.deinit(allocator); 200 auth_tab.text_box.getFocus().kind = .{ .custom = auth_link }; 201 try tab_ids.put(allocator, auth_tab.getFocus().id, {}); 202 if (std.mem.eql(u8, auth_link, current_link)) selected_tab = auth_tab.getFocus().id; 203 try box.children.put(allocator, auth_tab.getFocus().id, .{ 204 .widget = .{ .auth_tab = auth_tab }, 205 .rect = null, 206 .min_size = .{ .width = AuthTab.min_width, .height = null }, 207 }); 208 } 209 210 // quit tab 211 if (session.is_terminal) { 212 var text_box = try wgt.TextBox(ui.Widget).init(allocator, ui.Quit.tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 213 errdefer text_box.deinit(allocator); 214 text_box.getFocus().focusable = true; 215 text_box.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 216 try tab_ids.put(allocator, text_box.getFocus().id, {}); 217 try box.children.put(allocator, text_box.getFocus().id, .{ 218 .widget = .{ .text_box = text_box }, 219 .rect = null, 220 // the label is a single column; +2 for the border 221 .min_size = .{ .width = 1 + 2, .height = null }, 222 }); 223 } 224 225 var self = View{ .box = box, .data = data, .tab_ids = tab_ids }; 226 self.getFocus().child_id = selected_tab orelse self.tab_ids.keys()[0]; 227 return self; 228 } 229 230 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 231 self.box.deinit(allocator); 232 self.tab_ids.deinit(allocator); 233 } 234 235 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 236 self.clearGrid(); 237 // only the selected tab shows its border 238 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 239 const tb: ?*wgt.TextBox(ui.Widget) = switch (child.widget) { 240 .text_box => |*x| x, 241 .auth_tab => |*at| &at.text_box, 242 else => null, 243 }; 244 if (tb) |t| { 245 t.options.border_style = if (self.getFocus().child_id == id) .single else .hidden; 246 } 247 } 248 try self.box.build(allocator, constraint, root_focus); 249 } 250 251 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 252 _ = allocator; 253 const current_tab = self.currentTabIndex() orelse return; 254 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 255 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 256 } 257 } 258 259 pub fn clearGrid(self: *View) void { 260 self.box.clearGrid(); 261 } 262 263 pub fn getGrid(self: View) ?Grid { 264 return self.box.getGrid(); 265 } 266 267 pub fn getFocus(self: *View) *Focus { 268 return self.box.getFocus(); 269 } 270 271 pub fn getSelectedIndex(self: View) ?usize { 272 return self.currentTabIndex(); 273 } 274 275 fn currentTabIndex(self: View) ?usize { 276 const child_id = self.box.focus.child_id orelse return null; 277 return self.tab_ids.getIndex(child_id); 278 } 279 }; 280