╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1785 while (inner.getFocus().children.get(id)) |child| { 1786 if (child.parent_id == inner.getFocus().id) return inner.children.getIndex(id); 1787 id = child.parent_id; 1788 } 1789 return null; 1790 } 1791 1792 fn detailChildFocusable(self: *View, index: usize, child_index: usize) bool { 1793 const child = &self.detailInner(index).children.values()[child_index].widget; 1794 return switch (child.*) { 1795 .repo_comment => true, 1796 .box => |*box| for (box.children.values()) |*item| { 1797 if (item.widget.getFocus().focusable) break true; 1798 } else false, 1799 else => child.getFocus().focusable, 1800 }; 1801 } 1802 1803 fn focusDetailChild(self: *View, index: usize, child_index: usize, last: bool, root_focus: *Focus) void { 1804 const inner = self.detailInner(index); 1805 const child = &inner.children.values()[child_index]; 1806 var target = inner.children.keys()[child_index]; 1807 var rect = child.rect orelse return; 1808 switch (child.widget) { 1809 .repo_comment => |*comment| { 1810 if (comment.rowRect(last)) |row_rect| { 1811 rect.x += row_rect.x; 1812 rect.y += row_rect.y; 1813 rect.size = row_rect.size; 1814 } 1815 if (last) comment.focusBody(root_focus) else comment.focusMetadata(root_focus); 1816 }, 1817 .box => |*box| if (box.children.count() > 0) { 1818 target = box.children.keys()[if (last) box.children.count() - 1 else 0]; 1819 root_focus.setFocus(target); 1820 }, 1821 else => root_focus.setFocus(target), 1822 } 1823 self.detailScroll(index).scrollToRect(rect); 1824 } 1825 1826 fn moveCommentVertical(self: *View, index: usize, root_focus: *Focus, down: bool) bool { 1827 const inner = self.detailInner(index); 1828 const current = self.focusedDetailChild(index, root_focus) orelse return false; 1829 if (inner.children.values()[current].widget == .repo_comment) { 1830 const comment = &inner.children.values()[current].widget.repo_comment; 1831 if (down and !comment.bodyFocused()) { 1832 self.focusDetailChild(index, current, true, root_focus); 1833 return true; 1834 } 1835 if (!down and comment.bodyFocused()) { 1836 self.focusDetailChild(index, current, false, root_focus); 1837 return true; 1838 } 1839 } 1840 1841 var next = current; 1842 while (true) { 1843 if (down) { 1844 next += 1; 1845 if (next >= inner.children.count()) return false; 1846 } else { 1847 if (next == 0) return false; 1848 next -= 1; 1849 } 1850 if (!self.detailChildFocusable(index, next)) continue; 1851 self.focusDetailChild(index, next, !down, root_focus); 1852 return true; 1853 } 1854 } 1855 1856 fn moveCommentHorizontal(self: *View, index: usize, root_focus: *Focus, right: bool) bool { 1857 const inner = self.detailInner(index); 1858 const current = self.focusedDetailChild(index, root_focus) orelse return false; 1859 const child = &inner.children.values()[current]; 1860 if (child.widget == .repo_comment) { 1861 const moved = child.widget.repo_comment.moveHorizontal(root_focus, right); 1862 if (moved) if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 1863 return moved; 1864 } 1865 const row = switch (child.widget) { 1866 .box => |*box| box, 1867 else => return false, 1868 }; 1869 const selected = row.getFocus().child_id orelse return false; 1870 const selected_index = row.children.getIndex(selected) orelse return false; 1871 const target = if (right) selected_index + 1 else selected_index -| 1; 1872 if (target == selected_index or target >= row.children.count()) return false; 1873 root_focus.setFocus(row.children.keys()[target]); 1874 if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 1875 return true; 1876 } 1877 1878 // up/down (and tab/shift+tab) move between a form's fields; up from the 1879 // title crosses into the header tabs. the multiline description keeps 1880 // enter and any up/down that has a row to move to. 1881 fn formInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1882 const form = self.issueForm() orelse return; 1883 const cid = form.getFocus().child_id orelse return; 1884 const cur = form.children.getIndex(cid) orelse return; 1885 1886 if (cur == description_field_index) { 1887 const child = &form.children.values()[cur]; 1888 if (try multilineKeepsKey(&child.widget.text_input, allocator, key)) 1889 return child.widget.input(allocator, key, root_focus); 1890 } 1891 1892 switch (key) { 1893 .arrow_up, .back_tab => if (cur > 0) 1894 root_focus.setFocus(form.children.keys()[cur - 1]) 1895 else 1896 self.focusHeader(root_focus), 1897 .arrow_down, .tab => if (cur < submit_field_index) { 1898 root_focus.setFocus(form.children.keys()[cur + 1]); 1899 }, 1900 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 1901 .mouse => |mouse| if (cur == submit_field_index) { 1902 const submit = &form.children.values()[cur].widget.submit_button; 1903 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitForm(allocator); 1904 }, 1905 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 1906 } 1907 } 1908 1909 fn commentFormInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1910 const form = self.issueForm() orelse return; 1911 const cid = form.getFocus().child_id orelse return; 1912 const cur = form.children.getIndex(cid) orelse return; 1913 const child = &form.children.values()[cur]; 1914 1915 if (cur == comment_body_field_index and try multilineKeepsKey(&child.widget.text_input, allocator, key)) 1916 return child.widget.input(allocator, key, root_focus); 1917 1918 switch (key) { 1919 .arrow_up, .back_tab => if (cur > comment_author_field_index) 1920 root_focus.setFocus(form.children.keys()[cur - 1]) 1921 else 1922 self.focusHeader(root_focus), 1923 .arrow_down, .tab => if (cur < comment_submit_field_index) 1924 root_focus.setFocus(form.children.keys()[cur + 1]), 1925 .enter => if (cur == comment_submit_field_index) try self.submitComment(allocator), 1926 .mouse => |mouse| if (cur == comment_submit_field_index) { 1927 const submit = &child.widget.submit_button; 1928 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitComment(allocator); 1929 }, 1930 else => try child.widget.input(allocator, key, root_focus), 1931 } 1932 } 1933 1934 // the multiline hunk inputs keep enter and any up/down that has a row to 1935 // move to; enter on a "use this" link is a navigation the host follows 1936 // before this runs. 1937 fn resolveInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1938 const form = self.issueForm() orelse return; 1939 const cid = form.getFocus().child_id orelse return; 1940 const cur = form.children.getIndex(cid) orelse return; 1941 const child = &form.children.values()[cur]; 1942 1943 if (child.widget == .text_input) { 1944 const ti = &child.widget.text_input; 1945 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 1946 return child.widget.input(allocator, key, root_focus); 1947 } 1948 1949 const on_submit = child.widget == .submit_button; 1950 1951 switch (key) { 1952 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 1953 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 1954 .arrow_left, .arrow_right => switch (child.widget) { 1955 .box => |*row| { 1956 const row_cid = row.getFocus().child_id orelse return; 1957 const row_index = row.children.getIndex(row_cid) orelse return; 1958 if (key == .arrow_left) { 1959 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 1960 } else if (row_index + 1 < row.children.count()) { 1961 root_focus.setFocus(row.children.keys()[row_index + 1]); 1962 } 1963 }, 1964 else => try child.widget.input(allocator, key, root_focus), 1965 }, 1966 .enter => if (on_submit) try self.submitResolution(allocator), 1967 .mouse => |mouse| if (on_submit) { 1968 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 1969 }, 1970 else => try child.widget.input(allocator, key, root_focus), 1971 } 1972 } 1973 1974 // a multiline input keeps enter and any up/down that has a row to move to 1975 fn multilineKeepsKey(text_input: *wgt.TextInput(ui.Widget), allocator: std.mem.Allocator, key: Key) !bool { 1976 return switch (key) { 1977 .enter => true, 1978 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 1979 .arrow_down => !try text_input.cursorOnLastRow(allocator), 1980 else => false, 1981 }; 1982 } 1983 1984 // the neighboring focusable resolve-form child, skipping the spacer and 1985 // the blank gap rows 1986 fn resolveStep(form: *wgt.Box(ui.Widget), cur: usize, down: bool) ?usize { 1987 var i = cur; 1988 while (true) { 1989 if (down) { 1990 i += 1; 1991 if (i >= form.children.count()) return null; 1992 } else { 1993 if (i == 0) return null; 1994 i -= 1; 1995 } 1996 switch (form.children.values()[i].widget) { 1997 .spacer, .text => continue, 1998 else => return i, 1999 } 2000 } 2001 } 2002 2003 // focus the resolve-form child at `i` (a version row focuses its selected 2004 // child) and keep it visible on the terminal 2005 fn focusResolveChild(self: *View, form: *wgt.Box(ui.Widget), i: usize, root_focus: *Focus) void { 2006 const child = &form.children.values()[i]; 2007 switch (child.widget) { 2008 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2009 else => root_focus.setFocus(form.children.keys()[i]), 2010 } 2011 if (self.session.is_terminal) { 2012 if (self.resolveScroll()) |sc| { 2013 if (child.rect) |rect| sc.scrollToRect(rect); 2014 } 2015 } 2016 } 2017 2018 // re-emit the conflicted issue's event with the resolved content; any 2019 // event on the issue settles its conflict entry. this is the terminal 2020 // path; the web posts the form to the resolve route. 2021 fn submitResolution(self: *View, allocator: std.mem.Allocator) !void { 2022 if (comptime wasm) return; 2023 const io = self.session.io orelse return; 2024 const src = self.data.repo_source orelse return; 2025 const entry = self.data.selectedIssue() orelse return; 2026 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2027 const form = self.issueForm() orelse return; 2028 2029 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2030 var title: ?[]u8 = null; 2031 var tags: ?[]u8 = null; 2032 var hunks: std.ArrayList([]const u8) = .empty; 2033 defer { 2034 if (title) |t| allocator.free(t); 2035 if (tags) |t| allocator.free(t); 2036 for (hunks.items) |h| allocator.free(h); 2037 hunks.deinit(allocator); 2038 } 2039 for (form.children.values()) |*child| switch (child.widget) { 2040 .text_input => |*ti| { 2041 const text = try ti.text(allocator); 2042 errdefer allocator.free(text); 2043 if (std.mem.eql(u8, ti.options.name, "title")) { 2044 title = text; 2045 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2046 tags = text; 2047 } else { 2048 try hunks.append(allocator, text); 2049 } 2050 }, 2051 else => {}, 2052 }; 2053 2054 var id_bytes: [evt.event_id_size]u8 = undefined; 2055 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2056 2057 switch (src.repo_kind) { 2058 inline else => |repo_kind| { 2059 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2060 defer any_repo.deinit(io, allocator); 2061 switch (any_repo) { 2062 inline else => |*repo| evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .resolve = .{ 2063 .title = title, 2064 .tags = tags, 2065 .hunks = hunks.items, 2066 } }, author_email) catch |err| switch (err) { 2067 // leave the form up for correction 2068 error.InvalidFields => return, 2069 else => |e| return e, 2070 }, 2071 } 2072 }, 2073 } 2074 2075 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 2076 try self.session.navigate(route); 2077 } 2078 2079 // commit a new or edited comment and navigate to its permalink. this is the 2080 // terminal path; the web posts the form to its comment route. 2081 fn submitComment(self: *View, allocator: std.mem.Allocator) !void { 2082 if (comptime wasm) return; 2083 const io = self.session.io orelse return; 2084 const src = self.data.repo_source orelse return; 2085 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2086 const form = self.issueForm() orelse return; 2087 const body_input = &form.children.values()[comment_body_field_index].widget.text_input; 2088 const body = try body_input.text(allocator); 2089 defer allocator.free(body); 2090 if (!evt.Comment.fieldsValid(body)) return; 2091 2092 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2093 @memcpy(&thread_id, self.data.selected_id); 2094 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2095 var parent: [evt.event_id_size * 2]u8 = undefined; 2096 @memcpy(&parent, self.data.comment_id); 2097 break :blk parent; 2098 }; 2099 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2100 const editing = self.data.view == .edit_comment; 2101 switch (src.repo_kind) { 2102 inline else => |repo_kind| { 2103 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2104 defer any_repo.deinit(io, allocator); 2105 switch (any_repo) { 2106 inline else => |*repo| if (editing) { 2107 var comment_id: [evt.event_id_size]u8 = undefined; 2108 _ = std.fmt.hexToBytes(&comment_id, self.data.comment_id) catch return; 2109 try evt.Comment.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author_email); 2110 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2111 } else { 2112 event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author_email); 2113 }, 2114 } 2115 }, 2116 } 2117 2118 body_input.clear(allocator); 2119 const route = ui.RoutablePage.repoCommentsRoute(self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2120 try self.session.navigate(route); 2121 } 2122 2123 // the terminal submit paths: the new form commits a new-issue event, the 2124 // edit form re-emits the selected issue's event with the form's content. 2125 // the web posts the forms to their routes instead. 2126 fn submitForm(self: *View, allocator: std.mem.Allocator) !void { 2127 if (self.data.view == .edit) { 2128 try self.submitEditedIssue(allocator); 2129 } else { 2130 try self.submitNewIssue(allocator); 2131 } 2132 } 2133 2134 // commit the new issue to the repo's events branch and navigate to it. 2135 // this is the terminal path; the web posts the form to the issue route, 2136 // so the wasm side never runs (or compiles) the repo access below. 2137 fn submitNewIssue(self: *View, allocator: std.mem.Allocator) !void { 2138 if (comptime wasm) return; 2139 const io = self.session.io orelse return; 2140 const src = self.data.repo_source orelse return; 2141 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2142 2143 const form = self.issueForm() orelse return; 2144 const title_input = &form.children.values()[title_field_index].widget.text_input; 2145 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2146 const description_input = &form.children.values()[description_field_index].widget.text_input; 2147 2148 const title = try title_input.text(allocator); 2149 defer allocator.free(title); 2150 const tags = try tags_input.text(allocator); 2151 defer allocator.free(tags); 2152 const description = try description_input.text(allocator); 2153 defer allocator.free(description); 2154 2155 if (!evt.Issue.fieldsValid(title, tags)) return; 2156 2157 var id_bytes: [evt.event_id_size]u8 = undefined; 2158 io.random(&id_bytes); 2159 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2160 2161 const event = evt.EventWithId{ 2162 .id = event_id_hex, 2163 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2164 .author_email = author_email, 2165 .event = .{ .issue = .{ 2166 .title = title, 2167 .description = description, 2168 .tags = tags, 2169 } }, 2170 }; 2171 2172 switch (src.repo_kind) { 2173 inline else => |repo_kind| { 2174 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2175 defer any_repo.deinit(io, allocator); 2176 switch (any_repo) { 2177 inline else => |*repo| try evt.consume(repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2178 } 2179 }, 2180 } 2181 2182 // wipe the form so a return visit starts fresh 2183 title_input.clear(allocator); 2184 tags_input.clear(allocator); 2185 description_input.clear(allocator); 2186 2187 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, "", &event_id_hex) orelse return; 2188 try self.session.navigate(route); 2189 } 2190 2191 // re-emit the selected issue's event with the form's content (status 2192 // preserved), then reload the issue's page. this is the terminal path; 2193 // the web posts the form to the edit route. 2194 fn submitEditedIssue(self: *View, allocator: std.mem.Allocator) !void { 2195 if (comptime wasm) return; 2196 const io = self.session.io orelse return; 2197 const src = self.data.repo_source orelse return; 2198 const entry = self.data.selectedIssue() orelse return; 2199 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2200 2201 const form = self.issueForm() orelse return; 2202 const title_input = &form.children.values()[title_field_index].widget.text_input; 2203 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2204 const description_input = &form.children.values()[description_field_index].widget.text_input; 2205 2206 const title = try title_input.text(allocator); 2207 defer allocator.free(title); 2208 const tags = try tags_input.text(allocator); 2209 defer allocator.free(tags); 2210 const description = try description_input.text(allocator); 2211 defer allocator.free(description); 2212 2213 if (!evt.Issue.fieldsValid(title, tags)) return; 2214 2215 var id_bytes: [evt.event_id_size]u8 = undefined; 2216 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2217 2218 switch (src.repo_kind) { 2219 inline else => |repo_kind| { 2220 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2221 defer any_repo.deinit(io, allocator); 2222 switch (any_repo) { 2223 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2224 .title = title, 2225 .tags = tags, 2226 .description = description, 2227 } }, author_email), 2228 } 2229 }, 2230 } 2231 2232 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 2233 try self.session.navigate(route); 2234 } 2235 2236 // flip the shown issue's status by re-emitting its event, then reload the 2237 // page rooted at the issue so the view reflects the change. this is the 2238 // terminal path; the web posts the button's form to the status route. 2239 fn toggleIssueStatus(self: *View, allocator: std.mem.Allocator, index: usize) !void { 2240 if (comptime wasm) return; 2241 const io = self.session.io orelse return; 2242 const src = self.data.repo_source orelse return; 2243 const sel = self.detailed_index[index] orelse return; 2244 const entry = self.window(index).issues[sel]; 2245 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2246 2247 const status: evt.Issue.Status = switch (entry.issue.event.status) { 2248 .open => .closed, 2249 .closed => .open, 2250 }; 2251 2252 var id_bytes: [evt.event_id_size]u8 = undefined; 2253 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2254 2255 switch (src.repo_kind) { 2256 inline else => |repo_kind| { 2257 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2258 defer any_repo.deinit(io, allocator); 2259 switch (any_repo) { 2260 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .status = status }, author_email), 2261 } 2262 }, 2263 } 2264 2265 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, status, "", entry.id) orelse return; 2266 try self.session.navigate(route); 2267 } 2268 2269 // arrow keys move the tag selection; at the flow's edges focus crosses to 2270 // the neighboring widgets. 2271 fn tagsInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 2272 const tf = self.tagFlow(index) orelse return; 2273 const cid = tf.focus.child_id orelse return; 2274 const cur = tf.indexOfFocusId(cid) orelse return; 2275 const count = tf.text_boxes.items.len; 2276 switch (key) { 2277 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 2278 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 2279 .arrow_up => if (tf.rowStep(cur, false)) |i| self.focusTag(index, tf, root_focus, i) else self.focusAuthor(index, root_focus), 2280 .arrow_down => if (tf.rowStep(cur, true)) |i| self.focusTag(index, tf, root_focus, i) else try self.focusDescription(index, root_focus), 2281 .home => self.focusTag(index, tf, root_focus, 0), 2282 .end => self.focusTag(index, tf, root_focus, count - 1), 2283 else => {}, 2284 } 2285 } 2286 2287 const button_in_row_index: usize = 1; 2288 const title_child_index: usize = 0; 2289 const tags_child_index: usize = 2; 2290 2291 fn tagFlow(self: *View, index: usize) ?*ui.TagFlow { 2292 const inner = self.detailInner(index); 2293 if (inner.children.count() <= tags_child_index) return null; 2294 return switch (inner.children.values()[tags_child_index].widget) { 2295 .tag_flow => |*tf| tf, 2296 else => null, 2297 }; 2298 } 2299 2300 fn tagsFocused(self: *View, index: usize) bool { 2301 const inner = self.detailInner(index); 2302 const cid = inner.getFocus().child_id orelse return false; 2303 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 2304 } 2305 2306 // the open/close button inside the detail frame's tool row, identified 2307 // by its posting kind; a logged-out session's or conflicted issue's row 2308 // has none. 2309 fn statusButton(self: *View, index: usize) ?*wgt.TextBox(ui.Widget) { 2310 const row = self.toolRow(index); 2311 if (row.children.count() <= button_in_row_index) return null; 2312 switch (row.children.values()[button_in_row_index].widget) { 2313 .text_box => |*tb| switch (tb.box.focus.kind) { 2314 .custom => |custom| if (std.mem.eql(u8, custom, "submit")) return tb, 2315 else => {}, 2316 }, 2317 else => {}, 2318 } 2319 return null; 2320 } 2321 2322 fn toolRowFocused(self: *View, index: usize) bool { 2323 const outer = self.detailOuter(index); 2324 const cid = outer.getFocus().child_id orelse return false; 2325 return outer.children.getIndex(cid) == tool_row_index; 2326 } 2327 2328 fn focusTag(self: *View, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 2329 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 2330 // keep the tag visible on the terminal: its rect offset by the flow's 2331 // position in the pane. 2332 if (self.session.is_terminal and item < tf.rects.items.len) { 2333 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 2334 var rect = tf.rects.items[item]; 2335 rect.x += flow_rect.x; 2336 rect.y += flow_rect.y; 2337 self.detailScroll(index).scrollToRect(rect); 2338 } 2339 } 2340 } 2341 2342 fn titleFocused(self: *View, index: usize) bool { 2343 const inner = self.detailInner(index); 2344 const cid = inner.getFocus().child_id orelse return false; 2345 return inner.children.getIndex(cid) == title_child_index; 2346 } 2347 2348 fn authorPresent(self: *View, index: usize) bool { 2349 return self.author_id[index] != null; 2350 } 2351 2352 fn authorFocused(self: *View, index: usize) bool { 2353 const inner = self.detailInner(index); 2354 const cid = inner.getFocus().child_id orelse return false; 2355 return cid == self.author_id[index]; 2356 } 2357 2358 fn descriptionFocused(self: *View, index: usize) bool { 2359 const cid = self.detailInner(index).getFocus().child_id orelse return false; 2360 return cid == self.description_id[index]; 2361 } 2362 2363 // the author sits right under the title at the top of the pane, so 2364 // focusing it scrolls there. 2365 fn focusAuthor(self: *View, index: usize, root_focus: *Focus) void { 2366 const id = self.author_id[index] orelse return; 2367 root_focus.setFocus(id); 2368 self.detailScroll(index).y = 0; 2369 } 2370 2371 // the title sits at the top of the pane, so focusing it scrolls there. 2372 fn focusTitle(self: *View, index: usize, root_focus: *Focus) void { 2373 const inner = self.detailInner(index); 2374 if (inner.children.count() == 0) return; 2375 root_focus.setFocus(inner.children.keys()[title_child_index]); 2376 self.detailScroll(index).y = 0; 2377 } 2378 2379 fn focusTags(self: *View, index: usize, root_focus: *Focus) !void { 2380 const tf = self.tagFlow(index) orelse return; 2381 if (tf.text_boxes.items.len == 0) return; 2382 const cid = tf.focus.child_id orelse tf.text_boxes.items[0].getFocus().id; 2383 const item = tf.indexOfFocusId(cid) orelse 0; 2384 self.focusTag(index, tf, root_focus, item); 2385 } 2386 2387 fn focusDescription(self: *View, index: usize, root_focus: *Focus) !void { 2388 if (self.description_id[index]) |id| root_focus.setFocus(id); 2389 } 2390 2391 // return to the tool row's last-focused button (the header when the 2392 // description page shows no row). 2393 fn focusToolRow(self: *View, index: usize, root_focus: *Focus) void { 2394 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 2395 root_focus.setFocus(cid); 2396 } 2397 2398 // enter the detail pane. an empty pane (no issues) can't be entered. 2399 fn focusDetail(self: *View, index: usize, root_focus: *Focus) !void { 2400 if (self.detailInner(index).children.count() == 0) return; 2401 root_focus.setFocus(self.detailOuter(index).getFocus().id); 2402 } 2403 2404 // return to the list. 2405 fn focusList(self: *View, index: usize, root_focus: *Focus) !void { 2406 root_focus.setFocus(self.listScroll(index).getFocus().id); 2407 } 2408 2409 // cross to the header tabs above the stack. 2410 fn focusHeader(self: *View, root_focus: *Focus) void { 2411 root_focus.setFocus(self.box.children.keys()[header_index]); 2412 } 2413 2414 pub fn clearGrid(self: *View) void { 2415 self.box.clearGrid(); 2416 } 2417 2418 pub fn getGrid(self: View) ?Grid { 2419 return self.box.getGrid(); 2420 } 2421 2422 pub fn getFocus(self: *View) *Focus { 2423 return self.box.getFocus(); 2424 } 2425 2426 // for the parent's "scroll up at the top jumps to the header" check: at the 2427 // top only while the header tabs hold focus, so up from the split 2428 // crosses into the tabs first. 2429 pub fn getSelectedIndex(self: *View) ?usize { 2430 return if (self.headerActive()) 0 else 1; 2431 } 2432 }; 2433 2434 // the "a:" navigation link for `status`'s issues page filtered to the 2435 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 2436 // a rooted url ignores `status` (it derives its view from the issue's own); 2437 // with an empty `id` this is the bare list link. 2438 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 2439 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 2440 const url = try route.toUrl(page_arena); 2441 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2442 } 2443 2444 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 2445 // href is only followed with js off. 2446 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2447 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 2448 const url = try route.toUrl(page_arena); 2449 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 2450 } 2451 2452 // the "a:" link to the window `status_maybe` names, keeping the tag filter, 2453 // or the conflicts window when null, rooted at issue `id`. 2454 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?evt.Issue.Status, id: []const u8) ![]const u8 { 2455 if (status_maybe) |status| return issuesLink(page_arena, data.identity, status, data.tag, id); 2456 return conflictsLink(page_arena, data.identity, id); 2457 } 2458 2459 // the "a:" link to the conflicts list rooted at issue `start` ("" = the 2460 // first window). 2461 fn conflictsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, start: []const u8) ![]const u8 { 2462 const route = ui.RoutablePage.repoIssuesConflictsRoute(identity, start) orelse return error.RouteTooLong; 2463 const url = try route.toUrl(page_arena); 2464 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2465 } 2466 2467 // the "a:" link to the whole-description page of issue `id` within `identity`. 2468 fn descriptionLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2469 const route = ui.RoutablePage.repoIssuesDescriptionRoute(identity, id) orelse return error.RouteTooLong; 2470 const url = try route.toUrl(page_arena); 2471 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2472 } 2473 2474 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 2475 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 2476 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 2477 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 2478 const url = try route.toUrl(page_arena); 2479 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2480 } 2481 2482 // tabs switching between the issues page's views. 2483 pub const Header = struct { 2484 box: wgt.Box(ui.Widget), 2485 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 2486 2487 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 2488 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2489 errdefer box.deinit(allocator); 2490 2491 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 2492 errdefer tab_ids.deinit(allocator); 2493 2494 const aa = session.page_arena.allocator(); 2495 2496 // a list tab per status, labeled with its listing's issue count 2497 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 2498 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 2499 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2500 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 2501 try addTab(allocator, &box, &tab_ids, label, link); 2502 } 2503 2504 // tags tab, labeled with the active tag filter 2505 { 2506 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 2507 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 2508 const label = if (data.tag.len == 0) "tags" else blk: { 2509 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 2510 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 2511 }; 2512 try addTab(allocator, &box, &tab_ids, label, tags_link); 2513 } 2514 2515 // new-issue tab; an edit or resolve url shows its tab in this place 2516 { 2517 const route = switch (data.view) { 2518 .edit => ui.RoutablePage.repoIssuesEditRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 2519 .new_comment => ui.RoutablePage.repoCommentNewRoute(data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 2520 .edit_comment => ui.RoutablePage.repoCommentEditRoute(data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 2521 .resolve => ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 2522 else => ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong, 2523 }; 2524 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2525 const label: []const u8 = switch (data.view) { 2526 .edit => "edit", 2527 .new_comment => "reply", 2528 .edit_comment => "edit", 2529 .resolve => "resolve", 2530 else => "new", 2531 }; 2532 try addTab(allocator, &box, &tab_ids, label, link); 2533 } 2534 2535 // conflicts tab, labeled with the conflict count 2536 if (data.conflicts.count > 0) { 2537 const route = ui.RoutablePage.repoIssuesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 2538 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2539 const label = try std.fmt.allocPrint(aa, "conflicts ({d})", .{data.conflicts.count}); 2540 try addTab(allocator, &box, &tab_ids, label, link); 2541 } 2542 2543 var self = Header{ .box = box, .tab_ids = tab_ids }; 2544 // the tab matching the page's view is selected initially. 2545 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 2546 return self; 2547 } 2548 2549 fn addTab(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), tab_ids: *std.AutoArrayHashMapUnmanaged(usize, void), label: []const u8, link: []const u8) !void { 2550 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2551 errdefer text_box.deinit(allocator); 2552 text_box.getFocus().focusable = true; 2553 text_box.getFocus().kind = .{ .custom = link }; 2554 try tab_ids.put(allocator, text_box.getFocus().id, {}); 2555 try box.children.put(allocator, text_box.getFocus().id, .{ 2556 .widget = .{ .text_box = text_box }, 2557 .rect = null, 2558 .min_size = .{ .width = label.len + 2, .height = null }, 2559 }); 2560 } 2561 2562 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 2563 self.box.deinit(allocator); 2564 self.tab_ids.deinit(allocator); 2565 } 2566 2567 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2568 self.clearGrid(); 2569 // only the selected tab shows its border 2570 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 2571 switch (child.widget) { 2572 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 2573 else => {}, 2574 } 2575 } 2576 try self.box.build(allocator, constraint, root_focus); 2577 } 2578 2579 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2580 _ = allocator; 2581 const current_tab = self.currentTabIndex() orelse return; 2582 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 2583 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 2584 } 2585 } 2586 2587 pub fn clearGrid(self: *Header) void { 2588 self.box.clearGrid(); 2589 } 2590 2591 pub fn getGrid(self: Header) ?Grid { 2592 return self.box.getGrid(); 2593 } 2594 2595 pub fn getFocus(self: *Header) *Focus { 2596 return self.box.getFocus(); 2597 } 2598 2599 pub fn getSelectedIndex(self: Header) ?usize { 2600 return self.currentTabIndex(); 2601 } 2602 2603 fn currentTabIndex(self: Header) ?usize { 2604 const child_id = self.box.focus.child_id orelse return null; 2605 return self.tab_ids.getIndex(child_id); 2606 } 2607 }; 2608