╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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