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