╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2014 } 2015 } 2016 2017 try addSubmitButtonLabeled(allocator, &box, "submit"); 2018 2019 box.getFocus().child_id = box.children.keys()[0]; 2020 return box; 2021 } 2022 2023 // one conflicted scalar field: both sides' version rows, then the 2024 // resolution input prefilled from the picked side. 2025 fn addFieldConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 2026 const aa = session.page_arena.allocator(); 2027 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 2028 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 2029 2030 var resolution_input = try wgt.TextInput.init(allocator, .{ .label = " resolution ", .name = name, .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 2031 errdefer resolution_input.deinit(allocator); 2032 resolution_input.getFocus().mode = .all; 2033 try resolution_input.setContent(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text); 2034 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 2035 } 2036 2037 // atomic fields can only pick one complete side 2038 fn addAtomicConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 2039 const aa = session.page_arena.allocator(); 2040 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 2041 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 2042 var selected = try wgt.TextBox.init(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text, .{ 2043 .border_style = .single, 2044 .rounded_corners = true, 2045 .wrap_kind = .word, 2046 .label = " resolution ", 2047 }); 2048 errdefer selected.deinit(allocator); 2049 selected.getFocus().mode = .all; 2050 try box.children.put(allocator, selected.getFocus().id, .{ .widget = .{ .text_box = selected }, .rect = null, .min_size = null }); 2051 } 2052 2053 // a blank row setting a conflict group apart from its neighbors 2054 fn addGap(allocator: std.mem.Allocator, box: *wgt.Box(Widget)) !void { 2055 var text = try wgt.Text.init(allocator, " "); 2056 errdefer text.deinit(allocator); 2057 try box.children.put(allocator, text.getFocus().id, .{ .widget = .{ .text = text }, .rect = null, .min_size = null }); 2058 } 2059 2060 fn addLabel(allocator: std.mem.Allocator, box: *wgt.Box(Widget), content: []const u8) !void { 2061 var label = try SectionLabel.init(allocator, content); 2062 errdefer label.deinit(allocator); 2063 try box.children.put(allocator, label.getFocus().id, .{ .widget = .{ .section_label = label }, .rect = null, .min_size = null }); 2064 } 2065 2066 // one side of a conflict: the "use this" link on the left, the version 2067 // itself as a labeled read-only box. 2068 fn addVersionRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8, text: []const u8, link: []const u8) !void { 2069 var row = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2070 errdefer row.deinit(allocator); 2071 2072 { 2073 const use_label = "use this"; 2074 var use = try wgt.TextBox.init(allocator, use_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2075 errdefer use.deinit(allocator); 2076 use.getFocus().mode = .all; 2077 use.getFocus().kind = .{ .custom = link }; 2078 try row.children.put(allocator, use.getFocus().id, .{ 2079 .widget = .{ .text_box = use }, 2080 .rect = null, 2081 .min_size = .{ .width = use_label.len + 2, .height = null }, 2082 .max_size = .{ .width = use_label.len + 2, .height = null }, 2083 }); 2084 } 2085 2086 { 2087 var tb = try wgt.TextBox.init(allocator, if (text.len > 0) text else "(removed)", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = label }); 2088 errdefer tb.deinit(allocator); 2089 tb.getFocus().mode = .all; 2090 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2091 } 2092 2093 row.getFocus().child_id = row.children.keys()[0]; 2094 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 2095 } 2096 2097 // the label of one side's version box, naming its author 2098 fn sideLabel(aa: std.mem.Allocator, author: ui.Author, ours: bool) ![]const u8 { 2099 return switch (author) { 2100 .user_name, .email => |name| try std.fmt.allocPrint(aa, " edited by {s} ", .{name}), 2101 .unknown => if (ours) " current version " else " their version ", 2102 }; 2103 } 2104 2105 // the a: link that reloads the resolve page with `name` picked from ours 2106 // or theirs, reprefilling its resolution input 2107 fn useThisLink(session: *ui.Session, data: *const Self, name: []const u8, theirs: bool) ![]const u8 { 2108 const aa = session.page_arena.allocator(); 2109 var picks: std.ArrayList(u8) = .empty; 2110 var it = std.mem.splitScalar(u8, data.theirs_picks, ','); 2111 while (it.next()) |pick| { 2112 if (pick.len == 0 or std.mem.eql(u8, pick, name)) continue; 2113 if (picks.items.len > 0) try picks.append(aa, ','); 2114 try picks.appendSlice(aa, pick); 2115 } 2116 if (theirs) { 2117 if (picks.items.len > 0) try picks.append(aa, ','); 2118 try picks.appendSlice(aa, name); 2119 } 2120 const route = resolveRoute(data.identity, data.selected_id, picks.items) orelse return error.RouteTooLong; 2121 return std.fmt.allocPrint(aa, "a:{s}", .{try route.toUrl(session.page_arena)}); 2122 } 2123 2124 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), text: []const u8, bottom_label: []const u8, link: []const u8) !void { 2125 var row = try wgt.TextBox.init(allocator, text, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word, .bottom_label = bottom_label }); 2126 errdefer row.deinit(allocator); 2127 row.getFocus().mode = .all; 2128 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 2129 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 2130 } 2131 2132 pub fn deinit(self: *This, allocator: std.mem.Allocator) void { 2133 self.box.deinit(allocator); 2134 } 2135 2136 fn header(self: *This) *HeaderType { 2137 return &@field(self.box.children.values()[header_index].widget, Data.header_widget_name); 2138 } 2139 2140 fn viewStack(self: *This) *wgt.Stack(Widget) { 2141 return &self.box.children.values()[stack_index].widget.stack; 2142 } 2143 2144 // `index`'s master-detail split inside the stack. 2145 fn resultsBox(self: *This, index: usize) *wgt.Box(Widget) { 2146 return &self.viewStack().children.values()[index].box; 2147 } 2148 2149 // the tags view's flow inside the stack. 2150 fn tagsView(self: *This) *TagFlow { 2151 return &self.viewStack().children.values()[tags_view_index].tag_flow; 2152 } 2153 2154 // the non-confirmation form inside the stack 2155 fn formBox(self: *This) ?*wgt.Box(Widget) { 2156 return switch (self.viewStack().children.values()[form_view_index]) { 2157 .box => |*box| box, 2158 // the resolve form sits inside a scroll on the terminal 2159 .scroll => |*scroll| &scroll.child.box, 2160 else => null, 2161 }; 2162 } 2163 2164 fn threadForm(self: *This) ?*wgt.Box(Widget) { 2165 if (supports_conflicts and self.data.view == .resolve) return null; 2166 return switch (self.data.view) { 2167 .new_comment, .edit_comment => null, 2168 else => self.formBox(), 2169 }; 2170 } 2171 2172 fn formField(form: *wgt.Box(Widget), name: []const u8) !*wgt.TextInput { 2173 for (form.children.values()) |*child| switch (child.widget) { 2174 .text_input => |*text_input| if (std.mem.eql(u8, text_input.options.name, name)) return text_input, 2175 else => {}, 2176 }; 2177 return error.MissingFormField; 2178 } 2179 2180 fn formWidget(form: *wgt.Box(Widget), comptime widget_kind: std.meta.Tag(Widget)) ?*@FieldType(Widget, @tagName(widget_kind)) { 2181 for (form.children.values()) |*child| { 2182 if (child.widget == widget_kind) return &@field(child.widget, @tagName(widget_kind)); 2183 } 2184 return null; 2185 } 2186 2187 fn confirmationForm(self: *This) ?*wgt.Box(Widget) { 2188 return switch (self.viewStack().children.values()[form_view_index]) { 2189 .center => |*center| switch (center.child.*) { 2190 .box => |*box| box, 2191 else => null, 2192 }, 2193 else => null, 2194 }; 2195 } 2196 2197 // the resolve form's scroll on the terminal (the web page scrolls itself) 2198 fn resolveScroll(self: *This) ?*wgt.Scroll(Widget) { 2199 return switch (self.viewStack().children.values()[form_view_index]) { 2200 .scroll => |*scroll| scroll, 2201 else => null, 2202 }; 2203 } 2204 2205 fn listScroll(self: *This, index: usize) *wgt.Scroll(Widget) { 2206 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 2207 } 2208 2209 fn listBox(self: *This, index: usize) *wgt.Box(Widget) { 2210 return &self.listScroll(index).child.box; 2211 } 2212 2213 fn detail(self: *This, index: usize) *DetailType { 2214 const frame = &self.resultsBox(index).children.values()[detail_index].widget.box; 2215 return &@field(frame.children.values()[0].widget, Data.detail_widget_name); 2216 } 2217 2218 fn window(self: *This, index: usize) *const Window { 2219 if (supports_conflicts and index == conflict_view_index) return &self.data.conflicts; 2220 if (supports_drafts and index == drafts_view_index) return &self.data.drafts; 2221 return self.data.window(splitStatus(index)); 2222 } 2223 2224 fn stackSelectedIndex(self: *This) ?usize { 2225 const stack = self.viewStack(); 2226 const cid = stack.getFocus().child_id orelse return null; 2227 return stack.children.getIndex(cid); 2228 } 2229 2230 // the stack's selected master-detail split (null when the tags view or the 2231 // new-thread form shows). 2232 fn selectedSplitIndex(self: *This) ?usize { 2233 const idx = self.stackSelectedIndex() orelse return null; 2234 return if (idx < status_count or (supports_drafts and idx == drafts_view_index) or idx == conflict_view_index) idx else null; 2235 } 2236 2237 fn detailActive(self: *This, index: usize) bool { 2238 const rb = self.resultsBox(index); 2239 const cid = rb.getFocus().child_id orelse return false; 2240 return rb.children.getIndex(cid) == detail_index; 2241 } 2242 2243 fn headerActive(self: *This) bool { 2244 const cid = self.box.getFocus().child_id orelse return false; 2245 return self.box.children.getIndex(cid) == header_index; 2246 } 2247 2248 fn tagsViewActive(self: *This) bool { 2249 if (self.headerActive()) return false; 2250 return self.stackSelectedIndex() == tags_view_index; 2251 } 2252 2253 fn formViewActive(self: *This) bool { 2254 if (self.headerActive()) return false; 2255 return self.stackSelectedIndex() == form_view_index; 2256 } 2257 2258 // the selected thread's index, or null when a window-navigation row is 2259 // selected (a leading "previous" row shifts the thread rows down by one). 2260 fn selectedThreadIndex(self: *This, index: usize) ?usize { 2261 const lb = self.listBox(index); 2262 const cid = lb.getFocus().child_id orelse return null; 2263 const idx = lb.children.getIndex(cid) orelse return null; 2264 const win = self.window(index); 2265 const lead: usize = if (win.prev_id != null) 1 else 0; 2266 if (idx < lead or idx - lead >= win.items.len) return null; 2267 return idx - lead; 2268 } 2269 2270 pub fn build(self: *This, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2271 self.clearGrid(); 2272 2273 // each header tab maps 1:1 to a stack child by position 2274 if (self.header().getSelectedIndex()) |index| { 2275 const stack = self.viewStack(); 2276 stack.getFocus().child_id = stack.children.keys()[index]; 2277 } 2278 2279 if (self.selectedSplitIndex()) |i| { 2280 // swap the detail pane to the selected thread when it changes. 2281 if (self.selectedThreadIndex(i)) |selected| { 2282 const entry = self.window(i).items[selected]; 2283 const changed = if (self.detail(i).entry) |current| !std.mem.eql(u8, current.id, entry.id) else true; 2284 if (changed) { 2285 try self.detail(i).setEntry(allocator, entry); 2286 } 2287 } 2288 2289 // the selected list row shows a border (the focused TextBox 2290 // upgrades it to a double border itself); the rest stay borderless. 2291 const lb = self.listBox(i); 2292 for (lb.children.keys(), lb.children.values()) |id, *child| { 2293 switch (child.widget) { 2294 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 2295 else => {}, 2296 } 2297 } 2298 2299 // cap the list at list_max_width only while the detail pane fits 2300 // beside it. the box drops the detail when the width can't hold 2301 // both minimums, so when it's that narrow we lift the cap and let 2302 // the list fill the whole width. 2303 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 2304 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 2305 2306 // stretch the detail pane across the rest of the width so it fills 2307 // the area rather than shrinking to its content; its scroll fills 2308 // the pane. 2309 if (constraint.max_size.width) |w| { 2310 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 2311 } else { 2312 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 2313 } 2314 } 2315 2316 // refresh the form inputs' entries in the session's focus-id -> input 2317 // map with this frame's addresses, so the web/wasm form handling can 2318 // find them by focus id 2319 if (self.threadForm()) |form| { 2320 const failure = if (self.session.formFeedback(feedback_tag)) |saved| saved.failure else null; 2321 (try formField(form, "title")).options.label = if (failure == .required_title) " title (required) " else " title "; 2322 if (comptime supports_drafts) { 2323 if (formWidget(form, .patch_source)) |source| { 2324 source.field().options.label = if (failure == .invalid_source_branch) " source branch (not found) " else if (failure == .same_branch) " source branch (same as target) " else if (failure == .unrelated_branches) " source branch (unrelated) " else " source branch "; 2325 const submit = formWidget(form, .submit_button) orelse return error.MissingFormField; 2326 try submit.setLabel(allocator, switch (source.selected) { 2327 .fork => "submit draft", 2328 .branch => "submit patch", 2329 }); 2330 } 2331 (try formField(form, "target_branch")).options.label = 2332 if (failure == .invalid_target_branch) " target branch (invalid) " else if (self.data.view == .edit and failure == .same_branch) " target branch (same as source) " else if (self.data.view == .edit and failure == .unrelated_branches) " target branch (unrelated to source) " else if (self.data.view == .edit and failure == .invalid_source_branch) " target branch (source branch not found) " else " target branch "; 2333 } 2334 } 2335 if (self.formBox()) |form| { 2336 const inputs_arena = self.session.arena.allocator(); 2337 for (form.children.values()) |*child| switch (child.widget) { 2338 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 2339 else => {}, 2340 }; 2341 } 2342 2343 try self.box.build(allocator, constraint, root_focus); 2344 } 2345 2346 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 2347 // wheel input moves focus like the arrows 2348 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 2349 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 2350 else 2351 raw_key; 2352 2353 if (self.headerActive()) { 2354 // down from the sub-header re-enters the stack if the selected 2355 // view has something to focus; enter in the search box submits 2356 // the query; other keys move within the sub-header. 2357 if (inp.vertDirection(key) == .down) { 2358 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 2359 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 2360 // nothing focusable in the unauthorized view 2361 .unauthorized => false, 2362 else => true, 2363 } else false; 2364 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 2365 } else if (key == .enter) { 2366 if (try self.header().submittedText(allocator)) |text| { 2367 defer allocator.free(text); 2368 try self.submitSearch(text); 2369 } 2370 } else { 2371 try self.header().input(allocator, key, root_focus); 2372 } 2373 return; 2374 } 2375 if (self.tagsViewActive()) { 2376 self.tagsViewInput(key, root_focus); 2377 return; 2378 } 2379 if (self.formViewActive()) { 2380 if (confirmation(self.data.view) != null) { 2381 try self.confirmationFormInput(allocator, key, root_focus); 2382 } else if (supports_conflicts and self.data.view == .resolve) { 2383 try self.resolveInput(allocator, key, root_focus); 2384 } else { 2385 try self.formInput(allocator, key, root_focus); 2386 } 2387 return; 2388 } 2389 const i = self.selectedSplitIndex() orelse return; 2390 if (self.detailActive(i)) { 2391 switch (try self.detail(i).inputWithExit(allocator, key, root_focus)) { 2392 .none => {}, 2393 .list => self.focusList(i, root_focus), 2394 .header => self.focusHeader(root_focus), 2395 } 2396 } else { 2397 self.listInput(i, key, root_focus); 2398 } 2399 } 2400 2401 // navigate to the typed query's results, keeping the tag filter and the 2402 // status list the page shows. an empty box clears a filtered page and 2403 // does nothing on any other one. 2404 fn submitSearch(self: *This, text: []const u8) !void { 2405 if (text.len == 0 and self.data.search == null) return; 2406 const selected = self.header().getSelectedIndex() orelse 0; 2407 const status: Status = if (selected < status_count) splitStatus(selected) else @enumFromInt(0); 2408 const route = listRoute(self.data.identity, status, self.data.tag, "") orelse return; 2409 try self.session.navigate(route.withSearch(text) orelse return); 2410 } 2411 2412 // arrow keys move the tag selection; up from the top row crosses to the 2413 // header tabs. 2414 fn tagsViewInput(self: *This, key: Key, root_focus: *Focus) void { 2415 const tf = self.tagsView(); 2416 const cid = tf.focus.child_id orelse return; 2417 const cur = tf.indexOfFocusId(cid) orelse return; 2418 const count = tf.text_boxes.items.len; 2419 switch (key) { 2420 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 2421 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 2422 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 2423 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 2424 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 2425 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 2426 else => {}, 2427 } 2428 } 2429 2430 fn listInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 2431 // up/down (and the scroll wheel) move the selection a row; page up/down 2432 // jump a fixed amount. right/Enter cross into the detail pane. up from 2433 // the top row crosses into the header tabs. 2434 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 2435 const lb = self.listBox(index); 2436 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 2437 if (delta < 0 and at_top) return self.focusHeader(root_focus); 2438 moveRowFocus(lb, self.listScroll(index), root_focus, delta); 2439 return; 2440 } 2441 switch (key) { 2442 .enter, .arrow_right => _ = self.detail(index).focusFirst(root_focus), 2443 else => {}, 2444 } 2445 } 2446 2447 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2448 const form = self.formBox() orelse return; 2449 const cid = form.getFocus().child_id orelse return; 2450 const cur = form.children.getIndex(cid) orelse return; 2451 const child = &form.children.values()[cur]; 2452 2453 if (child.widget == .text_input) { 2454 const text_input = &child.widget.text_input; 2455 if (text_input.options.multiline and try multilineKeepsKey(text_input, allocator, key)) 2456 return child.widget.input(allocator, key, root_focus); 2457 } 2458 2459 const on_submit = child.widget == .submit_button; 2460 switch (key) { 2461 .arrow_up, .back_tab => return if (formStep(form, cur, false)) |i| 2462 root_focus.setFocus(form.children.keys()[i]) 2463 else 2464 self.focusHeader(root_focus), 2465 .arrow_down, .tab => return if (formStep(form, cur, true)) |i| root_focus.setFocus(form.children.keys()[i]), 2466 .enter => if (on_submit) return self.submitForm(allocator), 2467 .mouse => |mouse| if (on_submit) { 2468 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) return self.submitForm(allocator); 2469 }, 2470 else => {}, 2471 } 2472 try child.widget.input(allocator, key, root_focus); 2473 } 2474 2475 fn confirmationFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2476 const form = self.confirmationForm() orelse return; 2477 const child_id = form.getFocus().child_id orelse return; 2478 const index = form.children.getIndex(child_id) orelse return; 2479 switch (key) { 2480 .arrow_up, .back_tab => if (index > 1) 2481 root_focus.setFocus(form.children.keys()[index - 1]) 2482 else 2483 self.focusHeader(root_focus), 2484 .arrow_down, .tab => if (index + 1 < form.children.count()) root_focus.setFocus(form.children.keys()[index + 1]), 2485 .enter => try self.submitConfirmation(allocator, index), 2486 .mouse => |mouse| for (form.children.keys()[1..], 1..) |button_id, button_index| { 2487 if (inp.leftClickOn(root_focus, button_id, mouse)) { 2488 try self.submitConfirmation(allocator, button_index); 2489 break; 2490 } 2491 }, 2492 else => {}, 2493 } 2494 } 2495 2496 // the multiline hunk inputs keep enter and any up/down that has a row to 2497 // move to; enter on a "use this" link is a navigation the host follows 2498 // before this runs. 2499 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2500 const form = self.formBox() orelse return; 2501 const cid = form.getFocus().child_id orelse return; 2502 const cur = form.children.getIndex(cid) orelse return; 2503 const child = &form.children.values()[cur]; 2504 2505 if (child.widget == .text_input) { 2506 const ti = &child.widget.text_input; 2507 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2508 return child.widget.input(allocator, key, root_focus); 2509 } 2510 2511 const on_submit = child.widget == .submit_button; 2512 2513 switch (key) { 2514 .arrow_up, .back_tab => if (formStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2515 .arrow_down, .tab => if (formStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2516 .arrow_left, .arrow_right => switch (child.widget) { 2517 .box => |*row| _ = widget.moveInRow(row, root_focus, key == .arrow_right), 2518 else => try child.widget.input(allocator, key, root_focus), 2519 }, 2520 .enter => if (on_submit) try self.submitResolution(allocator), 2521 .mouse => |mouse| if (on_submit) { 2522 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2523 }, 2524 else => try child.widget.input(allocator, key, root_focus), 2525 } 2526 } 2527 2528 // a multiline input keeps enter and any up/down that has a row to move to 2529 fn multilineKeepsKey(text_input: *wgt.TextInput, allocator: std.mem.Allocator, key: Key) !bool { 2530 return switch (key) { 2531 .enter => true, 2532 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2533 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2534 else => false, 2535 }; 2536 } 2537 2538 // the neighboring form control, skipping spacers and blank rows 2539 fn formStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2540 var i = cur; 2541 while (true) { 2542 if (down) { 2543 i += 1; 2544 if (i >= form.children.count()) return null; 2545 } else { 2546 if (i == 0) return null; 2547 i -= 1; 2548 } 2549 const child = &form.children.values()[i]; 2550 if (child.hidden) continue; 2551 switch (child.widget) { 2552 .spacer, .text => continue, 2553 else => return i, 2554 } 2555 } 2556 } 2557 2558 // focus the resolve-form child at `i` (a version row focuses its selected 2559 // child) and keep it visible on the terminal 2560 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2561 const child = &form.children.values()[i]; 2562 switch (child.widget) { 2563 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2564 else => root_focus.setFocus(form.children.keys()[i]), 2565 } 2566 if (self.session.is_terminal) { 2567 if (self.resolveScroll()) |sc| { 2568 if (child.rect) |rect| sc.scrollToRect(rect); 2569 } 2570 } 2571 } 2572 2573 // re-emit the conflicted thread's event with the resolved content; any 2574 // event on the thread settles its conflict entry. this is the terminal 2575 // path; the web posts the form to the resolve route. 2576 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2577 if (comptime wasm) return; 2578 const io = self.session.io orelse return; 2579 const src = self.data.repo_source orelse return; 2580 const entry = self.data.selectedThread() orelse return; 2581 const author = ((try self.session.authorize(self.data.identity, .write)) orelse return).author; 2582 const form = self.formBox() orelse return; 2583 2584 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2585 var title: ?[]u8 = null; 2586 var tags: ?[]u8 = null; 2587 var hunks: std.ArrayList([]const u8) = .empty; 2588 defer { 2589 if (title) |t| allocator.free(t); 2590 if (tags) |t| allocator.free(t); 2591 for (hunks.items) |h| allocator.free(h); 2592 hunks.deinit(allocator); 2593 } 2594 for (form.children.values()) |*child| switch (child.widget) { 2595 .text_input => |*ti| { 2596 const text = try ti.text(allocator); 2597 errdefer allocator.free(text); 2598 if (std.mem.eql(u8, ti.options.name, "title")) { 2599 title = text; 2600 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2601 tags = text; 2602 } else { 2603 try hunks.append(allocator, text); 2604 } 2605 }, 2606 else => {}, 2607 }; 2608 2609 const id_bytes = try evt.parseEventId(entry.id); 2610 2611 switch (src.repo_kind) { 2612 inline else => |repo_kind| { 2613 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2614 defer any_repo.deinit(io, allocator); 2615 switch (any_repo) { 2616 inline else => |*repo| { 2617 var resolution: Event.Resolve = .{ 2618 .title = title, 2619 .tags = tags, 2620 .hunks = hunks.items, 2621 }; 2622 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2623 const change = Event.Update{ .resolve = resolution }; 2624 Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2625 // leave the form up for correction 2626 error.InvalidFields => return, 2627 else => |e| return e, 2628 }; 2629 }, 2630 } 2631 }, 2632 } 2633 2634 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2635 try self.session.navigate(route); 2636 } 2637 2638 // commit a new or edited comment and navigate to its permalink. this is the 2639 // terminal path; the web posts the form to its comment route. 2640 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2641 if (comptime wasm) return; 2642 const io = self.session.io orelse return; 2643 const src = self.data.repo_source orelse return; 2644 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2645 const author = actor.author; 2646 const form = self.formBox() orelse return; 2647 const body_input = try formField(form, "body"); 2648 const body = try body_input.text(allocator); 2649 defer allocator.free(body); 2650 if (!evt.Comment.fieldsValid(body)) return; 2651 2652 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2653 @memcpy(&thread_id, self.data.selected_id); 2654 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2655 var parent: [evt.event_id_size * 2]u8 = undefined; 2656 @memcpy(&parent, self.data.comment_id); 2657 break :blk parent; 2658 }; 2659 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2660 const editing = self.data.view == .edit_comment; 2661 if (editing) { 2662 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2663 if (!try src.canModify(io, allocator, actor, .comment, &comment_id)) return; 2664 } 2665 switch (src.repo_kind) { 2666 inline else => |repo_kind| { 2667 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2668 defer any_repo.deinit(io, allocator); 2669 switch (any_repo) { 2670 inline else => |*repo| if (editing) { 2671 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2672 try evt.Comment.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2673 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2674 } else { 2675 event_id_hex = try evt.Comment.create(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2676 }, 2677 } 2678 }, 2679 } 2680 2681 body_input.clear(allocator); 2682 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2683 try self.session.navigate(route); 2684 } 2685 2686 // terminal form submissions; the web posts to the forms' routes instead 2687 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2688 switch (self.data.view) { 2689 .new_comment, .edit_comment => try self.submitComment(allocator), 2690 .edit => try self.submitEditedThread(allocator), 2691 else => try self.submitNewThread(allocator), 2692 } 2693 } 2694 2695 // commit the new thread to the repo's events branch and navigate to it. 2696 // this is the terminal path; the web posts the form to the thread route, 2697 // so the wasm side never runs (or compiles) the repo access below. 2698 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2699 if (comptime wasm) return; 2700 const io = self.session.io orelse return; 2701 const src = self.data.repo_source orelse return; 2702 const author = ((try self.session.authorize(self.data.identity, .read)) orelse return).author; 2703 2704 const form = self.threadForm() orelse return; 2705 const title_input = try formField(form, "title"); 2706 const tags_input = try formField(form, "tags"); 2707 const description_input = try formField(form, "description"); 2708 2709 const title = try title_input.text(allocator); 2710 defer allocator.free(title); 2711 const tags = try tags_input.text(allocator); 2712 defer allocator.free(tags); 2713 const description = try description_input.text(allocator); 2714 defer allocator.free(description); 2715 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 2716 defer if (comptime supports_drafts) allocator.free(target_branch); 2717 2718 if (!Event.fieldsValid(title, tags)) { 2719 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, tags, description, target_branch); 2720 return; 2721 } 2722 2723 if (comptime supports_drafts) { 2724 const source = formWidget(form, .patch_source) orelse return error.MissingFormField; 2725 const source_branch = switch (source.selected) { 2726 .fork => null, 2727 .branch => try source.field().text(allocator), 2728 }; 2729 defer if (source_branch) |branch| allocator.free(branch); 2730 const event_id_hex = Data.create(self.data, self.session, allocator, author, title, tags, description, target_branch, source_branch) catch |err| { 2731 const failure = FeedbackFailure.fromError(err) orelse return err; 2732 try self.rememberThreadFeedback(failure, title, tags, description, target_branch); 2733 return; 2734 }; 2735 title_input.clear(allocator); 2736 tags_input.clear(allocator); 2737 description_input.clear(allocator); 2738 (try formField(form, "target_branch")).clear(allocator); 2739 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2740 try self.session.navigate(route); 2741 return; 2742 } 2743 2744 var id_bytes: [evt.event_id_size]u8 = undefined; 2745 io.random(&id_bytes); 2746 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2747 2748 const event = evt.EventWithId{ 2749 .id = event_id_hex, 2750 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2751 .author = author, 2752 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2753 .title = title, 2754 .description = description, 2755 .tags = tags, 2756 }), 2757 }; 2758 2759 switch (src.repo_kind) { 2760 inline else => |repo_kind| { 2761 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2762 defer any_repo.deinit(io, allocator); 2763 switch (any_repo) { 2764 inline else => |*repo| try evt.consume(self.session.data.host_kind, .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2765 } 2766 }, 2767 } 2768 2769 // wipe the form so a return visit starts fresh 2770 title_input.clear(allocator); 2771 tags_input.clear(allocator); 2772 description_input.clear(allocator); 2773 2774 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2775 try self.session.navigate(route); 2776 } 2777 2778 // re-emit the selected thread's event with the form's content (status 2779 // preserved), then reload the thread's page. this is the terminal path; 2780 // the web posts the form to the edit route. 2781 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2782 if (comptime wasm) return; 2783 const io = self.session.io orelse return; 2784 const entry = self.data.selectedThread() orelse return; 2785 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2786 const author = actor.author; 2787 2788 const form = self.threadForm() orelse return; 2789 const title_input = try formField(form, "title"); 2790 const tags_input = try formField(form, "tags"); 2791 const description_input = try formField(form, "description"); 2792 2793 const title = try title_input.text(allocator); 2794 defer allocator.free(title); 2795 const tags = try tags_input.text(allocator); 2796 defer allocator.free(tags); 2797 const description = try description_input.text(allocator); 2798 defer allocator.free(description); 2799 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 2800 defer if (comptime supports_drafts) allocator.free(target_branch); 2801 2802 if (!Event.fieldsValid(title, tags)) { 2803 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, tags, description, target_branch); 2804 return; 2805 } 2806 const src = self.data.repo_source orelse return; 2807 2808 if (comptime supports_drafts) { 2809 if (entryDraft(entry.*)) { 2810 self.data.editDraft(self.session, allocator, author, entry.id, title, tags, description, target_branch) catch |err| { 2811 const failure = FeedbackFailure.fromError(err) orelse return err; 2812 try self.rememberThreadFeedback(failure, title, tags, description, target_branch); 2813 return; 2814 }; 2815 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2816 try self.session.navigate(route); 2817 return; 2818 } 2819 } 2820 2821 const id_bytes = try evt.parseEventId(entry.id); 2822 if (!try src.canModify(io, allocator, actor, kind, &id_bytes)) return; 2823 2824 switch (src.repo_kind) { 2825 inline else => |repo_kind| { 2826 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2827 defer any_repo.deinit(io, allocator); 2828 switch (any_repo) { 2829 inline else => |*repo| if (supports_drafts) { 2830 Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2831 .title = title, 2832 .tags = tags, 2833 .description = description, 2834 .target_branch = target_branch, 2835 } }, author) catch |err| { 2836 const failure = FeedbackFailure.fromError(err) orelse return err; 2837 try self.rememberThreadFeedback(failure, title, tags, description, target_branch); 2838 return; 2839 }; 2840 } else if (has_status) 2841 try Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2842 .title = title, 2843 .tags = tags, 2844 .description = description, 2845 } }, author) 2846 else 2847 try Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 2848 } 2849 }, 2850 } 2851 2852 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2853 try self.session.navigate(route); 2854 } 2855 2856 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, tags: []const u8, description: []const u8, target_branch: []const u8) !void { 2857 const aa = self.session.arena.allocator(); 2858 const source = if (supports_drafts) blk: { 2859 const form = self.threadForm() orelse break :blk null; 2860 break :blk formWidget(form, .patch_source); 2861 } else null; 2862 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 2863 .failure = failure, 2864 .fields = if (comptime supports_drafts) .{ 2865 .title = try aa.dupe(u8, title), 2866 .tags = try aa.dupe(u8, tags), 2867 .description = try aa.dupe(u8, description), 2868 .target_branch = try aa.dupe(u8, target_branch), 2869 .source_branch = if (source) |value| try value.field().text(aa) else "", 2870 .source_kind = if (source) |value| value.selected else .fork, 2871 } else .{ 2872 .title = try aa.dupe(u8, title), 2873 .tags = try aa.dupe(u8, tags), 2874 .description = try aa.dupe(u8, description), 2875 }, 2876 }); 2877 } 2878 2879 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 2880 switch (confirmation(self.data.view) orelse return) { 2881 .publish => if (comptime supports_drafts) { 2882 if (comptime wasm) return; 2883 const entry = self.data.selectedThread() orelse return; 2884 if (!entryDraft(entry.*)) return; 2885 const author = ((try self.session.authorize(self.data.identity, .read)) orelse return).author; 2886 try self.data.publishDraft(self.session, allocator, author, entry.id); 2887 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 2888 try self.session.navigate(route); 2889 } else unreachable, 2890 .merge => if (comptime supports_merge) { 2891 if (comptime wasm) return; 2892 const entry = self.data.selectedThread() orelse return; 2893 const revision: evt.Patch.MergeRevision = if (button_index > 1) .squash else .source; 2894 if (!Data.canMerge(entry.*, self.session) or entry.mergeability.get(revision) != .clean) return; 2895 const author = ((try self.session.authorize(self.data.identity, .write)) orelse return).author; 2896 self.data.mergePatch(self.session, allocator, author, entry.id, revision) catch |err| switch (err) { 2897 error.MergeConflict, error.MergeCheckUnavailable, error.PatchOutOfDate, error.PatchDataUnavailable => { 2898 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2899 try self.session.navigate(route); 2900 return; 2901 }, 2902 else => return err, 2903 }; 2904 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 2905 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 2906 try self.session.navigate(route); 2907 } else unreachable, 2908 .remove => try self.submitRemove(allocator), 2909 } 2910 } 2911 2912 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2913 const comment = self.data.comment_id.len != 0; 2914 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2915 const id = evt.parseEventId(event_id) catch return; 2916 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2917 } 2918 2919 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2920 if (comptime wasm) return; 2921 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2922 const author = actor.author; 2923 2924 if (comptime supports_drafts) { 2925 const selected = self.data.selectedThread() orelse return; 2926 if (event_kind == kind and entryDraft(selected.*)) { 2927 try Data.removeDraft(self.session, allocator, author, &id); 2928 const route = draftsRoute(self.data.identity) orelse return; 2929 try self.session.navigate(route); 2930 return; 2931 } 2932 } 2933 2934 const io = self.session.io orelse return; 2935 const src = self.data.repo_source orelse return; 2936 if (!try src.canModify(io, allocator, actor, event_kind, &id)) return; 2937 2938 switch (src.repo_kind) { 2939 inline else => |repo_kind| { 2940 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2941 defer any_repo.deinit(io, allocator); 2942 switch (any_repo) { 2943 inline else => |*repo| try evt.remove(self.session.data.host_kind, .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2944 } 2945 }, 2946 } 2947 2948 const id_hex = std.fmt.bytesToHex(id, .lower); 2949 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex, null) orelse return; 2950 try self.session.navigate(route); 2951 } 2952 2953 // return to the list. 2954 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2955 root_focus.setFocus(self.listScroll(index).getFocus().id); 2956 } 2957 2958 // cross to the header tabs above the stack. 2959 fn focusHeader(self: *This, root_focus: *Focus) void { 2960 root_focus.setFocus(self.box.children.keys()[header_index]); 2961 } 2962 2963 pub fn clearGrid(self: *This) void { 2964 self.box.clearGrid(); 2965 } 2966 2967 pub fn getGrid(self: This) ?Grid { 2968 return self.box.getGrid(); 2969 } 2970 2971 pub fn getFocus(self: *This) *Focus { 2972 return self.box.getFocus(); 2973 } 2974 2975 // for the parent's "scroll up at the top jumps to the header" check: at the 2976 // top only while the header tabs hold focus, so up from the split 2977 // crosses into the tabs first. 2978 pub fn atTop(self: *This) bool { 2979 return self.headerActive(); 2980 } 2981 }; 2982 } 2983