╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ User.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
295 index = header_index; 296 } else { 297 try child.input(allocator, key, root_focus); 298 } 299 } 300 }, 301 else => {}, 302 } 303 }, 304 .down => { 305 switch (child.*) { 306 .user_header => { 307 if (stack.getSelected()) |selected_widget| switch (selected_widget.*) { 308 .user_repos => |*v| return v.focusHeader(root_focus), 309 // an empty list has nothing to focus 310 .flow_box_scroll => |*v| if (v.isEmpty()) return, 311 else => {}, 312 }; 313 index = stack_index; 314 }, 315 .stack => { 316 try child.input(allocator, key, root_focus); 317 }, 318 else => {}, 319 } 320 }, 321 .none => { 322 try child.input(allocator, key, root_focus); 323 }, 324 } 325 326 if (index != current_index) { 327 root_focus.setFocus(self.box.children.keys()[index]); 328 } 329 } 330 } 331 } 332 333 pub fn clearGrid(self: *View) void { 334 self.box.clearGrid(); 335 } 336 337 pub fn getGrid(self: View) ?Grid { 338 return self.box.getGrid(); 339 } 340 341 pub fn getFocus(self: *View) *Focus { 342 return self.box.getFocus(); 343 } 344 }; 345 346 pub const ReposView = struct { 347 // a vertical box: the search sub-header above the scrolling list. focus 348 // points at the header's box or the list's selected row. 349 box: wgt.Box(ui.Widget), 350 data: *const Self, 351 session: *ui.Session, 352 353 const header_index = 0; 354 const list_index = 1; 355 356 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !ReposView { 357 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 358 errdefer box.deinit(allocator); 359 360 { 361 var header = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 362 errdefer header.deinit(allocator); 363 { 364 var search_box = try ui.widget.SearchBox.init(allocator, session, " search ", "search", data.repos_search); 365 errdefer search_box.deinit(allocator); 366 header.getFocus().child_id = search_box.getFocus().id; 367 try header.children.put(allocator, search_box.getFocus().id, .{ .widget = .{ .search_box = search_box }, .rect = null, .min_size = ui.widget.SearchBox.min_size }); 368 } 369 // the spacer keeps the box at its own width 370 { 371 var spacer = try ui.widget.Spacer.init(allocator); 372 errdefer spacer.deinit(allocator); 373 try header.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 374 } 375 try box.children.put(allocator, header.getFocus().id, .{ .widget = .{ .box = header }, .rect = null, .min_size = .{ .width = null, .height = ui.widget.SearchBox.min_size.height } }); 376 } 377 378 { 379 var list = try ui.widget.FlowBox.Scroll.init(allocator, .{}, !session.is_terminal); 380 errdefer list.deinit(allocator); 381 382 var arena = std.heap.ArenaAllocator.init(allocator); 383 defer arena.deinit(); 384 const aa = arena.allocator(); 385 386 const route = (reposRoute(data.user.name) orelse return error.RouteTooLong).withSearch(data.repos_search orelse "") orelse return error.RouteTooLong; 387 388 // a leading "previous" row off the first window, one row per repo, 389 // then a trailing "next" row when more remain. each window row 390 // navigates to the adjacent window of this user's repos. 391 var items: std.ArrayList(ui.widget.FlowBox.Item) = .empty; 392 if (data.repos_start > 0) { 393 var prev = route; 394 prev.user_repos.start = data.repos_start -| page_size; 395 try items.append(aa, .{ .text = "← previous", .link = try std.fmt.allocPrint(aa, "a:{s}", .{try prev.toUrl(&arena)}) }); 396 } 397 for (data.repos) |repo| 398 // clicking a repo opens its page; the "a:" prefix makes the web 399 // renderer emit an <a href="/repo/alice/foo"> anchor. 400 try items.append(aa, .{ 401 .text = if (repo.event.description.len == 0) repo.event.name else try std.fmt.allocPrint(aa, "{s} - {s}", .{ repo.event.name, repo.event.description }), 402 .link = try std.fmt.allocPrint(aa, "a:/repo/{s}/{s}", .{ data.user.name, repo.event.name }), 403 }); 404 if (data.repos_next_start) |next_start| { 405 var next = route; 406 next.user_repos.start = next_start; 407 try items.append(aa, .{ .text = "next →", .link = try std.fmt.allocPrint(aa, "a:{s}", .{try next.toUrl(&arena)}) }); 408 } 409 try list.setItems(allocator, items.items); 410 411 try box.children.put(allocator, list.getFocus().id, .{ .widget = .{ .flow_box_scroll = list }, .rect = null, .min_size = null }); 412 } 413 414 // search results start in the box so the term can be refined right away 415 box.getFocus().child_id = box.children.keys()[if (data.repos_search != null) header_index else list_index]; 416 return .{ .box = box, .data = data, .session = session }; 417 } 418 419 pub fn deinit(self: *ReposView, allocator: std.mem.Allocator) void { 420 self.box.deinit(allocator); 421 } 422 423 pub fn build(self: *ReposView, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 424 self.clearGrid(); 425 // clear the incoming min height so the list scrolls within what's left 426 try self.box.build(allocator, .{ 427 .min_size = .{ .width = null, .height = null }, 428 .max_size = constraint.max_size, 429 }, root_focus); 430 } 431 432 pub fn input(self: *ReposView, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 433 const direction = inp.vertDirection(key); 434 if (self.headerActive()) { 435 const search_box = self.searchBox(); 436 if (direction == .down) return root_focus.setFocus(self.listScroll().getFocus().id); 437 if (key == .enter) { 438 const text = try search_box.text(allocator); 439 defer allocator.free(text); 440 return self.submit(text); 441 } 442 return search_box.input(allocator, key, root_focus); 443 } 444 // up from the first row reaches the search box 445 if (direction == .up and self.listScroll().atTop()) return self.focusHeader(root_focus); 446 try self.listScroll().input(allocator, key, root_focus); 447 } 448 449 // the repos narrowed to `text` from the first window. an empty box on an 450 // unsearched page has nothing to clear. 451 fn submit(self: *ReposView, text: []const u8) !void { 452 if (text.len == 0 and self.data.repos_search == null) return; 453 const route = reposRoute(self.data.user.name) orelse return; 454 try self.session.navigate(route.withSearch(text) orelse return); 455 } 456 457 fn searchBox(self: *ReposView) *ui.widget.SearchBox { 458 return &self.box.children.values()[header_index].widget.box.children.values()[0].widget.search_box; 459 } 460 461 fn listScroll(self: *ReposView) *ui.widget.FlowBox.Scroll { 462 return &self.box.children.values()[list_index].widget.flow_box_scroll; 463 } 464 465 fn headerActive(self: *ReposView) bool { 466 return self.box.getFocus().child_id == self.box.children.keys()[header_index]; 467 } 468 469 pub fn clearGrid(self: *ReposView) void { 470 self.box.clearGrid(); 471 } 472 473 pub fn getGrid(self: ReposView) ?Grid { 474 return self.box.getGrid(); 475 } 476 477 pub fn getFocus(self: *ReposView) *Focus { 478 return self.box.getFocus(); 479 } 480 481 // up leaves the tab from the search box 482 pub fn atTop(self: *ReposView) bool { 483 return self.headerActive(); 484 } 485 486 pub fn focusHeader(self: *ReposView, root_focus: *Focus) void { 487 root_focus.setFocus(self.searchBox().getFocus().id); 488 } 489 }; 490 491 // the first window of `name`'s repos 492 fn reposRoute(name: []const u8) ?ui.RoutablePage { 493 return .{ .user_repos = .{ .name = ui.RoutablePage.Array(evt.User.name_max_len).from(name) orelse return null } }; 494 } 495