╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ User.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
214 const aa = item_arena.allocator(); 215 var items: std.ArrayList(ui.widget.FlowBox.Item) = .empty; 216 if (data.forks_start > 0) 217 try items.append(aa, .{ .text = "← previous", .link = try std.fmt.allocPrint(aa, "a:/user/{s}/forks/start:{d}", .{ data.user.name, data.forks_start -| page_size }) }); 218 for (data.forks) |fork_item| { 219 // the fork route names the forker 220 const identity = try std.fmt.allocPrint(aa, "{s}/{s}", .{ data.user.name, fork_item.target.name }); 221 const route = ui.RoutablePage.forkPatchRoute(identity, fork_item.id) orelse return error.RouteTooLong; 222 try items.append(aa, .{ 223 .text = try std.fmt.allocPrint(aa, "{s} - {s}", .{ fork_item.target.name, fork_item.title }), 224 .link = try std.fmt.allocPrint(aa, "a:{s}", .{try route.toUrl(session.page_arena)}), 225 .bottom_label = if (fork_item.target.read_access == .private) "(private)" else "", 226 }); 227 } 228 if (data.forks_next_start) |next_start| 229 try items.append(aa, .{ .text = "next →", .link = try std.fmt.allocPrint(aa, "a:/user/{s}/forks/start:{d}", .{ data.user.name, next_start }) }); 230 try list.setItems(allocator, items.items); 231 try stack.children.put(allocator, list.getFocus().id, .{ .flow_box_scroll = list }); 232 } 233 234 // the header has no new repo tab without a login, so keep the 235 // stack's children 1:1 with the tabs by skipping that view too 236 if (session.data.user_id != null) { 237 const route = ui.RoutablePage{ .user_new_repo = ui.RoutablePage.Array(evt.User.name_max_len).from(data.user.name) orelse return error.RouteTooLong }; 238 var new_repo_view = try ui.NewRepo.View.init(allocator, session, route); 239 errdefer new_repo_view.deinit(allocator); 240 try stack.children.put(allocator, new_repo_view.getFocus().id, .{ .new_repo = new_repo_view }); 241 } 242 243 { 244 var auth_view = try Auth.View.init(allocator, &data.auth, session); 245 errdefer auth_view.deinit(allocator); 246 try stack.children.put(allocator, auth_view.getFocus().id, .{ .home_auth = auth_view }); 247 } 248 249 if (session.is_terminal) { 250 var quit_view = try Quit.View.init(allocator, session); 251 errdefer quit_view.deinit(allocator); 252 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 253 } 254 255 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 256 } 257 258 var self = View{ .box = box }; 259 // search results open in the tab's own search box 260 self.getFocus().child_id = box.children.keys()[if (data.repos_search != null) stack_index else header_index]; 261 return self; 262 } 263 264 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 265 self.box.deinit(allocator); 266 } 267 268 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 269 self.clearGrid(); 270 const header = &self.box.children.values()[header_index].widget.user_header; 271 const stack = &self.box.children.values()[stack_index].widget.stack; 272 273 // each header tab maps 1:1 to a stack child by position 274 if (header.getSelectedIndex()) |index| 275 stack.getFocus().child_id = stack.children.keys()[index]; 276 try self.box.build(allocator, constraint, root_focus); 277 } 278 279 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 280 const stack = &self.box.children.values()[stack_index].widget.stack; 281 if (self.getFocus().child_id) |child_id| { 282 if (self.box.children.getIndex(child_id)) |current_index| { 283 const child = &self.box.children.values()[current_index].widget; 284 var index = current_index; 285 286 const direction = inp.vertDirection(key); 287 288 switch (direction) { 289 .up => { 290 switch (child.*) { 291 .user_header => { 292 try child.input(allocator, key, root_focus); 293 }, 294 .stack => { 295 if (child.stack.getSelected()) |selected_widget| { 296 if (selected_widget.atTop(root_focus)) { 297 index = header_index; 298 } else { 299 try child.input(allocator, key, root_focus); 300 } 301 } 302 }, 303 else => {}, 304 } 305 }, 306 .down => { 307 switch (child.*) { 308 .user_header => { 309 if (stack.getSelected()) |selected_widget| switch (selected_widget.*) { 310 .user_repos => |*v| return v.focusHeader(root_focus), 311 // an empty list has nothing to focus 312 .flow_box_scroll => |*v| if (v.isEmpty()) return, 313 else => {}, 314 }; 315 index = stack_index; 316 }, 317 .stack => { 318 try child.input(allocator, key, root_focus); 319 }, 320 else => {}, 321 } 322 }, 323 .none => { 324 try child.input(allocator, key, root_focus); 325 }, 326 } 327 328 if (index != current_index) { 329 root_focus.setFocus(self.box.children.keys()[index]); 330 } 331 } 332 } 333 } 334 335 pub fn clearGrid(self: *View) void { 336 self.box.clearGrid(); 337 } 338 339 pub fn getGrid(self: View) ?Grid { 340 return self.box.getGrid(); 341 } 342 343 pub fn getFocus(self: *View) *Focus { 344 return self.box.getFocus(); 345 } 346 }; 347 348 pub const ReposView = struct { 349 // a vertical box: the search sub-header above the scrolling list. focus 350 // points at the header's box or the list's selected row. 351 box: wgt.Box(ui.Widget), 352 data: *const Self, 353 session: *ui.Session, 354 355 const header_index = 0; 356 const list_index = 1; 357 358 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !ReposView { 359 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 360 errdefer box.deinit(allocator); 361 362 { 363 var header = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 364 errdefer header.deinit(allocator); 365 { 366 var search_box = try ui.widget.SearchBox.init(allocator, session, " search ", "search", data.repos_search); 367 errdefer search_box.deinit(allocator); 368 header.getFocus().child_id = search_box.getFocus().id; 369 try header.children.put(allocator, search_box.getFocus().id, .{ .widget = .{ .search_box = search_box }, .rect = null, .min_size = ui.widget.SearchBox.min_size }); 370 } 371 // the spacer keeps the box at its own width 372 { 373 var spacer = try ui.widget.Spacer.init(allocator); 374 errdefer spacer.deinit(allocator); 375 try header.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 376 } 377 try box.children.put(allocator, header.getFocus().id, .{ .widget = .{ .box = header }, .rect = null, .min_size = .{ .width = null, .height = ui.widget.SearchBox.min_size.height } }); 378 } 379 380 { 381 var list = try ui.widget.FlowBox.Scroll.init(allocator, .{}, !session.is_terminal); 382 errdefer list.deinit(allocator); 383 384 var arena = std.heap.ArenaAllocator.init(allocator); 385 defer arena.deinit(); 386 const aa = arena.allocator(); 387 388 const route = (reposRoute(data.user.name) orelse return error.RouteTooLong).withSearch(data.repos_search orelse "") orelse return error.RouteTooLong; 389 390 // a leading "previous" row off the first window, one row per repo, 391 // then a trailing "next" row when more remain. each window row 392 // navigates to the adjacent window of this user's repos. 393 var items: std.ArrayList(ui.widget.FlowBox.Item) = .empty; 394 if (data.repos_start > 0) { 395 var prev = route; 396 prev.user_repos.start = data.repos_start -| page_size; 397 try items.append(aa, .{ .text = "← previous", .link = try std.fmt.allocPrint(aa, "a:{s}", .{try prev.toUrl(&arena)}) }); 398 } 399 for (data.repos) |repo| 400 // clicking a repo opens its page; the "a:" prefix makes the web 401 // renderer emit an <a href="/repo/alice/foo"> anchor. 402 try items.append(aa, .{ 403 .text = if (repo.event.description.len == 0) repo.event.name else try std.fmt.allocPrint(aa, "{s} - {s}", .{ repo.event.name, repo.event.description }), 404 .link = try std.fmt.allocPrint(aa, "a:/repo/{s}/{s}", .{ data.user.name, repo.event.name }), 405 .bottom_label = if (repo.event.read_access == .private) "(private)" else "", 406 }); 407 if (data.repos_next_start) |next_start| { 408 var next = route; 409 next.user_repos.start = next_start; 410 try items.append(aa, .{ .text = "next →", .link = try std.fmt.allocPrint(aa, "a:{s}", .{try next.toUrl(&arena)}) }); 411 } 412 try list.setItems(allocator, items.items); 413 414 try box.children.put(allocator, list.getFocus().id, .{ .widget = .{ .flow_box_scroll = list }, .rect = null, .min_size = null }); 415 } 416 417 // search results start in the box so the term can be refined right away 418 box.getFocus().child_id = box.children.keys()[if (data.repos_search != null) header_index else list_index]; 419 return .{ .box = box, .data = data, .session = session }; 420 } 421 422 pub fn deinit(self: *ReposView, allocator: std.mem.Allocator) void { 423 self.box.deinit(allocator); 424 } 425 426 pub fn build(self: *ReposView, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 427 self.clearGrid(); 428 // clear the incoming min height so the list scrolls within what's left 429 try self.box.build(allocator, .{ 430 .min_size = .{ .width = null, .height = null }, 431 .max_size = constraint.max_size, 432 }, root_focus); 433 } 434 435 pub fn input(self: *ReposView, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 436 const direction = inp.vertDirection(key); 437 if (self.headerActive()) { 438 const search_box = self.searchBox(); 439 if (direction == .down) return root_focus.setFocus(self.listScroll().getFocus().id); 440 if (key == .enter) { 441 const text = try search_box.text(allocator); 442 defer allocator.free(text); 443 return self.submit(text); 444 } 445 return search_box.input(allocator, key, root_focus); 446 } 447 // up from the first row reaches the search box 448 if (direction == .up and self.listScroll().atTop()) return self.focusHeader(root_focus); 449 try self.listScroll().input(allocator, key, root_focus); 450 } 451 452 // the repos narrowed to `text` from the first window. an empty box on an 453 // unsearched page has nothing to clear. 454 fn submit(self: *ReposView, text: []const u8) !void { 455 if (text.len == 0 and self.data.repos_search == null) return; 456 const route = reposRoute(self.data.user.name) orelse return; 457 try self.session.navigate(route.withSearch(text) orelse return); 458 } 459 460 fn searchBox(self: *ReposView) *ui.widget.SearchBox { 461 return &self.box.children.values()[header_index].widget.box.children.values()[0].widget.search_box; 462 } 463 464 fn listScroll(self: *ReposView) *ui.widget.FlowBox.Scroll { 465 return &self.box.children.values()[list_index].widget.flow_box_scroll; 466 } 467 468 fn headerActive(self: *ReposView) bool { 469 return self.box.getFocus().child_id == self.box.children.keys()[header_index]; 470 } 471 472 pub fn clearGrid(self: *ReposView) void { 473 self.box.clearGrid(); 474 } 475 476 pub fn getGrid(self: ReposView) ?Grid { 477 return self.box.getGrid(); 478 } 479 480 pub fn getFocus(self: *ReposView) *Focus { 481 return self.box.getFocus(); 482 } 483 484 // up leaves the tab from the search box 485 pub fn atTop(self: *ReposView) bool { 486 return self.headerActive(); 487 } 488 489 pub fn focusHeader(self: *ReposView, root_focus: *Focus) void { 490 root_focus.setFocus(self.searchBox().getFocus().id); 491 } 492 }; 493 494 // the first window of `name`'s repos 495 fn reposRoute(name: []const u8) ?ui.RoutablePage { 496 return .{ .user_repos = .{ .name = ui.RoutablePage.Array(evt.User.name_max_len).from(name) orelse return null } }; 497 } 498