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