╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1390 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1391 break :blk tb.getFocus().id; 1392 }; 1393 1394 // the thread's tags, each linking to this status's list filtered to 1395 // that tag. 1396 { 1397 var items: std.ArrayList(TagFlow.Item) = .empty; 1398 defer items.deinit(allocator); 1399 var tag_iter = Event.tagIterator(entry.record.event.tags); 1400 while (tag_iter.next()) |tag| { 1401 if (tag.len == 0) continue; 1402 try items.append(allocator, .{ .text = tag, .link = try tagLink(self.session.page_arena, self.data.identity, entryStatus(entry), tag) }); 1403 } 1404 if (items.items.len > 0) { 1405 var tf = try TagFlow.init(allocator); 1406 errdefer tf.deinit(allocator); 1407 try tf.setItems(allocator, items.items); 1408 try inner.children.put(allocator, tf.getFocus().id, .{ .widget = .{ .tag_flow = tf }, .rect = null, .min_size = null }); 1409 } 1410 } 1411 } 1412 1413 // the description as a focusable word-wrapped text box. past the limit 1414 // it's cut at the last whole line and links to the /description page, 1415 // which shows the whole thing. 1416 self.description_id[index] = blk: { 1417 const whole = entry.record.event.description; 1418 const cut_short = !description_page and whole.len > Data.max_description_size; 1419 const shown = if (cut_short) 1420 std.mem.trimEnd(u8, whole[0 .. std.mem.lastIndexOfScalar(u8, whole[0..Data.max_description_size], '\n') orelse 0], " \t\r\n") 1421 else if (whole.len == 0) 1422 "(no description)" 1423 else 1424 whole; 1425 var tb = try wgt.TextBox.init(allocator, shown, .{ 1426 .border_style = .single, 1427 .rounded_corners = true, 1428 .wrap_kind = .word, 1429 .label = " description ", 1430 .bottom_label = if (cut_short) " click or press enter to see more " else "", 1431 }); 1432 errdefer tb.deinit(allocator); 1433 tb.getFocus().focusable = true; 1434 if (cut_short) { 1435 const route = ui.RoutablePage.repoThreadDescriptionRoute(kind, self.data.identity, entry.id) orelse return error.RouteTooLong; 1436 tb.getFocus().kind = .{ .custom = try std.fmt.allocPrint(self.session.page_arena.allocator(), "a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 1437 } 1438 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1439 break :blk tb.getFocus().id; 1440 }; 1441 1442 if (supports_drafts and entryDraft(entry)) { 1443 inner.getFocus().child_id = inner.children.keys()[0]; 1444 const sc = self.detailScroll(index); 1445 sc.x = 0; 1446 sc.y = 0; 1447 sc.getFocus().version +%= 1; 1448 return; 1449 } 1450 1451 const parent_route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, entry.id, 0) orelse return error.RouteTooLong; 1452 try Attachment.appendRows(allocator, inner, self.session, self.data.identity, try parent_route.toUrl(self.session.page_arena), entry.attachments); 1453 1454 if (!description_page) { 1455 var reply = try Comment.linkBox(allocator, self.session, "new comment", ui.RoutablePage.repoThreadCommentNewRoute(kind, self.data.identity, entry.id, "") orelse return error.RouteTooLong); 1456 errdefer reply.deinit(allocator); 1457 try inner.children.put(allocator, reply.getFocus().id, .{ .widget = .{ .text_box = reply }, .rect = null, .min_size = null }); 1458 1459 try Comment.appendCount(allocator, inner, entry.comments.count, "comment", "comments"); 1460 for (entry.comments.comments) |comment| try Comment.appendComment(allocator, inner, self.session, self.data.identity, kind, comment); 1461 try Comment.appendWindowNav(allocator, inner, self.session, self.data.identity, kind, entry.id, null, entry.comments); 1462 1463 var spacer = try Spacer.init(allocator); 1464 errdefer spacer.deinit(allocator); 1465 try inner.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1466 } 1467 1468 // select the title by default 1469 if (self.title_id[index]) |id| inner.getFocus().child_id = id; 1470 1471 // reset the scroll to the top for the newly-shown thread: directly on the 1472 // terminal (the wasm offset), and via a version bump on the web (so the 1473 // renderer's scroll id changes and JS drops the preserved position). 1474 const sc = self.detailScroll(index); 1475 sc.x = 0; 1476 sc.y = 0; 1477 sc.getFocus().version +%= 1; 1478 } 1479 1480 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 1481 // wheel input moves focus like the arrows 1482 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 1483 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 1484 else 1485 raw_key; 1486 1487 if (self.headerActive()) { 1488 // down from the tabs re-enters the stack if the selected view has 1489 // something to focus; other keys move the tabs. 1490 if (inp.vertDirection(key) == .down) { 1491 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 1492 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 1493 // nothing focusable in the unauthorized view 1494 .unauthorized => false, 1495 else => true, 1496 } else false; 1497 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 1498 } else { 1499 try self.header().input(allocator, key, root_focus); 1500 } 1501 return; 1502 } 1503 if (self.tagsViewActive()) { 1504 self.tagsViewInput(key, root_focus); 1505 return; 1506 } 1507 if (self.formViewActive()) { 1508 if (self.data.view == .remove) { 1509 try self.removeFormInput(allocator, key, root_focus); 1510 } else if (supports_conflicts and self.data.view == .resolve) { 1511 try self.resolveInput(allocator, key, root_focus); 1512 } else if (self.data.view == .new_comment or self.data.view == .edit_comment) { 1513 try self.commentFormInput(allocator, key, root_focus); 1514 } else { 1515 try self.formInput(allocator, key, root_focus); 1516 } 1517 return; 1518 } 1519 const i = self.selectedSplitIndex() orelse return; 1520 if (self.detailActive(i)) { 1521 try self.detailInput(allocator, i, key, root_focus); 1522 } else { 1523 self.listInput(i, key, root_focus); 1524 } 1525 } 1526 1527 // arrow keys move the tag selection; up from the top row crosses to the 1528 // header tabs. 1529 fn tagsViewInput(self: *This, key: Key, root_focus: *Focus) void { 1530 const tf = self.tagsView(); 1531 const cid = tf.focus.child_id orelse return; 1532 const cur = tf.indexOfFocusId(cid) orelse return; 1533 const count = tf.text_boxes.items.len; 1534 switch (key) { 1535 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 1536 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 1537 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 1538 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 1539 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 1540 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 1541 else => {}, 1542 } 1543 } 1544 1545 fn listInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 1546 // up/down (and the scroll wheel) move the selection a row; page up/down 1547 // jump a fixed amount. right/Enter cross into the detail pane. up from 1548 // the top row crosses into the header tabs. 1549 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 1550 const lb = self.listBox(index); 1551 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 1552 if (delta < 0 and at_top) return self.focusHeader(root_focus); 1553 moveRowFocus(lb, self.listScroll(index), root_focus, delta); 1554 return; 1555 } 1556 switch (key) { 1557 .enter, .arrow_right => self.focusDetail(index, root_focus), 1558 else => {}, 1559 } 1560 } 1561 1562 fn detailInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1563 switch (key) { 1564 .page_up => { 1565 self.pageDetail(index, root_focus, -10); 1566 return; 1567 }, 1568 .page_down => { 1569 self.pageDetail(index, root_focus, 10); 1570 return; 1571 }, 1572 .home => { 1573 self.jumpDetail(index, root_focus, false); 1574 return; 1575 }, 1576 .end => { 1577 self.jumpDetail(index, root_focus, true); 1578 return; 1579 }, 1580 else => {}, 1581 } 1582 if (self.data.comment_page != null and std.mem.eql(u8, self.window(index).items[self.detailed_index[index] orelse return].id, self.data.selected_id)) { 1583 self.commentInput(index, key, root_focus); 1584 return; 1585 } 1586 if (self.toolRowFocused(index)) { 1587 try self.toolRowInput(allocator, index, key, root_focus); 1588 return; 1589 } 1590 1591 const child_index = self.focusedDetailChild(index, root_focus) orelse return; 1592 const inner = self.detailInner(index); 1593 const child_id = inner.children.keys()[child_index]; 1594 const child = &inner.children.values()[child_index].widget; 1595 1596 if (child_id == self.title_id[index]) { 1597 switch (key) { 1598 .arrow_left => self.focusList(index, root_focus), 1599 .arrow_up => if (!self.moveDetailVertical(index, root_focus, false)) self.focusToolRow(index, root_focus), 1600 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1601 else => {}, 1602 } 1603 return; 1604 } 1605 if (child_id == self.author_id[index]) { 1606 // the author's a: link is followed by the host; arrows cross to 1607 // neighboring widgets. 1608 switch (key) { 1609 .arrow_left => self.focusList(index, root_focus), 1610 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1611 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1612 else => {}, 1613 } 1614 return; 1615 } 1616 if (child_id == self.description_id[index]) { 1617 self.descriptionInput(index, key, root_focus); 1618 return; 1619 } 1620 1621 switch (child.*) { 1622 .tag_flow => |*tf| { 1623 self.tagsInput(index, child_index, tf, key, root_focus); 1624 return; 1625 }, 1626 .copyable_text => |*copyable| { 1627 switch (key) { 1628 .arrow_up => if (!self.moveDetailVertical(index, root_focus, false)) self.focusToolRow(index, root_focus), 1629 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1630 .arrow_left => if (copyable.selected == 0) 1631 self.focusList(index, root_focus) 1632 else 1633 try copyable.input(allocator, key, root_focus), 1634 else => try copyable.input(allocator, key, root_focus), 1635 } 1636 return; 1637 }, 1638 .box => |*row| if (Attachment.removeId(row)) |attachment_id| { 1639 const remove_id = row.children.keys()[1]; 1640 switch (key) { 1641 .arrow_left => if (!self.moveCommentHorizontal(index, root_focus, false)) self.focusList(index, root_focus), 1642 .arrow_right => _ = self.moveCommentHorizontal(index, root_focus, true), 1643 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1644 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1645 .enter => if (row.getFocus().child_id == remove_id) try self.removeEvent(allocator, .attach, attachment_id), 1646 .mouse => |mouse| if (inp.leftClickOn(root_focus, remove_id, mouse)) try self.removeEvent(allocator, .attach, attachment_id), 1647 else => {}, 1648 } 1649 return; 1650 }, 1651 else => {}, 1652 } 1653 self.commentInput(index, key, root_focus); 1654 } 1655 1656 // enter or a click runs the primary action; links are handled by the host 1657 fn toolRowInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1658 const row = self.toolRow(index); 1659 const cur = if (row.getFocus().child_id) |cid| row.children.getIndex(cid) orelse return else return; 1660 const child_id = row.children.keys()[cur]; 1661 const on_primary = switch (row.children.values()[cur].widget.getFocus().kind) { 1662 .custom => |action| std.mem.eql(u8, action, "submit"), 1663 else => false, 1664 }; 1665 switch (key) { 1666 .arrow_left => if (cur > first_in_row_index) root_focus.setFocus(row.children.keys()[cur - 1]) else self.focusList(index, root_focus), 1667 .arrow_right => if (cur + 1 < row.children.count()) root_focus.setFocus(row.children.keys()[cur + 1]), 1668 .arrow_up => self.focusHeader(root_focus), 1669 .arrow_down => { 1670 self.focusDetailEdge(index, root_focus, false); 1671 self.detailScroll(index).y = 0; 1672 }, 1673 .enter => if (on_primary) try self.primaryAction(allocator, index), 1674 .mouse => |mouse| if (on_primary and inp.leftClickOn(root_focus, child_id, mouse)) try self.primaryAction(allocator, index), 1675 else => {}, 1676 } 1677 } 1678 1679 fn primaryAction(self: *This, allocator: std.mem.Allocator, index: usize) !void { 1680 const selected = self.detailed_index[index] orelse return; 1681 const entry = self.window(index).items[selected]; 1682 if (supports_drafts and entryDraft(entry)) 1683 try self.postDraft(allocator, index) 1684 else 1685 try self.toggleStatus(allocator, index); 1686 } 1687 1688 fn postDraft(self: *This, allocator: std.mem.Allocator, index: usize) !void { 1689 if (!supports_drafts or comptime wasm) return; 1690 const selected = self.detailed_index[index] orelse return; 1691 const entry = self.window(index).items[selected]; 1692 if (!entryDraft(entry) or !entry.revision_ready) return; 1693 try self.data.postDraft(self.session, allocator, entry.id); 1694 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 1695 try self.session.navigate(route); 1696 } 1697 1698 fn descriptionInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 1699 const sc = self.detailScroll(index); 1700 switch (key) { 1701 .arrow_left => return self.focusList(index, root_focus), 1702 // the terminal moves through the description one line at a time; 1703 // the web lets focus changes drive its native scroll. 1704 .arrow_up => { 1705 if (!self.session.is_terminal) { 1706 _ = self.moveDetailVertical(index, root_focus, false); 1707 return; 1708 } 1709 if (self.focusAdjacentVisible(index, root_focus, false)) return; 1710 const before = sc.y; 1711 sc.y -= 1; 1712 sc.clampToContent(); 1713 if (sc.y == before) _ = self.moveDetailVertical(index, root_focus, false); 1714 return; 1715 }, 1716 .arrow_down => { 1717 if (!self.session.is_terminal) { 1718 _ = self.moveDetailVertical(index, root_focus, true); 1719 return; 1720 } 1721 if (self.focusAdjacentVisible(index, root_focus, true)) return; 1722 const before = sc.y; 1723 sc.y += 1; 1724 sc.clampToContent(); 1725 if (sc.y == before) _ = self.moveDetailVertical(index, root_focus, true); 1726 return; 1727 }, 1728 else => return, 1729 } 1730 } 1731 1732 fn commentInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 1733 switch (key) { 1734 .arrow_left => if (!self.moveCommentHorizontal(index, root_focus, false)) return self.focusList(index, root_focus), 1735 .arrow_right => _ = self.moveCommentHorizontal(index, root_focus, true), 1736 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1737 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1738 else => return, 1739 } 1740 } 1741 1742 // page the detail scroll, then focus the first or last visible widget. 1743 fn pageDetail(self: *This, index: usize, root_focus: *Focus, delta: isize) void { 1744 if (!self.session.is_terminal) return; 1745 const sc = self.detailScroll(index); 1746 sc.y += delta; 1747 sc.clampToContent(); 1748 self.focusVisible(index, root_focus, delta > 0); 1749 } 1750 1751 // jump to the detail's first or last focusable widget. the terminal pins 1752 // its synthetic scroll too; the browser scrolls to the new focus itself. 1753 fn jumpDetail(self: *This, index: usize, root_focus: *Focus, to_end: bool) void { 1754 if (self.session.is_terminal) { 1755 const sc = self.detailScroll(index); 1756 sc.y = if (to_end) std.math.maxInt(isize) else 0; 1757 sc.clampToContent(); 1758 self.focusVisible(index, root_focus, to_end); 1759 } else { 1760 self.focusDetailEdge(index, root_focus, to_end); 1761 } 1762 } 1763 1764 fn focusDetailEdge(self: *This, index: usize, root_focus: *Focus, last: bool) void { 1765 const focus = self.detailInner(index).getFocus(); 1766 var chosen: ?usize = null; 1767 for (focus.children.keys(), focus.children.values()) |id, child| { 1768 if (!child.focus.focusable) continue; 1769 chosen = id; 1770 if (!last) break; 1771 } 1772 if (chosen) |id| root_focus.setFocus(id); 1773 } 1774 1775 fn detailRectVisible(self: *This, index: usize, rect: layout.IRect) bool { 1776 const sc = self.detailScroll(index); 1777 const viewport = sc.grid orelse return false; 1778 const top = sc.y; 1779 const bottom = top + @as(isize, @intCast(viewport.size.height - sc.bar_h)); 1780 return rect.y + @as(isize, @intCast(rect.size.height)) > top and rect.y < bottom; 1781 } 1782 1783 // focus the top- or bottom-most focusable widget intersecting the viewport. 1784 fn focusVisible(self: *This, index: usize, root_focus: *Focus, last: bool) void { 1785 const inner = self.detailInner(index); 1786 var offset: usize = 0; 1787 while (offset < inner.children.count()) : (offset += 1) { 1788 const child_index = if (last) inner.children.count() - 1 - offset else offset; 1789 if (self.focusVisibleChild(index, child_index, root_focus, last)) return; 1790 } 1791 } 1792 1793 fn focusVisibleChild(self: *This, index: usize, child_index: usize, root_focus: *Focus, last: bool) bool { 1794 const child = &self.detailInner(index).children.values()[child_index]; 1795 const child_rect = child.rect orelse return false; 1796 var chosen: ?usize = null; 1797 if (child.widget.getFocus().focusable and self.detailRectVisible(index, child_rect)) { 1798 const id = self.detailInner(index).children.keys()[child_index]; 1799 chosen = id; 1800 if (!last) { 1801 root_focus.setFocus(id); 1802 return true; 1803 } 1804 } 1805 1806 for (child.widget.getFocus().children.keys(), child.widget.getFocus().children.values()) |id, focus_child| { 1807 if (!focus_child.focus.focusable) continue; 1808 const rect: layout.IRect = .{ 1809 .x = child_rect.x + @as(isize, @intCast(focus_child.rect.x)), 1810 .y = child_rect.y + @as(isize, @intCast(focus_child.rect.y)), 1811 .size = focus_child.rect.size, 1812 }; 1813 if (!self.detailRectVisible(index, rect)) continue; 1814 chosen = id; 1815 if (!last) break; 1816 } 1817 if (chosen) |id| { 1818 root_focus.setFocus(id); 1819 return true; 1820 } 1821 return false; 1822 } 1823 1824 fn focusAdjacentVisible(self: *This, index: usize, root_focus: *Focus, down: bool) bool { 1825 const inner = self.detailInner(index); 1826 var next = self.focusedDetailChild(index, root_focus) orelse return false; 1827 while (true) { 1828 if (down) { 1829 next += 1; 1830 if (next >= inner.children.count()) return false; 1831 } else { 1832 if (next == 0) return false; 1833 next -= 1; 1834 } 1835 if (self.focusVisibleChild(index, next, root_focus, !down)) { 1836 self.focusDetailChild(index, next, !down, root_focus); 1837 return true; 1838 } 1839 } 1840 } 1841 1842 fn focusedDetailChild(self: *This, index: usize, root_focus: *Focus) ?usize { 1843 const inner = self.detailInner(index); 1844 var id = root_focus.grandchild_id orelse return null; 1845 while (inner.getFocus().children.get(id)) |child| { 1846 if (child.parent_id == inner.getFocus().id) return inner.children.getIndex(id); 1847 id = child.parent_id; 1848 } 1849 return null; 1850 } 1851 1852 fn detailChildFocusable(self: *This, index: usize, child_index: usize) bool { 1853 const child = &self.detailInner(index).children.values()[child_index].widget; 1854 const focus = child.getFocus(); 1855 if (focus.focusable) return true; 1856 for (focus.children.values()) |item| { 1857 if (item.focus.focusable) return true; 1858 } 1859 return false; 1860 } 1861 1862 fn focusDetailChild(self: *This, index: usize, child_index: usize, last: bool, root_focus: *Focus) void { 1863 const inner = self.detailInner(index); 1864 const child = &inner.children.values()[child_index]; 1865 var target = inner.children.keys()[child_index]; 1866 var rect = child.rect orelse return; 1867 switch (child.widget) { 1868 .repo_comment => |*comment| { 1869 if (comment.rowRect(last)) |row_rect| { 1870 rect.x += row_rect.x; 1871 rect.y += row_rect.y; 1872 rect.size = row_rect.size; 1873 } 1874 if (last) comment.focusBody(root_focus) else comment.focusMetadata(root_focus); 1875 }, 1876 .box => |*box| if (box.children.count() > 0) { 1877 target = box.children.keys()[if (last) box.children.count() - 1 else 0]; 1878 root_focus.setFocus(target); 1879 }, 1880 else => root_focus.setFocus(target), 1881 } 1882 const sc = self.detailScroll(index); 1883 if (sc.grid) |viewport| { 1884 const viewport_height = viewport.size.height - sc.bar_h; 1885 if (rect.size.height > viewport_height) { 1886 const bottom_aligned = rect.y + @as(isize, @intCast(rect.size.height - viewport_height)); 1887 sc.y = std.math.clamp(sc.y, rect.y, bottom_aligned); 1888 sc.clampToContent(); 1889 return; 1890 } 1891 } 1892 sc.scrollToRect(rect); 1893 } 1894 1895 fn moveDetailVertical(self: *This, index: usize, root_focus: *Focus, down: bool) bool { 1896 const inner = self.detailInner(index); 1897 const current = self.focusedDetailChild(index, root_focus) orelse return false; 1898 if (inner.children.values()[current].widget == .repo_comment) { 1899 const comment = &inner.children.values()[current].widget.repo_comment; 1900 if (down and !comment.bodyFocused()) { 1901 self.focusDetailChild(index, current, true, root_focus); 1902 return true; 1903 } 1904 if (!down and comment.bodyFocused()) { 1905 self.focusDetailChild(index, current, false, root_focus); 1906 return true; 1907 } 1908 } 1909 1910 var next = current; 1911 while (true) { 1912 if (down) { 1913 next += 1; 1914 if (next >= inner.children.count()) return false; 1915 } else { 1916 if (next == 0) return false; 1917 next -= 1; 1918 } 1919 if (!self.detailChildFocusable(index, next)) continue; 1920 self.focusDetailChild(index, next, !down, root_focus); 1921 return true; 1922 } 1923 } 1924 1925 fn moveCommentHorizontal(self: *This, index: usize, root_focus: *Focus, right: bool) bool { 1926 const inner = self.detailInner(index); 1927 const current = self.focusedDetailChild(index, root_focus) orelse return false; 1928 const child = &inner.children.values()[current]; 1929 if (child.widget == .repo_comment) { 1930 const moved = child.widget.repo_comment.moveHorizontal(root_focus, right); 1931 if (moved) if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 1932 return moved; 1933 } 1934 const row = switch (child.widget) { 1935 .box => |*box| box, 1936 else => return false, 1937 }; 1938 const selected = row.getFocus().child_id orelse return false; 1939 const selected_index = row.children.getIndex(selected) orelse return false; 1940 const target = if (right) selected_index + 1 else selected_index -| 1; 1941 if (target == selected_index or target >= row.children.count()) return false; 1942 root_focus.setFocus(row.children.keys()[target]); 1943 if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 1944 return true; 1945 } 1946 1947 // up/down (and tab/shift+tab) move between a form's fields; up from the 1948 // title crosses into the header tabs. the multiline description keeps 1949 // enter and any up/down that has a row to move to. 1950 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1951 const form = self.threadForm() orelse return; 1952 const cid = form.getFocus().child_id orelse return; 1953 const cur = form.children.getIndex(cid) orelse return; 1954 1955 if (cur == description_field_index) { 1956 const child = &form.children.values()[cur]; 1957 if (try multilineKeepsKey(&child.widget.text_input, allocator, key)) 1958 return child.widget.input(allocator, key, root_focus); 1959 } 1960 1961 switch (key) { 1962 .arrow_up, .back_tab => if (cur > 0) 1963 root_focus.setFocus(form.children.keys()[cur - 1]) 1964 else 1965 self.focusHeader(root_focus), 1966 .arrow_down, .tab => if (cur < submit_field_index) { 1967 root_focus.setFocus(form.children.keys()[cur + 1]); 1968 }, 1969 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 1970 .mouse => |mouse| if (cur == submit_field_index) { 1971 const submit = &form.children.values()[cur].widget.submit_button; 1972 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitForm(allocator); 1973 }, 1974 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 1975 } 1976 } 1977 1978 fn commentFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1979 const form = self.threadForm() orelse return; 1980 const cid = form.getFocus().child_id orelse return; 1981 const cur = form.children.getIndex(cid) orelse return; 1982 const child = &form.children.values()[cur]; 1983 1984 if (cur == comment_body_field_index and try multilineKeepsKey(&child.widget.text_input, allocator, key)) 1985 return child.widget.input(allocator, key, root_focus); 1986 1987 switch (key) { 1988 .arrow_up, .back_tab => if (cur > comment_author_field_index) 1989 root_focus.setFocus(form.children.keys()[cur - 1]) 1990 else 1991 self.focusHeader(root_focus), 1992 .arrow_down, .tab => if (cur < comment_submit_field_index) 1993 root_focus.setFocus(form.children.keys()[cur + 1]), 1994 .enter => if (cur == comment_submit_field_index) try self.submitComment(allocator), 1995 .mouse => |mouse| if (cur == comment_submit_field_index) { 1996 const submit = &child.widget.submit_button; 1997 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitComment(allocator); 1998 }, 1999 else => try child.widget.input(allocator, key, root_focus), 2000 } 2001 } 2002 2003 fn removeFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2004 const form = self.removeForm() orelse return; 2005 const button_id = form.children.keys()[1]; 2006 switch (key) { 2007 .arrow_up, .back_tab => self.focusHeader(root_focus), 2008 .enter => try self.submitRemove(allocator), 2009 .mouse => |mouse| if (inp.leftClickOn(root_focus, button_id, mouse)) try self.submitRemove(allocator), 2010 else => {}, 2011 } 2012 } 2013 2014 // the multiline hunk inputs keep enter and any up/down that has a row to 2015 // move to; enter on a "use this" link is a navigation the host follows 2016 // before this runs. 2017 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2018 const form = self.threadForm() orelse return; 2019 const cid = form.getFocus().child_id orelse return; 2020 const cur = form.children.getIndex(cid) orelse return; 2021 const child = &form.children.values()[cur]; 2022 2023 if (child.widget == .text_input) { 2024 const ti = &child.widget.text_input; 2025 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2026 return child.widget.input(allocator, key, root_focus); 2027 } 2028 2029 const on_submit = child.widget == .submit_button; 2030 2031 switch (key) { 2032 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2033 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2034 .arrow_left, .arrow_right => switch (child.widget) { 2035 .box => |*row| { 2036 const row_cid = row.getFocus().child_id orelse return; 2037 const row_index = row.children.getIndex(row_cid) orelse return; 2038 if (key == .arrow_left) { 2039 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 2040 } else if (row_index + 1 < row.children.count()) { 2041 root_focus.setFocus(row.children.keys()[row_index + 1]); 2042 } 2043 }, 2044 else => try child.widget.input(allocator, key, root_focus), 2045 }, 2046 .enter => if (on_submit) try self.submitResolution(allocator), 2047 .mouse => |mouse| if (on_submit) { 2048 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2049 }, 2050 else => try child.widget.input(allocator, key, root_focus), 2051 } 2052 } 2053 2054 // a multiline input keeps enter and any up/down that has a row to move to 2055 fn multilineKeepsKey(text_input: *wgt.TextInput, allocator: std.mem.Allocator, key: Key) !bool { 2056 return switch (key) { 2057 .enter => true, 2058 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2059 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2060 else => false, 2061 }; 2062 } 2063 2064 // the neighboring focusable resolve-form child, skipping the spacer and 2065 // the blank gap rows 2066 fn resolveStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2067 var i = cur; 2068 while (true) { 2069 if (down) { 2070 i += 1; 2071 if (i >= form.children.count()) return null; 2072 } else { 2073 if (i == 0) return null; 2074 i -= 1; 2075 } 2076 switch (form.children.values()[i].widget) { 2077 .spacer, .text => continue, 2078 else => return i, 2079 } 2080 } 2081 } 2082 2083 // focus the resolve-form child at `i` (a version row focuses its selected 2084 // child) and keep it visible on the terminal 2085 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2086 const child = &form.children.values()[i]; 2087 switch (child.widget) { 2088 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2089 else => root_focus.setFocus(form.children.keys()[i]), 2090 } 2091 if (self.session.is_terminal) { 2092 if (self.resolveScroll()) |sc| { 2093 if (child.rect) |rect| sc.scrollToRect(rect); 2094 } 2095 } 2096 } 2097 2098 // re-emit the conflicted thread's event with the resolved content; any 2099 // event on the thread settles its conflict entry. this is the terminal 2100 // path; the web posts the form to the resolve route. 2101 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2102 if (comptime wasm) return; 2103 const io = self.session.io orelse return; 2104 const src = self.data.repo_source orelse return; 2105 const entry = self.data.selectedThread() orelse return; 2106 const author = (try self.session.eventAuthor()) orelse return; 2107 const form = self.threadForm() orelse return; 2108 2109 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2110 var title: ?[]u8 = null; 2111 var tags: ?[]u8 = null; 2112 var hunks: std.ArrayList([]const u8) = .empty; 2113 defer { 2114 if (title) |t| allocator.free(t); 2115 if (tags) |t| allocator.free(t); 2116 for (hunks.items) |h| allocator.free(h); 2117 hunks.deinit(allocator); 2118 } 2119 for (form.children.values()) |*child| switch (child.widget) { 2120 .text_input => |*ti| { 2121 const text = try ti.text(allocator); 2122 errdefer allocator.free(text); 2123 if (std.mem.eql(u8, ti.options.name, "title")) { 2124 title = text; 2125 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2126 tags = text; 2127 } else { 2128 try hunks.append(allocator, text); 2129 } 2130 }, 2131 else => {}, 2132 }; 2133 2134 const id_bytes = try evt.parseEventId(entry.id); 2135 2136 switch (src.repo_kind) { 2137 inline else => |repo_kind| { 2138 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2139 defer any_repo.deinit(io, allocator); 2140 switch (any_repo) { 2141 inline else => |*repo| { 2142 var resolution: Event.Resolve = .{ 2143 .title = title, 2144 .tags = tags, 2145 .hunks = hunks.items, 2146 }; 2147 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2148 const change = Event.Update{ .resolve = resolution }; 2149 Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2150 // leave the form up for correction 2151 error.InvalidFields => return, 2152 else => |e| return e, 2153 }; 2154 }, 2155 } 2156 }, 2157 } 2158 2159 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2160 try self.session.navigate(route); 2161 } 2162 2163 // commit a new or edited comment and navigate to its permalink. this is the 2164 // terminal path; the web posts the form to its comment route. 2165 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2166 if (comptime wasm) return; 2167 const io = self.session.io orelse return; 2168 const src = self.data.repo_source orelse return; 2169 const author = (try self.session.eventAuthor()) orelse return; 2170 const form = self.threadForm() orelse return; 2171 const body_input = &form.children.values()[comment_body_field_index].widget.text_input; 2172 const body = try body_input.text(allocator); 2173 defer allocator.free(body); 2174 if (!evt.Comment.fieldsValid(body)) return; 2175 2176 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2177 @memcpy(&thread_id, self.data.selected_id); 2178 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2179 var parent: [evt.event_id_size * 2]u8 = undefined; 2180 @memcpy(&parent, self.data.comment_id); 2181 break :blk parent; 2182 }; 2183 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2184 const editing = self.data.view == .edit_comment; 2185 switch (src.repo_kind) { 2186 inline else => |repo_kind| { 2187 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2188 defer any_repo.deinit(io, allocator); 2189 switch (any_repo) { 2190 inline else => |*repo| if (editing) { 2191 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2192 try evt.Comment.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2193 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2194 } else { 2195 event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2196 }, 2197 } 2198 }, 2199 } 2200 2201 body_input.clear(allocator); 2202 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2203 try self.session.navigate(route); 2204 } 2205 2206 // the terminal submit paths: the new form commits a new thread event, the 2207 // edit form re-emits the selected thread's event with the form's content. 2208 // the web posts the forms to their routes instead. 2209 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2210 if (self.data.view == .edit) { 2211 try self.submitEditedThread(allocator); 2212 } else { 2213 try self.submitNewThread(allocator); 2214 } 2215 } 2216 2217 // commit the new thread to the repo's events branch and navigate to it. 2218 // this is the terminal path; the web posts the form to the thread route, 2219 // so the wasm side never runs (or compiles) the repo access below. 2220 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2221 if (comptime wasm) return; 2222 const io = self.session.io orelse return; 2223 const src = self.data.repo_source orelse return; 2224 const author = (try self.session.eventAuthor()) orelse return; 2225 2226 const form = self.threadForm() orelse return; 2227 const title_input = &form.children.values()[title_field_index].widget.text_input; 2228 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2229 const description_input = &form.children.values()[description_field_index].widget.text_input; 2230 2231 const title = try title_input.text(allocator); 2232 defer allocator.free(title); 2233 const tags = try tags_input.text(allocator); 2234 defer allocator.free(tags); 2235 const description = try description_input.text(allocator); 2236 defer allocator.free(description); 2237 2238 if (!Event.fieldsValid(title, tags)) return; 2239 2240 if (comptime supports_drafts) { 2241 const event_id_hex = try Data.createDraft(self.data, self.session, allocator, title, tags, description); 2242 title_input.clear(allocator); 2243 tags_input.clear(allocator); 2244 description_input.clear(allocator); 2245 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2246 try self.session.navigate(route); 2247 return; 2248 } 2249 2250 var id_bytes: [evt.event_id_size]u8 = undefined; 2251 io.random(&id_bytes); 2252 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2253 2254 const event = evt.EventWithId{ 2255 .id = event_id_hex, 2256 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2257 .author = author, 2258 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2259 .title = title, 2260 .description = description, 2261 .tags = tags, 2262 }), 2263 }; 2264 2265 switch (src.repo_kind) { 2266 inline else => |repo_kind| { 2267 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2268 defer any_repo.deinit(io, allocator); 2269 switch (any_repo) { 2270 inline else => |*repo| try evt.consume(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2271 } 2272 }, 2273 } 2274 2275 // wipe the form so a return visit starts fresh 2276 title_input.clear(allocator); 2277 tags_input.clear(allocator); 2278 description_input.clear(allocator); 2279 2280 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2281 try self.session.navigate(route); 2282 } 2283 2284 // re-emit the selected thread's event with the form's content (status 2285 // preserved), then reload the thread's page. this is the terminal path; 2286 // the web posts the form to the edit route. 2287 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2288 if (comptime wasm) return; 2289 const io = self.session.io orelse return; 2290 const src = self.data.repo_source orelse return; 2291 const entry = self.data.selectedThread() orelse return; 2292 const author = (try self.session.eventAuthor()) orelse return; 2293 2294 const form = self.threadForm() orelse return; 2295 const title_input = &form.children.values()[title_field_index].widget.text_input; 2296 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2297 const description_input = &form.children.values()[description_field_index].widget.text_input; 2298 2299 const title = try title_input.text(allocator); 2300 defer allocator.free(title); 2301 const tags = try tags_input.text(allocator); 2302 defer allocator.free(tags); 2303 const description = try description_input.text(allocator); 2304 defer allocator.free(description); 2305 2306 if (!Event.fieldsValid(title, tags)) return; 2307 2308 const id_bytes = try evt.parseEventId(entry.id); 2309 2310 switch (src.repo_kind) { 2311 inline else => |repo_kind| { 2312 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2313 defer any_repo.deinit(io, allocator); 2314 switch (any_repo) { 2315 inline else => |*repo| if (has_status) 2316 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2317 .title = title, 2318 .tags = tags, 2319 .description = description, 2320 } }, author) 2321 else 2322 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 2323 } 2324 }, 2325 } 2326 2327 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2328 try self.session.navigate(route); 2329 } 2330 2331 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2332 const comment = self.data.comment_id.len != 0; 2333 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2334 const id = evt.parseEventId(event_id) catch return; 2335 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2336 } 2337 2338 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2339 if (comptime wasm) return; 2340 2341 if (comptime supports_drafts) { 2342 const selected = self.data.selectedThread() orelse return; 2343 if (event_kind == kind and entryDraft(selected.*)) { 2344 try Data.removeDraft(self.session, allocator, &id); 2345 const route = draftsRoute(self.data.identity) orelse return; 2346 try self.session.navigate(route); 2347 return; 2348 } 2349 } 2350 2351 const io = self.session.io orelse return; 2352 const src = self.data.repo_source orelse return; 2353 const author = (try self.session.eventAuthor()) orelse return; 2354 2355 switch (src.repo_kind) { 2356 inline else => |repo_kind| { 2357 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2358 defer any_repo.deinit(io, allocator); 2359 switch (any_repo) { 2360 inline else => |*repo| try evt.remove(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2361 } 2362 }, 2363 } 2364 2365 const id_hex = std.fmt.bytesToHex(id, .lower); 2366 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 2367 try self.session.navigate(route); 2368 } 2369 2370 // flip the shown thread's status by re-emitting its event, then reload the 2371 // page rooted at the thread so the view reflects the change. this is the 2372 // terminal path; the web posts the button's form to the status route. 2373 fn toggleStatus(self: *This, allocator: std.mem.Allocator, index: usize) !void { 2374 if (comptime !has_status) return; 2375 if (comptime wasm) return; 2376 const io = self.session.io orelse return; 2377 const src = self.data.repo_source orelse return; 2378 const sel = self.detailed_index[index] orelse return; 2379 const entry = self.window(index).items[sel]; 2380 const author = (try self.session.eventAuthor()) orelse return; 2381 2382 const status = (statusChange(entryStatus(entry)) orelse return).status; 2383 2384 const id_bytes = try evt.parseEventId(entry.id); 2385 2386 switch (src.repo_kind) { 2387 inline else => |repo_kind| { 2388 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2389 defer any_repo.deinit(io, allocator); 2390 switch (any_repo) { 2391 inline else => |*repo| try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .status = status }, author), 2392 } 2393 }, 2394 } 2395 2396 const route = listRoute(self.data.identity, status, "", entry.id) orelse return; 2397 try self.session.navigate(route); 2398 } 2399 2400 // arrow keys move the tag selection; at the flow's edges focus crosses to 2401 // the neighboring widgets. 2402 fn tagsInput(self: *This, index: usize, child_index: usize, tf: *TagFlow, key: Key, root_focus: *Focus) void { 2403 const cid = tf.focus.child_id orelse return; 2404 const cur = tf.indexOfFocusId(cid) orelse return; 2405 const count = tf.text_boxes.items.len; 2406 switch (key) { 2407 .arrow_left => if (cur > 0) self.focusTag(index, child_index, tf, root_focus, cur - 1) else self.focusList(index, root_focus), 2408 .arrow_right => if (cur + 1 < count) self.focusTag(index, child_index, tf, root_focus, cur + 1), 2409 .arrow_up => if (tf.rowStep(cur, false)) |i| { 2410 self.focusTag(index, child_index, tf, root_focus, i); 2411 } else { 2412 _ = self.moveDetailVertical(index, root_focus, false); 2413 }, 2414 .arrow_down => if (tf.rowStep(cur, true)) |i| { 2415 self.focusTag(index, child_index, tf, root_focus, i); 2416 } else { 2417 _ = self.moveDetailVertical(index, root_focus, true); 2418 }, 2419 .home => self.focusTag(index, child_index, tf, root_focus, 0), 2420 .end => self.focusTag(index, child_index, tf, root_focus, count - 1), 2421 else => {}, 2422 } 2423 } 2424 2425 // the tool row's first child after the spacer that pushes it right 2426 const first_in_row_index: usize = 1; 2427 2428 fn toolRowFocused(self: *This, index: usize) bool { 2429 const outer = self.detailOuter(index); 2430 const cid = outer.getFocus().child_id orelse return false; 2431 return outer.children.getIndex(cid) == tool_row_index; 2432 } 2433 2434 fn focusTag(self: *This, index: usize, child_index: usize, tf: *TagFlow, root_focus: *Focus, item: usize) void { 2435 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 2436 // keep the tag visible on the terminal: its rect offset by the flow's 2437 // position in the pane. 2438 if (self.session.is_terminal and item < tf.rects.items.len) { 2439 if (self.detailInner(index).children.values()[child_index].rect) |flow_rect| { 2440 var rect = tf.rects.items[item]; 2441 rect.x += flow_rect.x; 2442 rect.y += flow_rect.y; 2443 self.detailScroll(index).scrollToRect(rect); 2444 } 2445 } 2446 } 2447 2448 // return to the tool row's last-focused button (the header when the 2449 // description page shows no row). 2450 fn focusToolRow(self: *This, index: usize, root_focus: *Focus) void { 2451 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 2452 root_focus.setFocus(cid); 2453 } 2454 2455 // enter the detail pane. an empty pane can't be entered. 2456 fn focusDetail(self: *This, index: usize, root_focus: *Focus) void { 2457 if (self.detailInner(index).children.count() == 0) return; 2458 root_focus.setFocus(self.detailOuter(index).getFocus().id); 2459 } 2460 2461 // return to the list. 2462 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2463 root_focus.setFocus(self.listScroll(index).getFocus().id); 2464 } 2465 2466 // cross to the header tabs above the stack. 2467 fn focusHeader(self: *This, root_focus: *Focus) void { 2468 root_focus.setFocus(self.box.children.keys()[header_index]); 2469 } 2470 2471 pub fn clearGrid(self: *This) void { 2472 self.box.clearGrid(); 2473 } 2474 2475 pub fn getGrid(self: This) ?Grid { 2476 return self.box.getGrid(); 2477 } 2478 2479 pub fn getFocus(self: *This) *Focus { 2480 return self.box.getFocus(); 2481 } 2482 2483 // for the parent's "scroll up at the top jumps to the header" check: at the 2484 // top only while the header tabs hold focus, so up from the split 2485 // crosses into the tabs first. 2486 pub fn getSelectedIndex(self: *This) ?usize { 2487 return if (self.headerActive()) 0 else 1; 2488 } 2489 }; 2490 } 2491