╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
556 } 557 558 { 559 var description = try wgt.TextInput(ui.Widget).init(allocator, .{ .label = " description ", .name = "description", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal, .multiline = true, .scroll = .{ .fill = true } }); 560 errdefer description.deinit(allocator); 561 description.getFocus().focusable = true; 562 if (issue) |i| try description.setContent(allocator, i.event.description); 563 try box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .text_input = description }, .rect = null, .min_size = null }); 564 } 565 566 { 567 var button = try wgt.TextBox(ui.Widget).init(allocator, "submit", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 568 errdefer button.deinit(allocator); 569 button.getFocus().focusable = true; 570 // the renderer distinguishes plain clickables from buttons that 571 // should POST to a server route by this kind. 572 button.getFocus().kind = .{ .custom = "submit" }; 573 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 574 } 575 576 // absorbs the leftover min-height the box hands its last child, so 577 // the button keeps its natural height 578 { 579 var spacer = try ui.Spacer.init(allocator); 580 errdefer spacer.deinit(allocator); 581 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 582 } 583 584 box.getFocus().child_id = box.children.keys()[title_field_index]; 585 return box; 586 } 587 588 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, link: []const u8) !void { 589 var row = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 590 errdefer row.deinit(allocator); 591 row.getFocus().focusable = true; 592 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 593 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 594 } 595 596 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 597 self.box.deinit(allocator); 598 } 599 600 fn header(self: *View) *Header { 601 return &self.box.children.values()[header_index].widget.repo_issues_header; 602 } 603 604 fn viewStack(self: *View) *wgt.Stack(ui.Widget) { 605 return &self.box.children.values()[stack_index].widget.stack; 606 } 607 608 // `index`'s master-detail split inside the stack. 609 fn resultsBox(self: *View, index: usize) *wgt.Box(ui.Widget) { 610 return &self.viewStack().children.values()[index].box; 611 } 612 613 // the tags view's flow inside the stack. 614 fn tagsView(self: *View) *ui.TagFlow { 615 return &self.viewStack().children.values()[tags_view_index].tag_flow; 616 } 617 618 // the new-issue (or edit) form inside the stack, or null when the 619 // unauthorized view stands in for it. 620 fn issueForm(self: *View) ?*wgt.Box(ui.Widget) { 621 return switch (self.viewStack().children.values()[form_view_index]) { 622 .box => |*box| box, 623 else => null, 624 }; 625 } 626 627 fn listScroll(self: *View, index: usize) *wgt.Scroll(ui.Widget) { 628 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 629 } 630 631 fn listBox(self: *View, index: usize) *wgt.Box(ui.Widget) { 632 return &self.listScroll(index).child.box; 633 } 634 635 // the detail frame's children: the tool row above the scroll pane. 636 const tool_row_index: usize = 0; 637 const detail_scroll_index: usize = 1; 638 639 fn detailOuter(self: *View, index: usize) *wgt.Box(ui.Widget) { 640 return &self.resultsBox(index).children.values()[detail_index].widget.box; 641 } 642 643 fn toolRow(self: *View, index: usize) *wgt.Box(ui.Widget) { 644 return &self.detailOuter(index).children.values()[tool_row_index].widget.box; 645 } 646 647 fn detailScroll(self: *View, index: usize) *wgt.Scroll(ui.Widget) { 648 return &self.detailOuter(index).children.values()[detail_scroll_index].widget.scroll; 649 } 650 651 fn detailInner(self: *View, index: usize) *wgt.Box(ui.Widget) { 652 return &self.detailScroll(index).child.box; 653 } 654 655 fn window(self: *View, index: usize) *const Window { 656 return self.data.window(splitStatus(index)); 657 } 658 659 fn stackSelectedIndex(self: *View) ?usize { 660 const stack = self.viewStack(); 661 const cid = stack.getFocus().child_id orelse return null; 662 return stack.children.getIndex(cid); 663 } 664 665 // the stack's selected master-detail split (null when the tags view or the 666 // new-issue form shows). 667 fn selectedSplitIndex(self: *View) ?usize { 668 const idx = self.stackSelectedIndex() orelse return null; 669 return if (idx < split_count) idx else null; 670 } 671 672 fn detailActive(self: *View, index: usize) bool { 673 const rb = self.resultsBox(index); 674 const cid = rb.getFocus().child_id orelse return false; 675 return rb.children.getIndex(cid) == detail_index; 676 } 677 678 fn headerActive(self: *View) bool { 679 const cid = self.box.getFocus().child_id orelse return false; 680 return self.box.children.getIndex(cid) == header_index; 681 } 682 683 fn tagsViewActive(self: *View) bool { 684 if (self.headerActive()) return false; 685 return self.stackSelectedIndex() == tags_view_index; 686 } 687 688 fn formViewActive(self: *View) bool { 689 if (self.headerActive()) return false; 690 return self.stackSelectedIndex() == form_view_index; 691 } 692 693 // the selected issue's index, or null when a window-navigation row is 694 // selected (a leading "previous" row shifts the issue rows down by one). 695 fn selectedIssueIndex(self: *View, index: usize) ?usize { 696 const lb = self.listBox(index); 697 const cid = lb.getFocus().child_id orelse return null; 698 const idx = lb.children.getIndex(cid) orelse return null; 699 const win = self.window(index); 700 const lead: usize = if (win.prev_id != null) 1 else 0; 701 if (idx < lead or idx - lead >= win.issues.len) return null; 702 return idx - lead; 703 } 704 705 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 706 self.clearGrid(); 707 708 // each header tab maps 1:1 to a stack child by position. mirror the 709 // selection into the url: the view the page's window is rooted at 710 // keeps its rooted url, the others get their list route (keeping the 711 // tag filter). 712 if (self.header().getSelectedIndex()) |index| { 713 const stack = self.viewStack(); 714 stack.getFocus().child_id = stack.children.keys()[index]; 715 self.session.data.current_page = (if (index == form_view_index and self.data.view == .edit) 716 ui.RoutablePage.repoIssuesEditRoute(self.data.identity, self.data.selected_id) 717 else if (index == viewIndex(self.data.view) and self.data.selected_id.len != 0) 718 (if (self.data.description_page) 719 ui.RoutablePage.repoIssuesDescriptionRoute(self.data.identity, self.data.selected_id) 720 else 721 ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, self.data.tag, self.data.selected_id)) 722 else if (index == tags_view_index) 723 ui.RoutablePage.repoIssuesTagsRoute(self.data.identity, self.data.tag) 724 else if (index == form_view_index) 725 ui.RoutablePage.repoIssuesNewRoute(self.data.identity) 726 else 727 ui.RoutablePage.repoIssuesRoute(self.data.identity, splitStatus(index), self.data.tag, "")) orelse self.session.data.current_page; 728 } 729 730 if (self.selectedSplitIndex()) |i| { 731 // swap the detail pane to the selected issue when it changes. 732 try self.refreshDetail(allocator, i); 733 734 // mirror the focused issue into the url, but only while focus is 735 // inside the split. an issue's url is the same whether or not the 736 // list is filtered, so the mirror drops the tag. 737 if (root_focus.grandchild_id) |g| { 738 if (self.resultsBox(i).getFocus().children.contains(g)) { 739 if (self.selectedIssueIndex(i)) |sel| { 740 // selecting another issue leaves the description page 741 // behind, like it leaves the tag filter behind. 742 const id = self.window(i).issues[sel].id; 743 const mirrored = if (self.data.description_page and std.mem.eql(u8, id, self.data.selected_id)) 744 ui.RoutablePage.repoIssuesDescriptionRoute(self.data.identity, id) 745 else 746 ui.RoutablePage.repoIssuesRoute(self.data.identity, splitStatus(i), "", id); 747 if (mirrored) |route| self.session.data.current_page = route; 748 } 749 } 750 } 751 752 // the selected list row shows a border (the focused TextBox 753 // upgrades it to a double border itself); the rest stay borderless. 754 const lb = self.listBox(i); 755 for (lb.children.keys(), lb.children.values()) |id, *child| { 756 switch (child.widget) { 757 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 758 else => {}, 759 } 760 } 761 762 // the pane's selected child shows the selection border: the 763 // description directly, the tags via the flow's selected item. 764 // cap the list at list_max_width only while the detail pane fits 765 // beside it. the box drops the detail when the width can't hold 766 // both minimums, so when it's that narrow we lift the cap and let 767 // the list fill the whole width. 768 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 769 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 770 771 // stretch the detail pane across the rest of the width so it fills 772 // the area rather than shrinking to its content; its scroll fills 773 // the pane. 774 if (constraint.max_size.width) |w| { 775 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 776 } else { 777 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 778 } 779 } 780 781 // refresh the form inputs' entries in the session's focus-id -> input 782 // map with this frame's addresses, so the web/wasm form handling can 783 // find them by focus id 784 if (self.issueForm()) |form| { 785 const inputs_arena = self.session.arena.allocator(); 786 for (form.children.values()) |*child| switch (child.widget) { 787 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 788 else => {}, 789 }; 790 791 // on an edit page, register the issue's content as the inputs' 792 // page-constant initial values, which the web overlay renders 793 // into them. 794 if (self.data.view == .edit) { 795 if (self.data.selectedIssue()) |entry| { 796 const fields = form.children.values(); 797 try self.session.input_values.put(inputs_arena, fields[title_field_index].widget.text_input.getFocus().id, entry.issue.event.title); 798 try self.session.input_values.put(inputs_arena, fields[tags_field_index].widget.text_input.getFocus().id, entry.issue.event.tags); 799 try self.session.input_values.put(inputs_arena, fields[description_field_index].widget.text_input.getFocus().id, entry.issue.event.description); 800 } 801 } 802 } 803 804 try self.box.build(allocator, constraint, root_focus); 805 } 806 807 fn refreshDetail(self: *View, allocator: std.mem.Allocator, index: usize) !void { 808 const sel = self.selectedIssueIndex(index) orelse return; 809 if (self.detailed_index[index]) |d| if (d == sel) return; 810 try self.populateDetail(allocator, index, sel); 811 self.detailed_index[index] = sel; 812 } 813 814 fn populateDetail(self: *View, allocator: std.mem.Allocator, index: usize, sel: usize) !void { 815 const entry = self.window(index).issues[sel]; 816 const inner = self.detailInner(index); 817 // the /description page replaces its issue's detail with a back link 818 // and the whole description; other issues keep their normal detail. 819 const description_page = self.data.description_page and std.mem.eql(u8, entry.id, self.data.selected_id); 820 821 for (inner.children.values()) |*child| child.widget.deinit(allocator); 822 inner.children.clearAndFree(allocator); 823 inner.getFocus().child_id = null; 824 self.author_id[index] = null; 825 826 // the tool row: the open/close and edit buttons; the description page 827 // shows none. 828 { 829 const row = self.toolRow(index); 830 for (row.children.values()) |*child| child.widget.deinit(allocator); 831 row.children.clearAndFree(allocator); 832 row.getFocus().child_id = null; 833 row.getFocus().kind = .container; 834 } 835 if (!description_page) { 836 const row = self.toolRow(index); 837 const pa = self.session.page_arena.allocator(); 838 839 { 840 var spacer = try ui.Spacer.init(allocator); 841 errdefer spacer.deinit(allocator); 842 try row.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 843 } 844 845 // a logged-out session can't flip the status, so it gets no 846 // open/close button (and no form action on the row) 847 if (self.session.data.is_local or self.session.data.user_id != null) { 848 const action: []const u8 = switch (entry.issue.event.status) { 849 .open => "close", 850 .closed => "open", 851 }; 852 row.getFocus().kind = .{ .custom = if (self.data.identity.len == 0) 853 try std.fmt.allocPrint(pa, "form:/issues/{s}/{s}", .{ entry.id, action }) 854 else 855 try std.fmt.allocPrint(pa, "form:/repo/{s}/issues/{s}/{s}", .{ self.data.identity, entry.id, action }) }; 856 857 var button = try wgt.TextBox(ui.Widget).init(allocator, action, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 858 errdefer button.deinit(allocator); 859 button.getFocus().focusable = true; 860 // the renderer distinguishes plain clickables from buttons that 861 // should POST to a server route by this kind. 862 button.getFocus().kind = .{ .custom = "submit" }; 863 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = action.len + 2, .height = null } }); 864 } 865 866 // the edit button links to the issue's edit page. 867 { 868 var button = try wgt.TextBox(ui.Widget).init(allocator, "edit", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 869 errdefer button.deinit(allocator); 870 button.getFocus().focusable = true; 871 const route = ui.RoutablePage.repoIssuesEditRoute(self.data.identity, entry.id) orelse return error.RouteTooLong; 872 button.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 873 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = "edit".len + 2, .height = null } }); 874 } 875 876 // the open/close button when present, the edit button otherwise 877 row.getFocus().child_id = row.children.keys()[button_in_row_index]; 878 } 879 880 if (description_page) { 881 // the back link, in the title slot so the pane's input handling 882 // applies to it unchanged. 883 var tb = try wgt.TextBox(ui.Widget).init(allocator, "← back to issue", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 884 errdefer tb.deinit(allocator); 885 tb.getFocus().focusable = true; 886 tb.getFocus().kind = .{ .custom = try issuesLink(self.session.page_arena, self.data.identity, entry.issue.event.status, "", entry.id) }; 887 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 888 } else { 889 // the issue's title as a focusable word-wrapped text box. 890 { 891 var tb = try wgt.TextBox(ui.Widget).init(allocator, entry.issue.event.title, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = " title " }); 892 errdefer tb.deinit(allocator); 893 tb.getFocus().focusable = true; 894 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 895 } 896 897 self.author_id[index] = blk: { 898 var tb = try ui.authorBox(allocator, self.session.page_arena, entry.author); 899 errdefer tb.deinit(allocator); 900 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 901 break :blk tb.getFocus().id; 902 }; 903 904 // the issue's tags, each linking to this status's list filtered to 905 // that tag. 906 { 907 var items: std.ArrayList(ui.TagFlow.Item) = .empty; 908 defer items.deinit(allocator); 909 var tag_iter = evt.Issue.tagIterator(entry.issue.event.tags); 910 while (tag_iter.next()) |tag| { 911 if (tag.len == 0) continue; 912 try items.append(allocator, .{ .text = tag, .link = try tagLink(self.session.page_arena, self.data.identity, splitStatus(index), tag) }); 913 } 914 if (items.items.len > 0) { 915 var tf = try ui.TagFlow.init(allocator); 916 errdefer tf.deinit(allocator); 917 try tf.setItems(allocator, items.items); 918 try inner.children.put(allocator, tf.getFocus().id, .{ .widget = .{ .tag_flow = tf }, .rect = null, .min_size = null }); 919 } 920 } 921 } 922 923 // the description as a focusable word-wrapped text box. past the limit 924 // it's cut at the last whole line and links to the /description page, 925 // which shows the whole thing. 926 self.description_id[index] = blk: { 927 const whole = entry.issue.event.description; 928 const cut_short = !description_page and whole.len > max_description_size; 929 const shown = if (cut_short) 930 std.mem.trimEnd(u8, whole[0 .. std.mem.lastIndexOfScalar(u8, whole[0..max_description_size], '\n') orelse 0], " \t\r\n") 931 else if (whole.len == 0) 932 "(no description)" 933 else 934 whole; 935 var tb = try wgt.TextBox(ui.Widget).init(allocator, shown, .{ 936 .border_style = .single, 937 .rounded_corners = true, 938 .wrap_kind = .word, 939 .label = " description ", 940 .bottom_label = if (cut_short) " click or press enter to see more " else "", 941 }); 942 errdefer tb.deinit(allocator); 943 tb.getFocus().focusable = true; 944 if (cut_short) tb.getFocus().kind = .{ .custom = try descriptionLink(self.session.page_arena, self.data.identity, entry.id) }; 945 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 946 break :blk tb.getFocus().id; 947 }; 948 949 // select the title by default 950 inner.getFocus().child_id = inner.children.keys()[title_child_index]; 951 952 // reset the scroll to the top for the newly-shown issue: directly on the 953 // terminal (the wasm offset), and via a version bump on the web (so the 954 // renderer's scroll id changes and JS drops the preserved position). 955 const sc = self.detailScroll(index); 956 sc.x = 0; 957 sc.y = 0; 958 sc.getFocus().version +%= 1; 959 } 960 961 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 962 if (self.headerActive()) { 963 // down from the tabs re-enters the stack if the selected view has 964 // something to focus; other keys move the tabs. 965 if (inp.vertDirection(key) == .down) { 966 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 967 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 968 // nothing focusable in the unauthorized view 969 .unauthorized => false, 970 else => true, 971 } else false; 972 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 973 } else { 974 try self.header().input(allocator, key, root_focus); 975 } 976 return; 977 } 978 if (self.tagsViewActive()) { 979 self.tagsViewInput(key, root_focus); 980 return; 981 } 982 if (self.formViewActive()) { 983 try self.formInput(allocator, key, root_focus); 984 return; 985 } 986 const i = self.selectedSplitIndex() orelse return; 987 if (self.detailActive(i)) { 988 try self.detailInput(allocator, i, key, root_focus); 989 } else { 990 try self.listInput(i, key, root_focus); 991 } 992 } 993 994 // arrow keys move the tag selection; up from the top row crosses to the 995 // header tabs. 996 fn tagsViewInput(self: *View, key: Key, root_focus: *Focus) void { 997 const tf = self.tagsView(); 998 const cid = tf.focus.child_id orelse return; 999 const cur = tf.indexOfFocusId(cid) orelse return; 1000 const count = tf.text_boxes.items.len; 1001 switch (key) { 1002 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 1003 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 1004 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 1005 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 1006 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 1007 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 1008 else => {}, 1009 } 1010 } 1011 1012 fn listInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1013 // up/down (and the scroll wheel) move the selection a row; page up/down 1014 // jump a fixed amount. right/Enter cross into the detail pane. up from 1015 // the top row crosses into the header tabs. 1016 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 1017 const lb = self.listBox(index); 1018 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 1019 if (delta < 0 and at_top) return self.focusHeader(root_focus); 1020 ui.moveRowFocus(lb, self.listScroll(index), root_focus, delta); 1021 return; 1022 } 1023 switch (key) { 1024 .enter, .arrow_right => try self.focusDetail(index, root_focus), 1025 else => {}, 1026 } 1027 } 1028 1029 fn detailInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1030 if (self.toolRowFocused(index)) { 1031 try self.toolRowInput(allocator, index, key, root_focus); 1032 } else if (self.titleFocused(index)) { 1033 try self.titleInput(index, key, root_focus); 1034 } else if (self.authorFocused(index)) { 1035 try self.authorInput(index, key, root_focus); 1036 } else if (self.tagsFocused(index)) { 1037 try self.tagsInput(index, key, root_focus); 1038 } else { 1039 try self.descriptionInput(index, key, root_focus); 1040 } 1041 } 1042 1043 // the tool row: enter or a click on the open/close button flips the 1044 // issue's status; the edit button is an a: link the host follows. 1045 // arrows cross between the buttons and to the neighboring widgets. 1046 fn toolRowInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1047 const row = self.toolRow(index); 1048 // the edit button is always the row's last child; the open/close 1049 // button before it is only there when the session may author events 1050 const edit_index = row.children.count() - 1; 1051 const on_edit = if (row.getFocus().child_id) |cid| row.children.getIndex(cid) == edit_index else false; 1052 switch (key) { 1053 .arrow_left => if (on_edit and self.statusButton(index) != null) root_focus.setFocus(row.children.keys()[button_in_row_index]) else try self.focusList(index, root_focus), 1054 .arrow_right => if (!on_edit) root_focus.setFocus(row.children.keys()[edit_index]), 1055 .arrow_up => self.focusHeader(root_focus), 1056 .arrow_down => self.focusTitle(index, root_focus), 1057 .enter => if (!on_edit) try self.toggleIssueStatus(allocator, index), 1058 .mouse => |mouse| switch (mouse.action) { 1059 .scroll => |dir| { 1060 const sc = self.detailScroll(index); 1061 sc.y += if (dir == .up) @as(isize, -1) else 1; 1062 sc.clampToContent(); 1063 }, 1064 else => if (self.statusButton(index)) |button| { 1065 if (inp.leftClickOn(root_focus, button.getFocus().id, mouse)) try self.toggleIssueStatus(allocator, index); 1066 }, 1067 }, 1068 else => {}, 1069 } 1070 } 1071 1072 fn titleInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1073 const sc = self.detailScroll(index); 1074 switch (key) { 1075 .arrow_left => try self.focusList(index, root_focus), 1076 .arrow_up => self.focusToolRow(index, root_focus), 1077 .arrow_down => if (self.authorPresent(index)) self.focusAuthor(index, root_focus) else try self.focusDescription(index, root_focus), 1078 .mouse => |mouse| switch (mouse.action) { 1079 .scroll => |dir| { 1080 sc.y += if (dir == .up) @as(isize, -1) else 1; 1081 sc.clampToContent(); 1082 }, 1083 else => {}, 1084 }, 1085 else => {}, 1086 } 1087 } 1088 1089 // the author box's a: link (when it has one) is followed by the host on 1090 // enter / a click; arrows cross to the neighboring widgets. 1091 fn authorInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1092 const sc = self.detailScroll(index); 1093 switch (key) { 1094 .arrow_left => try self.focusList(index, root_focus), 1095 .arrow_up => self.focusTitle(index, root_focus), 1096 .arrow_down => if (self.tagFlow(index) != null) try self.focusTags(index, root_focus) else try self.focusDescription(index, root_focus), 1097 .mouse => |mouse| switch (mouse.action) { 1098 .scroll => |dir| { 1099 sc.y += if (dir == .up) @as(isize, -1) else 1; 1100 sc.clampToContent(); 1101 }, 1102 else => {}, 1103 }, 1104 else => {}, 1105 } 1106 } 1107 1108 fn descriptionInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1109 const sc = self.detailScroll(index); 1110 switch (key) { 1111 .arrow_left => return self.focusList(index, root_focus), 1112 // once the scroll can't move further, cross into the tags (or the 1113 // open/close button when the issue has none). 1114 .arrow_up => { 1115 const before = sc.y; 1116 sc.y -= 1; 1117 sc.clampToContent(); 1118 if (sc.y == before) { 1119 if (self.tagFlow(index) != null) 1120 try self.focusTags(index, root_focus) 1121 else if (self.authorPresent(index)) 1122 self.focusAuthor(index, root_focus) 1123 else 1124 self.focusTitle(index, root_focus); 1125 } 1126 return; 1127 }, 1128 .arrow_down => sc.y += 1, 1129 .page_up => sc.y -= 10, 1130 .page_down => sc.y += 10, 1131 .home => sc.y = 0, 1132 .end => sc.y = std.math.maxInt(isize), 1133 .mouse => |mouse| switch (mouse.action) { 1134 .scroll => |dir| sc.y += if (dir == .up) @as(isize, -1) else 1, 1135 else => {}, 1136 }, 1137 else => return, 1138 } 1139 sc.clampToContent(); 1140 } 1141 1142 // up/down (and tab/shift+tab) move between a form's fields; up from the 1143 // title crosses into the header tabs. the multiline description keeps 1144 // enter and any up/down that has a row to move to. 1145 fn formInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1146 const form = self.issueForm() orelse return; 1147 const cid = form.getFocus().child_id orelse return; 1148 const cur = form.children.getIndex(cid) orelse return; 1149 1150 if (cur == description_field_index) { 1151 const child = &form.children.values()[cur]; 1152 const description = &child.widget.text_input; 1153 switch (key) { 1154 .enter => return child.widget.input(allocator, key, root_focus), 1155 .arrow_up => if (!try description.cursorOnFirstRow(allocator)) 1156 return child.widget.input(allocator, key, root_focus), 1157 .arrow_down => if (!try description.cursorOnLastRow(allocator)) 1158 return child.widget.input(allocator, key, root_focus), 1159 else => {}, 1160 } 1161 } 1162 1163 switch (key) { 1164 .arrow_up, .back_tab => if (cur > 0) 1165 root_focus.setFocus(form.children.keys()[cur - 1]) 1166 else 1167 self.focusHeader(root_focus), 1168 .arrow_down, .tab => if (cur < submit_field_index) { 1169 root_focus.setFocus(form.children.keys()[cur + 1]); 1170 }, 1171 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 1172 .mouse => |mouse| if (cur == submit_field_index and inp.leftClickOn(root_focus, cid, mouse)) { 1173 try self.submitForm(allocator); 1174 }, 1175 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 1176 } 1177 } 1178 1179 // the terminal submit paths: the new form commits a new-issue event, the 1180 // edit form re-emits the selected issue's event with the form's content. 1181 // the web posts the forms to their routes instead. 1182 fn submitForm(self: *View, allocator: std.mem.Allocator) !void { 1183 if (self.data.view == .edit) { 1184 try self.submitEditedIssue(allocator); 1185 } else { 1186 try self.submitNewIssue(allocator); 1187 } 1188 } 1189 1190 // commit the new issue to the repo's events branch and navigate to it. 1191 // this is the terminal path; the web posts the form to the issue route, 1192 // so the wasm side never runs (or compiles) the repo access below. 1193 fn submitNewIssue(self: *View, allocator: std.mem.Allocator) !void { 1194 if (comptime wasm) return; 1195 const io = self.session.io orelse return; 1196 const src = self.data.repo_source orelse return; 1197 const author_email = (try self.session.eventAuthorEmail()) orelse return; 1198 1199 const form = self.issueForm() orelse return; 1200 const title_input = &form.children.values()[title_field_index].widget.text_input; 1201 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 1202 const description_input = &form.children.values()[description_field_index].widget.text_input; 1203 1204 const title = try title_input.text(allocator); 1205 defer allocator.free(title); 1206 const tags = try tags_input.text(allocator); 1207 defer allocator.free(tags); 1208 const description = try description_input.text(allocator); 1209 defer allocator.free(description); 1210 1211 if (!evt.Issue.fieldsValid(title, tags)) return; 1212 1213 var id_bytes: [evt.event_id_size]u8 = undefined; 1214 io.random(&id_bytes); 1215 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 1216 1217 const event = evt.EventWithId{ 1218 .id = event_id_hex, 1219 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 1220 .author_email = author_email, 1221 .event = .{ .issue = .{ 1222 .title = title, 1223 .description = description, 1224 .tags = tags, 1225 } }, 1226 }; 1227 1228 switch (src.repo_kind) { 1229 inline else => |repo_kind| { 1230 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1231 defer any_repo.deinit(io, allocator); 1232 switch (any_repo) { 1233 inline else => |*repo| try evt.consume(repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 1234 } 1235 }, 1236 } 1237 1238 // wipe the form so a return visit starts fresh 1239 title_input.clear(allocator); 1240 tags_input.clear(allocator); 1241 description_input.clear(allocator); 1242 1243 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, "", &event_id_hex) orelse return; 1244 try self.session.navigate(route); 1245 } 1246 1247 // re-emit the selected issue's event with the form's content (status 1248 // preserved), then reload the issue's page. this is the terminal path; 1249 // the web posts the form to the edit route. 1250 fn submitEditedIssue(self: *View, allocator: std.mem.Allocator) !void { 1251 if (comptime wasm) return; 1252 const io = self.session.io orelse return; 1253 const src = self.data.repo_source orelse return; 1254 const entry = self.data.selectedIssue() orelse return; 1255 const author_email = (try self.session.eventAuthorEmail()) orelse return; 1256 1257 const form = self.issueForm() orelse return; 1258 const title_input = &form.children.values()[title_field_index].widget.text_input; 1259 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 1260 const description_input = &form.children.values()[description_field_index].widget.text_input; 1261 1262 const title = try title_input.text(allocator); 1263 defer allocator.free(title); 1264 const tags = try tags_input.text(allocator); 1265 defer allocator.free(tags); 1266 const description = try description_input.text(allocator); 1267 defer allocator.free(description); 1268 1269 if (!evt.Issue.fieldsValid(title, tags)) return; 1270 1271 var id_bytes: [evt.event_id_size]u8 = undefined; 1272 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 1273 1274 switch (src.repo_kind) { 1275 inline else => |repo_kind| { 1276 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1277 defer any_repo.deinit(io, allocator); 1278 switch (any_repo) { 1279 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 1280 .title = title, 1281 .tags = tags, 1282 .description = description, 1283 } }, author_email), 1284 } 1285 }, 1286 } 1287 1288 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 1289 try self.session.navigate(route); 1290 } 1291 1292 // flip the shown issue's status by re-emitting its event, then reload the 1293 // page rooted at the issue so the view reflects the change. this is the 1294 // terminal path; the web posts the button's form to the status route. 1295 fn toggleIssueStatus(self: *View, allocator: std.mem.Allocator, index: usize) !void { 1296 if (comptime wasm) return; 1297 const io = self.session.io orelse return; 1298 const src = self.data.repo_source orelse return; 1299 const sel = self.detailed_index[index] orelse return; 1300 const entry = self.window(index).issues[sel]; 1301 const author_email = (try self.session.eventAuthorEmail()) orelse return; 1302 1303 const status: evt.Issue.Status = switch (entry.issue.event.status) { 1304 .open => .closed, 1305 .closed => .open, 1306 }; 1307 1308 var id_bytes: [evt.event_id_size]u8 = undefined; 1309 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 1310 1311 switch (src.repo_kind) { 1312 inline else => |repo_kind| { 1313 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1314 defer any_repo.deinit(io, allocator); 1315 switch (any_repo) { 1316 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .status = status }, author_email), 1317 } 1318 }, 1319 } 1320 1321 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, status, "", entry.id) orelse return; 1322 try self.session.navigate(route); 1323 } 1324 1325 // arrow keys move the tag selection; at the flow's edges focus crosses to 1326 // the neighboring widgets. 1327 fn tagsInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1328 const tf = self.tagFlow(index) orelse return; 1329 const cid = tf.focus.child_id orelse return; 1330 const cur = tf.indexOfFocusId(cid) orelse return; 1331 const count = tf.text_boxes.items.len; 1332 const sc = self.detailScroll(index); 1333 switch (key) { 1334 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 1335 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 1336 .arrow_up => if (tf.rowStep(cur, false)) |i| self.focusTag(index, tf, root_focus, i) else self.focusAuthor(index, root_focus), 1337 .arrow_down => if (tf.rowStep(cur, true)) |i| self.focusTag(index, tf, root_focus, i) else try self.focusDescription(index, root_focus), 1338 .home => self.focusTag(index, tf, root_focus, 0), 1339 .end => self.focusTag(index, tf, root_focus, count - 1), 1340 .mouse => |mouse| switch (mouse.action) { 1341 .scroll => |dir| { 1342 sc.y += if (dir == .up) @as(isize, -1) else 1; 1343 sc.clampToContent(); 1344 }, 1345 else => {}, 1346 }, 1347 else => {}, 1348 } 1349 } 1350 1351 const button_in_row_index: usize = 1; 1352 const title_child_index: usize = 0; 1353 const tags_child_index: usize = 2; 1354 1355 fn tagFlow(self: *View, index: usize) ?*ui.TagFlow { 1356 const inner = self.detailInner(index); 1357 if (inner.children.count() <= tags_child_index) return null; 1358 return switch (inner.children.values()[tags_child_index].widget) { 1359 .tag_flow => |*tf| tf, 1360 else => null, 1361 }; 1362 } 1363 1364 fn tagsFocused(self: *View, index: usize) bool { 1365 const inner = self.detailInner(index); 1366 const cid = inner.getFocus().child_id orelse return false; 1367 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 1368 } 1369 1370 // the open/close button inside the detail frame's tool row. the full row 1371 // is spacer + open/close + edit; a logged-out session's row has no 1372 // open/close button, and the description page has no row at all. 1373 fn statusButton(self: *View, index: usize) ?*wgt.TextBox(ui.Widget) { 1374 const row = self.toolRow(index); 1375 if (row.children.count() != 3) return null; 1376 return &row.children.values()[button_in_row_index].widget.text_box; 1377 } 1378 1379 fn toolRowFocused(self: *View, index: usize) bool { 1380 const outer = self.detailOuter(index); 1381 const cid = outer.getFocus().child_id orelse return false; 1382 return outer.children.getIndex(cid) == tool_row_index; 1383 } 1384 1385 fn focusTag(self: *View, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 1386 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 1387 // keep the tag visible on the terminal: its rect offset by the flow's 1388 // position in the pane. 1389 if (self.session.is_terminal and item < tf.rects.items.len) { 1390 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 1391 var rect = tf.rects.items[item]; 1392 rect.x += flow_rect.x; 1393 rect.y += flow_rect.y; 1394 self.detailScroll(index).scrollToRect(rect); 1395 } 1396 } 1397 } 1398 1399 fn titleFocused(self: *View, index: usize) bool { 1400 const inner = self.detailInner(index); 1401 const cid = inner.getFocus().child_id orelse return false; 1402 return inner.children.getIndex(cid) == title_child_index; 1403 } 1404 1405 fn authorPresent(self: *View, index: usize) bool { 1406 return self.author_id[index] != null; 1407 } 1408 1409 fn authorFocused(self: *View, index: usize) bool { 1410 const inner = self.detailInner(index); 1411 const cid = inner.getFocus().child_id orelse return false; 1412 return cid == self.author_id[index]; 1413 } 1414 1415 // the author sits right under the title at the top of the pane, so 1416 // focusing it scrolls there. 1417 fn focusAuthor(self: *View, index: usize, root_focus: *Focus) void { 1418 const id = self.author_id[index] orelse return; 1419 root_focus.setFocus(id); 1420 self.detailScroll(index).y = 0; 1421 } 1422 1423 // the title sits at the top of the pane, so focusing it scrolls there. 1424 fn focusTitle(self: *View, index: usize, root_focus: *Focus) void { 1425 const inner = self.detailInner(index); 1426 if (inner.children.count() == 0) return; 1427 root_focus.setFocus(inner.children.keys()[title_child_index]); 1428 self.detailScroll(index).y = 0; 1429 } 1430 1431 fn focusTags(self: *View, index: usize, root_focus: *Focus) !void { 1432 const tf = self.tagFlow(index) orelse return; 1433 if (tf.text_boxes.items.len == 0) return; 1434 const cid = tf.focus.child_id orelse tf.text_boxes.items[0].getFocus().id; 1435 const item = tf.indexOfFocusId(cid) orelse 0; 1436 self.focusTag(index, tf, root_focus, item); 1437 } 1438 1439 fn focusDescription(self: *View, index: usize, root_focus: *Focus) !void { 1440 if (self.description_id[index]) |id| root_focus.setFocus(id); 1441 } 1442 1443 // return to the tool row's last-focused button (the header when the 1444 // description page shows no row). 1445 fn focusToolRow(self: *View, index: usize, root_focus: *Focus) void { 1446 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 1447 root_focus.setFocus(cid); 1448 } 1449 1450 // enter the detail pane. an empty pane (no issues) can't be entered. 1451 fn focusDetail(self: *View, index: usize, root_focus: *Focus) !void { 1452 if (self.detailInner(index).children.count() == 0) return; 1453 root_focus.setFocus(self.detailOuter(index).getFocus().id); 1454 } 1455 1456 // return to the list. 1457 fn focusList(self: *View, index: usize, root_focus: *Focus) !void { 1458 root_focus.setFocus(self.listScroll(index).getFocus().id); 1459 } 1460 1461 // cross to the header tabs above the stack. 1462 fn focusHeader(self: *View, root_focus: *Focus) void { 1463 root_focus.setFocus(self.box.children.keys()[header_index]); 1464 } 1465 1466 pub fn clearGrid(self: *View) void { 1467 self.box.clearGrid(); 1468 } 1469 1470 pub fn getGrid(self: View) ?Grid { 1471 return self.box.getGrid(); 1472 } 1473 1474 pub fn getFocus(self: *View) *Focus { 1475 return self.box.getFocus(); 1476 } 1477 1478 // for the parent's "scroll up at the top jumps to the header" check: at the 1479 // top only while the header tabs hold focus, so up from the split 1480 // crosses into the tabs first. 1481 pub fn getSelectedIndex(self: *View) ?usize { 1482 return if (self.headerActive()) 0 else 1; 1483 } 1484 }; 1485 1486 // the "a:" navigation link for `status`'s issues page filtered to the 1487 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 1488 // a rooted url ignores `status` (it derives its view from the issue's own); 1489 // with an empty `id` this is the bare list link. 1490 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 1491 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 1492 const url = try route.toUrl(page_arena); 1493 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1494 } 1495 1496 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 1497 // href is only followed with js off. 1498 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 1499 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 1500 const url = try route.toUrl(page_arena); 1501 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 1502 } 1503 1504 // the "a:" link to the whole-description page of issue `id` within `identity`. 1505 fn descriptionLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 1506 const route = ui.RoutablePage.repoIssuesDescriptionRoute(identity, id) orelse return error.RouteTooLong; 1507 const url = try route.toUrl(page_arena); 1508 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1509 } 1510 1511 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 1512 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 1513 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 1514 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 1515 const url = try route.toUrl(page_arena); 1516 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1517 } 1518 1519 // tabs switching between the issues page's views. 1520 pub const Header = struct { 1521 box: wgt.Box(ui.Widget), 1522 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 1523 1524 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 1525 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1526 errdefer box.deinit(allocator); 1527 1528 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 1529 errdefer tab_ids.deinit(allocator); 1530 1531 const aa = session.page_arena.allocator(); 1532 1533 // a list tab per status, labeled with its listing's issue count 1534 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 1535 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 1536 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 1537 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 1538 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1539 errdefer text_box.deinit(allocator); 1540 text_box.getFocus().focusable = true; 1541 text_box.getFocus().kind = .{ .custom = link }; 1542 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1543 try box.children.put(allocator, text_box.getFocus().id, .{ 1544 .widget = .{ .text_box = text_box }, 1545 .rect = null, 1546 .min_size = .{ .width = label.len + 2, .height = null }, 1547 }); 1548 } 1549 1550 // tags tab, labeled with the active tag filter 1551 { 1552 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 1553 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 1554 const label = if (data.tag.len == 0) "tags" else blk: { 1555 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 1556 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 1557 }; 1558 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1559 errdefer text_box.deinit(allocator); 1560 text_box.getFocus().focusable = true; 1561 text_box.getFocus().kind = .{ .custom = tags_link }; 1562 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1563 try box.children.put(allocator, text_box.getFocus().id, .{ 1564 .widget = .{ .text_box = text_box }, 1565 .rect = null, 1566 .min_size = .{ .width = label.len + 2, .height = null }, 1567 }); 1568 } 1569 1570 // new-issue tab; an edit url shows the edit tab in its place 1571 { 1572 const route = if (data.view == .edit) 1573 ui.RoutablePage.repoIssuesEditRoute(data.identity, data.selected_id) orelse return error.RouteTooLong 1574 else 1575 ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong; 1576 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 1577 const label: []const u8 = if (data.view == .edit) "edit" else "new"; 1578 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1579 errdefer text_box.deinit(allocator); 1580 text_box.getFocus().focusable = true; 1581 text_box.getFocus().kind = .{ .custom = link }; 1582 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1583 try box.children.put(allocator, text_box.getFocus().id, .{ 1584 .widget = .{ .text_box = text_box }, 1585 .rect = null, 1586 .min_size = .{ .width = label.len + 2, .height = null }, 1587 }); 1588 } 1589 1590 var self = Header{ .box = box, .tab_ids = tab_ids }; 1591 // the tab matching the page's view is selected initially. 1592 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 1593 return self; 1594 } 1595 1596 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 1597 self.box.deinit(allocator); 1598 self.tab_ids.deinit(allocator); 1599 } 1600 1601 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1602 self.clearGrid(); 1603 // only the selected tab shows its border 1604 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 1605 switch (child.widget) { 1606 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 1607 else => {}, 1608 } 1609 } 1610 try self.box.build(allocator, constraint, root_focus); 1611 } 1612 1613 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1614 _ = allocator; 1615 const current_tab = self.currentTabIndex() orelse return; 1616 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 1617 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 1618 } 1619 } 1620 1621 pub fn clearGrid(self: *Header) void { 1622 self.box.clearGrid(); 1623 } 1624 1625 pub fn getGrid(self: Header) ?Grid { 1626 return self.box.getGrid(); 1627 } 1628 1629 pub fn getFocus(self: *Header) *Focus { 1630 return self.box.getFocus(); 1631 } 1632 1633 pub fn getSelectedIndex(self: Header) ?usize { 1634 return self.currentTabIndex(); 1635 } 1636 1637 fn currentTabIndex(self: Header) ?usize { 1638 const child_id = self.box.focus.child_id orelse return null; 1639 return self.tab_ids.getIndex(child_id); 1640 } 1641 }; 1642