╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1418 .merge => "merge patch", 1419 } }; 1420 var button_count: usize = 1; 1421 if (confirmation_kind == .merge) { 1422 if (comptime supports_merge) { 1423 buttons[1] = .{ 1424 .label = "squash and merge patch", 1425 .action = try std.fmt.allocPrint(aa, "{s}{s}/squash", .{ ui.submit_action_prefix, page_url }), 1426 }; 1427 button_count += 1; 1428 } else unreachable; 1429 } 1430 var center = try initConfirmationForm(allocator, action, buttons[0..button_count]); 1431 errdefer center.deinit(allocator); 1432 try stack.children.put(allocator, center.getFocus().id, .{ .center = center }); 1433 } else if (data.view == .new_comment or data.view == .edit_comment) { 1434 const editing = data.view == .edit_comment; 1435 const route = if (editing) 1436 ui.RoutablePage.repoThreadCommentEditRoute(kind, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong 1437 else 1438 ui.RoutablePage.repoThreadCommentNewRoute(kind, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong; 1439 const page_url = try route.toUrl(session.page_arena); 1440 const action = try std.fmt.allocPrint(session.page_arena.allocator(), "form:{s}", .{page_url}); 1441 const page = data.comment_page; 1442 const top_level = if (page) |p| std.mem.eql(u8, &p.selected.comment.event.parent_id, &p.selected.comment.event.thread_id) else false; 1443 const parent_route = if (editing) 1444 if (top_level) 1445 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, data.selected_id, 0) 1446 else if (page) |p| 1447 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, data.selected_id, &p.selected.comment.event.parent_id, 0) 1448 else 1449 null 1450 else if (page) |p| 1451 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, data.selected_id, &p.selected.id, 0) 1452 else 1453 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, data.selected_id, 0); 1454 const parent_author: ui.Author = if (editing) 1455 if (top_level) 1456 if (data.selectedThread()) |entry| entry.author else .unknown 1457 else if (page) |p| 1458 p.selected.parent_author orelse .unknown 1459 else 1460 .unknown 1461 else if (page) |p| 1462 p.selected.author 1463 else if (data.selectedThread()) |entry| 1464 entry.author 1465 else 1466 .unknown; 1467 const initial_body: ?[]const u8 = if (editing) if (page) |p| p.selected.comment.event.body else null else null; 1468 var form = try initCommentForm(allocator, session, action, parent_author, parent_route orelse return error.RouteTooLong, initial_body); 1469 errdefer form.deinit(allocator); 1470 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 1471 } else if (supports_conflicts and data.view == .resolve) { 1472 // on the web the page grows to the form's height and the 1473 // browser scrolls it; the terminal scrolls the form itself 1474 var form_widget: Widget = blk: { 1475 var form = try initResolveForm(allocator, session, data); 1476 errdefer form.deinit(allocator); 1477 break :blk if (session.is_terminal) 1478 .{ .scroll = try wgt.Scroll(Widget).init(allocator, .{ .box = form }, .{ .direction = .vert }) } 1479 else 1480 .{ .box = form }; 1481 }; 1482 errdefer form_widget.deinit(allocator); 1483 try stack.children.put(allocator, form_widget.getFocus().id, form_widget); 1484 } else { 1485 const aa = session.page_arena.allocator(); 1486 const route = if (data.view == .edit) 1487 ui.RoutablePage.repoThreadEditRoute(kind, data.identity, data.selected_id) orelse return error.RouteTooLong 1488 else 1489 ui.RoutablePage.repoThreadNewRoute(kind, data.identity) orelse return error.RouteTooLong; 1490 const action = try std.fmt.allocPrint(aa, "form:{s}", .{try route.toUrl(session.page_arena)}); 1491 const record = if (data.view == .edit) 1492 (if (data.selectedThread()) |entry| &entry.record else null) 1493 else 1494 null; 1495 var form = try initThreadForm(allocator, session, action, record); 1496 errdefer form.deinit(allocator); 1497 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 1498 } 1499 } else { 1500 var message = try ui.Unauthorized.View.init(allocator); 1501 errdefer message.deinit(allocator); 1502 try stack.children.put(allocator, message.getFocus().id, .{ .unauthorized = message }); 1503 } 1504 1505 if (supports_drafts) { 1506 var split = try initSplit(allocator, session, data, null, true); 1507 errdefer split.deinit(allocator); 1508 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 1509 } 1510 1511 // the conflicts split; its tab exists only when the repo has 1512 // conflicts, so skip the child too to keep the stack 1:1 with the 1513 // tabs. 1514 if (supports_conflicts and data.conflicts.count > 0) { 1515 var split = try initSplit(allocator, session, data, null, false); 1516 errdefer split.deinit(allocator); 1517 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 1518 } 1519 1520 // the stack starts on the page's view. 1521 stack.getFocus().child_id = stack.children.keys()[viewIndex(data.view)]; 1522 1523 // focus entering the view lands on the tabs first. 1524 outer.getFocus().child_id = outer.children.keys()[header_index]; 1525 1526 return .{ 1527 .box = outer, 1528 .data = data, 1529 .session = session, 1530 }; 1531 } 1532 1533 // the master-detail split showing `status`'s window, or the conflicts 1534 // window when null. 1535 fn initSplit(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, status_maybe: ?Status, drafts: bool) !wgt.Box(Widget) { 1536 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1537 errdefer box.deinit(allocator); 1538 1539 const win = if (status_maybe) |status| data.window(status) else if (drafts and supports_drafts) &data.drafts else if (supports_conflicts) &data.conflicts else unreachable; 1540 1541 // the thread list (one focusable row per title), plus a "next" link that 1542 // reloads the page rooted at the following thread. 1543 { 1544 var list_scroll = blk: { 1545 var list_box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert, .stretch = true }); 1546 errdefer list_box.deinit(allocator); 1547 if (win.prev_id) |prev| 1548 try addRow(allocator, &list_box, "← previous", "", try windowLink(session.page_arena, data, status_maybe, drafts, prev)); 1549 for (win.items) |entry| { 1550 // conflicts take precedence over the comment count 1551 const label: []const u8 = if (entryConflicted(entry) and status_maybe != null) 1552 " conflict " 1553 else if (entry.comments.count > 0) 1554 try std.fmt.allocPrint(session.page_arena.allocator(), " {d} comments ", .{entry.comments.count}) 1555 else 1556 ""; 1557 try addRow(allocator, &list_box, entry.record.event.title, label, try rowLink(session.page_arena, data, entry.id)); 1558 } 1559 if (win.next_id) |next| 1560 try addRow(allocator, &list_box, "next →", "", try windowLink(session.page_arena, data, status_maybe, drafts, next)); 1561 // select the window's first thread (past a leading "previous" 1562 // row) so its description shows on load. 1563 if (win.items.len > 0) 1564 list_box.getFocus().child_id = list_box.children.keys()[if (win.prev_id != null) 1 else 0] 1565 else if (list_box.children.count() > 0) 1566 list_box.getFocus().child_id = list_box.children.keys()[0]; 1567 break :blk try wgt.Scroll(Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal }); 1568 }; 1569 errdefer list_scroll.deinit(allocator); 1570 try box.children.put(allocator, list_scroll.getFocus().id, .{ .widget = .{ .scroll = list_scroll }, .rect = null, .min_size = .{ .width = list_max_width, .height = null }, .max_size = .{ .width = list_max_width, .height = null } }); 1571 } 1572 1573 // frame the detail pane inside the master-detail split 1574 { 1575 var frame = try wgt.Box(Widget).init(allocator, .{ .border_style = .hidden, .direction = .vert }); 1576 errdefer frame.deinit(allocator); 1577 var detail_view = try DetailType.init(allocator, data, session, null, .{}); 1578 errdefer detail_view.deinit(allocator); 1579 frame.getFocus().child_id = detail_view.getFocus().id; 1580 try frame.children.put(allocator, detail_view.getFocus().id, .{ .widget = @unionInit(Widget, Data.detail_widget_name, detail_view), .rect = null, .min_size = null }); 1581 try box.children.put(allocator, frame.getFocus().id, .{ .widget = .{ .box = frame }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 1582 } 1583 1584 box.getFocus().child_id = box.children.keys()[list_index]; 1585 return box; 1586 } 1587 1588 // a thread form: title/tags/description inputs and a submit button, 1589 // prefilled from `record` when given. its form: subtree makes the web 1590 // renderer POST them to `action`'s route. 1591 fn initThreadForm(allocator: std.mem.Allocator, session: *ui.Session, action: []const u8, record: ?*const Event.Record) !wgt.Box(Widget) { 1592 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1593 errdefer box.deinit(allocator); 1594 box.getFocus().kind = .{ .custom = action }; 1595 1596 { 1597 var title = try wgt.TextInput.init(allocator, .{ .label = " title ", .name = "title", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 1598 errdefer title.deinit(allocator); 1599 title.getFocus().focusable = true; 1600 if (record) |r| try title.setContent(allocator, r.event.title); 1601 try box.children.put(allocator, title.getFocus().id, .{ .widget = .{ .text_input = title }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1602 } 1603 1604 { 1605 var tags = try wgt.TextInput.init(allocator, .{ .label = " tags (separate with spaces) ", .name = "tags", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 1606 errdefer tags.deinit(allocator); 1607 tags.getFocus().focusable = true; 1608 if (record) |r| try tags.setContent(allocator, r.event.tags); 1609 try box.children.put(allocator, tags.getFocus().id, .{ .widget = .{ .text_input = tags }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1610 } 1611 1612 { 1613 var description = try wgt.TextInput.init(allocator, .{ .label = " description ", .name = "description", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal, .multiline = true, .scroll = .{ .fill = true } }); 1614 errdefer description.deinit(allocator); 1615 description.getFocus().focusable = true; 1616 if (record) |r| try description.setContent(allocator, r.event.description); 1617 try box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .text_input = description }, .rect = null, .min_size = null }); 1618 } 1619 1620 try addSubmitButtonLabeled(allocator, &box, if (supports_drafts and record == null) "submit draft" else "submit"); 1621 1622 box.getFocus().child_id = box.children.keys()[title_field_index]; 1623 return box; 1624 } 1625 1626 fn initCommentForm(allocator: std.mem.Allocator, session: *ui.Session, action: []const u8, author: ui.Author, parent_route: ui.RoutablePage, initial_body: ?[]const u8) !wgt.Box(Widget) { 1627 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1628 errdefer box.deinit(allocator); 1629 box.getFocus().kind = .{ .custom = action }; 1630 1631 { 1632 var parent = try ui.authorBox(allocator, session.page_arena, author); 1633 errdefer parent.deinit(allocator); 1634 parent.options.label = " replying to "; 1635 parent.getFocus().kind = .{ .custom = try std.fmt.allocPrint(session.page_arena.allocator(), "a:{s}", .{try parent_route.toUrl(session.page_arena)}) }; 1636 try box.children.put(allocator, parent.getFocus().id, .{ .widget = .{ .text_box = parent }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1637 } 1638 1639 { 1640 var body = try wgt.TextInput.init(allocator, .{ 1641 .label = " comment ", 1642 .name = "body", 1643 .visible_width = null, 1644 .rounded_corners = true, 1645 .render_content = session.is_terminal, 1646 .multiline = true, 1647 .visible_height = 5, 1648 .scroll = .{ .fill = true }, 1649 }); 1650 errdefer body.deinit(allocator); 1651 body.getFocus().focusable = true; 1652 if (initial_body) |text| try body.setContent(allocator, text); 1653 try box.children.put(allocator, body.getFocus().id, .{ .widget = .{ .text_input = body }, .rect = null, .min_size = null }); 1654 } 1655 1656 try addSubmitButtonLabeled(allocator, &box, "submit"); 1657 box.getFocus().child_id = box.children.keys()[comment_author_field_index]; 1658 return box; 1659 } 1660 1661 const ConfirmationButton = struct { 1662 label: []const u8, 1663 action: []const u8 = "submit", 1664 }; 1665 1666 fn initConfirmationForm(allocator: std.mem.Allocator, action: []const u8, buttons: []const ConfirmationButton) !Center { 1667 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .rounded_corners = true, .direction = .vert }); 1668 errdefer box.deinit(allocator); 1669 box.getFocus().kind = .{ .custom = action }; 1670 1671 var prompt = try wgt.Text.init(allocator, "are you sure?"); 1672 errdefer prompt.deinit(allocator); 1673 try box.children.put(allocator, prompt.getFocus().id, .{ .widget = .{ .text = prompt }, .rect = null, .min_size = null }); 1674 1675 for (buttons, 0..) |options, index| { 1676 var button = try wgt.TextBox.init(allocator, options.label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1677 errdefer button.deinit(allocator); 1678 button.getFocus().focusable = true; 1679 button.getFocus().kind = .{ .custom = options.action }; 1680 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 1681 if (index == 0) box.getFocus().child_id = button.getFocus().id; 1682 } 1683 1684 return Center.init(allocator, .{ .box = box }); 1685 } 1686 1687 // a form's submit button, then a spacer absorbing the leftover 1688 // min-height the box hands its last child, so the button keeps its 1689 // natural height 1690 fn addSubmitButtonLabeled(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8) !void { 1691 { 1692 var submit = try SubmitButton.initLabeled(allocator, label); 1693 errdefer submit.deinit(allocator); 1694 try box.children.put(allocator, submit.getFocus().id, .{ .widget = .{ .submit_button = submit }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1695 } 1696 { 1697 var spacer = try Spacer.init(allocator); 1698 errdefer spacer.deinit(allocator); 1699 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1700 } 1701 } 1702 1703 // the conflict resolve form. the "use this" links are navigations that 1704 // flip the url's theirs: pick, reloading the prefills; one submit 1705 // settles every conflict. the form: subtree makes the web renderer POST 1706 // to the resolve route. 1707 fn initResolveForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !wgt.Box(Widget) { 1708 const aa = session.page_arena.allocator(); 1709 // init only picks the resolve view once it has read the conflict 1710 const conflict = if (data.conflict) |*c| c else unreachable; 1711 1712 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1713 errdefer box.deinit(allocator); 1714 const route = resolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong; 1715 box.getFocus().kind = .{ .custom = try std.fmt.allocPrint(aa, "form:{s}", .{try route.toUrl(session.page_arena)}) }; 1716 1717 if (conflict.title) |*fc| { 1718 try addLabel(allocator, &box, "title conflict:"); 1719 try addFieldConflict(allocator, &box, session, data, "title", fc); 1720 } 1721 if (conflict.tags) |*fc| { 1722 try addLabel(allocator, &box, "tags conflict:"); 1723 try addFieldConflict(allocator, &box, session, data, "tags", fc); 1724 } 1725 if (comptime @hasField(Data.Conflict, "status")) { 1726 if (conflict.status) |*fc| { 1727 try addLabel(allocator, &box, "status conflict:"); 1728 try addAtomicConflict(allocator, &box, session, data, "status", fc); 1729 } 1730 } 1731 if (comptime @hasField(Data.Conflict, "revision")) { 1732 if (conflict.revision) |*fc| { 1733 try addLabel(allocator, &box, "revision conflict:"); 1734 try addAtomicConflict(allocator, &box, session, data, "revision", fc); 1735 } 1736 } 1737 1738 if (conflict.description) |*desc| { 1739 try addLabel(allocator, &box, "description conflict:"); 1740 var hunk_index: usize = 0; 1741 for (desc.chunks, 0..) |*chunk, chunk_index| { 1742 switch (chunk.*) { 1743 .same => |text| { 1744 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word }); 1745 errdefer tb.deinit(allocator); 1746 tb.getFocus().focusable = true; 1747 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1748 }, 1749 // an auto-resolved chunk: only one side changed it, so its 1750 // version stands, shown distinctly with its author 1751 .auto => |auto| { 1752 const author = if (auto.theirs) desc.theirs_author else desc.ours_author; 1753 const verb: []const u8 = if (auto.text == null) "removed" else "edited"; 1754 const label = switch (author) { 1755 .user_name, .email => |name| try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, name }), 1756 .unknown => try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, if (auto.theirs) @as([]const u8, "them") else "us" }), 1757 }; 1758 var tb = try wgt.TextBox.init(allocator, auto.text orelse "(removed)", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = label }); 1759 errdefer tb.deinit(allocator); 1760 tb.getFocus().focusable = true; 1761 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1762 }, 1763 .conflict => |hunk| { 1764 // a blank row on each side sets the group apart from 1765 // the flowing shared and auto-resolved chunks 1766 if (chunk_index > 0) try addGap(allocator, &box); 1767 const name = try std.fmt.allocPrint(aa, "d{d}", .{hunk_index}); 1768 hunk_index += 1; 1769 const picked_theirs = data.theirsPicked(name); 1770 try addVersionRow(allocator, &box, try sideLabel(aa, desc.ours_author, true), hunk.ours orelse "", try useThisLink(session, data, name, false)); 1771 try addVersionRow(allocator, &box, try sideLabel(aa, desc.theirs_author, false), hunk.theirs orelse "", try useThisLink(session, data, name, true)); 1772 var resolution_input = try wgt.TextInput.init(allocator, .{ .label = " resolution ", .name = name, .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal, .multiline = true }); 1773 errdefer resolution_input.deinit(allocator); 1774 resolution_input.getFocus().focusable = true; 1775 try resolution_input.setContent(allocator, (if (picked_theirs) hunk.theirs else hunk.ours) orelse ""); 1776 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 1777 if (chunk_index + 1 < desc.chunks.len) try addGap(allocator, &box); 1778 }, 1779 } 1780 } 1781 } 1782 1783 try addSubmitButtonLabeled(allocator, &box, "submit"); 1784 1785 box.getFocus().child_id = box.children.keys()[0]; 1786 return box; 1787 } 1788 1789 // one conflicted scalar field: both sides' version rows, then the 1790 // resolution input prefilled from the picked side. 1791 fn addFieldConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 1792 const aa = session.page_arena.allocator(); 1793 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 1794 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 1795 1796 var resolution_input = try wgt.TextInput.init(allocator, .{ .label = " resolution ", .name = name, .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 1797 errdefer resolution_input.deinit(allocator); 1798 resolution_input.getFocus().focusable = true; 1799 try resolution_input.setContent(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text); 1800 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 1801 } 1802 1803 // atomic fields can only pick one complete side 1804 fn addAtomicConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 1805 const aa = session.page_arena.allocator(); 1806 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 1807 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 1808 var selected = try wgt.TextBox.init(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text, .{ 1809 .border_style = .single, 1810 .rounded_corners = true, 1811 .wrap_kind = .word, 1812 .label = " resolution ", 1813 }); 1814 errdefer selected.deinit(allocator); 1815 selected.getFocus().focusable = true; 1816 try box.children.put(allocator, selected.getFocus().id, .{ .widget = .{ .text_box = selected }, .rect = null, .min_size = null }); 1817 } 1818 1819 // a blank row setting a conflict group apart from its neighbors 1820 fn addGap(allocator: std.mem.Allocator, box: *wgt.Box(Widget)) !void { 1821 var text = try wgt.Text.init(allocator, " "); 1822 errdefer text.deinit(allocator); 1823 try box.children.put(allocator, text.getFocus().id, .{ .widget = .{ .text = text }, .rect = null, .min_size = null }); 1824 } 1825 1826 fn addLabel(allocator: std.mem.Allocator, box: *wgt.Box(Widget), content: []const u8) !void { 1827 var label = try SectionLabel.init(allocator, content); 1828 errdefer label.deinit(allocator); 1829 try box.children.put(allocator, label.getFocus().id, .{ .widget = .{ .section_label = label }, .rect = null, .min_size = null }); 1830 } 1831 1832 // one side of a conflict: the "use this" link on the left, the version 1833 // itself as a labeled read-only box. 1834 fn addVersionRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8, text: []const u8, link: []const u8) !void { 1835 var row = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1836 errdefer row.deinit(allocator); 1837 1838 { 1839 const use_label = "use this"; 1840 var use = try wgt.TextBox.init(allocator, use_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1841 errdefer use.deinit(allocator); 1842 use.getFocus().focusable = true; 1843 use.getFocus().kind = .{ .custom = link }; 1844 try row.children.put(allocator, use.getFocus().id, .{ 1845 .widget = .{ .text_box = use }, 1846 .rect = null, 1847 .min_size = .{ .width = use_label.len + 2, .height = null }, 1848 .max_size = .{ .width = use_label.len + 2, .height = null }, 1849 }); 1850 } 1851 1852 { 1853 var tb = try wgt.TextBox.init(allocator, if (text.len > 0) text else "(removed)", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = label }); 1854 errdefer tb.deinit(allocator); 1855 tb.getFocus().focusable = true; 1856 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1857 } 1858 1859 row.getFocus().child_id = row.children.keys()[0]; 1860 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 1861 } 1862 1863 // the label of one side's version box, naming its author 1864 fn sideLabel(aa: std.mem.Allocator, author: ui.Author, ours: bool) ![]const u8 { 1865 return switch (author) { 1866 .user_name, .email => |name| try std.fmt.allocPrint(aa, " edited by {s} ", .{name}), 1867 .unknown => if (ours) " current version " else " their version ", 1868 }; 1869 } 1870 1871 // the a: link that reloads the resolve page with `name` picked from ours 1872 // or theirs, reprefilling its resolution input 1873 fn useThisLink(session: *ui.Session, data: *const Self, name: []const u8, theirs: bool) ![]const u8 { 1874 const aa = session.page_arena.allocator(); 1875 var picks: std.ArrayList(u8) = .empty; 1876 var it = std.mem.splitScalar(u8, data.theirs_picks, ','); 1877 while (it.next()) |pick| { 1878 if (pick.len == 0 or std.mem.eql(u8, pick, name)) continue; 1879 if (picks.items.len > 0) try picks.append(aa, ','); 1880 try picks.appendSlice(aa, pick); 1881 } 1882 if (theirs) { 1883 if (picks.items.len > 0) try picks.append(aa, ','); 1884 try picks.appendSlice(aa, name); 1885 } 1886 const route = resolveRoute(data.identity, data.selected_id, picks.items) orelse return error.RouteTooLong; 1887 return std.fmt.allocPrint(aa, "a:{s}", .{try route.toUrl(session.page_arena)}); 1888 } 1889 1890 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), text: []const u8, bottom_label: []const u8, link: []const u8) !void { 1891 var row = try wgt.TextBox.init(allocator, text, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word, .bottom_label = bottom_label }); 1892 errdefer row.deinit(allocator); 1893 row.getFocus().focusable = true; 1894 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 1895 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 1896 } 1897 1898 pub fn deinit(self: *This, allocator: std.mem.Allocator) void { 1899 self.box.deinit(allocator); 1900 } 1901 1902 fn header(self: *This) *HeaderType { 1903 return &@field(self.box.children.values()[header_index].widget, Data.header_widget_name); 1904 } 1905 1906 fn viewStack(self: *This) *wgt.Stack(Widget) { 1907 return &self.box.children.values()[stack_index].widget.stack; 1908 } 1909 1910 // `index`'s master-detail split inside the stack. 1911 fn resultsBox(self: *This, index: usize) *wgt.Box(Widget) { 1912 return &self.viewStack().children.values()[index].box; 1913 } 1914 1915 // the tags view's flow inside the stack. 1916 fn tagsView(self: *This) *TagFlow { 1917 return &self.viewStack().children.values()[tags_view_index].tag_flow; 1918 } 1919 1920 // the new-thread, edit, or resolve form inside the stack, or null when the 1921 // unauthorized view stands in for it. 1922 fn threadForm(self: *This) ?*wgt.Box(Widget) { 1923 return switch (self.viewStack().children.values()[form_view_index]) { 1924 .box => |*box| box, 1925 // the resolve form sits inside a scroll on the terminal 1926 .scroll => |*scroll| &scroll.child.box, 1927 else => null, 1928 }; 1929 } 1930 1931 fn confirmationForm(self: *This) ?*wgt.Box(Widget) { 1932 return switch (self.viewStack().children.values()[form_view_index]) { 1933 .center => |*center| switch (center.child.*) { 1934 .box => |*box| box, 1935 else => null, 1936 }, 1937 else => null, 1938 }; 1939 } 1940 1941 // the resolve form's scroll on the terminal (the web page scrolls itself) 1942 fn resolveScroll(self: *This) ?*wgt.Scroll(Widget) { 1943 return switch (self.viewStack().children.values()[form_view_index]) { 1944 .scroll => |*scroll| scroll, 1945 else => null, 1946 }; 1947 } 1948 1949 fn listScroll(self: *This, index: usize) *wgt.Scroll(Widget) { 1950 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 1951 } 1952 1953 fn listBox(self: *This, index: usize) *wgt.Box(Widget) { 1954 return &self.listScroll(index).child.box; 1955 } 1956 1957 fn detail(self: *This, index: usize) *DetailType { 1958 const frame = &self.resultsBox(index).children.values()[detail_index].widget.box; 1959 return &@field(frame.children.values()[0].widget, Data.detail_widget_name); 1960 } 1961 1962 fn window(self: *This, index: usize) *const Window { 1963 if (supports_conflicts and index == conflict_view_index) return &self.data.conflicts; 1964 if (supports_drafts and index == drafts_view_index) return &self.data.drafts; 1965 return self.data.window(splitStatus(index)); 1966 } 1967 1968 fn stackSelectedIndex(self: *This) ?usize { 1969 const stack = self.viewStack(); 1970 const cid = stack.getFocus().child_id orelse return null; 1971 return stack.children.getIndex(cid); 1972 } 1973 1974 // the stack's selected master-detail split (null when the tags view or the 1975 // new-thread form shows). 1976 fn selectedSplitIndex(self: *This) ?usize { 1977 const idx = self.stackSelectedIndex() orelse return null; 1978 return if (idx < status_count or (supports_drafts and idx == drafts_view_index) or idx == conflict_view_index) idx else null; 1979 } 1980 1981 fn detailActive(self: *This, index: usize) bool { 1982 const rb = self.resultsBox(index); 1983 const cid = rb.getFocus().child_id orelse return false; 1984 return rb.children.getIndex(cid) == detail_index; 1985 } 1986 1987 fn headerActive(self: *This) bool { 1988 const cid = self.box.getFocus().child_id orelse return false; 1989 return self.box.children.getIndex(cid) == header_index; 1990 } 1991 1992 fn tagsViewActive(self: *This) bool { 1993 if (self.headerActive()) return false; 1994 return self.stackSelectedIndex() == tags_view_index; 1995 } 1996 1997 fn formViewActive(self: *This) bool { 1998 if (self.headerActive()) return false; 1999 return self.stackSelectedIndex() == form_view_index; 2000 } 2001 2002 // the selected thread's index, or null when a window-navigation row is 2003 // selected (a leading "previous" row shifts the thread rows down by one). 2004 fn selectedThreadIndex(self: *This, index: usize) ?usize { 2005 const lb = self.listBox(index); 2006 const cid = lb.getFocus().child_id orelse return null; 2007 const idx = lb.children.getIndex(cid) orelse return null; 2008 const win = self.window(index); 2009 const lead: usize = if (win.prev_id != null) 1 else 0; 2010 if (idx < lead or idx - lead >= win.items.len) return null; 2011 return idx - lead; 2012 } 2013 2014 pub fn build(self: *This, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2015 self.clearGrid(); 2016 2017 // each header tab maps 1:1 to a stack child by position 2018 if (self.header().getSelectedIndex()) |index| { 2019 const stack = self.viewStack(); 2020 stack.getFocus().child_id = stack.children.keys()[index]; 2021 } 2022 2023 if (self.selectedSplitIndex()) |i| { 2024 // swap the detail pane to the selected thread when it changes. 2025 if (self.selectedThreadIndex(i)) |selected| { 2026 const entry = self.window(i).items[selected]; 2027 const changed = if (self.detail(i).entry) |current| !std.mem.eql(u8, current.id, entry.id) else true; 2028 if (changed) { 2029 try self.detail(i).setEntry(allocator, entry); 2030 } 2031 } 2032 2033 // the selected list row shows a border (the focused TextBox 2034 // upgrades it to a double border itself); the rest stay borderless. 2035 const lb = self.listBox(i); 2036 for (lb.children.keys(), lb.children.values()) |id, *child| { 2037 switch (child.widget) { 2038 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 2039 else => {}, 2040 } 2041 } 2042 2043 // cap the list at list_max_width only while the detail pane fits 2044 // beside it. the box drops the detail when the width can't hold 2045 // both minimums, so when it's that narrow we lift the cap and let 2046 // the list fill the whole width. 2047 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 2048 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 2049 2050 // stretch the detail pane across the rest of the width so it fills 2051 // the area rather than shrinking to its content; its scroll fills 2052 // the pane. 2053 if (constraint.max_size.width) |w| { 2054 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 2055 } else { 2056 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 2057 } 2058 } 2059 2060 // refresh the form inputs' entries in the session's focus-id -> input 2061 // map with this frame's addresses, so the web/wasm form handling can 2062 // find them by focus id 2063 if (self.threadForm()) |form| { 2064 const inputs_arena = self.session.arena.allocator(); 2065 for (form.children.values()) |*child| switch (child.widget) { 2066 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 2067 else => {}, 2068 }; 2069 } 2070 2071 try self.box.build(allocator, constraint, root_focus); 2072 } 2073 2074 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 2075 // wheel input moves focus like the arrows 2076 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 2077 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 2078 else 2079 raw_key; 2080 2081 if (self.headerActive()) { 2082 // down from the tabs re-enters the stack if the selected view has 2083 // something to focus; other keys move the tabs. 2084 if (inp.vertDirection(key) == .down) { 2085 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 2086 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 2087 // nothing focusable in the unauthorized view 2088 .unauthorized => false, 2089 else => true, 2090 } else false; 2091 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 2092 } else { 2093 try self.header().input(allocator, key, root_focus); 2094 } 2095 return; 2096 } 2097 if (self.tagsViewActive()) { 2098 self.tagsViewInput(key, root_focus); 2099 return; 2100 } 2101 if (self.formViewActive()) { 2102 if (confirmation(self.data.view) != null) { 2103 try self.confirmationFormInput(allocator, key, root_focus); 2104 } else if (supports_conflicts and self.data.view == .resolve) { 2105 try self.resolveInput(allocator, key, root_focus); 2106 } else if (self.data.view == .new_comment or self.data.view == .edit_comment) { 2107 try self.commentFormInput(allocator, key, root_focus); 2108 } else { 2109 try self.formInput(allocator, key, root_focus); 2110 } 2111 return; 2112 } 2113 const i = self.selectedSplitIndex() orelse return; 2114 if (self.detailActive(i)) { 2115 switch (try self.detail(i).inputWithExit(allocator, key, root_focus)) { 2116 .none => {}, 2117 .list => self.focusList(i, root_focus), 2118 .header => self.focusHeader(root_focus), 2119 } 2120 } else { 2121 self.listInput(i, key, root_focus); 2122 } 2123 } 2124 2125 // arrow keys move the tag selection; up from the top row crosses to the 2126 // header tabs. 2127 fn tagsViewInput(self: *This, key: Key, root_focus: *Focus) void { 2128 const tf = self.tagsView(); 2129 const cid = tf.focus.child_id orelse return; 2130 const cur = tf.indexOfFocusId(cid) orelse return; 2131 const count = tf.text_boxes.items.len; 2132 switch (key) { 2133 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 2134 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 2135 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 2136 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 2137 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 2138 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 2139 else => {}, 2140 } 2141 } 2142 2143 fn listInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 2144 // up/down (and the scroll wheel) move the selection a row; page up/down 2145 // jump a fixed amount. right/Enter cross into the detail pane. up from 2146 // the top row crosses into the header tabs. 2147 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 2148 const lb = self.listBox(index); 2149 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 2150 if (delta < 0 and at_top) return self.focusHeader(root_focus); 2151 moveRowFocus(lb, self.listScroll(index), root_focus, delta); 2152 return; 2153 } 2154 switch (key) { 2155 .enter, .arrow_right => _ = self.detail(index).focusFirst(root_focus), 2156 else => {}, 2157 } 2158 } 2159 2160 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2161 const form = self.threadForm() orelse return; 2162 const cid = form.getFocus().child_id orelse return; 2163 const cur = form.children.getIndex(cid) orelse return; 2164 2165 if (cur == description_field_index) { 2166 const child = &form.children.values()[cur]; 2167 if (try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2168 return child.widget.input(allocator, key, root_focus); 2169 } 2170 2171 switch (key) { 2172 .arrow_up, .back_tab => if (cur > 0) 2173 root_focus.setFocus(form.children.keys()[cur - 1]) 2174 else 2175 self.focusHeader(root_focus), 2176 .arrow_down, .tab => if (cur < submit_field_index) { 2177 root_focus.setFocus(form.children.keys()[cur + 1]); 2178 }, 2179 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 2180 .mouse => |mouse| if (cur == submit_field_index) { 2181 const submit = &form.children.values()[cur].widget.submit_button; 2182 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitForm(allocator); 2183 }, 2184 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 2185 } 2186 } 2187 2188 fn commentFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2189 const form = self.threadForm() orelse return; 2190 const cid = form.getFocus().child_id orelse return; 2191 const cur = form.children.getIndex(cid) orelse return; 2192 const child = &form.children.values()[cur]; 2193 2194 if (cur == comment_body_field_index and try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2195 return child.widget.input(allocator, key, root_focus); 2196 2197 switch (key) { 2198 .arrow_up, .back_tab => if (cur > comment_author_field_index) 2199 root_focus.setFocus(form.children.keys()[cur - 1]) 2200 else 2201 self.focusHeader(root_focus), 2202 .arrow_down, .tab => if (cur < comment_submit_field_index) 2203 root_focus.setFocus(form.children.keys()[cur + 1]), 2204 .enter => if (cur == comment_submit_field_index) try self.submitComment(allocator), 2205 .mouse => |mouse| if (cur == comment_submit_field_index) { 2206 const submit = &child.widget.submit_button; 2207 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitComment(allocator); 2208 }, 2209 else => try child.widget.input(allocator, key, root_focus), 2210 } 2211 } 2212 2213 fn confirmationFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2214 const form = self.confirmationForm() orelse return; 2215 const child_id = form.getFocus().child_id orelse return; 2216 const index = form.children.getIndex(child_id) orelse return; 2217 switch (key) { 2218 .arrow_up, .back_tab => if (index > 1) 2219 root_focus.setFocus(form.children.keys()[index - 1]) 2220 else 2221 self.focusHeader(root_focus), 2222 .arrow_down, .tab => if (index + 1 < form.children.count()) root_focus.setFocus(form.children.keys()[index + 1]), 2223 .enter => try self.submitConfirmation(allocator, index), 2224 .mouse => |mouse| for (form.children.keys()[1..], 1..) |button_id, button_index| { 2225 if (inp.leftClickOn(root_focus, button_id, mouse)) { 2226 try self.submitConfirmation(allocator, button_index); 2227 break; 2228 } 2229 }, 2230 else => {}, 2231 } 2232 } 2233 2234 // the multiline hunk inputs keep enter and any up/down that has a row to 2235 // move to; enter on a "use this" link is a navigation the host follows 2236 // before this runs. 2237 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2238 const form = self.threadForm() orelse return; 2239 const cid = form.getFocus().child_id orelse return; 2240 const cur = form.children.getIndex(cid) orelse return; 2241 const child = &form.children.values()[cur]; 2242 2243 if (child.widget == .text_input) { 2244 const ti = &child.widget.text_input; 2245 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2246 return child.widget.input(allocator, key, root_focus); 2247 } 2248 2249 const on_submit = child.widget == .submit_button; 2250 2251 switch (key) { 2252 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2253 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2254 .arrow_left, .arrow_right => switch (child.widget) { 2255 .box => |*row| { 2256 const row_cid = row.getFocus().child_id orelse return; 2257 const row_index = row.children.getIndex(row_cid) orelse return; 2258 if (key == .arrow_left) { 2259 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 2260 } else if (row_index + 1 < row.children.count()) { 2261 root_focus.setFocus(row.children.keys()[row_index + 1]); 2262 } 2263 }, 2264 else => try child.widget.input(allocator, key, root_focus), 2265 }, 2266 .enter => if (on_submit) try self.submitResolution(allocator), 2267 .mouse => |mouse| if (on_submit) { 2268 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2269 }, 2270 else => try child.widget.input(allocator, key, root_focus), 2271 } 2272 } 2273 2274 // a multiline input keeps enter and any up/down that has a row to move to 2275 fn multilineKeepsKey(text_input: *wgt.TextInput, allocator: std.mem.Allocator, key: Key) !bool { 2276 return switch (key) { 2277 .enter => true, 2278 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2279 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2280 else => false, 2281 }; 2282 } 2283 2284 // the neighboring focusable resolve-form child, skipping the spacer and 2285 // the blank gap rows 2286 fn resolveStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2287 var i = cur; 2288 while (true) { 2289 if (down) { 2290 i += 1; 2291 if (i >= form.children.count()) return null; 2292 } else { 2293 if (i == 0) return null; 2294 i -= 1; 2295 } 2296 switch (form.children.values()[i].widget) { 2297 .spacer, .text => continue, 2298 else => return i, 2299 } 2300 } 2301 } 2302 2303 // focus the resolve-form child at `i` (a version row focuses its selected 2304 // child) and keep it visible on the terminal 2305 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2306 const child = &form.children.values()[i]; 2307 switch (child.widget) { 2308 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2309 else => root_focus.setFocus(form.children.keys()[i]), 2310 } 2311 if (self.session.is_terminal) { 2312 if (self.resolveScroll()) |sc| { 2313 if (child.rect) |rect| sc.scrollToRect(rect); 2314 } 2315 } 2316 } 2317 2318 // re-emit the conflicted thread's event with the resolved content; any 2319 // event on the thread settles its conflict entry. this is the terminal 2320 // path; the web posts the form to the resolve route. 2321 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2322 if (comptime wasm) return; 2323 const io = self.session.io orelse return; 2324 const src = self.data.repo_source orelse return; 2325 const entry = self.data.selectedThread() orelse return; 2326 const author = (try self.session.eventAuthor()) orelse return; 2327 const form = self.threadForm() orelse return; 2328 2329 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2330 var title: ?[]u8 = null; 2331 var tags: ?[]u8 = null; 2332 var hunks: std.ArrayList([]const u8) = .empty; 2333 defer { 2334 if (title) |t| allocator.free(t); 2335 if (tags) |t| allocator.free(t); 2336 for (hunks.items) |h| allocator.free(h); 2337 hunks.deinit(allocator); 2338 } 2339 for (form.children.values()) |*child| switch (child.widget) { 2340 .text_input => |*ti| { 2341 const text = try ti.text(allocator); 2342 errdefer allocator.free(text); 2343 if (std.mem.eql(u8, ti.options.name, "title")) { 2344 title = text; 2345 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2346 tags = text; 2347 } else { 2348 try hunks.append(allocator, text); 2349 } 2350 }, 2351 else => {}, 2352 }; 2353 2354 const id_bytes = try evt.parseEventId(entry.id); 2355 2356 switch (src.repo_kind) { 2357 inline else => |repo_kind| { 2358 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2359 defer any_repo.deinit(io, allocator); 2360 switch (any_repo) { 2361 inline else => |*repo| { 2362 var resolution: Event.Resolve = .{ 2363 .title = title, 2364 .tags = tags, 2365 .hunks = hunks.items, 2366 }; 2367 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2368 const change = Event.Update{ .resolve = resolution }; 2369 Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2370 // leave the form up for correction 2371 error.InvalidFields => return, 2372 else => |e| return e, 2373 }; 2374 }, 2375 } 2376 }, 2377 } 2378 2379 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2380 try self.session.navigate(route); 2381 } 2382 2383 // commit a new or edited comment and navigate to its permalink. this is the 2384 // terminal path; the web posts the form to its comment route. 2385 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2386 if (comptime wasm) return; 2387 const io = self.session.io orelse return; 2388 const src = self.data.repo_source orelse return; 2389 const author = (try self.session.eventAuthor()) orelse return; 2390 const form = self.threadForm() orelse return; 2391 const body_input = &form.children.values()[comment_body_field_index].widget.text_input; 2392 const body = try body_input.text(allocator); 2393 defer allocator.free(body); 2394 if (!evt.Comment.fieldsValid(body)) return; 2395 2396 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2397 @memcpy(&thread_id, self.data.selected_id); 2398 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2399 var parent: [evt.event_id_size * 2]u8 = undefined; 2400 @memcpy(&parent, self.data.comment_id); 2401 break :blk parent; 2402 }; 2403 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2404 const editing = self.data.view == .edit_comment; 2405 switch (src.repo_kind) { 2406 inline else => |repo_kind| { 2407 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2408 defer any_repo.deinit(io, allocator); 2409 switch (any_repo) { 2410 inline else => |*repo| if (editing) { 2411 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2412 try evt.Comment.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2413 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2414 } else { 2415 event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2416 }, 2417 } 2418 }, 2419 } 2420 2421 body_input.clear(allocator); 2422 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2423 try self.session.navigate(route); 2424 } 2425 2426 // the terminal submit paths: the new form commits a new thread event, the 2427 // edit form re-emits the selected thread's event with the form's content. 2428 // the web posts the forms to their routes instead. 2429 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2430 if (self.data.view == .edit) { 2431 try self.submitEditedThread(allocator); 2432 } else { 2433 try self.submitNewThread(allocator); 2434 } 2435 } 2436 2437 // commit the new thread to the repo's events branch and navigate to it. 2438 // this is the terminal path; the web posts the form to the thread route, 2439 // so the wasm side never runs (or compiles) the repo access below. 2440 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2441 if (comptime wasm) return; 2442 const io = self.session.io orelse return; 2443 const src = self.data.repo_source orelse return; 2444 const author = (try self.session.eventAuthor()) orelse return; 2445 2446 const form = self.threadForm() orelse return; 2447 const title_input = &form.children.values()[title_field_index].widget.text_input; 2448 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2449 const description_input = &form.children.values()[description_field_index].widget.text_input; 2450 2451 const title = try title_input.text(allocator); 2452 defer allocator.free(title); 2453 const tags = try tags_input.text(allocator); 2454 defer allocator.free(tags); 2455 const description = try description_input.text(allocator); 2456 defer allocator.free(description); 2457 2458 if (!Event.fieldsValid(title, tags)) return; 2459 2460 if (comptime supports_drafts) { 2461 const event_id_hex = try Data.createDraft(self.data, self.session, allocator, title, tags, description); 2462 title_input.clear(allocator); 2463 tags_input.clear(allocator); 2464 description_input.clear(allocator); 2465 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2466 try self.session.navigate(route); 2467 return; 2468 } 2469 2470 var id_bytes: [evt.event_id_size]u8 = undefined; 2471 io.random(&id_bytes); 2472 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2473 2474 const event = evt.EventWithId{ 2475 .id = event_id_hex, 2476 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2477 .author = author, 2478 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2479 .title = title, 2480 .description = description, 2481 .tags = tags, 2482 }), 2483 }; 2484 2485 switch (src.repo_kind) { 2486 inline else => |repo_kind| { 2487 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2488 defer any_repo.deinit(io, allocator); 2489 switch (any_repo) { 2490 inline else => |*repo| try evt.consume(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2491 } 2492 }, 2493 } 2494 2495 // wipe the form so a return visit starts fresh 2496 title_input.clear(allocator); 2497 tags_input.clear(allocator); 2498 description_input.clear(allocator); 2499 2500 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2501 try self.session.navigate(route); 2502 } 2503 2504 // re-emit the selected thread's event with the form's content (status 2505 // preserved), then reload the thread's page. this is the terminal path; 2506 // the web posts the form to the edit route. 2507 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2508 if (comptime wasm) return; 2509 const io = self.session.io orelse return; 2510 const entry = self.data.selectedThread() orelse return; 2511 2512 const form = self.threadForm() orelse return; 2513 const title_input = &form.children.values()[title_field_index].widget.text_input; 2514 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2515 const description_input = &form.children.values()[description_field_index].widget.text_input; 2516 2517 const title = try title_input.text(allocator); 2518 defer allocator.free(title); 2519 const tags = try tags_input.text(allocator); 2520 defer allocator.free(tags); 2521 const description = try description_input.text(allocator); 2522 defer allocator.free(description); 2523 2524 if (!Event.fieldsValid(title, tags)) return; 2525 2526 if (comptime supports_drafts) { 2527 if (entryDraft(entry.*)) { 2528 try self.data.editDraft(self.session, allocator, entry.id, title, tags, description); 2529 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2530 try self.session.navigate(route); 2531 return; 2532 } 2533 } 2534 2535 const id_bytes = try evt.parseEventId(entry.id); 2536 const src = self.data.repo_source orelse return; 2537 const author = (try self.session.eventAuthor()) orelse return; 2538 2539 switch (src.repo_kind) { 2540 inline else => |repo_kind| { 2541 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2542 defer any_repo.deinit(io, allocator); 2543 switch (any_repo) { 2544 inline else => |*repo| if (has_status) 2545 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2546 .title = title, 2547 .tags = tags, 2548 .description = description, 2549 } }, author) 2550 else 2551 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 2552 } 2553 }, 2554 } 2555 2556 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2557 try self.session.navigate(route); 2558 } 2559 2560 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 2561 switch (confirmation(self.data.view) orelse return) { 2562 .publish => if (comptime supports_drafts) { 2563 if (comptime wasm) return; 2564 const entry = self.data.selectedThread() orelse return; 2565 if (!entryDraft(entry.*)) return; 2566 try self.data.publishDraft(self.session, allocator, entry.id); 2567 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 2568 try self.session.navigate(route); 2569 } else unreachable, 2570 .merge => if (comptime supports_merge) { 2571 if (comptime wasm) return; 2572 const entry = self.data.selectedThread() orelse return; 2573 try self.data.mergePatch(self.session, allocator, entry.id, button_index > 1); 2574 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 2575 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 2576 try self.session.navigate(route); 2577 } else unreachable, 2578 .remove => try self.submitRemove(allocator), 2579 } 2580 } 2581 2582 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2583 const comment = self.data.comment_id.len != 0; 2584 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2585 const id = evt.parseEventId(event_id) catch return; 2586 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2587 } 2588 2589 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2590 if (comptime wasm) return; 2591 2592 if (comptime supports_drafts) { 2593 const selected = self.data.selectedThread() orelse return; 2594 if (event_kind == kind and entryDraft(selected.*)) { 2595 try Data.removeDraft(self.session, allocator, &id); 2596 const route = draftsRoute(self.data.identity) orelse return; 2597 try self.session.navigate(route); 2598 return; 2599 } 2600 } 2601 2602 const io = self.session.io orelse return; 2603 const src = self.data.repo_source orelse return; 2604 const author = (try self.session.eventAuthor()) orelse return; 2605 2606 switch (src.repo_kind) { 2607 inline else => |repo_kind| { 2608 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2609 defer any_repo.deinit(io, allocator); 2610 switch (any_repo) { 2611 inline else => |*repo| try evt.remove(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2612 } 2613 }, 2614 } 2615 2616 const id_hex = std.fmt.bytesToHex(id, .lower); 2617 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 2618 try self.session.navigate(route); 2619 } 2620 2621 // return to the list. 2622 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2623 root_focus.setFocus(self.listScroll(index).getFocus().id); 2624 } 2625 2626 // cross to the header tabs above the stack. 2627 fn focusHeader(self: *This, root_focus: *Focus) void { 2628 root_focus.setFocus(self.box.children.keys()[header_index]); 2629 } 2630 2631 pub fn clearGrid(self: *This) void { 2632 self.box.clearGrid(); 2633 } 2634 2635 pub fn getGrid(self: This) ?Grid { 2636 return self.box.getGrid(); 2637 } 2638 2639 pub fn getFocus(self: *This) *Focus { 2640 return self.box.getFocus(); 2641 } 2642 2643 // for the parent's "scroll up at the top jumps to the header" check: at the 2644 // top only while the header tabs hold focus, so up from the split 2645 // crosses into the tabs first. 2646 pub fn getSelectedIndex(self: *This) ?usize { 2647 return if (self.headerActive()) 0 else 1; 2648 } 2649 }; 2650 } 2651