╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1722 .arrow_right => if (cur + 1 < row.children.count()) root_focus.setFocus(row.children.keys()[cur + 1]), 1723 .arrow_up => self.focusHeader(root_focus), 1724 .arrow_down => { 1725 self.focusDetailEdge(index, root_focus, false); 1726 self.detailScroll(index).y = 0; 1727 }, 1728 .enter => if (on_primary) try self.primaryAction(allocator, index), 1729 .mouse => |mouse| if (on_primary and inp.leftClickOn(root_focus, child_id, mouse)) try self.primaryAction(allocator, index), 1730 else => {}, 1731 } 1732 } 1733 1734 fn primaryAction(self: *This, allocator: std.mem.Allocator, index: usize) !void { 1735 const selected = self.detailed_index[index] orelse return; 1736 const entry = self.window(index).items[selected]; 1737 if (supports_drafts and entryDraft(entry)) 1738 try self.postDraft(allocator, index) 1739 else 1740 try self.toggleStatus(allocator, index); 1741 } 1742 1743 fn postDraft(self: *This, allocator: std.mem.Allocator, index: usize) !void { 1744 if (!supports_drafts or comptime wasm) return; 1745 const selected = self.detailed_index[index] orelse return; 1746 const entry = self.window(index).items[selected]; 1747 if (!entryDraft(entry) or !entry.revision_ready) return; 1748 try self.data.postDraft(self.session, allocator, entry.id); 1749 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 1750 try self.session.navigate(route); 1751 } 1752 1753 fn descriptionInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 1754 const sc = self.detailScroll(index); 1755 switch (key) { 1756 .arrow_left => return self.focusList(index, root_focus), 1757 // the terminal moves through the description one line at a time; 1758 // the web lets focus changes drive its native scroll. 1759 .arrow_up => { 1760 if (!self.session.is_terminal) { 1761 _ = self.moveDetailVertical(index, root_focus, false); 1762 return; 1763 } 1764 if (self.focusAdjacentVisible(index, root_focus, false)) return; 1765 const before = sc.y; 1766 sc.y -= 1; 1767 sc.clampToContent(); 1768 if (sc.y == before) _ = self.moveDetailVertical(index, root_focus, false); 1769 return; 1770 }, 1771 .arrow_down => { 1772 if (!self.session.is_terminal) { 1773 _ = self.moveDetailVertical(index, root_focus, true); 1774 return; 1775 } 1776 if (self.focusAdjacentVisible(index, root_focus, true)) return; 1777 const before = sc.y; 1778 sc.y += 1; 1779 sc.clampToContent(); 1780 if (sc.y == before) _ = self.moveDetailVertical(index, root_focus, true); 1781 return; 1782 }, 1783 else => return, 1784 } 1785 } 1786 1787 fn commentInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 1788 switch (key) { 1789 .arrow_left => if (!self.moveCommentHorizontal(index, root_focus, false)) return self.focusList(index, root_focus), 1790 .arrow_right => _ = self.moveCommentHorizontal(index, root_focus, true), 1791 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1792 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1793 else => return, 1794 } 1795 } 1796 1797 // page the detail scroll, then focus the first or last visible widget. 1798 fn pageDetail(self: *This, index: usize, root_focus: *Focus, delta: isize) void { 1799 if (!self.session.is_terminal) return; 1800 const sc = self.detailScroll(index); 1801 sc.y += delta; 1802 sc.clampToContent(); 1803 self.focusVisible(index, root_focus, delta > 0); 1804 } 1805 1806 // jump to the detail's first or last focusable widget. the terminal pins 1807 // its synthetic scroll too; the browser scrolls to the new focus itself. 1808 fn jumpDetail(self: *This, index: usize, root_focus: *Focus, to_end: bool) void { 1809 if (self.session.is_terminal) { 1810 const sc = self.detailScroll(index); 1811 sc.y = if (to_end) std.math.maxInt(isize) else 0; 1812 sc.clampToContent(); 1813 self.focusVisible(index, root_focus, to_end); 1814 } else { 1815 self.focusDetailEdge(index, root_focus, to_end); 1816 } 1817 } 1818 1819 fn focusDetailEdge(self: *This, index: usize, root_focus: *Focus, last: bool) void { 1820 const focus = self.detailInner(index).getFocus(); 1821 var chosen: ?usize = null; 1822 for (focus.children.keys(), focus.children.values()) |id, child| { 1823 if (!child.focus.focusable) continue; 1824 chosen = id; 1825 if (!last) break; 1826 } 1827 if (chosen) |id| root_focus.setFocus(id); 1828 } 1829 1830 fn detailRectVisible(self: *This, index: usize, rect: layout.IRect) bool { 1831 const sc = self.detailScroll(index); 1832 const viewport = sc.grid orelse return false; 1833 const top = sc.y; 1834 const bottom = top + @as(isize, @intCast(viewport.size.height - sc.bar_h)); 1835 return rect.y + @as(isize, @intCast(rect.size.height)) > top and rect.y < bottom; 1836 } 1837 1838 // focus the top- or bottom-most focusable widget intersecting the viewport. 1839 fn focusVisible(self: *This, index: usize, root_focus: *Focus, last: bool) void { 1840 const inner = self.detailInner(index); 1841 var offset: usize = 0; 1842 while (offset < inner.children.count()) : (offset += 1) { 1843 const child_index = if (last) inner.children.count() - 1 - offset else offset; 1844 if (self.focusVisibleChild(index, child_index, root_focus, last)) return; 1845 } 1846 } 1847 1848 fn focusVisibleChild(self: *This, index: usize, child_index: usize, root_focus: *Focus, last: bool) bool { 1849 const child = &self.detailInner(index).children.values()[child_index]; 1850 const child_rect = child.rect orelse return false; 1851 var chosen: ?usize = null; 1852 if (child.widget.getFocus().focusable and self.detailRectVisible(index, child_rect)) { 1853 const id = self.detailInner(index).children.keys()[child_index]; 1854 chosen = id; 1855 if (!last) { 1856 root_focus.setFocus(id); 1857 return true; 1858 } 1859 } 1860 1861 for (child.widget.getFocus().children.keys(), child.widget.getFocus().children.values()) |id, focus_child| { 1862 if (!focus_child.focus.focusable) continue; 1863 const rect: layout.IRect = .{ 1864 .x = child_rect.x + @as(isize, @intCast(focus_child.rect.x)), 1865 .y = child_rect.y + @as(isize, @intCast(focus_child.rect.y)), 1866 .size = focus_child.rect.size, 1867 }; 1868 if (!self.detailRectVisible(index, rect)) continue; 1869 chosen = id; 1870 if (!last) break; 1871 } 1872 if (chosen) |id| { 1873 root_focus.setFocus(id); 1874 return true; 1875 } 1876 return false; 1877 } 1878 1879 fn focusAdjacentVisible(self: *This, index: usize, root_focus: *Focus, down: bool) bool { 1880 const inner = self.detailInner(index); 1881 var next = self.focusedDetailChild(index, root_focus) orelse return false; 1882 while (true) { 1883 if (down) { 1884 next += 1; 1885 if (next >= inner.children.count()) return false; 1886 } else { 1887 if (next == 0) return false; 1888 next -= 1; 1889 } 1890 if (self.focusVisibleChild(index, next, root_focus, !down)) { 1891 self.focusDetailChild(index, next, !down, root_focus); 1892 return true; 1893 } 1894 } 1895 } 1896 1897 fn focusedDetailChild(self: *This, index: usize, root_focus: *Focus) ?usize { 1898 const inner = self.detailInner(index); 1899 var id = root_focus.grandchild_id orelse return null; 1900 while (inner.getFocus().children.get(id)) |child| { 1901 if (child.parent_id == inner.getFocus().id) return inner.children.getIndex(id); 1902 id = child.parent_id; 1903 } 1904 return null; 1905 } 1906 1907 fn detailChildFocusable(self: *This, index: usize, child_index: usize) bool { 1908 const child = &self.detailInner(index).children.values()[child_index].widget; 1909 const focus = child.getFocus(); 1910 if (focus.focusable) return true; 1911 for (focus.children.values()) |item| { 1912 if (item.focus.focusable) return true; 1913 } 1914 return false; 1915 } 1916 1917 fn focusDetailChild(self: *This, index: usize, child_index: usize, last: bool, root_focus: *Focus) void { 1918 const inner = self.detailInner(index); 1919 const child = &inner.children.values()[child_index]; 1920 var target = inner.children.keys()[child_index]; 1921 var rect = child.rect orelse return; 1922 switch (child.widget) { 1923 .repo_comment => |*comment| { 1924 if (comment.rowRect(last)) |row_rect| { 1925 rect.x += row_rect.x; 1926 rect.y += row_rect.y; 1927 rect.size = row_rect.size; 1928 } 1929 if (last) comment.focusBody(root_focus) else comment.focusMetadata(root_focus); 1930 }, 1931 .box => |*box| if (box.children.count() > 0) { 1932 target = box.children.keys()[if (last) box.children.count() - 1 else 0]; 1933 root_focus.setFocus(target); 1934 }, 1935 else => root_focus.setFocus(target), 1936 } 1937 const sc = self.detailScroll(index); 1938 if (sc.grid) |viewport| { 1939 const viewport_height = viewport.size.height - sc.bar_h; 1940 if (rect.size.height > viewport_height) { 1941 const bottom_aligned = rect.y + @as(isize, @intCast(rect.size.height - viewport_height)); 1942 sc.y = std.math.clamp(sc.y, rect.y, bottom_aligned); 1943 sc.clampToContent(); 1944 return; 1945 } 1946 } 1947 sc.scrollToRect(rect); 1948 } 1949 1950 fn moveDetailVertical(self: *This, index: usize, root_focus: *Focus, down: bool) bool { 1951 const inner = self.detailInner(index); 1952 const current = self.focusedDetailChild(index, root_focus) orelse return false; 1953 if (inner.children.values()[current].widget == .repo_comment) { 1954 const comment = &inner.children.values()[current].widget.repo_comment; 1955 if (down and !comment.bodyFocused()) { 1956 self.focusDetailChild(index, current, true, root_focus); 1957 return true; 1958 } 1959 if (!down and comment.bodyFocused()) { 1960 self.focusDetailChild(index, current, false, root_focus); 1961 return true; 1962 } 1963 } 1964 1965 var next = current; 1966 while (true) { 1967 if (down) { 1968 next += 1; 1969 if (next >= inner.children.count()) return false; 1970 } else { 1971 if (next == 0) return false; 1972 next -= 1; 1973 } 1974 if (!self.detailChildFocusable(index, next)) continue; 1975 self.focusDetailChild(index, next, !down, root_focus); 1976 return true; 1977 } 1978 } 1979 1980 fn moveCommentHorizontal(self: *This, index: usize, root_focus: *Focus, right: bool) bool { 1981 const inner = self.detailInner(index); 1982 const current = self.focusedDetailChild(index, root_focus) orelse return false; 1983 const child = &inner.children.values()[current]; 1984 if (child.widget == .repo_comment) { 1985 const moved = child.widget.repo_comment.moveHorizontal(root_focus, right); 1986 if (moved) if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 1987 return moved; 1988 } 1989 const row = switch (child.widget) { 1990 .box => |*box| box, 1991 else => return false, 1992 }; 1993 const selected = row.getFocus().child_id orelse return false; 1994 const selected_index = row.children.getIndex(selected) orelse return false; 1995 const target = if (right) selected_index + 1 else selected_index -| 1; 1996 if (target == selected_index or target >= row.children.count()) return false; 1997 root_focus.setFocus(row.children.keys()[target]); 1998 if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 1999 return true; 2000 } 2001 2002 // up/down (and tab/shift+tab) move between a form's fields; up from the 2003 // title crosses into the header tabs. the multiline description keeps 2004 // enter and any up/down that has a row to move to. 2005 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2006 const form = self.threadForm() orelse return; 2007 const cid = form.getFocus().child_id orelse return; 2008 const cur = form.children.getIndex(cid) orelse return; 2009 2010 if (cur == description_field_index) { 2011 const child = &form.children.values()[cur]; 2012 if (try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2013 return child.widget.input(allocator, key, root_focus); 2014 } 2015 2016 switch (key) { 2017 .arrow_up, .back_tab => if (cur > 0) 2018 root_focus.setFocus(form.children.keys()[cur - 1]) 2019 else 2020 self.focusHeader(root_focus), 2021 .arrow_down, .tab => if (cur < submit_field_index) { 2022 root_focus.setFocus(form.children.keys()[cur + 1]); 2023 }, 2024 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 2025 .mouse => |mouse| if (cur == submit_field_index) { 2026 const submit = &form.children.values()[cur].widget.submit_button; 2027 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitForm(allocator); 2028 }, 2029 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 2030 } 2031 } 2032 2033 fn commentFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2034 const form = self.threadForm() orelse return; 2035 const cid = form.getFocus().child_id orelse return; 2036 const cur = form.children.getIndex(cid) orelse return; 2037 const child = &form.children.values()[cur]; 2038 2039 if (cur == comment_body_field_index and try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2040 return child.widget.input(allocator, key, root_focus); 2041 2042 switch (key) { 2043 .arrow_up, .back_tab => if (cur > comment_author_field_index) 2044 root_focus.setFocus(form.children.keys()[cur - 1]) 2045 else 2046 self.focusHeader(root_focus), 2047 .arrow_down, .tab => if (cur < comment_submit_field_index) 2048 root_focus.setFocus(form.children.keys()[cur + 1]), 2049 .enter => if (cur == comment_submit_field_index) try self.submitComment(allocator), 2050 .mouse => |mouse| if (cur == comment_submit_field_index) { 2051 const submit = &child.widget.submit_button; 2052 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitComment(allocator); 2053 }, 2054 else => try child.widget.input(allocator, key, root_focus), 2055 } 2056 } 2057 2058 fn removeFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2059 const form = self.removeForm() orelse return; 2060 const button_id = form.children.keys()[1]; 2061 switch (key) { 2062 .arrow_up, .back_tab => self.focusHeader(root_focus), 2063 .enter => try self.submitRemove(allocator), 2064 .mouse => |mouse| if (inp.leftClickOn(root_focus, button_id, mouse)) try self.submitRemove(allocator), 2065 else => {}, 2066 } 2067 } 2068 2069 // the multiline hunk inputs keep enter and any up/down that has a row to 2070 // move to; enter on a "use this" link is a navigation the host follows 2071 // before this runs. 2072 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2073 const form = self.threadForm() orelse return; 2074 const cid = form.getFocus().child_id orelse return; 2075 const cur = form.children.getIndex(cid) orelse return; 2076 const child = &form.children.values()[cur]; 2077 2078 if (child.widget == .text_input) { 2079 const ti = &child.widget.text_input; 2080 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2081 return child.widget.input(allocator, key, root_focus); 2082 } 2083 2084 const on_submit = child.widget == .submit_button; 2085 2086 switch (key) { 2087 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2088 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2089 .arrow_left, .arrow_right => switch (child.widget) { 2090 .box => |*row| { 2091 const row_cid = row.getFocus().child_id orelse return; 2092 const row_index = row.children.getIndex(row_cid) orelse return; 2093 if (key == .arrow_left) { 2094 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 2095 } else if (row_index + 1 < row.children.count()) { 2096 root_focus.setFocus(row.children.keys()[row_index + 1]); 2097 } 2098 }, 2099 else => try child.widget.input(allocator, key, root_focus), 2100 }, 2101 .enter => if (on_submit) try self.submitResolution(allocator), 2102 .mouse => |mouse| if (on_submit) { 2103 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2104 }, 2105 else => try child.widget.input(allocator, key, root_focus), 2106 } 2107 } 2108 2109 // a multiline input keeps enter and any up/down that has a row to move to 2110 fn multilineKeepsKey(text_input: *wgt.TextInput(Widget), allocator: std.mem.Allocator, key: Key) !bool { 2111 return switch (key) { 2112 .enter => true, 2113 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2114 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2115 else => false, 2116 }; 2117 } 2118 2119 // the neighboring focusable resolve-form child, skipping the spacer and 2120 // the blank gap rows 2121 fn resolveStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2122 var i = cur; 2123 while (true) { 2124 if (down) { 2125 i += 1; 2126 if (i >= form.children.count()) return null; 2127 } else { 2128 if (i == 0) return null; 2129 i -= 1; 2130 } 2131 switch (form.children.values()[i].widget) { 2132 .spacer, .text => continue, 2133 else => return i, 2134 } 2135 } 2136 } 2137 2138 // focus the resolve-form child at `i` (a version row focuses its selected 2139 // child) and keep it visible on the terminal 2140 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2141 const child = &form.children.values()[i]; 2142 switch (child.widget) { 2143 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2144 else => root_focus.setFocus(form.children.keys()[i]), 2145 } 2146 if (self.session.is_terminal) { 2147 if (self.resolveScroll()) |sc| { 2148 if (child.rect) |rect| sc.scrollToRect(rect); 2149 } 2150 } 2151 } 2152 2153 // re-emit the conflicted thread's event with the resolved content; any 2154 // event on the thread settles its conflict entry. this is the terminal 2155 // path; the web posts the form to the resolve route. 2156 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2157 if (comptime wasm) return; 2158 const io = self.session.io orelse return; 2159 const src = self.data.repo_source orelse return; 2160 const entry = self.data.selectedThread() orelse return; 2161 const author = (try self.session.eventAuthor()) orelse return; 2162 const form = self.threadForm() orelse return; 2163 2164 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2165 var title: ?[]u8 = null; 2166 var tags: ?[]u8 = null; 2167 var hunks: std.ArrayList([]const u8) = .empty; 2168 defer { 2169 if (title) |t| allocator.free(t); 2170 if (tags) |t| allocator.free(t); 2171 for (hunks.items) |h| allocator.free(h); 2172 hunks.deinit(allocator); 2173 } 2174 for (form.children.values()) |*child| switch (child.widget) { 2175 .text_input => |*ti| { 2176 const text = try ti.text(allocator); 2177 errdefer allocator.free(text); 2178 if (std.mem.eql(u8, ti.options.name, "title")) { 2179 title = text; 2180 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2181 tags = text; 2182 } else { 2183 try hunks.append(allocator, text); 2184 } 2185 }, 2186 else => {}, 2187 }; 2188 2189 const id_bytes = try evt.parseEventId(entry.id); 2190 2191 switch (src.repo_kind) { 2192 inline else => |repo_kind| { 2193 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2194 defer any_repo.deinit(io, allocator); 2195 switch (any_repo) { 2196 inline else => |*repo| { 2197 var resolution: Event.Resolve = .{ 2198 .title = title, 2199 .tags = tags, 2200 .hunks = hunks.items, 2201 }; 2202 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2203 const change = Event.Update{ .resolve = resolution }; 2204 Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2205 // leave the form up for correction 2206 error.InvalidFields => return, 2207 else => |e| return e, 2208 }; 2209 }, 2210 } 2211 }, 2212 } 2213 2214 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2215 try self.session.navigate(route); 2216 } 2217 2218 // commit a new or edited comment and navigate to its permalink. this is the 2219 // terminal path; the web posts the form to its comment route. 2220 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2221 if (comptime wasm) return; 2222 const io = self.session.io orelse return; 2223 const src = self.data.repo_source orelse return; 2224 const author = (try self.session.eventAuthor()) orelse return; 2225 const form = self.threadForm() orelse return; 2226 const body_input = &form.children.values()[comment_body_field_index].widget.text_input; 2227 const body = try body_input.text(allocator); 2228 defer allocator.free(body); 2229 if (!evt.Comment.fieldsValid(body)) return; 2230 2231 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2232 @memcpy(&thread_id, self.data.selected_id); 2233 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2234 var parent: [evt.event_id_size * 2]u8 = undefined; 2235 @memcpy(&parent, self.data.comment_id); 2236 break :blk parent; 2237 }; 2238 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2239 const editing = self.data.view == .edit_comment; 2240 switch (src.repo_kind) { 2241 inline else => |repo_kind| { 2242 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2243 defer any_repo.deinit(io, allocator); 2244 switch (any_repo) { 2245 inline else => |*repo| if (editing) { 2246 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2247 try evt.Comment.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2248 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2249 } else { 2250 event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2251 }, 2252 } 2253 }, 2254 } 2255 2256 body_input.clear(allocator); 2257 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2258 try self.session.navigate(route); 2259 } 2260 2261 // the terminal submit paths: the new form commits a new thread event, the 2262 // edit form re-emits the selected thread's event with the form's content. 2263 // the web posts the forms to their routes instead. 2264 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2265 if (self.data.view == .edit) { 2266 try self.submitEditedThread(allocator); 2267 } else { 2268 try self.submitNewThread(allocator); 2269 } 2270 } 2271 2272 // commit the new thread to the repo's events branch and navigate to it. 2273 // this is the terminal path; the web posts the form to the thread route, 2274 // so the wasm side never runs (or compiles) the repo access below. 2275 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2276 if (comptime wasm) return; 2277 const io = self.session.io orelse return; 2278 const src = self.data.repo_source orelse return; 2279 const author = (try self.session.eventAuthor()) orelse return; 2280 2281 const form = self.threadForm() orelse return; 2282 const title_input = &form.children.values()[title_field_index].widget.text_input; 2283 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2284 const description_input = &form.children.values()[description_field_index].widget.text_input; 2285 2286 const title = try title_input.text(allocator); 2287 defer allocator.free(title); 2288 const tags = try tags_input.text(allocator); 2289 defer allocator.free(tags); 2290 const description = try description_input.text(allocator); 2291 defer allocator.free(description); 2292 2293 if (!Event.fieldsValid(title, tags)) return; 2294 2295 if (comptime supports_drafts) { 2296 const event_id_hex = try Data.createDraft(self.data, self.session, allocator, title, tags, description); 2297 title_input.clear(allocator); 2298 tags_input.clear(allocator); 2299 description_input.clear(allocator); 2300 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2301 try self.session.navigate(route); 2302 return; 2303 } 2304 2305 var id_bytes: [evt.event_id_size]u8 = undefined; 2306 io.random(&id_bytes); 2307 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2308 2309 const event = evt.EventWithId{ 2310 .id = event_id_hex, 2311 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2312 .author = author, 2313 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2314 .title = title, 2315 .description = description, 2316 .tags = tags, 2317 }), 2318 }; 2319 2320 switch (src.repo_kind) { 2321 inline else => |repo_kind| { 2322 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2323 defer any_repo.deinit(io, allocator); 2324 switch (any_repo) { 2325 inline else => |*repo| try evt.consume(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2326 } 2327 }, 2328 } 2329 2330 // wipe the form so a return visit starts fresh 2331 title_input.clear(allocator); 2332 tags_input.clear(allocator); 2333 description_input.clear(allocator); 2334 2335 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2336 try self.session.navigate(route); 2337 } 2338 2339 // re-emit the selected thread's event with the form's content (status 2340 // preserved), then reload the thread's page. this is the terminal path; 2341 // the web posts the form to the edit route. 2342 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2343 if (comptime wasm) return; 2344 const io = self.session.io orelse return; 2345 const src = self.data.repo_source orelse return; 2346 const entry = self.data.selectedThread() orelse return; 2347 const author = (try self.session.eventAuthor()) orelse return; 2348 2349 const form = self.threadForm() orelse return; 2350 const title_input = &form.children.values()[title_field_index].widget.text_input; 2351 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2352 const description_input = &form.children.values()[description_field_index].widget.text_input; 2353 2354 const title = try title_input.text(allocator); 2355 defer allocator.free(title); 2356 const tags = try tags_input.text(allocator); 2357 defer allocator.free(tags); 2358 const description = try description_input.text(allocator); 2359 defer allocator.free(description); 2360 2361 if (!Event.fieldsValid(title, tags)) return; 2362 2363 const id_bytes = try evt.parseEventId(entry.id); 2364 2365 switch (src.repo_kind) { 2366 inline else => |repo_kind| { 2367 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2368 defer any_repo.deinit(io, allocator); 2369 switch (any_repo) { 2370 inline else => |*repo| if (has_status) 2371 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2372 .title = title, 2373 .tags = tags, 2374 .description = description, 2375 } }, author) 2376 else 2377 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 2378 } 2379 }, 2380 } 2381 2382 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2383 try self.session.navigate(route); 2384 } 2385 2386 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2387 const comment = self.data.comment_id.len != 0; 2388 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2389 const id = evt.parseEventId(event_id) catch return; 2390 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2391 } 2392 2393 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2394 if (comptime wasm) return; 2395 const io = self.session.io orelse return; 2396 const src = self.data.repo_source orelse return; 2397 const author = (try self.session.eventAuthor()) orelse return; 2398 2399 switch (src.repo_kind) { 2400 inline else => |repo_kind| { 2401 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2402 defer any_repo.deinit(io, allocator); 2403 switch (any_repo) { 2404 inline else => |*repo| try evt.remove(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2405 } 2406 }, 2407 } 2408 2409 const id_hex = std.fmt.bytesToHex(id, .lower); 2410 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 2411 try self.session.navigate(route); 2412 } 2413 2414 // flip the shown thread's status by re-emitting its event, then reload the 2415 // page rooted at the thread so the view reflects the change. this is the 2416 // terminal path; the web posts the button's form to the status route. 2417 fn toggleStatus(self: *This, allocator: std.mem.Allocator, index: usize) !void { 2418 if (comptime !has_status) return; 2419 if (comptime wasm) return; 2420 const io = self.session.io orelse return; 2421 const src = self.data.repo_source orelse return; 2422 const sel = self.detailed_index[index] orelse return; 2423 const entry = self.window(index).items[sel]; 2424 const author = (try self.session.eventAuthor()) orelse return; 2425 2426 const status = (statusChange(entryStatus(entry)) orelse return).status; 2427 2428 const id_bytes = try evt.parseEventId(entry.id); 2429 2430 switch (src.repo_kind) { 2431 inline else => |repo_kind| { 2432 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2433 defer any_repo.deinit(io, allocator); 2434 switch (any_repo) { 2435 inline else => |*repo| try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .status = status }, author), 2436 } 2437 }, 2438 } 2439 2440 const route = listRoute(self.data.identity, status, "", entry.id) orelse return; 2441 try self.session.navigate(route); 2442 } 2443 2444 // arrow keys move the tag selection; at the flow's edges focus crosses to 2445 // the neighboring widgets. 2446 fn tagsInput(self: *This, index: usize, child_index: usize, tf: *TagFlow, key: Key, root_focus: *Focus) void { 2447 const cid = tf.focus.child_id orelse return; 2448 const cur = tf.indexOfFocusId(cid) orelse return; 2449 const count = tf.text_boxes.items.len; 2450 switch (key) { 2451 .arrow_left => if (cur > 0) self.focusTag(index, child_index, tf, root_focus, cur - 1) else self.focusList(index, root_focus), 2452 .arrow_right => if (cur + 1 < count) self.focusTag(index, child_index, tf, root_focus, cur + 1), 2453 .arrow_up => if (tf.rowStep(cur, false)) |i| { 2454 self.focusTag(index, child_index, tf, root_focus, i); 2455 } else { 2456 _ = self.moveDetailVertical(index, root_focus, false); 2457 }, 2458 .arrow_down => if (tf.rowStep(cur, true)) |i| { 2459 self.focusTag(index, child_index, tf, root_focus, i); 2460 } else { 2461 _ = self.moveDetailVertical(index, root_focus, true); 2462 }, 2463 .home => self.focusTag(index, child_index, tf, root_focus, 0), 2464 .end => self.focusTag(index, child_index, tf, root_focus, count - 1), 2465 else => {}, 2466 } 2467 } 2468 2469 // the tool row's first child after the spacer that pushes it right 2470 const first_in_row_index: usize = 1; 2471 2472 fn toolRowFocused(self: *This, index: usize) bool { 2473 const outer = self.detailOuter(index); 2474 const cid = outer.getFocus().child_id orelse return false; 2475 return outer.children.getIndex(cid) == tool_row_index; 2476 } 2477 2478 fn focusTag(self: *This, index: usize, child_index: usize, tf: *TagFlow, root_focus: *Focus, item: usize) void { 2479 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 2480 // keep the tag visible on the terminal: its rect offset by the flow's 2481 // position in the pane. 2482 if (self.session.is_terminal and item < tf.rects.items.len) { 2483 if (self.detailInner(index).children.values()[child_index].rect) |flow_rect| { 2484 var rect = tf.rects.items[item]; 2485 rect.x += flow_rect.x; 2486 rect.y += flow_rect.y; 2487 self.detailScroll(index).scrollToRect(rect); 2488 } 2489 } 2490 } 2491 2492 // return to the tool row's last-focused button (the header when the 2493 // description page shows no row). 2494 fn focusToolRow(self: *This, index: usize, root_focus: *Focus) void { 2495 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 2496 root_focus.setFocus(cid); 2497 } 2498 2499 // enter the detail pane. an empty pane can't be entered. 2500 fn focusDetail(self: *This, index: usize, root_focus: *Focus) void { 2501 if (self.detailInner(index).children.count() == 0) return; 2502 root_focus.setFocus(self.detailOuter(index).getFocus().id); 2503 } 2504 2505 // return to the list. 2506 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2507 root_focus.setFocus(self.listScroll(index).getFocus().id); 2508 } 2509 2510 // cross to the header tabs above the stack. 2511 fn focusHeader(self: *This, root_focus: *Focus) void { 2512 root_focus.setFocus(self.box.children.keys()[header_index]); 2513 } 2514 2515 pub fn clearGrid(self: *This) void { 2516 self.box.clearGrid(); 2517 } 2518 2519 pub fn getGrid(self: This) ?Grid { 2520 return self.box.getGrid(); 2521 } 2522 2523 pub fn getFocus(self: *This) *Focus { 2524 return self.box.getFocus(); 2525 } 2526 2527 // for the parent's "scroll up at the top jumps to the header" check: at the 2528 // top only while the header tabs hold focus, so up from the split 2529 // crosses into the tabs first. 2530 pub fn getSelectedIndex(self: *This) ?usize { 2531 return if (self.headerActive()) 0 else 1; 2532 } 2533 }; 2534 } 2535