╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1994 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 1995 } 1996 1997 fn listBox(self: *This, index: usize) *wgt.Box(Widget) { 1998 return &self.listScroll(index).child.box; 1999 } 2000 2001 fn detail(self: *This, index: usize) *DetailType { 2002 const frame = &self.resultsBox(index).children.values()[detail_index].widget.box; 2003 return &@field(frame.children.values()[0].widget, Data.detail_widget_name); 2004 } 2005 2006 fn window(self: *This, index: usize) *const Window { 2007 if (supports_conflicts and index == conflict_view_index) return &self.data.conflicts; 2008 if (supports_drafts and index == drafts_view_index) return &self.data.drafts; 2009 return self.data.window(splitStatus(index)); 2010 } 2011 2012 fn stackSelectedIndex(self: *This) ?usize { 2013 const stack = self.viewStack(); 2014 const cid = stack.getFocus().child_id orelse return null; 2015 return stack.children.getIndex(cid); 2016 } 2017 2018 // the stack's selected master-detail split (null when the tags view or the 2019 // new-thread form shows). 2020 fn selectedSplitIndex(self: *This) ?usize { 2021 const idx = self.stackSelectedIndex() orelse return null; 2022 return if (idx < status_count or (supports_drafts and idx == drafts_view_index) or idx == conflict_view_index) idx else null; 2023 } 2024 2025 fn detailActive(self: *This, index: usize) bool { 2026 const rb = self.resultsBox(index); 2027 const cid = rb.getFocus().child_id orelse return false; 2028 return rb.children.getIndex(cid) == detail_index; 2029 } 2030 2031 fn headerActive(self: *This) bool { 2032 const cid = self.box.getFocus().child_id orelse return false; 2033 return self.box.children.getIndex(cid) == header_index; 2034 } 2035 2036 fn tagsViewActive(self: *This) bool { 2037 if (self.headerActive()) return false; 2038 return self.stackSelectedIndex() == tags_view_index; 2039 } 2040 2041 fn formViewActive(self: *This) bool { 2042 if (self.headerActive()) return false; 2043 return self.stackSelectedIndex() == form_view_index; 2044 } 2045 2046 // the selected thread's index, or null when a window-navigation row is 2047 // selected (a leading "previous" row shifts the thread rows down by one). 2048 fn selectedThreadIndex(self: *This, index: usize) ?usize { 2049 const lb = self.listBox(index); 2050 const cid = lb.getFocus().child_id orelse return null; 2051 const idx = lb.children.getIndex(cid) orelse return null; 2052 const win = self.window(index); 2053 const lead: usize = if (win.prev_id != null) 1 else 0; 2054 if (idx < lead or idx - lead >= win.items.len) return null; 2055 return idx - lead; 2056 } 2057 2058 pub fn build(self: *This, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2059 self.clearGrid(); 2060 2061 // each header tab maps 1:1 to a stack child by position 2062 if (self.header().getSelectedIndex()) |index| { 2063 const stack = self.viewStack(); 2064 stack.getFocus().child_id = stack.children.keys()[index]; 2065 } 2066 2067 if (self.selectedSplitIndex()) |i| { 2068 // swap the detail pane to the selected thread when it changes. 2069 if (self.selectedThreadIndex(i)) |selected| { 2070 const entry = self.window(i).items[selected]; 2071 const changed = if (self.detail(i).entry) |current| !std.mem.eql(u8, current.id, entry.id) else true; 2072 if (changed) { 2073 try self.detail(i).setEntry(allocator, entry); 2074 } 2075 } 2076 2077 // the selected list row shows a border (the focused TextBox 2078 // upgrades it to a double border itself); the rest stay borderless. 2079 const lb = self.listBox(i); 2080 for (lb.children.keys(), lb.children.values()) |id, *child| { 2081 switch (child.widget) { 2082 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 2083 else => {}, 2084 } 2085 } 2086 2087 // cap the list at list_max_width only while the detail pane fits 2088 // beside it. the box drops the detail when the width can't hold 2089 // both minimums, so when it's that narrow we lift the cap and let 2090 // the list fill the whole width. 2091 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 2092 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 2093 2094 // stretch the detail pane across the rest of the width so it fills 2095 // the area rather than shrinking to its content; its scroll fills 2096 // the pane. 2097 if (constraint.max_size.width) |w| { 2098 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 2099 } else { 2100 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 2101 } 2102 } 2103 2104 // refresh the form inputs' entries in the session's focus-id -> input 2105 // map with this frame's addresses, so the web/wasm form handling can 2106 // find them by focus id 2107 if (self.threadForm()) |form| { 2108 const failure = if (self.session.formFeedback(feedback_tag)) |saved| saved.failure else null; 2109 form.children.values()[title_field_index].widget.text_input.options.label = if (failure == .required_title) " title (required) " else " title "; 2110 if (comptime supports_drafts) { 2111 const invalid_target = failure == .invalid_target_branch; 2112 form.children.values()[target_branch_field_index].widget.text_input.options.label = if (invalid_target) " target branch (not found) " else " target branch "; 2113 } 2114 } 2115 if (self.formBox()) |form| { 2116 const inputs_arena = self.session.arena.allocator(); 2117 for (form.children.values()) |*child| switch (child.widget) { 2118 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 2119 else => {}, 2120 }; 2121 } 2122 2123 try self.box.build(allocator, constraint, root_focus); 2124 } 2125 2126 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 2127 // wheel input moves focus like the arrows 2128 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 2129 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 2130 else 2131 raw_key; 2132 2133 if (self.headerActive()) { 2134 // down from the tabs re-enters the stack if the selected view has 2135 // something to focus; other keys move the tabs. 2136 if (inp.vertDirection(key) == .down) { 2137 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 2138 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 2139 // nothing focusable in the unauthorized view 2140 .unauthorized => false, 2141 else => true, 2142 } else false; 2143 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 2144 } else { 2145 try self.header().input(allocator, key, root_focus); 2146 } 2147 return; 2148 } 2149 if (self.tagsViewActive()) { 2150 self.tagsViewInput(key, root_focus); 2151 return; 2152 } 2153 if (self.formViewActive()) { 2154 if (confirmation(self.data.view) != null) { 2155 try self.confirmationFormInput(allocator, key, root_focus); 2156 } else if (supports_conflicts and self.data.view == .resolve) { 2157 try self.resolveInput(allocator, key, root_focus); 2158 } else if (self.data.view == .new_comment or self.data.view == .edit_comment) { 2159 try self.commentFormInput(allocator, key, root_focus); 2160 } else { 2161 try self.formInput(allocator, key, root_focus); 2162 } 2163 return; 2164 } 2165 const i = self.selectedSplitIndex() orelse return; 2166 if (self.detailActive(i)) { 2167 switch (try self.detail(i).inputWithExit(allocator, key, root_focus)) { 2168 .none => {}, 2169 .list => self.focusList(i, root_focus), 2170 .header => self.focusHeader(root_focus), 2171 } 2172 } else { 2173 self.listInput(i, key, root_focus); 2174 } 2175 } 2176 2177 // arrow keys move the tag selection; up from the top row crosses to the 2178 // header tabs. 2179 fn tagsViewInput(self: *This, key: Key, root_focus: *Focus) void { 2180 const tf = self.tagsView(); 2181 const cid = tf.focus.child_id orelse return; 2182 const cur = tf.indexOfFocusId(cid) orelse return; 2183 const count = tf.text_boxes.items.len; 2184 switch (key) { 2185 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 2186 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 2187 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 2188 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 2189 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 2190 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 2191 else => {}, 2192 } 2193 } 2194 2195 fn listInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 2196 // up/down (and the scroll wheel) move the selection a row; page up/down 2197 // jump a fixed amount. right/Enter cross into the detail pane. up from 2198 // the top row crosses into the header tabs. 2199 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 2200 const lb = self.listBox(index); 2201 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 2202 if (delta < 0 and at_top) return self.focusHeader(root_focus); 2203 moveRowFocus(lb, self.listScroll(index), root_focus, delta); 2204 return; 2205 } 2206 switch (key) { 2207 .enter, .arrow_right => _ = self.detail(index).focusFirst(root_focus), 2208 else => {}, 2209 } 2210 } 2211 2212 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2213 const form = self.threadForm() orelse return; 2214 const cid = form.getFocus().child_id orelse return; 2215 const cur = form.children.getIndex(cid) orelse return; 2216 2217 if (cur == description_field_index) { 2218 const child = &form.children.values()[cur]; 2219 if (try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2220 return child.widget.input(allocator, key, root_focus); 2221 } 2222 2223 switch (key) { 2224 .arrow_up, .back_tab => if (cur > 0) 2225 root_focus.setFocus(form.children.keys()[cur - 1]) 2226 else 2227 self.focusHeader(root_focus), 2228 .arrow_down, .tab => if (cur < submit_field_index) { 2229 root_focus.setFocus(form.children.keys()[cur + 1]); 2230 }, 2231 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 2232 .mouse => |mouse| if (cur == submit_field_index) { 2233 const submit = &form.children.values()[cur].widget.submit_button; 2234 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitForm(allocator); 2235 }, 2236 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 2237 } 2238 } 2239 2240 fn commentFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2241 const form = self.formBox() orelse return; 2242 const cid = form.getFocus().child_id orelse return; 2243 const cur = form.children.getIndex(cid) orelse return; 2244 const child = &form.children.values()[cur]; 2245 2246 if (cur == comment_body_field_index and try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2247 return child.widget.input(allocator, key, root_focus); 2248 2249 switch (key) { 2250 .arrow_up, .back_tab => if (cur > comment_author_field_index) 2251 root_focus.setFocus(form.children.keys()[cur - 1]) 2252 else 2253 self.focusHeader(root_focus), 2254 .arrow_down, .tab => if (cur < comment_submit_field_index) 2255 root_focus.setFocus(form.children.keys()[cur + 1]), 2256 .enter => if (cur == comment_submit_field_index) try self.submitComment(allocator), 2257 .mouse => |mouse| if (cur == comment_submit_field_index) { 2258 const submit = &child.widget.submit_button; 2259 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitComment(allocator); 2260 }, 2261 else => try child.widget.input(allocator, key, root_focus), 2262 } 2263 } 2264 2265 fn confirmationFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2266 const form = self.confirmationForm() orelse return; 2267 const child_id = form.getFocus().child_id orelse return; 2268 const index = form.children.getIndex(child_id) orelse return; 2269 switch (key) { 2270 .arrow_up, .back_tab => if (index > 1) 2271 root_focus.setFocus(form.children.keys()[index - 1]) 2272 else 2273 self.focusHeader(root_focus), 2274 .arrow_down, .tab => if (index + 1 < form.children.count()) root_focus.setFocus(form.children.keys()[index + 1]), 2275 .enter => try self.submitConfirmation(allocator, index), 2276 .mouse => |mouse| for (form.children.keys()[1..], 1..) |button_id, button_index| { 2277 if (inp.leftClickOn(root_focus, button_id, mouse)) { 2278 try self.submitConfirmation(allocator, button_index); 2279 break; 2280 } 2281 }, 2282 else => {}, 2283 } 2284 } 2285 2286 // the multiline hunk inputs keep enter and any up/down that has a row to 2287 // move to; enter on a "use this" link is a navigation the host follows 2288 // before this runs. 2289 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2290 const form = self.formBox() orelse return; 2291 const cid = form.getFocus().child_id orelse return; 2292 const cur = form.children.getIndex(cid) orelse return; 2293 const child = &form.children.values()[cur]; 2294 2295 if (child.widget == .text_input) { 2296 const ti = &child.widget.text_input; 2297 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2298 return child.widget.input(allocator, key, root_focus); 2299 } 2300 2301 const on_submit = child.widget == .submit_button; 2302 2303 switch (key) { 2304 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2305 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2306 .arrow_left, .arrow_right => switch (child.widget) { 2307 .box => |*row| { 2308 const row_cid = row.getFocus().child_id orelse return; 2309 const row_index = row.children.getIndex(row_cid) orelse return; 2310 if (key == .arrow_left) { 2311 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 2312 } else if (row_index + 1 < row.children.count()) { 2313 root_focus.setFocus(row.children.keys()[row_index + 1]); 2314 } 2315 }, 2316 else => try child.widget.input(allocator, key, root_focus), 2317 }, 2318 .enter => if (on_submit) try self.submitResolution(allocator), 2319 .mouse => |mouse| if (on_submit) { 2320 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2321 }, 2322 else => try child.widget.input(allocator, key, root_focus), 2323 } 2324 } 2325 2326 // a multiline input keeps enter and any up/down that has a row to move to 2327 fn multilineKeepsKey(text_input: *wgt.TextInput, allocator: std.mem.Allocator, key: Key) !bool { 2328 return switch (key) { 2329 .enter => true, 2330 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2331 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2332 else => false, 2333 }; 2334 } 2335 2336 // the neighboring focusable resolve-form child, skipping the spacer and 2337 // the blank gap rows 2338 fn resolveStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2339 var i = cur; 2340 while (true) { 2341 if (down) { 2342 i += 1; 2343 if (i >= form.children.count()) return null; 2344 } else { 2345 if (i == 0) return null; 2346 i -= 1; 2347 } 2348 switch (form.children.values()[i].widget) { 2349 .spacer, .text => continue, 2350 else => return i, 2351 } 2352 } 2353 } 2354 2355 // focus the resolve-form child at `i` (a version row focuses its selected 2356 // child) and keep it visible on the terminal 2357 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2358 const child = &form.children.values()[i]; 2359 switch (child.widget) { 2360 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2361 else => root_focus.setFocus(form.children.keys()[i]), 2362 } 2363 if (self.session.is_terminal) { 2364 if (self.resolveScroll()) |sc| { 2365 if (child.rect) |rect| sc.scrollToRect(rect); 2366 } 2367 } 2368 } 2369 2370 // re-emit the conflicted thread's event with the resolved content; any 2371 // event on the thread settles its conflict entry. this is the terminal 2372 // path; the web posts the form to the resolve route. 2373 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2374 if (comptime wasm) return; 2375 const io = self.session.io orelse return; 2376 const src = self.data.repo_source orelse return; 2377 const entry = self.data.selectedThread() orelse return; 2378 const author = (try self.session.eventAuthor()) orelse return; 2379 const form = self.formBox() orelse return; 2380 2381 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2382 var title: ?[]u8 = null; 2383 var tags: ?[]u8 = null; 2384 var hunks: std.ArrayList([]const u8) = .empty; 2385 defer { 2386 if (title) |t| allocator.free(t); 2387 if (tags) |t| allocator.free(t); 2388 for (hunks.items) |h| allocator.free(h); 2389 hunks.deinit(allocator); 2390 } 2391 for (form.children.values()) |*child| switch (child.widget) { 2392 .text_input => |*ti| { 2393 const text = try ti.text(allocator); 2394 errdefer allocator.free(text); 2395 if (std.mem.eql(u8, ti.options.name, "title")) { 2396 title = text; 2397 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2398 tags = text; 2399 } else { 2400 try hunks.append(allocator, text); 2401 } 2402 }, 2403 else => {}, 2404 }; 2405 2406 const id_bytes = try evt.parseEventId(entry.id); 2407 2408 switch (src.repo_kind) { 2409 inline else => |repo_kind| { 2410 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2411 defer any_repo.deinit(io, allocator); 2412 switch (any_repo) { 2413 inline else => |*repo| { 2414 var resolution: Event.Resolve = .{ 2415 .title = title, 2416 .tags = tags, 2417 .hunks = hunks.items, 2418 }; 2419 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2420 const change = Event.Update{ .resolve = resolution }; 2421 Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2422 // leave the form up for correction 2423 error.InvalidFields => return, 2424 else => |e| return e, 2425 }; 2426 }, 2427 } 2428 }, 2429 } 2430 2431 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2432 try self.session.navigate(route); 2433 } 2434 2435 // commit a new or edited comment and navigate to its permalink. this is the 2436 // terminal path; the web posts the form to its comment route. 2437 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2438 if (comptime wasm) return; 2439 const io = self.session.io orelse return; 2440 const src = self.data.repo_source orelse return; 2441 const author = (try self.session.eventAuthor()) orelse return; 2442 const form = self.formBox() orelse return; 2443 const body_input = &form.children.values()[comment_body_field_index].widget.text_input; 2444 const body = try body_input.text(allocator); 2445 defer allocator.free(body); 2446 if (!evt.Comment.fieldsValid(body)) return; 2447 2448 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2449 @memcpy(&thread_id, self.data.selected_id); 2450 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2451 var parent: [evt.event_id_size * 2]u8 = undefined; 2452 @memcpy(&parent, self.data.comment_id); 2453 break :blk parent; 2454 }; 2455 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2456 const editing = self.data.view == .edit_comment; 2457 switch (src.repo_kind) { 2458 inline else => |repo_kind| { 2459 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2460 defer any_repo.deinit(io, allocator); 2461 switch (any_repo) { 2462 inline else => |*repo| if (editing) { 2463 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2464 try evt.Comment.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2465 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2466 } else { 2467 event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2468 }, 2469 } 2470 }, 2471 } 2472 2473 body_input.clear(allocator); 2474 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2475 try self.session.navigate(route); 2476 } 2477 2478 // the terminal submit paths: the new form commits a new thread event, the 2479 // edit form re-emits the selected thread's event with the form's content. 2480 // the web posts the forms to their routes instead. 2481 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2482 if (self.data.view == .edit) { 2483 try self.submitEditedThread(allocator); 2484 } else { 2485 try self.submitNewThread(allocator); 2486 } 2487 } 2488 2489 // commit the new thread to the repo's events branch and navigate to it. 2490 // this is the terminal path; the web posts the form to the thread route, 2491 // so the wasm side never runs (or compiles) the repo access below. 2492 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2493 if (comptime wasm) return; 2494 const io = self.session.io orelse return; 2495 const src = self.data.repo_source orelse return; 2496 const author = (try self.session.eventAuthor()) orelse return; 2497 2498 const form = self.threadForm() orelse return; 2499 const title_input = &form.children.values()[title_field_index].widget.text_input; 2500 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2501 const description_input = &form.children.values()[description_field_index].widget.text_input; 2502 2503 const title = try title_input.text(allocator); 2504 defer allocator.free(title); 2505 const tags = try tags_input.text(allocator); 2506 defer allocator.free(tags); 2507 const description = try description_input.text(allocator); 2508 defer allocator.free(description); 2509 const target_branch = if (comptime supports_drafts) try form.children.values()[target_branch_field_index].widget.text_input.text(allocator) else ""; 2510 defer if (comptime supports_drafts) allocator.free(target_branch); 2511 2512 if (!Event.fieldsValid(title, tags)) { 2513 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, tags, description, target_branch); 2514 return; 2515 } 2516 2517 if (comptime supports_drafts) { 2518 const event_id_hex = Data.createDraft(self.data, self.session, allocator, title, tags, description, target_branch) catch |err| switch (err) { 2519 error.InvalidFields => { 2520 try self.rememberThreadFeedback(.invalid_target_branch, title, tags, description, target_branch); 2521 return; 2522 }, 2523 else => |other| return other, 2524 }; 2525 title_input.clear(allocator); 2526 tags_input.clear(allocator); 2527 description_input.clear(allocator); 2528 form.children.values()[target_branch_field_index].widget.text_input.clear(allocator); 2529 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2530 try self.session.navigate(route); 2531 return; 2532 } 2533 2534 var id_bytes: [evt.event_id_size]u8 = undefined; 2535 io.random(&id_bytes); 2536 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2537 2538 const event = evt.EventWithId{ 2539 .id = event_id_hex, 2540 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2541 .author = author, 2542 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2543 .title = title, 2544 .description = description, 2545 .tags = tags, 2546 }), 2547 }; 2548 2549 switch (src.repo_kind) { 2550 inline else => |repo_kind| { 2551 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2552 defer any_repo.deinit(io, allocator); 2553 switch (any_repo) { 2554 inline else => |*repo| try evt.consume(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2555 } 2556 }, 2557 } 2558 2559 // wipe the form so a return visit starts fresh 2560 title_input.clear(allocator); 2561 tags_input.clear(allocator); 2562 description_input.clear(allocator); 2563 2564 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2565 try self.session.navigate(route); 2566 } 2567 2568 // re-emit the selected thread's event with the form's content (status 2569 // preserved), then reload the thread's page. this is the terminal path; 2570 // the web posts the form to the edit route. 2571 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2572 if (comptime wasm) return; 2573 const io = self.session.io orelse return; 2574 const entry = self.data.selectedThread() orelse return; 2575 2576 const form = self.threadForm() orelse return; 2577 const title_input = &form.children.values()[title_field_index].widget.text_input; 2578 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2579 const description_input = &form.children.values()[description_field_index].widget.text_input; 2580 2581 const title = try title_input.text(allocator); 2582 defer allocator.free(title); 2583 const tags = try tags_input.text(allocator); 2584 defer allocator.free(tags); 2585 const description = try description_input.text(allocator); 2586 defer allocator.free(description); 2587 const target_branch = if (comptime supports_drafts) try form.children.values()[target_branch_field_index].widget.text_input.text(allocator) else ""; 2588 defer if (comptime supports_drafts) allocator.free(target_branch); 2589 2590 if (!Event.fieldsValid(title, tags)) { 2591 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, tags, description, target_branch); 2592 return; 2593 } 2594 const src = self.data.repo_source orelse return; 2595 2596 if (comptime supports_drafts) { 2597 if (entryDraft(entry.*)) { 2598 self.data.editDraft(self.session, allocator, entry.id, title, tags, description, target_branch) catch |err| switch (err) { 2599 error.InvalidFields => { 2600 try self.rememberThreadFeedback(.invalid_target_branch, title, tags, description, target_branch); 2601 return; 2602 }, 2603 else => |other| return other, 2604 }; 2605 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2606 try self.session.navigate(route); 2607 return; 2608 } 2609 } 2610 2611 const id_bytes = try evt.parseEventId(entry.id); 2612 const author = (try self.session.eventAuthor()) orelse return; 2613 2614 switch (src.repo_kind) { 2615 inline else => |repo_kind| { 2616 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2617 defer any_repo.deinit(io, allocator); 2618 switch (any_repo) { 2619 inline else => |*repo| if (supports_drafts) { 2620 Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2621 .title = title, 2622 .tags = tags, 2623 .description = description, 2624 .target_branch = target_branch, 2625 } }, author) catch |err| switch (err) { 2626 error.InvalidFields => { 2627 try self.rememberThreadFeedback(.invalid_target_branch, title, tags, description, target_branch); 2628 return; 2629 }, 2630 else => |other| return other, 2631 }; 2632 } else if (has_status) 2633 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2634 .title = title, 2635 .tags = tags, 2636 .description = description, 2637 } }, author) 2638 else 2639 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 2640 } 2641 }, 2642 } 2643 2644 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2645 try self.session.navigate(route); 2646 } 2647 2648 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, tags: []const u8, description: []const u8, target_branch: []const u8) !void { 2649 const aa = self.session.arena.allocator(); 2650 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 2651 .failure = failure, 2652 .fields = if (comptime supports_drafts) .{ 2653 .title = try aa.dupe(u8, title), 2654 .tags = try aa.dupe(u8, tags), 2655 .description = try aa.dupe(u8, description), 2656 .target_branch = try aa.dupe(u8, target_branch), 2657 } else .{ 2658 .title = try aa.dupe(u8, title), 2659 .tags = try aa.dupe(u8, tags), 2660 .description = try aa.dupe(u8, description), 2661 }, 2662 }); 2663 } 2664 2665 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 2666 switch (confirmation(self.data.view) orelse return) { 2667 .publish => if (comptime supports_drafts) { 2668 if (comptime wasm) return; 2669 const entry = self.data.selectedThread() orelse return; 2670 if (!entryDraft(entry.*)) return; 2671 try self.data.publishDraft(self.session, allocator, entry.id); 2672 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 2673 try self.session.navigate(route); 2674 } else unreachable, 2675 .merge => if (comptime supports_merge) { 2676 if (comptime wasm) return; 2677 const entry = self.data.selectedThread() orelse return; 2678 try self.data.mergePatch(self.session, allocator, entry.id, button_index > 1); 2679 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 2680 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 2681 try self.session.navigate(route); 2682 } else unreachable, 2683 .remove => try self.submitRemove(allocator), 2684 } 2685 } 2686 2687 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2688 const comment = self.data.comment_id.len != 0; 2689 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2690 const id = evt.parseEventId(event_id) catch return; 2691 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2692 } 2693 2694 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2695 if (comptime wasm) return; 2696 2697 if (comptime supports_drafts) { 2698 const selected = self.data.selectedThread() orelse return; 2699 if (event_kind == kind and entryDraft(selected.*)) { 2700 try Data.removeDraft(self.session, allocator, &id); 2701 const route = draftsRoute(self.data.identity) orelse return; 2702 try self.session.navigate(route); 2703 return; 2704 } 2705 } 2706 2707 const io = self.session.io orelse return; 2708 const src = self.data.repo_source orelse return; 2709 const author = (try self.session.eventAuthor()) orelse return; 2710 2711 switch (src.repo_kind) { 2712 inline else => |repo_kind| { 2713 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2714 defer any_repo.deinit(io, allocator); 2715 switch (any_repo) { 2716 inline else => |*repo| try evt.remove(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2717 } 2718 }, 2719 } 2720 2721 const id_hex = std.fmt.bytesToHex(id, .lower); 2722 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 2723 try self.session.navigate(route); 2724 } 2725 2726 // return to the list. 2727 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2728 root_focus.setFocus(self.listScroll(index).getFocus().id); 2729 } 2730 2731 // cross to the header tabs above the stack. 2732 fn focusHeader(self: *This, root_focus: *Focus) void { 2733 root_focus.setFocus(self.box.children.keys()[header_index]); 2734 } 2735 2736 pub fn clearGrid(self: *This) void { 2737 self.box.clearGrid(); 2738 } 2739 2740 pub fn getGrid(self: This) ?Grid { 2741 return self.box.getGrid(); 2742 } 2743 2744 pub fn getFocus(self: *This) *Focus { 2745 return self.box.getFocus(); 2746 } 2747 2748 // for the parent's "scroll up at the top jumps to the header" check: at the 2749 // top only while the header tabs hold focus, so up from the split 2750 // crosses into the tabs first. 2751 pub fn atTop(self: *This) bool { 2752 return self.headerActive(); 2753 } 2754 }; 2755 } 2756