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