╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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