╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1383 try self.box.build(allocator, constraint, root_focus); 1384 } 1385 1386 fn refreshDetail(self: *View, allocator: std.mem.Allocator, index: usize) !void { 1387 const sel = self.selectedIssueIndex(index) orelse return; 1388 if (self.detailed_index[index]) |d| if (d == sel) return; 1389 try self.populateDetail(allocator, index, sel); 1390 self.detailed_index[index] = sel; 1391 } 1392 1393 fn populateDetail(self: *View, allocator: std.mem.Allocator, index: usize, sel: usize) !void { 1394 const entry = self.window(index).issues[sel]; 1395 const inner = self.detailInner(index); 1396 // the /description page replaces its issue's detail with a back link 1397 // and the whole description; other issues keep their normal detail. 1398 const description_page = self.data.description_page and std.mem.eql(u8, entry.id, self.data.selected_id); 1399 const comment_page = if (std.mem.eql(u8, entry.id, self.data.selected_id)) self.data.comment_page else null; 1400 1401 for (inner.children.values()) |*child| child.widget.deinit(allocator); 1402 inner.children.clearAndFree(allocator); 1403 inner.getFocus().child_id = null; 1404 self.author_id[index] = null; 1405 1406 // the tool row: the open/close and edit buttons; the description page 1407 // shows none. 1408 { 1409 const row = self.toolRow(index); 1410 for (row.children.values()) |*child| child.widget.deinit(allocator); 1411 row.children.clearAndFree(allocator); 1412 row.getFocus().child_id = null; 1413 row.getFocus().kind = .container; 1414 } 1415 if (!description_page and comment_page == null) { 1416 const row = self.toolRow(index); 1417 const pa = self.session.page_arena.allocator(); 1418 1419 { 1420 var spacer = try ui.Spacer.init(allocator); 1421 errdefer spacer.deinit(allocator); 1422 try row.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1423 } 1424 1425 // a conflicted issue's only action is resolving it. the button 1426 // stays visible logged out; the resolve page shows the 1427 // unauthorized view then. 1428 if (entry.conflicted) { 1429 const label = "resolve conflict"; 1430 var button = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1431 errdefer button.deinit(allocator); 1432 button.getFocus().focusable = true; 1433 const route = ui.RoutablePage.repoIssuesResolveRoute(self.data.identity, entry.id, "") orelse return error.RouteTooLong; 1434 button.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 1435 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 1436 } else { 1437 // a logged-out session can't flip the status, so it gets no 1438 // open/close button (and no form action on the row) 1439 if (self.session.data.is_local or self.session.data.user_id != null) { 1440 const action: []const u8 = switch (entry.issue.event.status) { 1441 .open => "close", 1442 .closed => "open", 1443 }; 1444 row.getFocus().kind = .{ .custom = if (self.data.identity.len == 0) 1445 try std.fmt.allocPrint(pa, "form:/issue:{s}/{s}", .{ entry.id, action }) 1446 else 1447 try std.fmt.allocPrint(pa, "form:/repo/{s}/issue:{s}/{s}", .{ self.data.identity, entry.id, action }) }; 1448 1449 var button = try wgt.TextBox(ui.Widget).init(allocator, action, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1450 errdefer button.deinit(allocator); 1451 button.getFocus().focusable = true; 1452 // the renderer distinguishes plain clickables from buttons that 1453 // should POST to a server route by this kind. 1454 button.getFocus().kind = .{ .custom = "submit" }; 1455 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = action.len + 2, .height = null } }); 1456 } 1457 1458 // the edit button links to the issue's edit page. 1459 { 1460 var button = try wgt.TextBox(ui.Widget).init(allocator, "edit", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1461 errdefer button.deinit(allocator); 1462 button.getFocus().focusable = true; 1463 const route = ui.RoutablePage.repoIssuesEditRoute(self.data.identity, entry.id) orelse return error.RouteTooLong; 1464 button.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 1465 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = "edit".len + 2, .height = null } }); 1466 } 1467 } 1468 1469 // the resolve button on a conflicted issue, else the open/close 1470 // button when present, the edit button otherwise 1471 row.getFocus().child_id = row.children.keys()[button_in_row_index]; 1472 } 1473 1474 if (comment_page) |page| { 1475 var back = try Comment.linkBox(allocator, self.session, "← back to issue", ui.RoutablePage.repoIssueCommentsRoute(self.data.identity, entry.id, 0) orelse return error.RouteTooLong); 1476 errdefer back.deinit(allocator); 1477 try inner.children.put(allocator, back.getFocus().id, .{ .widget = .{ .text_box = back }, .rect = null, .min_size = null }); 1478 1479 try Comment.appendComment(allocator, inner, self.session, self.data.identity, page.selected); 1480 try Comment.appendCount(allocator, inner, page.replies.count, "reply", "replies", self.session.page_arena.allocator()); 1481 for (page.replies.comments) |comment| try Comment.appendComment(allocator, inner, self.session, self.data.identity, comment); 1482 try Comment.appendWindowNav(allocator, inner, self.session, self.data.identity, &page.selected.comment.event.thread_id, &page.selected.id, page.replies); 1483 1484 var spacer = try ui.Spacer.init(allocator); 1485 errdefer spacer.deinit(allocator); 1486 try inner.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1487 1488 inner.getFocus().child_id = inner.children.keys()[0]; 1489 const sc = self.detailScroll(index); 1490 sc.x = 0; 1491 sc.y = 0; 1492 sc.getFocus().version +%= 1; 1493 return; 1494 } 1495 1496 if (description_page) { 1497 // the back link, in the title slot so the pane's input handling 1498 // applies to it unchanged. 1499 var tb = try wgt.TextBox(ui.Widget).init(allocator, "← back to issue", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1500 errdefer tb.deinit(allocator); 1501 tb.getFocus().focusable = true; 1502 tb.getFocus().kind = .{ .custom = try issuesLink(self.session.page_arena, self.data.identity, entry.issue.event.status, "", entry.id) }; 1503 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1504 } else { 1505 // the issue's title as a focusable word-wrapped text box. 1506 { 1507 var tb = try wgt.TextBox(ui.Widget).init(allocator, entry.issue.event.title, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = " title " }); 1508 errdefer tb.deinit(allocator); 1509 tb.getFocus().focusable = true; 1510 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1511 } 1512 1513 self.author_id[index] = blk: { 1514 var tb = try ui.authorBox(allocator, self.session.page_arena, entry.author); 1515 errdefer tb.deinit(allocator); 1516 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1517 break :blk tb.getFocus().id; 1518 }; 1519 1520 // the issue's tags, each linking to this status's list filtered to 1521 // that tag. 1522 { 1523 var items: std.ArrayList(ui.TagFlow.Item) = .empty; 1524 defer items.deinit(allocator); 1525 var tag_iter = evt.Issue.tagIterator(entry.issue.event.tags); 1526 while (tag_iter.next()) |tag| { 1527 if (tag.len == 0) continue; 1528 try items.append(allocator, .{ .text = tag, .link = try tagLink(self.session.page_arena, self.data.identity, entry.issue.event.status, tag) }); 1529 } 1530 if (items.items.len > 0) { 1531 var tf = try ui.TagFlow.init(allocator); 1532 errdefer tf.deinit(allocator); 1533 try tf.setItems(allocator, items.items); 1534 try inner.children.put(allocator, tf.getFocus().id, .{ .widget = .{ .tag_flow = tf }, .rect = null, .min_size = null }); 1535 } 1536 } 1537 } 1538 1539 // the description as a focusable word-wrapped text box. past the limit 1540 // it's cut at the last whole line and links to the /description page, 1541 // which shows the whole thing. 1542 self.description_id[index] = blk: { 1543 const whole = entry.issue.event.description; 1544 const cut_short = !description_page and whole.len > max_description_size; 1545 const shown = if (cut_short) 1546 std.mem.trimEnd(u8, whole[0 .. std.mem.lastIndexOfScalar(u8, whole[0..max_description_size], '\n') orelse 0], " \t\r\n") 1547 else if (whole.len == 0) 1548 "(no description)" 1549 else 1550 whole; 1551 var tb = try wgt.TextBox(ui.Widget).init(allocator, shown, .{ 1552 .border_style = .single, 1553 .rounded_corners = true, 1554 .wrap_kind = .word, 1555 .label = " description ", 1556 .bottom_label = if (cut_short) " click or press enter to see more " else "", 1557 }); 1558 errdefer tb.deinit(allocator); 1559 tb.getFocus().focusable = true; 1560 if (cut_short) tb.getFocus().kind = .{ .custom = try descriptionLink(self.session.page_arena, self.data.identity, entry.id) }; 1561 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1562 break :blk tb.getFocus().id; 1563 }; 1564 1565 if (!description_page) { 1566 var reply = try Comment.linkBox(allocator, self.session, "new comment", ui.RoutablePage.repoCommentNewRoute(self.data.identity, entry.id, "") orelse return error.RouteTooLong); 1567 errdefer reply.deinit(allocator); 1568 try inner.children.put(allocator, reply.getFocus().id, .{ .widget = .{ .text_box = reply }, .rect = null, .min_size = null }); 1569 1570 try Comment.appendCount(allocator, inner, entry.comments.count, "comment", "comments", self.session.page_arena.allocator()); 1571 for (entry.comments.comments) |comment| try Comment.appendComment(allocator, inner, self.session, self.data.identity, comment); 1572 try Comment.appendWindowNav(allocator, inner, self.session, self.data.identity, entry.id, null, entry.comments); 1573 1574 var spacer = try ui.Spacer.init(allocator); 1575 errdefer spacer.deinit(allocator); 1576 try inner.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1577 } 1578 1579 // select the title by default 1580 inner.getFocus().child_id = inner.children.keys()[title_child_index]; 1581 1582 // reset the scroll to the top for the newly-shown issue: directly on the 1583 // terminal (the wasm offset), and via a version bump on the web (so the 1584 // renderer's scroll id changes and JS drops the preserved position). 1585 const sc = self.detailScroll(index); 1586 sc.x = 0; 1587 sc.y = 0; 1588 sc.getFocus().version +%= 1; 1589 } 1590 1591 pub fn input(self: *View, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 1592 // wheel input moves focus like the arrows 1593 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 1594 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 1595 else 1596 raw_key; 1597 1598 if (self.headerActive()) { 1599 // down from the tabs re-enters the stack if the selected view has 1600 // something to focus; other keys move the tabs. 1601 if (inp.vertDirection(key) == .down) { 1602 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 1603 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 1604 // nothing focusable in the unauthorized view 1605 .unauthorized => false, 1606 else => true, 1607 } else false; 1608 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 1609 } else { 1610 try self.header().input(allocator, key, root_focus); 1611 } 1612 return; 1613 } 1614 if (self.tagsViewActive()) { 1615 self.tagsViewInput(key, root_focus); 1616 return; 1617 } 1618 if (self.formViewActive()) { 1619 if (self.data.view == .resolve) { 1620 try self.resolveInput(allocator, key, root_focus); 1621 } else if (self.data.view == .new_comment or self.data.view == .edit_comment) { 1622 try self.commentFormInput(allocator, key, root_focus); 1623 } else { 1624 try self.formInput(allocator, key, root_focus); 1625 } 1626 return; 1627 } 1628 const i = self.selectedSplitIndex() orelse return; 1629 if (self.detailActive(i)) { 1630 try self.detailInput(allocator, i, key, root_focus); 1631 } else { 1632 try self.listInput(i, key, root_focus); 1633 } 1634 } 1635 1636 // arrow keys move the tag selection; up from the top row crosses to the 1637 // header tabs. 1638 fn tagsViewInput(self: *View, key: Key, root_focus: *Focus) void { 1639 const tf = self.tagsView(); 1640 const cid = tf.focus.child_id orelse return; 1641 const cur = tf.indexOfFocusId(cid) orelse return; 1642 const count = tf.text_boxes.items.len; 1643 switch (key) { 1644 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 1645 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 1646 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 1647 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 1648 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 1649 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 1650 else => {}, 1651 } 1652 } 1653 1654 fn listInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1655 // up/down (and the scroll wheel) move the selection a row; page up/down 1656 // jump a fixed amount. right/Enter cross into the detail pane. up from 1657 // the top row crosses into the header tabs. 1658 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 1659 const lb = self.listBox(index); 1660 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 1661 if (delta < 0 and at_top) return self.focusHeader(root_focus); 1662 ui.moveRowFocus(lb, self.listScroll(index), root_focus, delta); 1663 return; 1664 } 1665 switch (key) { 1666 .enter, .arrow_right => try self.focusDetail(index, root_focus), 1667 else => {}, 1668 } 1669 } 1670 1671 fn detailInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1672 switch (key) { 1673 .page_up => { 1674 self.pageDetail(index, root_focus, -10); 1675 return; 1676 }, 1677 .page_down => { 1678 self.pageDetail(index, root_focus, 10); 1679 return; 1680 }, 1681 .home => { 1682 self.jumpDetail(index, root_focus, false); 1683 return; 1684 }, 1685 .end => { 1686 self.jumpDetail(index, root_focus, true); 1687 return; 1688 }, 1689 else => {}, 1690 } 1691 if (self.data.comment_page != null and std.mem.eql(u8, self.window(index).issues[self.detailed_index[index] orelse return].id, self.data.selected_id)) { 1692 try self.commentInput(index, key, root_focus); 1693 } else if (self.toolRowFocused(index)) { 1694 try self.toolRowInput(allocator, index, key, root_focus); 1695 } else if (self.titleFocused(index)) { 1696 try self.titleInput(index, key, root_focus); 1697 } else if (self.authorFocused(index)) { 1698 try self.authorInput(index, key, root_focus); 1699 } else if (self.tagsFocused(index)) { 1700 try self.tagsInput(index, key, root_focus); 1701 } else if (self.descriptionFocused(index)) { 1702 try self.descriptionInput(index, key, root_focus); 1703 } else { 1704 try self.commentInput(index, key, root_focus); 1705 } 1706 } 1707 1708 // the tool row: enter or a click on the open/close button flips the 1709 // issue's status; the edit and resolve buttons are a: links the host 1710 // follows. arrows cross between the buttons and to the neighboring 1711 // widgets. 1712 fn toolRowInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1713 const row = self.toolRow(index); 1714 const cur = if (row.getFocus().child_id) |cid| row.children.getIndex(cid) orelse return else return; 1715 const on_status = cur == button_in_row_index and self.statusButton(index) != null; 1716 switch (key) { 1717 .arrow_left => if (cur > button_in_row_index) root_focus.setFocus(row.children.keys()[cur - 1]) else try self.focusList(index, root_focus), 1718 .arrow_right => if (cur + 1 < row.children.count()) root_focus.setFocus(row.children.keys()[cur + 1]), 1719 .arrow_up => self.focusHeader(root_focus), 1720 .arrow_down => { 1721 self.focusDetailEdge(index, root_focus, false); 1722 self.detailScroll(index).y = 0; 1723 }, 1724 .enter => if (on_status) try self.toggleIssueStatus(allocator, index), 1725 .mouse => |mouse| if (self.statusButton(index)) |button| { 1726 if (inp.leftClickOn(root_focus, button.getFocus().id, mouse)) try self.toggleIssueStatus(allocator, index); 1727 }, 1728 else => {}, 1729 } 1730 } 1731 1732 fn titleInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1733 switch (key) { 1734 .arrow_left => try self.focusList(index, root_focus), 1735 .arrow_up => self.focusToolRow(index, root_focus), 1736 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1737 else => {}, 1738 } 1739 } 1740 1741 // the author box's a: link (when it has one) is followed by the host on 1742 // enter / a click; arrows cross to the neighboring widgets. 1743 fn authorInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1744 switch (key) { 1745 .arrow_left => try self.focusList(index, root_focus), 1746 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1747 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1748 else => {}, 1749 } 1750 } 1751 1752 fn descriptionInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1753 const sc = self.detailScroll(index); 1754 switch (key) { 1755 .arrow_left => return self.focusList(index, root_focus), 1756 // the terminal moves through the description one line at a time; 1757 // the web lets focus changes drive its native scroll. 1758 .arrow_up => { 1759 if (!self.session.is_terminal) { 1760 _ = self.moveDetailVertical(index, root_focus, false); 1761 return; 1762 } 1763 if (self.focusAdjacentVisible(index, root_focus, false)) return; 1764 const before = sc.y; 1765 sc.y -= 1; 1766 sc.clampToContent(); 1767 if (sc.y == before) _ = self.moveDetailVertical(index, root_focus, false); 1768 return; 1769 }, 1770 .arrow_down => { 1771 if (!self.session.is_terminal) { 1772 _ = self.moveDetailVertical(index, root_focus, true); 1773 return; 1774 } 1775 if (self.focusAdjacentVisible(index, root_focus, true)) return; 1776 const before = sc.y; 1777 sc.y += 1; 1778 sc.clampToContent(); 1779 if (sc.y == before) _ = self.moveDetailVertical(index, root_focus, true); 1780 return; 1781 }, 1782 else => return, 1783 } 1784 sc.clampToContent(); 1785 } 1786 1787 fn commentInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1788 switch (key) { 1789 .arrow_left => if (!self.moveCommentHorizontal(index, root_focus, false)) return self.focusList(index, root_focus), 1790 .arrow_right => _ = self.moveCommentHorizontal(index, root_focus, true), 1791 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1792 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1793 else => return, 1794 } 1795 } 1796 1797 // page the detail scroll, then focus the first or last visible widget. 1798 fn pageDetail(self: *View, index: usize, root_focus: *Focus, delta: isize) void { 1799 if (!self.session.is_terminal) return; 1800 const sc = self.detailScroll(index); 1801 sc.y += delta; 1802 sc.clampToContent(); 1803 self.focusVisible(index, root_focus, delta > 0); 1804 } 1805 1806 // jump to the detail's first or last focusable widget. the terminal pins 1807 // its synthetic scroll too; the browser scrolls to the new focus itself. 1808 fn jumpDetail(self: *View, index: usize, root_focus: *Focus, to_end: bool) void { 1809 if (self.session.is_terminal) { 1810 const sc = self.detailScroll(index); 1811 sc.y = if (to_end) std.math.maxInt(isize) else 0; 1812 sc.clampToContent(); 1813 self.focusVisible(index, root_focus, to_end); 1814 } else { 1815 self.focusDetailEdge(index, root_focus, to_end); 1816 } 1817 } 1818 1819 fn focusDetailEdge(self: *View, index: usize, root_focus: *Focus, last: bool) void { 1820 const focus = self.detailInner(index).getFocus(); 1821 var chosen: ?usize = null; 1822 for (focus.children.keys(), focus.children.values()) |id, child| { 1823 if (!child.focus.focusable) continue; 1824 chosen = id; 1825 if (!last) break; 1826 } 1827 if (chosen) |id| root_focus.setFocus(id); 1828 } 1829 1830 fn detailRectVisible(self: *View, index: usize, rect: layout.IRect) bool { 1831 const sc = self.detailScroll(index); 1832 const viewport = sc.grid orelse return false; 1833 const top = sc.y; 1834 const bottom = top + @as(isize, @intCast(viewport.size.height - sc.bar_h)); 1835 return rect.y + @as(isize, @intCast(rect.size.height)) > top and rect.y < bottom; 1836 } 1837 1838 // focus the top- or bottom-most focusable widget intersecting the viewport. 1839 fn focusVisible(self: *View, index: usize, root_focus: *Focus, last: bool) void { 1840 const inner = self.detailInner(index); 1841 var offset: usize = 0; 1842 while (offset < inner.children.count()) : (offset += 1) { 1843 const child_index = if (last) inner.children.count() - 1 - offset else offset; 1844 if (self.focusVisibleChild(index, child_index, root_focus, last)) return; 1845 } 1846 } 1847 1848 fn focusVisibleChild(self: *View, index: usize, child_index: usize, root_focus: *Focus, last: bool) bool { 1849 const child = &self.detailInner(index).children.values()[child_index]; 1850 const child_rect = child.rect orelse return false; 1851 var chosen: ?usize = null; 1852 if (child.widget.getFocus().focusable and self.detailRectVisible(index, child_rect)) { 1853 const id = self.detailInner(index).children.keys()[child_index]; 1854 chosen = id; 1855 if (!last) { 1856 root_focus.setFocus(id); 1857 return true; 1858 } 1859 } 1860 1861 for (child.widget.getFocus().children.keys(), child.widget.getFocus().children.values()) |id, focus_child| { 1862 if (!focus_child.focus.focusable) continue; 1863 const rect: layout.IRect = .{ 1864 .x = child_rect.x + @as(isize, @intCast(focus_child.rect.x)), 1865 .y = child_rect.y + @as(isize, @intCast(focus_child.rect.y)), 1866 .size = focus_child.rect.size, 1867 }; 1868 if (!self.detailRectVisible(index, rect)) continue; 1869 chosen = id; 1870 if (!last) break; 1871 } 1872 if (chosen) |id| { 1873 root_focus.setFocus(id); 1874 return true; 1875 } 1876 return false; 1877 } 1878 1879 fn focusAdjacentVisible(self: *View, index: usize, root_focus: *Focus, down: bool) bool { 1880 const inner = self.detailInner(index); 1881 var next = self.focusedDetailChild(index, root_focus) orelse return false; 1882 while (true) { 1883 if (down) { 1884 next += 1; 1885 if (next >= inner.children.count()) return false; 1886 } else { 1887 if (next == 0) return false; 1888 next -= 1; 1889 } 1890 if (self.focusVisibleChild(index, next, root_focus, !down)) return true; 1891 } 1892 } 1893 1894 fn focusedDetailChild(self: *View, index: usize, root_focus: *Focus) ?usize { 1895 const inner = self.detailInner(index); 1896 var id = root_focus.grandchild_id orelse return null; 1897 while (inner.getFocus().children.get(id)) |child| { 1898 if (child.parent_id == inner.getFocus().id) return inner.children.getIndex(id); 1899 id = child.parent_id; 1900 } 1901 return null; 1902 } 1903 1904 fn detailChildFocusable(self: *View, index: usize, child_index: usize) bool { 1905 const child = &self.detailInner(index).children.values()[child_index].widget; 1906 const focus = child.getFocus(); 1907 if (focus.focusable) return true; 1908 for (focus.children.values()) |item| { 1909 if (item.focus.focusable) return true; 1910 } 1911 return false; 1912 } 1913 1914 fn focusDetailChild(self: *View, index: usize, child_index: usize, last: bool, root_focus: *Focus) void { 1915 const inner = self.detailInner(index); 1916 const child = &inner.children.values()[child_index]; 1917 var target = inner.children.keys()[child_index]; 1918 var rect = child.rect orelse return; 1919 switch (child.widget) { 1920 .repo_comment => |*comment| { 1921 if (comment.rowRect(last)) |row_rect| { 1922 rect.x += row_rect.x; 1923 rect.y += row_rect.y; 1924 rect.size = row_rect.size; 1925 } 1926 if (last) comment.focusBody(root_focus) else comment.focusMetadata(root_focus); 1927 }, 1928 .box => |*box| if (box.children.count() > 0) { 1929 target = box.children.keys()[if (last) box.children.count() - 1 else 0]; 1930 root_focus.setFocus(target); 1931 }, 1932 else => root_focus.setFocus(target), 1933 } 1934 const sc = self.detailScroll(index); 1935 if (sc.grid) |viewport| { 1936 const viewport_height = viewport.size.height - sc.bar_h; 1937 if (rect.size.height > viewport_height) { 1938 const bottom_aligned = rect.y + @as(isize, @intCast(rect.size.height - viewport_height)); 1939 sc.y = std.math.clamp(sc.y, rect.y, bottom_aligned); 1940 sc.clampToContent(); 1941 return; 1942 } 1943 } 1944 sc.scrollToRect(rect); 1945 } 1946 1947 fn moveDetailVertical(self: *View, index: usize, root_focus: *Focus, down: bool) bool { 1948 const inner = self.detailInner(index); 1949 const current = self.focusedDetailChild(index, root_focus) orelse return false; 1950 if (inner.children.values()[current].widget == .repo_comment) { 1951 const comment = &inner.children.values()[current].widget.repo_comment; 1952 if (down and !comment.bodyFocused()) { 1953 self.focusDetailChild(index, current, true, root_focus); 1954 return true; 1955 } 1956 if (!down and comment.bodyFocused()) { 1957 self.focusDetailChild(index, current, false, root_focus); 1958 return true; 1959 } 1960 } 1961 1962 var next = current; 1963 while (true) { 1964 if (down) { 1965 next += 1; 1966 if (next >= inner.children.count()) return false; 1967 } else { 1968 if (next == 0) return false; 1969 next -= 1; 1970 } 1971 if (!self.detailChildFocusable(index, next)) continue; 1972 self.focusDetailChild(index, next, !down, root_focus); 1973 return true; 1974 } 1975 } 1976 1977 fn moveCommentHorizontal(self: *View, index: usize, root_focus: *Focus, right: bool) bool { 1978 const inner = self.detailInner(index); 1979 const current = self.focusedDetailChild(index, root_focus) orelse return false; 1980 const child = &inner.children.values()[current]; 1981 if (child.widget == .repo_comment) { 1982 const moved = child.widget.repo_comment.moveHorizontal(root_focus, right); 1983 if (moved) if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 1984 return moved; 1985 } 1986 const row = switch (child.widget) { 1987 .box => |*box| box, 1988 else => return false, 1989 }; 1990 const selected = row.getFocus().child_id orelse return false; 1991 const selected_index = row.children.getIndex(selected) orelse return false; 1992 const target = if (right) selected_index + 1 else selected_index -| 1; 1993 if (target == selected_index or target >= row.children.count()) return false; 1994 root_focus.setFocus(row.children.keys()[target]); 1995 if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 1996 return true; 1997 } 1998 1999 // up/down (and tab/shift+tab) move between a form's fields; up from the 2000 // title crosses into the header tabs. the multiline description keeps 2001 // enter and any up/down that has a row to move to. 2002 fn formInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2003 const form = self.issueForm() orelse return; 2004 const cid = form.getFocus().child_id orelse return; 2005 const cur = form.children.getIndex(cid) orelse return; 2006 2007 if (cur == description_field_index) { 2008 const child = &form.children.values()[cur]; 2009 if (try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2010 return child.widget.input(allocator, key, root_focus); 2011 } 2012 2013 switch (key) { 2014 .arrow_up, .back_tab => if (cur > 0) 2015 root_focus.setFocus(form.children.keys()[cur - 1]) 2016 else 2017 self.focusHeader(root_focus), 2018 .arrow_down, .tab => if (cur < submit_field_index) { 2019 root_focus.setFocus(form.children.keys()[cur + 1]); 2020 }, 2021 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 2022 .mouse => |mouse| if (cur == submit_field_index) { 2023 const submit = &form.children.values()[cur].widget.submit_button; 2024 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitForm(allocator); 2025 }, 2026 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 2027 } 2028 } 2029 2030 fn commentFormInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2031 const form = self.issueForm() orelse return; 2032 const cid = form.getFocus().child_id orelse return; 2033 const cur = form.children.getIndex(cid) orelse return; 2034 const child = &form.children.values()[cur]; 2035 2036 if (cur == comment_body_field_index and try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2037 return child.widget.input(allocator, key, root_focus); 2038 2039 switch (key) { 2040 .arrow_up, .back_tab => if (cur > comment_author_field_index) 2041 root_focus.setFocus(form.children.keys()[cur - 1]) 2042 else 2043 self.focusHeader(root_focus), 2044 .arrow_down, .tab => if (cur < comment_submit_field_index) 2045 root_focus.setFocus(form.children.keys()[cur + 1]), 2046 .enter => if (cur == comment_submit_field_index) try self.submitComment(allocator), 2047 .mouse => |mouse| if (cur == comment_submit_field_index) { 2048 const submit = &child.widget.submit_button; 2049 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitComment(allocator); 2050 }, 2051 else => try child.widget.input(allocator, key, root_focus), 2052 } 2053 } 2054 2055 // the multiline hunk inputs keep enter and any up/down that has a row to 2056 // move to; enter on a "use this" link is a navigation the host follows 2057 // before this runs. 2058 fn resolveInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2059 const form = self.issueForm() orelse return; 2060 const cid = form.getFocus().child_id orelse return; 2061 const cur = form.children.getIndex(cid) orelse return; 2062 const child = &form.children.values()[cur]; 2063 2064 if (child.widget == .text_input) { 2065 const ti = &child.widget.text_input; 2066 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2067 return child.widget.input(allocator, key, root_focus); 2068 } 2069 2070 const on_submit = child.widget == .submit_button; 2071 2072 switch (key) { 2073 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2074 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2075 .arrow_left, .arrow_right => switch (child.widget) { 2076 .box => |*row| { 2077 const row_cid = row.getFocus().child_id orelse return; 2078 const row_index = row.children.getIndex(row_cid) orelse return; 2079 if (key == .arrow_left) { 2080 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 2081 } else if (row_index + 1 < row.children.count()) { 2082 root_focus.setFocus(row.children.keys()[row_index + 1]); 2083 } 2084 }, 2085 else => try child.widget.input(allocator, key, root_focus), 2086 }, 2087 .enter => if (on_submit) try self.submitResolution(allocator), 2088 .mouse => |mouse| if (on_submit) { 2089 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2090 }, 2091 else => try child.widget.input(allocator, key, root_focus), 2092 } 2093 } 2094 2095 // a multiline input keeps enter and any up/down that has a row to move to 2096 fn multilineKeepsKey(text_input: *wgt.TextInput(ui.Widget), allocator: std.mem.Allocator, key: Key) !bool { 2097 return switch (key) { 2098 .enter => true, 2099 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2100 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2101 else => false, 2102 }; 2103 } 2104 2105 // the neighboring focusable resolve-form child, skipping the spacer and 2106 // the blank gap rows 2107 fn resolveStep(form: *wgt.Box(ui.Widget), cur: usize, down: bool) ?usize { 2108 var i = cur; 2109 while (true) { 2110 if (down) { 2111 i += 1; 2112 if (i >= form.children.count()) return null; 2113 } else { 2114 if (i == 0) return null; 2115 i -= 1; 2116 } 2117 switch (form.children.values()[i].widget) { 2118 .spacer, .text => continue, 2119 else => return i, 2120 } 2121 } 2122 } 2123 2124 // focus the resolve-form child at `i` (a version row focuses its selected 2125 // child) and keep it visible on the terminal 2126 fn focusResolveChild(self: *View, form: *wgt.Box(ui.Widget), i: usize, root_focus: *Focus) void { 2127 const child = &form.children.values()[i]; 2128 switch (child.widget) { 2129 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2130 else => root_focus.setFocus(form.children.keys()[i]), 2131 } 2132 if (self.session.is_terminal) { 2133 if (self.resolveScroll()) |sc| { 2134 if (child.rect) |rect| sc.scrollToRect(rect); 2135 } 2136 } 2137 } 2138 2139 // re-emit the conflicted issue's event with the resolved content; any 2140 // event on the issue settles its conflict entry. this is the terminal 2141 // path; the web posts the form to the resolve route. 2142 fn submitResolution(self: *View, allocator: std.mem.Allocator) !void { 2143 if (comptime wasm) return; 2144 const io = self.session.io orelse return; 2145 const src = self.data.repo_source orelse return; 2146 const entry = self.data.selectedIssue() orelse return; 2147 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2148 const form = self.issueForm() orelse return; 2149 2150 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2151 var title: ?[]u8 = null; 2152 var tags: ?[]u8 = null; 2153 var hunks: std.ArrayList([]const u8) = .empty; 2154 defer { 2155 if (title) |t| allocator.free(t); 2156 if (tags) |t| allocator.free(t); 2157 for (hunks.items) |h| allocator.free(h); 2158 hunks.deinit(allocator); 2159 } 2160 for (form.children.values()) |*child| switch (child.widget) { 2161 .text_input => |*ti| { 2162 const text = try ti.text(allocator); 2163 errdefer allocator.free(text); 2164 if (std.mem.eql(u8, ti.options.name, "title")) { 2165 title = text; 2166 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2167 tags = text; 2168 } else { 2169 try hunks.append(allocator, text); 2170 } 2171 }, 2172 else => {}, 2173 }; 2174 2175 var id_bytes: [evt.event_id_size]u8 = undefined; 2176 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2177 2178 switch (src.repo_kind) { 2179 inline else => |repo_kind| { 2180 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2181 defer any_repo.deinit(io, allocator); 2182 switch (any_repo) { 2183 inline else => |*repo| evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .resolve = .{ 2184 .title = title, 2185 .tags = tags, 2186 .hunks = hunks.items, 2187 } }, author_email) catch |err| switch (err) { 2188 // leave the form up for correction 2189 error.InvalidFields => return, 2190 else => |e| return e, 2191 }, 2192 } 2193 }, 2194 } 2195 2196 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 2197 try self.session.navigate(route); 2198 } 2199 2200 // commit a new or edited comment and navigate to its permalink. this is the 2201 // terminal path; the web posts the form to its comment route. 2202 fn submitComment(self: *View, allocator: std.mem.Allocator) !void { 2203 if (comptime wasm) return; 2204 const io = self.session.io orelse return; 2205 const src = self.data.repo_source orelse return; 2206 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2207 const form = self.issueForm() orelse return; 2208 const body_input = &form.children.values()[comment_body_field_index].widget.text_input; 2209 const body = try body_input.text(allocator); 2210 defer allocator.free(body); 2211 if (!evt.Comment.fieldsValid(body)) return; 2212 2213 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2214 @memcpy(&thread_id, self.data.selected_id); 2215 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2216 var parent: [evt.event_id_size * 2]u8 = undefined; 2217 @memcpy(&parent, self.data.comment_id); 2218 break :blk parent; 2219 }; 2220 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2221 const editing = self.data.view == .edit_comment; 2222 switch (src.repo_kind) { 2223 inline else => |repo_kind| { 2224 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2225 defer any_repo.deinit(io, allocator); 2226 switch (any_repo) { 2227 inline else => |*repo| if (editing) { 2228 var comment_id: [evt.event_id_size]u8 = undefined; 2229 _ = std.fmt.hexToBytes(&comment_id, self.data.comment_id) catch return; 2230 try evt.Comment.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author_email); 2231 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2232 } else { 2233 event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author_email); 2234 }, 2235 } 2236 }, 2237 } 2238 2239 body_input.clear(allocator); 2240 const route = ui.RoutablePage.repoCommentsRoute(self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2241 try self.session.navigate(route); 2242 } 2243 2244 // the terminal submit paths: the new form commits a new-issue event, the 2245 // edit form re-emits the selected issue's event with the form's content. 2246 // the web posts the forms to their routes instead. 2247 fn submitForm(self: *View, allocator: std.mem.Allocator) !void { 2248 if (self.data.view == .edit) { 2249 try self.submitEditedIssue(allocator); 2250 } else { 2251 try self.submitNewIssue(allocator); 2252 } 2253 } 2254 2255 // commit the new issue to the repo's events branch and navigate to it. 2256 // this is the terminal path; the web posts the form to the issue route, 2257 // so the wasm side never runs (or compiles) the repo access below. 2258 fn submitNewIssue(self: *View, allocator: std.mem.Allocator) !void { 2259 if (comptime wasm) return; 2260 const io = self.session.io orelse return; 2261 const src = self.data.repo_source orelse return; 2262 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2263 2264 const form = self.issueForm() orelse return; 2265 const title_input = &form.children.values()[title_field_index].widget.text_input; 2266 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2267 const description_input = &form.children.values()[description_field_index].widget.text_input; 2268 2269 const title = try title_input.text(allocator); 2270 defer allocator.free(title); 2271 const tags = try tags_input.text(allocator); 2272 defer allocator.free(tags); 2273 const description = try description_input.text(allocator); 2274 defer allocator.free(description); 2275 2276 if (!evt.Issue.fieldsValid(title, tags)) return; 2277 2278 var id_bytes: [evt.event_id_size]u8 = undefined; 2279 io.random(&id_bytes); 2280 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2281 2282 const event = evt.EventWithId{ 2283 .id = event_id_hex, 2284 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2285 .author_email = author_email, 2286 .event = .{ .issue = .{ 2287 .title = title, 2288 .description = description, 2289 .tags = tags, 2290 } }, 2291 }; 2292 2293 switch (src.repo_kind) { 2294 inline else => |repo_kind| { 2295 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2296 defer any_repo.deinit(io, allocator); 2297 switch (any_repo) { 2298 inline else => |*repo| try evt.consume(repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2299 } 2300 }, 2301 } 2302 2303 // wipe the form so a return visit starts fresh 2304 title_input.clear(allocator); 2305 tags_input.clear(allocator); 2306 description_input.clear(allocator); 2307 2308 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, "", &event_id_hex) orelse return; 2309 try self.session.navigate(route); 2310 } 2311 2312 // re-emit the selected issue's event with the form's content (status 2313 // preserved), then reload the issue's page. this is the terminal path; 2314 // the web posts the form to the edit route. 2315 fn submitEditedIssue(self: *View, allocator: std.mem.Allocator) !void { 2316 if (comptime wasm) return; 2317 const io = self.session.io orelse return; 2318 const src = self.data.repo_source orelse return; 2319 const entry = self.data.selectedIssue() orelse return; 2320 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2321 2322 const form = self.issueForm() orelse return; 2323 const title_input = &form.children.values()[title_field_index].widget.text_input; 2324 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2325 const description_input = &form.children.values()[description_field_index].widget.text_input; 2326 2327 const title = try title_input.text(allocator); 2328 defer allocator.free(title); 2329 const tags = try tags_input.text(allocator); 2330 defer allocator.free(tags); 2331 const description = try description_input.text(allocator); 2332 defer allocator.free(description); 2333 2334 if (!evt.Issue.fieldsValid(title, tags)) return; 2335 2336 var id_bytes: [evt.event_id_size]u8 = undefined; 2337 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2338 2339 switch (src.repo_kind) { 2340 inline else => |repo_kind| { 2341 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2342 defer any_repo.deinit(io, allocator); 2343 switch (any_repo) { 2344 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2345 .title = title, 2346 .tags = tags, 2347 .description = description, 2348 } }, author_email), 2349 } 2350 }, 2351 } 2352 2353 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 2354 try self.session.navigate(route); 2355 } 2356 2357 // flip the shown issue's status by re-emitting its event, then reload the 2358 // page rooted at the issue so the view reflects the change. this is the 2359 // terminal path; the web posts the button's form to the status route. 2360 fn toggleIssueStatus(self: *View, allocator: std.mem.Allocator, index: usize) !void { 2361 if (comptime wasm) return; 2362 const io = self.session.io orelse return; 2363 const src = self.data.repo_source orelse return; 2364 const sel = self.detailed_index[index] orelse return; 2365 const entry = self.window(index).issues[sel]; 2366 const author_email = (try self.session.eventAuthorEmail()) orelse return; 2367 2368 const status: evt.Issue.Status = switch (entry.issue.event.status) { 2369 .open => .closed, 2370 .closed => .open, 2371 }; 2372 2373 var id_bytes: [evt.event_id_size]u8 = undefined; 2374 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2375 2376 switch (src.repo_kind) { 2377 inline else => |repo_kind| { 2378 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 2379 defer any_repo.deinit(io, allocator); 2380 switch (any_repo) { 2381 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .status = status }, author_email), 2382 } 2383 }, 2384 } 2385 2386 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, status, "", entry.id) orelse return; 2387 try self.session.navigate(route); 2388 } 2389 2390 // arrow keys move the tag selection; at the flow's edges focus crosses to 2391 // the neighboring widgets. 2392 fn tagsInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 2393 const tf = self.tagFlow(index) orelse return; 2394 const cid = tf.focus.child_id orelse return; 2395 const cur = tf.indexOfFocusId(cid) orelse return; 2396 const count = tf.text_boxes.items.len; 2397 switch (key) { 2398 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 2399 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 2400 .arrow_up => if (tf.rowStep(cur, false)) |i| { 2401 self.focusTag(index, tf, root_focus, i); 2402 } else { 2403 _ = self.moveDetailVertical(index, root_focus, false); 2404 }, 2405 .arrow_down => if (tf.rowStep(cur, true)) |i| { 2406 self.focusTag(index, tf, root_focus, i); 2407 } else { 2408 _ = self.moveDetailVertical(index, root_focus, true); 2409 }, 2410 .home => self.focusTag(index, tf, root_focus, 0), 2411 .end => self.focusTag(index, tf, root_focus, count - 1), 2412 else => {}, 2413 } 2414 } 2415 2416 const button_in_row_index: usize = 1; 2417 const title_child_index: usize = 0; 2418 const tags_child_index: usize = 2; 2419 2420 fn tagFlow(self: *View, index: usize) ?*ui.TagFlow { 2421 const inner = self.detailInner(index); 2422 if (inner.children.count() <= tags_child_index) return null; 2423 return switch (inner.children.values()[tags_child_index].widget) { 2424 .tag_flow => |*tf| tf, 2425 else => null, 2426 }; 2427 } 2428 2429 fn tagsFocused(self: *View, index: usize) bool { 2430 const inner = self.detailInner(index); 2431 const cid = inner.getFocus().child_id orelse return false; 2432 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 2433 } 2434 2435 // the open/close button inside the detail frame's tool row, identified 2436 // by its posting kind; a logged-out session's or conflicted issue's row 2437 // has none. 2438 fn statusButton(self: *View, index: usize) ?*wgt.TextBox(ui.Widget) { 2439 const row = self.toolRow(index); 2440 if (row.children.count() <= button_in_row_index) return null; 2441 switch (row.children.values()[button_in_row_index].widget) { 2442 .text_box => |*tb| switch (tb.box.focus.kind) { 2443 .custom => |custom| if (std.mem.eql(u8, custom, "submit")) return tb, 2444 else => {}, 2445 }, 2446 else => {}, 2447 } 2448 return null; 2449 } 2450 2451 fn toolRowFocused(self: *View, index: usize) bool { 2452 const outer = self.detailOuter(index); 2453 const cid = outer.getFocus().child_id orelse return false; 2454 return outer.children.getIndex(cid) == tool_row_index; 2455 } 2456 2457 fn focusTag(self: *View, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 2458 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 2459 // keep the tag visible on the terminal: its rect offset by the flow's 2460 // position in the pane. 2461 if (self.session.is_terminal and item < tf.rects.items.len) { 2462 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 2463 var rect = tf.rects.items[item]; 2464 rect.x += flow_rect.x; 2465 rect.y += flow_rect.y; 2466 self.detailScroll(index).scrollToRect(rect); 2467 } 2468 } 2469 } 2470 2471 fn titleFocused(self: *View, index: usize) bool { 2472 const inner = self.detailInner(index); 2473 const cid = inner.getFocus().child_id orelse return false; 2474 return inner.children.getIndex(cid) == title_child_index; 2475 } 2476 2477 fn authorFocused(self: *View, index: usize) bool { 2478 const inner = self.detailInner(index); 2479 const cid = inner.getFocus().child_id orelse return false; 2480 return cid == self.author_id[index]; 2481 } 2482 2483 fn descriptionFocused(self: *View, index: usize) bool { 2484 const cid = self.detailInner(index).getFocus().child_id orelse return false; 2485 return cid == self.description_id[index]; 2486 } 2487 2488 // return to the tool row's last-focused button (the header when the 2489 // description page shows no row). 2490 fn focusToolRow(self: *View, index: usize, root_focus: *Focus) void { 2491 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 2492 root_focus.setFocus(cid); 2493 } 2494 2495 // enter the detail pane. an empty pane (no issues) can't be entered. 2496 fn focusDetail(self: *View, index: usize, root_focus: *Focus) !void { 2497 if (self.detailInner(index).children.count() == 0) return; 2498 root_focus.setFocus(self.detailOuter(index).getFocus().id); 2499 } 2500 2501 // return to the list. 2502 fn focusList(self: *View, index: usize, root_focus: *Focus) !void { 2503 root_focus.setFocus(self.listScroll(index).getFocus().id); 2504 } 2505 2506 // cross to the header tabs above the stack. 2507 fn focusHeader(self: *View, root_focus: *Focus) void { 2508 root_focus.setFocus(self.box.children.keys()[header_index]); 2509 } 2510 2511 pub fn clearGrid(self: *View) void { 2512 self.box.clearGrid(); 2513 } 2514 2515 pub fn getGrid(self: View) ?Grid { 2516 return self.box.getGrid(); 2517 } 2518 2519 pub fn getFocus(self: *View) *Focus { 2520 return self.box.getFocus(); 2521 } 2522 2523 // for the parent's "scroll up at the top jumps to the header" check: at the 2524 // top only while the header tabs hold focus, so up from the split 2525 // crosses into the tabs first. 2526 pub fn getSelectedIndex(self: *View) ?usize { 2527 return if (self.headerActive()) 0 else 1; 2528 } 2529 }; 2530 2531 // the "a:" navigation link for `status`'s issues page filtered to the 2532 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 2533 // a rooted url ignores `status` (it derives its view from the issue's own); 2534 // with an empty `id` this is the bare list link. 2535 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 2536 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 2537 const url = try route.toUrl(page_arena); 2538 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2539 } 2540 2541 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 2542 // href is only followed with js off. 2543 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2544 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 2545 const url = try route.toUrl(page_arena); 2546 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 2547 } 2548 2549 // the "a:" link to the window `status_maybe` names, keeping the tag filter, 2550 // or the conflicts window when null, rooted at issue `id`. 2551 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?evt.Issue.Status, id: []const u8) ![]const u8 { 2552 if (status_maybe) |status| return issuesLink(page_arena, data.identity, status, data.tag, id); 2553 return conflictsLink(page_arena, data.identity, id); 2554 } 2555 2556 // the "a:" link to the conflicts list rooted at issue `start` ("" = the 2557 // first window). 2558 fn conflictsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, start: []const u8) ![]const u8 { 2559 const route = ui.RoutablePage.repoIssuesConflictsRoute(identity, start) orelse return error.RouteTooLong; 2560 const url = try route.toUrl(page_arena); 2561 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2562 } 2563 2564 // the "a:" link to the whole-description page of issue `id` within `identity`. 2565 fn descriptionLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2566 const route = ui.RoutablePage.repoIssuesDescriptionRoute(identity, id) orelse return error.RouteTooLong; 2567 const url = try route.toUrl(page_arena); 2568 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2569 } 2570 2571 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 2572 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 2573 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 2574 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 2575 const url = try route.toUrl(page_arena); 2576 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2577 } 2578 2579 // tabs switching between the issues page's views. 2580 pub const Header = struct { 2581 box: wgt.Box(ui.Widget), 2582 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 2583 2584 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 2585 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2586 errdefer box.deinit(allocator); 2587 2588 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 2589 errdefer tab_ids.deinit(allocator); 2590 2591 const aa = session.page_arena.allocator(); 2592 2593 // a list tab per status, labeled with its listing's issue count 2594 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 2595 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 2596 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2597 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 2598 try addTab(allocator, &box, &tab_ids, label, link); 2599 } 2600 2601 // tags tab, labeled with the active tag filter 2602 { 2603 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 2604 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 2605 const label = if (data.tag.len == 0) "tags" else blk: { 2606 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 2607 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 2608 }; 2609 try addTab(allocator, &box, &tab_ids, label, tags_link); 2610 } 2611 2612 // new-issue tab; an edit or resolve url shows its tab in this place 2613 { 2614 const route = switch (data.view) { 2615 .edit => ui.RoutablePage.repoIssuesEditRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 2616 .new_comment => ui.RoutablePage.repoCommentNewRoute(data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 2617 .edit_comment => ui.RoutablePage.repoCommentEditRoute(data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 2618 .resolve => ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 2619 else => ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong, 2620 }; 2621 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2622 const label: []const u8 = switch (data.view) { 2623 .edit => "edit", 2624 .new_comment => "reply", 2625 .edit_comment => "edit", 2626 .resolve => "resolve", 2627 else => "new", 2628 }; 2629 try addTab(allocator, &box, &tab_ids, label, link); 2630 } 2631 2632 // conflicts tab, labeled with the conflict count 2633 if (data.conflicts.count > 0) { 2634 const route = ui.RoutablePage.repoIssuesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 2635 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2636 const label = try std.fmt.allocPrint(aa, "conflicts ({d})", .{data.conflicts.count}); 2637 try addTab(allocator, &box, &tab_ids, label, link); 2638 } 2639 2640 var self = Header{ .box = box, .tab_ids = tab_ids }; 2641 // the tab matching the page's view is selected initially. 2642 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 2643 return self; 2644 } 2645 2646 fn addTab(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), tab_ids: *std.AutoArrayHashMapUnmanaged(usize, void), label: []const u8, link: []const u8) !void { 2647 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2648 errdefer text_box.deinit(allocator); 2649 text_box.getFocus().focusable = true; 2650 text_box.getFocus().kind = .{ .custom = link }; 2651 try tab_ids.put(allocator, text_box.getFocus().id, {}); 2652 try box.children.put(allocator, text_box.getFocus().id, .{ 2653 .widget = .{ .text_box = text_box }, 2654 .rect = null, 2655 .min_size = .{ .width = label.len + 2, .height = null }, 2656 }); 2657 } 2658 2659 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 2660 self.box.deinit(allocator); 2661 self.tab_ids.deinit(allocator); 2662 } 2663 2664 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2665 self.clearGrid(); 2666 // only the selected tab shows its border 2667 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 2668 switch (child.widget) { 2669 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 2670 else => {}, 2671 } 2672 } 2673 try self.box.build(allocator, constraint, root_focus); 2674 } 2675 2676 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2677 _ = allocator; 2678 const current_tab = self.currentTabIndex() orelse return; 2679 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 2680 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 2681 } 2682 } 2683 2684 pub fn clearGrid(self: *Header) void { 2685 self.box.clearGrid(); 2686 } 2687 2688 pub fn getGrid(self: Header) ?Grid { 2689 return self.box.getGrid(); 2690 } 2691 2692 pub fn getFocus(self: *Header) *Focus { 2693 return self.box.getFocus(); 2694 } 2695 2696 pub fn getSelectedIndex(self: Header) ?usize { 2697 return self.currentTabIndex(); 2698 } 2699 2700 fn currentTabIndex(self: Header) ?usize { 2701 const child_id = self.box.focus.child_id orelse return null; 2702 return self.tab_ids.getIndex(child_id); 2703 } 2704 }; 2705