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