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