╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1309 const count = tf.text_boxes.items.len; 1310 const sc = self.detailScroll(index); 1311 switch (key) { 1312 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 1313 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 1314 .arrow_up => if (tf.rowStep(cur, false)) |i| self.focusTag(index, tf, root_focus, i) else self.focusAuthor(index, root_focus), 1315 .arrow_down => if (tf.rowStep(cur, true)) |i| self.focusTag(index, tf, root_focus, i) else try self.focusDescription(index, root_focus), 1316 .home => self.focusTag(index, tf, root_focus, 0), 1317 .end => self.focusTag(index, tf, root_focus, count - 1), 1318 .mouse => |mouse| switch (mouse.action) { 1319 .scroll => |dir| { 1320 sc.y += if (dir == .up) @as(isize, -1) else 1; 1321 sc.clampToContent(); 1322 }, 1323 else => {}, 1324 }, 1325 else => {}, 1326 } 1327 } 1328 1329 const button_in_row_index: usize = 1; 1330 const edit_in_row_index: usize = 2; 1331 const title_child_index: usize = 0; 1332 const tags_child_index: usize = 2; 1333 1334 fn tagFlow(self: *View, index: usize) ?*ui.TagFlow { 1335 const inner = self.detailInner(index); 1336 if (inner.children.count() <= tags_child_index) return null; 1337 return switch (inner.children.values()[tags_child_index].widget) { 1338 .tag_flow => |*tf| tf, 1339 else => null, 1340 }; 1341 } 1342 1343 fn tagsFocused(self: *View, index: usize) bool { 1344 const inner = self.detailInner(index); 1345 const cid = inner.getFocus().child_id orelse return false; 1346 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 1347 } 1348 1349 // the open/close button inside the detail frame's tool row. 1350 fn statusButton(self: *View, index: usize) ?*wgt.TextBox(ui.Widget) { 1351 const row = self.toolRow(index); 1352 if (row.children.count() == 0) return null; 1353 return &row.children.values()[button_in_row_index].widget.text_box; 1354 } 1355 1356 fn toolRowFocused(self: *View, index: usize) bool { 1357 const outer = self.detailOuter(index); 1358 const cid = outer.getFocus().child_id orelse return false; 1359 return outer.children.getIndex(cid) == tool_row_index; 1360 } 1361 1362 fn focusTag(self: *View, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 1363 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 1364 // keep the tag visible on the terminal: its rect offset by the flow's 1365 // position in the pane. 1366 if (self.session.is_terminal and item < tf.rects.items.len) { 1367 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 1368 var rect = tf.rects.items[item]; 1369 rect.x += flow_rect.x; 1370 rect.y += flow_rect.y; 1371 self.detailScroll(index).scrollToRect(rect); 1372 } 1373 } 1374 } 1375 1376 fn titleFocused(self: *View, index: usize) bool { 1377 const inner = self.detailInner(index); 1378 const cid = inner.getFocus().child_id orelse return false; 1379 return inner.children.getIndex(cid) == title_child_index; 1380 } 1381 1382 fn authorPresent(self: *View, index: usize) bool { 1383 return self.author_id[index] != null; 1384 } 1385 1386 fn authorFocused(self: *View, index: usize) bool { 1387 const inner = self.detailInner(index); 1388 const cid = inner.getFocus().child_id orelse return false; 1389 return cid == self.author_id[index]; 1390 } 1391 1392 // the author sits right under the title at the top of the pane, so 1393 // focusing it scrolls there. 1394 fn focusAuthor(self: *View, index: usize, root_focus: *Focus) void { 1395 const id = self.author_id[index] orelse return; 1396 root_focus.setFocus(id); 1397 self.detailScroll(index).y = 0; 1398 } 1399 1400 // the title sits at the top of the pane, so focusing it scrolls there. 1401 fn focusTitle(self: *View, index: usize, root_focus: *Focus) void { 1402 const inner = self.detailInner(index); 1403 if (inner.children.count() == 0) return; 1404 root_focus.setFocus(inner.children.keys()[title_child_index]); 1405 self.detailScroll(index).y = 0; 1406 } 1407 1408 fn focusTags(self: *View, index: usize, root_focus: *Focus) !void { 1409 const tf = self.tagFlow(index) orelse return; 1410 if (tf.text_boxes.items.len == 0) return; 1411 const cid = tf.focus.child_id orelse tf.text_boxes.items[0].getFocus().id; 1412 const item = tf.indexOfFocusId(cid) orelse 0; 1413 self.focusTag(index, tf, root_focus, item); 1414 } 1415 1416 fn focusDescription(self: *View, index: usize, root_focus: *Focus) !void { 1417 if (self.description_id[index]) |id| root_focus.setFocus(id); 1418 } 1419 1420 // return to the tool row's last-focused button (the header when the 1421 // description page shows no row). 1422 fn focusToolRow(self: *View, index: usize, root_focus: *Focus) void { 1423 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 1424 root_focus.setFocus(cid); 1425 } 1426 1427 // enter the detail pane. an empty pane (no issues) can't be entered. 1428 fn focusDetail(self: *View, index: usize, root_focus: *Focus) !void { 1429 if (self.detailInner(index).children.count() == 0) return; 1430 root_focus.setFocus(self.detailOuter(index).getFocus().id); 1431 } 1432 1433 // return to the list. 1434 fn focusList(self: *View, index: usize, root_focus: *Focus) !void { 1435 root_focus.setFocus(self.listScroll(index).getFocus().id); 1436 } 1437 1438 // cross to the header tabs above the stack. 1439 fn focusHeader(self: *View, root_focus: *Focus) void { 1440 root_focus.setFocus(self.box.children.keys()[header_index]); 1441 } 1442 1443 pub fn clearGrid(self: *View) void { 1444 self.box.clearGrid(); 1445 } 1446 1447 pub fn getGrid(self: View) ?Grid { 1448 return self.box.getGrid(); 1449 } 1450 1451 pub fn getFocus(self: *View) *Focus { 1452 return self.box.getFocus(); 1453 } 1454 1455 // for the parent's "scroll up at the top jumps to the header" check: at the 1456 // top only while the header tabs hold focus, so up from the split 1457 // crosses into the tabs first. 1458 pub fn getSelectedIndex(self: *View) ?usize { 1459 return if (self.headerActive()) 0 else 1; 1460 } 1461 }; 1462 1463 // the "a:" navigation link for `status`'s issues page filtered to the 1464 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 1465 // a rooted url ignores `status` (it derives its view from the issue's own); 1466 // with an empty `id` this is the bare list link. 1467 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 1468 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 1469 const url = try route.toUrl(page_arena); 1470 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1471 } 1472 1473 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 1474 // href is only followed with js off. 1475 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 1476 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 1477 const url = try route.toUrl(page_arena); 1478 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 1479 } 1480 1481 // the "a:" link to the whole-description page of issue `id` within `identity`. 1482 fn descriptionLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 1483 const route = ui.RoutablePage.repoIssuesDescriptionRoute(identity, id) orelse return error.RouteTooLong; 1484 const url = try route.toUrl(page_arena); 1485 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1486 } 1487 1488 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 1489 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 1490 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 1491 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 1492 const url = try route.toUrl(page_arena); 1493 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1494 } 1495 1496 // tabs switching between the issues page's views. 1497 pub const Header = struct { 1498 box: wgt.Box(ui.Widget), 1499 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 1500 1501 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 1502 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1503 errdefer box.deinit(allocator); 1504 1505 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 1506 errdefer tab_ids.deinit(allocator); 1507 1508 const aa = session.page_arena.allocator(); 1509 1510 // a list tab per status, labeled with its listing's issue count 1511 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 1512 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 1513 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 1514 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 1515 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1516 errdefer text_box.deinit(allocator); 1517 text_box.getFocus().focusable = true; 1518 text_box.getFocus().kind = .{ .custom = link }; 1519 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1520 try box.children.put(allocator, text_box.getFocus().id, .{ 1521 .widget = .{ .text_box = text_box }, 1522 .rect = null, 1523 .min_size = .{ .width = label.len + 2, .height = null }, 1524 }); 1525 } 1526 1527 // tags tab, labeled with the active tag filter 1528 { 1529 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 1530 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 1531 const label = if (data.tag.len == 0) "tags" else blk: { 1532 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 1533 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 1534 }; 1535 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1536 errdefer text_box.deinit(allocator); 1537 text_box.getFocus().focusable = true; 1538 text_box.getFocus().kind = .{ .custom = tags_link }; 1539 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1540 try box.children.put(allocator, text_box.getFocus().id, .{ 1541 .widget = .{ .text_box = text_box }, 1542 .rect = null, 1543 .min_size = .{ .width = label.len + 2, .height = null }, 1544 }); 1545 } 1546 1547 // new-issue tab; an edit url shows the edit tab in its place 1548 { 1549 const route = if (data.view == .edit) 1550 ui.RoutablePage.repoIssuesEditRoute(data.identity, data.selected_id) orelse return error.RouteTooLong 1551 else 1552 ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong; 1553 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 1554 const label: []const u8 = if (data.view == .edit) "edit" else "new"; 1555 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1556 errdefer text_box.deinit(allocator); 1557 text_box.getFocus().focusable = true; 1558 text_box.getFocus().kind = .{ .custom = link }; 1559 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1560 try box.children.put(allocator, text_box.getFocus().id, .{ 1561 .widget = .{ .text_box = text_box }, 1562 .rect = null, 1563 .min_size = .{ .width = label.len + 2, .height = null }, 1564 }); 1565 } 1566 1567 var self = Header{ .box = box, .tab_ids = tab_ids }; 1568 // the tab matching the page's view is selected initially. 1569 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 1570 return self; 1571 } 1572 1573 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 1574 self.box.deinit(allocator); 1575 self.tab_ids.deinit(allocator); 1576 } 1577 1578 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1579 self.clearGrid(); 1580 // only the selected tab shows its border 1581 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 1582 switch (child.widget) { 1583 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 1584 else => {}, 1585 } 1586 } 1587 try self.box.build(allocator, constraint, root_focus); 1588 } 1589 1590 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1591 _ = allocator; 1592 const current_tab = self.currentTabIndex() orelse return; 1593 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 1594 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 1595 } 1596 } 1597 1598 pub fn clearGrid(self: *Header) void { 1599 self.box.clearGrid(); 1600 } 1601 1602 pub fn getGrid(self: Header) ?Grid { 1603 return self.box.getGrid(); 1604 } 1605 1606 pub fn getFocus(self: *Header) *Focus { 1607 return self.box.getFocus(); 1608 } 1609 1610 pub fn getSelectedIndex(self: Header) ?usize { 1611 return self.currentTabIndex(); 1612 } 1613 1614 fn currentTabIndex(self: Header) ?usize { 1615 const child_id = self.box.focus.child_id orelse return null; 1616 return self.tab_ids.getIndex(child_id); 1617 } 1618 }; 1619