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