╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1920 1921 switch (key) { 1922 .arrow_up, .back_tab => if (cur > comment_author_field_index) 1923 root_focus.setFocus(form.children.keys()[cur - 1]) 1924 else 1925 self.focusHeader(root_focus), 1926 .arrow_down, .tab => if (cur < comment_submit_field_index) 1927 root_focus.setFocus(form.children.keys()[cur + 1]), 1928 .enter => if (cur == comment_submit_field_index) try self.submitComment(allocator), 1929 .mouse => |mouse| if (cur == comment_submit_field_index) { 1930 const submit = &child.widget.submit_button; 1931 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitComment(allocator); 1932 }, 1933 else => try child.widget.input(allocator, key, root_focus), 1934 } 1935 } 1936 1937 // the multiline hunk inputs keep enter and any up/down that has a row to 1938 // move to; enter on a "use this" link is a navigation the host follows 1939 // before this runs. 1940 fn resolveInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1941 const form = self.issueForm() orelse return; 1942 const cid = form.getFocus().child_id orelse return; 1943 const cur = form.children.getIndex(cid) orelse return; 1944 const child = &form.children.values()[cur]; 1945 1946 if (child.widget == .text_input) { 1947 const ti = &child.widget.text_input; 1948 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 1949 return child.widget.input(allocator, key, root_focus); 1950 } 1951 1952 const on_submit = child.widget == .submit_button; 1953 1954 switch (key) { 1955 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 1956 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 1957 .arrow_left, .arrow_right => switch (child.widget) { 1958 .box => |*row| { 1959 const row_cid = row.getFocus().child_id orelse return; 1960 const row_index = row.children.getIndex(row_cid) orelse return; 1961 if (key == .arrow_left) { 1962 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 1963 } else if (row_index + 1 < row.children.count()) { 1964 root_focus.setFocus(row.children.keys()[row_index + 1]); 1965 } 1966 }, 1967 else => try child.widget.input(allocator, key, root_focus), 1968 }, 1969 .enter => if (on_submit) try self.submitResolution(allocator), 1970 .mouse => |mouse| if (on_submit) { 1971 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 1972 }, 1973 else => try child.widget.input(allocator, key, root_focus), 1974 } 1975 } 1976 1977 // a multiline input keeps enter and any up/down that has a row to move to 1978 fn multilineKeepsKey(text_input: *wgt.TextInput(ui.Widget), allocator: std.mem.Allocator, key: Key) !bool { 1979 return switch (key) { 1980 .enter => true, 1981 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 1982 .arrow_down => !try text_input.cursorOnLastRow(allocator), 1983 else => false, 1984 }; 1985 } 1986 1987 // the neighboring focusable resolve-form child, skipping the spacer and 1988 // the blank gap rows 1989 fn resolveStep(form: *wgt.Box(ui.Widget), cur: usize, down: bool) ?usize { 1990 var i = cur; 1991 while (true) { 1992 if (down) { 1993 i += 1; 1994 if (i >= form.children.count()) return null; 1995 } else { 1996 if (i == 0) return null; 1997 i -= 1; 1998 } 1999 switch (form.children.values()[i].widget) { 2000 .spacer, .text => continue, 2001 else => return i, 2002 } 2003 } 2004 } 2005 2006 // focus the resolve-form child at `i` (a version row focuses its selected 2007 // child) and keep it visible on the terminal 2008 fn focusResolveChild(self: *View, form: *wgt.Box(ui.Widget), i: usize, root_focus: *Focus) void { 2009 const child = &form.children.values()[i]; 2010 switch (child.widget) { 2011 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2012 else => root_focus.setFocus(form.children.keys()[i]), 2013 } 2014 if (self.session.is_terminal) { 2015 if (self.resolveScroll()) |sc| { 2016 if (child.rect) |rect| sc.scrollToRect(rect); 2017 } 2018 } 2019 } 2020 2021 // re-emit the conflicted issue's event with the resolved content; any 2022 // event on the issue settles its conflict entry. this is the terminal 2023 // path; the web posts the form to the resolve route. 2024 fn submitResolution(self: *View, allocator: std.mem.Allocator) !void { 2025 if (comptime wasm) return; 2026 const io = self.session.io orelse return; 2027 const src = self.data.repo_source orelse return; 2028 const entry = self.data.selectedIssue() orelse return; 2029 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2030 const form = self.issueForm() orelse return; 2031 2032 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2033 var title: ?[]u8 = null; 2034 var tags: ?[]u8 = null; 2035 var hunks: std.ArrayList([]const u8) = .empty; 2036 defer { 2037 if (title) |t| allocator.free(t); 2038 if (tags) |t| allocator.free(t); 2039 for (hunks.items) |h| allocator.free(h); 2040 hunks.deinit(allocator); 2041 } 2042 for (form.children.values()) |*child| switch (child.widget) { 2043 .text_input => |*ti| { 2044 const text = try ti.text(allocator); 2045 errdefer allocator.free(text); 2046 if (std.mem.eql(u8, ti.options.name, "title")) { 2047 title = text; 2048 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2049 tags = text; 2050 } else { 2051 try hunks.append(allocator, text); 2052 } 2053 }, 2054 else => {}, 2055 }; 2056 2057 var id_bytes: [evt.event_id_size]u8 = undefined; 2058 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2059 2060 switch (src.repo_kind) { 2061 inline else => |repo_kind| { 2062 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2063 defer any_repo.deinit(io, allocator); 2064 switch (any_repo) { 2065 inline else => |*repo| evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .resolve = .{ 2066 .title = title, 2067 .tags = tags, 2068 .hunks = hunks.items, 2069 } }, author_email) catch |err| switch (err) { 2070 // leave the form up for correction 2071 error.InvalidFields => return, 2072 else => |e| return e, 2073 }, 2074 } 2075 }, 2076 } 2077 2078 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 2079 try self.session.navigate(route); 2080 } 2081 2082 // commit a new or edited comment and navigate to its permalink. this is the 2083 // terminal path; the web posts the form to its comment route. 2084 fn submitComment(self: *View, allocator: std.mem.Allocator) !void { 2085 if (comptime wasm) return; 2086 const io = self.session.io orelse return; 2087 const src = self.data.repo_source orelse return; 2088 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2089 const form = self.issueForm() orelse return; 2090 const body_input = &form.children.values()[comment_body_field_index].widget.text_input; 2091 const body = try body_input.text(allocator); 2092 defer allocator.free(body); 2093 if (!evt.Comment.fieldsValid(body)) return; 2094 2095 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2096 @memcpy(&thread_id, self.data.selected_id); 2097 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2098 var parent: [evt.event_id_size * 2]u8 = undefined; 2099 @memcpy(&parent, self.data.comment_id); 2100 break :blk parent; 2101 }; 2102 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2103 const editing = self.data.view == .edit_comment; 2104 switch (src.repo_kind) { 2105 inline else => |repo_kind| { 2106 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2107 defer any_repo.deinit(io, allocator); 2108 switch (any_repo) { 2109 inline else => |*repo| if (editing) { 2110 var comment_id: [evt.event_id_size]u8 = undefined; 2111 _ = std.fmt.hexToBytes(&comment_id, self.data.comment_id) catch return; 2112 try evt.Comment.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author_email); 2113 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2114 } else { 2115 event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author_email); 2116 }, 2117 } 2118 }, 2119 } 2120 2121 body_input.clear(allocator); 2122 const route = ui.RoutablePage.repoCommentsRoute(self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2123 try self.session.navigate(route); 2124 } 2125 2126 // the terminal submit paths: the new form commits a new-issue event, the 2127 // edit form re-emits the selected issue's event with the form's content. 2128 // the web posts the forms to their routes instead. 2129 fn submitForm(self: *View, allocator: std.mem.Allocator) !void { 2130 if (self.data.view == .edit) { 2131 try self.submitEditedIssue(allocator); 2132 } else { 2133 try self.submitNewIssue(allocator); 2134 } 2135 } 2136 2137 // commit the new issue to the repo's events branch and navigate to it. 2138 // this is the terminal path; the web posts the form to the issue route, 2139 // so the wasm side never runs (or compiles) the repo access below. 2140 fn submitNewIssue(self: *View, allocator: std.mem.Allocator) !void { 2141 if (comptime wasm) return; 2142 const io = self.session.io orelse return; 2143 const src = self.data.repo_source orelse return; 2144 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2145 2146 const form = self.issueForm() orelse return; 2147 const title_input = &form.children.values()[title_field_index].widget.text_input; 2148 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2149 const description_input = &form.children.values()[description_field_index].widget.text_input; 2150 2151 const title = try title_input.text(allocator); 2152 defer allocator.free(title); 2153 const tags = try tags_input.text(allocator); 2154 defer allocator.free(tags); 2155 const description = try description_input.text(allocator); 2156 defer allocator.free(description); 2157 2158 if (!evt.Issue.fieldsValid(title, tags)) return; 2159 2160 var id_bytes: [evt.event_id_size]u8 = undefined; 2161 io.random(&id_bytes); 2162 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2163 2164 const event = evt.EventWithId{ 2165 .id = event_id_hex, 2166 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2167 .author_email = author_email, 2168 .event = .{ .issue = .{ 2169 .title = title, 2170 .description = description, 2171 .tags = tags, 2172 } }, 2173 }; 2174 2175 switch (src.repo_kind) { 2176 inline else => |repo_kind| { 2177 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2178 defer any_repo.deinit(io, allocator); 2179 switch (any_repo) { 2180 inline else => |*repo| try evt.consume(repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2181 } 2182 }, 2183 } 2184 2185 // wipe the form so a return visit starts fresh 2186 title_input.clear(allocator); 2187 tags_input.clear(allocator); 2188 description_input.clear(allocator); 2189 2190 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, "", &event_id_hex) orelse return; 2191 try self.session.navigate(route); 2192 } 2193 2194 // re-emit the selected issue's event with the form's content (status 2195 // preserved), then reload the issue's page. this is the terminal path; 2196 // the web posts the form to the edit route. 2197 fn submitEditedIssue(self: *View, allocator: std.mem.Allocator) !void { 2198 if (comptime wasm) return; 2199 const io = self.session.io orelse return; 2200 const src = self.data.repo_source orelse return; 2201 const entry = self.data.selectedIssue() orelse return; 2202 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2203 2204 const form = self.issueForm() orelse return; 2205 const title_input = &form.children.values()[title_field_index].widget.text_input; 2206 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2207 const description_input = &form.children.values()[description_field_index].widget.text_input; 2208 2209 const title = try title_input.text(allocator); 2210 defer allocator.free(title); 2211 const tags = try tags_input.text(allocator); 2212 defer allocator.free(tags); 2213 const description = try description_input.text(allocator); 2214 defer allocator.free(description); 2215 2216 if (!evt.Issue.fieldsValid(title, tags)) return; 2217 2218 var id_bytes: [evt.event_id_size]u8 = undefined; 2219 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2220 2221 switch (src.repo_kind) { 2222 inline else => |repo_kind| { 2223 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2224 defer any_repo.deinit(io, allocator); 2225 switch (any_repo) { 2226 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2227 .title = title, 2228 .tags = tags, 2229 .description = description, 2230 } }, author_email), 2231 } 2232 }, 2233 } 2234 2235 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 2236 try self.session.navigate(route); 2237 } 2238 2239 // flip the shown issue's status by re-emitting its event, then reload the 2240 // page rooted at the issue so the view reflects the change. this is the 2241 // terminal path; the web posts the button's form to the status route. 2242 fn toggleIssueStatus(self: *View, allocator: std.mem.Allocator, index: usize) !void { 2243 if (comptime wasm) return; 2244 const io = self.session.io orelse return; 2245 const src = self.data.repo_source orelse return; 2246 const sel = self.detailed_index[index] orelse return; 2247 const entry = self.window(index).issues[sel]; 2248 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2249 2250 const status: evt.Issue.Status = switch (entry.issue.event.status) { 2251 .open => .closed, 2252 .closed => .open, 2253 }; 2254 2255 var id_bytes: [evt.event_id_size]u8 = undefined; 2256 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2257 2258 switch (src.repo_kind) { 2259 inline else => |repo_kind| { 2260 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2261 defer any_repo.deinit(io, allocator); 2262 switch (any_repo) { 2263 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .status = status }, author_email), 2264 } 2265 }, 2266 } 2267 2268 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, status, "", entry.id) orelse return; 2269 try self.session.navigate(route); 2270 } 2271 2272 // arrow keys move the tag selection; at the flow's edges focus crosses to 2273 // the neighboring widgets. 2274 fn tagsInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 2275 const tf = self.tagFlow(index) orelse return; 2276 const cid = tf.focus.child_id orelse return; 2277 const cur = tf.indexOfFocusId(cid) orelse return; 2278 const count = tf.text_boxes.items.len; 2279 switch (key) { 2280 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 2281 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 2282 .arrow_up => if (tf.rowStep(cur, false)) |i| self.focusTag(index, tf, root_focus, i) else self.focusAuthor(index, root_focus), 2283 .arrow_down => if (tf.rowStep(cur, true)) |i| self.focusTag(index, tf, root_focus, i) else try self.focusDescription(index, root_focus), 2284 .home => self.focusTag(index, tf, root_focus, 0), 2285 .end => self.focusTag(index, tf, root_focus, count - 1), 2286 else => {}, 2287 } 2288 } 2289 2290 const button_in_row_index: usize = 1; 2291 const title_child_index: usize = 0; 2292 const tags_child_index: usize = 2; 2293 2294 fn tagFlow(self: *View, index: usize) ?*ui.TagFlow { 2295 const inner = self.detailInner(index); 2296 if (inner.children.count() <= tags_child_index) return null; 2297 return switch (inner.children.values()[tags_child_index].widget) { 2298 .tag_flow => |*tf| tf, 2299 else => null, 2300 }; 2301 } 2302 2303 fn tagsFocused(self: *View, index: usize) bool { 2304 const inner = self.detailInner(index); 2305 const cid = inner.getFocus().child_id orelse return false; 2306 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 2307 } 2308 2309 // the open/close button inside the detail frame's tool row, identified 2310 // by its posting kind; a logged-out session's or conflicted issue's row 2311 // has none. 2312 fn statusButton(self: *View, index: usize) ?*wgt.TextBox(ui.Widget) { 2313 const row = self.toolRow(index); 2314 if (row.children.count() <= button_in_row_index) return null; 2315 switch (row.children.values()[button_in_row_index].widget) { 2316 .text_box => |*tb| switch (tb.box.focus.kind) { 2317 .custom => |custom| if (std.mem.eql(u8, custom, "submit")) return tb, 2318 else => {}, 2319 }, 2320 else => {}, 2321 } 2322 return null; 2323 } 2324 2325 fn toolRowFocused(self: *View, index: usize) bool { 2326 const outer = self.detailOuter(index); 2327 const cid = outer.getFocus().child_id orelse return false; 2328 return outer.children.getIndex(cid) == tool_row_index; 2329 } 2330 2331 fn focusTag(self: *View, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 2332 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 2333 // keep the tag visible on the terminal: its rect offset by the flow's 2334 // position in the pane. 2335 if (self.session.is_terminal and item < tf.rects.items.len) { 2336 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 2337 var rect = tf.rects.items[item]; 2338 rect.x += flow_rect.x; 2339 rect.y += flow_rect.y; 2340 self.detailScroll(index).scrollToRect(rect); 2341 } 2342 } 2343 } 2344 2345 fn titleFocused(self: *View, index: usize) bool { 2346 const inner = self.detailInner(index); 2347 const cid = inner.getFocus().child_id orelse return false; 2348 return inner.children.getIndex(cid) == title_child_index; 2349 } 2350 2351 fn authorPresent(self: *View, index: usize) bool { 2352 return self.author_id[index] != null; 2353 } 2354 2355 fn authorFocused(self: *View, index: usize) bool { 2356 const inner = self.detailInner(index); 2357 const cid = inner.getFocus().child_id orelse return false; 2358 return cid == self.author_id[index]; 2359 } 2360 2361 fn descriptionFocused(self: *View, index: usize) bool { 2362 const cid = self.detailInner(index).getFocus().child_id orelse return false; 2363 return cid == self.description_id[index]; 2364 } 2365 2366 // the author sits right under the title at the top of the pane, so 2367 // focusing it scrolls there. 2368 fn focusAuthor(self: *View, index: usize, root_focus: *Focus) void { 2369 const id = self.author_id[index] orelse return; 2370 root_focus.setFocus(id); 2371 self.detailScroll(index).y = 0; 2372 } 2373 2374 // the title sits at the top of the pane, so focusing it scrolls there. 2375 fn focusTitle(self: *View, index: usize, root_focus: *Focus) void { 2376 const inner = self.detailInner(index); 2377 if (inner.children.count() == 0) return; 2378 root_focus.setFocus(inner.children.keys()[title_child_index]); 2379 self.detailScroll(index).y = 0; 2380 } 2381 2382 fn focusTags(self: *View, index: usize, root_focus: *Focus) !void { 2383 const tf = self.tagFlow(index) orelse return; 2384 if (tf.text_boxes.items.len == 0) return; 2385 const cid = tf.focus.child_id orelse tf.text_boxes.items[0].getFocus().id; 2386 const item = tf.indexOfFocusId(cid) orelse 0; 2387 self.focusTag(index, tf, root_focus, item); 2388 } 2389 2390 fn focusDescription(self: *View, index: usize, root_focus: *Focus) !void { 2391 if (self.description_id[index]) |id| root_focus.setFocus(id); 2392 } 2393 2394 // return to the tool row's last-focused button (the header when the 2395 // description page shows no row). 2396 fn focusToolRow(self: *View, index: usize, root_focus: *Focus) void { 2397 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 2398 root_focus.setFocus(cid); 2399 } 2400 2401 // enter the detail pane. an empty pane (no issues) can't be entered. 2402 fn focusDetail(self: *View, index: usize, root_focus: *Focus) !void { 2403 if (self.detailInner(index).children.count() == 0) return; 2404 root_focus.setFocus(self.detailOuter(index).getFocus().id); 2405 } 2406 2407 // return to the list. 2408 fn focusList(self: *View, index: usize, root_focus: *Focus) !void { 2409 root_focus.setFocus(self.listScroll(index).getFocus().id); 2410 } 2411 2412 // cross to the header tabs above the stack. 2413 fn focusHeader(self: *View, root_focus: *Focus) void { 2414 root_focus.setFocus(self.box.children.keys()[header_index]); 2415 } 2416 2417 pub fn clearGrid(self: *View) void { 2418 self.box.clearGrid(); 2419 } 2420 2421 pub fn getGrid(self: View) ?Grid { 2422 return self.box.getGrid(); 2423 } 2424 2425 pub fn getFocus(self: *View) *Focus { 2426 return self.box.getFocus(); 2427 } 2428 2429 // for the parent's "scroll up at the top jumps to the header" check: at the 2430 // top only while the header tabs hold focus, so up from the split 2431 // crosses into the tabs first. 2432 pub fn getSelectedIndex(self: *View) ?usize { 2433 return if (self.headerActive()) 0 else 1; 2434 } 2435 }; 2436 2437 // the "a:" navigation link for `status`'s issues page filtered to the 2438 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 2439 // a rooted url ignores `status` (it derives its view from the issue's own); 2440 // with an empty `id` this is the bare list link. 2441 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 2442 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 2443 const url = try route.toUrl(page_arena); 2444 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2445 } 2446 2447 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 2448 // href is only followed with js off. 2449 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2450 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 2451 const url = try route.toUrl(page_arena); 2452 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 2453 } 2454 2455 // the "a:" link to the window `status_maybe` names, keeping the tag filter, 2456 // or the conflicts window when null, rooted at issue `id`. 2457 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?evt.Issue.Status, id: []const u8) ![]const u8 { 2458 if (status_maybe) |status| return issuesLink(page_arena, data.identity, status, data.tag, id); 2459 return conflictsLink(page_arena, data.identity, id); 2460 } 2461 2462 // the "a:" link to the conflicts list rooted at issue `start` ("" = the 2463 // first window). 2464 fn conflictsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, start: []const u8) ![]const u8 { 2465 const route = ui.RoutablePage.repoIssuesConflictsRoute(identity, start) orelse return error.RouteTooLong; 2466 const url = try route.toUrl(page_arena); 2467 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2468 } 2469 2470 // the "a:" link to the whole-description page of issue `id` within `identity`. 2471 fn descriptionLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2472 const route = ui.RoutablePage.repoIssuesDescriptionRoute(identity, id) orelse return error.RouteTooLong; 2473 const url = try route.toUrl(page_arena); 2474 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2475 } 2476 2477 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 2478 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 2479 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 2480 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 2481 const url = try route.toUrl(page_arena); 2482 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2483 } 2484 2485 // tabs switching between the issues page's views. 2486 pub const Header = struct { 2487 box: wgt.Box(ui.Widget), 2488 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 2489 2490 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 2491 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2492 errdefer box.deinit(allocator); 2493 2494 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 2495 errdefer tab_ids.deinit(allocator); 2496 2497 const aa = session.page_arena.allocator(); 2498 2499 // a list tab per status, labeled with its listing's issue count 2500 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 2501 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 2502 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2503 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 2504 try addTab(allocator, &box, &tab_ids, label, link); 2505 } 2506 2507 // tags tab, labeled with the active tag filter 2508 { 2509 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 2510 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 2511 const label = if (data.tag.len == 0) "tags" else blk: { 2512 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 2513 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 2514 }; 2515 try addTab(allocator, &box, &tab_ids, label, tags_link); 2516 } 2517 2518 // new-issue tab; an edit or resolve url shows its tab in this place 2519 { 2520 const route = switch (data.view) { 2521 .edit => ui.RoutablePage.repoIssuesEditRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 2522 .new_comment => ui.RoutablePage.repoCommentNewRoute(data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 2523 .edit_comment => ui.RoutablePage.repoCommentEditRoute(data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 2524 .resolve => ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 2525 else => ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong, 2526 }; 2527 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2528 const label: []const u8 = switch (data.view) { 2529 .edit => "edit", 2530 .new_comment => "reply", 2531 .edit_comment => "edit", 2532 .resolve => "resolve", 2533 else => "new", 2534 }; 2535 try addTab(allocator, &box, &tab_ids, label, link); 2536 } 2537 2538 // conflicts tab, labeled with the conflict count 2539 if (data.conflicts.count > 0) { 2540 const route = ui.RoutablePage.repoIssuesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 2541 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2542 const label = try std.fmt.allocPrint(aa, "conflicts ({d})", .{data.conflicts.count}); 2543 try addTab(allocator, &box, &tab_ids, label, link); 2544 } 2545 2546 var self = Header{ .box = box, .tab_ids = tab_ids }; 2547 // the tab matching the page's view is selected initially. 2548 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 2549 return self; 2550 } 2551 2552 fn addTab(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), tab_ids: *std.AutoArrayHashMapUnmanaged(usize, void), label: []const u8, link: []const u8) !void { 2553 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2554 errdefer text_box.deinit(allocator); 2555 text_box.getFocus().focusable = true; 2556 text_box.getFocus().kind = .{ .custom = link }; 2557 try tab_ids.put(allocator, text_box.getFocus().id, {}); 2558 try box.children.put(allocator, text_box.getFocus().id, .{ 2559 .widget = .{ .text_box = text_box }, 2560 .rect = null, 2561 .min_size = .{ .width = label.len + 2, .height = null }, 2562 }); 2563 } 2564 2565 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 2566 self.box.deinit(allocator); 2567 self.tab_ids.deinit(allocator); 2568 } 2569 2570 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2571 self.clearGrid(); 2572 // only the selected tab shows its border 2573 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 2574 switch (child.widget) { 2575 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 2576 else => {}, 2577 } 2578 } 2579 try self.box.build(allocator, constraint, root_focus); 2580 } 2581 2582 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2583 _ = allocator; 2584 const current_tab = self.currentTabIndex() orelse return; 2585 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 2586 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 2587 } 2588 } 2589 2590 pub fn clearGrid(self: *Header) void { 2591 self.box.clearGrid(); 2592 } 2593 2594 pub fn getGrid(self: Header) ?Grid { 2595 return self.box.getGrid(); 2596 } 2597 2598 pub fn getFocus(self: *Header) *Focus { 2599 return self.box.getFocus(); 2600 } 2601 2602 pub fn getSelectedIndex(self: Header) ?usize { 2603 return self.currentTabIndex(); 2604 } 2605 2606 fn currentTabIndex(self: Header) ?usize { 2607 const child_id = self.box.focus.child_id orelse return null; 2608 return self.tab_ids.getIndex(child_id); 2609 } 2610 }; 2611