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