╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1714 action: []const u8 = "submit", 1715 }; 1716 1717 fn initConfirmationForm(allocator: std.mem.Allocator, action: []const u8, buttons: []const ConfirmationButton) !Center { 1718 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .rounded_corners = true, .direction = .vert }); 1719 errdefer box.deinit(allocator); 1720 box.getFocus().kind = .{ .custom = action }; 1721 1722 var prompt = try wgt.Text.init(allocator, "are you sure?"); 1723 errdefer prompt.deinit(allocator); 1724 try box.children.put(allocator, prompt.getFocus().id, .{ .widget = .{ .text = prompt }, .rect = null, .min_size = null }); 1725 1726 for (buttons, 0..) |options, index| { 1727 var button = try wgt.TextBox.init(allocator, options.label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .bottom_label = options.bottom_label }); 1728 errdefer button.deinit(allocator); 1729 button.getFocus().mode = .all; 1730 button.getFocus().kind = .{ .custom = options.action }; 1731 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 1732 if (index == 0) box.getFocus().child_id = button.getFocus().id; 1733 } 1734 1735 return Center.init(allocator, .{ .box = box }); 1736 } 1737 1738 // a form's submit button, then a spacer absorbing the leftover 1739 // min-height the box hands its last child, so the button keeps its 1740 // natural height 1741 fn addSubmitButtonLabeled(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8) !void { 1742 { 1743 var submit = try SubmitButton.initLabeled(allocator, label); 1744 errdefer submit.deinit(allocator); 1745 try box.children.put(allocator, submit.getFocus().id, .{ .widget = .{ .submit_button = submit }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1746 } 1747 { 1748 var spacer = try Spacer.init(allocator); 1749 errdefer spacer.deinit(allocator); 1750 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1751 } 1752 } 1753 1754 // the conflict resolve form. the "use this" links are navigations that 1755 // flip the url's theirs: pick, reloading the prefills; one submit 1756 // settles every conflict. the form: subtree makes the web renderer POST 1757 // to the resolve route. 1758 fn initResolveForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !wgt.Box(Widget) { 1759 const aa = session.page_arena.allocator(); 1760 // init only picks the resolve view once it has read the conflict 1761 const conflict = if (data.conflict) |*c| c else unreachable; 1762 1763 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1764 errdefer box.deinit(allocator); 1765 const route = resolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong; 1766 box.getFocus().kind = .{ .custom = try std.fmt.allocPrint(aa, "form:{s}", .{try route.toUrl(session.page_arena)}) }; 1767 1768 if (conflict.title) |*fc| { 1769 try addLabel(allocator, &box, "title conflict:"); 1770 try addFieldConflict(allocator, &box, session, data, "title", fc); 1771 } 1772 if (conflict.tags) |*fc| { 1773 try addLabel(allocator, &box, "tags conflict:"); 1774 try addFieldConflict(allocator, &box, session, data, "tags", fc); 1775 } 1776 if (comptime @hasField(Data.Conflict, "status")) { 1777 if (conflict.status) |*fc| { 1778 try addLabel(allocator, &box, "status conflict:"); 1779 try addAtomicConflict(allocator, &box, session, data, "status", fc); 1780 } 1781 } 1782 if (comptime @hasField(Data.Conflict, "revision")) { 1783 if (conflict.revision) |*fc| { 1784 try addLabel(allocator, &box, "revision conflict:"); 1785 try addAtomicConflict(allocator, &box, session, data, "revision", fc); 1786 } 1787 if (conflict.target_branch) |*fc| { 1788 try addLabel(allocator, &box, "target branch conflict:"); 1789 try addAtomicConflict(allocator, &box, session, data, "target_branch", fc); 1790 } 1791 } 1792 1793 if (conflict.description) |*desc| { 1794 try addLabel(allocator, &box, "description conflict:"); 1795 var hunk_index: usize = 0; 1796 for (desc.chunks, 0..) |*chunk, chunk_index| { 1797 switch (chunk.*) { 1798 .same => |text| { 1799 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word }); 1800 errdefer tb.deinit(allocator); 1801 tb.getFocus().mode = .all; 1802 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1803 }, 1804 // an auto-resolved chunk: only one side changed it, so its 1805 // version stands, shown distinctly with its author 1806 .auto => |auto| { 1807 const author = if (auto.theirs) desc.theirs_author else desc.ours_author; 1808 const verb: []const u8 = if (auto.text == null) "removed" else "edited"; 1809 const label = switch (author) { 1810 .user_name, .email => |name| try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, name }), 1811 .unknown => try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, if (auto.theirs) @as([]const u8, "them") else "us" }), 1812 }; 1813 var tb = try wgt.TextBox.init(allocator, auto.text orelse "(removed)", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = label }); 1814 errdefer tb.deinit(allocator); 1815 tb.getFocus().mode = .all; 1816 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1817 }, 1818 .conflict => |hunk| { 1819 // a blank row on each side sets the group apart from 1820 // the flowing shared and auto-resolved chunks 1821 if (chunk_index > 0) try addGap(allocator, &box); 1822 const name = try std.fmt.allocPrint(aa, "d{d}", .{hunk_index}); 1823 hunk_index += 1; 1824 const picked_theirs = data.theirsPicked(name); 1825 try addVersionRow(allocator, &box, try sideLabel(aa, desc.ours_author, true), hunk.ours orelse "", try useThisLink(session, data, name, false)); 1826 try addVersionRow(allocator, &box, try sideLabel(aa, desc.theirs_author, false), hunk.theirs orelse "", try useThisLink(session, data, name, true)); 1827 var resolution_input = try wgt.TextInput.init(allocator, .{ .label = " resolution ", .name = name, .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal, .multiline = true }); 1828 errdefer resolution_input.deinit(allocator); 1829 resolution_input.getFocus().mode = .all; 1830 try resolution_input.setContent(allocator, (if (picked_theirs) hunk.theirs else hunk.ours) orelse ""); 1831 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 1832 if (chunk_index + 1 < desc.chunks.len) try addGap(allocator, &box); 1833 }, 1834 } 1835 } 1836 } 1837 1838 try addSubmitButtonLabeled(allocator, &box, "submit"); 1839 1840 box.getFocus().child_id = box.children.keys()[0]; 1841 return box; 1842 } 1843 1844 // one conflicted scalar field: both sides' version rows, then the 1845 // resolution input prefilled from the picked side. 1846 fn addFieldConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 1847 const aa = session.page_arena.allocator(); 1848 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 1849 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 1850 1851 var resolution_input = try wgt.TextInput.init(allocator, .{ .label = " resolution ", .name = name, .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 1852 errdefer resolution_input.deinit(allocator); 1853 resolution_input.getFocus().mode = .all; 1854 try resolution_input.setContent(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text); 1855 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 1856 } 1857 1858 // atomic fields can only pick one complete side 1859 fn addAtomicConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 1860 const aa = session.page_arena.allocator(); 1861 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 1862 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 1863 var selected = try wgt.TextBox.init(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text, .{ 1864 .border_style = .single, 1865 .rounded_corners = true, 1866 .wrap_kind = .word, 1867 .label = " resolution ", 1868 }); 1869 errdefer selected.deinit(allocator); 1870 selected.getFocus().mode = .all; 1871 try box.children.put(allocator, selected.getFocus().id, .{ .widget = .{ .text_box = selected }, .rect = null, .min_size = null }); 1872 } 1873 1874 // a blank row setting a conflict group apart from its neighbors 1875 fn addGap(allocator: std.mem.Allocator, box: *wgt.Box(Widget)) !void { 1876 var text = try wgt.Text.init(allocator, " "); 1877 errdefer text.deinit(allocator); 1878 try box.children.put(allocator, text.getFocus().id, .{ .widget = .{ .text = text }, .rect = null, .min_size = null }); 1879 } 1880 1881 fn addLabel(allocator: std.mem.Allocator, box: *wgt.Box(Widget), content: []const u8) !void { 1882 var label = try SectionLabel.init(allocator, content); 1883 errdefer label.deinit(allocator); 1884 try box.children.put(allocator, label.getFocus().id, .{ .widget = .{ .section_label = label }, .rect = null, .min_size = null }); 1885 } 1886 1887 // one side of a conflict: the "use this" link on the left, the version 1888 // itself as a labeled read-only box. 1889 fn addVersionRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8, text: []const u8, link: []const u8) !void { 1890 var row = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1891 errdefer row.deinit(allocator); 1892 1893 { 1894 const use_label = "use this"; 1895 var use = try wgt.TextBox.init(allocator, use_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1896 errdefer use.deinit(allocator); 1897 use.getFocus().mode = .all; 1898 use.getFocus().kind = .{ .custom = link }; 1899 try row.children.put(allocator, use.getFocus().id, .{ 1900 .widget = .{ .text_box = use }, 1901 .rect = null, 1902 .min_size = .{ .width = use_label.len + 2, .height = null }, 1903 .max_size = .{ .width = use_label.len + 2, .height = null }, 1904 }); 1905 } 1906 1907 { 1908 var tb = try wgt.TextBox.init(allocator, if (text.len > 0) text else "(removed)", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = label }); 1909 errdefer tb.deinit(allocator); 1910 tb.getFocus().mode = .all; 1911 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1912 } 1913 1914 row.getFocus().child_id = row.children.keys()[0]; 1915 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 1916 } 1917 1918 // the label of one side's version box, naming its author 1919 fn sideLabel(aa: std.mem.Allocator, author: ui.Author, ours: bool) ![]const u8 { 1920 return switch (author) { 1921 .user_name, .email => |name| try std.fmt.allocPrint(aa, " edited by {s} ", .{name}), 1922 .unknown => if (ours) " current version " else " their version ", 1923 }; 1924 } 1925 1926 // the a: link that reloads the resolve page with `name` picked from ours 1927 // or theirs, reprefilling its resolution input 1928 fn useThisLink(session: *ui.Session, data: *const Self, name: []const u8, theirs: bool) ![]const u8 { 1929 const aa = session.page_arena.allocator(); 1930 var picks: std.ArrayList(u8) = .empty; 1931 var it = std.mem.splitScalar(u8, data.theirs_picks, ','); 1932 while (it.next()) |pick| { 1933 if (pick.len == 0 or std.mem.eql(u8, pick, name)) continue; 1934 if (picks.items.len > 0) try picks.append(aa, ','); 1935 try picks.appendSlice(aa, pick); 1936 } 1937 if (theirs) { 1938 if (picks.items.len > 0) try picks.append(aa, ','); 1939 try picks.appendSlice(aa, name); 1940 } 1941 const route = resolveRoute(data.identity, data.selected_id, picks.items) orelse return error.RouteTooLong; 1942 return std.fmt.allocPrint(aa, "a:{s}", .{try route.toUrl(session.page_arena)}); 1943 } 1944 1945 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), text: []const u8, bottom_label: []const u8, link: []const u8) !void { 1946 var row = try wgt.TextBox.init(allocator, text, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word, .bottom_label = bottom_label }); 1947 errdefer row.deinit(allocator); 1948 row.getFocus().mode = .all; 1949 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 1950 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 1951 } 1952 1953 pub fn deinit(self: *This, allocator: std.mem.Allocator) void { 1954 self.box.deinit(allocator); 1955 } 1956 1957 fn header(self: *This) *HeaderType { 1958 return &@field(self.box.children.values()[header_index].widget, Data.header_widget_name); 1959 } 1960 1961 fn viewStack(self: *This) *wgt.Stack(Widget) { 1962 return &self.box.children.values()[stack_index].widget.stack; 1963 } 1964 1965 // `index`'s master-detail split inside the stack. 1966 fn resultsBox(self: *This, index: usize) *wgt.Box(Widget) { 1967 return &self.viewStack().children.values()[index].box; 1968 } 1969 1970 // the tags view's flow inside the stack. 1971 fn tagsView(self: *This) *TagFlow { 1972 return &self.viewStack().children.values()[tags_view_index].tag_flow; 1973 } 1974 1975 // the non-confirmation form inside the stack 1976 fn formBox(self: *This) ?*wgt.Box(Widget) { 1977 return switch (self.viewStack().children.values()[form_view_index]) { 1978 .box => |*box| box, 1979 // the resolve form sits inside a scroll on the terminal 1980 .scroll => |*scroll| &scroll.child.box, 1981 else => null, 1982 }; 1983 } 1984 1985 fn threadForm(self: *This) ?*wgt.Box(Widget) { 1986 return switch (self.data.view) { 1987 .new, .edit => self.formBox(), 1988 else => null, 1989 }; 1990 } 1991 1992 fn confirmationForm(self: *This) ?*wgt.Box(Widget) { 1993 return switch (self.viewStack().children.values()[form_view_index]) { 1994 .center => |*center| switch (center.child.*) { 1995 .box => |*box| box, 1996 else => null, 1997 }, 1998 else => null, 1999 }; 2000 } 2001 2002 // the resolve form's scroll on the terminal (the web page scrolls itself) 2003 fn resolveScroll(self: *This) ?*wgt.Scroll(Widget) { 2004 return switch (self.viewStack().children.values()[form_view_index]) { 2005 .scroll => |*scroll| scroll, 2006 else => null, 2007 }; 2008 } 2009 2010 fn listScroll(self: *This, index: usize) *wgt.Scroll(Widget) { 2011 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 2012 } 2013 2014 fn listBox(self: *This, index: usize) *wgt.Box(Widget) { 2015 return &self.listScroll(index).child.box; 2016 } 2017 2018 fn detail(self: *This, index: usize) *DetailType { 2019 const frame = &self.resultsBox(index).children.values()[detail_index].widget.box; 2020 return &@field(frame.children.values()[0].widget, Data.detail_widget_name); 2021 } 2022 2023 fn window(self: *This, index: usize) *const Window { 2024 if (supports_conflicts and index == conflict_view_index) return &self.data.conflicts; 2025 if (supports_drafts and index == drafts_view_index) return &self.data.drafts; 2026 return self.data.window(splitStatus(index)); 2027 } 2028 2029 fn stackSelectedIndex(self: *This) ?usize { 2030 const stack = self.viewStack(); 2031 const cid = stack.getFocus().child_id orelse return null; 2032 return stack.children.getIndex(cid); 2033 } 2034 2035 // the stack's selected master-detail split (null when the tags view or the 2036 // new-thread form shows). 2037 fn selectedSplitIndex(self: *This) ?usize { 2038 const idx = self.stackSelectedIndex() orelse return null; 2039 return if (idx < status_count or (supports_drafts and idx == drafts_view_index) or idx == conflict_view_index) idx else null; 2040 } 2041 2042 fn detailActive(self: *This, index: usize) bool { 2043 const rb = self.resultsBox(index); 2044 const cid = rb.getFocus().child_id orelse return false; 2045 return rb.children.getIndex(cid) == detail_index; 2046 } 2047 2048 fn headerActive(self: *This) bool { 2049 const cid = self.box.getFocus().child_id orelse return false; 2050 return self.box.children.getIndex(cid) == header_index; 2051 } 2052 2053 fn tagsViewActive(self: *This) bool { 2054 if (self.headerActive()) return false; 2055 return self.stackSelectedIndex() == tags_view_index; 2056 } 2057 2058 fn formViewActive(self: *This) bool { 2059 if (self.headerActive()) return false; 2060 return self.stackSelectedIndex() == form_view_index; 2061 } 2062 2063 // the selected thread's index, or null when a window-navigation row is 2064 // selected (a leading "previous" row shifts the thread rows down by one). 2065 fn selectedThreadIndex(self: *This, index: usize) ?usize { 2066 const lb = self.listBox(index); 2067 const cid = lb.getFocus().child_id orelse return null; 2068 const idx = lb.children.getIndex(cid) orelse return null; 2069 const win = self.window(index); 2070 const lead: usize = if (win.prev_id != null) 1 else 0; 2071 if (idx < lead or idx - lead >= win.items.len) return null; 2072 return idx - lead; 2073 } 2074 2075 pub fn build(self: *This, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2076 self.clearGrid(); 2077 2078 // each header tab maps 1:1 to a stack child by position 2079 if (self.header().getSelectedIndex()) |index| { 2080 const stack = self.viewStack(); 2081 stack.getFocus().child_id = stack.children.keys()[index]; 2082 } 2083 2084 if (self.selectedSplitIndex()) |i| { 2085 // swap the detail pane to the selected thread when it changes. 2086 if (self.selectedThreadIndex(i)) |selected| { 2087 const entry = self.window(i).items[selected]; 2088 const changed = if (self.detail(i).entry) |current| !std.mem.eql(u8, current.id, entry.id) else true; 2089 if (changed) { 2090 try self.detail(i).setEntry(allocator, entry); 2091 } 2092 } 2093 2094 // the selected list row shows a border (the focused TextBox 2095 // upgrades it to a double border itself); the rest stay borderless. 2096 const lb = self.listBox(i); 2097 for (lb.children.keys(), lb.children.values()) |id, *child| { 2098 switch (child.widget) { 2099 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 2100 else => {}, 2101 } 2102 } 2103 2104 // cap the list at list_max_width only while the detail pane fits 2105 // beside it. the box drops the detail when the width can't hold 2106 // both minimums, so when it's that narrow we lift the cap and let 2107 // the list fill the whole width. 2108 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 2109 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 2110 2111 // stretch the detail pane across the rest of the width so it fills 2112 // the area rather than shrinking to its content; its scroll fills 2113 // the pane. 2114 if (constraint.max_size.width) |w| { 2115 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 2116 } else { 2117 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 2118 } 2119 } 2120 2121 // refresh the form inputs' entries in the session's focus-id -> input 2122 // map with this frame's addresses, so the web/wasm form handling can 2123 // find them by focus id 2124 if (self.threadForm()) |form| { 2125 const failure = if (self.session.formFeedback(feedback_tag)) |saved| saved.failure else null; 2126 form.children.values()[title_field_index].widget.text_input.options.label = if (failure == .required_title) " title (required) " else " title "; 2127 if (comptime supports_drafts) { 2128 const invalid_target = failure == .invalid_target_branch; 2129 form.children.values()[target_branch_field_index].widget.text_input.options.label = if (invalid_target) " target branch (not found) " else " target branch "; 2130 } 2131 } 2132 if (self.formBox()) |form| { 2133 const inputs_arena = self.session.arena.allocator(); 2134 for (form.children.values()) |*child| switch (child.widget) { 2135 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 2136 else => {}, 2137 }; 2138 } 2139 2140 try self.box.build(allocator, constraint, root_focus); 2141 } 2142 2143 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 2144 // wheel input moves focus like the arrows 2145 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 2146 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 2147 else 2148 raw_key; 2149 2150 if (self.headerActive()) { 2151 // down from the tabs re-enters the stack if the selected view has 2152 // something to focus; other keys move the tabs. 2153 if (inp.vertDirection(key) == .down) { 2154 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 2155 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 2156 // nothing focusable in the unauthorized view 2157 .unauthorized => false, 2158 else => true, 2159 } else false; 2160 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 2161 } else { 2162 try self.header().input(allocator, key, root_focus); 2163 } 2164 return; 2165 } 2166 if (self.tagsViewActive()) { 2167 self.tagsViewInput(key, root_focus); 2168 return; 2169 } 2170 if (self.formViewActive()) { 2171 if (confirmation(self.data.view) != null) { 2172 try self.confirmationFormInput(allocator, key, root_focus); 2173 } else if (supports_conflicts and self.data.view == .resolve) { 2174 try self.resolveInput(allocator, key, root_focus); 2175 } else if (self.data.view == .new_comment or self.data.view == .edit_comment) { 2176 try self.commentFormInput(allocator, key, root_focus); 2177 } else { 2178 try self.formInput(allocator, key, root_focus); 2179 } 2180 return; 2181 } 2182 const i = self.selectedSplitIndex() orelse return; 2183 if (self.detailActive(i)) { 2184 switch (try self.detail(i).inputWithExit(allocator, key, root_focus)) { 2185 .none => {}, 2186 .list => self.focusList(i, root_focus), 2187 .header => self.focusHeader(root_focus), 2188 } 2189 } else { 2190 self.listInput(i, key, root_focus); 2191 } 2192 } 2193 2194 // arrow keys move the tag selection; up from the top row crosses to the 2195 // header tabs. 2196 fn tagsViewInput(self: *This, key: Key, root_focus: *Focus) void { 2197 const tf = self.tagsView(); 2198 const cid = tf.focus.child_id orelse return; 2199 const cur = tf.indexOfFocusId(cid) orelse return; 2200 const count = tf.text_boxes.items.len; 2201 switch (key) { 2202 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 2203 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 2204 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 2205 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 2206 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 2207 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 2208 else => {}, 2209 } 2210 } 2211 2212 fn listInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 2213 // up/down (and the scroll wheel) move the selection a row; page up/down 2214 // jump a fixed amount. right/Enter cross into the detail pane. up from 2215 // the top row crosses into the header tabs. 2216 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 2217 const lb = self.listBox(index); 2218 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 2219 if (delta < 0 and at_top) return self.focusHeader(root_focus); 2220 moveRowFocus(lb, self.listScroll(index), root_focus, delta); 2221 return; 2222 } 2223 switch (key) { 2224 .enter, .arrow_right => _ = self.detail(index).focusFirst(root_focus), 2225 else => {}, 2226 } 2227 } 2228 2229 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2230 const form = self.threadForm() orelse return; 2231 const cid = form.getFocus().child_id orelse return; 2232 const cur = form.children.getIndex(cid) orelse return; 2233 2234 if (cur == description_field_index) { 2235 const child = &form.children.values()[cur]; 2236 if (try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2237 return child.widget.input(allocator, key, root_focus); 2238 } 2239 2240 switch (key) { 2241 .arrow_up, .back_tab => if (cur > 0) 2242 root_focus.setFocus(form.children.keys()[cur - 1]) 2243 else 2244 self.focusHeader(root_focus), 2245 .arrow_down, .tab => if (cur < submit_field_index) { 2246 root_focus.setFocus(form.children.keys()[cur + 1]); 2247 }, 2248 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 2249 .mouse => |mouse| if (cur == submit_field_index) { 2250 const submit = &form.children.values()[cur].widget.submit_button; 2251 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitForm(allocator); 2252 }, 2253 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 2254 } 2255 } 2256 2257 fn commentFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2258 const form = self.formBox() orelse return; 2259 const cid = form.getFocus().child_id orelse return; 2260 const cur = form.children.getIndex(cid) orelse return; 2261 const child = &form.children.values()[cur]; 2262 2263 if (cur == comment_body_field_index and try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2264 return child.widget.input(allocator, key, root_focus); 2265 2266 switch (key) { 2267 .arrow_up, .back_tab => if (cur > comment_author_field_index) 2268 root_focus.setFocus(form.children.keys()[cur - 1]) 2269 else 2270 self.focusHeader(root_focus), 2271 .arrow_down, .tab => if (cur < comment_submit_field_index) 2272 root_focus.setFocus(form.children.keys()[cur + 1]), 2273 .enter => if (cur == comment_submit_field_index) try self.submitComment(allocator), 2274 .mouse => |mouse| if (cur == comment_submit_field_index) { 2275 const submit = &child.widget.submit_button; 2276 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitComment(allocator); 2277 }, 2278 else => try child.widget.input(allocator, key, root_focus), 2279 } 2280 } 2281 2282 fn confirmationFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2283 const form = self.confirmationForm() orelse return; 2284 const child_id = form.getFocus().child_id orelse return; 2285 const index = form.children.getIndex(child_id) orelse return; 2286 switch (key) { 2287 .arrow_up, .back_tab => if (index > 1) 2288 root_focus.setFocus(form.children.keys()[index - 1]) 2289 else 2290 self.focusHeader(root_focus), 2291 .arrow_down, .tab => if (index + 1 < form.children.count()) root_focus.setFocus(form.children.keys()[index + 1]), 2292 .enter => try self.submitConfirmation(allocator, index), 2293 .mouse => |mouse| for (form.children.keys()[1..], 1..) |button_id, button_index| { 2294 if (inp.leftClickOn(root_focus, button_id, mouse)) { 2295 try self.submitConfirmation(allocator, button_index); 2296 break; 2297 } 2298 }, 2299 else => {}, 2300 } 2301 } 2302 2303 // the multiline hunk inputs keep enter and any up/down that has a row to 2304 // move to; enter on a "use this" link is a navigation the host follows 2305 // before this runs. 2306 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2307 const form = self.formBox() orelse return; 2308 const cid = form.getFocus().child_id orelse return; 2309 const cur = form.children.getIndex(cid) orelse return; 2310 const child = &form.children.values()[cur]; 2311 2312 if (child.widget == .text_input) { 2313 const ti = &child.widget.text_input; 2314 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2315 return child.widget.input(allocator, key, root_focus); 2316 } 2317 2318 const on_submit = child.widget == .submit_button; 2319 2320 switch (key) { 2321 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2322 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2323 .arrow_left, .arrow_right => switch (child.widget) { 2324 .box => |*row| { 2325 const row_cid = row.getFocus().child_id orelse return; 2326 const row_index = row.children.getIndex(row_cid) orelse return; 2327 if (key == .arrow_left) { 2328 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 2329 } else if (row_index + 1 < row.children.count()) { 2330 root_focus.setFocus(row.children.keys()[row_index + 1]); 2331 } 2332 }, 2333 else => try child.widget.input(allocator, key, root_focus), 2334 }, 2335 .enter => if (on_submit) try self.submitResolution(allocator), 2336 .mouse => |mouse| if (on_submit) { 2337 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2338 }, 2339 else => try child.widget.input(allocator, key, root_focus), 2340 } 2341 } 2342 2343 // a multiline input keeps enter and any up/down that has a row to move to 2344 fn multilineKeepsKey(text_input: *wgt.TextInput, allocator: std.mem.Allocator, key: Key) !bool { 2345 return switch (key) { 2346 .enter => true, 2347 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2348 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2349 else => false, 2350 }; 2351 } 2352 2353 // the neighboring focusable resolve-form child, skipping the spacer and 2354 // the blank gap rows 2355 fn resolveStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2356 var i = cur; 2357 while (true) { 2358 if (down) { 2359 i += 1; 2360 if (i >= form.children.count()) return null; 2361 } else { 2362 if (i == 0) return null; 2363 i -= 1; 2364 } 2365 switch (form.children.values()[i].widget) { 2366 .spacer, .text => continue, 2367 else => return i, 2368 } 2369 } 2370 } 2371 2372 // focus the resolve-form child at `i` (a version row focuses its selected 2373 // child) and keep it visible on the terminal 2374 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2375 const child = &form.children.values()[i]; 2376 switch (child.widget) { 2377 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2378 else => root_focus.setFocus(form.children.keys()[i]), 2379 } 2380 if (self.session.is_terminal) { 2381 if (self.resolveScroll()) |sc| { 2382 if (child.rect) |rect| sc.scrollToRect(rect); 2383 } 2384 } 2385 } 2386 2387 // re-emit the conflicted thread's event with the resolved content; any 2388 // event on the thread settles its conflict entry. this is the terminal 2389 // path; the web posts the form to the resolve route. 2390 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2391 if (comptime wasm) return; 2392 const io = self.session.io orelse return; 2393 const src = self.data.repo_source orelse return; 2394 const entry = self.data.selectedThread() orelse return; 2395 const author = (try self.session.eventAuthor()) orelse return; 2396 const form = self.formBox() orelse return; 2397 2398 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2399 var title: ?[]u8 = null; 2400 var tags: ?[]u8 = null; 2401 var hunks: std.ArrayList([]const u8) = .empty; 2402 defer { 2403 if (title) |t| allocator.free(t); 2404 if (tags) |t| allocator.free(t); 2405 for (hunks.items) |h| allocator.free(h); 2406 hunks.deinit(allocator); 2407 } 2408 for (form.children.values()) |*child| switch (child.widget) { 2409 .text_input => |*ti| { 2410 const text = try ti.text(allocator); 2411 errdefer allocator.free(text); 2412 if (std.mem.eql(u8, ti.options.name, "title")) { 2413 title = text; 2414 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2415 tags = text; 2416 } else { 2417 try hunks.append(allocator, text); 2418 } 2419 }, 2420 else => {}, 2421 }; 2422 2423 const id_bytes = try evt.parseEventId(entry.id); 2424 2425 switch (src.repo_kind) { 2426 inline else => |repo_kind| { 2427 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2428 defer any_repo.deinit(io, allocator); 2429 switch (any_repo) { 2430 inline else => |*repo| { 2431 var resolution: Event.Resolve = .{ 2432 .title = title, 2433 .tags = tags, 2434 .hunks = hunks.items, 2435 }; 2436 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2437 const change = Event.Update{ .resolve = resolution }; 2438 Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2439 // leave the form up for correction 2440 error.InvalidFields => return, 2441 else => |e| return e, 2442 }; 2443 }, 2444 } 2445 }, 2446 } 2447 2448 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2449 try self.session.navigate(route); 2450 } 2451 2452 // commit a new or edited comment and navigate to its permalink. this is the 2453 // terminal path; the web posts the form to its comment route. 2454 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2455 if (comptime wasm) return; 2456 const io = self.session.io orelse return; 2457 const src = self.data.repo_source orelse return; 2458 const author = (try self.session.eventAuthor()) orelse return; 2459 const form = self.formBox() orelse return; 2460 const body_input = &form.children.values()[comment_body_field_index].widget.text_input; 2461 const body = try body_input.text(allocator); 2462 defer allocator.free(body); 2463 if (!evt.Comment.fieldsValid(body)) return; 2464 2465 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2466 @memcpy(&thread_id, self.data.selected_id); 2467 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2468 var parent: [evt.event_id_size * 2]u8 = undefined; 2469 @memcpy(&parent, self.data.comment_id); 2470 break :blk parent; 2471 }; 2472 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2473 const editing = self.data.view == .edit_comment; 2474 switch (src.repo_kind) { 2475 inline else => |repo_kind| { 2476 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2477 defer any_repo.deinit(io, allocator); 2478 switch (any_repo) { 2479 inline else => |*repo| if (editing) { 2480 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2481 try evt.Comment.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2482 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2483 } else { 2484 event_id_hex = try evt.Comment.create(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2485 }, 2486 } 2487 }, 2488 } 2489 2490 body_input.clear(allocator); 2491 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2492 try self.session.navigate(route); 2493 } 2494 2495 // the terminal submit paths: the new form commits a new thread event, the 2496 // edit form re-emits the selected thread's event with the form's content. 2497 // the web posts the forms to their routes instead. 2498 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2499 if (self.data.view == .edit) { 2500 try self.submitEditedThread(allocator); 2501 } else { 2502 try self.submitNewThread(allocator); 2503 } 2504 } 2505 2506 // commit the new thread to the repo's events branch and navigate to it. 2507 // this is the terminal path; the web posts the form to the thread route, 2508 // so the wasm side never runs (or compiles) the repo access below. 2509 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2510 if (comptime wasm) return; 2511 const io = self.session.io orelse return; 2512 const src = self.data.repo_source orelse return; 2513 const author = (try self.session.eventAuthor()) orelse return; 2514 2515 const form = self.threadForm() orelse return; 2516 const title_input = &form.children.values()[title_field_index].widget.text_input; 2517 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2518 const description_input = &form.children.values()[description_field_index].widget.text_input; 2519 2520 const title = try title_input.text(allocator); 2521 defer allocator.free(title); 2522 const tags = try tags_input.text(allocator); 2523 defer allocator.free(tags); 2524 const description = try description_input.text(allocator); 2525 defer allocator.free(description); 2526 const target_branch = if (comptime supports_drafts) try form.children.values()[target_branch_field_index].widget.text_input.text(allocator) else ""; 2527 defer if (comptime supports_drafts) allocator.free(target_branch); 2528 2529 if (!Event.fieldsValid(title, tags)) { 2530 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, tags, description, target_branch); 2531 return; 2532 } 2533 2534 if (comptime supports_drafts) { 2535 const event_id_hex = Data.createDraft(self.data, self.session, allocator, title, tags, description, target_branch) catch |err| switch (err) { 2536 error.InvalidFields => { 2537 try self.rememberThreadFeedback(.invalid_target_branch, title, tags, description, target_branch); 2538 return; 2539 }, 2540 else => |other| return other, 2541 }; 2542 title_input.clear(allocator); 2543 tags_input.clear(allocator); 2544 description_input.clear(allocator); 2545 form.children.values()[target_branch_field_index].widget.text_input.clear(allocator); 2546 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2547 try self.session.navigate(route); 2548 return; 2549 } 2550 2551 var id_bytes: [evt.event_id_size]u8 = undefined; 2552 io.random(&id_bytes); 2553 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2554 2555 const event = evt.EventWithId{ 2556 .id = event_id_hex, 2557 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2558 .author = author, 2559 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2560 .title = title, 2561 .description = description, 2562 .tags = tags, 2563 }), 2564 }; 2565 2566 switch (src.repo_kind) { 2567 inline else => |repo_kind| { 2568 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2569 defer any_repo.deinit(io, allocator); 2570 switch (any_repo) { 2571 inline else => |*repo| try evt.consume(self.session.data.host_kind, .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2572 } 2573 }, 2574 } 2575 2576 // wipe the form so a return visit starts fresh 2577 title_input.clear(allocator); 2578 tags_input.clear(allocator); 2579 description_input.clear(allocator); 2580 2581 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2582 try self.session.navigate(route); 2583 } 2584 2585 // re-emit the selected thread's event with the form's content (status 2586 // preserved), then reload the thread's page. this is the terminal path; 2587 // the web posts the form to the edit route. 2588 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2589 if (comptime wasm) return; 2590 const io = self.session.io orelse return; 2591 const entry = self.data.selectedThread() orelse return; 2592 2593 const form = self.threadForm() orelse return; 2594 const title_input = &form.children.values()[title_field_index].widget.text_input; 2595 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2596 const description_input = &form.children.values()[description_field_index].widget.text_input; 2597 2598 const title = try title_input.text(allocator); 2599 defer allocator.free(title); 2600 const tags = try tags_input.text(allocator); 2601 defer allocator.free(tags); 2602 const description = try description_input.text(allocator); 2603 defer allocator.free(description); 2604 const target_branch = if (comptime supports_drafts) try form.children.values()[target_branch_field_index].widget.text_input.text(allocator) else ""; 2605 defer if (comptime supports_drafts) allocator.free(target_branch); 2606 2607 if (!Event.fieldsValid(title, tags)) { 2608 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, tags, description, target_branch); 2609 return; 2610 } 2611 const src = self.data.repo_source orelse return; 2612 2613 if (comptime supports_drafts) { 2614 if (entryDraft(entry.*)) { 2615 self.data.editDraft(self.session, allocator, entry.id, title, tags, description, target_branch) catch |err| switch (err) { 2616 error.InvalidFields => { 2617 try self.rememberThreadFeedback(.invalid_target_branch, title, tags, description, target_branch); 2618 return; 2619 }, 2620 else => |other| return other, 2621 }; 2622 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2623 try self.session.navigate(route); 2624 return; 2625 } 2626 } 2627 2628 const id_bytes = try evt.parseEventId(entry.id); 2629 const author = (try self.session.eventAuthor()) orelse return; 2630 2631 switch (src.repo_kind) { 2632 inline else => |repo_kind| { 2633 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2634 defer any_repo.deinit(io, allocator); 2635 switch (any_repo) { 2636 inline else => |*repo| if (supports_drafts) { 2637 Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2638 .title = title, 2639 .tags = tags, 2640 .description = description, 2641 .target_branch = target_branch, 2642 } }, author) catch |err| switch (err) { 2643 error.InvalidFields => { 2644 try self.rememberThreadFeedback(.invalid_target_branch, title, tags, description, target_branch); 2645 return; 2646 }, 2647 else => |other| return other, 2648 }; 2649 } else if (has_status) 2650 try Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2651 .title = title, 2652 .tags = tags, 2653 .description = description, 2654 } }, author) 2655 else 2656 try Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 2657 } 2658 }, 2659 } 2660 2661 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2662 try self.session.navigate(route); 2663 } 2664 2665 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, tags: []const u8, description: []const u8, target_branch: []const u8) !void { 2666 const aa = self.session.arena.allocator(); 2667 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 2668 .failure = failure, 2669 .fields = if (comptime supports_drafts) .{ 2670 .title = try aa.dupe(u8, title), 2671 .tags = try aa.dupe(u8, tags), 2672 .description = try aa.dupe(u8, description), 2673 .target_branch = try aa.dupe(u8, target_branch), 2674 } else .{ 2675 .title = try aa.dupe(u8, title), 2676 .tags = try aa.dupe(u8, tags), 2677 .description = try aa.dupe(u8, description), 2678 }, 2679 }); 2680 } 2681 2682 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 2683 switch (confirmation(self.data.view) orelse return) { 2684 .publish => if (comptime supports_drafts) { 2685 if (comptime wasm) return; 2686 const entry = self.data.selectedThread() orelse return; 2687 if (!entryDraft(entry.*)) return; 2688 try self.data.publishDraft(self.session, allocator, entry.id); 2689 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 2690 try self.session.navigate(route); 2691 } else unreachable, 2692 .merge => if (comptime supports_merge) { 2693 if (comptime wasm) return; 2694 const entry = self.data.selectedThread() orelse return; 2695 const revision: pch.MergeRevision = if (button_index > 1) .squash else .source; 2696 if (!Data.canMerge(entry.*, self.session) or entry.mergeability.get(revision) != .clean) return; 2697 self.data.mergePatch(self.session, allocator, entry.id, revision) catch |err| switch (err) { 2698 error.MergeConflict, error.MergeCheckUnavailable, error.PatchOutOfDate, error.PatchDataUnavailable => { 2699 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2700 try self.session.navigate(route); 2701 return; 2702 }, 2703 else => return err, 2704 }; 2705 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 2706 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 2707 try self.session.navigate(route); 2708 } else unreachable, 2709 .remove => try self.submitRemove(allocator), 2710 } 2711 } 2712 2713 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2714 const comment = self.data.comment_id.len != 0; 2715 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2716 const id = evt.parseEventId(event_id) catch return; 2717 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2718 } 2719 2720 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2721 if (comptime wasm) return; 2722 2723 if (comptime supports_drafts) { 2724 const selected = self.data.selectedThread() orelse return; 2725 if (event_kind == kind and entryDraft(selected.*)) { 2726 try Data.removeDraft(self.session, allocator, &id); 2727 const route = draftsRoute(self.data.identity) orelse return; 2728 try self.session.navigate(route); 2729 return; 2730 } 2731 } 2732 2733 const io = self.session.io orelse return; 2734 const src = self.data.repo_source orelse return; 2735 const author = (try self.session.eventAuthor()) orelse return; 2736 2737 switch (src.repo_kind) { 2738 inline else => |repo_kind| { 2739 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2740 defer any_repo.deinit(io, allocator); 2741 switch (any_repo) { 2742 inline else => |*repo| try evt.remove(self.session.data.host_kind, .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2743 } 2744 }, 2745 } 2746 2747 const id_hex = std.fmt.bytesToHex(id, .lower); 2748 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 2749 try self.session.navigate(route); 2750 } 2751 2752 // return to the list. 2753 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2754 root_focus.setFocus(self.listScroll(index).getFocus().id); 2755 } 2756 2757 // cross to the header tabs above the stack. 2758 fn focusHeader(self: *This, root_focus: *Focus) void { 2759 root_focus.setFocus(self.box.children.keys()[header_index]); 2760 } 2761 2762 pub fn clearGrid(self: *This) void { 2763 self.box.clearGrid(); 2764 } 2765 2766 pub fn getGrid(self: This) ?Grid { 2767 return self.box.getGrid(); 2768 } 2769 2770 pub fn getFocus(self: *This) *Focus { 2771 return self.box.getFocus(); 2772 } 2773 2774 // for the parent's "scroll up at the top jumps to the header" check: at the 2775 // top only while the header tabs hold focus, so up from the split 2776 // crosses into the tabs first. 2777 pub fn atTop(self: *This) bool { 2778 return self.headerActive(); 2779 } 2780 }; 2781 } 2782