╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Threads.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
671 .user_name, .email => |name| try std.fmt.allocPrint(aa, " edited by {s} ", .{name}), 672 .unknown => if (ours) " current version " else " their version ", 673 }; 674 } 675 676 // the a: link that reloads the resolve page with `name` picked from ours 677 // or theirs, reprefilling its resolution input 678 fn useThisLink(session: *ui.Session, data: *const Self, name: []const u8, theirs: bool) ![]const u8 { 679 const aa = session.page_arena.allocator(); 680 var picks: std.ArrayList(u8) = .empty; 681 var it = std.mem.splitScalar(u8, data.theirs_picks, ','); 682 while (it.next()) |pick| { 683 if (pick.len == 0 or std.mem.eql(u8, pick, name)) continue; 684 if (picks.items.len > 0) try picks.append(aa, ','); 685 try picks.appendSlice(aa, pick); 686 } 687 if (theirs) { 688 if (picks.items.len > 0) try picks.append(aa, ','); 689 try picks.appendSlice(aa, name); 690 } 691 const route = resolveRoute(data.identity, data.selected_id, picks.items) orelse return error.RouteTooLong; 692 return std.fmt.allocPrint(aa, "a:{s}", .{try route.toUrl(session.page_arena)}); 693 } 694 695 // the resolve form's page-constant initial value for the input `name`: 696 // the picked side of its conflicted field or hunk 697 fn resolvePrefill(self: *This, name: []const u8) ?[]const u8 { 698 const conflict = if (self.data.conflict) |*c| c else return null; 699 const theirs = self.data.theirsPicked(name); 700 if (std.mem.eql(u8, name, "title")) { 701 const fc = if (conflict.title) |*f| f else return null; 702 return if (theirs) fc.theirs.text else fc.ours.text; 703 } 704 if (std.mem.eql(u8, name, "tags")) { 705 const fc = if (conflict.tags) |*f| f else return null; 706 return if (theirs) fc.theirs.text else fc.ours.text; 707 } 708 if (std.mem.startsWith(u8, name, "d")) { 709 const desc = if (conflict.description) |*d| d else return null; 710 const hunk_index = std.fmt.parseInt(usize, name[1..], 10) catch return null; 711 var seen: usize = 0; 712 for (desc.chunks) |chunk| switch (chunk) { 713 .conflict => |hunk| { 714 if (seen == hunk_index) return (if (theirs) hunk.theirs else hunk.ours) orelse ""; 715 seen += 1; 716 }, 717 else => {}, 718 }; 719 } 720 return null; 721 } 722 723 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), text: []const u8, bottom_label: []const u8, link: []const u8) !void { 724 var row = try wgt.TextBox(ui.Widget).init(allocator, text, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word, .bottom_label = bottom_label }); 725 errdefer row.deinit(allocator); 726 row.getFocus().focusable = true; 727 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 728 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 729 } 730 731 pub fn deinit(self: *This, allocator: std.mem.Allocator) void { 732 self.box.deinit(allocator); 733 } 734 735 fn header(self: *This) *Header { 736 return switch (kind) { 737 .issue => &self.box.children.values()[header_index].widget.repo_issues_header, 738 .discuss => &self.box.children.values()[header_index].widget.repo_discussions_header, 739 else => @compileError("unsupported thread event kind"), 740 }; 741 } 742 743 fn viewStack(self: *This) *wgt.Stack(ui.Widget) { 744 return &self.box.children.values()[stack_index].widget.stack; 745 } 746 747 // `index`'s master-detail split inside the stack. 748 fn resultsBox(self: *This, index: usize) *wgt.Box(ui.Widget) { 749 return &self.viewStack().children.values()[index].box; 750 } 751 752 // the tags view's flow inside the stack. 753 fn tagsView(self: *This) *ui.TagFlow { 754 return &self.viewStack().children.values()[tags_view_index].tag_flow; 755 } 756 757 // the new-issue, edit, or resolve form inside the stack, or null when the 758 // unauthorized view stands in for it. 759 fn threadForm(self: *This) ?*wgt.Box(ui.Widget) { 760 return switch (self.viewStack().children.values()[form_view_index]) { 761 .box => |*box| box, 762 // the resolve form sits inside a scroll on the terminal 763 .scroll => |*scroll| &scroll.child.box, 764 else => null, 765 }; 766 } 767 768 fn removeForm(self: *This) ?*wgt.Box(ui.Widget) { 769 return switch (self.viewStack().children.values()[form_view_index]) { 770 .center => |*center| switch (center.child.*) { 771 .box => |*box| box, 772 else => null, 773 }, 774 else => null, 775 }; 776 } 777 778 // the resolve form's scroll on the terminal (the web page scrolls itself) 779 fn resolveScroll(self: *This) ?*wgt.Scroll(ui.Widget) { 780 return switch (self.viewStack().children.values()[form_view_index]) { 781 .scroll => |*scroll| scroll, 782 else => null, 783 }; 784 } 785 786 fn listScroll(self: *This, index: usize) *wgt.Scroll(ui.Widget) { 787 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 788 } 789 790 fn listBox(self: *This, index: usize) *wgt.Box(ui.Widget) { 791 return &self.listScroll(index).child.box; 792 } 793 794 // the detail frame's children: the tool row above the scroll pane. 795 const tool_row_index: usize = 0; 796 const detail_scroll_index: usize = 1; 797 798 fn detailOuter(self: *This, index: usize) *wgt.Box(ui.Widget) { 799 return &self.resultsBox(index).children.values()[detail_index].widget.box; 800 } 801 802 fn toolRow(self: *This, index: usize) *wgt.Box(ui.Widget) { 803 return &self.detailOuter(index).children.values()[tool_row_index].widget.box; 804 } 805 806 fn detailScroll(self: *This, index: usize) *wgt.Scroll(ui.Widget) { 807 return &self.detailOuter(index).children.values()[detail_scroll_index].widget.scroll; 808 } 809 810 fn detailInner(self: *This, index: usize) *wgt.Box(ui.Widget) { 811 return &self.detailScroll(index).child.box; 812 } 813 814 fn window(self: *This, index: usize) *const Window { 815 if (has_conflicts and index == conflict_view_index) return &self.data.conflicts; 816 return self.data.window(splitStatus(index)); 817 } 818 819 fn stackSelectedIndex(self: *This) ?usize { 820 const stack = self.viewStack(); 821 const cid = stack.getFocus().child_id orelse return null; 822 return stack.children.getIndex(cid); 823 } 824 825 // the stack's selected master-detail split (null when the tags view or the 826 // new-issue form shows). 827 fn selectedSplitIndex(self: *This) ?usize { 828 const idx = self.stackSelectedIndex() orelse return null; 829 return if (idx < status_count or idx == conflict_view_index) idx else null; 830 } 831 832 fn detailActive(self: *This, index: usize) bool { 833 const rb = self.resultsBox(index); 834 const cid = rb.getFocus().child_id orelse return false; 835 return rb.children.getIndex(cid) == detail_index; 836 } 837 838 fn headerActive(self: *This) bool { 839 const cid = self.box.getFocus().child_id orelse return false; 840 return self.box.children.getIndex(cid) == header_index; 841 } 842 843 fn tagsViewActive(self: *This) bool { 844 if (self.headerActive()) return false; 845 return self.stackSelectedIndex() == tags_view_index; 846 } 847 848 fn formViewActive(self: *This) bool { 849 if (self.headerActive()) return false; 850 return self.stackSelectedIndex() == form_view_index; 851 } 852 853 // the selected issue's index, or null when a window-navigation row is 854 // selected (a leading "previous" row shifts the issue rows down by one). 855 fn selectedThreadIndex(self: *This, index: usize) ?usize { 856 const lb = self.listBox(index); 857 const cid = lb.getFocus().child_id orelse return null; 858 const idx = lb.children.getIndex(cid) orelse return null; 859 const win = self.window(index); 860 const lead: usize = if (win.prev_id != null) 1 else 0; 861 if (idx < lead or idx - lead >= win.items.len) return null; 862 return idx - lead; 863 } 864 865 pub fn build(self: *This, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 866 self.clearGrid(); 867 868 // each header tab maps 1:1 to a stack child by position. mirror the 869 // selection into the url: the view the page's window is rooted at 870 // keeps its rooted url, the others get their list route (keeping the 871 // tag filter). 872 if (self.header().getSelectedIndex()) |index| { 873 const stack = self.viewStack(); 874 stack.getFocus().child_id = stack.children.keys()[index]; 875 self.session.data.current_page = (if (index == form_view_index) 876 (if (has_conflicts) switch (self.data.view) { 877 .edit => editRoute(self.data.identity, self.data.selected_id), 878 .new_comment => newCommentRoute(self.data.identity, self.data.selected_id, self.data.comment_id), 879 .edit_comment => editCommentRoute(self.data.identity, self.data.selected_id, self.data.comment_id), 880 .remove => removeRoute(self.data.identity, self.data.selected_id, self.data.comment_id), 881 .resolve => resolveRoute(self.data.identity, self.data.selected_id, self.data.theirs_picks), 882 else => newRoute(self.data.identity), 883 } else switch (self.data.view) { 884 .edit => editRoute(self.data.identity, self.data.selected_id), 885 .new_comment => newCommentRoute(self.data.identity, self.data.selected_id, self.data.comment_id), 886 .edit_comment => editCommentRoute(self.data.identity, self.data.selected_id, self.data.comment_id), 887 .remove => removeRoute(self.data.identity, self.data.selected_id, self.data.comment_id), 888 else => newRoute(self.data.identity), 889 }) 890 else if (has_conflicts and index == conflict_view_index) 891 conflictsRoute(self.data.identity, if (self.data.view == .conflicts) self.data.selected_id else "") 892 else if (index == viewIndex(self.data.view) and self.data.selected_id.len != 0) 893 (if (self.data.description_page) 894 descriptionRoute(self.data.identity, self.data.selected_id) 895 else if (self.data.comment_page) |page| 896 commentRoute(self.data.identity, self.data.selected_id, &page.selected.id, page.replies.start) 897 else 898 commentsRoute(self.data.identity, self.data.selected_id, self.data.comments_start)) 899 else if (index == tags_view_index) 900 tagsRoute(self.data.identity, self.data.tag) 901 else 902 listRoute(self.data.identity, splitStatus(index), self.data.tag, "")) orelse self.session.data.current_page; 903 } 904 905 if (self.selectedSplitIndex()) |i| { 906 // swap the detail pane to the selected issue when it changes. 907 try self.refreshDetail(allocator, i); 908 909 // mirror the focused issue into the url, but only while focus is 910 // inside the split. an issue's url is the same whether or not the 911 // list is filtered, so the mirror drops the tag. 912 if (root_focus.grandchild_id) |g| { 913 if (self.resultsBox(i).getFocus().children.contains(g)) { 914 if (self.selectedThreadIndex(i)) |sel| { 915 // selecting another issue leaves the description page 916 // behind, like it leaves the tag filter behind. 917 const entry = &self.window(i).items[sel]; 918 const mirrored = if (self.data.description_page and std.mem.eql(u8, entry.id, self.data.selected_id)) 919 descriptionRoute(self.data.identity, entry.id) 920 else if (self.data.comment_page) |page| 921 if (std.mem.eql(u8, entry.id, self.data.selected_id)) 922 commentRoute(self.data.identity, entry.id, &page.selected.id, page.replies.start) 923 else 924 commentsRoute(self.data.identity, entry.id, 0) 925 else if (std.mem.eql(u8, entry.id, self.data.selected_id)) 926 commentsRoute(self.data.identity, entry.id, entry.comments.start) 927 else 928 commentsRoute(self.data.identity, entry.id, 0); 929 if (mirrored) |route| self.session.data.current_page = route; 930 } 931 } 932 } 933 934 // the selected list row shows a border (the focused TextBox 935 // upgrades it to a double border itself); the rest stay borderless. 936 const lb = self.listBox(i); 937 for (lb.children.keys(), lb.children.values()) |id, *child| { 938 switch (child.widget) { 939 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 940 else => {}, 941 } 942 } 943 944 // the pane's selected child shows the selection border: the 945 // description directly, the tags via the flow's selected item. 946 // cap the list at list_max_width only while the detail pane fits 947 // beside it. the box drops the detail when the width can't hold 948 // both minimums, so when it's that narrow we lift the cap and let 949 // the list fill the whole width. 950 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 951 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 952 953 // stretch the detail pane across the rest of the width so it fills 954 // the area rather than shrinking to its content; its scroll fills 955 // the pane. 956 if (constraint.max_size.width) |w| { 957 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 958 } else { 959 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 960 } 961 } 962 963 // refresh the form inputs' entries in the session's focus-id -> input 964 // map with this frame's addresses, so the web/wasm form handling can 965 // find them by focus id 966 if (self.threadForm()) |form| { 967 const inputs_arena = self.session.arena.allocator(); 968 for (form.children.values()) |*child| switch (child.widget) { 969 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 970 else => {}, 971 }; 972 973 // on an edit page, register the issue's content as the inputs' 974 // page-constant initial values, which the web overlay renders 975 // into them. 976 if (self.data.view == .edit) { 977 if (self.data.selectedThread()) |entry| { 978 const fields = form.children.values(); 979 try self.session.input_values.put(inputs_arena, fields[title_field_index].widget.text_input.getFocus().id, entry.record.event.title); 980 try self.session.input_values.put(inputs_arena, fields[tags_field_index].widget.text_input.getFocus().id, entry.record.event.tags); 981 try self.session.input_values.put(inputs_arena, fields[description_field_index].widget.text_input.getFocus().id, entry.record.event.description); 982 } 983 } 984 985 if (self.data.view == .edit_comment) { 986 if (self.data.comment_page) |page| { 987 const body = &form.children.values()[comment_body_field_index].widget.text_input; 988 try self.session.input_values.put(inputs_arena, body.getFocus().id, page.selected.comment.event.body); 989 } 990 } 991 992 // the resolve form's inputs prefill from the picked side, which 993 // is url state and so page-constant like the edit values above 994 if (has_conflicts and self.data.view == .resolve) { 995 for (form.children.values()) |*child| switch (child.widget) { 996 .text_input => |*ti| if (self.resolvePrefill(ti.options.name)) |value| { 997 try self.session.input_values.put(inputs_arena, ti.getFocus().id, value); 998 }, 999 else => {}, 1000 }; 1001 } 1002 } 1003 1004 try self.box.build(allocator, constraint, root_focus); 1005 } 1006 1007 fn refreshDetail(self: *This, allocator: std.mem.Allocator, index: usize) !void { 1008 const sel = self.selectedThreadIndex(index) orelse return; 1009 if (self.detailed_index[index]) |d| if (d == sel) return; 1010 try self.populateDetail(allocator, index, sel); 1011 self.detailed_index[index] = sel; 1012 } 1013 1014 fn populateDetail(self: *This, allocator: std.mem.Allocator, index: usize, sel: usize) !void { 1015 const entry = self.window(index).items[sel]; 1016 const inner = self.detailInner(index); 1017 // the /description page replaces its issue's detail with a back link 1018 // and the whole description; other issues keep their normal detail. 1019 const description_page = self.data.description_page and std.mem.eql(u8, entry.id, self.data.selected_id); 1020 const comment_page = if (std.mem.eql(u8, entry.id, self.data.selected_id)) self.data.comment_page else null; 1021 1022 for (inner.children.values()) |*child| child.widget.deinit(allocator); 1023 inner.children.clearAndFree(allocator); 1024 inner.getFocus().child_id = null; 1025 self.author_id[index] = null; 1026 self.status_button_id[index] = null; 1027 1028 // the tool row: the open/close and edit buttons; the description page 1029 // shows none. 1030 { 1031 const row = self.toolRow(index); 1032 for (row.children.values()) |*child| child.widget.deinit(allocator); 1033 row.children.clearAndFree(allocator); 1034 row.getFocus().child_id = null; 1035 row.getFocus().kind = .container; 1036 } 1037 if (!description_page and comment_page == null) { 1038 const row = self.toolRow(index); 1039 const pa = self.session.page_arena.allocator(); 1040 1041 { 1042 var spacer = try ui.Spacer.init(allocator); 1043 errdefer spacer.deinit(allocator); 1044 try row.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1045 } 1046 1047 // a terminal has no file picker, so attaching is web only. the 1048 // renderer covers this button with a file input, so clicking it 1049 // opens the browser's own picker and posts what it chose. 1050 if (!self.session.is_terminal and !entryConflicted(entry) and (self.session.data.is_local or self.session.data.user_id != null)) { 1051 const label = "add attachment"; 1052 var button = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1053 errdefer button.deinit(allocator); 1054 button.getFocus().focusable = true; 1055 button.getFocus().kind = .{ .custom = if (self.data.identity.len == 0) 1056 try std.fmt.allocPrint(pa, "{s}/{s}:{s}/attach", .{ ui.file_input_prefix, @tagName(kind), entry.id }) 1057 else 1058 try std.fmt.allocPrint(pa, "{s}/repo/{s}/{s}:{s}/attach", .{ ui.file_input_prefix, self.data.identity, @tagName(kind), entry.id }) }; 1059 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 1060 } 1061 1062 // a conflicted issue's only action is resolving it. the button 1063 // stays visible logged out; the resolve page shows the 1064 // unauthorized view then. 1065 if (has_conflicts and entryConflicted(entry)) { 1066 const label = "resolve conflict"; 1067 var button = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1068 errdefer button.deinit(allocator); 1069 button.getFocus().focusable = true; 1070 const route = resolveRoute(self.data.identity, entry.id, "") orelse return error.RouteTooLong; 1071 button.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 1072 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 1073 } else { 1074 // a logged-out session can't flip the status, so it gets no 1075 // open/close button 1076 if (has_status and (self.session.data.is_local or self.session.data.user_id != null)) { 1077 const action: []const u8 = switch (entryStatus(entry)) { 1078 .open => "close", 1079 .closed => "open", 1080 }; 1081 const route = commentsRoute(self.data.identity, entry.id, 0) orelse return error.RouteTooLong; 1082 row.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "form:{s}/{s}", .{ try route.toUrl(self.session.page_arena), action }) }; 1083 1084 var button = try wgt.TextBox(ui.Widget).init(allocator, action, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1085 errdefer button.deinit(allocator); 1086 button.getFocus().focusable = true; 1087 // the renderer distinguishes plain clickables from buttons 1088 // that should POST to a server route by this kind. 1089 button.getFocus().kind = .{ .custom = "submit" }; 1090 self.status_button_id[index] = button.getFocus().id; 1091 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = action.len + 2, .height = null } }); 1092 } 1093 1094 // the edit button links to the issue's edit page. 1095 { 1096 var button = try wgt.TextBox(ui.Widget).init(allocator, "edit", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1097 errdefer button.deinit(allocator); 1098 button.getFocus().focusable = true; 1099 const route = editRoute(self.data.identity, entry.id) orelse return error.RouteTooLong; 1100 button.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 1101 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = "edit".len + 2, .height = null } }); 1102 } 1103 } 1104 1105 if (self.session.data.is_local or self.session.data.user_id != null) { 1106 var remove = try wgt.TextBox(ui.Widget).init(allocator, "✕", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1107 errdefer remove.deinit(allocator); 1108 remove.getFocus().focusable = true; 1109 const route = removeRoute(self.data.identity, entry.id, "") orelse return error.RouteTooLong; 1110 remove.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 1111 try row.children.put(allocator, remove.getFocus().id, .{ .widget = .{ .text_box = remove }, .rect = null, .min_size = .{ .width = 3, .height = null } }); 1112 } 1113 1114 // the leftmost button: the attachment button when the session has 1115 // one, else the resolve button on a conflicted issue, the 1116 // open/close button when present, the edit button otherwise 1117 row.getFocus().child_id = row.children.keys()[first_in_row_index]; 1118 } 1119 1120 if (comment_page) |page| { 1121 var back_label_buf: ["← back to ".len + thread_name.len]u8 = undefined; 1122 const back_label = try std.fmt.bufPrint(&back_label_buf, "← back to {s}", .{thread_name}); 1123 var back = try Comment.linkBox(allocator, self.session, back_label, commentsRoute(self.data.identity, entry.id, 0) orelse return error.RouteTooLong); 1124 errdefer back.deinit(allocator); 1125 try inner.children.put(allocator, back.getFocus().id, .{ .widget = .{ .text_box = back }, .rect = null, .min_size = null }); 1126 1127 try Comment.appendComment(allocator, inner, self.session, self.data.identity, kind, page.selected); 1128 try Comment.appendCount(allocator, inner, page.replies.count, "reply", "replies"); 1129 for (page.replies.comments) |comment| try Comment.appendComment(allocator, inner, self.session, self.data.identity, kind, comment); 1130 try Comment.appendWindowNav(allocator, inner, self.session, self.data.identity, kind, &page.selected.comment.event.thread_id, &page.selected.id, page.replies); 1131 1132 var spacer = try ui.Spacer.init(allocator); 1133 errdefer spacer.deinit(allocator); 1134 try inner.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1135 1136 inner.getFocus().child_id = inner.children.keys()[0]; 1137 const sc = self.detailScroll(index); 1138 sc.x = 0; 1139 sc.y = 0; 1140 sc.getFocus().version +%= 1; 1141 return; 1142 } 1143 1144 if (description_page) { 1145 // the back link, in the title slot so the pane's input handling 1146 // applies to it unchanged. 1147 var back_label_buf: ["← back to ".len + thread_name.len]u8 = undefined; 1148 const back_label = try std.fmt.bufPrint(&back_label_buf, "← back to {s}", .{thread_name}); 1149 var tb = try wgt.TextBox(ui.Widget).init(allocator, back_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1150 errdefer tb.deinit(allocator); 1151 tb.getFocus().focusable = true; 1152 tb.getFocus().kind = .{ .custom = try listLink(self.session.page_arena, self.data.identity, entryStatus(entry), "", entry.id) }; 1153 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1154 } else { 1155 // the issue's title as a focusable word-wrapped text box. 1156 { 1157 var tb = try wgt.TextBox(ui.Widget).init(allocator, entry.record.event.title, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = " title " }); 1158 errdefer tb.deinit(allocator); 1159 tb.getFocus().focusable = true; 1160 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1161 } 1162 1163 self.author_id[index] = blk: { 1164 var tb = try ui.authorBox(allocator, self.session.page_arena, entry.author); 1165 errdefer tb.deinit(allocator); 1166 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1167 break :blk tb.getFocus().id; 1168 }; 1169 1170 // the issue's tags, each linking to this status's list filtered to 1171 // that tag. 1172 { 1173 var items: std.ArrayList(ui.TagFlow.Item) = .empty; 1174 defer items.deinit(allocator); 1175 var tag_iter = Event.tagIterator(entry.record.event.tags); 1176 while (tag_iter.next()) |tag| { 1177 if (tag.len == 0) continue; 1178 try items.append(allocator, .{ .text = tag, .link = try tagLink(self.session.page_arena, self.data.identity, entryStatus(entry), tag) }); 1179 } 1180 if (items.items.len > 0) { 1181 var tf = try ui.TagFlow.init(allocator); 1182 errdefer tf.deinit(allocator); 1183 try tf.setItems(allocator, items.items); 1184 try inner.children.put(allocator, tf.getFocus().id, .{ .widget = .{ .tag_flow = tf }, .rect = null, .min_size = null }); 1185 } 1186 } 1187 } 1188 1189 // the description as a focusable word-wrapped text box. past the limit 1190 // it's cut at the last whole line and links to the /description page, 1191 // which shows the whole thing. 1192 self.description_id[index] = blk: { 1193 const whole = entry.record.event.description; 1194 const cut_short = !description_page and whole.len > Data.max_description_size; 1195 const shown = if (cut_short) 1196 std.mem.trimEnd(u8, whole[0 .. std.mem.lastIndexOfScalar(u8, whole[0..Data.max_description_size], '\n') orelse 0], " \t\r\n") 1197 else if (whole.len == 0) 1198 "(no description)" 1199 else 1200 whole; 1201 var tb = try wgt.TextBox(ui.Widget).init(allocator, shown, .{ 1202 .border_style = .single, 1203 .rounded_corners = true, 1204 .wrap_kind = .word, 1205 .label = " description ", 1206 .bottom_label = if (cut_short) " click or press enter to see more " else "", 1207 }); 1208 errdefer tb.deinit(allocator); 1209 tb.getFocus().focusable = true; 1210 if (cut_short) tb.getFocus().kind = .{ .custom = try descriptionLink(self.session.page_arena, self.data.identity, entry.id) }; 1211 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1212 break :blk tb.getFocus().id; 1213 }; 1214 1215 const parent_route = commentsRoute(self.data.identity, entry.id, 0) orelse return error.RouteTooLong; 1216 try Attachment.appendRows(allocator, inner, self.session, self.data.identity, try parent_route.toUrl(self.session.page_arena), entry.attachments); 1217 1218 if (!description_page) { 1219 var reply = try Comment.linkBox(allocator, self.session, "new comment", newCommentRoute(self.data.identity, entry.id, "") orelse return error.RouteTooLong); 1220 errdefer reply.deinit(allocator); 1221 try inner.children.put(allocator, reply.getFocus().id, .{ .widget = .{ .text_box = reply }, .rect = null, .min_size = null }); 1222 1223 try Comment.appendCount(allocator, inner, entry.comments.count, "comment", "comments"); 1224 for (entry.comments.comments) |comment| try Comment.appendComment(allocator, inner, self.session, self.data.identity, kind, comment); 1225 try Comment.appendWindowNav(allocator, inner, self.session, self.data.identity, kind, entry.id, null, entry.comments); 1226 1227 var spacer = try ui.Spacer.init(allocator); 1228 errdefer spacer.deinit(allocator); 1229 try inner.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1230 } 1231 1232 // select the title by default 1233 inner.getFocus().child_id = inner.children.keys()[title_child_index]; 1234 1235 // reset the scroll to the top for the newly-shown issue: directly on the 1236 // terminal (the wasm offset), and via a version bump on the web (so the 1237 // renderer's scroll id changes and JS drops the preserved position). 1238 const sc = self.detailScroll(index); 1239 sc.x = 0; 1240 sc.y = 0; 1241 sc.getFocus().version +%= 1; 1242 } 1243 1244 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 1245 // wheel input moves focus like the arrows 1246 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 1247 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 1248 else 1249 raw_key; 1250 1251 if (self.headerActive()) { 1252 // down from the tabs re-enters the stack if the selected view has 1253 // something to focus; other keys move the tabs. 1254 if (inp.vertDirection(key) == .down) { 1255 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 1256 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 1257 // nothing focusable in the unauthorized view 1258 .unauthorized => false, 1259 else => true, 1260 } else false; 1261 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 1262 } else { 1263 try self.header().input(allocator, key, root_focus); 1264 } 1265 return; 1266 } 1267 if (self.tagsViewActive()) { 1268 self.tagsViewInput(key, root_focus); 1269 return; 1270 } 1271 if (self.formViewActive()) { 1272 if (self.data.view == .remove) { 1273 try self.removeFormInput(allocator, key, root_focus); 1274 } else if (has_conflicts and self.data.view == .resolve) { 1275 try self.resolveInput(allocator, key, root_focus); 1276 } else if (self.data.view == .new_comment or self.data.view == .edit_comment) { 1277 try self.commentFormInput(allocator, key, root_focus); 1278 } else { 1279 try self.formInput(allocator, key, root_focus); 1280 } 1281 return; 1282 } 1283 const i = self.selectedSplitIndex() orelse return; 1284 if (self.detailActive(i)) { 1285 try self.detailInput(allocator, i, key, root_focus); 1286 } else { 1287 try self.listInput(i, key, root_focus); 1288 } 1289 } 1290 1291 // arrow keys move the tag selection; up from the top row crosses to the 1292 // header tabs. 1293 fn tagsViewInput(self: *This, key: Key, root_focus: *Focus) void { 1294 const tf = self.tagsView(); 1295 const cid = tf.focus.child_id orelse return; 1296 const cur = tf.indexOfFocusId(cid) orelse return; 1297 const count = tf.text_boxes.items.len; 1298 switch (key) { 1299 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 1300 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 1301 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 1302 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 1303 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 1304 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 1305 else => {}, 1306 } 1307 } 1308 1309 fn listInput(self: *This, index: usize, key: Key, root_focus: *Focus) !void { 1310 // up/down (and the scroll wheel) move the selection a row; page up/down 1311 // jump a fixed amount. right/Enter cross into the detail pane. up from 1312 // the top row crosses into the header tabs. 1313 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 1314 const lb = self.listBox(index); 1315 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 1316 if (delta < 0 and at_top) return self.focusHeader(root_focus); 1317 ui.moveRowFocus(lb, self.listScroll(index), root_focus, delta); 1318 return; 1319 } 1320 switch (key) { 1321 .enter, .arrow_right => try self.focusDetail(index, root_focus), 1322 else => {}, 1323 } 1324 } 1325 1326 fn detailInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1327 switch (key) { 1328 .page_up => { 1329 self.pageDetail(index, root_focus, -10); 1330 return; 1331 }, 1332 .page_down => { 1333 self.pageDetail(index, root_focus, 10); 1334 return; 1335 }, 1336 .home => { 1337 self.jumpDetail(index, root_focus, false); 1338 return; 1339 }, 1340 .end => { 1341 self.jumpDetail(index, root_focus, true); 1342 return; 1343 }, 1344 else => {}, 1345 } 1346 if (self.data.comment_page != null and std.mem.eql(u8, self.window(index).items[self.detailed_index[index] orelse return].id, self.data.selected_id)) { 1347 try self.commentInput(allocator, index, key, root_focus); 1348 } else if (self.toolRowFocused(index)) { 1349 try self.toolRowInput(allocator, index, key, root_focus); 1350 } else if (self.titleFocused(index)) { 1351 try self.titleInput(index, key, root_focus); 1352 } else if (self.authorFocused(index)) { 1353 try self.authorInput(index, key, root_focus); 1354 } else if (self.tagsFocused(index)) { 1355 try self.tagsInput(index, key, root_focus); 1356 } else if (self.descriptionFocused(index)) { 1357 try self.descriptionInput(index, key, root_focus); 1358 } else if (try self.attachmentInput(allocator, index, key, root_focus)) {} else { 1359 try self.commentInput(allocator, index, key, root_focus); 1360 } 1361 } 1362 1363 fn attachmentInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !bool { 1364 const child_index = self.focusedDetailChild(index, root_focus) orelse return false; 1365 const child = &self.detailInner(index).children.values()[child_index].widget; 1366 const row = switch (child.*) { 1367 .box => |*box| box, 1368 else => return false, 1369 }; 1370 const attachment_id = Attachment.removeId(row) orelse return false; 1371 if (row.children.count() < 2) return false; 1372 const remove_id = row.children.keys()[1]; 1373 switch (key) { 1374 .arrow_left => if (!self.moveCommentHorizontal(index, root_focus, false)) try self.focusList(index, root_focus), 1375 .arrow_right => _ = self.moveCommentHorizontal(index, root_focus, true), 1376 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1377 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1378 .enter => if (row.getFocus().child_id == remove_id) try self.removeEvent(allocator, .attach, attachment_id), 1379 .mouse => |mouse| if (inp.leftClickOn(root_focus, remove_id, mouse)) try self.removeEvent(allocator, .attach, attachment_id), 1380 else => {}, 1381 } 1382 return true; 1383 } 1384 1385 // the tool row: enter or a click on the open/close button flips the 1386 // issue's status; the edit and resolve buttons are a: links the host 1387 // follows. arrows cross between the buttons and to the neighboring 1388 // widgets. 1389 fn toolRowInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1390 const row = self.toolRow(index); 1391 const cur = if (row.getFocus().child_id) |cid| row.children.getIndex(cid) orelse return else return; 1392 const on_status = if (self.status_button_id[index]) |id| row.getFocus().child_id == id else false; 1393 switch (key) { 1394 .arrow_left => if (cur > first_in_row_index) root_focus.setFocus(row.children.keys()[cur - 1]) else try self.focusList(index, root_focus), 1395 .arrow_right => if (cur + 1 < row.children.count()) root_focus.setFocus(row.children.keys()[cur + 1]), 1396 .arrow_up => self.focusHeader(root_focus), 1397 .arrow_down => { 1398 self.focusDetailEdge(index, root_focus, false); 1399 self.detailScroll(index).y = 0; 1400 }, 1401 .enter => if (on_status) try self.toggleStatus(allocator, index), 1402 .mouse => |mouse| if (self.status_button_id[index]) |id| { 1403 if (inp.leftClickOn(root_focus, id, mouse)) try self.toggleStatus(allocator, index); 1404 }, 1405 else => {}, 1406 } 1407 } 1408 1409 fn titleInput(self: *This, index: usize, key: Key, root_focus: *Focus) !void { 1410 switch (key) { 1411 .arrow_left => try self.focusList(index, root_focus), 1412 .arrow_up => self.focusToolRow(index, root_focus), 1413 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1414 else => {}, 1415 } 1416 } 1417 1418 // the author box's a: link (when it has one) is followed by the host on 1419 // enter / a click; arrows cross to the neighboring widgets. 1420 fn authorInput(self: *This, index: usize, key: Key, root_focus: *Focus) !void { 1421 switch (key) { 1422 .arrow_left => try self.focusList(index, root_focus), 1423 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1424 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1425 else => {}, 1426 } 1427 } 1428 1429 fn descriptionInput(self: *This, index: usize, key: Key, root_focus: *Focus) !void { 1430 const sc = self.detailScroll(index); 1431 switch (key) { 1432 .arrow_left => return self.focusList(index, root_focus), 1433 // the terminal moves through the description one line at a time; 1434 // the web lets focus changes drive its native scroll. 1435 .arrow_up => { 1436 if (!self.session.is_terminal) { 1437 _ = self.moveDetailVertical(index, root_focus, false); 1438 return; 1439 } 1440 if (self.focusAdjacentVisible(index, root_focus, false)) return; 1441 const before = sc.y; 1442 sc.y -= 1; 1443 sc.clampToContent(); 1444 if (sc.y == before) _ = self.moveDetailVertical(index, root_focus, false); 1445 return; 1446 }, 1447 .arrow_down => { 1448 if (!self.session.is_terminal) { 1449 _ = self.moveDetailVertical(index, root_focus, true); 1450 return; 1451 } 1452 if (self.focusAdjacentVisible(index, root_focus, true)) return; 1453 const before = sc.y; 1454 sc.y += 1; 1455 sc.clampToContent(); 1456 if (sc.y == before) _ = self.moveDetailVertical(index, root_focus, true); 1457 return; 1458 }, 1459 else => return, 1460 } 1461 sc.clampToContent(); 1462 } 1463 1464 fn commentInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1465 _ = allocator; 1466 switch (key) { 1467 .arrow_left => if (!self.moveCommentHorizontal(index, root_focus, false)) return self.focusList(index, root_focus), 1468 .arrow_right => _ = self.moveCommentHorizontal(index, root_focus, true), 1469 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1470 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1471 else => return, 1472 } 1473 } 1474 1475 // page the detail scroll, then focus the first or last visible widget. 1476 fn pageDetail(self: *This, index: usize, root_focus: *Focus, delta: isize) void { 1477 if (!self.session.is_terminal) return; 1478 const sc = self.detailScroll(index); 1479 sc.y += delta; 1480 sc.clampToContent(); 1481 self.focusVisible(index, root_focus, delta > 0); 1482 } 1483 1484 // jump to the detail's first or last focusable widget. the terminal pins 1485 // its synthetic scroll too; the browser scrolls to the new focus itself. 1486 fn jumpDetail(self: *This, index: usize, root_focus: *Focus, to_end: bool) void { 1487 if (self.session.is_terminal) { 1488 const sc = self.detailScroll(index); 1489 sc.y = if (to_end) std.math.maxInt(isize) else 0; 1490 sc.clampToContent(); 1491 self.focusVisible(index, root_focus, to_end); 1492 } else { 1493 self.focusDetailEdge(index, root_focus, to_end); 1494 } 1495 } 1496 1497 fn focusDetailEdge(self: *This, index: usize, root_focus: *Focus, last: bool) void { 1498 const focus = self.detailInner(index).getFocus(); 1499 var chosen: ?usize = null; 1500 for (focus.children.keys(), focus.children.values()) |id, child| { 1501 if (!child.focus.focusable) continue; 1502 chosen = id; 1503 if (!last) break; 1504 } 1505 if (chosen) |id| root_focus.setFocus(id); 1506 } 1507 1508 fn detailRectVisible(self: *This, index: usize, rect: layout.IRect) bool { 1509 const sc = self.detailScroll(index); 1510 const viewport = sc.grid orelse return false; 1511 const top = sc.y; 1512 const bottom = top + @as(isize, @intCast(viewport.size.height - sc.bar_h)); 1513 return rect.y + @as(isize, @intCast(rect.size.height)) > top and rect.y < bottom; 1514 } 1515 1516 // focus the top- or bottom-most focusable widget intersecting the viewport. 1517 fn focusVisible(self: *This, index: usize, root_focus: *Focus, last: bool) void { 1518 const inner = self.detailInner(index); 1519 var offset: usize = 0; 1520 while (offset < inner.children.count()) : (offset += 1) { 1521 const child_index = if (last) inner.children.count() - 1 - offset else offset; 1522 if (self.focusVisibleChild(index, child_index, root_focus, last)) return; 1523 } 1524 } 1525 1526 fn focusVisibleChild(self: *This, index: usize, child_index: usize, root_focus: *Focus, last: bool) bool { 1527 const child = &self.detailInner(index).children.values()[child_index]; 1528 const child_rect = child.rect orelse return false; 1529 var chosen: ?usize = null; 1530 if (child.widget.getFocus().focusable and self.detailRectVisible(index, child_rect)) { 1531 const id = self.detailInner(index).children.keys()[child_index]; 1532 chosen = id; 1533 if (!last) { 1534 root_focus.setFocus(id); 1535 return true; 1536 } 1537 } 1538 1539 for (child.widget.getFocus().children.keys(), child.widget.getFocus().children.values()) |id, focus_child| { 1540 if (!focus_child.focus.focusable) continue; 1541 const rect: layout.IRect = .{ 1542 .x = child_rect.x + @as(isize, @intCast(focus_child.rect.x)), 1543 .y = child_rect.y + @as(isize, @intCast(focus_child.rect.y)), 1544 .size = focus_child.rect.size, 1545 }; 1546 if (!self.detailRectVisible(index, rect)) continue; 1547 chosen = id; 1548 if (!last) break; 1549 } 1550 if (chosen) |id| { 1551 root_focus.setFocus(id); 1552 return true; 1553 } 1554 return false; 1555 } 1556 1557 fn focusAdjacentVisible(self: *This, index: usize, root_focus: *Focus, down: bool) bool { 1558 const inner = self.detailInner(index); 1559 var next = self.focusedDetailChild(index, root_focus) orelse return false; 1560 while (true) { 1561 if (down) { 1562 next += 1; 1563 if (next >= inner.children.count()) return false; 1564 } else { 1565 if (next == 0) return false; 1566 next -= 1; 1567 } 1568 if (self.focusVisibleChild(index, next, root_focus, !down)) { 1569 self.focusDetailChild(index, next, !down, root_focus); 1570 return true; 1571 } 1572 } 1573 } 1574 1575 fn focusedDetailChild(self: *This, index: usize, root_focus: *Focus) ?usize { 1576 const inner = self.detailInner(index); 1577 var id = root_focus.grandchild_id orelse return null; 1578 while (inner.getFocus().children.get(id)) |child| { 1579 if (child.parent_id == inner.getFocus().id) return inner.children.getIndex(id); 1580 id = child.parent_id; 1581 } 1582 return null; 1583 } 1584 1585 fn detailChildFocusable(self: *This, index: usize, child_index: usize) bool { 1586 const child = &self.detailInner(index).children.values()[child_index].widget; 1587 const focus = child.getFocus(); 1588 if (focus.focusable) return true; 1589 for (focus.children.values()) |item| { 1590 if (item.focus.focusable) return true; 1591 } 1592 return false; 1593 } 1594 1595 fn focusDetailChild(self: *This, index: usize, child_index: usize, last: bool, root_focus: *Focus) void { 1596 const inner = self.detailInner(index); 1597 const child = &inner.children.values()[child_index]; 1598 var target = inner.children.keys()[child_index]; 1599 var rect = child.rect orelse return; 1600 switch (child.widget) { 1601 .repo_comment => |*comment| { 1602 if (comment.rowRect(last)) |row_rect| { 1603 rect.x += row_rect.x; 1604 rect.y += row_rect.y; 1605 rect.size = row_rect.size; 1606 } 1607 if (last) comment.focusBody(root_focus) else comment.focusMetadata(root_focus); 1608 }, 1609 .box => |*box| if (box.children.count() > 0) { 1610 target = box.children.keys()[if (last) box.children.count() - 1 else 0]; 1611 root_focus.setFocus(target); 1612 }, 1613 else => root_focus.setFocus(target), 1614 } 1615 const sc = self.detailScroll(index); 1616 if (sc.grid) |viewport| { 1617 const viewport_height = viewport.size.height - sc.bar_h; 1618 if (rect.size.height > viewport_height) { 1619 const bottom_aligned = rect.y + @as(isize, @intCast(rect.size.height - viewport_height)); 1620 sc.y = std.math.clamp(sc.y, rect.y, bottom_aligned); 1621 sc.clampToContent(); 1622 return; 1623 } 1624 } 1625 sc.scrollToRect(rect); 1626 } 1627 1628 fn moveDetailVertical(self: *This, index: usize, root_focus: *Focus, down: bool) bool { 1629 const inner = self.detailInner(index); 1630 const current = self.focusedDetailChild(index, root_focus) orelse return false; 1631 if (inner.children.values()[current].widget == .repo_comment) { 1632 const comment = &inner.children.values()[current].widget.repo_comment; 1633 if (down and !comment.bodyFocused()) { 1634 self.focusDetailChild(index, current, true, root_focus); 1635 return true; 1636 } 1637 if (!down and comment.bodyFocused()) { 1638 self.focusDetailChild(index, current, false, root_focus); 1639 return true; 1640 } 1641 } 1642 1643 var next = current; 1644 while (true) { 1645 if (down) { 1646 next += 1; 1647 if (next >= inner.children.count()) return false; 1648 } else { 1649 if (next == 0) return false; 1650 next -= 1; 1651 } 1652 if (!self.detailChildFocusable(index, next)) continue; 1653 self.focusDetailChild(index, next, !down, root_focus); 1654 return true; 1655 } 1656 } 1657 1658 fn moveCommentHorizontal(self: *This, index: usize, root_focus: *Focus, right: bool) bool { 1659 const inner = self.detailInner(index); 1660 const current = self.focusedDetailChild(index, root_focus) orelse return false; 1661 const child = &inner.children.values()[current]; 1662 if (child.widget == .repo_comment) { 1663 const moved = child.widget.repo_comment.moveHorizontal(root_focus, right); 1664 if (moved) if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 1665 return moved; 1666 } 1667 const row = switch (child.widget) { 1668 .box => |*box| box, 1669 else => return false, 1670 }; 1671 const selected = row.getFocus().child_id orelse return false; 1672 const selected_index = row.children.getIndex(selected) orelse return false; 1673 const target = if (right) selected_index + 1 else selected_index -| 1; 1674 if (target == selected_index or target >= row.children.count()) return false; 1675 root_focus.setFocus(row.children.keys()[target]); 1676 if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 1677 return true; 1678 } 1679 1680 // up/down (and tab/shift+tab) move between a form's fields; up from the 1681 // title crosses into the header tabs. the multiline description keeps 1682 // enter and any up/down that has a row to move to. 1683 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1684 const form = self.threadForm() orelse return; 1685 const cid = form.getFocus().child_id orelse return; 1686 const cur = form.children.getIndex(cid) orelse return; 1687 1688 if (cur == description_field_index) { 1689 const child = &form.children.values()[cur]; 1690 if (try multilineKeepsKey(&child.widget.text_input, allocator, key)) 1691 return child.widget.input(allocator, key, root_focus); 1692 } 1693 1694 switch (key) { 1695 .arrow_up, .back_tab => if (cur > 0) 1696 root_focus.setFocus(form.children.keys()[cur - 1]) 1697 else 1698 self.focusHeader(root_focus), 1699 .arrow_down, .tab => if (cur < submit_field_index) { 1700 root_focus.setFocus(form.children.keys()[cur + 1]); 1701 }, 1702 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 1703 .mouse => |mouse| if (cur == submit_field_index) { 1704 const submit = &form.children.values()[cur].widget.submit_button; 1705 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitForm(allocator); 1706 }, 1707 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 1708 } 1709 } 1710 1711 fn commentFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1712 const form = self.threadForm() orelse return; 1713 const cid = form.getFocus().child_id orelse return; 1714 const cur = form.children.getIndex(cid) orelse return; 1715 const child = &form.children.values()[cur]; 1716 1717 if (cur == comment_body_field_index and try multilineKeepsKey(&child.widget.text_input, allocator, key)) 1718 return child.widget.input(allocator, key, root_focus); 1719 1720 switch (key) { 1721 .arrow_up, .back_tab => if (cur > comment_author_field_index) 1722 root_focus.setFocus(form.children.keys()[cur - 1]) 1723 else 1724 self.focusHeader(root_focus), 1725 .arrow_down, .tab => if (cur < comment_submit_field_index) 1726 root_focus.setFocus(form.children.keys()[cur + 1]), 1727 .enter => if (cur == comment_submit_field_index) try self.submitComment(allocator), 1728 .mouse => |mouse| if (cur == comment_submit_field_index) { 1729 const submit = &child.widget.submit_button; 1730 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitComment(allocator); 1731 }, 1732 else => try child.widget.input(allocator, key, root_focus), 1733 } 1734 } 1735 1736 fn removeFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1737 const form = self.removeForm() orelse return; 1738 const button_id = form.children.keys()[1]; 1739 switch (key) { 1740 .arrow_up, .back_tab => self.focusHeader(root_focus), 1741 .enter => try self.submitRemove(allocator), 1742 .mouse => |mouse| if (inp.leftClickOn(root_focus, button_id, mouse)) try self.submitRemove(allocator), 1743 else => {}, 1744 } 1745 } 1746 1747 // the multiline hunk inputs keep enter and any up/down that has a row to 1748 // move to; enter on a "use this" link is a navigation the host follows 1749 // before this runs. 1750 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1751 const form = self.threadForm() orelse return; 1752 const cid = form.getFocus().child_id orelse return; 1753 const cur = form.children.getIndex(cid) orelse return; 1754 const child = &form.children.values()[cur]; 1755 1756 if (child.widget == .text_input) { 1757 const ti = &child.widget.text_input; 1758 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 1759 return child.widget.input(allocator, key, root_focus); 1760 } 1761 1762 const on_submit = child.widget == .submit_button; 1763 1764 switch (key) { 1765 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 1766 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 1767 .arrow_left, .arrow_right => switch (child.widget) { 1768 .box => |*row| { 1769 const row_cid = row.getFocus().child_id orelse return; 1770 const row_index = row.children.getIndex(row_cid) orelse return; 1771 if (key == .arrow_left) { 1772 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 1773 } else if (row_index + 1 < row.children.count()) { 1774 root_focus.setFocus(row.children.keys()[row_index + 1]); 1775 } 1776 }, 1777 else => try child.widget.input(allocator, key, root_focus), 1778 }, 1779 .enter => if (on_submit) try self.submitResolution(allocator), 1780 .mouse => |mouse| if (on_submit) { 1781 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 1782 }, 1783 else => try child.widget.input(allocator, key, root_focus), 1784 } 1785 } 1786 1787 // a multiline input keeps enter and any up/down that has a row to move to 1788 fn multilineKeepsKey(text_input: *wgt.TextInput(ui.Widget), allocator: std.mem.Allocator, key: Key) !bool { 1789 return switch (key) { 1790 .enter => true, 1791 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 1792 .arrow_down => !try text_input.cursorOnLastRow(allocator), 1793 else => false, 1794 }; 1795 } 1796 1797 // the neighboring focusable resolve-form child, skipping the spacer and 1798 // the blank gap rows 1799 fn resolveStep(form: *wgt.Box(ui.Widget), cur: usize, down: bool) ?usize { 1800 var i = cur; 1801 while (true) { 1802 if (down) { 1803 i += 1; 1804 if (i >= form.children.count()) return null; 1805 } else { 1806 if (i == 0) return null; 1807 i -= 1; 1808 } 1809 switch (form.children.values()[i].widget) { 1810 .spacer, .text => continue, 1811 else => return i, 1812 } 1813 } 1814 } 1815 1816 // focus the resolve-form child at `i` (a version row focuses its selected 1817 // child) and keep it visible on the terminal 1818 fn focusResolveChild(self: *This, form: *wgt.Box(ui.Widget), i: usize, root_focus: *Focus) void { 1819 const child = &form.children.values()[i]; 1820 switch (child.widget) { 1821 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 1822 else => root_focus.setFocus(form.children.keys()[i]), 1823 } 1824 if (self.session.is_terminal) { 1825 if (self.resolveScroll()) |sc| { 1826 if (child.rect) |rect| sc.scrollToRect(rect); 1827 } 1828 } 1829 } 1830 1831 // re-emit the conflicted issue's event with the resolved content; any 1832 // event on the issue settles its conflict entry. this is the terminal 1833 // path; the web posts the form to the resolve route. 1834 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 1835 if (comptime wasm) return; 1836 const io = self.session.io orelse return; 1837 const src = self.data.repo_source orelse return; 1838 const entry = self.data.selectedThread() orelse return; 1839 const author = (try self.session.eventAuthor()) orelse return; 1840 const form = self.threadForm() orelse return; 1841 1842 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 1843 var title: ?[]u8 = null; 1844 var tags: ?[]u8 = null; 1845 var hunks: std.ArrayList([]const u8) = .empty; 1846 defer { 1847 if (title) |t| allocator.free(t); 1848 if (tags) |t| allocator.free(t); 1849 for (hunks.items) |h| allocator.free(h); 1850 hunks.deinit(allocator); 1851 } 1852 for (form.children.values()) |*child| switch (child.widget) { 1853 .text_input => |*ti| { 1854 const text = try ti.text(allocator); 1855 errdefer allocator.free(text); 1856 if (std.mem.eql(u8, ti.options.name, "title")) { 1857 title = text; 1858 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 1859 tags = text; 1860 } else { 1861 try hunks.append(allocator, text); 1862 } 1863 }, 1864 else => {}, 1865 }; 1866 1867 var id_bytes: [evt.event_id_size]u8 = undefined; 1868 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 1869 1870 switch (src.repo_kind) { 1871 inline else => |repo_kind| { 1872 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 1873 defer any_repo.deinit(io, allocator); 1874 switch (any_repo) { 1875 inline else => |*repo| Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .resolve = .{ 1876 .title = title, 1877 .tags = tags, 1878 .hunks = hunks.items, 1879 } }, author) catch |err| switch (err) { 1880 // leave the form up for correction 1881 error.InvalidFields => return, 1882 else => |e| return e, 1883 }, 1884 } 1885 }, 1886 } 1887 1888 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 1889 try self.session.navigate(route); 1890 } 1891 1892 // commit a new or edited comment and navigate to its permalink. this is the 1893 // terminal path; the web posts the form to its comment route. 1894 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 1895 if (comptime wasm) return; 1896 const io = self.session.io orelse return; 1897 const src = self.data.repo_source orelse return; 1898 const author = (try self.session.eventAuthor()) orelse return; 1899 const form = self.threadForm() orelse return; 1900 const body_input = &form.children.values()[comment_body_field_index].widget.text_input; 1901 const body = try body_input.text(allocator); 1902 defer allocator.free(body); 1903 if (!evt.Comment.fieldsValid(body)) return; 1904 1905 var thread_id: [evt.event_id_size * 2]u8 = undefined; 1906 @memcpy(&thread_id, self.data.selected_id); 1907 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 1908 var parent: [evt.event_id_size * 2]u8 = undefined; 1909 @memcpy(&parent, self.data.comment_id); 1910 break :blk parent; 1911 }; 1912 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 1913 const editing = self.data.view == .edit_comment; 1914 switch (src.repo_kind) { 1915 inline else => |repo_kind| { 1916 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 1917 defer any_repo.deinit(io, allocator); 1918 switch (any_repo) { 1919 inline else => |*repo| if (editing) { 1920 var comment_id: [evt.event_id_size]u8 = undefined; 1921 _ = std.fmt.hexToBytes(&comment_id, self.data.comment_id) catch return; 1922 try evt.Comment.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 1923 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 1924 } else { 1925 event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 1926 }, 1927 } 1928 }, 1929 } 1930 1931 body_input.clear(allocator); 1932 const route = commentRoute(self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 1933 try self.session.navigate(route); 1934 } 1935 1936 // the terminal submit paths: the new form commits a new-issue event, the 1937 // edit form re-emits the selected issue's event with the form's content. 1938 // the web posts the forms to their routes instead. 1939 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 1940 if (self.data.view == .edit) { 1941 try self.submitEditedThread(allocator); 1942 } else { 1943 try self.submitNewThread(allocator); 1944 } 1945 } 1946 1947 // commit the new issue to the repo's events branch and navigate to it. 1948 // this is the terminal path; the web posts the form to the issue route, 1949 // so the wasm side never runs (or compiles) the repo access below. 1950 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 1951 if (comptime wasm) return; 1952 const io = self.session.io orelse return; 1953 const src = self.data.repo_source orelse return; 1954 const author = (try self.session.eventAuthor()) orelse return; 1955 1956 const form = self.threadForm() orelse return; 1957 const title_input = &form.children.values()[title_field_index].widget.text_input; 1958 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 1959 const description_input = &form.children.values()[description_field_index].widget.text_input; 1960 1961 const title = try title_input.text(allocator); 1962 defer allocator.free(title); 1963 const tags = try tags_input.text(allocator); 1964 defer allocator.free(tags); 1965 const description = try description_input.text(allocator); 1966 defer allocator.free(description); 1967 1968 if (!Event.fieldsValid(title, tags)) return; 1969 1970 var id_bytes: [evt.event_id_size]u8 = undefined; 1971 io.random(&id_bytes); 1972 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 1973 1974 const event = evt.EventWithId{ 1975 .id = event_id_hex, 1976 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 1977 .author = author, 1978 .event = @unionInit(evt.Event, @tagName(kind), Event{ 1979 .title = title, 1980 .description = description, 1981 .tags = tags, 1982 }), 1983 }; 1984 1985 switch (src.repo_kind) { 1986 inline else => |repo_kind| { 1987 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 1988 defer any_repo.deinit(io, allocator); 1989 switch (any_repo) { 1990 inline else => |*repo| try evt.consume(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 1991 } 1992 }, 1993 } 1994 1995 // wipe the form so a return visit starts fresh 1996 title_input.clear(allocator); 1997 tags_input.clear(allocator); 1998 description_input.clear(allocator); 1999 2000 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2001 try self.session.navigate(route); 2002 } 2003 2004 // re-emit the selected issue's event with the form's content (status 2005 // preserved), then reload the issue's page. this is the terminal path; 2006 // the web posts the form to the edit route. 2007 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2008 if (comptime wasm) return; 2009 const io = self.session.io orelse return; 2010 const src = self.data.repo_source orelse return; 2011 const entry = self.data.selectedThread() orelse return; 2012 const author = (try self.session.eventAuthor()) orelse return; 2013 2014 const form = self.threadForm() orelse return; 2015 const title_input = &form.children.values()[title_field_index].widget.text_input; 2016 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2017 const description_input = &form.children.values()[description_field_index].widget.text_input; 2018 2019 const title = try title_input.text(allocator); 2020 defer allocator.free(title); 2021 const tags = try tags_input.text(allocator); 2022 defer allocator.free(tags); 2023 const description = try description_input.text(allocator); 2024 defer allocator.free(description); 2025 2026 if (!Event.fieldsValid(title, tags)) return; 2027 2028 var id_bytes: [evt.event_id_size]u8 = undefined; 2029 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2030 2031 switch (src.repo_kind) { 2032 inline else => |repo_kind| { 2033 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2034 defer any_repo.deinit(io, allocator); 2035 switch (any_repo) { 2036 inline else => |*repo| if (has_status) 2037 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2038 .title = title, 2039 .tags = tags, 2040 .description = description, 2041 } }, author) 2042 else 2043 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 2044 } 2045 }, 2046 } 2047 2048 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2049 try self.session.navigate(route); 2050 } 2051 2052 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2053 const comment = self.data.comment_id.len != 0; 2054 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2055 var id: [evt.event_id_size]u8 = undefined; 2056 _ = std.fmt.hexToBytes(&id, event_id) catch return; 2057 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2058 } 2059 2060 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2061 if (comptime wasm) return; 2062 const io = self.session.io orelse return; 2063 const src = self.data.repo_source orelse return; 2064 const author = (try self.session.eventAuthor()) orelse return; 2065 2066 switch (src.repo_kind) { 2067 inline else => |repo_kind| { 2068 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2069 defer any_repo.deinit(io, allocator); 2070 switch (any_repo) { 2071 inline else => |*repo| try evt.remove(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2072 } 2073 }, 2074 } 2075 2076 const id_hex = std.fmt.bytesToHex(id, .lower); 2077 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 2078 try self.session.navigate(route); 2079 } 2080 2081 // flip the shown issue's status by re-emitting its event, then reload the 2082 // page rooted at the issue so the view reflects the change. this is the 2083 // terminal path; the web posts the button's form to the status route. 2084 fn toggleStatus(self: *This, allocator: std.mem.Allocator, index: usize) !void { 2085 if (comptime !has_status) return; 2086 if (comptime wasm) return; 2087 const io = self.session.io orelse return; 2088 const src = self.data.repo_source orelse return; 2089 const sel = self.detailed_index[index] orelse return; 2090 const entry = self.window(index).items[sel]; 2091 const author = (try self.session.eventAuthor()) orelse return; 2092 2093 const status: Status = switch (entryStatus(entry)) { 2094 .open => .closed, 2095 .closed => .open, 2096 }; 2097 2098 var id_bytes: [evt.event_id_size]u8 = undefined; 2099 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2100 2101 switch (src.repo_kind) { 2102 inline else => |repo_kind| { 2103 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2104 defer any_repo.deinit(io, allocator); 2105 switch (any_repo) { 2106 inline else => |*repo| try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .status = status }, author), 2107 } 2108 }, 2109 } 2110 2111 const route = listRoute(self.data.identity, status, "", entry.id) orelse return; 2112 try self.session.navigate(route); 2113 } 2114 2115 // arrow keys move the tag selection; at the flow's edges focus crosses to 2116 // the neighboring widgets. 2117 fn tagsInput(self: *This, index: usize, key: Key, root_focus: *Focus) !void { 2118 const tf = self.tagFlow(index) orelse return; 2119 const cid = tf.focus.child_id orelse return; 2120 const cur = tf.indexOfFocusId(cid) orelse return; 2121 const count = tf.text_boxes.items.len; 2122 switch (key) { 2123 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 2124 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 2125 .arrow_up => if (tf.rowStep(cur, false)) |i| { 2126 self.focusTag(index, tf, root_focus, i); 2127 } else { 2128 _ = self.moveDetailVertical(index, root_focus, false); 2129 }, 2130 .arrow_down => if (tf.rowStep(cur, true)) |i| { 2131 self.focusTag(index, tf, root_focus, i); 2132 } else { 2133 _ = self.moveDetailVertical(index, root_focus, true); 2134 }, 2135 .home => self.focusTag(index, tf, root_focus, 0), 2136 .end => self.focusTag(index, tf, root_focus, count - 1), 2137 else => {}, 2138 } 2139 } 2140 2141 const title_child_index: usize = 0; 2142 const tags_child_index: usize = 2; 2143 2144 // the tool row's first child after the spacer that pushes it right 2145 const first_in_row_index: usize = 1; 2146 2147 fn tagFlow(self: *This, index: usize) ?*ui.TagFlow { 2148 const inner = self.detailInner(index); 2149 if (inner.children.count() <= tags_child_index) return null; 2150 return switch (inner.children.values()[tags_child_index].widget) { 2151 .tag_flow => |*tf| tf, 2152 else => null, 2153 }; 2154 } 2155 2156 fn tagsFocused(self: *This, index: usize) bool { 2157 const inner = self.detailInner(index); 2158 const cid = inner.getFocus().child_id orelse return false; 2159 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 2160 } 2161 2162 fn toolRowFocused(self: *This, index: usize) bool { 2163 const outer = self.detailOuter(index); 2164 const cid = outer.getFocus().child_id orelse return false; 2165 return outer.children.getIndex(cid) == tool_row_index; 2166 } 2167 2168 fn focusTag(self: *This, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 2169 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 2170 // keep the tag visible on the terminal: its rect offset by the flow's 2171 // position in the pane. 2172 if (self.session.is_terminal and item < tf.rects.items.len) { 2173 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 2174 var rect = tf.rects.items[item]; 2175 rect.x += flow_rect.x; 2176 rect.y += flow_rect.y; 2177 self.detailScroll(index).scrollToRect(rect); 2178 } 2179 } 2180 } 2181 2182 fn titleFocused(self: *This, index: usize) bool { 2183 const inner = self.detailInner(index); 2184 const cid = inner.getFocus().child_id orelse return false; 2185 return inner.children.getIndex(cid) == title_child_index; 2186 } 2187 2188 fn authorFocused(self: *This, index: usize) bool { 2189 const inner = self.detailInner(index); 2190 const cid = inner.getFocus().child_id orelse return false; 2191 return cid == self.author_id[index]; 2192 } 2193 2194 fn descriptionFocused(self: *This, index: usize) bool { 2195 const cid = self.detailInner(index).getFocus().child_id orelse return false; 2196 return cid == self.description_id[index]; 2197 } 2198 2199 // return to the tool row's last-focused button (the header when the 2200 // description page shows no row). 2201 fn focusToolRow(self: *This, index: usize, root_focus: *Focus) void { 2202 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 2203 root_focus.setFocus(cid); 2204 } 2205 2206 // enter the detail pane. an empty pane (no issues) can't be entered. 2207 fn focusDetail(self: *This, index: usize, root_focus: *Focus) !void { 2208 if (self.detailInner(index).children.count() == 0) return; 2209 root_focus.setFocus(self.detailOuter(index).getFocus().id); 2210 } 2211 2212 // return to the list. 2213 fn focusList(self: *This, index: usize, root_focus: *Focus) !void { 2214 root_focus.setFocus(self.listScroll(index).getFocus().id); 2215 } 2216 2217 // cross to the header tabs above the stack. 2218 fn focusHeader(self: *This, root_focus: *Focus) void { 2219 root_focus.setFocus(self.box.children.keys()[header_index]); 2220 } 2221 2222 pub fn clearGrid(self: *This) void { 2223 self.box.clearGrid(); 2224 } 2225 2226 pub fn getGrid(self: This) ?Grid { 2227 return self.box.getGrid(); 2228 } 2229 2230 pub fn getFocus(self: *This) *Focus { 2231 return self.box.getFocus(); 2232 } 2233 2234 // for the parent's "scroll up at the top jumps to the header" check: at the 2235 // top only while the header tabs hold focus, so up from the split 2236 // crosses into the tabs first. 2237 pub fn getSelectedIndex(self: *This) ?usize { 2238 return if (self.headerActive()) 0 else 1; 2239 } 2240 }; 2241 } 2242