╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1721 .mute => try notify.mute(io, allocator, users_dir, &user_id, location, &thread_id, actor.author), 1722 } 1723 try self.session.navigate(ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, entry.id, 0) orelse return error.RouteTooLong); 1724 } 1725 1726 fn toggleStatus(self: *This, allocator: std.mem.Allocator) !void { 1727 if (comptime (!has_status or wasm)) return; 1728 const entry = self.entry orelse return; 1729 const io = self.session.io orelse return; 1730 const source = self.data.repo_source orelse return; 1731 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 1732 const author = actor.author; 1733 const status = (statusChange(entryStatus(entry)) orelse return).status; 1734 const id = try evt.parseEventId(entry.id); 1735 if (!try source.canModify(io, allocator, actor, kind, &id)) return; 1736 1737 switch (source.repo_kind) { 1738 inline else => |repo_kind| { 1739 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, source.localInitOpts()); 1740 defer any_repo.deinit(io, allocator); 1741 switch (any_repo) { 1742 inline else => |*repo| try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id, .{ .status = status }, author), 1743 } 1744 }, 1745 } 1746 1747 const route = listRoute(self.data.identity, status, entry.id) orelse return; 1748 try self.session.navigate(route); 1749 } 1750 }; 1751 } 1752 1753 pub fn View(comptime kind: evt.EventKind, comptime Data: type) type { 1754 const Self = Data; 1755 const HeaderType = Data.Header; 1756 const Window = Data.Window; 1757 const Entry = Data.Entry; 1758 const Event = Data.Event; 1759 const feedback_tag: std.meta.Tag(ui.Session.FormFeedback) = switch (kind) { 1760 .issue => .issue, 1761 .patch => .patch, 1762 .discuss => .discussion, 1763 else => unreachable, 1764 }; 1765 const FeedbackFailure = if (kind == .patch) ui.Session.FormFeedback.PatchFailure else ui.Session.FormFeedback.ThreadFailure; 1766 const Status = Data.Status; 1767 const has_status = @hasField(Event, "status"); 1768 const supports_conflicts = Event.merge_policy == .field_conflicts; 1769 const supports_drafts = @hasField(Self, "drafts"); 1770 const supports_merge = @hasDecl(Data, "mergeRoute"); 1771 const FieldConflict = if (supports_conflicts) Data.FieldConflict else void; 1772 const ViewKind = Data.ViewKind; 1773 const thread_name = Data.thread_name; 1774 const DetailType = Data.Detail; 1775 1776 return struct { 1777 const This = @This(); 1778 // a vertical box: the header tabs on top, then a stack holding a 1779 // master-detail split (thread list + description pane) per status list, 1780 // plus the labels view. 1781 box: wgt.Box(Widget), // vert: [header_index] = tabs, [stack_index] = stack 1782 data: *const Self, 1783 session: *ui.Session, 1784 1785 const header_index: usize = 0; 1786 const stack_index: usize = 1; 1787 // indices within the stack, 1:1 with the header tabs. 1788 const status_count = @typeInfo(Status).@"enum".field_names.len; 1789 const labels_view_index: usize = status_count; 1790 // the new-thread form, or the edit, comment, or resolve form when the page was 1791 // loaded at one of their urls. 1792 const form_view_index: usize = labels_view_index + 1; 1793 // the conflicts split; the tab and stack child exist only when the repo 1794 // has conflicted threads. 1795 const drafts_view_index: usize = form_view_index + 1; 1796 const conflict_view_index: usize = drafts_view_index + @intFromBool(supports_drafts); 1797 const view_count: usize = conflict_view_index + @intFromBool(supports_conflicts); 1798 // indices within a split (the horizontal box inside the stack). 1799 const list_index: usize = 0; 1800 const detail_index: usize = 1; 1801 const list_max_width: usize = 35; 1802 const detail_min_width: usize = 40; 1803 1804 pub fn viewIndex(view: ViewKind) usize { 1805 const name = @tagName(view); 1806 if (std.mem.eql(u8, name, "labels")) return labels_view_index; 1807 if (std.mem.eql(u8, name, "new") or std.mem.eql(u8, name, "edit") or std.mem.eql(u8, name, "publish") or std.mem.eql(u8, name, "merge") or std.mem.eql(u8, name, "new_comment") or std.mem.eql(u8, name, "edit_comment") or std.mem.eql(u8, name, "remove") or std.mem.eql(u8, name, "resolve")) return form_view_index; 1808 if (std.mem.eql(u8, name, "conflicts")) return conflict_view_index; 1809 if (std.mem.eql(u8, name, "drafts")) return drafts_view_index; 1810 if (std.mem.eql(u8, name, "description")) unreachable; 1811 inline for (@typeInfo(Status).@"enum".field_names, 0..) |status_name, index| { 1812 if (std.mem.eql(u8, name, status_name)) return index; 1813 } 1814 unreachable; 1815 } 1816 1817 const Confirmation = enum { remove, publish, merge }; 1818 1819 fn confirmation(view: ViewKind) ?Confirmation { 1820 if (view == .remove) return .remove; 1821 if (supports_drafts and std.mem.eql(u8, @tagName(view), "publish")) return .publish; 1822 if (supports_merge and std.mem.eql(u8, @tagName(view), "merge")) return .merge; 1823 return null; 1824 } 1825 1826 // the status a status split lists; the conflicts split has none (its 1827 // threads carry their own). 1828 fn splitStatus(index: usize) Status { 1829 return @fromBackingInt(@intCast(index)); 1830 } 1831 1832 fn entryConflicted(entry: Entry) bool { 1833 return if (supports_conflicts) entry.conflicted else false; 1834 } 1835 1836 fn entryStatus(entry: Entry) Status { 1837 if (comptime has_status) return Data.statusKind(entry.record.event); 1838 return @fromBackingInt(@intCast(0)); 1839 } 1840 1841 fn entryDraft(entry: Entry) bool { 1842 return if (supports_drafts) entry.draft else false; 1843 } 1844 1845 fn listRoute(identity: []const u8, status: Status, selected: []const u8) ?ui.RoutablePage { 1846 return Data.listRoute(identity, status, selected); 1847 } 1848 1849 fn draftsRoute(identity: []const u8) ?ui.RoutablePage { 1850 return if (supports_drafts) Data.draftsRoute(identity) else null; 1851 } 1852 1853 fn publishRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 1854 return if (supports_drafts) Data.publishRoute(identity, selected) else null; 1855 } 1856 1857 fn mergeRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 1858 return if (supports_merge) Data.mergeRoute(identity, selected) else null; 1859 } 1860 1861 fn conflictsRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 1862 return if (supports_conflicts) Data.conflictsRoute(identity, selected) else null; 1863 } 1864 1865 fn resolveRoute(identity: []const u8, selected: []const u8, picks: []const u8) ?ui.RoutablePage { 1866 return if (supports_conflicts) Data.resolveRoute(identity, selected, picks) else null; 1867 } 1868 1869 // a list link keeps the search the page carries 1870 fn listLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: Status, id: []const u8, search: []const u8) ![]const u8 { 1871 var route = listRoute(identity, status, id) orelse return error.RouteTooLong; 1872 if (search.len != 0) route = route.withSearch(search) orelse return error.RouteTooLong; 1873 return page_arena.allocator().print("a:{s}", .{try route.toUrl(page_arena)}); 1874 } 1875 1876 fn rowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, id: []const u8) ![]const u8 { 1877 const selected = std.mem.eql(u8, id, data.selected_id); 1878 const route = (if (selected and data.description_page) 1879 ui.RoutablePage.repoThreadDescriptionRoute(kind, data.identity, id) 1880 else if (selected) 1881 if (data.comment_page) |page| 1882 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, id, &page.selected.id, page.replies.start) 1883 else 1884 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, id, data.comments_start) 1885 else 1886 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, id, 0)) orelse return error.RouteTooLong; 1887 return page_arena.allocator().print("ai:{s}", .{try route.toUrl(page_arena)}); 1888 } 1889 1890 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?Status, drafts: bool, id: []const u8) ![]const u8 { 1891 if (supports_conflicts and status_maybe == null and !drafts) { 1892 const route = conflictsRoute(data.identity, id) orelse return error.RouteTooLong; 1893 return page_arena.allocator().print("a:{s}", .{try route.toUrl(page_arena)}); 1894 } 1895 if (drafts and id.len == 0) { 1896 const route = draftsRoute(data.identity) orelse return error.RouteTooLong; 1897 return page_arena.allocator().print("a:{s}", .{try route.toUrl(page_arena)}); 1898 } 1899 return listLink(page_arena, data.identity, status_maybe orelse @fromBackingInt(@intCast(0)), id, data.search orelse ""); 1900 } 1901 1902 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !This { 1903 var outer = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 1904 errdefer outer.deinit(allocator); 1905 1906 // the tabs at the top. 1907 { 1908 var hdr = try Data.initHeader(allocator, session, data); 1909 errdefer hdr.deinit(allocator); 1910 try outer.children.put(allocator, hdr.getFocus().id, .{ .widget = @unionInit(Widget, Data.header_widget_name, hdr), .rect = null, .min_size = null }); 1911 } 1912 1913 // the stack enters `outer` before its children enter it, so an error 1914 // frees each child exactly once. 1915 { 1916 var stack = try wgt.Stack(Widget).init(allocator); 1917 errdefer stack.deinit(allocator); 1918 try outer.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 1919 } 1920 const stack = &outer.children.values()[stack_index].widget.stack; 1921 1922 inline for (@typeInfo(Status).@"enum".field_values) |value| { 1923 var split = try initSplit(allocator, session, data, @fromBackingInt(@intCast(value)), false); 1924 errdefer split.deinit(allocator); 1925 try stack.children.put(allocator, split.getFocus().id, split); 1926 } 1927 1928 // the labels view, or the empty message without labels 1929 if (labelsEmpty(data)) { 1930 var center = try widget.initEmpty(allocator); 1931 errdefer center.deinit(allocator); 1932 try stack.children.put(allocator, center.getFocus().id, .{ .center = center }); 1933 } else { 1934 var tf = try WordFlow.init(allocator); 1935 errdefer tf.deinit(allocator); 1936 var items: std.ArrayList(WordFlow.Item) = .empty; 1937 defer items.deinit(allocator); 1938 for (data.labels) |label| { 1939 const search = try srch_thrd.withFilter(session.page_arena.allocator(), data.search orelse "", srch_thrd.label_prefix, label); 1940 try items.append(allocator, .{ .text = label, .link = try listLink(session.page_arena, data.identity, @fromBackingInt(@intCast(0)), "", search) }); 1941 } 1942 try tf.setItems(allocator, items.items); 1943 try stack.children.put(allocator, tf.getFocus().id, .{ .word_flow = tf }); 1944 } 1945 1946 // the new-thread form, or — on an edit, comment, or resolve url — that form in 1947 // its place, prefilled with the selected thread's content. a viewer 1948 // who can't write in this tab gets the unauthorized view instead, 1949 // while local mode always writes. 1950 if (session.data.host_kind == .local or data.viewer != null) { 1951 if (confirmation(data.view)) |confirmation_kind| { 1952 const aa = session.page_arena.allocator(); 1953 const route = switch (confirmation_kind) { 1954 .remove => ui.RoutablePage.repoThreadRemoveRoute(kind, data.identity, data.selected_id, data.comment_id), 1955 .publish => publishRoute(data.identity, data.selected_id), 1956 .merge => mergeRoute(data.identity, data.selected_id), 1957 } orelse return error.RouteTooLong; 1958 const page_url = try route.toUrl(session.page_arena); 1959 const action = try aa.print("form:{s}", .{page_url}); 1960 const event_name = if (data.comment_id.len == 0) thread_name else "comment"; 1961 var label_buf: ["remove ".len + @max(thread_name.len, "comment".len)]u8 = undefined; 1962 var buttons: [2]widget.ConfirmationButton = undefined; 1963 buttons[0] = .{ .label = switch (confirmation_kind) { 1964 .remove => try std.fmt.bufPrint(&label_buf, "remove {s}", .{event_name}), 1965 .publish => "publish patch", 1966 .merge => "merge patch", 1967 } }; 1968 var button_count: usize = 1; 1969 if (confirmation_kind == .merge) { 1970 if (comptime supports_merge) { 1971 const availability = if (data.selectedThread()) |entry| 1972 if (Data.canMerge(entry.*, session)) entry.mergeability else pch.Mergeability{} 1973 else 1974 pch.Mergeability{}; 1975 buttons[0].bottom_label = mergeBottomLabel(availability.source); 1976 if (availability.source != .clean) buttons[0].action = ""; 1977 buttons[1] = .{ 1978 .label = "squash and merge patch", 1979 .bottom_label = mergeBottomLabel(availability.squash), 1980 .action = if (availability.squash == .clean) try aa.print("{s}{s}/squash", .{ ui.submit_action_prefix, page_url }) else "", 1981 }; 1982 button_count += 1; 1983 } else unreachable; 1984 } 1985 var center = try widget.initConfirmationForm(allocator, action, buttons[0..button_count]); 1986 errdefer center.deinit(allocator); 1987 try stack.children.put(allocator, center.getFocus().id, .{ .center = center }); 1988 } else if (data.view == .new_comment or data.view == .edit_comment) { 1989 const editing = data.view == .edit_comment; 1990 const route = if (editing) 1991 ui.RoutablePage.repoThreadCommentEditRoute(kind, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong 1992 else 1993 ui.RoutablePage.repoThreadCommentNewRoute(kind, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong; 1994 const page_url = try route.toUrl(session.page_arena); 1995 const action = try session.page_arena.allocator().print("form:{s}", .{page_url}); 1996 const page = data.comment_page; 1997 const top_level = if (page) |p| std.mem.eql(u8, &p.selected.comment.event.parent_id, &p.selected.comment.event.thread_id) else false; 1998 const parent_route = if (editing) 1999 if (top_level) 2000 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, data.selected_id, 0) 2001 else if (page) |p| 2002 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, data.selected_id, &p.selected.comment.event.parent_id, 0) 2003 else 2004 null 2005 else if (page) |p| 2006 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, data.selected_id, &p.selected.id, 0) 2007 else 2008 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, data.selected_id, 0); 2009 const parent_author: ui.Author = if (editing) 2010 if (top_level) 2011 if (data.selectedThread()) |entry| entry.author else .unknown 2012 else if (page) |p| 2013 p.selected.parent_author orelse .unknown 2014 else 2015 .unknown 2016 else if (page) |p| 2017 p.selected.author 2018 else if (data.selectedThread()) |entry| 2019 entry.author 2020 else 2021 .unknown; 2022 const initial_body: ?[]const u8 = if (editing) if (page) |p| p.selected.comment.event.body else null else null; 2023 var form = try initCommentForm(allocator, session, action, parent_author, parent_route orelse return error.RouteTooLong, initial_body); 2024 errdefer form.deinit(allocator); 2025 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 2026 } else if (supports_conflicts and data.view == .resolve) { 2027 // on the web the page grows to the form's height and the 2028 // browser scrolls it; the terminal scrolls the form itself 2029 var form_widget: Widget = blk: { 2030 var form = try initResolveForm(allocator, session, data); 2031 errdefer form.deinit(allocator); 2032 break :blk if (session.is_terminal) 2033 .{ .scroll = try wgt.Scroll(Widget).init(allocator, .{ .box = form }, .{ .direction = .vert }) } 2034 else 2035 .{ .box = form }; 2036 }; 2037 errdefer form_widget.deinit(allocator); 2038 try stack.children.put(allocator, form_widget.getFocus().id, form_widget); 2039 } else { 2040 const aa = session.page_arena.allocator(); 2041 const route = if (data.view == .edit) 2042 ui.RoutablePage.repoThreadEditRoute(kind, data.identity, data.selected_id) orelse return error.RouteTooLong 2043 else 2044 ui.RoutablePage.repoThreadNewRoute(kind, data.identity) orelse return error.RouteTooLong; 2045 const action = try aa.print("form:{s}", .{try route.toUrl(session.page_arena)}); 2046 const record = if (data.view == .edit) 2047 (if (data.selectedThread()) |entry| &entry.record else null) 2048 else 2049 null; 2050 var form = try initThreadForm(allocator, session, data, action, record); 2051 errdefer form.deinit(allocator); 2052 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 2053 } 2054 } else { 2055 var message = try ui.Unauthorized.View.init(allocator); 2056 errdefer message.deinit(allocator); 2057 try stack.children.put(allocator, message.getFocus().id, .{ .unauthorized = message }); 2058 } 2059 2060 if (supports_drafts) { 2061 var split = try initSplit(allocator, session, data, null, true); 2062 errdefer split.deinit(allocator); 2063 try stack.children.put(allocator, split.getFocus().id, split); 2064 } 2065 2066 // the conflicts split; its tab exists only when the repo has 2067 // conflicts, so skip the child too to keep the stack 1:1 with the 2068 // tabs. 2069 if (supports_conflicts and data.conflicts.count > 0) { 2070 var split = try initSplit(allocator, session, data, null, false); 2071 errdefer split.deinit(allocator); 2072 try stack.children.put(allocator, split.getFocus().id, split); 2073 } 2074 2075 // the stack starts on the page's view. 2076 stack.getFocus().child_id = stack.children.keys()[viewIndex(data.view)]; 2077 2078 // focus entering the view lands on the tabs first. 2079 outer.getFocus().child_id = outer.children.keys()[header_index]; 2080 2081 return .{ 2082 .box = outer, 2083 .data = data, 2084 .session = session, 2085 }; 2086 } 2087 2088 // the master-detail split showing `status`'s window, or the conflicts 2089 // window when null. an empty first window shows the empty message instead. 2090 fn initSplit(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, status_maybe: ?Status, drafts: bool) !Widget { 2091 const win = if (status_maybe) |status| data.window(status) else if (drafts and supports_drafts) &data.drafts else if (supports_conflicts) &data.conflicts else unreachable; 2092 if (windowEmpty(win)) return .{ .center = try widget.initEmpty(allocator) }; 2093 2094 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 2095 errdefer box.deinit(allocator); 2096 2097 // the thread list (one focusable row per title), plus a "next" link that 2098 // reloads the page rooted at the following thread. 2099 { 2100 var list_scroll = blk: { 2101 var list_box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert, .stretch = true }); 2102 errdefer list_box.deinit(allocator); 2103 if (win.prev_id) |prev| 2104 try addRow(allocator, &list_box, "← previous", "", try windowLink(session.page_arena, data, status_maybe, drafts, prev)); 2105 for (win.items) |entry| { 2106 const label: []const u8 = if (entry.comments.count > 0) 2107 try session.page_arena.allocator().print(" {d} comments ", .{entry.comments.count}) 2108 else 2109 ""; 2110 try addRow(allocator, &list_box, entry.record.event.title, label, try rowLink(session.page_arena, data, entry.id)); 2111 } 2112 if (win.next_id) |next| 2113 try addRow(allocator, &list_box, "next →", "", try windowLink(session.page_arena, data, status_maybe, drafts, next)); 2114 // select the window's first thread (past a leading "previous" 2115 // row) so its description shows on load. 2116 if (win.items.len > 0) 2117 list_box.getFocus().child_id = list_box.children.keys()[if (win.prev_id != null) 1 else 0] 2118 else if (list_box.children.count() > 0) 2119 list_box.getFocus().child_id = list_box.children.keys()[0]; 2120 break :blk try wgt.Scroll(Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal }); 2121 }; 2122 errdefer list_scroll.deinit(allocator); 2123 try box.children.put(allocator, list_scroll.getFocus().id, .{ .widget = .{ .scroll = list_scroll }, .rect = null, .min_size = .{ .width = list_max_width, .height = null }, .max_size = .{ .width = list_max_width, .height = null } }); 2124 } 2125 2126 // frame the detail pane inside the master-detail split, below the 2127 // way back to the list, shown only while the list is hidden 2128 { 2129 var frame = try wgt.Box(Widget).init(allocator, .{ .border = .hidden, .direction = .vert }); 2130 errdefer frame.deinit(allocator); 2131 frame.getFocus().mode = .mouse; 2132 try widget.addListBack(allocator, &frame, "← back to " ++ thread_name ++ " list"); 2133 var detail_view = try DetailType.init(allocator, data, session, null, .{}); 2134 errdefer detail_view.deinit(allocator); 2135 frame.getFocus().child_id = detail_view.getFocus().id; 2136 try frame.children.put(allocator, detail_view.getFocus().id, .{ .widget = @unionInit(Widget, Data.detail_widget_name, detail_view), .rect = null, .min_size = null }); 2137 try box.children.put(allocator, frame.getFocus().id, .{ .widget = .{ .box = frame }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 2138 } 2139 2140 // the page opens on the list, unless the url names a thread 2141 box.getFocus().child_id = box.children.keys()[if (data.selected_id.len > 0) detail_index else list_index]; 2142 return .{ .box = box }; 2143 } 2144 2145 // a thread form, plus the target branch for patches, prefilled from 2146 // `record` when given. its form: subtree makes the web renderer POST 2147 // the inputs to `action`'s route. 2148 fn initThreadForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, action: []const u8, record: ?*const Event.Record) !wgt.Box(Widget) { 2149 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 2150 errdefer box.deinit(allocator); 2151 box.getFocus().kind = .{ .custom = action }; 2152 2153 const saved_form = session.formFeedback(feedback_tag); 2154 const saved_fields = if (saved_form) |saved| saved.fields else null; 2155 2156 { 2157 var title = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " title " }, .name = "title", .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 2158 errdefer title.deinit(allocator); 2159 title.getFocus().mode = .all; 2160 if (saved_fields) |saved| 2161 try title.setContent(allocator, saved.title) 2162 else if (record) |r| 2163 try title.setContent(allocator, r.event.title); 2164 try box.children.put(allocator, title.getFocus().id, .{ .widget = .{ .text_input = title }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2165 box.getFocus().child_id = title.getFocus().id; 2166 } 2167 2168 { 2169 var labels = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " labels (separate with spaces) " }, .name = "labels", .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 2170 errdefer labels.deinit(allocator); 2171 labels.getFocus().mode = .all; 2172 if (saved_fields) |saved| 2173 try labels.setContent(allocator, saved.labels) 2174 else if (record) |r| 2175 try labels.setContent(allocator, r.event.labels); 2176 try box.children.put(allocator, labels.getFocus().id, .{ .widget = .{ .text_input = labels }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2177 } 2178 2179 { 2180 var description = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " description (markdown supported) " }, .name = "description", .visible_width = null, .round_corners = true, .render_content = session.is_terminal, .multiline = true, .scroll = .{ .fill = true } }); 2181 errdefer description.deinit(allocator); 2182 description.getFocus().mode = .all; 2183 if (saved_fields) |saved| 2184 try description.setContent(allocator, saved.description) 2185 else if (record) |r| 2186 try description.setContent(allocator, r.event.description); 2187 try box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .text_input = description }, .rect = null, .min_size = null }); 2188 } 2189 2190 if (supports_drafts and record == null) { 2191 var source = try Data.Source.init(allocator, session, if (saved_fields) |saved| saved.source_branch else "", if (saved_fields) |saved| saved.source_kind else .fork); 2192 errdefer source.deinit(allocator); 2193 try box.children.put(allocator, source.getFocus().id, .{ .widget = .{ .patch_source = source }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2194 } 2195 2196 if (supports_drafts) { 2197 var target = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " target branch " }, .name = "target_branch", .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 2198 errdefer target.deinit(allocator); 2199 target.getFocus().mode = .all; 2200 const content = if (saved_fields) |saved| saved.target_branch else if (record) |r| r.event.target_branch else data.default_target_branch; 2201 try target.setContent(allocator, content); 2202 try box.children.put(allocator, target.getFocus().id, .{ .widget = .{ .text_input = target }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2203 } 2204 2205 try addSubmitButtonLabeled(allocator, &box, if (supports_drafts and record == null) "submit draft" else "submit"); 2206 2207 return box; 2208 } 2209 2210 fn initCommentForm(allocator: std.mem.Allocator, session: *ui.Session, action: []const u8, author: ui.Author, parent_route: ui.RoutablePage, initial_body: ?[]const u8) !wgt.Box(Widget) { 2211 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 2212 errdefer box.deinit(allocator); 2213 box.getFocus().kind = .{ .custom = action }; 2214 2215 { 2216 var parent = try ui.authorBox(allocator, session.page_arena, author); 2217 errdefer parent.deinit(allocator); 2218 parent.options.top_label.text = " replying to "; 2219 parent.getFocus().kind = .{ .custom = try session.page_arena.allocator().print("a:{s}", .{try parent_route.toUrl(session.page_arena)}) }; 2220 try box.children.put(allocator, parent.getFocus().id, .{ .widget = .{ .text_box = parent }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2221 box.getFocus().child_id = parent.getFocus().id; 2222 } 2223 2224 { 2225 var body = try wgt.TextInput.init(allocator, .{ 2226 .top_label = .{ .text = " comment (markdown supported) " }, 2227 .name = "body", 2228 .visible_width = null, 2229 .round_corners = true, 2230 .render_content = session.is_terminal, 2231 .multiline = true, 2232 .visible_height = 5, 2233 .scroll = .{ .fill = true }, 2234 }); 2235 errdefer body.deinit(allocator); 2236 body.getFocus().mode = .all; 2237 if (initial_body) |text| try body.setContent(allocator, text); 2238 try box.children.put(allocator, body.getFocus().id, .{ .widget = .{ .text_input = body }, .rect = null, .min_size = null }); 2239 } 2240 2241 try addSubmitButtonLabeled(allocator, &box, "submit"); 2242 return box; 2243 } 2244 2245 // a form's submit button, then a spacer absorbing the leftover 2246 // min-height the box hands its last child, so the button keeps its 2247 // natural height 2248 fn addSubmitButtonLabeled(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8) !void { 2249 { 2250 var submit = try SubmitButton.initLabeled(allocator, label); 2251 errdefer submit.deinit(allocator); 2252 try box.children.put(allocator, submit.getFocus().id, .{ .widget = .{ .submit_button = submit }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2253 } 2254 { 2255 var spacer = try Spacer.init(allocator); 2256 errdefer spacer.deinit(allocator); 2257 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 2258 } 2259 } 2260 2261 // the conflict resolve form. the "use this" links are navigations that 2262 // flip the url's theirs: pick, reloading the prefills; one submit 2263 // settles every conflict. the form: subtree makes the web renderer POST 2264 // to the resolve route. 2265 fn initResolveForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !wgt.Box(Widget) { 2266 const aa = session.page_arena.allocator(); 2267 // init only picks the resolve view once it has read the conflict 2268 const conflict = if (data.conflict) |*c| c else unreachable; 2269 2270 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 2271 errdefer box.deinit(allocator); 2272 const route = resolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong; 2273 box.getFocus().kind = .{ .custom = try aa.print("form:{s}", .{try route.toUrl(session.page_arena)}) }; 2274 2275 if (conflict.title) |*fc| { 2276 try addLabel(allocator, &box, "title conflict:"); 2277 try addFieldConflict(allocator, &box, session, data, "title", fc); 2278 } 2279 if (conflict.labels) |*fc| { 2280 try addLabel(allocator, &box, "labels conflict:"); 2281 try addFieldConflict(allocator, &box, session, data, "labels", fc); 2282 } 2283 if (comptime @hasField(Data.Conflict, "status")) { 2284 if (conflict.status) |*fc| { 2285 try addLabel(allocator, &box, "status conflict:"); 2286 try addAtomicConflict(allocator, &box, session, data, "status", fc); 2287 } 2288 } 2289 if (comptime @hasField(Data.Conflict, "revision")) { 2290 if (conflict.revision) |*fc| { 2291 try addLabel(allocator, &box, "revision conflict:"); 2292 try addAtomicConflict(allocator, &box, session, data, "revision", fc); 2293 } 2294 if (conflict.target_branch) |*fc| { 2295 try addLabel(allocator, &box, "target branch conflict:"); 2296 try addAtomicConflict(allocator, &box, session, data, "target_branch", fc); 2297 } 2298 } 2299 2300 if (conflict.description) |*desc| { 2301 try addLabel(allocator, &box, "description conflict:"); 2302 var hunk_index: usize = 0; 2303 for (desc.chunks, 0..) |*chunk, chunk_index| { 2304 switch (chunk.*) { 2305 .same => |text| { 2306 var tb = try wgt.TextBox.init(allocator, text, .{ .border = .single, .round_corners = true, .wrap_kind = .word }); 2307 errdefer tb.deinit(allocator); 2308 tb.getFocus().mode = .all; 2309 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2310 }, 2311 // an auto-resolved chunk: only one side changed it, so its 2312 // version stands, shown distinctly with its author 2313 .auto => |auto| { 2314 const author = if (auto.theirs) desc.theirs_author else desc.ours_author; 2315 const verb: []const u8 = if (auto.text == null) "removed" else "edited"; 2316 const label = switch (author) { 2317 .user_name, .email => |name| try aa.print(" {s} by {s} ", .{ verb, name }), 2318 .unknown => try aa.print(" {s} by {s} ", .{ verb, if (auto.theirs) @as([]const u8, "them") else "us" }), 2319 }; 2320 var tb = try wgt.TextBox.init(allocator, auto.text orelse "(removed)", .{ .border = .single, .round_corners = true, .wrap_kind = .word, .top_label = .{ .text = label } }); 2321 errdefer tb.deinit(allocator); 2322 tb.getFocus().mode = .all; 2323 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2324 }, 2325 .conflict => |hunk| { 2326 // a blank row on each side sets the group apart from 2327 // the flowing shared and auto-resolved chunks 2328 if (chunk_index > 0) try addGap(allocator, &box); 2329 const name = try aa.print("d{d}", .{hunk_index}); 2330 hunk_index += 1; 2331 const picked_theirs = data.theirsPicked(name); 2332 try addVersionRow(allocator, &box, try sideLabel(aa, desc.ours_author, true), hunk.ours orelse "", try useThisLink(session, data, name, false)); 2333 try addVersionRow(allocator, &box, try sideLabel(aa, desc.theirs_author, false), hunk.theirs orelse "", try useThisLink(session, data, name, true)); 2334 var resolution_input = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " resolution " }, .name = name, .visible_width = null, .round_corners = true, .render_content = session.is_terminal, .multiline = true }); 2335 errdefer resolution_input.deinit(allocator); 2336 resolution_input.getFocus().mode = .all; 2337 try resolution_input.setContent(allocator, (if (picked_theirs) hunk.theirs else hunk.ours) orelse ""); 2338 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 2339 if (chunk_index + 1 < desc.chunks.len) try addGap(allocator, &box); 2340 }, 2341 } 2342 } 2343 } 2344 2345 try addSubmitButtonLabeled(allocator, &box, "submit"); 2346 2347 box.getFocus().child_id = box.children.keys()[0]; 2348 return box; 2349 } 2350 2351 // one conflicted scalar field: both sides' version rows, then the 2352 // resolution input prefilled from the picked side. 2353 fn addFieldConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 2354 const aa = session.page_arena.allocator(); 2355 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 2356 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 2357 2358 var resolution_input = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " resolution " }, .name = name, .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 2359 errdefer resolution_input.deinit(allocator); 2360 resolution_input.getFocus().mode = .all; 2361 try resolution_input.setContent(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text); 2362 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 2363 } 2364 2365 // atomic fields can only pick one complete side 2366 fn addAtomicConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 2367 const aa = session.page_arena.allocator(); 2368 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 2369 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 2370 var selected = try wgt.TextBox.init(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text, .{ 2371 .border = .single, 2372 .round_corners = true, 2373 .wrap_kind = .word, 2374 .top_label = .{ .text = " resolution " }, 2375 }); 2376 errdefer selected.deinit(allocator); 2377 selected.getFocus().mode = .all; 2378 try box.children.put(allocator, selected.getFocus().id, .{ .widget = .{ .text_box = selected }, .rect = null, .min_size = null }); 2379 } 2380 2381 // a blank row setting a conflict group apart from its neighbors 2382 fn addGap(allocator: std.mem.Allocator, box: *wgt.Box(Widget)) !void { 2383 var text = try wgt.Text.init(allocator, " "); 2384 errdefer text.deinit(allocator); 2385 try box.children.put(allocator, text.getFocus().id, .{ .widget = .{ .text = text }, .rect = null, .min_size = null }); 2386 } 2387 2388 fn addLabel(allocator: std.mem.Allocator, box: *wgt.Box(Widget), content: []const u8) !void { 2389 var label = try SectionLabel.init(allocator, content); 2390 errdefer label.deinit(allocator); 2391 try box.children.put(allocator, label.getFocus().id, .{ .widget = .{ .section_label = label }, .rect = null, .min_size = null }); 2392 } 2393 2394 // one side of a conflict: the "use this" link on the left, the version 2395 // itself as a labeled read-only box. 2396 fn addVersionRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8, text: []const u8, link: []const u8) !void { 2397 var row = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 2398 errdefer row.deinit(allocator); 2399 2400 { 2401 const use_label = "use this"; 2402 var use = try wgt.TextBox.init(allocator, use_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 2403 errdefer use.deinit(allocator); 2404 use.getFocus().mode = .all; 2405 use.getFocus().kind = .{ .custom = link }; 2406 try row.children.put(allocator, use.getFocus().id, .{ 2407 .widget = .{ .text_box = use }, 2408 .rect = null, 2409 .min_size = .{ .width = use_label.len + 2, .height = null }, 2410 .max_size = .{ .width = use_label.len + 2, .height = null }, 2411 }); 2412 } 2413 2414 { 2415 var tb = try wgt.TextBox.init(allocator, if (text.len > 0) text else "(removed)", .{ .border = .single, .round_corners = true, .wrap_kind = .word, .top_label = .{ .text = label } }); 2416 errdefer tb.deinit(allocator); 2417 tb.getFocus().mode = .all; 2418 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2419 } 2420 2421 row.getFocus().child_id = row.children.keys()[0]; 2422 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 2423 } 2424 2425 // the label of one side's version box, naming its author 2426 fn sideLabel(aa: std.mem.Allocator, author: ui.Author, ours: bool) ![]const u8 { 2427 return switch (author) { 2428 .user_name, .email => |name| try aa.print(" edited by {s} ", .{name}), 2429 .unknown => if (ours) " current version " else " their version ", 2430 }; 2431 } 2432 2433 // the a: link that reloads the resolve page with `name` picked from ours 2434 // or theirs, reprefilling its resolution input 2435 fn useThisLink(session: *ui.Session, data: *const Self, name: []const u8, theirs: bool) ![]const u8 { 2436 const aa = session.page_arena.allocator(); 2437 var picks: std.ArrayList(u8) = .empty; 2438 var it = std.mem.splitScalar(u8, data.theirs_picks, ','); 2439 while (it.next()) |pick| { 2440 if (pick.len == 0 or std.mem.eql(u8, pick, name)) continue; 2441 if (picks.items.len > 0) try picks.append(aa, ','); 2442 try picks.appendSlice(aa, pick); 2443 } 2444 if (theirs) { 2445 if (picks.items.len > 0) try picks.append(aa, ','); 2446 try picks.appendSlice(aa, name); 2447 } 2448 const route = resolveRoute(data.identity, data.selected_id, picks.items) orelse return error.RouteTooLong; 2449 return aa.print("a:{s}", .{try route.toUrl(session.page_arena)}); 2450 } 2451 2452 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), text: []const u8, bottom_label: []const u8, link: []const u8) !void { 2453 var row = try wgt.TextBox.init(allocator, text, .{ .border = .hidden, .round_corners = true, .wrap_kind = .word, .bottom_label = .{ .text = bottom_label } }); 2454 errdefer row.deinit(allocator); 2455 row.getFocus().mode = .all; 2456 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 2457 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 2458 } 2459 2460 pub fn deinit(self: *This, allocator: std.mem.Allocator) void { 2461 self.box.deinit(allocator); 2462 } 2463 2464 fn header(self: *This) *HeaderType { 2465 return &@field(self.box.children.values()[header_index].widget, Data.header_widget_name); 2466 } 2467 2468 fn viewStack(self: *This) *wgt.Stack(Widget) { 2469 return &self.box.children.values()[stack_index].widget.stack; 2470 } 2471 2472 // `index`'s master-detail split inside the stack. 2473 fn resultsBox(self: *This, index: usize) *wgt.Box(Widget) { 2474 return &self.viewStack().children.values()[index].box; 2475 } 2476 2477 // the labels view's flow inside the stack. 2478 fn labelsView(self: *This) *WordFlow { 2479 return &self.viewStack().children.values()[labels_view_index].word_flow; 2480 } 2481 2482 // no labels, which shows the empty message 2483 fn labelsEmpty(data: *const Self) bool { 2484 return data.labels.len == 0; 2485 } 2486 2487 // the non-confirmation form inside the stack 2488 fn formBox(self: *This) ?*wgt.Box(Widget) { 2489 return switch (self.viewStack().children.values()[form_view_index]) { 2490 .box => |*box| box, 2491 // the resolve form sits inside a scroll on the terminal 2492 .scroll => |*scroll| &scroll.child.box, 2493 else => null, 2494 }; 2495 } 2496 2497 fn threadForm(self: *This) ?*wgt.Box(Widget) { 2498 if (supports_conflicts and self.data.view == .resolve) return null; 2499 return switch (self.data.view) { 2500 .new_comment, .edit_comment => null, 2501 else => self.formBox(), 2502 }; 2503 } 2504 2505 fn formField(form: *wgt.Box(Widget), name: []const u8) !*wgt.TextInput { 2506 for (form.children.values()) |*child| switch (child.widget) { 2507 .text_input => |*text_input| if (std.mem.eql(u8, text_input.options.name, name)) return text_input, 2508 else => {}, 2509 }; 2510 return error.MissingFormField; 2511 } 2512 2513 fn formWidget(form: *wgt.Box(Widget), comptime widget_kind: std.meta.Tag(Widget)) ?*@FieldType(Widget, @tagName(widget_kind)) { 2514 for (form.children.values()) |*child| { 2515 if (child.widget == widget_kind) return &@field(child.widget, @tagName(widget_kind)); 2516 } 2517 return null; 2518 } 2519 2520 fn confirmationForm(self: *This) ?*wgt.Box(Widget) { 2521 return switch (self.viewStack().children.values()[form_view_index]) { 2522 .center => |*center| switch (center.child.*) { 2523 .box => |*box| box, 2524 else => null, 2525 }, 2526 else => null, 2527 }; 2528 } 2529 2530 // the resolve form's scroll on the terminal (the web page scrolls itself) 2531 fn resolveScroll(self: *This) ?*wgt.Scroll(Widget) { 2532 return switch (self.viewStack().children.values()[form_view_index]) { 2533 .scroll => |*scroll| scroll, 2534 else => null, 2535 }; 2536 } 2537 2538 fn listScroll(self: *This, index: usize) *wgt.Scroll(Widget) { 2539 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 2540 } 2541 2542 fn listBox(self: *This, index: usize) *wgt.Box(Widget) { 2543 return &self.listScroll(index).child.box; 2544 } 2545 2546 fn detailFrame(self: *This, index: usize) *wgt.Box(Widget) { 2547 return &self.resultsBox(index).children.values()[detail_index].widget.box; 2548 } 2549 2550 fn detail(self: *This, index: usize) *DetailType { 2551 return &@field(self.detailFrame(index).children.values()[widget.list_pane_index].widget, Data.detail_widget_name); 2552 } 2553 2554 fn window(self: *This, index: usize) *const Window { 2555 if (supports_conflicts and index == conflict_view_index) return &self.data.conflicts; 2556 if (supports_drafts and index == drafts_view_index) return &self.data.drafts; 2557 return self.data.window(splitStatus(index)); 2558 } 2559 2560 // an empty first window, which shows the empty message 2561 fn windowEmpty(win: *const Window) bool { 2562 return win.items.len == 0 and win.prev_id == null; 2563 } 2564 2565 fn stackSelectedIndex(self: *This) ?usize { 2566 const stack = self.viewStack(); 2567 const cid = stack.getFocus().child_id orelse return null; 2568 return stack.children.getIndex(cid); 2569 } 2570 2571 // the stack's selected master-detail split (null when the labels view, the 2572 // new-thread form, or the empty message shows). 2573 fn selectedSplitIndex(self: *This) ?usize { 2574 const idx = self.stackSelectedIndex() orelse return null; 2575 const split = idx < status_count or (supports_drafts and idx == drafts_view_index) or idx == conflict_view_index; 2576 return if (split and !windowEmpty(self.window(idx))) idx else null; 2577 } 2578 2579 fn detailActive(self: *This, index: usize) bool { 2580 const rb = self.resultsBox(index); 2581 const cid = rb.getFocus().child_id orelse return false; 2582 return rb.children.getIndex(cid) == detail_index; 2583 } 2584 2585 fn headerActive(self: *This) bool { 2586 const cid = self.box.getFocus().child_id orelse return false; 2587 return self.box.children.getIndex(cid) == header_index; 2588 } 2589 2590 fn labelsViewActive(self: *This) bool { 2591 if (self.headerActive()) return false; 2592 return self.stackSelectedIndex() == labels_view_index; 2593 } 2594 2595 fn formViewActive(self: *This) bool { 2596 if (self.headerActive()) return false; 2597 return self.stackSelectedIndex() == form_view_index; 2598 } 2599 2600 // the selected thread's index, or null when a window-navigation row is 2601 // selected (a leading "previous" row shifts the thread rows down by one). 2602 fn selectedThreadIndex(self: *This, index: usize) ?usize { 2603 const lb = self.listBox(index); 2604 const cid = lb.getFocus().child_id orelse return null; 2605 const idx = lb.children.getIndex(cid) orelse return null; 2606 const win = self.window(index); 2607 const lead: usize = if (win.prev_id != null) 1 else 0; 2608 if (idx < lead or idx - lead >= win.items.len) return null; 2609 return idx - lead; 2610 } 2611 2612 // swap the detail pane to the selected thread when it changes 2613 fn refreshDetail(self: *This, allocator: std.mem.Allocator, index: usize) !void { 2614 const selected = self.selectedThreadIndex(index) orelse return; 2615 const entry = self.window(index).items[selected]; 2616 const changed = if (self.detail(index).entry) |current| !std.mem.eql(u8, current.id, entry.id) else true; 2617 if (!changed) return; 2618 try self.detail(index).setEntry(allocator, entry); 2619 widget.setListBackLink(self.detailFrame(index), try rowLink(self.session.page_arena, self.data, entry.id)); 2620 } 2621 2622 pub fn build(self: *This, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2623 self.clearGrid(); 2624 2625 // each header tab maps 1:1 to a stack child by position 2626 if (self.header().getSelectedIndex()) |index| { 2627 const stack = self.viewStack(); 2628 stack.getFocus().child_id = stack.children.keys()[index]; 2629 } 2630 2631 if (self.selectedSplitIndex()) |i| { 2632 // whether the detail pane fits beside the list, which decides 2633 // the list cap below and whether the detail carries a way back 2634 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 2635 try self.refreshDetail(allocator, i); 2636 widget.showListBack(self.detailFrame(i), !both_panes_fit); 2637 2638 // the selected list row shows a border (the focused TextBox 2639 // upgrades it to a double border itself); the rest stay borderless. 2640 const lb = self.listBox(i); 2641 for (lb.children.keys(), lb.children.values()) |id, *child| { 2642 switch (child.widget) { 2643 .text_box => |*tb| widget.markSelected(tb, lb.getFocus().child_id == id), 2644 else => {}, 2645 } 2646 } 2647 2648 // cap the list at list_max_width only while the detail pane fits 2649 // beside it. the box drops the detail when the width can't hold 2650 // both minimums, so when it's that narrow we lift the cap and let 2651 // the list fill the whole width. 2652 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 2653 2654 // stretch the detail pane across the rest of the width so it fills 2655 // the area rather than shrinking to its content; its scroll fills 2656 // the pane. 2657 if (constraint.max_size.width) |w| { 2658 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 2659 } else { 2660 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 2661 } 2662 } 2663 2664 // refresh the form inputs' entries in the session's focus-id -> input 2665 // map with this frame's addresses, so the web/wasm form handling can 2666 // find them by focus id 2667 if (self.threadForm()) |form| { 2668 const failure = if (self.session.formFeedback(feedback_tag)) |saved| saved.failure else null; 2669 (try formField(form, "title")).options.top_label.text = if (failure == .required_title) " title (required) " else " title "; 2670 if (comptime supports_drafts) { 2671 if (formWidget(form, .patch_source)) |source| { 2672 source.field().options.top_label.text = if (failure == .invalid_source_branch) " source branch (not found) " else if (failure == .same_branch) " source branch (same as target) " else if (failure == .unrelated_branches) " source branch (unrelated) " else " source branch "; 2673 const submit = formWidget(form, .submit_button) orelse return error.MissingFormField; 2674 try submit.setLabel(allocator, switch (source.selectedKind()) { 2675 .fork => "submit draft", 2676 .branch => "submit patch", 2677 }); 2678 } 2679 (try formField(form, "target_branch")).options.top_label.text = 2680 if (failure == .invalid_target_branch) " target branch (invalid) " else if (self.data.view == .edit and failure == .same_branch) " target branch (same as source) " else if (self.data.view == .edit and failure == .unrelated_branches) " target branch (unrelated to source) " else if (self.data.view == .edit and failure == .invalid_source_branch) " target branch (source branch not found) " else " target branch "; 2681 } 2682 } 2683 if (self.formBox()) |form| { 2684 const inputs_arena = self.session.arena.allocator(); 2685 for (form.children.values()) |*child| switch (child.widget) { 2686 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 2687 else => {}, 2688 }; 2689 } 2690 2691 try self.box.build(allocator, constraint, root_focus); 2692 } 2693 2694 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 2695 // wheel input moves focus like the arrows 2696 const key: Key = switch (raw_key) { 2697 .scroll_up => .arrow_up, 2698 .scroll_down => .arrow_down, 2699 else => raw_key, 2700 }; 2701 2702 if (self.headerActive()) { 2703 // down from the sub-header re-enters the stack if the selected 2704 // view has something to focus; enter in the search box submits 2705 // the query; other keys move within the sub-header. 2706 if (inp.vertDirection(key) == .down) { 2707 const enterable = if (self.stackSelectedIndex()) |idx| 2708 if (idx == labels_view_index) 2709 !labelsEmpty(self.data) 2710 else if (idx == form_view_index) 2711 // nothing focusable in the unauthorized view 2712 self.viewStack().children.values()[idx] != .unauthorized 2713 else 2714 !windowEmpty(self.window(idx)) 2715 else 2716 false; 2717 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 2718 } else if (key == .enter) { 2719 if (try self.header().submittedText(allocator)) |text| { 2720 defer allocator.free(text); 2721 try self.submitSearch(text); 2722 } 2723 } else { 2724 try self.header().input(allocator, key, root_focus); 2725 } 2726 return; 2727 } 2728 if (self.labelsViewActive()) { 2729 self.labelsViewInput(key, root_focus); 2730 return; 2731 } 2732 if (self.formViewActive()) { 2733 if (confirmation(self.data.view) != null) { 2734 try self.confirmationFormInput(allocator, key, root_focus); 2735 } else if (supports_conflicts and self.data.view == .resolve) { 2736 try self.resolveInput(allocator, key, root_focus); 2737 } else { 2738 try self.formInput(allocator, key, root_focus); 2739 } 2740 return; 2741 } 2742 const i = self.selectedSplitIndex() orelse return; 2743 if (self.detailActive(i)) { 2744 // the way back to the list, by enter or a click 2745 if (widget.listBackId(self.detailFrame(i))) |id| { 2746 if (inp.activated(root_focus, id, key)) return self.focusList(i, root_focus); 2747 if (root_focus.grandchild_id == id) return switch (key) { 2748 .arrow_left => self.focusList(i, root_focus), 2749 .arrow_up => self.focusHeader(root_focus), 2750 .arrow_down => _ = self.detail(i).focusFirst(root_focus), 2751 else => {}, 2752 }; 2753 } 2754 switch (try self.detail(i).inputWithExit(allocator, key, root_focus)) { 2755 .none => {}, 2756 .list => self.focusList(i, root_focus), 2757 .header => if (widget.listBackId(self.detailFrame(i))) |id| root_focus.setFocus(id) else self.focusHeader(root_focus), 2758 } 2759 } else { 2760 try self.listInput(allocator, i, key, root_focus); 2761 } 2762 } 2763 2764 // navigate to the typed query's results, keeping the status list the 2765 // page shows. an empty box clears a filtered page and does nothing on 2766 // any other one. 2767 fn submitSearch(self: *This, text: []const u8) !void { 2768 if (text.len == 0 and self.data.search == null) return; 2769 const selected = self.header().getSelectedIndex() orelse 0; 2770 const status: Status = if (selected < status_count) splitStatus(selected) else @fromBackingInt(@intCast(0)); 2771 const route = listRoute(self.data.identity, status, "") orelse return; 2772 try self.session.navigate(route.withSearch(text) orelse return); 2773 } 2774 2775 // arrow keys move the label selection; up from the top row crosses to the 2776 // header tabs. 2777 fn labelsViewInput(self: *This, key: Key, root_focus: *Focus) void { 2778 const tf = self.labelsView(); 2779 const cid = tf.focus.child_id orelse return; 2780 const cur = tf.indexOfFocusId(cid) orelse return; 2781 const count = tf.text_boxes.items.len; 2782 switch (key) { 2783 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 2784 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 2785 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 2786 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 2787 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 2788 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 2789 else => {}, 2790 } 2791 } 2792 2793 fn listInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 2794 // up/down (and the scroll wheel) move the selection a row; page up/down 2795 // jump a fixed amount. right/Enter cross into the detail pane. up from 2796 // the top row crosses into the header tabs. 2797 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 2798 const lb = self.listBox(index); 2799 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 2800 if (delta < 0 and at_top) return self.focusHeader(root_focus); 2801 moveRowFocus(lb, self.listScroll(index), root_focus, delta); 2802 return; 2803 } 2804 switch (key) { 2805 .enter, .arrow_right => self.focusDetail(index, root_focus), 2806 // when the window is too narrow to lay out the detail pane beside 2807 // the list, a click on a row opens it like enter. the row was just 2808 // selected, so the detail is swapped to it ahead of the build. 2809 .mouse => |mouse| if (self.resultsBox(index).children.values()[detail_index].rect == null and widget.clickOnSelectedRow(self.listBox(index), root_focus, mouse)) { 2810 try self.refreshDetail(allocator, index); 2811 self.focusDetail(index, root_focus); 2812 }, 2813 else => {}, 2814 } 2815 } 2816 2817 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2818 const form = self.formBox() orelse return; 2819 const cid = form.getFocus().child_id orelse return; 2820 const cur = form.children.getIndex(cid) orelse return; 2821 const child = &form.children.values()[cur]; 2822 2823 if (child.widget == .text_input) { 2824 const text_input = &child.widget.text_input; 2825 if (text_input.options.multiline and try multilineKeepsKey(text_input, allocator, key)) 2826 return child.widget.input(allocator, key, root_focus); 2827 } 2828 2829 const on_submit = child.widget == .submit_button; 2830 switch (key) { 2831 .arrow_up, .back_tab => return if (formStep(form, cur, false)) |i| 2832 root_focus.setFocus(form.children.keys()[i]) 2833 else 2834 self.focusHeader(root_focus), 2835 .arrow_down, .tab => return if (formStep(form, cur, true)) |i| root_focus.setFocus(form.children.keys()[i]), 2836 .enter => if (on_submit) return self.submitForm(allocator), 2837 .mouse => |mouse| if (on_submit) { 2838 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) return self.submitForm(allocator); 2839 }, 2840 else => {}, 2841 } 2842 try child.widget.input(allocator, key, root_focus); 2843 } 2844 2845 fn confirmationFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2846 const form = self.confirmationForm() orelse return; 2847 const child_id = form.getFocus().child_id orelse return; 2848 const index = form.children.getIndex(child_id) orelse return; 2849 switch (key) { 2850 .arrow_up, .back_tab => if (index > 1) 2851 root_focus.setFocus(form.children.keys()[index - 1]) 2852 else 2853 self.focusHeader(root_focus), 2854 .arrow_down, .tab => if (index + 1 < form.children.count()) root_focus.setFocus(form.children.keys()[index + 1]), 2855 .enter => try self.submitConfirmation(allocator, index), 2856 .mouse => |mouse| for (form.children.keys()[1..], 1..) |button_id, button_index| { 2857 if (inp.leftClickOn(root_focus, button_id, mouse)) { 2858 try self.submitConfirmation(allocator, button_index); 2859 break; 2860 } 2861 }, 2862 else => {}, 2863 } 2864 } 2865 2866 // the multiline hunk inputs keep enter and any up/down that has a row to 2867 // move to; enter on a "use this" link is a navigation the host follows 2868 // before this runs. 2869 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2870 const form = self.formBox() orelse return; 2871 const cid = form.getFocus().child_id orelse return; 2872 const cur = form.children.getIndex(cid) orelse return; 2873 const child = &form.children.values()[cur]; 2874 2875 if (child.widget == .text_input) { 2876 const ti = &child.widget.text_input; 2877 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2878 return child.widget.input(allocator, key, root_focus); 2879 } 2880 2881 const on_submit = child.widget == .submit_button; 2882 2883 switch (key) { 2884 .arrow_up, .back_tab => if (formStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2885 .arrow_down, .tab => if (formStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2886 .arrow_left, .arrow_right => switch (child.widget) { 2887 .box => |*row| _ = widget.moveInRow(row, root_focus, key == .arrow_right), 2888 else => try child.widget.input(allocator, key, root_focus), 2889 }, 2890 .enter => if (on_submit) try self.submitResolution(allocator), 2891 .mouse => |mouse| if (on_submit) { 2892 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2893 }, 2894 else => try child.widget.input(allocator, key, root_focus), 2895 } 2896 } 2897 2898 // a multiline input keeps enter and any up/down that has a row to move to 2899 fn multilineKeepsKey(text_input: *wgt.TextInput, allocator: std.mem.Allocator, key: Key) !bool { 2900 return switch (key) { 2901 .enter => true, 2902 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2903 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2904 else => false, 2905 }; 2906 } 2907 2908 // the neighboring form control, skipping spacers and blank rows 2909 fn formStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2910 var i = cur; 2911 while (true) { 2912 if (down) { 2913 i += 1; 2914 if (i >= form.children.count()) return null; 2915 } else { 2916 if (i == 0) return null; 2917 i -= 1; 2918 } 2919 const child = &form.children.values()[i]; 2920 if (child.hidden) continue; 2921 switch (child.widget) { 2922 .spacer, .text => continue, 2923 else => return i, 2924 } 2925 } 2926 } 2927 2928 // focus the resolve-form child at `i` (a version row focuses its selected 2929 // child) and keep it visible on the terminal 2930 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2931 const child = &form.children.values()[i]; 2932 switch (child.widget) { 2933 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2934 else => root_focus.setFocus(form.children.keys()[i]), 2935 } 2936 if (self.session.is_terminal) { 2937 if (self.resolveScroll()) |sc| { 2938 if (child.rect) |rect| sc.scrollToRect(rect); 2939 } 2940 } 2941 } 2942 2943 // re-emit the conflicted thread's event with the resolved content; any 2944 // event on the thread settles its conflict entry. this is the terminal 2945 // path; the web posts the form to the resolve route. 2946 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2947 if (comptime wasm) return; 2948 const io = self.session.io orelse return; 2949 const src = self.data.repo_source orelse return; 2950 const entry = self.data.selectedThread() orelse return; 2951 const author = ((try self.session.authorize(self.data.identity, .write, kind)) orelse return).author; 2952 const form = self.formBox() orelse return; 2953 2954 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2955 var title: ?[]u8 = null; 2956 var labels: ?[]u8 = null; 2957 var hunks: std.ArrayList([]const u8) = .empty; 2958 defer { 2959 if (title) |t| allocator.free(t); 2960 if (labels) |t| allocator.free(t); 2961 for (hunks.items) |h| allocator.free(h); 2962 hunks.deinit(allocator); 2963 } 2964 for (form.children.values()) |*child| switch (child.widget) { 2965 .text_input => |*ti| { 2966 const text = try ti.text(allocator); 2967 errdefer allocator.free(text); 2968 if (std.mem.eql(u8, ti.options.name, "title")) { 2969 title = text; 2970 } else if (std.mem.eql(u8, ti.options.name, "labels")) { 2971 labels = text; 2972 } else { 2973 try hunks.append(allocator, text); 2974 } 2975 }, 2976 else => {}, 2977 }; 2978 2979 const id_bytes = try evt.parseEventId(entry.id); 2980 2981 switch (src.repo_kind) { 2982 inline else => |repo_kind| { 2983 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2984 defer any_repo.deinit(io, allocator); 2985 switch (any_repo) { 2986 inline else => |*repo| { 2987 var resolution: Event.Resolve = .{ 2988 .title = title, 2989 .labels = labels, 2990 .hunks = hunks.items, 2991 }; 2992 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2993 const change = Event.Update{ .resolve = resolution }; 2994 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2995 // leave the form up for correction 2996 error.InvalidFields => return, 2997 else => |e| return e, 2998 }; 2999 }, 3000 } 3001 }, 3002 } 3003 3004 const route = listRoute(self.data.identity, entryStatus(entry.*), entry.id) orelse return; 3005 try self.session.navigate(route); 3006 } 3007 3008 // commit a new or edited comment and navigate to its permalink. this is the 3009 // terminal path; the web posts the form to its comment route. 3010 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 3011 if (comptime wasm) return; 3012 const io = self.session.io orelse return; 3013 const src = self.data.repo_source orelse return; 3014 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3015 const author = actor.author; 3016 const form = self.formBox() orelse return; 3017 const body_input = try formField(form, "body"); 3018 const body = try body_input.text(allocator); 3019 defer allocator.free(body); 3020 if (!evt.Comment.fieldsValid(body)) return; 3021 3022 var thread_id: [evt.event_id_size * 2]u8 = undefined; 3023 @memcpy(&thread_id, self.data.selected_id); 3024 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 3025 var parent: [evt.event_id_size * 2]u8 = undefined; 3026 @memcpy(&parent, self.data.comment_id); 3027 break :blk parent; 3028 }; 3029 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 3030 const editing = self.data.view == .edit_comment; 3031 if (editing) { 3032 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 3033 if (!try src.canModify(io, allocator, actor, .comment, &comment_id)) return; 3034 } 3035 switch (src.repo_kind) { 3036 inline else => |repo_kind| { 3037 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3038 defer any_repo.deinit(io, allocator); 3039 switch (any_repo) { 3040 inline else => |*repo| if (editing) { 3041 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 3042 try evt.Comment.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, &comment_id, body, author); 3043 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 3044 } else { 3045 event_id_hex = try evt.Comment.create(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, &parent_id, body, author); 3046 }, 3047 } 3048 }, 3049 } 3050 3051 body_input.clear(allocator); 3052 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 3053 try self.session.navigate(route); 3054 } 3055 3056 // terminal form submissions; the web posts to the forms' routes instead 3057 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 3058 switch (self.data.view) { 3059 .new_comment, .edit_comment => try self.submitComment(allocator), 3060 .edit => try self.submitEditedThread(allocator), 3061 else => try self.submitNewThread(allocator), 3062 } 3063 } 3064 3065 // commit the new thread to the repo's events branch and navigate to it. 3066 // this is the terminal path; the web posts the form to the thread route, 3067 // so the wasm side never runs (or compiles) the repo access below. 3068 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 3069 if (comptime wasm) return; 3070 const io = self.session.io orelse return; 3071 const src = self.data.repo_source orelse return; 3072 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3073 3074 const form = self.threadForm() orelse return; 3075 const title_input = try formField(form, "title"); 3076 const labels_input = try formField(form, "labels"); 3077 const description_input = try formField(form, "description"); 3078 3079 const title = try title_input.text(allocator); 3080 defer allocator.free(title); 3081 const labels = try labels_input.text(allocator); 3082 defer allocator.free(labels); 3083 const description = try description_input.text(allocator); 3084 defer allocator.free(description); 3085 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 3086 defer if (comptime supports_drafts) allocator.free(target_branch); 3087 3088 if (!Event.fieldsValid(title, labels)) { 3089 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 3090 return; 3091 } 3092 3093 if (comptime supports_drafts) { 3094 const source = formWidget(form, .patch_source) orelse return error.MissingFormField; 3095 const source_branch = switch (source.selectedKind()) { 3096 .fork => null, 3097 .branch => try source.field().text(allocator), 3098 }; 3099 defer if (source_branch) |branch| allocator.free(branch); 3100 const event_id_hex = Data.create(self.data, self.session, allocator, actor, title, labels, description, target_branch, source_branch) catch |err| { 3101 const failure = FeedbackFailure.fromError(err) orelse return err; 3102 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3103 return; 3104 }; 3105 title_input.clear(allocator); 3106 labels_input.clear(allocator); 3107 description_input.clear(allocator); 3108 (try formField(form, "target_branch")).clear(allocator); 3109 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 3110 try self.session.navigate(route); 3111 return; 3112 } 3113 3114 var id_bytes: [evt.event_id_size]u8 = undefined; 3115 io.random(&id_bytes); 3116 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 3117 3118 const event = evt.EventWithId{ 3119 .id = event_id_hex, 3120 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 3121 .author = actor.author, 3122 .event = @unionInit(evt.Event, @tagName(kind), Event{ 3123 .title = title, 3124 .description = description, 3125 .labels = labels, 3126 }), 3127 }; 3128 3129 switch (src.repo_kind) { 3130 inline else => |repo_kind| { 3131 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3132 defer any_repo.deinit(io, allocator); 3133 switch (any_repo) { 3134 inline else => |*repo| try evt.consume(self.session.eventHost(), .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 3135 } 3136 }, 3137 } 3138 3139 // wipe the form so a return visit starts fresh 3140 title_input.clear(allocator); 3141 labels_input.clear(allocator); 3142 description_input.clear(allocator); 3143 3144 const route = listRoute(self.data.identity, @fromBackingInt(@intCast(0)), &event_id_hex) orelse return; 3145 try self.session.navigate(route); 3146 } 3147 3148 // re-emit the selected thread's event with the form's content (status 3149 // preserved), then reload the thread's page. this is the terminal path; 3150 // the web posts the form to the edit route. 3151 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 3152 if (comptime wasm) return; 3153 const io = self.session.io orelse return; 3154 const entry = self.data.selectedThread() orelse return; 3155 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3156 const author = actor.author; 3157 3158 const form = self.threadForm() orelse return; 3159 const title_input = try formField(form, "title"); 3160 const labels_input = try formField(form, "labels"); 3161 const description_input = try formField(form, "description"); 3162 3163 const title = try title_input.text(allocator); 3164 defer allocator.free(title); 3165 const labels = try labels_input.text(allocator); 3166 defer allocator.free(labels); 3167 const description = try description_input.text(allocator); 3168 defer allocator.free(description); 3169 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 3170 defer if (comptime supports_drafts) allocator.free(target_branch); 3171 3172 if (!Event.fieldsValid(title, labels)) { 3173 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 3174 return; 3175 } 3176 const src = self.data.repo_source orelse return; 3177 3178 if (comptime supports_drafts) { 3179 if (entryDraft(entry.*)) { 3180 self.data.editDraft(self.session, allocator, author, entry.id, title, labels, description, target_branch) catch |err| { 3181 const failure = FeedbackFailure.fromError(err) orelse return err; 3182 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3183 return; 3184 }; 3185 const route = listRoute(self.data.identity, entryStatus(entry.*), entry.id) orelse return; 3186 try self.session.navigate(route); 3187 return; 3188 } 3189 } 3190 3191 const id_bytes = try evt.parseEventId(entry.id); 3192 if (!try src.canModify(io, allocator, actor, kind, &id_bytes)) return; 3193 3194 switch (src.repo_kind) { 3195 inline else => |repo_kind| { 3196 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3197 defer any_repo.deinit(io, allocator); 3198 switch (any_repo) { 3199 inline else => |*repo| if (supports_drafts) { 3200 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 3201 .title = title, 3202 .labels = labels, 3203 .description = description, 3204 .target_branch = target_branch, 3205 } }, author) catch |err| { 3206 const failure = FeedbackFailure.fromError(err) orelse return err; 3207 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3208 return; 3209 }; 3210 } else if (has_status) 3211 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 3212 .title = title, 3213 .labels = labels, 3214 .description = description, 3215 } }, author) 3216 else 3217 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, labels, description, author), 3218 } 3219 }, 3220 } 3221 3222 const route = listRoute(self.data.identity, entryStatus(entry.*), entry.id) orelse return; 3223 try self.session.navigate(route); 3224 } 3225 3226 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, labels: []const u8, description: []const u8, target_branch: []const u8) !void { 3227 const aa = self.session.arena.allocator(); 3228 const source = if (supports_drafts) blk: { 3229 const form = self.threadForm() orelse break :blk null; 3230 break :blk formWidget(form, .patch_source); 3231 } else null; 3232 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 3233 .failure = failure, 3234 .fields = if (comptime supports_drafts) .{ 3235 .title = try aa.dupe(u8, title), 3236 .labels = try aa.dupe(u8, labels), 3237 .description = try aa.dupe(u8, description), 3238 .target_branch = try aa.dupe(u8, target_branch), 3239 .source_branch = if (source) |value| try value.field().text(aa) else "", 3240 .source_kind = if (source) |value| value.selectedKind() else .fork, 3241 } else .{ 3242 .title = try aa.dupe(u8, title), 3243 .labels = try aa.dupe(u8, labels), 3244 .description = try aa.dupe(u8, description), 3245 }, 3246 }); 3247 } 3248 3249 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 3250 switch (confirmation(self.data.view) orelse return) { 3251 .publish => if (comptime supports_drafts) { 3252 if (comptime wasm) return; 3253 const entry = self.data.selectedThread() orelse return; 3254 if (!entryDraft(entry.*)) return; 3255 const author = ((try self.session.authorize(self.data.identity, .read, kind)) orelse return).author; 3256 try self.data.publishDraft(self.session, allocator, author, entry.id); 3257 const route = listRoute(self.data.identity, @fromBackingInt(@intCast(0)), entry.id) orelse return; 3258 try self.session.navigate(route); 3259 } else unreachable, 3260 .merge => if (comptime supports_merge) { 3261 if (comptime wasm) return; 3262 const entry = self.data.selectedThread() orelse return; 3263 const revision: evt.Patch.MergeRevision = if (button_index > 1) .squash else .source; 3264 if (!Data.canMerge(entry.*, self.session) or entry.mergeability.get(revision) != .clean) return; 3265 const author = ((try self.session.authorize(self.data.identity, .write, kind)) orelse return).author; 3266 self.data.mergePatch(self.session, allocator, author, entry.id, revision) catch |err| switch (err) { 3267 error.MergeConflict, error.MergeCheckUnavailable, error.PatchOutOfDate, error.PatchDataUnavailable => { 3268 const route = listRoute(self.data.identity, entryStatus(entry.*), entry.id) orelse return; 3269 try self.session.navigate(route); 3270 return; 3271 }, 3272 else => return err, 3273 }; 3274 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 3275 const route = listRoute(self.data.identity, merged, entry.id) orelse return; 3276 try self.session.navigate(route); 3277 } else unreachable, 3278 .remove => try self.submitRemove(allocator), 3279 } 3280 } 3281 3282 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 3283 const comment = self.data.comment_id.len != 0; 3284 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 3285 const id = evt.parseEventId(event_id) catch return; 3286 try self.removeEvent(allocator, if (comment) .comment else kind, id); 3287 } 3288 3289 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 3290 if (comptime wasm) return; 3291 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3292 const author = actor.author; 3293 3294 if (comptime supports_drafts) { 3295 const selected = self.data.selectedThread() orelse return; 3296 if (event_kind == kind and entryDraft(selected.*)) { 3297 try Data.removeDraft(self.session, allocator, author, &id); 3298 const route = draftsRoute(self.data.identity) orelse return; 3299 try self.session.navigate(route); 3300 return; 3301 } 3302 } 3303 3304 const io = self.session.io orelse return; 3305 const src = self.data.repo_source orelse return; 3306 if (!try src.canModify(io, allocator, actor, event_kind, &id)) return; 3307 const thread_id = evt.parseEventId(self.data.selected_id) catch return; 3308 3309 switch (src.repo_kind) { 3310 inline else => |repo_kind| { 3311 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3312 defer any_repo.deinit(io, allocator); 3313 switch (any_repo) { 3314 inline else => |*repo| try evt.removeInThread(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, event_kind, &id, author), 3315 } 3316 }, 3317 } 3318 3319 const id_hex = std.fmt.bytesToHex(id, .lower); 3320 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex, null) orelse return; 3321 try self.session.navigate(route); 3322 } 3323 3324 // enter the detail pane, landing on the way back to the list when it shows 3325 fn focusDetail(self: *This, index: usize, root_focus: *Focus) void { 3326 if (widget.listBackId(self.detailFrame(index)) != null) return widget.focusListBack(self.detailFrame(index), root_focus); 3327 _ = self.detail(index).focusFirst(root_focus); 3328 } 3329 3330 // return to the list. 3331 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 3332 root_focus.setFocus(self.listScroll(index).getFocus().id); 3333 } 3334 3335 // cross to the header tabs above the stack. 3336 fn focusHeader(self: *This, root_focus: *Focus) void { 3337 root_focus.setFocus(self.box.children.keys()[header_index]); 3338 } 3339 3340 pub fn clearGrid(self: *This) void { 3341 self.box.clearGrid(); 3342 } 3343 3344 pub fn getGrid(self: This) ?Grid { 3345 return self.box.getGrid(); 3346 } 3347 3348 pub fn getFocus(self: *This) *Focus { 3349 return self.box.getFocus(); 3350 } 3351 3352 // for the parent's "scroll up at the top jumps to the header" check: at the 3353 // top only while the header tabs hold focus, so up from the split 3354 // crosses into the tabs first. 3355 pub fn atTop(self: *This) bool { 3356 return self.headerActive(); 3357 } 3358 }; 3359 } 3360