╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1442 .closed => "open", 1443 }; 1444 row.getFocus().kind = .{ .custom = if (self.data.identity.len == 0) 1445 try std.fmt.allocPrint(pa, "form:/issue:{s}/{s}", .{ entry.id, action }) 1446 else 1447 try std.fmt.allocPrint(pa, "form:/repo/{s}/issue:{s}/{s}", .{ self.data.identity, entry.id, action }) }; 1448 1449 var button = try wgt.TextBox(ui.Widget).init(allocator, action, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1450 errdefer button.deinit(allocator); 1451 button.getFocus().focusable = true; 1452 // the renderer distinguishes plain clickables from buttons that 1453 // should POST to a server route by this kind. 1454 button.getFocus().kind = .{ .custom = "submit" }; 1455 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = action.len + 2, .height = null } }); 1456 } 1457 1458 // the edit button links to the issue's edit page. 1459 { 1460 var button = try wgt.TextBox(ui.Widget).init(allocator, "edit", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1461 errdefer button.deinit(allocator); 1462 button.getFocus().focusable = true; 1463 const route = ui.RoutablePage.repoIssuesEditRoute(self.data.identity, entry.id) orelse return error.RouteTooLong; 1464 button.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 1465 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = "edit".len + 2, .height = null } }); 1466 } 1467 } 1468 1469 // the resolve button on a conflicted issue, else the open/close 1470 // button when present, the edit button otherwise 1471 row.getFocus().child_id = row.children.keys()[button_in_row_index]; 1472 } 1473 1474 if (comment_page) |page| { 1475 var back = try Comment.linkBox(allocator, self.session, "← back to issue", ui.RoutablePage.repoIssueCommentsRoute(self.data.identity, entry.id, 0) orelse return error.RouteTooLong); 1476 errdefer back.deinit(allocator); 1477 try inner.children.put(allocator, back.getFocus().id, .{ .widget = .{ .text_box = back }, .rect = null, .min_size = null }); 1478 1479 try Comment.appendComment(allocator, inner, self.session, self.data.identity, page.selected); 1480 try Comment.appendCount(allocator, inner, page.replies.count, "reply", "replies", self.session.page_arena.allocator()); 1481 for (page.replies.comments) |comment| try Comment.appendComment(allocator, inner, self.session, self.data.identity, comment); 1482 try Comment.appendWindowNav(allocator, inner, self.session, self.data.identity, &page.selected.comment.event.thread_id, &page.selected.id, page.replies); 1483 1484 var spacer = try ui.Spacer.init(allocator); 1485 errdefer spacer.deinit(allocator); 1486 try inner.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1487 1488 inner.getFocus().child_id = inner.children.keys()[0]; 1489 const sc = self.detailScroll(index); 1490 sc.x = 0; 1491 sc.y = 0; 1492 sc.getFocus().version +%= 1; 1493 return; 1494 } 1495 1496 if (description_page) { 1497 // the back link, in the title slot so the pane's input handling 1498 // applies to it unchanged. 1499 var tb = try wgt.TextBox(ui.Widget).init(allocator, "← back to issue", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1500 errdefer tb.deinit(allocator); 1501 tb.getFocus().focusable = true; 1502 tb.getFocus().kind = .{ .custom = try issuesLink(self.session.page_arena, self.data.identity, entry.issue.event.status, "", entry.id) }; 1503 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1504 } else { 1505 // the issue's title as a focusable word-wrapped text box. 1506 { 1507 var tb = try wgt.TextBox(ui.Widget).init(allocator, entry.issue.event.title, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = " title " }); 1508 errdefer tb.deinit(allocator); 1509 tb.getFocus().focusable = true; 1510 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1511 } 1512 1513 self.author_id[index] = blk: { 1514 var tb = try ui.authorBox(allocator, self.session.page_arena, entry.author); 1515 errdefer tb.deinit(allocator); 1516 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1517 break :blk tb.getFocus().id; 1518 }; 1519 1520 // the issue's tags, each linking to this status's list filtered to 1521 // that tag. 1522 { 1523 var items: std.ArrayList(ui.TagFlow.Item) = .empty; 1524 defer items.deinit(allocator); 1525 var tag_iter = evt.Issue.tagIterator(entry.issue.event.tags); 1526 while (tag_iter.next()) |tag| { 1527 if (tag.len == 0) continue; 1528 try items.append(allocator, .{ .text = tag, .link = try tagLink(self.session.page_arena, self.data.identity, entry.issue.event.status, tag) }); 1529 } 1530 if (items.items.len > 0) { 1531 var tf = try ui.TagFlow.init(allocator); 1532 errdefer tf.deinit(allocator); 1533 try tf.setItems(allocator, items.items); 1534 try inner.children.put(allocator, tf.getFocus().id, .{ .widget = .{ .tag_flow = tf }, .rect = null, .min_size = null }); 1535 } 1536 } 1537 } 1538 1539 // the description as a focusable word-wrapped text box. past the limit 1540 // it's cut at the last whole line and links to the /description page, 1541 // which shows the whole thing. 1542 self.description_id[index] = blk: { 1543 const whole = entry.issue.event.description; 1544 const cut_short = !description_page and whole.len > max_description_size; 1545 const shown = if (cut_short) 1546 std.mem.trimEnd(u8, whole[0 .. std.mem.lastIndexOfScalar(u8, whole[0..max_description_size], '\n') orelse 0], " \t\r\n") 1547 else if (whole.len == 0) 1548 "(no description)" 1549 else 1550 whole; 1551 var tb = try wgt.TextBox(ui.Widget).init(allocator, shown, .{ 1552 .border_style = .single, 1553 .rounded_corners = true, 1554 .wrap_kind = .word, 1555 .label = " description ", 1556 .bottom_label = if (cut_short) " click or press enter to see more " else "", 1557 }); 1558 errdefer tb.deinit(allocator); 1559 tb.getFocus().focusable = true; 1560 if (cut_short) tb.getFocus().kind = .{ .custom = try descriptionLink(self.session.page_arena, self.data.identity, entry.id) }; 1561 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1562 break :blk tb.getFocus().id; 1563 }; 1564 1565 if (!description_page) { 1566 var reply = try Comment.linkBox(allocator, self.session, "new comment", ui.RoutablePage.repoCommentNewRoute(self.data.identity, entry.id, "") orelse return error.RouteTooLong); 1567 errdefer reply.deinit(allocator); 1568 try inner.children.put(allocator, reply.getFocus().id, .{ .widget = .{ .text_box = reply }, .rect = null, .min_size = null }); 1569 1570 try Comment.appendCount(allocator, inner, entry.comments.count, "comment", "comments", self.session.page_arena.allocator()); 1571 for (entry.comments.comments) |comment| try Comment.appendComment(allocator, inner, self.session, self.data.identity, comment); 1572 try Comment.appendWindowNav(allocator, inner, self.session, self.data.identity, entry.id, null, entry.comments); 1573 1574 var spacer = try ui.Spacer.init(allocator); 1575 errdefer spacer.deinit(allocator); 1576 try inner.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1577 } 1578 1579 // select the title by default 1580 inner.getFocus().child_id = inner.children.keys()[title_child_index]; 1581 1582 // reset the scroll to the top for the newly-shown issue: directly on the 1583 // terminal (the wasm offset), and via a version bump on the web (so the 1584 // renderer's scroll id changes and JS drops the preserved position). 1585 const sc = self.detailScroll(index); 1586 sc.x = 0; 1587 sc.y = 0; 1588 sc.getFocus().version +%= 1; 1589 } 1590 1591 pub fn input(self: *View, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 1592 // wheel input moves focus like the arrows 1593 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 1594 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 1595 else 1596 raw_key; 1597 1598 if (self.headerActive()) { 1599 // down from the tabs re-enters the stack if the selected view has 1600 // something to focus; other keys move the tabs. 1601 if (inp.vertDirection(key) == .down) { 1602 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 1603 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 1604 // nothing focusable in the unauthorized view 1605 .unauthorized => false, 1606 else => true, 1607 } else false; 1608 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 1609 } else { 1610 try self.header().input(allocator, key, root_focus); 1611 } 1612 return; 1613 } 1614 if (self.tagsViewActive()) { 1615 self.tagsViewInput(key, root_focus); 1616 return; 1617 } 1618 if (self.formViewActive()) { 1619 if (self.data.view == .resolve) { 1620 try self.resolveInput(allocator, key, root_focus); 1621 } else if (self.data.view == .new_comment or self.data.view == .edit_comment) { 1622 try self.commentFormInput(allocator, key, root_focus); 1623 } else { 1624 try self.formInput(allocator, key, root_focus); 1625 } 1626 return; 1627 } 1628 const i = self.selectedSplitIndex() orelse return; 1629 if (self.detailActive(i)) { 1630 try self.detailInput(allocator, i, key, root_focus); 1631 } else { 1632 try self.listInput(i, key, root_focus); 1633 } 1634 } 1635 1636 // arrow keys move the tag selection; up from the top row crosses to the 1637 // header tabs. 1638 fn tagsViewInput(self: *View, key: Key, root_focus: *Focus) void { 1639 const tf = self.tagsView(); 1640 const cid = tf.focus.child_id orelse return; 1641 const cur = tf.indexOfFocusId(cid) orelse return; 1642 const count = tf.text_boxes.items.len; 1643 switch (key) { 1644 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 1645 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 1646 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 1647 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 1648 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 1649 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 1650 else => {}, 1651 } 1652 } 1653 1654 fn listInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1655 // up/down (and the scroll wheel) move the selection a row; page up/down 1656 // jump a fixed amount. right/Enter cross into the detail pane. up from 1657 // the top row crosses into the header tabs. 1658 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 1659 const lb = self.listBox(index); 1660 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 1661 if (delta < 0 and at_top) return self.focusHeader(root_focus); 1662 ui.moveRowFocus(lb, self.listScroll(index), root_focus, delta); 1663 return; 1664 } 1665 switch (key) { 1666 .enter, .arrow_right => try self.focusDetail(index, root_focus), 1667 else => {}, 1668 } 1669 } 1670 1671 fn detailInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1672 if (self.data.comment_page != null and std.mem.eql(u8, self.window(index).issues[self.detailed_index[index] orelse return].id, self.data.selected_id)) { 1673 try self.commentInput(index, key, root_focus); 1674 } else if (self.toolRowFocused(index)) { 1675 try self.toolRowInput(allocator, index, key, root_focus); 1676 } else if (self.titleFocused(index)) { 1677 try self.titleInput(index, key, root_focus); 1678 } else if (self.authorFocused(index)) { 1679 try self.authorInput(index, key, root_focus); 1680 } else if (self.tagsFocused(index)) { 1681 try self.tagsInput(index, key, root_focus); 1682 } else if (self.descriptionFocused(index)) { 1683 try self.descriptionInput(index, key, root_focus); 1684 } else { 1685 try self.commentInput(index, key, root_focus); 1686 } 1687 } 1688 1689 // the tool row: enter or a click on the open/close button flips the 1690 // issue's status; the edit and resolve buttons are a: links the host 1691 // follows. arrows cross between the buttons and to the neighboring 1692 // widgets. 1693 fn toolRowInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1694 const row = self.toolRow(index); 1695 const cur = if (row.getFocus().child_id) |cid| row.children.getIndex(cid) orelse return else return; 1696 const on_status = cur == button_in_row_index and self.statusButton(index) != null; 1697 switch (key) { 1698 .arrow_left => if (cur > button_in_row_index) root_focus.setFocus(row.children.keys()[cur - 1]) else try self.focusList(index, root_focus), 1699 .arrow_right => if (cur + 1 < row.children.count()) root_focus.setFocus(row.children.keys()[cur + 1]), 1700 .arrow_up => self.focusHeader(root_focus), 1701 .arrow_down => self.focusTitle(index, root_focus), 1702 .enter => if (on_status) try self.toggleIssueStatus(allocator, index), 1703 .mouse => |mouse| if (self.statusButton(index)) |button| { 1704 if (inp.leftClickOn(root_focus, button.getFocus().id, mouse)) try self.toggleIssueStatus(allocator, index); 1705 }, 1706 else => {}, 1707 } 1708 } 1709 1710 fn titleInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1711 switch (key) { 1712 .arrow_left => try self.focusList(index, root_focus), 1713 .arrow_up => self.focusToolRow(index, root_focus), 1714 .arrow_down => if (self.authorPresent(index)) self.focusAuthor(index, root_focus) else try self.focusDescription(index, root_focus), 1715 else => {}, 1716 } 1717 } 1718 1719 // the author box's a: link (when it has one) is followed by the host on 1720 // enter / a click; arrows cross to the neighboring widgets. 1721 fn authorInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1722 switch (key) { 1723 .arrow_left => try self.focusList(index, root_focus), 1724 .arrow_up => self.focusTitle(index, root_focus), 1725 .arrow_down => if (self.tagFlow(index) != null) try self.focusTags(index, root_focus) else try self.focusDescription(index, root_focus), 1726 else => {}, 1727 } 1728 } 1729 1730 fn descriptionInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1731 const sc = self.detailScroll(index); 1732 switch (key) { 1733 .arrow_left => return self.focusList(index, root_focus), 1734 // once the scroll can't move further, cross into the tags (or the 1735 // open/close button when the issue has none). 1736 .arrow_up => { 1737 const before = sc.y; 1738 sc.y -= 1; 1739 sc.clampToContent(); 1740 if (sc.y == before) { 1741 if (self.tagFlow(index) != null) 1742 try self.focusTags(index, root_focus) 1743 else if (self.authorPresent(index)) 1744 self.focusAuthor(index, root_focus) 1745 else 1746 self.focusTitle(index, root_focus); 1747 } 1748 return; 1749 }, 1750 .arrow_down => { 1751 if (self.moveCommentVertical(index, root_focus, true)) return; 1752 const before = sc.y; 1753 sc.y += 1; 1754 sc.clampToContent(); 1755 if (sc.y == before) _ = self.moveCommentVertical(index, root_focus, true); 1756 return; 1757 }, 1758 .page_up => sc.y -= 10, 1759 .page_down => sc.y += 10, 1760 .home => sc.y = 0, 1761 .end => sc.y = std.math.maxInt(isize), 1762 else => return, 1763 } 1764 sc.clampToContent(); 1765 } 1766 1767 fn commentInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1768 switch (key) { 1769 .arrow_left => if (!self.moveCommentHorizontal(index, root_focus, false)) return self.focusList(index, root_focus), 1770 .arrow_right => _ = self.moveCommentHorizontal(index, root_focus, true), 1771 .arrow_up => _ = self.moveCommentVertical(index, root_focus, false), 1772 .arrow_down => _ = self.moveCommentVertical(index, root_focus, true), 1773 .page_up => self.detailScroll(index).y -= 10, 1774 .page_down => self.detailScroll(index).y += 10, 1775 .home => self.detailScroll(index).y = 0, 1776 .end => self.detailScroll(index).y = std.math.maxInt(isize), 1777 else => return, 1778 } 1779 self.detailScroll(index).clampToContent(); 1780 } 1781 1782 fn focusedDetailChild(self: *View, index: usize, root_focus: *Focus) ?usize { 1783 const inner = self.detailInner(index); 1784 var id = root_focus.grandchild_id orelse return null; 1785 while (inner.getFocus().children.get(id)) |child| { 1786 if (child.parent_id == inner.getFocus().id) return inner.children.getIndex(id); 1787 id = child.parent_id; 1788 } 1789 return null; 1790 } 1791 1792 fn detailChildFocusable(self: *View, index: usize, child_index: usize) bool { 1793 const child = &self.detailInner(index).children.values()[child_index].widget; 1794 return switch (child.*) { 1795 .repo_comment => true, 1796 .box => |*box| for (box.children.values()) |*item| { 1797 if (item.widget.getFocus().focusable) break true; 1798 } else false, 1799 else => child.getFocus().focusable, 1800 }; 1801 } 1802 1803 fn focusDetailChild(self: *View, index: usize, child_index: usize, last: bool, root_focus: *Focus) void { 1804 const inner = self.detailInner(index); 1805 const child = &inner.children.values()[child_index]; 1806 var target = inner.children.keys()[child_index]; 1807 var rect = child.rect orelse return; 1808 switch (child.widget) { 1809 .repo_comment => |*comment| { 1810 if (comment.rowRect(last)) |row_rect| { 1811 rect.x += row_rect.x; 1812 rect.y += row_rect.y; 1813 rect.size = row_rect.size; 1814 } 1815 if (last) comment.focusBody(root_focus) else comment.focusMetadata(root_focus); 1816 }, 1817 .box => |*box| if (box.children.count() > 0) { 1818 target = box.children.keys()[if (last) box.children.count() - 1 else 0]; 1819 root_focus.setFocus(target); 1820 }, 1821 else => root_focus.setFocus(target), 1822 } 1823 self.detailScroll(index).scrollToRect(rect); 1824 } 1825 1826 fn moveCommentVertical(self: *View, index: usize, root_focus: *Focus, down: bool) bool { 1827 const inner = self.detailInner(index); 1828 const current = self.focusedDetailChild(index, root_focus) orelse return false; 1829 if (inner.children.values()[current].widget == .repo_comment) { 1830 const comment = &inner.children.values()[current].widget.repo_comment; 1831 if (down and !comment.bodyFocused()) { 1832 self.focusDetailChild(index, current, true, root_focus); 1833 return true; 1834 } 1835 if (!down and comment.bodyFocused()) { 1836 self.focusDetailChild(index, current, false, root_focus); 1837 return true; 1838 } 1839 } 1840 1841 var next = current; 1842 while (true) { 1843 if (down) { 1844 next += 1; 1845 if (next >= inner.children.count()) return false; 1846 } else { 1847 if (next == 0) return false; 1848 next -= 1; 1849 } 1850 if (!self.detailChildFocusable(index, next)) continue; 1851 self.focusDetailChild(index, next, !down, root_focus); 1852 return true; 1853 } 1854 } 1855 1856 fn moveCommentHorizontal(self: *View, index: usize, root_focus: *Focus, right: bool) bool { 1857 const inner = self.detailInner(index); 1858 const current = self.focusedDetailChild(index, root_focus) orelse return false; 1859 const child = &inner.children.values()[current]; 1860 if (child.widget == .repo_comment) { 1861 const moved = child.widget.repo_comment.moveHorizontal(root_focus, right); 1862 if (moved) if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 1863 return moved; 1864 } 1865 const row = switch (child.widget) { 1866 .box => |*box| box, 1867 else => return false, 1868 }; 1869 const selected = row.getFocus().child_id orelse return false; 1870 const selected_index = row.children.getIndex(selected) orelse return false; 1871 const target = if (right) selected_index + 1 else selected_index -| 1; 1872 if (target == selected_index or target >= row.children.count()) return false; 1873 root_focus.setFocus(row.children.keys()[target]); 1874 if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 1875 return true; 1876 } 1877 1878 // up/down (and tab/shift+tab) move between a form's fields; up from the 1879 // title crosses into the header tabs. the multiline description keeps 1880 // enter and any up/down that has a row to move to. 1881 fn formInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1882 const form = self.issueForm() orelse return; 1883 const cid = form.getFocus().child_id orelse return; 1884 const cur = form.children.getIndex(cid) orelse return; 1885 1886 if (cur == description_field_index) { 1887 const child = &form.children.values()[cur]; 1888 if (try multilineKeepsKey(&child.widget.text_input, allocator, key)) 1889 return child.widget.input(allocator, key, root_focus); 1890 } 1891 1892 switch (key) { 1893 .arrow_up, .back_tab => if (cur > 0) 1894 root_focus.setFocus(form.children.keys()[cur - 1]) 1895 else 1896 self.focusHeader(root_focus), 1897 .arrow_down, .tab => if (cur < submit_field_index) { 1898 root_focus.setFocus(form.children.keys()[cur + 1]); 1899 }, 1900 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 1901 .mouse => |mouse| if (cur == submit_field_index) { 1902 const submit = &form.children.values()[cur].widget.submit_button; 1903 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitForm(allocator); 1904 }, 1905 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 1906 } 1907 } 1908 1909 fn commentFormInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1910 const form = self.issueForm() orelse return; 1911 const cid = form.getFocus().child_id orelse return; 1912 const cur = form.children.getIndex(cid) orelse return; 1913 const child = &form.children.values()[cur]; 1914 1915 if (cur == comment_body_field_index and try multilineKeepsKey(&child.widget.text_input, allocator, key)) 1916 return child.widget.input(allocator, key, root_focus); 1917 1918 switch (key) { 1919 .arrow_up, .back_tab => if (cur > comment_author_field_index) 1920 root_focus.setFocus(form.children.keys()[cur - 1]) 1921 else 1922 self.focusHeader(root_focus), 1923 .arrow_down, .tab => if (cur < comment_submit_field_index) 1924 root_focus.setFocus(form.children.keys()[cur + 1]), 1925 .enter => if (cur == comment_submit_field_index) try self.submitComment(allocator), 1926 .mouse => |mouse| if (cur == comment_submit_field_index) { 1927 const submit = &child.widget.submit_button; 1928 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitComment(allocator); 1929 }, 1930 else => try child.widget.input(allocator, key, root_focus), 1931 } 1932 } 1933 1934 // the multiline hunk inputs keep enter and any up/down that has a row to 1935 // move to; enter on a "use this" link is a navigation the host follows 1936 // before this runs. 1937 fn resolveInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1938 const form = self.issueForm() orelse return; 1939 const cid = form.getFocus().child_id orelse return; 1940 const cur = form.children.getIndex(cid) orelse return; 1941 const child = &form.children.values()[cur]; 1942 1943 if (child.widget == .text_input) { 1944 const ti = &child.widget.text_input; 1945 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 1946 return child.widget.input(allocator, key, root_focus); 1947 } 1948 1949 const on_submit = child.widget == .submit_button; 1950 1951 switch (key) { 1952 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 1953 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 1954 .arrow_left, .arrow_right => switch (child.widget) { 1955 .box => |*row| { 1956 const row_cid = row.getFocus().child_id orelse return; 1957 const row_index = row.children.getIndex(row_cid) orelse return; 1958 if (key == .arrow_left) { 1959 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 1960 } else if (row_index + 1 < row.children.count()) { 1961 root_focus.setFocus(row.children.keys()[row_index + 1]); 1962 } 1963 }, 1964 else => try child.widget.input(allocator, key, root_focus), 1965 }, 1966 .enter => if (on_submit) try self.submitResolution(allocator), 1967 .mouse => |mouse| if (on_submit) { 1968 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 1969 }, 1970 else => try child.widget.input(allocator, key, root_focus), 1971 } 1972 } 1973 1974 // a multiline input keeps enter and any up/down that has a row to move to 1975 fn multilineKeepsKey(text_input: *wgt.TextInput(ui.Widget), allocator: std.mem.Allocator, key: Key) !bool { 1976 return switch (key) { 1977 .enter => true, 1978 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 1979 .arrow_down => !try text_input.cursorOnLastRow(allocator), 1980 else => false, 1981 }; 1982 } 1983 1984 // the neighboring focusable resolve-form child, skipping the spacer and 1985 // the blank gap rows 1986 fn resolveStep(form: *wgt.Box(ui.Widget), cur: usize, down: bool) ?usize { 1987 var i = cur; 1988 while (true) { 1989 if (down) { 1990 i += 1; 1991 if (i >= form.children.count()) return null; 1992 } else { 1993 if (i == 0) return null; 1994 i -= 1; 1995 } 1996 switch (form.children.values()[i].widget) { 1997 .spacer, .text => continue, 1998 else => return i, 1999 } 2000 } 2001 } 2002 2003 // focus the resolve-form child at `i` (a version row focuses its selected 2004 // child) and keep it visible on the terminal 2005 fn focusResolveChild(self: *View, form: *wgt.Box(ui.Widget), i: usize, root_focus: *Focus) void { 2006 const child = &form.children.values()[i]; 2007 switch (child.widget) { 2008 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2009 else => root_focus.setFocus(form.children.keys()[i]), 2010 } 2011 if (self.session.is_terminal) { 2012 if (self.resolveScroll()) |sc| { 2013 if (child.rect) |rect| sc.scrollToRect(rect); 2014 } 2015 } 2016 } 2017 2018 // re-emit the conflicted issue's event with the resolved content; any 2019 // event on the issue settles its conflict entry. this is the terminal 2020 // path; the web posts the form to the resolve route. 2021 fn submitResolution(self: *View, allocator: std.mem.Allocator) !void { 2022 if (comptime wasm) return; 2023 const io = self.session.io orelse return; 2024 const src = self.data.repo_source orelse return; 2025 const entry = self.data.selectedIssue() orelse return; 2026 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2027 const form = self.issueForm() orelse return; 2028 2029 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2030 var title: ?[]u8 = null; 2031 var tags: ?[]u8 = null; 2032 var hunks: std.ArrayList([]const u8) = .empty; 2033 defer { 2034 if (title) |t| allocator.free(t); 2035 if (tags) |t| allocator.free(t); 2036 for (hunks.items) |h| allocator.free(h); 2037 hunks.deinit(allocator); 2038 } 2039 for (form.children.values()) |*child| switch (child.widget) { 2040 .text_input => |*ti| { 2041 const text = try ti.text(allocator); 2042 errdefer allocator.free(text); 2043 if (std.mem.eql(u8, ti.options.name, "title")) { 2044 title = text; 2045 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2046 tags = text; 2047 } else { 2048 try hunks.append(allocator, text); 2049 } 2050 }, 2051 else => {}, 2052 }; 2053 2054 var id_bytes: [evt.event_id_size]u8 = undefined; 2055 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2056 2057 switch (src.repo_kind) { 2058 inline else => |repo_kind| { 2059 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2060 defer any_repo.deinit(io, allocator); 2061 switch (any_repo) { 2062 inline else => |*repo| evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .resolve = .{ 2063 .title = title, 2064 .tags = tags, 2065 .hunks = hunks.items, 2066 } }, author_email) catch |err| switch (err) { 2067 // leave the form up for correction 2068 error.InvalidFields => return, 2069 else => |e| return e, 2070 }, 2071 } 2072 }, 2073 } 2074 2075 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 2076 try self.session.navigate(route); 2077 } 2078 2079 // commit a new or edited comment and navigate to its permalink. this is the 2080 // terminal path; the web posts the form to its comment route. 2081 fn submitComment(self: *View, allocator: std.mem.Allocator) !void { 2082 if (comptime wasm) return; 2083 const io = self.session.io orelse return; 2084 const src = self.data.repo_source orelse return; 2085 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2086 const form = self.issueForm() orelse return; 2087 const body_input = &form.children.values()[comment_body_field_index].widget.text_input; 2088 const body = try body_input.text(allocator); 2089 defer allocator.free(body); 2090 if (!evt.Comment.fieldsValid(body)) return; 2091 2092 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2093 @memcpy(&thread_id, self.data.selected_id); 2094 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2095 var parent: [evt.event_id_size * 2]u8 = undefined; 2096 @memcpy(&parent, self.data.comment_id); 2097 break :blk parent; 2098 }; 2099 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2100 const editing = self.data.view == .edit_comment; 2101 switch (src.repo_kind) { 2102 inline else => |repo_kind| { 2103 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2104 defer any_repo.deinit(io, allocator); 2105 switch (any_repo) { 2106 inline else => |*repo| if (editing) { 2107 var comment_id: [evt.event_id_size]u8 = undefined; 2108 _ = std.fmt.hexToBytes(&comment_id, self.data.comment_id) catch return; 2109 try evt.Comment.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author_email); 2110 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2111 } else { 2112 event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author_email); 2113 }, 2114 } 2115 }, 2116 } 2117 2118 body_input.clear(allocator); 2119 const route = ui.RoutablePage.repoCommentsRoute(self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2120 try self.session.navigate(route); 2121 } 2122 2123 // the terminal submit paths: the new form commits a new-issue event, the 2124 // edit form re-emits the selected issue's event with the form's content. 2125 // the web posts the forms to their routes instead. 2126 fn submitForm(self: *View, allocator: std.mem.Allocator) !void { 2127 if (self.data.view == .edit) { 2128 try self.submitEditedIssue(allocator); 2129 } else { 2130 try self.submitNewIssue(allocator); 2131 } 2132 } 2133 2134 // commit the new issue to the repo's events branch and navigate to it. 2135 // this is the terminal path; the web posts the form to the issue route, 2136 // so the wasm side never runs (or compiles) the repo access below. 2137 fn submitNewIssue(self: *View, allocator: std.mem.Allocator) !void { 2138 if (comptime wasm) return; 2139 const io = self.session.io orelse return; 2140 const src = self.data.repo_source orelse return; 2141 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2142 2143 const form = self.issueForm() orelse return; 2144 const title_input = &form.children.values()[title_field_index].widget.text_input; 2145 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2146 const description_input = &form.children.values()[description_field_index].widget.text_input; 2147 2148 const title = try title_input.text(allocator); 2149 defer allocator.free(title); 2150 const tags = try tags_input.text(allocator); 2151 defer allocator.free(tags); 2152 const description = try description_input.text(allocator); 2153 defer allocator.free(description); 2154 2155 if (!evt.Issue.fieldsValid(title, tags)) return; 2156 2157 var id_bytes: [evt.event_id_size]u8 = undefined; 2158 io.random(&id_bytes); 2159 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2160 2161 const event = evt.EventWithId{ 2162 .id = event_id_hex, 2163 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2164 .author_email = author_email, 2165 .event = .{ .issue = .{ 2166 .title = title, 2167 .description = description, 2168 .tags = tags, 2169 } }, 2170 }; 2171 2172 switch (src.repo_kind) { 2173 inline else => |repo_kind| { 2174 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2175 defer any_repo.deinit(io, allocator); 2176 switch (any_repo) { 2177 inline else => |*repo| try evt.consume(repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2178 } 2179 }, 2180 } 2181 2182 // wipe the form so a return visit starts fresh 2183 title_input.clear(allocator); 2184 tags_input.clear(allocator); 2185 description_input.clear(allocator); 2186 2187 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, "", &event_id_hex) orelse return; 2188 try self.session.navigate(route); 2189 } 2190 2191 // re-emit the selected issue's event with the form's content (status 2192 // preserved), then reload the issue's page. this is the terminal path; 2193 // the web posts the form to the edit route. 2194 fn submitEditedIssue(self: *View, allocator: std.mem.Allocator) !void { 2195 if (comptime wasm) return; 2196 const io = self.session.io orelse return; 2197 const src = self.data.repo_source orelse return; 2198 const entry = self.data.selectedIssue() orelse return; 2199 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2200 2201 const form = self.issueForm() orelse return; 2202 const title_input = &form.children.values()[title_field_index].widget.text_input; 2203 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2204 const description_input = &form.children.values()[description_field_index].widget.text_input; 2205 2206 const title = try title_input.text(allocator); 2207 defer allocator.free(title); 2208 const tags = try tags_input.text(allocator); 2209 defer allocator.free(tags); 2210 const description = try description_input.text(allocator); 2211 defer allocator.free(description); 2212 2213 if (!evt.Issue.fieldsValid(title, tags)) return; 2214 2215 var id_bytes: [evt.event_id_size]u8 = undefined; 2216 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2217 2218 switch (src.repo_kind) { 2219 inline else => |repo_kind| { 2220 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2221 defer any_repo.deinit(io, allocator); 2222 switch (any_repo) { 2223 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2224 .title = title, 2225 .tags = tags, 2226 .description = description, 2227 } }, author_email), 2228 } 2229 }, 2230 } 2231 2232 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 2233 try self.session.navigate(route); 2234 } 2235 2236 // flip the shown issue's status by re-emitting its event, then reload the 2237 // page rooted at the issue so the view reflects the change. this is the 2238 // terminal path; the web posts the button's form to the status route. 2239 fn toggleIssueStatus(self: *View, allocator: std.mem.Allocator, index: usize) !void { 2240 if (comptime wasm) return; 2241 const io = self.session.io orelse return; 2242 const src = self.data.repo_source orelse return; 2243 const sel = self.detailed_index[index] orelse return; 2244 const entry = self.window(index).issues[sel]; 2245 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2246 2247 const status: evt.Issue.Status = switch (entry.issue.event.status) { 2248 .open => .closed, 2249 .closed => .open, 2250 }; 2251 2252 var id_bytes: [evt.event_id_size]u8 = undefined; 2253 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2254 2255 switch (src.repo_kind) { 2256 inline else => |repo_kind| { 2257 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2258 defer any_repo.deinit(io, allocator); 2259 switch (any_repo) { 2260 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .status = status }, author_email), 2261 } 2262 }, 2263 } 2264 2265 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, status, "", entry.id) orelse return; 2266 try self.session.navigate(route); 2267 } 2268 2269 // arrow keys move the tag selection; at the flow's edges focus crosses to 2270 // the neighboring widgets. 2271 fn tagsInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 2272 const tf = self.tagFlow(index) orelse return; 2273 const cid = tf.focus.child_id orelse return; 2274 const cur = tf.indexOfFocusId(cid) orelse return; 2275 const count = tf.text_boxes.items.len; 2276 switch (key) { 2277 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 2278 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 2279 .arrow_up => if (tf.rowStep(cur, false)) |i| self.focusTag(index, tf, root_focus, i) else self.focusAuthor(index, root_focus), 2280 .arrow_down => if (tf.rowStep(cur, true)) |i| self.focusTag(index, tf, root_focus, i) else try self.focusDescription(index, root_focus), 2281 .home => self.focusTag(index, tf, root_focus, 0), 2282 .end => self.focusTag(index, tf, root_focus, count - 1), 2283 else => {}, 2284 } 2285 } 2286 2287 const button_in_row_index: usize = 1; 2288 const title_child_index: usize = 0; 2289 const tags_child_index: usize = 2; 2290 2291 fn tagFlow(self: *View, index: usize) ?*ui.TagFlow { 2292 const inner = self.detailInner(index); 2293 if (inner.children.count() <= tags_child_index) return null; 2294 return switch (inner.children.values()[tags_child_index].widget) { 2295 .tag_flow => |*tf| tf, 2296 else => null, 2297 }; 2298 } 2299 2300 fn tagsFocused(self: *View, index: usize) bool { 2301 const inner = self.detailInner(index); 2302 const cid = inner.getFocus().child_id orelse return false; 2303 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 2304 } 2305 2306 // the open/close button inside the detail frame's tool row, identified 2307 // by its posting kind; a logged-out session's or conflicted issue's row 2308 // has none. 2309 fn statusButton(self: *View, index: usize) ?*wgt.TextBox(ui.Widget) { 2310 const row = self.toolRow(index); 2311 if (row.children.count() <= button_in_row_index) return null; 2312 switch (row.children.values()[button_in_row_index].widget) { 2313 .text_box => |*tb| switch (tb.box.focus.kind) { 2314 .custom => |custom| if (std.mem.eql(u8, custom, "submit")) return tb, 2315 else => {}, 2316 }, 2317 else => {}, 2318 } 2319 return null; 2320 } 2321 2322 fn toolRowFocused(self: *View, index: usize) bool { 2323 const outer = self.detailOuter(index); 2324 const cid = outer.getFocus().child_id orelse return false; 2325 return outer.children.getIndex(cid) == tool_row_index; 2326 } 2327 2328 fn focusTag(self: *View, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 2329 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 2330 // keep the tag visible on the terminal: its rect offset by the flow's 2331 // position in the pane. 2332 if (self.session.is_terminal and item < tf.rects.items.len) { 2333 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 2334 var rect = tf.rects.items[item]; 2335 rect.x += flow_rect.x; 2336 rect.y += flow_rect.y; 2337 self.detailScroll(index).scrollToRect(rect); 2338 } 2339 } 2340 } 2341 2342 fn titleFocused(self: *View, index: usize) bool { 2343 const inner = self.detailInner(index); 2344 const cid = inner.getFocus().child_id orelse return false; 2345 return inner.children.getIndex(cid) == title_child_index; 2346 } 2347 2348 fn authorPresent(self: *View, index: usize) bool { 2349 return self.author_id[index] != null; 2350 } 2351 2352 fn authorFocused(self: *View, index: usize) bool { 2353 const inner = self.detailInner(index); 2354 const cid = inner.getFocus().child_id orelse return false; 2355 return cid == self.author_id[index]; 2356 } 2357 2358 fn descriptionFocused(self: *View, index: usize) bool { 2359 const cid = self.detailInner(index).getFocus().child_id orelse return false; 2360 return cid == self.description_id[index]; 2361 } 2362 2363 // the author sits right under the title at the top of the pane, so 2364 // focusing it scrolls there. 2365 fn focusAuthor(self: *View, index: usize, root_focus: *Focus) void { 2366 const id = self.author_id[index] orelse return; 2367 root_focus.setFocus(id); 2368 self.detailScroll(index).y = 0; 2369 } 2370 2371 // the title sits at the top of the pane, so focusing it scrolls there. 2372 fn focusTitle(self: *View, index: usize, root_focus: *Focus) void { 2373 const inner = self.detailInner(index); 2374 if (inner.children.count() == 0) return; 2375 root_focus.setFocus(inner.children.keys()[title_child_index]); 2376 self.detailScroll(index).y = 0; 2377 } 2378 2379 fn focusTags(self: *View, index: usize, root_focus: *Focus) !void { 2380 const tf = self.tagFlow(index) orelse return; 2381 if (tf.text_boxes.items.len == 0) return; 2382 const cid = tf.focus.child_id orelse tf.text_boxes.items[0].getFocus().id; 2383 const item = tf.indexOfFocusId(cid) orelse 0; 2384 self.focusTag(index, tf, root_focus, item); 2385 } 2386 2387 fn focusDescription(self: *View, index: usize, root_focus: *Focus) !void { 2388 if (self.description_id[index]) |id| root_focus.setFocus(id); 2389 } 2390 2391 // return to the tool row's last-focused button (the header when the 2392 // description page shows no row). 2393 fn focusToolRow(self: *View, index: usize, root_focus: *Focus) void { 2394 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 2395 root_focus.setFocus(cid); 2396 } 2397 2398 // enter the detail pane. an empty pane (no issues) can't be entered. 2399 fn focusDetail(self: *View, index: usize, root_focus: *Focus) !void { 2400 if (self.detailInner(index).children.count() == 0) return; 2401 root_focus.setFocus(self.detailOuter(index).getFocus().id); 2402 } 2403 2404 // return to the list. 2405 fn focusList(self: *View, index: usize, root_focus: *Focus) !void { 2406 root_focus.setFocus(self.listScroll(index).getFocus().id); 2407 } 2408 2409 // cross to the header tabs above the stack. 2410 fn focusHeader(self: *View, root_focus: *Focus) void { 2411 root_focus.setFocus(self.box.children.keys()[header_index]); 2412 } 2413 2414 pub fn clearGrid(self: *View) void { 2415 self.box.clearGrid(); 2416 } 2417 2418 pub fn getGrid(self: View) ?Grid { 2419 return self.box.getGrid(); 2420 } 2421 2422 pub fn getFocus(self: *View) *Focus { 2423 return self.box.getFocus(); 2424 } 2425 2426 // for the parent's "scroll up at the top jumps to the header" check: at the 2427 // top only while the header tabs hold focus, so up from the split 2428 // crosses into the tabs first. 2429 pub fn getSelectedIndex(self: *View) ?usize { 2430 return if (self.headerActive()) 0 else 1; 2431 } 2432 }; 2433 2434 // the "a:" navigation link for `status`'s issues page filtered to the 2435 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 2436 // a rooted url ignores `status` (it derives its view from the issue's own); 2437 // with an empty `id` this is the bare list link. 2438 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 2439 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 2440 const url = try route.toUrl(page_arena); 2441 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2442 } 2443 2444 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 2445 // href is only followed with js off. 2446 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2447 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 2448 const url = try route.toUrl(page_arena); 2449 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 2450 } 2451 2452 // the "a:" link to the window `status_maybe` names, keeping the tag filter, 2453 // or the conflicts window when null, rooted at issue `id`. 2454 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?evt.Issue.Status, id: []const u8) ![]const u8 { 2455 if (status_maybe) |status| return issuesLink(page_arena, data.identity, status, data.tag, id); 2456 return conflictsLink(page_arena, data.identity, id); 2457 } 2458 2459 // the "a:" link to the conflicts list rooted at issue `start` ("" = the 2460 // first window). 2461 fn conflictsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, start: []const u8) ![]const u8 { 2462 const route = ui.RoutablePage.repoIssuesConflictsRoute(identity, start) orelse return error.RouteTooLong; 2463 const url = try route.toUrl(page_arena); 2464 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2465 } 2466 2467 // the "a:" link to the whole-description page of issue `id` within `identity`. 2468 fn descriptionLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2469 const route = ui.RoutablePage.repoIssuesDescriptionRoute(identity, id) orelse return error.RouteTooLong; 2470 const url = try route.toUrl(page_arena); 2471 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2472 } 2473 2474 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 2475 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 2476 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 2477 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 2478 const url = try route.toUrl(page_arena); 2479 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2480 } 2481 2482 // tabs switching between the issues page's views. 2483 pub const Header = struct { 2484 box: wgt.Box(ui.Widget), 2485 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 2486 2487 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 2488 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2489 errdefer box.deinit(allocator); 2490 2491 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 2492 errdefer tab_ids.deinit(allocator); 2493 2494 const aa = session.page_arena.allocator(); 2495 2496 // a list tab per status, labeled with its listing's issue count 2497 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 2498 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 2499 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2500 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 2501 try addTab(allocator, &box, &tab_ids, label, link); 2502 } 2503 2504 // tags tab, labeled with the active tag filter 2505 { 2506 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 2507 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 2508 const label = if (data.tag.len == 0) "tags" else blk: { 2509 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 2510 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 2511 }; 2512 try addTab(allocator, &box, &tab_ids, label, tags_link); 2513 } 2514 2515 // new-issue tab; an edit or resolve url shows its tab in this place 2516 { 2517 const route = switch (data.view) { 2518 .edit => ui.RoutablePage.repoIssuesEditRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 2519 .new_comment => ui.RoutablePage.repoCommentNewRoute(data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 2520 .edit_comment => ui.RoutablePage.repoCommentEditRoute(data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 2521 .resolve => ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 2522 else => ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong, 2523 }; 2524 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2525 const label: []const u8 = switch (data.view) { 2526 .edit => "edit", 2527 .new_comment => "reply", 2528 .edit_comment => "edit", 2529 .resolve => "resolve", 2530 else => "new", 2531 }; 2532 try addTab(allocator, &box, &tab_ids, label, link); 2533 } 2534 2535 // conflicts tab, labeled with the conflict count 2536 if (data.conflicts.count > 0) { 2537 const route = ui.RoutablePage.repoIssuesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 2538 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2539 const label = try std.fmt.allocPrint(aa, "conflicts ({d})", .{data.conflicts.count}); 2540 try addTab(allocator, &box, &tab_ids, label, link); 2541 } 2542 2543 var self = Header{ .box = box, .tab_ids = tab_ids }; 2544 // the tab matching the page's view is selected initially. 2545 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 2546 return self; 2547 } 2548 2549 fn addTab(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), tab_ids: *std.AutoArrayHashMapUnmanaged(usize, void), label: []const u8, link: []const u8) !void { 2550 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2551 errdefer text_box.deinit(allocator); 2552 text_box.getFocus().focusable = true; 2553 text_box.getFocus().kind = .{ .custom = link }; 2554 try tab_ids.put(allocator, text_box.getFocus().id, {}); 2555 try box.children.put(allocator, text_box.getFocus().id, .{ 2556 .widget = .{ .text_box = text_box }, 2557 .rect = null, 2558 .min_size = .{ .width = label.len + 2, .height = null }, 2559 }); 2560 } 2561 2562 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 2563 self.box.deinit(allocator); 2564 self.tab_ids.deinit(allocator); 2565 } 2566 2567 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2568 self.clearGrid(); 2569 // only the selected tab shows its border 2570 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 2571 switch (child.widget) { 2572 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 2573 else => {}, 2574 } 2575 } 2576 try self.box.build(allocator, constraint, root_focus); 2577 } 2578 2579 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2580 _ = allocator; 2581 const current_tab = self.currentTabIndex() orelse return; 2582 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 2583 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 2584 } 2585 } 2586 2587 pub fn clearGrid(self: *Header) void { 2588 self.box.clearGrid(); 2589 } 2590 2591 pub fn getGrid(self: Header) ?Grid { 2592 return self.box.getGrid(); 2593 } 2594 2595 pub fn getFocus(self: *Header) *Focus { 2596 return self.box.getFocus(); 2597 } 2598 2599 pub fn getSelectedIndex(self: Header) ?usize { 2600 return self.currentTabIndex(); 2601 } 2602 2603 fn currentTabIndex(self: Header) ?usize { 2604 const child_id = self.box.focus.child_id orelse return null; 2605 return self.tab_ids.getIndex(child_id); 2606 } 2607 }; 2608