╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1998 fn focusAuthor(self: *View, index: usize, root_focus: *Focus) void { 1999 const id = self.author_id[index] orelse return; 2000 root_focus.setFocus(id); 2001 self.detailScroll(index).y = 0; 2002 } 2003 2004 // the title sits at the top of the pane, so focusing it scrolls there. 2005 fn focusTitle(self: *View, index: usize, root_focus: *Focus) void { 2006 const inner = self.detailInner(index); 2007 if (inner.children.count() == 0) return; 2008 root_focus.setFocus(inner.children.keys()[title_child_index]); 2009 self.detailScroll(index).y = 0; 2010 } 2011 2012 fn focusTags(self: *View, index: usize, root_focus: *Focus) !void { 2013 const tf = self.tagFlow(index) orelse return; 2014 if (tf.text_boxes.items.len == 0) return; 2015 const cid = tf.focus.child_id orelse tf.text_boxes.items[0].getFocus().id; 2016 const item = tf.indexOfFocusId(cid) orelse 0; 2017 self.focusTag(index, tf, root_focus, item); 2018 } 2019 2020 fn focusDescription(self: *View, index: usize, root_focus: *Focus) !void { 2021 if (self.description_id[index]) |id| root_focus.setFocus(id); 2022 } 2023 2024 // return to the tool row's last-focused button (the header when the 2025 // description page shows no row). 2026 fn focusToolRow(self: *View, index: usize, root_focus: *Focus) void { 2027 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 2028 root_focus.setFocus(cid); 2029 } 2030 2031 // enter the detail pane. an empty pane (no issues) can't be entered. 2032 fn focusDetail(self: *View, index: usize, root_focus: *Focus) !void { 2033 if (self.detailInner(index).children.count() == 0) return; 2034 root_focus.setFocus(self.detailOuter(index).getFocus().id); 2035 } 2036 2037 // return to the list. 2038 fn focusList(self: *View, index: usize, root_focus: *Focus) !void { 2039 root_focus.setFocus(self.listScroll(index).getFocus().id); 2040 } 2041 2042 // cross to the header tabs above the stack. 2043 fn focusHeader(self: *View, root_focus: *Focus) void { 2044 root_focus.setFocus(self.box.children.keys()[header_index]); 2045 } 2046 2047 pub fn clearGrid(self: *View) void { 2048 self.box.clearGrid(); 2049 } 2050 2051 pub fn getGrid(self: View) ?Grid { 2052 return self.box.getGrid(); 2053 } 2054 2055 pub fn getFocus(self: *View) *Focus { 2056 return self.box.getFocus(); 2057 } 2058 2059 // for the parent's "scroll up at the top jumps to the header" check: at the 2060 // top only while the header tabs hold focus, so up from the split 2061 // crosses into the tabs first. 2062 pub fn getSelectedIndex(self: *View) ?usize { 2063 return if (self.headerActive()) 0 else 1; 2064 } 2065 }; 2066 2067 // the "a:" navigation link for `status`'s issues page filtered to the 2068 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 2069 // a rooted url ignores `status` (it derives its view from the issue's own); 2070 // with an empty `id` this is the bare list link. 2071 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 2072 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 2073 const url = try route.toUrl(page_arena); 2074 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2075 } 2076 2077 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 2078 // href is only followed with js off. 2079 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2080 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 2081 const url = try route.toUrl(page_arena); 2082 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 2083 } 2084 2085 // the "a:" link to the window `status_maybe` names, keeping the tag filter, 2086 // or the conflicts window when null, rooted at issue `id`. 2087 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?evt.Issue.Status, id: []const u8) ![]const u8 { 2088 if (status_maybe) |status| return issuesLink(page_arena, data.identity, status, data.tag, id); 2089 return conflictsLink(page_arena, data.identity, id); 2090 } 2091 2092 // the "a:" link to the conflicts list rooted at issue `start` ("" = the 2093 // first window). 2094 fn conflictsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, start: []const u8) ![]const u8 { 2095 const route = ui.RoutablePage.repoIssuesConflictsRoute(identity, start) orelse return error.RouteTooLong; 2096 const url = try route.toUrl(page_arena); 2097 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2098 } 2099 2100 // the "a:" link to the whole-description page of issue `id` within `identity`. 2101 fn descriptionLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2102 const route = ui.RoutablePage.repoIssuesDescriptionRoute(identity, id) orelse return error.RouteTooLong; 2103 const url = try route.toUrl(page_arena); 2104 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2105 } 2106 2107 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 2108 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 2109 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 2110 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 2111 const url = try route.toUrl(page_arena); 2112 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2113 } 2114 2115 // tabs switching between the issues page's views. 2116 pub const Header = struct { 2117 box: wgt.Box(ui.Widget), 2118 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 2119 2120 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 2121 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2122 errdefer box.deinit(allocator); 2123 2124 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 2125 errdefer tab_ids.deinit(allocator); 2126 2127 const aa = session.page_arena.allocator(); 2128 2129 // a list tab per status, labeled with its listing's issue count 2130 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 2131 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 2132 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2133 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 2134 try addTab(allocator, &box, &tab_ids, label, link); 2135 } 2136 2137 // tags tab, labeled with the active tag filter 2138 { 2139 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 2140 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 2141 const label = if (data.tag.len == 0) "tags" else blk: { 2142 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 2143 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 2144 }; 2145 try addTab(allocator, &box, &tab_ids, label, tags_link); 2146 } 2147 2148 // new-issue tab; an edit or resolve url shows its tab in this place 2149 { 2150 const route = switch (data.view) { 2151 .edit => ui.RoutablePage.repoIssuesEditRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 2152 .resolve => ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 2153 else => ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong, 2154 }; 2155 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2156 const label: []const u8 = switch (data.view) { 2157 .edit => "edit", 2158 .resolve => "resolve", 2159 else => "new", 2160 }; 2161 try addTab(allocator, &box, &tab_ids, label, link); 2162 } 2163 2164 // conflicts tab, labeled with the conflict count 2165 if (data.conflicts.count > 0) { 2166 const route = ui.RoutablePage.repoIssuesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 2167 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2168 const label = try std.fmt.allocPrint(aa, "conflicts ({d})", .{data.conflicts.count}); 2169 try addTab(allocator, &box, &tab_ids, label, link); 2170 } 2171 2172 var self = Header{ .box = box, .tab_ids = tab_ids }; 2173 // the tab matching the page's view is selected initially. 2174 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 2175 return self; 2176 } 2177 2178 fn addTab(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), tab_ids: *std.AutoArrayHashMapUnmanaged(usize, void), label: []const u8, link: []const u8) !void { 2179 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2180 errdefer text_box.deinit(allocator); 2181 text_box.getFocus().focusable = true; 2182 text_box.getFocus().kind = .{ .custom = link }; 2183 try tab_ids.put(allocator, text_box.getFocus().id, {}); 2184 try box.children.put(allocator, text_box.getFocus().id, .{ 2185 .widget = .{ .text_box = text_box }, 2186 .rect = null, 2187 .min_size = .{ .width = label.len + 2, .height = null }, 2188 }); 2189 } 2190 2191 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 2192 self.box.deinit(allocator); 2193 self.tab_ids.deinit(allocator); 2194 } 2195 2196 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2197 self.clearGrid(); 2198 // only the selected tab shows its border 2199 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 2200 switch (child.widget) { 2201 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 2202 else => {}, 2203 } 2204 } 2205 try self.box.build(allocator, constraint, root_focus); 2206 } 2207 2208 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2209 _ = allocator; 2210 const current_tab = self.currentTabIndex() orelse return; 2211 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 2212 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 2213 } 2214 } 2215 2216 pub fn clearGrid(self: *Header) void { 2217 self.box.clearGrid(); 2218 } 2219 2220 pub fn getGrid(self: Header) ?Grid { 2221 return self.box.getGrid(); 2222 } 2223 2224 pub fn getFocus(self: *Header) *Focus { 2225 return self.box.getFocus(); 2226 } 2227 2228 pub fn getSelectedIndex(self: Header) ?usize { 2229 return self.currentTabIndex(); 2230 } 2231 2232 fn currentTabIndex(self: Header) ?usize { 2233 const child_id = self.box.focus.child_id orelse return null; 2234 return self.tab_ids.getIndex(child_id); 2235 } 2236 }; 2237