╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("../../event.zig"); 3 const ui = @import("../../ui.zig"); 4 const inp = @import("../input.zig"); 5 const xit = @import("xit"); 6 const xitui = xit.xitui; 7 const wgt = xitui.widget; 8 const layout = xitui.layout; 9 const Key = xitui.input.Key; 10 const Grid = xitui.grid.Grid; 11 const Focus = xitui.focus.Focus; 12 13 const diff_tab_label = "diff"; 14 const commits_tab_label = "commits"; 15 const files_tab_label = "files"; 16 const refs_tab_label = "refs"; 17 const discuss_tab_label = "discuss"; 18 const issues_tab_label = "issues"; 19 const patches_tab_label = "patches"; 20 const events_tab_label = "events"; 21 const new_tab_label = "new"; 22 const new_repo_tab_bottom_label = "repo"; 23 const new_user_tab_bottom_label = "user"; 24 25 const RefOrOid = ui.RoutablePage.RefOrOid; 26 const Array = ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len); 27 28 name: []const u8, 29 owner_name: []const u8, 30 title: ui.Title, 31 // the ref/oid this page is viewing; the value is url-encoded 32 ref_or_oid: RefOrOid, 33 ref_or_oid_value: []const u8, 34 // each thread tab's label filter, url-encoded ("" = unfiltered), so the tab 35 // links back to the filtered list and shows the label beneath it. 36 discussions_label: []const u8, 37 issues_label: []const u8, 38 patches_label: []const u8, 39 40 const Self = @This(); 41 42 pub fn init(arena: *std.heap.ArenaAllocator, name: []const u8, owner_name: []const u8, ref_or_oid: RefOrOid, ref_or_oid_value: []const u8, discussions_label: []const u8, issues_label: []const u8, patches_label: []const u8) !Self { 43 return .{ 44 .name = name, 45 .owner_name = owner_name, 46 .title = try ui.Title.init(arena, name, .scanlines), 47 .ref_or_oid = ref_or_oid, 48 .ref_or_oid_value = ref_or_oid_value, 49 .discussions_label = discussions_label, 50 .issues_label = issues_label, 51 .patches_label = patches_label, 52 }; 53 } 54 55 pub const View = struct { 56 scroll: wgt.Scroll(ui.Widget), 57 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 58 tabs_id: usize, 59 gap_id: usize, 60 first_group_width: usize, 61 session: *ui.Session, 62 63 pub fn init(allocator: std.mem.Allocator, page: *const ui.Repo, session: *ui.Session) !View { 64 const data = &page.header; 65 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = .hidden, .round_corners = true, .direction = .horiz }); 66 errdefer box.deinit(allocator); 67 68 var title_box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 69 var title_box_owned = false; 70 errdefer if (!title_box_owned) title_box.deinit(allocator); 71 72 var tabs_box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 73 var tabs_box_owned = false; 74 errdefer if (!tabs_box_owned) tabs_box.deinit(allocator); 75 76 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 77 errdefer tab_ids.deinit(allocator); 78 79 const aa = session.page_arena.allocator(); 80 const ref_name = std.Uri.percentDecodeInPlace(try aa.dupe(u8, page.files.patchrev_id orelse data.ref_or_oid_value)); 81 const bottom_label = try ui.clippedBottomLabel(try aa.alloc(u8, ui.clipped_bottom_label_max_len), ref_name); 82 const discussions_bottom_label = try ui.clippedBottomLabel(try aa.alloc(u8, ui.clipped_bottom_label_max_len), std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.discussions_label))); 83 const issues_bottom_label = try ui.clippedBottomLabel(try aa.alloc(u8, ui.clipped_bottom_label_max_len), std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.issues_label))); 84 const patches_bottom_label = try ui.clippedBottomLabel(try aa.alloc(u8, ui.clipped_bottom_label_max_len), std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.patches_label))); 85 const changes_label = switch (page.changes) { 86 .diff => diff_tab_label, 87 .commits => |c| if (c.commit_count) |count| try aa.print(commits_tab_label ++ " ({d})", .{count}) else commits_tab_label, 88 }; 89 const changes_label_width = try xitui.width.displayWidth(changes_label); 90 var first_group_width = try data.title.width(); 91 92 try ui.widget.addBackButton(allocator, &title_box, session); 93 94 // the user's name (local mode has no user pages to link to) 95 if (session.data.host_kind == .server) { 96 var text_buf: [evt.User.name_max_len + 1]u8 = undefined; 97 const text = try std.fmt.bufPrint(&text_buf, "{s}:", .{data.owner_name}); 98 const link = try aa.print("a:/{s}", .{data.owner_name}); 99 100 const private_label = if (page.repo.event.read_access == .private) "(private)" else ""; 101 var text_box = try wgt.TextBox.init(allocator, text, .{ .border = .hidden, .wrap_kind = .none, .bottom_label = .{ .text = private_label } }); 102 errdefer text_box.deinit(allocator); 103 text_box.getFocus().mode = .all; 104 text_box.getFocus().kind = .{ .custom = link }; 105 first_group_width += @max(try xitui.width.displayWidth(text), private_label.len) + 2; 106 try title_box.children.put(allocator, text_box.getFocus().id, .{ 107 .widget = .{ .text_box = text_box }, 108 .rect = null, 109 .min_size = null, 110 }); 111 } 112 113 // local routes carry no identity, so their urls come out elided 114 const identity = if (session.data.host_kind == .local) "" else try aa.print("{s}:{s}", .{ data.owner_name, data.name }); 115 116 // title links to the repo's files root (the bare route, so it resolves 117 // to the default branch). 118 { 119 const files_root_route = ui.RoutablePage.repoFilesRoute(identity, null, "", "", 0) orelse return error.RouteTooLong; 120 const title_link = try aa.print("a:{s}", .{try files_root_route.toUrl(session.page_arena)}); 121 var title_view = try ui.Title.View.init(allocator, &data.title); 122 errdefer title_view.deinit(allocator); 123 title_view.getFocus().mode = .all; 124 title_view.getFocus().kind = .{ .custom = title_link }; 125 try title_box.children.put(allocator, title_view.getFocus().id, .{ 126 .widget = .{ .title = title_view }, 127 .rect = null, 128 .min_size = null, 129 }); 130 } 131 132 // spacer 133 { 134 var text = try wgt.Text.init(allocator, " "); 135 errdefer text.deinit(allocator); 136 try tabs_box.children.put(allocator, text.getFocus().id, .{ 137 .widget = .{ .text = text }, 138 .rect = null, 139 .min_size = .{ .width = 1, .height = null }, 140 }); 141 } 142 143 // every tab link is repo-scoped so selecting one stays on this page — 144 // switching its stack and updating the url — instead of navigating to 145 // the global new repo or user pages. the "ai:" prefix makes each an 146 // in-page anchor: crossPageLink ignores it so a wasm click just switches 147 // tabs (the page already holds every tab's content), while the href is 148 // still followed with js off. the files tab routes through the shared 149 // helper so the /.../files url format lives in one place. 150 // files and changes keep the same ref or revision when switching tabs. 151 // the files tab opens at its root directory. 152 const current_page = session.data.current_page; 153 const current_tag = std.meta.activeTag(current_page); 154 const files_route = page.files.filesRoute("", 0) orelse return error.RouteTooLong; 155 const files_link = try ui.inPageTabLink(session, files_route, current_tag == .repo_files); 156 const changes_route = (switch (page.changes) { 157 .diff => |d| d.route.page(d.window.start, d.path), 158 .commits => |c| ui.RoutablePage.repoCommitsRoute(identity, data.ref_or_oid, data.ref_or_oid_value, c.base_oid), 159 }) orelse return error.RouteTooLong; 160 const changes_tag = std.meta.activeTag(changes_route); 161 const changes_link = try ui.inPageTabLink(session, changes_route, current_tag == changes_tag); 162 const refs_route = ui.RoutablePage.repoRefsRoute(identity, .branch, "") orelse return error.RouteTooLong; 163 const refs_link = try ui.inPageTabLink(session, refs_route, current_tag == .repo_refs); 164 const discussions_route = ui.RoutablePage.repoDiscussionsRoute(identity, data.discussions_label, "") orelse return error.RouteTooLong; 165 const discussions_link = try ui.inPageTabLink(session, discussions_route, current_tag == .repo_discussions); 166 const issues_route = ui.RoutablePage.repoIssuesRoute(identity, .open, data.issues_label, "") orelse return error.RouteTooLong; 167 const issues_link = try ui.inPageTabLink(session, issues_route, current_tag == .repo_issues); 168 const patches_route = ui.RoutablePage.repoPatchesRoute(identity, .open, data.patches_label, "") orelse return error.RouteTooLong; 169 const patches_link = try ui.inPageTabLink(session, patches_route, current_tag == .repo_patches); 170 const events_route = ui.RoutablePage.repoEventsRoute(identity, .active, null, "", null) orelse return error.RouteTooLong; 171 const events_link = try ui.inPageTabLink(session, events_route, current_tag == .repo_events); 172 const new_repo_route = ui.RoutablePage{ .repo_repo_new = Array.from(identity) orelse return error.RouteTooLong }; 173 const new_repo_link = try ui.inPageTabLink(session, new_repo_route, current_tag == .repo_repo_new); 174 const new_user_route = ui.RoutablePage{ .repo_user_new = Array.from(identity) orelse return error.RouteTooLong }; 175 const new_user_link = try ui.inPageTabLink(session, new_user_route, current_tag == .repo_user_new); 176 const user_route = ui.RoutablePage{ .repo_user = .{ .name = Array.from(identity) orelse return error.RouteTooLong } }; 177 const user_link = try ui.inPageTabLink(session, user_route, current_tag == .repo_user); 178 179 // the tab matching the current page is focused initially; matching by 180 // link (rather than position) keeps this robust to tab changes. 181 var selected_tab: ?usize = null; 182 183 // files tab 184 { 185 var text_box = try wgt.TextBox.init(allocator, files_tab_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = .{ .text = bottom_label } }); 186 errdefer text_box.deinit(allocator); 187 text_box.getFocus().mode = .all; 188 text_box.getFocus().kind = .{ .custom = files_link }; 189 try tab_ids.put(allocator, text_box.getFocus().id, {}); 190 if (current_tag == .repo_files) selected_tab = text_box.getFocus().id; 191 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 192 .widget = .{ .text_box = text_box }, 193 .rect = null, 194 .min_size = .{ .width = files_tab_label.len + 2, .height = null }, 195 }); 196 } 197 198 // commits, or the comparison selected by a diff / patchrev url 199 { 200 var text_box = try wgt.TextBox.init(allocator, changes_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = .{ .text = bottom_label } }); 201 errdefer text_box.deinit(allocator); 202 text_box.getFocus().mode = .all; 203 text_box.getFocus().kind = .{ .custom = changes_link }; 204 try tab_ids.put(allocator, text_box.getFocus().id, {}); 205 if (current_tag == changes_tag) selected_tab = text_box.getFocus().id; 206 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 207 .widget = .{ .text_box = text_box }, 208 .rect = null, 209 .min_size = .{ .width = changes_label_width + 2, .height = null }, 210 }); 211 } 212 213 // refs tab 214 { 215 var text_box = try wgt.TextBox.init(allocator, refs_tab_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 216 errdefer text_box.deinit(allocator); 217 text_box.getFocus().mode = .all; 218 text_box.getFocus().kind = .{ .custom = refs_link }; 219 try tab_ids.put(allocator, text_box.getFocus().id, {}); 220 if (current_tag == .repo_refs) selected_tab = text_box.getFocus().id; 221 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 222 .widget = .{ .text_box = text_box }, 223 .rect = null, 224 .min_size = .{ .width = refs_tab_label.len + 2, .height = null }, 225 }); 226 } 227 228 // discuss tab, unless it is off 229 if (page.repo.event.discuss_role != null) { 230 var text_box = try wgt.TextBox.init(allocator, discuss_tab_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = .{ .text = discussions_bottom_label } }); 231 errdefer text_box.deinit(allocator); 232 text_box.getFocus().mode = .all; 233 text_box.getFocus().kind = .{ .custom = discussions_link }; 234 try tab_ids.put(allocator, text_box.getFocus().id, {}); 235 if (current_tag == .repo_discussions) selected_tab = text_box.getFocus().id; 236 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 237 .widget = .{ .text_box = text_box }, 238 .rect = null, 239 .min_size = .{ .width = discuss_tab_label.len + 2, .height = null }, 240 }); 241 } 242 243 // issues tab, unless it is off 244 if (page.repo.event.issue_role != null) { 245 var text_box = try wgt.TextBox.init(allocator, issues_tab_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = .{ .text = issues_bottom_label } }); 246 errdefer text_box.deinit(allocator); 247 text_box.getFocus().mode = .all; 248 text_box.getFocus().kind = .{ .custom = issues_link }; 249 try tab_ids.put(allocator, text_box.getFocus().id, {}); 250 if (current_tag == .repo_issues) selected_tab = text_box.getFocus().id; 251 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 252 .widget = .{ .text_box = text_box }, 253 .rect = null, 254 .min_size = .{ .width = issues_tab_label.len + 2, .height = null }, 255 }); 256 } 257 258 // patches tab, unless it is off 259 if (page.repo.event.patch_role != null) { 260 var text_box = try wgt.TextBox.init(allocator, patches_tab_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = .{ .text = patches_bottom_label } }); 261 errdefer text_box.deinit(allocator); 262 text_box.getFocus().mode = .all; 263 text_box.getFocus().kind = .{ .custom = patches_link }; 264 try tab_ids.put(allocator, text_box.getFocus().id, {}); 265 if (current_tag == .repo_patches) selected_tab = text_box.getFocus().id; 266 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 267 .widget = .{ .text_box = text_box }, 268 .rect = null, 269 .min_size = .{ .width = patches_tab_label.len + 2, .height = null }, 270 }); 271 } 272 273 // events, which a server only shows to whoever it shows undo to 274 if (page.handle.canWrite()) { 275 var text_box = try wgt.TextBox.init(allocator, events_tab_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 276 errdefer text_box.deinit(allocator); 277 text_box.getFocus().mode = .all; 278 text_box.getFocus().kind = .{ .custom = events_link }; 279 try tab_ids.put(allocator, text_box.getFocus().id, {}); 280 if (current_tag == .repo_events) selected_tab = text_box.getFocus().id; 281 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 282 .widget = .{ .text_box = text_box }, 283 .rect = null, 284 .min_size = .{ .width = events_tab_label.len + 2, .height = null }, 285 }); 286 } 287 288 if (page.undo != null) { 289 const route = ui.RoutablePage.repoUndoRoute(identity, null) orelse return error.RouteTooLong; 290 var text_box = try wgt.TextBox.init(allocator, "undo", .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 291 errdefer text_box.deinit(allocator); 292 text_box.getFocus().mode = .all; 293 text_box.getFocus().kind = .{ .custom = try ui.inPageTabLink(session, route, current_tag == .repo_undo) }; 294 try tab_ids.put(allocator, text_box.getFocus().id, {}); 295 if (current_tag == .repo_undo) selected_tab = text_box.getFocus().id; 296 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 297 .widget = .{ .text_box = text_box }, 298 .rect = null, 299 .min_size = .{ .width = 6, .height = null }, 300 }); 301 } 302 303 if (page.settings != null) { 304 const route = ui.RoutablePage.repoSettingsRoute(identity) orelse return error.RouteTooLong; 305 var text_box = try wgt.TextBox.init(allocator, ui.Repo.Settings.tab_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 306 errdefer text_box.deinit(allocator); 307 text_box.getFocus().mode = .all; 308 const selected = current_tag == .repo_settings or current_tag == .repo_roles; 309 text_box.getFocus().kind = .{ .custom = try ui.inPageTabLink(session, route, selected) }; 310 try tab_ids.put(allocator, text_box.getFocus().id, {}); 311 if (selected) selected_tab = text_box.getFocus().id; 312 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 313 .widget = .{ .text_box = text_box }, 314 .rect = null, 315 .min_size = .{ .width = 3, .height = null }, 316 }); 317 } 318 319 // spacer pushes new repo, new user, and user to the right 320 { 321 var spacer = try ui.widget.Spacer.init(allocator); 322 errdefer spacer.deinit(allocator); 323 try tabs_box.children.put(allocator, spacer.getFocus().id, .{ 324 .widget = .{ .spacer = spacer }, 325 .rect = null, 326 .min_size = null, 327 .flex = .grow, 328 }); 329 } 330 331 // new repo tab with a login, new user tab without one outside local mode 332 if (session.data.user_id != null) { 333 var text_box = try wgt.TextBox.init(allocator, new_tab_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = .{ .text = new_repo_tab_bottom_label } }); 334 errdefer text_box.deinit(allocator); 335 text_box.getFocus().mode = .all; 336 text_box.getFocus().kind = .{ .custom = new_repo_link }; 337 try tab_ids.put(allocator, text_box.getFocus().id, {}); 338 if (current_tag == .repo_repo_new) selected_tab = text_box.getFocus().id; 339 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 340 .widget = .{ .text_box = text_box }, 341 .rect = null, 342 .min_size = .{ .width = new_tab_label.len + 2, .height = null }, 343 }); 344 } else if (session.data.host_kind == .server) { 345 var text_box = try wgt.TextBox.init(allocator, new_tab_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = .{ .text = new_user_tab_bottom_label } }); 346 errdefer text_box.deinit(allocator); 347 text_box.getFocus().mode = .all; 348 text_box.getFocus().kind = .{ .custom = new_user_link }; 349 try tab_ids.put(allocator, text_box.getFocus().id, {}); 350 if (current_tag == .repo_user_new) selected_tab = text_box.getFocus().id; 351 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 352 .widget = .{ .text_box = text_box }, 353 .rect = null, 354 .min_size = .{ .width = new_tab_label.len + 2, .height = null }, 355 }); 356 } 357 358 // user tab. local mode has no users, so it has none. 359 if (session.data.host_kind == .server) { 360 const label = ui.UserSettings.tabLabel(session); 361 const count_label = try ui.UserSettings.tabBottomLabel(session); 362 var text_box = try wgt.TextBox.init(allocator, label, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = .{ .text = count_label } }); 363 errdefer text_box.deinit(allocator); 364 text_box.getFocus().mode = .all; 365 text_box.getFocus().kind = .{ .custom = user_link }; 366 try tab_ids.put(allocator, text_box.getFocus().id, {}); 367 if (current_tag == .repo_user) selected_tab = text_box.getFocus().id; 368 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 369 .widget = .{ .text_box = text_box }, 370 .rect = null, 371 .min_size = .{ .width = @max(label.len, count_label.len) + 2, .height = null }, 372 }); 373 } 374 375 // quit tab 376 if (session.is_terminal) { 377 var text_box = try wgt.TextBox.init(allocator, ui.Quit.tab_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 378 errdefer text_box.deinit(allocator); 379 text_box.getFocus().mode = .all; 380 text_box.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 381 try tab_ids.put(allocator, text_box.getFocus().id, {}); 382 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 383 .widget = .{ .text_box = text_box }, 384 .rect = null, 385 // the label is a single column; +2 for the border 386 .min_size = .{ .width = 1 + 2, .height = null }, 387 }); 388 } 389 390 tabs_box.getFocus().child_id = selected_tab orelse tab_ids.keys()[0]; 391 const title_id = title_box.getFocus().id; 392 const tabs_id = tabs_box.getFocus().id; 393 try box.children.put(allocator, title_id, .{ 394 .widget = .{ .box = title_box }, 395 .rect = null, 396 .min_size = null, 397 }); 398 title_box_owned = true; 399 // a blank row between the title and the tabs, shown only when they wrap 400 const gap_id = blk: { 401 var gap = try wgt.Text.init(allocator, " "); 402 errdefer gap.deinit(allocator); 403 const id = gap.getFocus().id; 404 try box.children.put(allocator, id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null, .hidden = true }); 405 break :blk id; 406 }; 407 try box.children.put(allocator, tabs_id, .{ 408 .widget = .{ .box = tabs_box }, 409 .rect = null, 410 .min_size = null, 411 }); 412 tabs_box_owned = true; 413 box.getFocus().child_id = tabs_id; 414 415 return .{ 416 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = box }, .{ .direction = .horiz, .show_bar = false, .web_native = !session.is_terminal }), 417 .tab_ids = tab_ids, 418 .tabs_id = tabs_id, 419 .gap_id = gap_id, 420 .first_group_width = first_group_width, 421 .session = session, 422 }; 423 } 424 425 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 426 self.scroll.deinit(allocator); 427 self.tab_ids.deinit(allocator); 428 } 429 430 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 431 self.clearGrid(); 432 const box = &self.scroll.child.box; 433 const back_visible = self.session.back == .available; 434 ui.widget.setBackButtonVisible(box, back_visible); 435 const tabs_child = self.tabsChild(); 436 const tabs_box = &tabs_child.widget.box; 437 438 // only the selected tab shows its border 439 const selected_tab = tabs_box.getFocus().child_id; 440 var tabs_width: usize = 0; 441 for (tabs_box.children.keys(), tabs_box.children.values()) |id, *child| { 442 const tb: ?*wgt.TextBox = switch (child.widget) { 443 .text_box => |*x| x, 444 else => null, 445 }; 446 if (tb) |t| ui.widget.markSelected(t, selected_tab == id); 447 tabs_width += if (child.min_size) |min_size| min_size.width orelse 0 else 0; 448 } 449 450 const viewport_width = constraint.max_size.width orelse constraint.min_size.width; 451 // the outer box's hidden border occupies two columns. keep the title 452 // and tabs together if they fit; otherwise tab strip on its own row. 453 const content_width = if (viewport_width) |width| width -| 2 else null; 454 const back_width: usize = if (back_visible) ui.widget.back_button_width else 0; 455 const wrap = if (content_width) |width| self.first_group_width + back_width + tabs_width > width else false; 456 box.options.direction = if (wrap) .vert else .horiz; 457 tabs_child.min_size = if (wrap) .{ .width = content_width, .height = null } else null; 458 (box.children.getPtr(self.gap_id) orelse unreachable).hidden = !wrap; 459 460 var scroll_constraint = constraint; 461 scroll_constraint.min_size.width = viewport_width; 462 try self.scroll.build(allocator, scroll_constraint, root_focus); 463 } 464 465 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 466 _ = allocator; 467 const current_tab = self.currentTabIndex() orelse return; 468 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 469 const tab_id = self.tab_ids.keys()[new_tab]; 470 root_focus.setFocus(tab_id); 471 const tabs_child = self.tabsChild(); 472 const tabs_rect = tabs_child.rect orelse return; 473 const tab = tabs_child.widget.box.children.get(tab_id) orelse return; 474 var rect = tab.rect orelse return; 475 rect.x += tabs_rect.x; 476 rect.y += tabs_rect.y; 477 self.scroll.scrollToRect(rect); 478 } 479 } 480 481 pub fn clearGrid(self: *View) void { 482 self.scroll.clearGrid(); 483 } 484 485 pub fn getGrid(self: View) ?Grid { 486 return self.scroll.getGrid(); 487 } 488 489 pub fn getFocus(self: *View) *Focus { 490 return self.scroll.getFocus(); 491 } 492 493 pub fn getSelectedIndex(self: View) ?usize { 494 return self.currentTabIndex(); 495 } 496 497 fn currentTabIndex(self: View) ?usize { 498 if (self.scroll.child.box.focus.child_id != self.tabs_id) return null; 499 const child_id = self.tabsChild().widget.box.focus.child_id orelse return null; 500 return self.tab_ids.getIndex(child_id); 501 } 502 503 fn tabsChild(self: *const View) *wgt.Box(ui.Widget).Child { 504 return self.scroll.child.box.children.getPtr(self.tabs_id) orelse unreachable; 505 } 506 }; 507