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