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