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