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