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