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