╭ 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 auth_tab: AuthTab, 22 // the ref/oid this page is viewing, so the files and commits tabs both link to 23 // it (switching tabs keeps the same ref). the value is url-encoded. 24 ref_or_oid: RefOrOid, 25 ref_or_oid_value: []const u8, 26 // the issues tab's tag filter, url-encoded ("" = unfiltered), so the tab links 27 // back to the filtered list. 28 issues_tag: []const u8, 29 patches_tag: []const u8, 30 discussions_tag: []const u8, 31 32 const Self = @This(); 33 34 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 { 35 return .{ 36 .name = name, 37 .owner_name = owner_name, 38 .title = try ui.Title.init(arena, name), 39 .auth_tab = AuthTab.init(), 40 .ref_or_oid = ref_or_oid, 41 .ref_or_oid_value = ref_or_oid_value, 42 .issues_tag = issues_tag, 43 .patches_tag = patches_tag, 44 .discussions_tag = discussions_tag, 45 }; 46 } 47 48 pub const View = struct { 49 scroll: wgt.Scroll(ui.Widget), 50 data: *const Self, 51 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 52 tabs_id: usize, 53 first_group_width: usize, 54 55 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 56 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .hidden, .rounded_corners = true, .direction = .horiz }); 57 errdefer box.deinit(allocator); 58 59 var title_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 60 var title_box_owned = false; 61 errdefer if (!title_box_owned) title_box.deinit(allocator); 62 63 var tabs_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 64 var tabs_box_owned = false; 65 errdefer if (!tabs_box_owned) tabs_box.deinit(allocator); 66 67 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 68 errdefer tab_ids.deinit(allocator); 69 70 const aa = session.page_arena.allocator(); 71 const ref_name = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.ref_or_oid_value)); 72 const bottom_label = try ui.clippedBottomLabel(try aa.alloc(u8, ui.clipped_bottom_label_max_len), ref_name); 73 const bottom_label_width = try xitui.width.displayWidth(bottom_label); 74 var first_group_width = try data.title.width(); 75 76 // the user's name (local mode has no user pages to link to) 77 if (!session.data.is_local) { 78 var text_buf: [evt.User.name_max_len + 1]u8 = undefined; 79 const text = try std.fmt.bufPrint(&text_buf, "{s}/", .{data.owner_name}); 80 const link = try std.fmt.allocPrint(aa, "a:/user/{s}", .{data.owner_name}); 81 82 var text_box = try wgt.TextBox(ui.Widget).init(allocator, text, .{ .border_style = .hidden, .wrap_kind = .none }); 83 errdefer text_box.deinit(allocator); 84 text_box.getFocus().focusable = true; 85 text_box.getFocus().kind = .{ .custom = link }; 86 first_group_width += try xitui.width.displayWidth(text) + 2; 87 try title_box.children.put(allocator, text_box.getFocus().id, .{ 88 .widget = .{ .text_box = text_box }, 89 .rect = null, 90 .min_size = null, 91 }); 92 } 93 94 // local routes carry no identity, so their urls come out elided 95 const identity = if (session.data.is_local) "" else try std.fmt.allocPrint(aa, "{s}/{s}", .{ data.owner_name, data.name }); 96 97 // title links to the repo's files root (the bare route, so it resolves 98 // to the default branch). 99 { 100 const files_root_route = ui.RoutablePage.repoFilesRoute(identity, null, "", "", 0) orelse return error.RouteTooLong; 101 const title_link = try std.fmt.allocPrint(aa, "a:{s}", .{try files_root_route.toUrl(session.page_arena)}); 102 var title_view = try ui.Title.View.init(allocator, &data.title); 103 errdefer title_view.deinit(allocator); 104 title_view.getFocus().focusable = true; 105 title_view.getFocus().kind = .{ .custom = title_link }; 106 try title_box.children.put(allocator, title_view.getFocus().id, .{ 107 .widget = .{ .title = title_view }, 108 .rect = null, 109 .min_size = null, 110 }); 111 } 112 113 // spacer 114 { 115 var text = try wgt.Text(ui.Widget).init(allocator, " "); 116 errdefer text.deinit(allocator); 117 try tabs_box.children.put(allocator, text.getFocus().id, .{ 118 .widget = .{ .text = text }, 119 .rect = null, 120 .min_size = .{ .width = 1, .height = null }, 121 }); 122 } 123 124 // every tab link is repo-scoped so selecting one stays on this page — 125 // switching its stack and updating the url — instead of navigating to 126 // the global settings or auth pages. the "ai:" prefix makes each an 127 // in-page anchor: crossPageLink ignores it so a wasm click just switches 128 // tabs (the page already holds every tab's content), while the href is 129 // still followed with js off. the files tab routes through the shared 130 // helper so the /repo/.../files url format lives in one place. 131 // both tabs link to the ref/oid this page is viewing, so switching tabs 132 // keeps the same ref (the files tab opens at its root directory). 133 const files_route = ui.RoutablePage.repoFilesRoute(identity, data.ref_or_oid, data.ref_or_oid_value, "", 0) orelse return error.RouteTooLong; 134 const files_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try files_route.toUrl(session.page_arena)}); 135 const commits_route = ui.RoutablePage.repoCommitsRoute(identity, data.ref_or_oid, data.ref_or_oid_value, 0, "") orelse return error.RouteTooLong; 136 const commits_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try commits_route.toUrl(session.page_arena)}); 137 const refs_route = ui.RoutablePage.repoRefsRoute(identity, .branch, "") orelse return error.RouteTooLong; 138 const refs_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try refs_route.toUrl(session.page_arena)}); 139 const issues_route = ui.RoutablePage.repoIssuesRoute(identity, .open, data.issues_tag, "") orelse return error.RouteTooLong; 140 const issues_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try issues_route.toUrl(session.page_arena)}); 141 const patches_route = ui.RoutablePage.repoPatchesRoute(identity, .open, data.patches_tag, "") orelse return error.RouteTooLong; 142 const patches_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try patches_route.toUrl(session.page_arena)}); 143 const discussions_route = ui.RoutablePage.repoDiscussionsRoute(identity, data.discussions_tag, "") orelse return error.RouteTooLong; 144 const discussions_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try discussions_route.toUrl(session.page_arena)}); 145 const events_route = ui.RoutablePage.repoEventsRoute(identity, .active, null, "") orelse return error.RouteTooLong; 146 const events_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try events_route.toUrl(session.page_arena)}); 147 const settings_route = ui.RoutablePage{ .repo_settings = Array.from(identity) orelse return error.RouteTooLong }; 148 const settings_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try settings_route.toUrl(session.page_arena)}); 149 const auth_route = ui.RoutablePage{ .repo_auth = Array.from(identity) orelse return error.RouteTooLong }; 150 const auth_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try auth_route.toUrl(session.page_arena)}); 151 152 // the tab matching the current page is focused initially; matching by 153 // link (rather than position) keeps this robust to tab changes. 154 const current_link: []const u8 = switch (session.data.current_page) { 155 .repo_commits => commits_link, 156 .repo_refs => refs_link, 157 .repo_issues => issues_link, 158 .repo_patches => patches_link, 159 .repo_discussions => discussions_link, 160 .repo_events => events_link, 161 .repo_settings => settings_link, 162 .repo_auth => auth_link, 163 else => files_link, 164 }; 165 var selected_tab: ?usize = null; 166 167 // files tab 168 { 169 var text_box = try wgt.TextBox(ui.Widget).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().focusable = true; 172 text_box.getFocus().kind = .{ .custom = files_link }; 173 try tab_ids.put(allocator, text_box.getFocus().id, {}); 174 if (std.mem.eql(u8, files_link, current_link)) 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 tab 183 { 184 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "commits", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .bottom_label = bottom_label }); 185 errdefer text_box.deinit(allocator); 186 text_box.getFocus().focusable = true; 187 text_box.getFocus().kind = .{ .custom = commits_link }; 188 try tab_ids.put(allocator, text_box.getFocus().id, {}); 189 if (std.mem.eql(u8, commits_link, current_link)) 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("commits".len, bottom_label_width) + 2, .height = null }, 194 }); 195 } 196 197 // refs tab 198 { 199 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "refs", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 200 errdefer text_box.deinit(allocator); 201 text_box.getFocus().focusable = true; 202 text_box.getFocus().kind = .{ .custom = refs_link }; 203 try tab_ids.put(allocator, text_box.getFocus().id, {}); 204 if (std.mem.eql(u8, refs_link, current_link)) 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(ui.Widget).init(allocator, "issues", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 215 errdefer text_box.deinit(allocator); 216 text_box.getFocus().focusable = true; 217 text_box.getFocus().kind = .{ .custom = issues_link }; 218 try tab_ids.put(allocator, text_box.getFocus().id, {}); 219 if (std.mem.eql(u8, issues_link, current_link)) 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(ui.Widget).init(allocator, "patches", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 230 errdefer text_box.deinit(allocator); 231 text_box.getFocus().focusable = true; 232 text_box.getFocus().kind = .{ .custom = patches_link }; 233 try tab_ids.put(allocator, text_box.getFocus().id, {}); 234 if (std.mem.eql(u8, patches_link, current_link)) 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(ui.Widget).init(allocator, "discussions", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 245 errdefer text_box.deinit(allocator); 246 text_box.getFocus().focusable = true; 247 text_box.getFocus().kind = .{ .custom = discussions_link }; 248 try tab_ids.put(allocator, text_box.getFocus().id, {}); 249 if (std.mem.eql(u8, discussions_link, current_link)) 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(ui.Widget).init(allocator, "events", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 260 errdefer text_box.deinit(allocator); 261 text_box.getFocus().focusable = true; 262 text_box.getFocus().kind = .{ .custom = events_link }; 263 try tab_ids.put(allocator, text_box.getFocus().id, {}); 264 if (std.mem.eql(u8, events_link, current_link)) 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.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(ui.Widget).init(allocator, "settings", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 288 errdefer text_box.deinit(allocator); 289 text_box.getFocus().focusable = true; 290 text_box.getFocus().kind = .{ .custom = settings_link }; 291 try tab_ids.put(allocator, text_box.getFocus().id, {}); 292 if (std.mem.eql(u8, settings_link, current_link)) 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.is_local) { 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 (std.mem.eql(u8, auth_link, current_link)) 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(ui.Widget).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().focusable = true; 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 .data = data, 351 .tab_ids = tab_ids, 352 .tabs_id = tabs_id, 353 .first_group_width = first_group_width, 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 tabs_child = self.tabsChild(); 366 const tabs_box = &tabs_child.widget.box; 367 368 // only the selected tab shows its border 369 const selected_tab = if (box.getFocus().child_id == self.tabs_id) tabs_box.getFocus().child_id else null; 370 var tabs_width: usize = 0; 371 for (tabs_box.children.keys(), tabs_box.children.values()) |id, *child| { 372 const tb: ?*wgt.TextBox(ui.Widget) = switch (child.widget) { 373 .text_box => |*x| x, 374 .auth_tab => |*at| blk: { 375 // the label tracks login state per frame, so the width must too 376 child.min_size = .{ .width = at.minWidth(), .height = null }; 377 break :blk &at.text_box; 378 }, 379 else => null, 380 }; 381 if (tb) |t| { 382 t.options.border_style = if (selected_tab == id) .single else .hidden; 383 } 384 tabs_width += if (child.min_size) |min_size| min_size.width orelse 0 else 0; 385 } 386 387 const viewport_width = constraint.max_size.width orelse constraint.min_size.width; 388 // the outer box's hidden border occupies two columns. keep the title 389 // and tabs together if they fit; otherwise tab strip on its own row. 390 const content_width = if (viewport_width) |width| width -| 2 else null; 391 const wrap = if (content_width) |width| self.first_group_width + tabs_width > width else false; 392 box.options.direction = if (wrap) .vert else .horiz; 393 tabs_child.min_size = if (wrap) .{ .width = content_width, .height = null } else null; 394 395 var scroll_constraint = constraint; 396 scroll_constraint.min_size.width = viewport_width; 397 try self.scroll.build(allocator, scroll_constraint, root_focus); 398 } 399 400 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 401 _ = allocator; 402 const current_tab = self.currentTabIndex() orelse return; 403 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 404 const tab_id = self.tab_ids.keys()[new_tab]; 405 root_focus.setFocus(tab_id); 406 const tabs_child = self.tabsChild(); 407 const tabs_rect = tabs_child.rect orelse return; 408 const tab = tabs_child.widget.box.children.get(tab_id) orelse return; 409 var rect = tab.rect orelse return; 410 rect.x += tabs_rect.x; 411 rect.y += tabs_rect.y; 412 self.scroll.scrollToRect(rect); 413 } 414 } 415 416 pub fn clearGrid(self: *View) void { 417 self.scroll.clearGrid(); 418 } 419 420 pub fn getGrid(self: View) ?Grid { 421 return self.scroll.getGrid(); 422 } 423 424 pub fn getFocus(self: *View) *Focus { 425 return self.scroll.getFocus(); 426 } 427 428 pub fn getSelectedIndex(self: View) ?usize { 429 return self.currentTabIndex(); 430 } 431 432 fn currentTabIndex(self: View) ?usize { 433 if (self.scroll.child.box.focus.child_id != self.tabs_id) return null; 434 const child_id = self.tabsChild().widget.box.focus.child_id orelse return null; 435 return self.tab_ids.getIndex(child_id); 436 } 437 438 fn tabsChild(self: *const View) *wgt.Box(ui.Widget).Child { 439 return self.scroll.child.box.children.getPtr(self.tabs_id) orelse unreachable; 440 } 441 }; 442