╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
994 // arrow keys move the tag selection; up from the top row crosses to the 995 // header tabs. 996 fn tagsViewInput(self: *View, key: Key, root_focus: *Focus) void { 997 const tf = self.tagsView(); 998 const cid = tf.focus.child_id orelse return; 999 const cur = tf.indexOfFocusId(cid) orelse return; 1000 const count = tf.text_boxes.items.len; 1001 switch (key) { 1002 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 1003 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 1004 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 1005 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 1006 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 1007 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 1008 else => {}, 1009 } 1010 } 1011 1012 fn listInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1013 // up/down (and the scroll wheel) move the selection a row; page up/down 1014 // jump a fixed amount. right/Enter cross into the detail pane. up from 1015 // the top row crosses into the header tabs. 1016 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 1017 const lb = self.listBox(index); 1018 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 1019 if (delta < 0 and at_top) return self.focusHeader(root_focus); 1020 ui.moveRowFocus(lb, self.listScroll(index), root_focus, delta); 1021 return; 1022 } 1023 switch (key) { 1024 .enter, .arrow_right => try self.focusDetail(index, root_focus), 1025 else => {}, 1026 } 1027 } 1028 1029 fn detailInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1030 if (self.toolRowFocused(index)) { 1031 try self.toolRowInput(allocator, index, key, root_focus); 1032 } else if (self.titleFocused(index)) { 1033 try self.titleInput(index, key, root_focus); 1034 } else if (self.authorFocused(index)) { 1035 try self.authorInput(index, key, root_focus); 1036 } else if (self.tagsFocused(index)) { 1037 try self.tagsInput(index, key, root_focus); 1038 } else { 1039 try self.descriptionInput(index, key, root_focus); 1040 } 1041 } 1042 1043 // the tool row: enter or a click on the open/close button flips the 1044 // issue's status; the edit button is an a: link the host follows. 1045 // arrows cross between the buttons and to the neighboring widgets. 1046 fn toolRowInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1047 const row = self.toolRow(index); 1048 // the edit button is always the row's last child; the open/close 1049 // button before it is only there when the session may author events 1050 const edit_index = row.children.count() - 1; 1051 const on_edit = if (row.getFocus().child_id) |cid| row.children.getIndex(cid) == edit_index else false; 1052 switch (key) { 1053 .arrow_left => if (on_edit and self.statusButton(index) != null) root_focus.setFocus(row.children.keys()[button_in_row_index]) else try self.focusList(index, root_focus), 1054 .arrow_right => if (!on_edit) root_focus.setFocus(row.children.keys()[edit_index]), 1055 .arrow_up => self.focusHeader(root_focus), 1056 .arrow_down => self.focusTitle(index, root_focus), 1057 .enter => if (!on_edit) try self.toggleIssueStatus(allocator, index), 1058 .mouse => |mouse| switch (mouse.action) { 1059 .scroll => |dir| { 1060 const sc = self.detailScroll(index); 1061 sc.y += if (dir == .up) @as(isize, -1) else 1; 1062 sc.clampToContent(); 1063 }, 1064 else => if (self.statusButton(index)) |button| { 1065 if (inp.leftClickOn(root_focus, button.getFocus().id, mouse)) try self.toggleIssueStatus(allocator, index); 1066 }, 1067 }, 1068 else => {}, 1069 } 1070 } 1071 1072 fn titleInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1073 const sc = self.detailScroll(index); 1074 switch (key) { 1075 .arrow_left => try self.focusList(index, root_focus), 1076 .arrow_up => self.focusToolRow(index, root_focus), 1077 .arrow_down => if (self.authorPresent(index)) self.focusAuthor(index, root_focus) else try self.focusDescription(index, root_focus), 1078 .mouse => |mouse| switch (mouse.action) { 1079 .scroll => |dir| { 1080 sc.y += if (dir == .up) @as(isize, -1) else 1; 1081 sc.clampToContent(); 1082 }, 1083 else => {}, 1084 }, 1085 else => {}, 1086 } 1087 } 1088 1089 // the author box's a: link (when it has one) is followed by the host on 1090 // enter / a click; arrows cross to the neighboring widgets. 1091 fn authorInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1092 const sc = self.detailScroll(index); 1093 switch (key) { 1094 .arrow_left => try self.focusList(index, root_focus), 1095 .arrow_up => self.focusTitle(index, root_focus), 1096 .arrow_down => if (self.tagFlow(index) != null) try self.focusTags(index, root_focus) else try self.focusDescription(index, root_focus), 1097 .mouse => |mouse| switch (mouse.action) { 1098 .scroll => |dir| { 1099 sc.y += if (dir == .up) @as(isize, -1) else 1; 1100 sc.clampToContent(); 1101 }, 1102 else => {}, 1103 }, 1104 else => {}, 1105 } 1106 } 1107 1108 fn descriptionInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1109 const sc = self.detailScroll(index); 1110 switch (key) { 1111 .arrow_left => return self.focusList(index, root_focus), 1112 // once the scroll can't move further, cross into the tags (or the 1113 // open/close button when the issue has none). 1114 .arrow_up => { 1115 const before = sc.y; 1116 sc.y -= 1; 1117 sc.clampToContent(); 1118 if (sc.y == before) { 1119 if (self.tagFlow(index) != null) 1120 try self.focusTags(index, root_focus) 1121 else if (self.authorPresent(index)) 1122 self.focusAuthor(index, root_focus) 1123 else 1124 self.focusTitle(index, root_focus); 1125 } 1126 return; 1127 }, 1128 .arrow_down => sc.y += 1, 1129 .page_up => sc.y -= 10, 1130 .page_down => sc.y += 10, 1131 .home => sc.y = 0, 1132 .end => sc.y = std.math.maxInt(isize), 1133 .mouse => |mouse| switch (mouse.action) { 1134 .scroll => |dir| sc.y += if (dir == .up) @as(isize, -1) else 1, 1135 else => {}, 1136 }, 1137 else => return, 1138 } 1139 sc.clampToContent(); 1140 } 1141 1142 // up/down (and tab/shift+tab) move between a form's fields; up from the 1143 // title crosses into the header tabs. the multiline description keeps 1144 // enter and any up/down that has a row to move to. 1145 fn formInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1146 const form = self.issueForm() orelse return; 1147 const cid = form.getFocus().child_id orelse return; 1148 const cur = form.children.getIndex(cid) orelse return; 1149 1150 if (cur == description_field_index) { 1151 const child = &form.children.values()[cur]; 1152 const description = &child.widget.text_input; 1153 switch (key) { 1154 .enter => return child.widget.input(allocator, key, root_focus), 1155 .arrow_up => if (!try description.cursorOnFirstRow(allocator)) 1156 return child.widget.input(allocator, key, root_focus), 1157 .arrow_down => if (!try description.cursorOnLastRow(allocator)) 1158 return child.widget.input(allocator, key, root_focus), 1159 else => {}, 1160 } 1161 } 1162 1163 switch (key) { 1164 .arrow_up, .back_tab => if (cur > 0) 1165 root_focus.setFocus(form.children.keys()[cur - 1]) 1166 else 1167 self.focusHeader(root_focus), 1168 .arrow_down, .tab => if (cur < submit_field_index) { 1169 root_focus.setFocus(form.children.keys()[cur + 1]); 1170 }, 1171 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 1172 .mouse => |mouse| if (cur == submit_field_index and inp.leftClickOn(root_focus, cid, mouse)) { 1173 try self.submitForm(allocator); 1174 }, 1175 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 1176 } 1177 } 1178 1179 // the terminal submit paths: the new form commits a new-issue event, the 1180 // edit form re-emits the selected issue's event with the form's content. 1181 // the web posts the forms to their routes instead. 1182 fn submitForm(self: *View, allocator: std.mem.Allocator) !void { 1183 if (self.data.view == .edit) { 1184 try self.submitEditedIssue(allocator); 1185 } else { 1186 try self.submitNewIssue(allocator); 1187 } 1188 } 1189 1190 // commit the new issue to the repo's events branch and navigate to it. 1191 // this is the terminal path; the web posts the form to the issue route, 1192 // so the wasm side never runs (or compiles) the repo access below. 1193 fn submitNewIssue(self: *View, allocator: std.mem.Allocator) !void { 1194 if (comptime wasm) return; 1195 const io = self.session.io orelse return; 1196 const src = self.data.repo_source orelse return; 1197 const author_email = (try self.session.eventAuthorEmail()) orelse return; 1198 1199 const form = self.issueForm() orelse return; 1200 const title_input = &form.children.values()[title_field_index].widget.text_input; 1201 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 1202 const description_input = &form.children.values()[description_field_index].widget.text_input; 1203 1204 const title = try title_input.text(allocator); 1205 defer allocator.free(title); 1206 const tags = try tags_input.text(allocator); 1207 defer allocator.free(tags); 1208 const description = try description_input.text(allocator); 1209 defer allocator.free(description); 1210 1211 if (!evt.Issue.fieldsValid(title, tags)) return; 1212 1213 var id_bytes: [evt.event_id_size]u8 = undefined; 1214 io.random(&id_bytes); 1215 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 1216 1217 const event = evt.EventWithId{ 1218 .id = event_id_hex, 1219 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 1220 .author_email = author_email, 1221 .event = .{ .issue = .{ 1222 .title = title, 1223 .description = description, 1224 .tags = tags, 1225 } }, 1226 }; 1227 1228 switch (src.repo_kind) { 1229 inline else => |repo_kind| { 1230 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1231 defer any_repo.deinit(io, allocator); 1232 switch (any_repo) { 1233 inline else => |*repo| try evt.consume(repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 1234 } 1235 }, 1236 } 1237 1238 // wipe the form so a return visit starts fresh 1239 title_input.clear(allocator); 1240 tags_input.clear(allocator); 1241 description_input.clear(allocator); 1242 1243 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, "", &event_id_hex) orelse return; 1244 try self.session.navigate(route); 1245 } 1246 1247 // re-emit the selected issue's event with the form's content (status 1248 // preserved), then reload the issue's page. this is the terminal path; 1249 // the web posts the form to the edit route. 1250 fn submitEditedIssue(self: *View, allocator: std.mem.Allocator) !void { 1251 if (comptime wasm) return; 1252 const io = self.session.io orelse return; 1253 const src = self.data.repo_source orelse return; 1254 const entry = self.data.selectedIssue() orelse return; 1255 const author_email = (try self.session.eventAuthorEmail()) orelse return; 1256 1257 const form = self.issueForm() orelse return; 1258 const title_input = &form.children.values()[title_field_index].widget.text_input; 1259 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 1260 const description_input = &form.children.values()[description_field_index].widget.text_input; 1261 1262 const title = try title_input.text(allocator); 1263 defer allocator.free(title); 1264 const tags = try tags_input.text(allocator); 1265 defer allocator.free(tags); 1266 const description = try description_input.text(allocator); 1267 defer allocator.free(description); 1268 1269 if (!evt.Issue.fieldsValid(title, tags)) return; 1270 1271 var id_bytes: [evt.event_id_size]u8 = undefined; 1272 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 1273 1274 switch (src.repo_kind) { 1275 inline else => |repo_kind| { 1276 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1277 defer any_repo.deinit(io, allocator); 1278 switch (any_repo) { 1279 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 1280 .title = title, 1281 .tags = tags, 1282 .description = description, 1283 } }, author_email), 1284 } 1285 }, 1286 } 1287 1288 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 1289 try self.session.navigate(route); 1290 } 1291 1292 // flip the shown issue's status by re-emitting its event, then reload the 1293 // page rooted at the issue so the view reflects the change. this is the 1294 // terminal path; the web posts the button's form to the status route. 1295 fn toggleIssueStatus(self: *View, allocator: std.mem.Allocator, index: usize) !void { 1296 if (comptime wasm) return; 1297 const io = self.session.io orelse return; 1298 const src = self.data.repo_source orelse return; 1299 const sel = self.detailed_index[index] orelse return; 1300 const entry = self.window(index).issues[sel]; 1301 const author_email = (try self.session.eventAuthorEmail()) orelse return; 1302 1303 const status: evt.Issue.Status = switch (entry.issue.event.status) { 1304 .open => .closed, 1305 .closed => .open, 1306 }; 1307 1308 var id_bytes: [evt.event_id_size]u8 = undefined; 1309 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 1310 1311 switch (src.repo_kind) { 1312 inline else => |repo_kind| { 1313 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1314 defer any_repo.deinit(io, allocator); 1315 switch (any_repo) { 1316 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .status = status }, author_email), 1317 } 1318 }, 1319 } 1320 1321 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, status, "", entry.id) orelse return; 1322 try self.session.navigate(route); 1323 } 1324 1325 // arrow keys move the tag selection; at the flow's edges focus crosses to 1326 // the neighboring widgets. 1327 fn tagsInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1328 const tf = self.tagFlow(index) orelse return; 1329 const cid = tf.focus.child_id orelse return; 1330 const cur = tf.indexOfFocusId(cid) orelse return; 1331 const count = tf.text_boxes.items.len; 1332 const sc = self.detailScroll(index); 1333 switch (key) { 1334 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 1335 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 1336 .arrow_up => if (tf.rowStep(cur, false)) |i| self.focusTag(index, tf, root_focus, i) else self.focusAuthor(index, root_focus), 1337 .arrow_down => if (tf.rowStep(cur, true)) |i| self.focusTag(index, tf, root_focus, i) else try self.focusDescription(index, root_focus), 1338 .home => self.focusTag(index, tf, root_focus, 0), 1339 .end => self.focusTag(index, tf, root_focus, count - 1), 1340 .mouse => |mouse| switch (mouse.action) { 1341 .scroll => |dir| { 1342 sc.y += if (dir == .up) @as(isize, -1) else 1; 1343 sc.clampToContent(); 1344 }, 1345 else => {}, 1346 }, 1347 else => {}, 1348 } 1349 } 1350 1351 const button_in_row_index: usize = 1; 1352 const title_child_index: usize = 0; 1353 const tags_child_index: usize = 2; 1354 1355 fn tagFlow(self: *View, index: usize) ?*ui.TagFlow { 1356 const inner = self.detailInner(index); 1357 if (inner.children.count() <= tags_child_index) return null; 1358 return switch (inner.children.values()[tags_child_index].widget) { 1359 .tag_flow => |*tf| tf, 1360 else => null, 1361 }; 1362 } 1363 1364 fn tagsFocused(self: *View, index: usize) bool { 1365 const inner = self.detailInner(index); 1366 const cid = inner.getFocus().child_id orelse return false; 1367 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 1368 } 1369 1370 // the open/close button inside the detail frame's tool row. the full row 1371 // is spacer + open/close + edit; a logged-out session's row has no 1372 // open/close button, and the description page has no row at all. 1373 fn statusButton(self: *View, index: usize) ?*wgt.TextBox(ui.Widget) { 1374 const row = self.toolRow(index); 1375 if (row.children.count() != 3) return null; 1376 return &row.children.values()[button_in_row_index].widget.text_box; 1377 } 1378 1379 fn toolRowFocused(self: *View, index: usize) bool { 1380 const outer = self.detailOuter(index); 1381 const cid = outer.getFocus().child_id orelse return false; 1382 return outer.children.getIndex(cid) == tool_row_index; 1383 } 1384 1385 fn focusTag(self: *View, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 1386 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 1387 // keep the tag visible on the terminal: its rect offset by the flow's 1388 // position in the pane. 1389 if (self.session.is_terminal and item < tf.rects.items.len) { 1390 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 1391 var rect = tf.rects.items[item]; 1392 rect.x += flow_rect.x; 1393 rect.y += flow_rect.y; 1394 self.detailScroll(index).scrollToRect(rect); 1395 } 1396 } 1397 } 1398 1399 fn titleFocused(self: *View, index: usize) bool { 1400 const inner = self.detailInner(index); 1401 const cid = inner.getFocus().child_id orelse return false; 1402 return inner.children.getIndex(cid) == title_child_index; 1403 } 1404 1405 fn authorPresent(self: *View, index: usize) bool { 1406 return self.author_id[index] != null; 1407 } 1408 1409 fn authorFocused(self: *View, index: usize) bool { 1410 const inner = self.detailInner(index); 1411 const cid = inner.getFocus().child_id orelse return false; 1412 return cid == self.author_id[index]; 1413 } 1414 1415 // the author sits right under the title at the top of the pane, so 1416 // focusing it scrolls there. 1417 fn focusAuthor(self: *View, index: usize, root_focus: *Focus) void { 1418 const id = self.author_id[index] orelse return; 1419 root_focus.setFocus(id); 1420 self.detailScroll(index).y = 0; 1421 } 1422 1423 // the title sits at the top of the pane, so focusing it scrolls there. 1424 fn focusTitle(self: *View, index: usize, root_focus: *Focus) void { 1425 const inner = self.detailInner(index); 1426 if (inner.children.count() == 0) return; 1427 root_focus.setFocus(inner.children.keys()[title_child_index]); 1428 self.detailScroll(index).y = 0; 1429 } 1430 1431 fn focusTags(self: *View, index: usize, root_focus: *Focus) !void { 1432 const tf = self.tagFlow(index) orelse return; 1433 if (tf.text_boxes.items.len == 0) return; 1434 const cid = tf.focus.child_id orelse tf.text_boxes.items[0].getFocus().id; 1435 const item = tf.indexOfFocusId(cid) orelse 0; 1436 self.focusTag(index, tf, root_focus, item); 1437 } 1438 1439 fn focusDescription(self: *View, index: usize, root_focus: *Focus) !void { 1440 if (self.description_id[index]) |id| root_focus.setFocus(id); 1441 } 1442 1443 // return to the tool row's last-focused button (the header when the 1444 // description page shows no row). 1445 fn focusToolRow(self: *View, index: usize, root_focus: *Focus) void { 1446 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 1447 root_focus.setFocus(cid); 1448 } 1449 1450 // enter the detail pane. an empty pane (no issues) can't be entered. 1451 fn focusDetail(self: *View, index: usize, root_focus: *Focus) !void { 1452 if (self.detailInner(index).children.count() == 0) return; 1453 root_focus.setFocus(self.detailOuter(index).getFocus().id); 1454 } 1455 1456 // return to the list. 1457 fn focusList(self: *View, index: usize, root_focus: *Focus) !void { 1458 root_focus.setFocus(self.listScroll(index).getFocus().id); 1459 } 1460 1461 // cross to the header tabs above the stack. 1462 fn focusHeader(self: *View, root_focus: *Focus) void { 1463 root_focus.setFocus(self.box.children.keys()[header_index]); 1464 } 1465 1466 pub fn clearGrid(self: *View) void { 1467 self.box.clearGrid(); 1468 } 1469 1470 pub fn getGrid(self: View) ?Grid { 1471 return self.box.getGrid(); 1472 } 1473 1474 pub fn getFocus(self: *View) *Focus { 1475 return self.box.getFocus(); 1476 } 1477 1478 // for the parent's "scroll up at the top jumps to the header" check: at the 1479 // top only while the header tabs hold focus, so up from the split 1480 // crosses into the tabs first. 1481 pub fn getSelectedIndex(self: *View) ?usize { 1482 return if (self.headerActive()) 0 else 1; 1483 } 1484 }; 1485 1486 // the "a:" navigation link for `status`'s issues page filtered to the 1487 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 1488 // a rooted url ignores `status` (it derives its view from the issue's own); 1489 // with an empty `id` this is the bare list link. 1490 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 1491 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 1492 const url = try route.toUrl(page_arena); 1493 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1494 } 1495 1496 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 1497 // href is only followed with js off. 1498 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 1499 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 1500 const url = try route.toUrl(page_arena); 1501 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 1502 } 1503 1504 // the "a:" link to the whole-description page of issue `id` within `identity`. 1505 fn descriptionLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 1506 const route = ui.RoutablePage.repoIssuesDescriptionRoute(identity, id) orelse return error.RouteTooLong; 1507 const url = try route.toUrl(page_arena); 1508 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1509 } 1510 1511 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 1512 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 1513 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 1514 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 1515 const url = try route.toUrl(page_arena); 1516 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1517 } 1518 1519 // tabs switching between the issues page's views. 1520 pub const Header = struct { 1521 box: wgt.Box(ui.Widget), 1522 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 1523 1524 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 1525 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1526 errdefer box.deinit(allocator); 1527 1528 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 1529 errdefer tab_ids.deinit(allocator); 1530 1531 const aa = session.page_arena.allocator(); 1532 1533 // a list tab per status, labeled with its listing's issue count 1534 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 1535 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 1536 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 1537 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 1538 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1539 errdefer text_box.deinit(allocator); 1540 text_box.getFocus().focusable = true; 1541 text_box.getFocus().kind = .{ .custom = link }; 1542 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1543 try box.children.put(allocator, text_box.getFocus().id, .{ 1544 .widget = .{ .text_box = text_box }, 1545 .rect = null, 1546 .min_size = .{ .width = label.len + 2, .height = null }, 1547 }); 1548 } 1549 1550 // tags tab, labeled with the active tag filter 1551 { 1552 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 1553 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 1554 const label = if (data.tag.len == 0) "tags" else blk: { 1555 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 1556 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 1557 }; 1558 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1559 errdefer text_box.deinit(allocator); 1560 text_box.getFocus().focusable = true; 1561 text_box.getFocus().kind = .{ .custom = tags_link }; 1562 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1563 try box.children.put(allocator, text_box.getFocus().id, .{ 1564 .widget = .{ .text_box = text_box }, 1565 .rect = null, 1566 .min_size = .{ .width = label.len + 2, .height = null }, 1567 }); 1568 } 1569 1570 // new-issue tab; an edit url shows the edit tab in its place 1571 { 1572 const route = if (data.view == .edit) 1573 ui.RoutablePage.repoIssuesEditRoute(data.identity, data.selected_id) orelse return error.RouteTooLong 1574 else 1575 ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong; 1576 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 1577 const label: []const u8 = if (data.view == .edit) "edit" else "new"; 1578 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1579 errdefer text_box.deinit(allocator); 1580 text_box.getFocus().focusable = true; 1581 text_box.getFocus().kind = .{ .custom = link }; 1582 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1583 try box.children.put(allocator, text_box.getFocus().id, .{ 1584 .widget = .{ .text_box = text_box }, 1585 .rect = null, 1586 .min_size = .{ .width = label.len + 2, .height = null }, 1587 }); 1588 } 1589 1590 var self = Header{ .box = box, .tab_ids = tab_ids }; 1591 // the tab matching the page's view is selected initially. 1592 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 1593 return self; 1594 } 1595 1596 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 1597 self.box.deinit(allocator); 1598 self.tab_ids.deinit(allocator); 1599 } 1600 1601 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1602 self.clearGrid(); 1603 // only the selected tab shows its border 1604 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 1605 switch (child.widget) { 1606 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 1607 else => {}, 1608 } 1609 } 1610 try self.box.build(allocator, constraint, root_focus); 1611 } 1612 1613 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1614 _ = allocator; 1615 const current_tab = self.currentTabIndex() orelse return; 1616 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 1617 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 1618 } 1619 } 1620 1621 pub fn clearGrid(self: *Header) void { 1622 self.box.clearGrid(); 1623 } 1624 1625 pub fn getGrid(self: Header) ?Grid { 1626 return self.box.getGrid(); 1627 } 1628 1629 pub fn getFocus(self: *Header) *Focus { 1630 return self.box.getFocus(); 1631 } 1632 1633 pub fn getSelectedIndex(self: Header) ?usize { 1634 return self.currentTabIndex(); 1635 } 1636 1637 fn currentTabIndex(self: Header) ?usize { 1638 const child_id = self.box.focus.child_id orelse return null; 1639 return self.tab_ids.getIndex(child_id); 1640 } 1641 }; 1642