╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1363 break :blk tb.getFocus().id; 1364 }; 1365 1366 // the issue's tags, each linking to this status's list filtered to 1367 // that tag. 1368 { 1369 var items: std.ArrayList(ui.TagFlow.Item) = .empty; 1370 defer items.deinit(allocator); 1371 var tag_iter = evt.Issue.tagIterator(entry.issue.event.tags); 1372 while (tag_iter.next()) |tag| { 1373 if (tag.len == 0) continue; 1374 try items.append(allocator, .{ .text = tag, .link = try tagLink(self.session.page_arena, self.data.identity, entry.issue.event.status, tag) }); 1375 } 1376 if (items.items.len > 0) { 1377 var tf = try ui.TagFlow.init(allocator); 1378 errdefer tf.deinit(allocator); 1379 try tf.setItems(allocator, items.items); 1380 try inner.children.put(allocator, tf.getFocus().id, .{ .widget = .{ .tag_flow = tf }, .rect = null, .min_size = null }); 1381 } 1382 } 1383 } 1384 1385 // the description as a focusable word-wrapped text box. past the limit 1386 // it's cut at the last whole line and links to the /description page, 1387 // which shows the whole thing. 1388 self.description_id[index] = blk: { 1389 const whole = entry.issue.event.description; 1390 const cut_short = !description_page and whole.len > max_description_size; 1391 const shown = if (cut_short) 1392 std.mem.trimEnd(u8, whole[0 .. std.mem.lastIndexOfScalar(u8, whole[0..max_description_size], '\n') orelse 0], " \t\r\n") 1393 else if (whole.len == 0) 1394 "(no description)" 1395 else 1396 whole; 1397 var tb = try wgt.TextBox(ui.Widget).init(allocator, shown, .{ 1398 .border_style = .single, 1399 .rounded_corners = true, 1400 .wrap_kind = .word, 1401 .label = " description ", 1402 .bottom_label = if (cut_short) " click or press enter to see more " else "", 1403 }); 1404 errdefer tb.deinit(allocator); 1405 tb.getFocus().focusable = true; 1406 if (cut_short) tb.getFocus().kind = .{ .custom = try descriptionLink(self.session.page_arena, self.data.identity, entry.id) }; 1407 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1408 break :blk tb.getFocus().id; 1409 }; 1410 1411 // select the title by default 1412 inner.getFocus().child_id = inner.children.keys()[title_child_index]; 1413 1414 // reset the scroll to the top for the newly-shown issue: directly on the 1415 // terminal (the wasm offset), and via a version bump on the web (so the 1416 // renderer's scroll id changes and JS drops the preserved position). 1417 const sc = self.detailScroll(index); 1418 sc.x = 0; 1419 sc.y = 0; 1420 sc.getFocus().version +%= 1; 1421 } 1422 1423 pub fn input(self: *View, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 1424 // wheel input moves focus like the arrows 1425 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 1426 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 1427 else 1428 raw_key; 1429 1430 if (self.headerActive()) { 1431 // down from the tabs re-enters the stack if the selected view has 1432 // something to focus; other keys move the tabs. 1433 if (inp.vertDirection(key) == .down) { 1434 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 1435 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 1436 // nothing focusable in the unauthorized view 1437 .unauthorized => false, 1438 else => true, 1439 } else false; 1440 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 1441 } else { 1442 try self.header().input(allocator, key, root_focus); 1443 } 1444 return; 1445 } 1446 if (self.tagsViewActive()) { 1447 self.tagsViewInput(key, root_focus); 1448 return; 1449 } 1450 if (self.formViewActive()) { 1451 if (self.data.view == .resolve) { 1452 try self.resolveInput(allocator, key, root_focus); 1453 } else { 1454 try self.formInput(allocator, key, root_focus); 1455 } 1456 return; 1457 } 1458 const i = self.selectedSplitIndex() orelse return; 1459 if (self.detailActive(i)) { 1460 try self.detailInput(allocator, i, key, root_focus); 1461 } else { 1462 try self.listInput(i, key, root_focus); 1463 } 1464 } 1465 1466 // arrow keys move the tag selection; up from the top row crosses to the 1467 // header tabs. 1468 fn tagsViewInput(self: *View, key: Key, root_focus: *Focus) void { 1469 const tf = self.tagsView(); 1470 const cid = tf.focus.child_id orelse return; 1471 const cur = tf.indexOfFocusId(cid) orelse return; 1472 const count = tf.text_boxes.items.len; 1473 switch (key) { 1474 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 1475 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 1476 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 1477 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 1478 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 1479 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 1480 else => {}, 1481 } 1482 } 1483 1484 fn listInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1485 // up/down (and the scroll wheel) move the selection a row; page up/down 1486 // jump a fixed amount. right/Enter cross into the detail pane. up from 1487 // the top row crosses into the header tabs. 1488 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 1489 const lb = self.listBox(index); 1490 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 1491 if (delta < 0 and at_top) return self.focusHeader(root_focus); 1492 ui.moveRowFocus(lb, self.listScroll(index), root_focus, delta); 1493 return; 1494 } 1495 switch (key) { 1496 .enter, .arrow_right => try self.focusDetail(index, root_focus), 1497 else => {}, 1498 } 1499 } 1500 1501 fn detailInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1502 if (self.toolRowFocused(index)) { 1503 try self.toolRowInput(allocator, index, key, root_focus); 1504 } else if (self.titleFocused(index)) { 1505 try self.titleInput(index, key, root_focus); 1506 } else if (self.authorFocused(index)) { 1507 try self.authorInput(index, key, root_focus); 1508 } else if (self.tagsFocused(index)) { 1509 try self.tagsInput(index, key, root_focus); 1510 } else { 1511 try self.descriptionInput(index, key, root_focus); 1512 } 1513 } 1514 1515 // the tool row: enter or a click on the open/close button flips the 1516 // issue's status; the edit and resolve buttons are a: links the host 1517 // follows. arrows cross between the buttons and to the neighboring 1518 // widgets. 1519 fn toolRowInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1520 const row = self.toolRow(index); 1521 const cur = if (row.getFocus().child_id) |cid| row.children.getIndex(cid) orelse return else return; 1522 const on_status = cur == button_in_row_index and self.statusButton(index) != null; 1523 switch (key) { 1524 .arrow_left => if (cur > button_in_row_index) root_focus.setFocus(row.children.keys()[cur - 1]) else try self.focusList(index, root_focus), 1525 .arrow_right => if (cur + 1 < row.children.count()) root_focus.setFocus(row.children.keys()[cur + 1]), 1526 .arrow_up => self.focusHeader(root_focus), 1527 .arrow_down => self.focusTitle(index, root_focus), 1528 .enter => if (on_status) try self.toggleIssueStatus(allocator, index), 1529 .mouse => |mouse| if (self.statusButton(index)) |button| { 1530 if (inp.leftClickOn(root_focus, button.getFocus().id, mouse)) try self.toggleIssueStatus(allocator, index); 1531 }, 1532 else => {}, 1533 } 1534 } 1535 1536 fn titleInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1537 switch (key) { 1538 .arrow_left => try self.focusList(index, root_focus), 1539 .arrow_up => self.focusToolRow(index, root_focus), 1540 .arrow_down => if (self.authorPresent(index)) self.focusAuthor(index, root_focus) else try self.focusDescription(index, root_focus), 1541 else => {}, 1542 } 1543 } 1544 1545 // the author box's a: link (when it has one) is followed by the host on 1546 // enter / a click; arrows cross to the neighboring widgets. 1547 fn authorInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1548 switch (key) { 1549 .arrow_left => try self.focusList(index, root_focus), 1550 .arrow_up => self.focusTitle(index, root_focus), 1551 .arrow_down => if (self.tagFlow(index) != null) try self.focusTags(index, root_focus) else try self.focusDescription(index, root_focus), 1552 else => {}, 1553 } 1554 } 1555 1556 fn descriptionInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1557 const sc = self.detailScroll(index); 1558 switch (key) { 1559 .arrow_left => return self.focusList(index, root_focus), 1560 // once the scroll can't move further, cross into the tags (or the 1561 // open/close button when the issue has none). 1562 .arrow_up => { 1563 const before = sc.y; 1564 sc.y -= 1; 1565 sc.clampToContent(); 1566 if (sc.y == before) { 1567 if (self.tagFlow(index) != null) 1568 try self.focusTags(index, root_focus) 1569 else if (self.authorPresent(index)) 1570 self.focusAuthor(index, root_focus) 1571 else 1572 self.focusTitle(index, root_focus); 1573 } 1574 return; 1575 }, 1576 .arrow_down => sc.y += 1, 1577 .page_up => sc.y -= 10, 1578 .page_down => sc.y += 10, 1579 .home => sc.y = 0, 1580 .end => sc.y = std.math.maxInt(isize), 1581 else => return, 1582 } 1583 sc.clampToContent(); 1584 } 1585 1586 // up/down (and tab/shift+tab) move between a form's fields; up from the 1587 // title crosses into the header tabs. the multiline description keeps 1588 // enter and any up/down that has a row to move to. 1589 fn formInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1590 const form = self.issueForm() orelse return; 1591 const cid = form.getFocus().child_id orelse return; 1592 const cur = form.children.getIndex(cid) orelse return; 1593 1594 if (cur == description_field_index) { 1595 const child = &form.children.values()[cur]; 1596 if (try multilineKeepsKey(&child.widget.text_input, allocator, key)) 1597 return child.widget.input(allocator, key, root_focus); 1598 } 1599 1600 switch (key) { 1601 .arrow_up, .back_tab => if (cur > 0) 1602 root_focus.setFocus(form.children.keys()[cur - 1]) 1603 else 1604 self.focusHeader(root_focus), 1605 .arrow_down, .tab => if (cur < submit_field_index) { 1606 root_focus.setFocus(form.children.keys()[cur + 1]); 1607 }, 1608 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 1609 .mouse => |mouse| if (cur == submit_field_index and inp.leftClickOn(root_focus, cid, mouse)) { 1610 try self.submitForm(allocator); 1611 }, 1612 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 1613 } 1614 } 1615 1616 // the multiline hunk inputs keep enter and any up/down that has a row to 1617 // move to; enter on a "use this" link is a navigation the host follows 1618 // before this runs. 1619 fn resolveInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1620 const form = self.issueForm() orelse return; 1621 const cid = form.getFocus().child_id orelse return; 1622 const cur = form.children.getIndex(cid) orelse return; 1623 const child = &form.children.values()[cur]; 1624 1625 if (child.widget == .text_input) { 1626 const ti = &child.widget.text_input; 1627 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 1628 return child.widget.input(allocator, key, root_focus); 1629 } 1630 1631 const on_submit = isSubmitButton(&child.widget); 1632 1633 switch (key) { 1634 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 1635 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 1636 .arrow_left, .arrow_right => switch (child.widget) { 1637 .box => |*row| { 1638 const row_cid = row.getFocus().child_id orelse return; 1639 const row_index = row.children.getIndex(row_cid) orelse return; 1640 if (key == .arrow_left) { 1641 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 1642 } else if (row_index + 1 < row.children.count()) { 1643 root_focus.setFocus(row.children.keys()[row_index + 1]); 1644 } 1645 }, 1646 else => try child.widget.input(allocator, key, root_focus), 1647 }, 1648 .enter => if (on_submit) try self.submitResolution(allocator), 1649 .mouse => |mouse| if (on_submit and inp.leftClickOn(root_focus, cid, mouse)) { 1650 try self.submitResolution(allocator); 1651 }, 1652 else => try child.widget.input(allocator, key, root_focus), 1653 } 1654 } 1655 1656 // a multiline input keeps enter and any up/down that has a row to move to 1657 fn multilineKeepsKey(text_input: *wgt.TextInput(ui.Widget), allocator: std.mem.Allocator, key: Key) !bool { 1658 return switch (key) { 1659 .enter => true, 1660 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 1661 .arrow_down => !try text_input.cursorOnLastRow(allocator), 1662 else => false, 1663 }; 1664 } 1665 1666 // whether the widget is a button that posts its form 1667 fn isSubmitButton(widget: *const ui.Widget) bool { 1668 return switch (widget.*) { 1669 .text_box => |*tb| switch (tb.box.focus.kind) { 1670 .custom => |custom| std.mem.eql(u8, custom, "submit"), 1671 else => false, 1672 }, 1673 else => false, 1674 }; 1675 } 1676 1677 // the neighboring focusable resolve-form child, skipping the spacer and 1678 // the blank gap rows 1679 fn resolveStep(form: *wgt.Box(ui.Widget), cur: usize, down: bool) ?usize { 1680 var i = cur; 1681 while (true) { 1682 if (down) { 1683 i += 1; 1684 if (i >= form.children.count()) return null; 1685 } else { 1686 if (i == 0) return null; 1687 i -= 1; 1688 } 1689 switch (form.children.values()[i].widget) { 1690 .spacer, .text => continue, 1691 else => return i, 1692 } 1693 } 1694 } 1695 1696 // focus the resolve-form child at `i` (a version row focuses its selected 1697 // child) and keep it visible on the terminal 1698 fn focusResolveChild(self: *View, form: *wgt.Box(ui.Widget), i: usize, root_focus: *Focus) void { 1699 const child = &form.children.values()[i]; 1700 switch (child.widget) { 1701 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 1702 else => root_focus.setFocus(form.children.keys()[i]), 1703 } 1704 if (self.session.is_terminal) { 1705 if (self.resolveScroll()) |sc| { 1706 if (child.rect) |rect| sc.scrollToRect(rect); 1707 } 1708 } 1709 } 1710 1711 // re-emit the conflicted issue's event with the resolved content; any 1712 // event on the issue settles its conflict entry. this is the terminal 1713 // path; the web posts the form to the resolve route. 1714 fn submitResolution(self: *View, allocator: std.mem.Allocator) !void { 1715 if (comptime wasm) return; 1716 const io = self.session.io orelse return; 1717 const src = self.data.repo_source orelse return; 1718 const entry = self.data.selectedIssue() orelse return; 1719 const author_email = (try self.session.eventAuthorEmail()) orelse return; 1720 const form = self.issueForm() orelse return; 1721 1722 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 1723 var title: ?[]u8 = null; 1724 var tags: ?[]u8 = null; 1725 var hunks: std.ArrayList([]const u8) = .empty; 1726 defer { 1727 if (title) |t| allocator.free(t); 1728 if (tags) |t| allocator.free(t); 1729 for (hunks.items) |h| allocator.free(h); 1730 hunks.deinit(allocator); 1731 } 1732 for (form.children.values()) |*child| switch (child.widget) { 1733 .text_input => |*ti| { 1734 const text = try ti.text(allocator); 1735 errdefer allocator.free(text); 1736 if (std.mem.eql(u8, ti.options.name, "title")) { 1737 title = text; 1738 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 1739 tags = text; 1740 } else { 1741 try hunks.append(allocator, text); 1742 } 1743 }, 1744 else => {}, 1745 }; 1746 1747 var id_bytes: [evt.event_id_size]u8 = undefined; 1748 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 1749 1750 switch (src.repo_kind) { 1751 inline else => |repo_kind| { 1752 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1753 defer any_repo.deinit(io, allocator); 1754 switch (any_repo) { 1755 inline else => |*repo| evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .resolve = .{ 1756 .title = title, 1757 .tags = tags, 1758 .hunks = hunks.items, 1759 } }, author_email) catch |err| switch (err) { 1760 // leave the form up for correction 1761 error.InvalidFields => return, 1762 else => |e| return e, 1763 }, 1764 } 1765 }, 1766 } 1767 1768 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 1769 try self.session.navigate(route); 1770 } 1771 1772 // the terminal submit paths: the new form commits a new-issue event, the 1773 // edit form re-emits the selected issue's event with the form's content. 1774 // the web posts the forms to their routes instead. 1775 fn submitForm(self: *View, allocator: std.mem.Allocator) !void { 1776 if (self.data.view == .edit) { 1777 try self.submitEditedIssue(allocator); 1778 } else { 1779 try self.submitNewIssue(allocator); 1780 } 1781 } 1782 1783 // commit the new issue to the repo's events branch and navigate to it. 1784 // this is the terminal path; the web posts the form to the issue route, 1785 // so the wasm side never runs (or compiles) the repo access below. 1786 fn submitNewIssue(self: *View, allocator: std.mem.Allocator) !void { 1787 if (comptime wasm) return; 1788 const io = self.session.io orelse return; 1789 const src = self.data.repo_source orelse return; 1790 const author_email = (try self.session.eventAuthorEmail()) orelse return; 1791 1792 const form = self.issueForm() orelse return; 1793 const title_input = &form.children.values()[title_field_index].widget.text_input; 1794 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 1795 const description_input = &form.children.values()[description_field_index].widget.text_input; 1796 1797 const title = try title_input.text(allocator); 1798 defer allocator.free(title); 1799 const tags = try tags_input.text(allocator); 1800 defer allocator.free(tags); 1801 const description = try description_input.text(allocator); 1802 defer allocator.free(description); 1803 1804 if (!evt.Issue.fieldsValid(title, tags)) return; 1805 1806 var id_bytes: [evt.event_id_size]u8 = undefined; 1807 io.random(&id_bytes); 1808 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 1809 1810 const event = evt.EventWithId{ 1811 .id = event_id_hex, 1812 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 1813 .author_email = author_email, 1814 .event = .{ .issue = .{ 1815 .title = title, 1816 .description = description, 1817 .tags = tags, 1818 } }, 1819 }; 1820 1821 switch (src.repo_kind) { 1822 inline else => |repo_kind| { 1823 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1824 defer any_repo.deinit(io, allocator); 1825 switch (any_repo) { 1826 inline else => |*repo| try evt.consume(repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 1827 } 1828 }, 1829 } 1830 1831 // wipe the form so a return visit starts fresh 1832 title_input.clear(allocator); 1833 tags_input.clear(allocator); 1834 description_input.clear(allocator); 1835 1836 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, "", &event_id_hex) orelse return; 1837 try self.session.navigate(route); 1838 } 1839 1840 // re-emit the selected issue's event with the form's content (status 1841 // preserved), then reload the issue's page. this is the terminal path; 1842 // the web posts the form to the edit route. 1843 fn submitEditedIssue(self: *View, allocator: std.mem.Allocator) !void { 1844 if (comptime wasm) return; 1845 const io = self.session.io orelse return; 1846 const src = self.data.repo_source orelse return; 1847 const entry = self.data.selectedIssue() orelse return; 1848 const author_email = (try self.session.eventAuthorEmail()) orelse return; 1849 1850 const form = self.issueForm() orelse return; 1851 const title_input = &form.children.values()[title_field_index].widget.text_input; 1852 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 1853 const description_input = &form.children.values()[description_field_index].widget.text_input; 1854 1855 const title = try title_input.text(allocator); 1856 defer allocator.free(title); 1857 const tags = try tags_input.text(allocator); 1858 defer allocator.free(tags); 1859 const description = try description_input.text(allocator); 1860 defer allocator.free(description); 1861 1862 if (!evt.Issue.fieldsValid(title, tags)) return; 1863 1864 var id_bytes: [evt.event_id_size]u8 = undefined; 1865 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 1866 1867 switch (src.repo_kind) { 1868 inline else => |repo_kind| { 1869 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1870 defer any_repo.deinit(io, allocator); 1871 switch (any_repo) { 1872 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 1873 .title = title, 1874 .tags = tags, 1875 .description = description, 1876 } }, author_email), 1877 } 1878 }, 1879 } 1880 1881 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 1882 try self.session.navigate(route); 1883 } 1884 1885 // flip the shown issue's status by re-emitting its event, then reload the 1886 // page rooted at the issue so the view reflects the change. this is the 1887 // terminal path; the web posts the button's form to the status route. 1888 fn toggleIssueStatus(self: *View, allocator: std.mem.Allocator, index: usize) !void { 1889 if (comptime wasm) return; 1890 const io = self.session.io orelse return; 1891 const src = self.data.repo_source orelse return; 1892 const sel = self.detailed_index[index] orelse return; 1893 const entry = self.window(index).issues[sel]; 1894 const author_email = (try self.session.eventAuthorEmail()) orelse return; 1895 1896 const status: evt.Issue.Status = switch (entry.issue.event.status) { 1897 .open => .closed, 1898 .closed => .open, 1899 }; 1900 1901 var id_bytes: [evt.event_id_size]u8 = undefined; 1902 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 1903 1904 switch (src.repo_kind) { 1905 inline else => |repo_kind| { 1906 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1907 defer any_repo.deinit(io, allocator); 1908 switch (any_repo) { 1909 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .status = status }, author_email), 1910 } 1911 }, 1912 } 1913 1914 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, status, "", entry.id) orelse return; 1915 try self.session.navigate(route); 1916 } 1917 1918 // arrow keys move the tag selection; at the flow's edges focus crosses to 1919 // the neighboring widgets. 1920 fn tagsInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1921 const tf = self.tagFlow(index) orelse return; 1922 const cid = tf.focus.child_id orelse return; 1923 const cur = tf.indexOfFocusId(cid) orelse return; 1924 const count = tf.text_boxes.items.len; 1925 switch (key) { 1926 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 1927 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 1928 .arrow_up => if (tf.rowStep(cur, false)) |i| self.focusTag(index, tf, root_focus, i) else self.focusAuthor(index, root_focus), 1929 .arrow_down => if (tf.rowStep(cur, true)) |i| self.focusTag(index, tf, root_focus, i) else try self.focusDescription(index, root_focus), 1930 .home => self.focusTag(index, tf, root_focus, 0), 1931 .end => self.focusTag(index, tf, root_focus, count - 1), 1932 else => {}, 1933 } 1934 } 1935 1936 const button_in_row_index: usize = 1; 1937 const title_child_index: usize = 0; 1938 const tags_child_index: usize = 2; 1939 1940 fn tagFlow(self: *View, index: usize) ?*ui.TagFlow { 1941 const inner = self.detailInner(index); 1942 if (inner.children.count() <= tags_child_index) return null; 1943 return switch (inner.children.values()[tags_child_index].widget) { 1944 .tag_flow => |*tf| tf, 1945 else => null, 1946 }; 1947 } 1948 1949 fn tagsFocused(self: *View, index: usize) bool { 1950 const inner = self.detailInner(index); 1951 const cid = inner.getFocus().child_id orelse return false; 1952 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 1953 } 1954 1955 // the open/close button inside the detail frame's tool row; a logged-out 1956 // session's or conflicted issue's row has none. 1957 fn statusButton(self: *View, index: usize) ?*wgt.TextBox(ui.Widget) { 1958 const row = self.toolRow(index); 1959 if (row.children.count() <= button_in_row_index) return null; 1960 const child = &row.children.values()[button_in_row_index].widget; 1961 if (!isSubmitButton(child)) return null; 1962 return &child.text_box; 1963 } 1964 1965 fn toolRowFocused(self: *View, index: usize) bool { 1966 const outer = self.detailOuter(index); 1967 const cid = outer.getFocus().child_id orelse return false; 1968 return outer.children.getIndex(cid) == tool_row_index; 1969 } 1970 1971 fn focusTag(self: *View, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 1972 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 1973 // keep the tag visible on the terminal: its rect offset by the flow's 1974 // position in the pane. 1975 if (self.session.is_terminal and item < tf.rects.items.len) { 1976 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 1977 var rect = tf.rects.items[item]; 1978 rect.x += flow_rect.x; 1979 rect.y += flow_rect.y; 1980 self.detailScroll(index).scrollToRect(rect); 1981 } 1982 } 1983 } 1984 1985 fn titleFocused(self: *View, index: usize) bool { 1986 const inner = self.detailInner(index); 1987 const cid = inner.getFocus().child_id orelse return false; 1988 return inner.children.getIndex(cid) == title_child_index; 1989 } 1990 1991 fn authorPresent(self: *View, index: usize) bool { 1992 return self.author_id[index] != null; 1993 } 1994 1995 fn authorFocused(self: *View, index: usize) bool { 1996 const inner = self.detailInner(index); 1997 const cid = inner.getFocus().child_id orelse return false; 1998 return cid == self.author_id[index]; 1999 } 2000 2001 // the author sits right under the title at the top of the pane, so 2002 // focusing it scrolls there. 2003 fn focusAuthor(self: *View, index: usize, root_focus: *Focus) void { 2004 const id = self.author_id[index] orelse return; 2005 root_focus.setFocus(id); 2006 self.detailScroll(index).y = 0; 2007 } 2008 2009 // the title sits at the top of the pane, so focusing it scrolls there. 2010 fn focusTitle(self: *View, index: usize, root_focus: *Focus) void { 2011 const inner = self.detailInner(index); 2012 if (inner.children.count() == 0) return; 2013 root_focus.setFocus(inner.children.keys()[title_child_index]); 2014 self.detailScroll(index).y = 0; 2015 } 2016 2017 fn focusTags(self: *View, index: usize, root_focus: *Focus) !void { 2018 const tf = self.tagFlow(index) orelse return; 2019 if (tf.text_boxes.items.len == 0) return; 2020 const cid = tf.focus.child_id orelse tf.text_boxes.items[0].getFocus().id; 2021 const item = tf.indexOfFocusId(cid) orelse 0; 2022 self.focusTag(index, tf, root_focus, item); 2023 } 2024 2025 fn focusDescription(self: *View, index: usize, root_focus: *Focus) !void { 2026 if (self.description_id[index]) |id| root_focus.setFocus(id); 2027 } 2028 2029 // return to the tool row's last-focused button (the header when the 2030 // description page shows no row). 2031 fn focusToolRow(self: *View, index: usize, root_focus: *Focus) void { 2032 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 2033 root_focus.setFocus(cid); 2034 } 2035 2036 // enter the detail pane. an empty pane (no issues) can't be entered. 2037 fn focusDetail(self: *View, index: usize, root_focus: *Focus) !void { 2038 if (self.detailInner(index).children.count() == 0) return; 2039 root_focus.setFocus(self.detailOuter(index).getFocus().id); 2040 } 2041 2042 // return to the list. 2043 fn focusList(self: *View, index: usize, root_focus: *Focus) !void { 2044 root_focus.setFocus(self.listScroll(index).getFocus().id); 2045 } 2046 2047 // cross to the header tabs above the stack. 2048 fn focusHeader(self: *View, root_focus: *Focus) void { 2049 root_focus.setFocus(self.box.children.keys()[header_index]); 2050 } 2051 2052 pub fn clearGrid(self: *View) void { 2053 self.box.clearGrid(); 2054 } 2055 2056 pub fn getGrid(self: View) ?Grid { 2057 return self.box.getGrid(); 2058 } 2059 2060 pub fn getFocus(self: *View) *Focus { 2061 return self.box.getFocus(); 2062 } 2063 2064 // for the parent's "scroll up at the top jumps to the header" check: at the 2065 // top only while the header tabs hold focus, so up from the split 2066 // crosses into the tabs first. 2067 pub fn getSelectedIndex(self: *View) ?usize { 2068 return if (self.headerActive()) 0 else 1; 2069 } 2070 }; 2071 2072 // the "a:" navigation link for `status`'s issues page filtered to the 2073 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 2074 // a rooted url ignores `status` (it derives its view from the issue's own); 2075 // with an empty `id` this is the bare list link. 2076 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 2077 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 2078 const url = try route.toUrl(page_arena); 2079 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2080 } 2081 2082 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 2083 // href is only followed with js off. 2084 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2085 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 2086 const url = try route.toUrl(page_arena); 2087 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 2088 } 2089 2090 // the "a:" link to the window `status_maybe` names, keeping the tag filter, 2091 // or the conflicts window when null, rooted at issue `id`. 2092 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?evt.Issue.Status, id: []const u8) ![]const u8 { 2093 if (status_maybe) |status| return issuesLink(page_arena, data.identity, status, data.tag, id); 2094 return conflictsLink(page_arena, data.identity, id); 2095 } 2096 2097 // the "a:" link to the conflicts list rooted at issue `start` ("" = the 2098 // first window). 2099 fn conflictsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, start: []const u8) ![]const u8 { 2100 const route = ui.RoutablePage.repoIssuesConflictsRoute(identity, start) orelse return error.RouteTooLong; 2101 const url = try route.toUrl(page_arena); 2102 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2103 } 2104 2105 // the "a:" link to the whole-description page of issue `id` within `identity`. 2106 fn descriptionLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2107 const route = ui.RoutablePage.repoIssuesDescriptionRoute(identity, id) orelse return error.RouteTooLong; 2108 const url = try route.toUrl(page_arena); 2109 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2110 } 2111 2112 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 2113 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 2114 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 2115 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 2116 const url = try route.toUrl(page_arena); 2117 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2118 } 2119 2120 // tabs switching between the issues page's views. 2121 pub const Header = struct { 2122 box: wgt.Box(ui.Widget), 2123 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 2124 2125 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 2126 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2127 errdefer box.deinit(allocator); 2128 2129 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 2130 errdefer tab_ids.deinit(allocator); 2131 2132 const aa = session.page_arena.allocator(); 2133 2134 // a list tab per status, labeled with its listing's issue count 2135 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 2136 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 2137 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2138 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 2139 try addTab(allocator, &box, &tab_ids, label, link); 2140 } 2141 2142 // tags tab, labeled with the active tag filter 2143 { 2144 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 2145 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 2146 const label = if (data.tag.len == 0) "tags" else blk: { 2147 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 2148 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 2149 }; 2150 try addTab(allocator, &box, &tab_ids, label, tags_link); 2151 } 2152 2153 // new-issue tab; an edit or resolve url shows its tab in this place 2154 { 2155 const route = switch (data.view) { 2156 .edit => ui.RoutablePage.repoIssuesEditRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 2157 .resolve => ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 2158 else => ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong, 2159 }; 2160 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2161 const label: []const u8 = switch (data.view) { 2162 .edit => "edit", 2163 .resolve => "resolve", 2164 else => "new", 2165 }; 2166 try addTab(allocator, &box, &tab_ids, label, link); 2167 } 2168 2169 // conflicts tab, labeled with the conflict count 2170 if (data.conflicts.count > 0) { 2171 const route = ui.RoutablePage.repoIssuesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 2172 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2173 const label = try std.fmt.allocPrint(aa, "conflicts ({d})", .{data.conflicts.count}); 2174 try addTab(allocator, &box, &tab_ids, label, link); 2175 } 2176 2177 var self = Header{ .box = box, .tab_ids = tab_ids }; 2178 // the tab matching the page's view is selected initially. 2179 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 2180 return self; 2181 } 2182 2183 fn addTab(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), tab_ids: *std.AutoArrayHashMapUnmanaged(usize, void), label: []const u8, link: []const u8) !void { 2184 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2185 errdefer text_box.deinit(allocator); 2186 text_box.getFocus().focusable = true; 2187 text_box.getFocus().kind = .{ .custom = link }; 2188 try tab_ids.put(allocator, text_box.getFocus().id, {}); 2189 try box.children.put(allocator, text_box.getFocus().id, .{ 2190 .widget = .{ .text_box = text_box }, 2191 .rect = null, 2192 .min_size = .{ .width = label.len + 2, .height = null }, 2193 }); 2194 } 2195 2196 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 2197 self.box.deinit(allocator); 2198 self.tab_ids.deinit(allocator); 2199 } 2200 2201 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2202 self.clearGrid(); 2203 // only the selected tab shows its border 2204 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 2205 switch (child.widget) { 2206 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 2207 else => {}, 2208 } 2209 } 2210 try self.box.build(allocator, constraint, root_focus); 2211 } 2212 2213 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2214 _ = allocator; 2215 const current_tab = self.currentTabIndex() orelse return; 2216 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 2217 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 2218 } 2219 } 2220 2221 pub fn clearGrid(self: *Header) void { 2222 self.box.clearGrid(); 2223 } 2224 2225 pub fn getGrid(self: Header) ?Grid { 2226 return self.box.getGrid(); 2227 } 2228 2229 pub fn getFocus(self: *Header) *Focus { 2230 return self.box.getFocus(); 2231 } 2232 2233 pub fn getSelectedIndex(self: Header) ?usize { 2234 return self.currentTabIndex(); 2235 } 2236 2237 fn currentTabIndex(self: Header) ?usize { 2238 const child_id = self.box.focus.child_id orelse return null; 2239 return self.tab_ids.getIndex(child_id); 2240 } 2241 }; 2242