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