╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2042 } else { 2043 var message = try ui.Unauthorized.View.init(allocator); 2044 errdefer message.deinit(allocator); 2045 try stack.children.put(allocator, message.getFocus().id, .{ .unauthorized = message }); 2046 } 2047 2048 if (supports_drafts) { 2049 var split = try initSplit(allocator, session, data, null, true); 2050 errdefer split.deinit(allocator); 2051 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 2052 } 2053 2054 // the conflicts split; its tab exists only when the repo has 2055 // conflicts, so skip the child too to keep the stack 1:1 with the 2056 // tabs. 2057 if (supports_conflicts and data.conflicts.count > 0) { 2058 var split = try initSplit(allocator, session, data, null, false); 2059 errdefer split.deinit(allocator); 2060 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 2061 } 2062 2063 // the stack starts on the page's view. 2064 stack.getFocus().child_id = stack.children.keys()[viewIndex(data.view)]; 2065 2066 // focus entering the view lands on the tabs first. 2067 outer.getFocus().child_id = outer.children.keys()[header_index]; 2068 2069 return .{ 2070 .box = outer, 2071 .data = data, 2072 .session = session, 2073 }; 2074 } 2075 2076 // the master-detail split showing `status`'s window, or the conflicts 2077 // window when null. 2078 fn initSplit(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, status_maybe: ?Status, drafts: bool) !wgt.Box(Widget) { 2079 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 2080 errdefer box.deinit(allocator); 2081 2082 const win = if (status_maybe) |status| data.window(status) else if (drafts and supports_drafts) &data.drafts else if (supports_conflicts) &data.conflicts else unreachable; 2083 2084 // the thread list (one focusable row per title), plus a "next" link that 2085 // reloads the page rooted at the following thread. 2086 { 2087 var list_scroll = blk: { 2088 var list_box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert, .stretch = true }); 2089 errdefer list_box.deinit(allocator); 2090 if (win.prev_id) |prev| 2091 try addRow(allocator, &list_box, "← previous", "", try windowLink(session.page_arena, data, status_maybe, drafts, prev)); 2092 for (win.items) |entry| { 2093 const label: []const u8 = if (entry.comments.count > 0) 2094 try session.page_arena.allocator().print(" {d} comments ", .{entry.comments.count}) 2095 else 2096 ""; 2097 try addRow(allocator, &list_box, entry.record.event.title, label, try rowLink(session.page_arena, data, entry.id)); 2098 } 2099 if (win.next_id) |next| 2100 try addRow(allocator, &list_box, "next →", "", try windowLink(session.page_arena, data, status_maybe, drafts, next)); 2101 // select the window's first thread (past a leading "previous" 2102 // row) so its description shows on load. 2103 if (win.items.len > 0) 2104 list_box.getFocus().child_id = list_box.children.keys()[if (win.prev_id != null) 1 else 0] 2105 else if (list_box.children.count() > 0) 2106 list_box.getFocus().child_id = list_box.children.keys()[0]; 2107 break :blk try wgt.Scroll(Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal }); 2108 }; 2109 errdefer list_scroll.deinit(allocator); 2110 try box.children.put(allocator, list_scroll.getFocus().id, .{ .widget = .{ .scroll = list_scroll }, .rect = null, .min_size = .{ .width = list_max_width, .height = null }, .max_size = .{ .width = list_max_width, .height = null } }); 2111 } 2112 2113 // frame the detail pane inside the master-detail split, below the 2114 // way back to the list, shown only while the list is hidden 2115 { 2116 var frame = try wgt.Box(Widget).init(allocator, .{ .border = .hidden, .direction = .vert }); 2117 errdefer frame.deinit(allocator); 2118 frame.getFocus().mode = .mouse; 2119 try widget.addListBack(allocator, &frame, "← back to " ++ thread_name ++ " list"); 2120 var detail_view = try DetailType.init(allocator, data, session, null, .{}); 2121 errdefer detail_view.deinit(allocator); 2122 frame.getFocus().child_id = detail_view.getFocus().id; 2123 try frame.children.put(allocator, detail_view.getFocus().id, .{ .widget = @unionInit(Widget, Data.detail_widget_name, detail_view), .rect = null, .min_size = null }); 2124 try box.children.put(allocator, frame.getFocus().id, .{ .widget = .{ .box = frame }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 2125 } 2126 2127 // the page opens on the list, unless the url names a thread 2128 box.getFocus().child_id = box.children.keys()[if (data.selected_id.len > 0) detail_index else list_index]; 2129 return box; 2130 } 2131 2132 // a thread form, plus the target branch for patches, prefilled from 2133 // `record` when given. its form: subtree makes the web renderer POST 2134 // the inputs to `action`'s route. 2135 fn initThreadForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, action: []const u8, record: ?*const Event.Record) !wgt.Box(Widget) { 2136 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 2137 errdefer box.deinit(allocator); 2138 box.getFocus().kind = .{ .custom = action }; 2139 2140 const saved_form = session.formFeedback(feedback_tag); 2141 const saved_fields = if (saved_form) |saved| saved.fields else null; 2142 2143 { 2144 var title = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " title " }, .name = "title", .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 2145 errdefer title.deinit(allocator); 2146 title.getFocus().mode = .all; 2147 if (saved_fields) |saved| 2148 try title.setContent(allocator, saved.title) 2149 else if (record) |r| 2150 try title.setContent(allocator, r.event.title); 2151 try box.children.put(allocator, title.getFocus().id, .{ .widget = .{ .text_input = title }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2152 box.getFocus().child_id = title.getFocus().id; 2153 } 2154 2155 { 2156 var labels = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " labels (separate with spaces) " }, .name = "labels", .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 2157 errdefer labels.deinit(allocator); 2158 labels.getFocus().mode = .all; 2159 if (saved_fields) |saved| 2160 try labels.setContent(allocator, saved.labels) 2161 else if (record) |r| 2162 try labels.setContent(allocator, r.event.labels); 2163 try box.children.put(allocator, labels.getFocus().id, .{ .widget = .{ .text_input = labels }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2164 } 2165 2166 { 2167 var description = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " description (markdown supported) " }, .name = "description", .visible_width = null, .round_corners = true, .render_content = session.is_terminal, .multiline = true, .scroll = .{ .fill = true } }); 2168 errdefer description.deinit(allocator); 2169 description.getFocus().mode = .all; 2170 if (saved_fields) |saved| 2171 try description.setContent(allocator, saved.description) 2172 else if (record) |r| 2173 try description.setContent(allocator, r.event.description); 2174 try box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .text_input = description }, .rect = null, .min_size = null }); 2175 } 2176 2177 if (supports_drafts and record == null) { 2178 var source = try Data.Source.init(allocator, session, if (saved_fields) |saved| saved.source_branch else "", if (saved_fields) |saved| saved.source_kind else .fork); 2179 errdefer source.deinit(allocator); 2180 try box.children.put(allocator, source.getFocus().id, .{ .widget = .{ .patch_source = source }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2181 } 2182 2183 if (supports_drafts) { 2184 var target = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " target branch " }, .name = "target_branch", .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 2185 errdefer target.deinit(allocator); 2186 target.getFocus().mode = .all; 2187 const content = if (saved_fields) |saved| saved.target_branch else if (record) |r| r.event.target_branch else data.default_target_branch; 2188 try target.setContent(allocator, content); 2189 try box.children.put(allocator, target.getFocus().id, .{ .widget = .{ .text_input = target }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2190 } 2191 2192 try addSubmitButtonLabeled(allocator, &box, if (supports_drafts and record == null) "submit draft" else "submit"); 2193 2194 return box; 2195 } 2196 2197 fn initCommentForm(allocator: std.mem.Allocator, session: *ui.Session, action: []const u8, author: ui.Author, parent_route: ui.RoutablePage, initial_body: ?[]const u8) !wgt.Box(Widget) { 2198 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 2199 errdefer box.deinit(allocator); 2200 box.getFocus().kind = .{ .custom = action }; 2201 2202 { 2203 var parent = try ui.authorBox(allocator, session.page_arena, author); 2204 errdefer parent.deinit(allocator); 2205 parent.options.top_label.text = " replying to "; 2206 parent.getFocus().kind = .{ .custom = try session.page_arena.allocator().print("a:{s}", .{try parent_route.toUrl(session.page_arena)}) }; 2207 try box.children.put(allocator, parent.getFocus().id, .{ .widget = .{ .text_box = parent }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2208 box.getFocus().child_id = parent.getFocus().id; 2209 } 2210 2211 { 2212 var body = try wgt.TextInput.init(allocator, .{ 2213 .top_label = .{ .text = " comment (markdown supported) " }, 2214 .name = "body", 2215 .visible_width = null, 2216 .round_corners = true, 2217 .render_content = session.is_terminal, 2218 .multiline = true, 2219 .visible_height = 5, 2220 .scroll = .{ .fill = true }, 2221 }); 2222 errdefer body.deinit(allocator); 2223 body.getFocus().mode = .all; 2224 if (initial_body) |text| try body.setContent(allocator, text); 2225 try box.children.put(allocator, body.getFocus().id, .{ .widget = .{ .text_input = body }, .rect = null, .min_size = null }); 2226 } 2227 2228 try addSubmitButtonLabeled(allocator, &box, "submit"); 2229 return box; 2230 } 2231 2232 const ConfirmationButton = struct { 2233 label: []const u8, 2234 bottom_label: []const u8 = "", 2235 action: []const u8 = "submit", 2236 }; 2237 2238 fn initConfirmationForm(allocator: std.mem.Allocator, action: []const u8, buttons: []const ConfirmationButton) !Center { 2239 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .round_corners = true, .direction = .vert }); 2240 errdefer box.deinit(allocator); 2241 box.getFocus().kind = .{ .custom = action }; 2242 2243 var prompt = try wgt.Text.init(allocator, "are you sure?"); 2244 errdefer prompt.deinit(allocator); 2245 try box.children.put(allocator, prompt.getFocus().id, .{ .widget = .{ .text = prompt }, .rect = null, .min_size = null }); 2246 2247 for (buttons, 0..) |options, index| { 2248 var button = try wgt.TextBox.init(allocator, options.label, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = .{ .text = options.bottom_label } }); 2249 errdefer button.deinit(allocator); 2250 button.getFocus().mode = .all; 2251 button.getFocus().kind = .{ .custom = options.action }; 2252 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 2253 if (index == 0) box.getFocus().child_id = button.getFocus().id; 2254 } 2255 2256 return Center.init(allocator, .{ .box = box }); 2257 } 2258 2259 // a form's submit button, then a spacer absorbing the leftover 2260 // min-height the box hands its last child, so the button keeps its 2261 // natural height 2262 fn addSubmitButtonLabeled(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8) !void { 2263 { 2264 var submit = try SubmitButton.initLabeled(allocator, label); 2265 errdefer submit.deinit(allocator); 2266 try box.children.put(allocator, submit.getFocus().id, .{ .widget = .{ .submit_button = submit }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2267 } 2268 { 2269 var spacer = try Spacer.init(allocator); 2270 errdefer spacer.deinit(allocator); 2271 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 2272 } 2273 } 2274 2275 // the conflict resolve form. the "use this" links are navigations that 2276 // flip the url's theirs: pick, reloading the prefills; one submit 2277 // settles every conflict. the form: subtree makes the web renderer POST 2278 // to the resolve route. 2279 fn initResolveForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !wgt.Box(Widget) { 2280 const aa = session.page_arena.allocator(); 2281 // init only picks the resolve view once it has read the conflict 2282 const conflict = if (data.conflict) |*c| c else unreachable; 2283 2284 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 2285 errdefer box.deinit(allocator); 2286 const route = resolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong; 2287 box.getFocus().kind = .{ .custom = try aa.print("form:{s}", .{try route.toUrl(session.page_arena)}) }; 2288 2289 if (conflict.title) |*fc| { 2290 try addLabel(allocator, &box, "title conflict:"); 2291 try addFieldConflict(allocator, &box, session, data, "title", fc); 2292 } 2293 if (conflict.labels) |*fc| { 2294 try addLabel(allocator, &box, "labels conflict:"); 2295 try addFieldConflict(allocator, &box, session, data, "labels", fc); 2296 } 2297 if (comptime @hasField(Data.Conflict, "status")) { 2298 if (conflict.status) |*fc| { 2299 try addLabel(allocator, &box, "status conflict:"); 2300 try addAtomicConflict(allocator, &box, session, data, "status", fc); 2301 } 2302 } 2303 if (comptime @hasField(Data.Conflict, "revision")) { 2304 if (conflict.revision) |*fc| { 2305 try addLabel(allocator, &box, "revision conflict:"); 2306 try addAtomicConflict(allocator, &box, session, data, "revision", fc); 2307 } 2308 if (conflict.target_branch) |*fc| { 2309 try addLabel(allocator, &box, "target branch conflict:"); 2310 try addAtomicConflict(allocator, &box, session, data, "target_branch", fc); 2311 } 2312 } 2313 2314 if (conflict.description) |*desc| { 2315 try addLabel(allocator, &box, "description conflict:"); 2316 var hunk_index: usize = 0; 2317 for (desc.chunks, 0..) |*chunk, chunk_index| { 2318 switch (chunk.*) { 2319 .same => |text| { 2320 var tb = try wgt.TextBox.init(allocator, text, .{ .border = .single, .round_corners = true, .wrap_kind = .word }); 2321 errdefer tb.deinit(allocator); 2322 tb.getFocus().mode = .all; 2323 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2324 }, 2325 // an auto-resolved chunk: only one side changed it, so its 2326 // version stands, shown distinctly with its author 2327 .auto => |auto| { 2328 const author = if (auto.theirs) desc.theirs_author else desc.ours_author; 2329 const verb: []const u8 = if (auto.text == null) "removed" else "edited"; 2330 const label = switch (author) { 2331 .user_name, .email => |name| try aa.print(" {s} by {s} ", .{ verb, name }), 2332 .unknown => try aa.print(" {s} by {s} ", .{ verb, if (auto.theirs) @as([]const u8, "them") else "us" }), 2333 }; 2334 var tb = try wgt.TextBox.init(allocator, auto.text orelse "(removed)", .{ .border = .single, .round_corners = true, .wrap_kind = .word, .top_label = .{ .text = label } }); 2335 errdefer tb.deinit(allocator); 2336 tb.getFocus().mode = .all; 2337 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2338 }, 2339 .conflict => |hunk| { 2340 // a blank row on each side sets the group apart from 2341 // the flowing shared and auto-resolved chunks 2342 if (chunk_index > 0) try addGap(allocator, &box); 2343 const name = try aa.print("d{d}", .{hunk_index}); 2344 hunk_index += 1; 2345 const picked_theirs = data.theirsPicked(name); 2346 try addVersionRow(allocator, &box, try sideLabel(aa, desc.ours_author, true), hunk.ours orelse "", try useThisLink(session, data, name, false)); 2347 try addVersionRow(allocator, &box, try sideLabel(aa, desc.theirs_author, false), hunk.theirs orelse "", try useThisLink(session, data, name, true)); 2348 var resolution_input = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " resolution " }, .name = name, .visible_width = null, .round_corners = true, .render_content = session.is_terminal, .multiline = true }); 2349 errdefer resolution_input.deinit(allocator); 2350 resolution_input.getFocus().mode = .all; 2351 try resolution_input.setContent(allocator, (if (picked_theirs) hunk.theirs else hunk.ours) orelse ""); 2352 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 2353 if (chunk_index + 1 < desc.chunks.len) try addGap(allocator, &box); 2354 }, 2355 } 2356 } 2357 } 2358 2359 try addSubmitButtonLabeled(allocator, &box, "submit"); 2360 2361 box.getFocus().child_id = box.children.keys()[0]; 2362 return box; 2363 } 2364 2365 // one conflicted scalar field: both sides' version rows, then the 2366 // resolution input prefilled from the picked side. 2367 fn addFieldConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 2368 const aa = session.page_arena.allocator(); 2369 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 2370 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 2371 2372 var resolution_input = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " resolution " }, .name = name, .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 2373 errdefer resolution_input.deinit(allocator); 2374 resolution_input.getFocus().mode = .all; 2375 try resolution_input.setContent(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text); 2376 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 2377 } 2378 2379 // atomic fields can only pick one complete side 2380 fn addAtomicConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 2381 const aa = session.page_arena.allocator(); 2382 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 2383 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 2384 var selected = try wgt.TextBox.init(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text, .{ 2385 .border = .single, 2386 .round_corners = true, 2387 .wrap_kind = .word, 2388 .top_label = .{ .text = " resolution " }, 2389 }); 2390 errdefer selected.deinit(allocator); 2391 selected.getFocus().mode = .all; 2392 try box.children.put(allocator, selected.getFocus().id, .{ .widget = .{ .text_box = selected }, .rect = null, .min_size = null }); 2393 } 2394 2395 // a blank row setting a conflict group apart from its neighbors 2396 fn addGap(allocator: std.mem.Allocator, box: *wgt.Box(Widget)) !void { 2397 var text = try wgt.Text.init(allocator, " "); 2398 errdefer text.deinit(allocator); 2399 try box.children.put(allocator, text.getFocus().id, .{ .widget = .{ .text = text }, .rect = null, .min_size = null }); 2400 } 2401 2402 fn addLabel(allocator: std.mem.Allocator, box: *wgt.Box(Widget), content: []const u8) !void { 2403 var label = try SectionLabel.init(allocator, content); 2404 errdefer label.deinit(allocator); 2405 try box.children.put(allocator, label.getFocus().id, .{ .widget = .{ .section_label = label }, .rect = null, .min_size = null }); 2406 } 2407 2408 // one side of a conflict: the "use this" link on the left, the version 2409 // itself as a labeled read-only box. 2410 fn addVersionRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8, text: []const u8, link: []const u8) !void { 2411 var row = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 2412 errdefer row.deinit(allocator); 2413 2414 { 2415 const use_label = "use this"; 2416 var use = try wgt.TextBox.init(allocator, use_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 2417 errdefer use.deinit(allocator); 2418 use.getFocus().mode = .all; 2419 use.getFocus().kind = .{ .custom = link }; 2420 try row.children.put(allocator, use.getFocus().id, .{ 2421 .widget = .{ .text_box = use }, 2422 .rect = null, 2423 .min_size = .{ .width = use_label.len + 2, .height = null }, 2424 .max_size = .{ .width = use_label.len + 2, .height = null }, 2425 }); 2426 } 2427 2428 { 2429 var tb = try wgt.TextBox.init(allocator, if (text.len > 0) text else "(removed)", .{ .border = .single, .round_corners = true, .wrap_kind = .word, .top_label = .{ .text = label } }); 2430 errdefer tb.deinit(allocator); 2431 tb.getFocus().mode = .all; 2432 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2433 } 2434 2435 row.getFocus().child_id = row.children.keys()[0]; 2436 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 2437 } 2438 2439 // the label of one side's version box, naming its author 2440 fn sideLabel(aa: std.mem.Allocator, author: ui.Author, ours: bool) ![]const u8 { 2441 return switch (author) { 2442 .user_name, .email => |name| try aa.print(" edited by {s} ", .{name}), 2443 .unknown => if (ours) " current version " else " their version ", 2444 }; 2445 } 2446 2447 // the a: link that reloads the resolve page with `name` picked from ours 2448 // or theirs, reprefilling its resolution input 2449 fn useThisLink(session: *ui.Session, data: *const Self, name: []const u8, theirs: bool) ![]const u8 { 2450 const aa = session.page_arena.allocator(); 2451 var picks: std.ArrayList(u8) = .empty; 2452 var it = std.mem.splitScalar(u8, data.theirs_picks, ','); 2453 while (it.next()) |pick| { 2454 if (pick.len == 0 or std.mem.eql(u8, pick, name)) continue; 2455 if (picks.items.len > 0) try picks.append(aa, ','); 2456 try picks.appendSlice(aa, pick); 2457 } 2458 if (theirs) { 2459 if (picks.items.len > 0) try picks.append(aa, ','); 2460 try picks.appendSlice(aa, name); 2461 } 2462 const route = resolveRoute(data.identity, data.selected_id, picks.items) orelse return error.RouteTooLong; 2463 return aa.print("a:{s}", .{try route.toUrl(session.page_arena)}); 2464 } 2465 2466 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), text: []const u8, bottom_label: []const u8, link: []const u8) !void { 2467 var row = try wgt.TextBox.init(allocator, text, .{ .border = .hidden, .round_corners = true, .wrap_kind = .word, .bottom_label = .{ .text = bottom_label } }); 2468 errdefer row.deinit(allocator); 2469 row.getFocus().mode = .all; 2470 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 2471 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 2472 } 2473 2474 pub fn deinit(self: *This, allocator: std.mem.Allocator) void { 2475 self.box.deinit(allocator); 2476 } 2477 2478 fn header(self: *This) *HeaderType { 2479 return &@field(self.box.children.values()[header_index].widget, Data.header_widget_name); 2480 } 2481 2482 fn viewStack(self: *This) *wgt.Stack(Widget) { 2483 return &self.box.children.values()[stack_index].widget.stack; 2484 } 2485 2486 // `index`'s master-detail split inside the stack. 2487 fn resultsBox(self: *This, index: usize) *wgt.Box(Widget) { 2488 return &self.viewStack().children.values()[index].box; 2489 } 2490 2491 // the labels view's flow inside the stack. 2492 fn labelsView(self: *This) *WordFlow { 2493 return &self.viewStack().children.values()[labels_view_index].word_flow; 2494 } 2495 2496 // the non-confirmation form inside the stack 2497 fn formBox(self: *This) ?*wgt.Box(Widget) { 2498 return switch (self.viewStack().children.values()[form_view_index]) { 2499 .box => |*box| box, 2500 // the resolve form sits inside a scroll on the terminal 2501 .scroll => |*scroll| &scroll.child.box, 2502 else => null, 2503 }; 2504 } 2505 2506 fn threadForm(self: *This) ?*wgt.Box(Widget) { 2507 if (supports_conflicts and self.data.view == .resolve) return null; 2508 return switch (self.data.view) { 2509 .new_comment, .edit_comment => null, 2510 else => self.formBox(), 2511 }; 2512 } 2513 2514 fn formField(form: *wgt.Box(Widget), name: []const u8) !*wgt.TextInput { 2515 for (form.children.values()) |*child| switch (child.widget) { 2516 .text_input => |*text_input| if (std.mem.eql(u8, text_input.options.name, name)) return text_input, 2517 else => {}, 2518 }; 2519 return error.MissingFormField; 2520 } 2521 2522 fn formWidget(form: *wgt.Box(Widget), comptime widget_kind: std.meta.Tag(Widget)) ?*@FieldType(Widget, @tagName(widget_kind)) { 2523 for (form.children.values()) |*child| { 2524 if (child.widget == widget_kind) return &@field(child.widget, @tagName(widget_kind)); 2525 } 2526 return null; 2527 } 2528 2529 fn confirmationForm(self: *This) ?*wgt.Box(Widget) { 2530 return switch (self.viewStack().children.values()[form_view_index]) { 2531 .center => |*center| switch (center.child.*) { 2532 .box => |*box| box, 2533 else => null, 2534 }, 2535 else => null, 2536 }; 2537 } 2538 2539 // the resolve form's scroll on the terminal (the web page scrolls itself) 2540 fn resolveScroll(self: *This) ?*wgt.Scroll(Widget) { 2541 return switch (self.viewStack().children.values()[form_view_index]) { 2542 .scroll => |*scroll| scroll, 2543 else => null, 2544 }; 2545 } 2546 2547 fn listScroll(self: *This, index: usize) *wgt.Scroll(Widget) { 2548 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 2549 } 2550 2551 fn listBox(self: *This, index: usize) *wgt.Box(Widget) { 2552 return &self.listScroll(index).child.box; 2553 } 2554 2555 fn detailFrame(self: *This, index: usize) *wgt.Box(Widget) { 2556 return &self.resultsBox(index).children.values()[detail_index].widget.box; 2557 } 2558 2559 fn detail(self: *This, index: usize) *DetailType { 2560 return &@field(self.detailFrame(index).children.values()[widget.list_pane_index].widget, Data.detail_widget_name); 2561 } 2562 2563 fn window(self: *This, index: usize) *const Window { 2564 if (supports_conflicts and index == conflict_view_index) return &self.data.conflicts; 2565 if (supports_drafts and index == drafts_view_index) return &self.data.drafts; 2566 return self.data.window(splitStatus(index)); 2567 } 2568 2569 fn stackSelectedIndex(self: *This) ?usize { 2570 const stack = self.viewStack(); 2571 const cid = stack.getFocus().child_id orelse return null; 2572 return stack.children.getIndex(cid); 2573 } 2574 2575 // the stack's selected master-detail split (null when the labels view or the 2576 // new-thread form shows). 2577 fn selectedSplitIndex(self: *This) ?usize { 2578 const idx = self.stackSelectedIndex() orelse return null; 2579 return if (idx < status_count or (supports_drafts and idx == drafts_view_index) or idx == conflict_view_index) idx else null; 2580 } 2581 2582 fn detailActive(self: *This, index: usize) bool { 2583 const rb = self.resultsBox(index); 2584 const cid = rb.getFocus().child_id orelse return false; 2585 return rb.children.getIndex(cid) == detail_index; 2586 } 2587 2588 fn headerActive(self: *This) bool { 2589 const cid = self.box.getFocus().child_id orelse return false; 2590 return self.box.children.getIndex(cid) == header_index; 2591 } 2592 2593 fn labelsViewActive(self: *This) bool { 2594 if (self.headerActive()) return false; 2595 return self.stackSelectedIndex() == labels_view_index; 2596 } 2597 2598 fn formViewActive(self: *This) bool { 2599 if (self.headerActive()) return false; 2600 return self.stackSelectedIndex() == form_view_index; 2601 } 2602 2603 // the selected thread's index, or null when a window-navigation row is 2604 // selected (a leading "previous" row shifts the thread rows down by one). 2605 fn selectedThreadIndex(self: *This, index: usize) ?usize { 2606 const lb = self.listBox(index); 2607 const cid = lb.getFocus().child_id orelse return null; 2608 const idx = lb.children.getIndex(cid) orelse return null; 2609 const win = self.window(index); 2610 const lead: usize = if (win.prev_id != null) 1 else 0; 2611 if (idx < lead or idx - lead >= win.items.len) return null; 2612 return idx - lead; 2613 } 2614 2615 // swap the detail pane to the selected thread when it changes 2616 fn refreshDetail(self: *This, allocator: std.mem.Allocator, index: usize) !void { 2617 const selected = self.selectedThreadIndex(index) orelse return; 2618 const entry = self.window(index).items[selected]; 2619 const changed = if (self.detail(index).entry) |current| !std.mem.eql(u8, current.id, entry.id) else true; 2620 if (!changed) return; 2621 try self.detail(index).setEntry(allocator, entry); 2622 widget.setListBackLink(self.detailFrame(index), try rowLink(self.session.page_arena, self.data, entry.id)); 2623 } 2624 2625 pub fn build(self: *This, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2626 self.clearGrid(); 2627 2628 // each header tab maps 1:1 to a stack child by position 2629 if (self.header().getSelectedIndex()) |index| { 2630 const stack = self.viewStack(); 2631 stack.getFocus().child_id = stack.children.keys()[index]; 2632 } 2633 2634 if (self.selectedSplitIndex()) |i| { 2635 // whether the detail pane fits beside the list, which decides 2636 // the list cap below and whether the detail carries a way back 2637 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 2638 try self.refreshDetail(allocator, i); 2639 widget.showListBack(self.detailFrame(i), !both_panes_fit); 2640 2641 // the selected list row shows a border (the focused TextBox 2642 // upgrades it to a double border itself); the rest stay borderless. 2643 const lb = self.listBox(i); 2644 for (lb.children.keys(), lb.children.values()) |id, *child| { 2645 switch (child.widget) { 2646 .text_box => |*tb| widget.markSelected(tb, lb.getFocus().child_id == id), 2647 else => {}, 2648 } 2649 } 2650 2651 // cap the list at list_max_width only while the detail pane fits 2652 // beside it. the box drops the detail when the width can't hold 2653 // both minimums, so when it's that narrow we lift the cap and let 2654 // the list fill the whole width. 2655 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 2656 2657 // stretch the detail pane across the rest of the width so it fills 2658 // the area rather than shrinking to its content; its scroll fills 2659 // the pane. 2660 if (constraint.max_size.width) |w| { 2661 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 2662 } else { 2663 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 2664 } 2665 } 2666 2667 // refresh the form inputs' entries in the session's focus-id -> input 2668 // map with this frame's addresses, so the web/wasm form handling can 2669 // find them by focus id 2670 if (self.threadForm()) |form| { 2671 const failure = if (self.session.formFeedback(feedback_tag)) |saved| saved.failure else null; 2672 (try formField(form, "title")).options.top_label.text = if (failure == .required_title) " title (required) " else " title "; 2673 if (comptime supports_drafts) { 2674 if (formWidget(form, .patch_source)) |source| { 2675 source.field().options.top_label.text = 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 "; 2676 const submit = formWidget(form, .submit_button) orelse return error.MissingFormField; 2677 try submit.setLabel(allocator, switch (source.selectedKind()) { 2678 .fork => "submit draft", 2679 .branch => "submit patch", 2680 }); 2681 } 2682 (try formField(form, "target_branch")).options.top_label.text = 2683 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 "; 2684 } 2685 } 2686 if (self.formBox()) |form| { 2687 const inputs_arena = self.session.arena.allocator(); 2688 for (form.children.values()) |*child| switch (child.widget) { 2689 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 2690 else => {}, 2691 }; 2692 } 2693 2694 try self.box.build(allocator, constraint, root_focus); 2695 } 2696 2697 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 2698 // wheel input moves focus like the arrows 2699 const key: Key = switch (raw_key) { 2700 .scroll_up => .arrow_up, 2701 .scroll_down => .arrow_down, 2702 else => raw_key, 2703 }; 2704 2705 if (self.headerActive()) { 2706 // down from the sub-header re-enters the stack if the selected 2707 // view has something to focus; enter in the search box submits 2708 // the query; other keys move within the sub-header. 2709 if (inp.vertDirection(key) == .down) { 2710 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 2711 .word_flow => |*tf| tf.text_boxes.items.len > 0, 2712 // nothing focusable in the unauthorized view 2713 .unauthorized => false, 2714 else => true, 2715 } else false; 2716 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 2717 } else if (key == .enter) { 2718 if (try self.header().submittedText(allocator)) |submission| { 2719 defer allocator.free(submission.text); 2720 switch (submission.kind) { 2721 .search => try self.submitSearch(submission.text), 2722 .assignee => try self.submitAssignee(submission.text), 2723 } 2724 } 2725 } else { 2726 try self.header().input(allocator, key, root_focus); 2727 } 2728 return; 2729 } 2730 if (self.labelsViewActive()) { 2731 self.labelsViewInput(key, root_focus); 2732 return; 2733 } 2734 if (self.formViewActive()) { 2735 if (confirmation(self.data.view) != null) { 2736 try self.confirmationFormInput(allocator, key, root_focus); 2737 } else if (supports_conflicts and self.data.view == .resolve) { 2738 try self.resolveInput(allocator, key, root_focus); 2739 } else { 2740 try self.formInput(allocator, key, root_focus); 2741 } 2742 return; 2743 } 2744 const i = self.selectedSplitIndex() orelse return; 2745 if (self.detailActive(i)) { 2746 // the way back to the list, by enter or a click 2747 if (widget.listBackId(self.detailFrame(i))) |id| { 2748 if (inp.activated(root_focus, id, key)) return self.focusList(i, root_focus); 2749 if (root_focus.grandchild_id == id) return switch (key) { 2750 .arrow_left => self.focusList(i, root_focus), 2751 .arrow_up => self.focusHeader(root_focus), 2752 .arrow_down => _ = self.detail(i).focusFirst(root_focus), 2753 else => {}, 2754 }; 2755 } 2756 switch (try self.detail(i).inputWithExit(allocator, key, root_focus)) { 2757 .none => {}, 2758 .list => self.focusList(i, root_focus), 2759 .header => if (widget.listBackId(self.detailFrame(i))) |id| root_focus.setFocus(id) else self.focusHeader(root_focus), 2760 } 2761 } else { 2762 try self.listInput(allocator, i, key, root_focus); 2763 } 2764 } 2765 2766 // navigate to the typed query's results, keeping the label filter and the 2767 // status list the page shows. an empty box clears a filtered page and 2768 // does nothing on any other one. 2769 fn submitSearch(self: *This, text: []const u8) !void { 2770 if (text.len == 0 and self.data.search == null) return; 2771 const selected = self.header().getSelectedIndex() orelse 0; 2772 const status: Status = if (selected < status_count) splitStatus(selected) else @fromBackingInt(@intCast(0)); 2773 var route = listRoute(self.data.identity, status, self.data.label, "") orelse return; 2774 const assignee = assigneeFilter(self.data); 2775 if (assignee.len != 0) route = route.withEncodedAssignee(assignee) orelse return; 2776 try self.session.navigate(route.withSearch(text) orelse return); 2777 } 2778 2779 // navigate to the issues assigned to the typed name or email, keeping 2780 // the label, the search and the status list. an empty box clears the 2781 // filter and does nothing on an unfiltered page. 2782 fn submitAssignee(self: *This, typed: []const u8) !void { 2783 const text = std.mem.trim(u8, typed, " \t"); 2784 if (text.len == 0 and assigneeFilter(self.data).len == 0) return; 2785 const selected = self.header().getSelectedIndex() orelse 0; 2786 const status: Status = if (selected < status_count) splitStatus(selected) else @fromBackingInt(@intCast(0)); 2787 var route = listRoute(self.data.identity, status, self.data.label, "") orelse return; 2788 if (self.data.search) |search| route = route.withSearch(search) orelse return; 2789 try self.session.navigate(route.withEncodedAssignee(try ui.urlEncodeRef(self.session.page_arena.allocator(), text)) orelse return); 2790 } 2791 2792 // arrow keys move the label selection; up from the top row crosses to the 2793 // header tabs. 2794 fn labelsViewInput(self: *This, key: Key, root_focus: *Focus) void { 2795 const tf = self.labelsView(); 2796 const cid = tf.focus.child_id orelse return; 2797 const cur = tf.indexOfFocusId(cid) orelse return; 2798 const count = tf.text_boxes.items.len; 2799 switch (key) { 2800 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 2801 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 2802 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 2803 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 2804 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 2805 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 2806 else => {}, 2807 } 2808 } 2809 2810 fn listInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 2811 // up/down (and the scroll wheel) move the selection a row; page up/down 2812 // jump a fixed amount. right/Enter cross into the detail pane. up from 2813 // the top row crosses into the header tabs. 2814 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 2815 const lb = self.listBox(index); 2816 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 2817 if (delta < 0 and at_top) return self.focusHeader(root_focus); 2818 moveRowFocus(lb, self.listScroll(index), root_focus, delta); 2819 return; 2820 } 2821 switch (key) { 2822 .enter, .arrow_right => self.focusDetail(index, root_focus), 2823 // when the window is too narrow to lay out the detail pane beside 2824 // the list, a click on a row opens it like enter. the row was just 2825 // selected, so the detail is swapped to it ahead of the build. 2826 .mouse => |mouse| if (self.resultsBox(index).children.values()[detail_index].rect == null and widget.clickOnSelectedRow(self.listBox(index), root_focus, mouse)) { 2827 try self.refreshDetail(allocator, index); 2828 self.focusDetail(index, root_focus); 2829 }, 2830 else => {}, 2831 } 2832 } 2833 2834 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2835 const form = self.formBox() orelse return; 2836 const cid = form.getFocus().child_id orelse return; 2837 const cur = form.children.getIndex(cid) orelse return; 2838 const child = &form.children.values()[cur]; 2839 2840 if (child.widget == .text_input) { 2841 const text_input = &child.widget.text_input; 2842 if (text_input.options.multiline and try multilineKeepsKey(text_input, allocator, key)) 2843 return child.widget.input(allocator, key, root_focus); 2844 } 2845 2846 const on_submit = child.widget == .submit_button; 2847 switch (key) { 2848 .arrow_up, .back_tab => return if (formStep(form, cur, false)) |i| 2849 root_focus.setFocus(form.children.keys()[i]) 2850 else 2851 self.focusHeader(root_focus), 2852 .arrow_down, .tab => return if (formStep(form, cur, true)) |i| root_focus.setFocus(form.children.keys()[i]), 2853 .enter => if (on_submit) return self.submitForm(allocator), 2854 .mouse => |mouse| if (on_submit) { 2855 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) return self.submitForm(allocator); 2856 }, 2857 else => {}, 2858 } 2859 try child.widget.input(allocator, key, root_focus); 2860 } 2861 2862 fn confirmationFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2863 const form = self.confirmationForm() orelse return; 2864 const child_id = form.getFocus().child_id orelse return; 2865 const index = form.children.getIndex(child_id) orelse return; 2866 switch (key) { 2867 .arrow_up, .back_tab => if (index > 1) 2868 root_focus.setFocus(form.children.keys()[index - 1]) 2869 else 2870 self.focusHeader(root_focus), 2871 .arrow_down, .tab => if (index + 1 < form.children.count()) root_focus.setFocus(form.children.keys()[index + 1]), 2872 .enter => try self.submitConfirmation(allocator, index), 2873 .mouse => |mouse| for (form.children.keys()[1..], 1..) |button_id, button_index| { 2874 if (inp.leftClickOn(root_focus, button_id, mouse)) { 2875 try self.submitConfirmation(allocator, button_index); 2876 break; 2877 } 2878 }, 2879 else => {}, 2880 } 2881 } 2882 2883 // the multiline hunk inputs keep enter and any up/down that has a row to 2884 // move to; enter on a "use this" link is a navigation the host follows 2885 // before this runs. 2886 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2887 const form = self.formBox() orelse return; 2888 const cid = form.getFocus().child_id orelse return; 2889 const cur = form.children.getIndex(cid) orelse return; 2890 const child = &form.children.values()[cur]; 2891 2892 if (child.widget == .text_input) { 2893 const ti = &child.widget.text_input; 2894 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2895 return child.widget.input(allocator, key, root_focus); 2896 } 2897 2898 const on_submit = child.widget == .submit_button; 2899 2900 switch (key) { 2901 .arrow_up, .back_tab => if (formStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2902 .arrow_down, .tab => if (formStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2903 .arrow_left, .arrow_right => switch (child.widget) { 2904 .box => |*row| _ = widget.moveInRow(row, root_focus, key == .arrow_right), 2905 else => try child.widget.input(allocator, key, root_focus), 2906 }, 2907 .enter => if (on_submit) try self.submitResolution(allocator), 2908 .mouse => |mouse| if (on_submit) { 2909 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2910 }, 2911 else => try child.widget.input(allocator, key, root_focus), 2912 } 2913 } 2914 2915 // a multiline input keeps enter and any up/down that has a row to move to 2916 fn multilineKeepsKey(text_input: *wgt.TextInput, allocator: std.mem.Allocator, key: Key) !bool { 2917 return switch (key) { 2918 .enter => true, 2919 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2920 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2921 else => false, 2922 }; 2923 } 2924 2925 // the neighboring form control, skipping spacers and blank rows 2926 fn formStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2927 var i = cur; 2928 while (true) { 2929 if (down) { 2930 i += 1; 2931 if (i >= form.children.count()) return null; 2932 } else { 2933 if (i == 0) return null; 2934 i -= 1; 2935 } 2936 const child = &form.children.values()[i]; 2937 if (child.hidden) continue; 2938 switch (child.widget) { 2939 .spacer, .text => continue, 2940 else => return i, 2941 } 2942 } 2943 } 2944 2945 // focus the resolve-form child at `i` (a version row focuses its selected 2946 // child) and keep it visible on the terminal 2947 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2948 const child = &form.children.values()[i]; 2949 switch (child.widget) { 2950 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2951 else => root_focus.setFocus(form.children.keys()[i]), 2952 } 2953 if (self.session.is_terminal) { 2954 if (self.resolveScroll()) |sc| { 2955 if (child.rect) |rect| sc.scrollToRect(rect); 2956 } 2957 } 2958 } 2959 2960 // re-emit the conflicted thread's event with the resolved content; any 2961 // event on the thread settles its conflict entry. this is the terminal 2962 // path; the web posts the form to the resolve route. 2963 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2964 if (comptime wasm) return; 2965 const io = self.session.io orelse return; 2966 const src = self.data.repo_source orelse return; 2967 const entry = self.data.selectedThread() orelse return; 2968 const author = ((try self.session.authorize(self.data.identity, .write, kind)) orelse return).author; 2969 const form = self.formBox() orelse return; 2970 2971 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2972 var title: ?[]u8 = null; 2973 var labels: ?[]u8 = null; 2974 var hunks: std.ArrayList([]const u8) = .empty; 2975 defer { 2976 if (title) |t| allocator.free(t); 2977 if (labels) |t| allocator.free(t); 2978 for (hunks.items) |h| allocator.free(h); 2979 hunks.deinit(allocator); 2980 } 2981 for (form.children.values()) |*child| switch (child.widget) { 2982 .text_input => |*ti| { 2983 const text = try ti.text(allocator); 2984 errdefer allocator.free(text); 2985 if (std.mem.eql(u8, ti.options.name, "title")) { 2986 title = text; 2987 } else if (std.mem.eql(u8, ti.options.name, "labels")) { 2988 labels = text; 2989 } else { 2990 try hunks.append(allocator, text); 2991 } 2992 }, 2993 else => {}, 2994 }; 2995 2996 const id_bytes = try evt.parseEventId(entry.id); 2997 2998 switch (src.repo_kind) { 2999 inline else => |repo_kind| { 3000 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3001 defer any_repo.deinit(io, allocator); 3002 switch (any_repo) { 3003 inline else => |*repo| { 3004 var resolution: Event.Resolve = .{ 3005 .title = title, 3006 .labels = labels, 3007 .hunks = hunks.items, 3008 }; 3009 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 3010 const change = Event.Update{ .resolve = resolution }; 3011 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 3012 // leave the form up for correction 3013 error.InvalidFields => return, 3014 else => |e| return e, 3015 }; 3016 }, 3017 } 3018 }, 3019 } 3020 3021 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 3022 try self.session.navigate(route); 3023 } 3024 3025 // commit a new or edited comment and navigate to its permalink. this is the 3026 // terminal path; the web posts the form to its comment route. 3027 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 3028 if (comptime wasm) return; 3029 const io = self.session.io orelse return; 3030 const src = self.data.repo_source orelse return; 3031 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3032 const author = actor.author; 3033 const form = self.formBox() orelse return; 3034 const body_input = try formField(form, "body"); 3035 const body = try body_input.text(allocator); 3036 defer allocator.free(body); 3037 if (!evt.Comment.fieldsValid(body)) return; 3038 3039 var thread_id: [evt.event_id_size * 2]u8 = undefined; 3040 @memcpy(&thread_id, self.data.selected_id); 3041 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 3042 var parent: [evt.event_id_size * 2]u8 = undefined; 3043 @memcpy(&parent, self.data.comment_id); 3044 break :blk parent; 3045 }; 3046 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 3047 const editing = self.data.view == .edit_comment; 3048 if (editing) { 3049 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 3050 if (!try src.canModify(io, allocator, actor, .comment, &comment_id)) return; 3051 } 3052 switch (src.repo_kind) { 3053 inline else => |repo_kind| { 3054 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3055 defer any_repo.deinit(io, allocator); 3056 switch (any_repo) { 3057 inline else => |*repo| if (editing) { 3058 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 3059 try evt.Comment.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, &comment_id, body, author); 3060 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 3061 } else { 3062 event_id_hex = try evt.Comment.create(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, &parent_id, body, author); 3063 }, 3064 } 3065 }, 3066 } 3067 3068 body_input.clear(allocator); 3069 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 3070 try self.session.navigate(route); 3071 } 3072 3073 // terminal form submissions; the web posts to the forms' routes instead 3074 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 3075 switch (self.data.view) { 3076 .new_comment, .edit_comment => try self.submitComment(allocator), 3077 .edit => try self.submitEditedThread(allocator), 3078 else => try self.submitNewThread(allocator), 3079 } 3080 } 3081 3082 // commit the new thread to the repo's events branch and navigate to it. 3083 // this is the terminal path; the web posts the form to the thread route, 3084 // so the wasm side never runs (or compiles) the repo access below. 3085 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 3086 if (comptime wasm) return; 3087 const io = self.session.io orelse return; 3088 const src = self.data.repo_source orelse return; 3089 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3090 3091 const form = self.threadForm() orelse return; 3092 const title_input = try formField(form, "title"); 3093 const labels_input = try formField(form, "labels"); 3094 const description_input = try formField(form, "description"); 3095 3096 const title = try title_input.text(allocator); 3097 defer allocator.free(title); 3098 const labels = try labels_input.text(allocator); 3099 defer allocator.free(labels); 3100 const description = try description_input.text(allocator); 3101 defer allocator.free(description); 3102 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 3103 defer if (comptime supports_drafts) allocator.free(target_branch); 3104 3105 if (!Event.fieldsValid(title, labels)) { 3106 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 3107 return; 3108 } 3109 3110 if (comptime supports_drafts) { 3111 const source = formWidget(form, .patch_source) orelse return error.MissingFormField; 3112 const source_branch = switch (source.selectedKind()) { 3113 .fork => null, 3114 .branch => try source.field().text(allocator), 3115 }; 3116 defer if (source_branch) |branch| allocator.free(branch); 3117 const event_id_hex = Data.create(self.data, self.session, allocator, actor, title, labels, description, target_branch, source_branch) catch |err| { 3118 const failure = FeedbackFailure.fromError(err) orelse return err; 3119 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3120 return; 3121 }; 3122 title_input.clear(allocator); 3123 labels_input.clear(allocator); 3124 description_input.clear(allocator); 3125 (try formField(form, "target_branch")).clear(allocator); 3126 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 3127 try self.session.navigate(route); 3128 return; 3129 } 3130 3131 var id_bytes: [evt.event_id_size]u8 = undefined; 3132 io.random(&id_bytes); 3133 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 3134 3135 const event = evt.EventWithId{ 3136 .id = event_id_hex, 3137 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 3138 .author = actor.author, 3139 .event = @unionInit(evt.Event, @tagName(kind), Event{ 3140 .title = title, 3141 .description = description, 3142 .labels = labels, 3143 }), 3144 }; 3145 3146 switch (src.repo_kind) { 3147 inline else => |repo_kind| { 3148 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3149 defer any_repo.deinit(io, allocator); 3150 switch (any_repo) { 3151 inline else => |*repo| try evt.consume(self.session.eventHost(), .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 3152 } 3153 }, 3154 } 3155 3156 // wipe the form so a return visit starts fresh 3157 title_input.clear(allocator); 3158 labels_input.clear(allocator); 3159 description_input.clear(allocator); 3160 3161 const route = listRoute(self.data.identity, @fromBackingInt(@intCast(0)), "", &event_id_hex) orelse return; 3162 try self.session.navigate(route); 3163 } 3164 3165 // re-emit the selected thread's event with the form's content (status 3166 // preserved), then reload the thread's page. this is the terminal path; 3167 // the web posts the form to the edit route. 3168 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 3169 if (comptime wasm) return; 3170 const io = self.session.io orelse return; 3171 const entry = self.data.selectedThread() orelse return; 3172 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3173 const author = actor.author; 3174 3175 const form = self.threadForm() orelse return; 3176 const title_input = try formField(form, "title"); 3177 const labels_input = try formField(form, "labels"); 3178 const description_input = try formField(form, "description"); 3179 3180 const title = try title_input.text(allocator); 3181 defer allocator.free(title); 3182 const labels = try labels_input.text(allocator); 3183 defer allocator.free(labels); 3184 const description = try description_input.text(allocator); 3185 defer allocator.free(description); 3186 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 3187 defer if (comptime supports_drafts) allocator.free(target_branch); 3188 3189 if (!Event.fieldsValid(title, labels)) { 3190 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 3191 return; 3192 } 3193 const src = self.data.repo_source orelse return; 3194 3195 if (comptime supports_drafts) { 3196 if (entryDraft(entry.*)) { 3197 self.data.editDraft(self.session, allocator, author, entry.id, title, labels, description, target_branch) catch |err| { 3198 const failure = FeedbackFailure.fromError(err) orelse return err; 3199 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3200 return; 3201 }; 3202 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 3203 try self.session.navigate(route); 3204 return; 3205 } 3206 } 3207 3208 const id_bytes = try evt.parseEventId(entry.id); 3209 if (!try src.canModify(io, allocator, actor, kind, &id_bytes)) return; 3210 3211 switch (src.repo_kind) { 3212 inline else => |repo_kind| { 3213 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3214 defer any_repo.deinit(io, allocator); 3215 switch (any_repo) { 3216 inline else => |*repo| if (supports_drafts) { 3217 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 3218 .title = title, 3219 .labels = labels, 3220 .description = description, 3221 .target_branch = target_branch, 3222 } }, author) catch |err| { 3223 const failure = FeedbackFailure.fromError(err) orelse return err; 3224 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3225 return; 3226 }; 3227 } else if (has_status) 3228 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 3229 .title = title, 3230 .labels = labels, 3231 .description = description, 3232 } }, author) 3233 else 3234 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, labels, description, author), 3235 } 3236 }, 3237 } 3238 3239 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 3240 try self.session.navigate(route); 3241 } 3242 3243 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, labels: []const u8, description: []const u8, target_branch: []const u8) !void { 3244 const aa = self.session.arena.allocator(); 3245 const source = if (supports_drafts) blk: { 3246 const form = self.threadForm() orelse break :blk null; 3247 break :blk formWidget(form, .patch_source); 3248 } else null; 3249 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 3250 .failure = failure, 3251 .fields = if (comptime supports_drafts) .{ 3252 .title = try aa.dupe(u8, title), 3253 .labels = try aa.dupe(u8, labels), 3254 .description = try aa.dupe(u8, description), 3255 .target_branch = try aa.dupe(u8, target_branch), 3256 .source_branch = if (source) |value| try value.field().text(aa) else "", 3257 .source_kind = if (source) |value| value.selectedKind() else .fork, 3258 } else .{ 3259 .title = try aa.dupe(u8, title), 3260 .labels = try aa.dupe(u8, labels), 3261 .description = try aa.dupe(u8, description), 3262 }, 3263 }); 3264 } 3265 3266 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 3267 switch (confirmation(self.data.view) orelse return) { 3268 .publish => if (comptime supports_drafts) { 3269 if (comptime wasm) return; 3270 const entry = self.data.selectedThread() orelse return; 3271 if (!entryDraft(entry.*)) return; 3272 const author = ((try self.session.authorize(self.data.identity, .read, kind)) orelse return).author; 3273 try self.data.publishDraft(self.session, allocator, author, entry.id); 3274 const route = listRoute(self.data.identity, @fromBackingInt(@intCast(0)), "", entry.id) orelse return; 3275 try self.session.navigate(route); 3276 } else unreachable, 3277 .merge => if (comptime supports_merge) { 3278 if (comptime wasm) return; 3279 const entry = self.data.selectedThread() orelse return; 3280 const revision: evt.Patch.MergeRevision = if (button_index > 1) .squash else .source; 3281 if (!Data.canMerge(entry.*, self.session) or entry.mergeability.get(revision) != .clean) return; 3282 const author = ((try self.session.authorize(self.data.identity, .write, kind)) orelse return).author; 3283 self.data.mergePatch(self.session, allocator, author, entry.id, revision) catch |err| switch (err) { 3284 error.MergeConflict, error.MergeCheckUnavailable, error.PatchOutOfDate, error.PatchDataUnavailable => { 3285 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 3286 try self.session.navigate(route); 3287 return; 3288 }, 3289 else => return err, 3290 }; 3291 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 3292 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 3293 try self.session.navigate(route); 3294 } else unreachable, 3295 .remove => try self.submitRemove(allocator), 3296 } 3297 } 3298 3299 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 3300 const comment = self.data.comment_id.len != 0; 3301 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 3302 const id = evt.parseEventId(event_id) catch return; 3303 try self.removeEvent(allocator, if (comment) .comment else kind, id); 3304 } 3305 3306 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 3307 if (comptime wasm) return; 3308 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3309 const author = actor.author; 3310 3311 if (comptime supports_drafts) { 3312 const selected = self.data.selectedThread() orelse return; 3313 if (event_kind == kind and entryDraft(selected.*)) { 3314 try Data.removeDraft(self.session, allocator, author, &id); 3315 const route = draftsRoute(self.data.identity) orelse return; 3316 try self.session.navigate(route); 3317 return; 3318 } 3319 } 3320 3321 const io = self.session.io orelse return; 3322 const src = self.data.repo_source orelse return; 3323 if (!try src.canModify(io, allocator, actor, event_kind, &id)) return; 3324 const thread_id = evt.parseEventId(self.data.selected_id) catch return; 3325 3326 switch (src.repo_kind) { 3327 inline else => |repo_kind| { 3328 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3329 defer any_repo.deinit(io, allocator); 3330 switch (any_repo) { 3331 inline else => |*repo| try evt.removeInThread(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, event_kind, &id, author), 3332 } 3333 }, 3334 } 3335 3336 const id_hex = std.fmt.bytesToHex(id, .lower); 3337 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex, null) orelse return; 3338 try self.session.navigate(route); 3339 } 3340 3341 // enter the detail pane, landing on the way back to the list when it shows 3342 fn focusDetail(self: *This, index: usize, root_focus: *Focus) void { 3343 if (widget.listBackId(self.detailFrame(index)) != null) return widget.focusListBack(self.detailFrame(index), root_focus); 3344 _ = self.detail(index).focusFirst(root_focus); 3345 } 3346 3347 // return to the list. 3348 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 3349 root_focus.setFocus(self.listScroll(index).getFocus().id); 3350 } 3351 3352 // cross to the header tabs above the stack. 3353 fn focusHeader(self: *This, root_focus: *Focus) void { 3354 root_focus.setFocus(self.box.children.keys()[header_index]); 3355 } 3356 3357 pub fn clearGrid(self: *This) void { 3358 self.box.clearGrid(); 3359 } 3360 3361 pub fn getGrid(self: This) ?Grid { 3362 return self.box.getGrid(); 3363 } 3364 3365 pub fn getFocus(self: *This) *Focus { 3366 return self.box.getFocus(); 3367 } 3368 3369 // for the parent's "scroll up at the top jumps to the header" check: at the 3370 // top only while the header tabs hold focus, so up from the split 3371 // crosses into the tabs first. 3372 pub fn atTop(self: *This) bool { 3373 return self.headerActive(); 3374 } 3375 }; 3376 } 3377