╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1299 1300 // move the focused row in `box` by `delta`, clamped to the ends, scrolling 1301 // `scroll` to keep it visible. 1302 pub fn moveRowFocus(box: *wgt.Box(Widget), scroll: *wgt.Scroll(Widget), root_focus: *Focus, delta: isize) void { 1303 const keys = box.children.keys(); 1304 if (keys.len == 0) return; 1305 const cur_id = box.getFocus().child_id orelse return; 1306 const cur: isize = @intCast(box.children.getIndex(cur_id) orelse return); 1307 const last: isize = @intCast(keys.len - 1); 1308 const next: usize = @intCast(std.math.clamp(cur + delta, 0, last)); 1309 if (next == @as(usize, @intCast(cur))) return; 1310 root_focus.setFocus(keys[next]); 1311 if (box.children.values()[next].rect) |rect| scroll.scrollToRect(rect); 1312 } 1313 1314 pub const FlowBox = struct { 1315 focus: *Focus, 1316 grid: ?Grid, 1317 text_boxes: std.ArrayList(wgt.TextBox(Widget)), 1318 // backs the per-item strings the text boxes borrow: each box's `content` 1319 // and, for link items, its `.custom` focus kind (e.g. "a:/user/foo"). reset 1320 // wholesale on each setItems so there's no per-string ownership to track. 1321 arena: std.heap.ArenaAllocator, 1322 // column count from the last build — FlowBox.Scroll.input uses it so arrow 1323 // up/down can step by a row's worth of items. 1324 last_cols: usize, 1325 options: Options, 1326 1327 const border_rows: usize = 2; 1328 1329 pub const Options = struct { 1330 cell_width: usize = 40, 1331 cell_height: usize = 3, 1332 }; 1333 1334 pub fn init(allocator: std.mem.Allocator, options: Options) !FlowBox { 1335 return .{ 1336 .focus = try Focus.create(allocator, .container), 1337 .grid = null, 1338 .text_boxes = .empty, 1339 .arena = std.heap.ArenaAllocator.init(allocator), 1340 .last_cols = 1, 1341 .options = options, 1342 }; 1343 } 1344 1345 pub fn deinit(self: *FlowBox, allocator: std.mem.Allocator) void { 1346 self.focus.destroy(allocator); 1347 if (self.grid) |*grid| { 1348 grid.deinit(); 1349 self.grid = null; 1350 } 1351 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1352 self.text_boxes.deinit(allocator); 1353 self.arena.deinit(); 1354 } 1355 1356 // one flow item: its display text plus an optional link. a non-empty `link` 1357 // sets the item's focus kind such as `.{ .custom = "a:/user/foo" }`, which 1358 // the web renderer turns into an anchor. 1359 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 1360 1361 // both the item text and the link are copied into the arena, so the caller's 1362 // slices needn't outlive this call. 1363 pub fn setItems(self: *FlowBox, allocator: std.mem.Allocator, items: []const Item) !void { 1364 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1365 self.text_boxes.clearAndFree(allocator); 1366 1367 // the old text boxes (and their borrowed content/links) are gone now, so 1368 // the arena that backed those strings can be reclaimed. 1369 _ = self.arena.reset(.retain_capacity); 1370 const aa = self.arena.allocator(); 1371 1372 self.focus.clear(); 1373 self.focus.child_id = null; 1374 1375 for (items) |item| { 1376 const line = try aa.dupe(u8, item.text); 1377 1378 var text_box = try wgt.TextBox(Widget).init(allocator, line, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 1379 errdefer text_box.deinit(allocator); 1380 text_box.getFocus().focusable = true; 1381 1382 if (item.link.len > 0) { 1383 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 1384 } 1385 1386 try self.text_boxes.append(allocator, text_box); 1387 } 1388 1389 if (self.text_boxes.items.len > 0) { 1390 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 1391 } 1392 } 1393 1394 pub fn build(self: *FlowBox, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1395 self.clearGrid(); 1396 self.focus.clear(); 1397 1398 const cell_width = self.options.cell_width; 1399 const max_width = constraint.max_size.width orelse cell_width; 1400 const cols = if (cell_width == 0) 1 else @max(1, max_width / cell_width); 1401 self.last_cols = cols; 1402 1403 const count = self.text_boxes.items.len; 1404 if (count == 0) return; 1405 const slot_height = self.options.cell_height + border_rows; 1406 if (slot_height == 0) return; 1407 1408 // build at the slot size so every text box fits a single grid cell 1409 for (self.text_boxes.items) |*tb| { 1410 tb.options.border_style = if (self.focus.child_id == tb.getFocus().id) .single else .hidden; 1411 try tb.build(allocator, .{ 1412 .min_size = .{ .width = cell_width, .height = null }, 1413 .max_size = .{ .width = cell_width, .height = slot_height }, 1414 }, root_focus); 1415 } 1416 1417 const rows = (count + cols - 1) / cols; 1418 const content_height = rows * slot_height; 1419 const total_width = cols * cell_width; 1420 if (total_width == 0 or content_height == 0) return; 1421 1422 var grid = try Grid.init(allocator, .{ .width = total_width, .height = content_height }); 1423 errdefer grid.deinit(); 1424 1425 for (self.text_boxes.items, 0..) |*tb, i| { 1426 const tb_grid = tb.getGrid() orelse continue; 1427 const col = i % cols; 1428 const row = i / cols; 1429 const cell_x = col * cell_width; 1430 const cell_y = row * slot_height; 1431 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, cell_x, cell_y); 1432 try grid.drawGrid(tb_grid, cell_x, cell_y); 1433 } 1434 1435 self.grid = grid; 1436 } 1437 1438 pub fn input(self: *FlowBox, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1439 _ = self; 1440 _ = allocator; 1441 _ = key; 1442 _ = root_focus; 1443 } 1444 1445 pub fn clearGrid(self: *FlowBox) void { 1446 if (self.grid) |*grid| { 1447 grid.deinit(); 1448 self.grid = null; 1449 } 1450 for (self.text_boxes.items) |*tb| tb.clearGrid(); 1451 } 1452 1453 pub fn getGrid(self: FlowBox) ?Grid { 1454 return self.grid; 1455 } 1456 1457 pub fn getFocus(self: *FlowBox) *Focus { 1458 return self.focus; 1459 } 1460 1461 pub fn cellRect(self: FlowBox, index: usize) ?layout.IRect { 1462 if (self.last_cols == 0 or index >= self.text_boxes.items.len) return null; 1463 const slot_height = self.options.cell_height + border_rows; 1464 const col = index % self.last_cols; 1465 const row = index / self.last_cols; 1466 return .{ 1467 .x = @intCast(col * self.options.cell_width), 1468 .y = @intCast(row * slot_height), 1469 .size = .{ .width = self.options.cell_width, .height = slot_height }, 1470 }; 1471 } 1472 1473 pub fn indexOfFocusId(self: FlowBox, focus_id: usize) ?usize { 1474 for (self.text_boxes.items, 0..) |tb, i| { 1475 if (tb.box.focus.id == focus_id) return i; 1476 } 1477 return null; 1478 } 1479 1480 pub const Scroll = struct { 1481 scroll: wgt.Scroll(Widget), 1482 1483 pub fn init(allocator: std.mem.Allocator, options: FlowBox.Options, web_native: bool) !Scroll { 1484 var layout_inner = try FlowBox.init(allocator, options); 1485 errdefer layout_inner.deinit(allocator); 1486 var scroll = try wgt.Scroll(Widget).init(allocator, .{ .flow_box = layout_inner }, .{ .direction = .vert, .web_native = web_native }); 1487 errdefer scroll.deinit(allocator); 1488 return .{ .scroll = scroll }; 1489 } 1490 1491 pub fn deinit(self: *Scroll, allocator: std.mem.Allocator) void { 1492 self.scroll.deinit(allocator); 1493 } 1494 1495 pub fn setItems(self: *Scroll, allocator: std.mem.Allocator, items: []const Item) !void { 1496 self.scroll.x = 0; 1497 self.scroll.y = 0; 1498 try self.inner().setItems(allocator, items); 1499 } 1500 1501 pub fn build(self: *Scroll, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1502 try self.scroll.build(allocator, constraint, root_focus); 1503 } 1504 1505 pub fn input(self: *Scroll, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1506 _ = allocator; 1507 const in = self.inner(); 1508 const child_id = in.focus.child_id orelse return; 1509 const current_index = in.indexOfFocusId(child_id) orelse return; 1510 const count = in.text_boxes.items.len; 1511 if (count == 0) return; 1512 const cols = in.last_cols; 1513 const slot_height = in.options.cell_height + FlowBox.border_rows; 1514 1515 var index = current_index; 1516 switch (key) { 1517 .arrow_up => index -|= cols, 1518 .arrow_down => if (index + cols < count) { 1519 index += cols; 1520 }, 1521 .arrow_left => index -|= 1, 1522 .arrow_right => if (index + 1 < count) { 1523 index += 1; 1524 }, 1525 .home => index = 0, 1526 .end => index = count - 1, 1527 .page_up => { 1528 if (self.scroll.grid) |grid| if (slot_height > 0) { 1529 const rows_per_page = grid.size.height / slot_height; 1530 index -|= rows_per_page * cols; 1531 }; 1532 }, 1533 .page_down => { 1534 if (self.scroll.grid) |grid| if (slot_height > 0) { 1535 const rows_per_page = grid.size.height / slot_height; 1536 index = @min(index + rows_per_page * cols, count - 1); 1537 }; 1538 }, 1539 // scroll wheel moves the focused cell by a full row so the 1540 // viewport (via scrollToRect) follows in row-sized steps, 1541 // matching how a scroll wheel feels in a grid view. 1542 .mouse => |mouse| switch (mouse.action) { 1543 .scroll => |dir| switch (dir) { 1544 .up => index -|= cols, 1545 .down => if (index + cols < count) { 1546 index += cols; 1547 }, 1548 }, 1549 else => {}, 1550 }, 1551 else => {}, 1552 } 1553 1554 if (index != current_index) { 1555 root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 1556 if (in.cellRect(index)) |rect| { 1557 self.scroll.scrollToRect(rect); 1558 } 1559 } 1560 } 1561 1562 pub fn clearGrid(self: *Scroll) void { 1563 self.scroll.clearGrid(); 1564 } 1565 1566 pub fn getGrid(self: Scroll) ?Grid { 1567 return self.scroll.getGrid(); 1568 } 1569 1570 pub fn getFocus(self: *Scroll) *Focus { 1571 return self.scroll.getFocus(); 1572 } 1573 1574 pub fn getSelectedIndex(self: Scroll) ?usize { 1575 const in = self.scroll.child.flow_box; 1576 const child_id = in.focus.child_id orelse return null; 1577 return in.indexOfFocusId(child_id); 1578 } 1579 1580 fn inner(self: *Scroll) *FlowBox { 1581 return &self.scroll.child.flow_box; 1582 } 1583 }; 1584 }; 1585 1586 // a left-to-right flow of variable-width focusable items that wraps at the 1587 // available width, like words in a paragraph. unlike FlowBox there is no cell 1588 // grid: each item is as wide as its text. selection movement is driven by the 1589 // owning view via indexOfFocusId/verticalNeighbor. 1590 pub const TagFlow = struct { 1591 focus: *Focus, 1592 grid: ?Grid, 1593 text_boxes: std.ArrayList(wgt.TextBox(Widget)), 1594 // last build's item rects (content space), for vertical navigation and 1595 // scroll-into-view. 1596 rects: std.ArrayList(layout.IRect), 1597 // backs the per-item strings the text boxes borrow. 1598 arena: std.heap.ArenaAllocator, 1599 1600 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 1601 1602 pub fn init(allocator: std.mem.Allocator) !TagFlow { 1603 return .{ 1604 .focus = try Focus.create(allocator, .container), 1605 .grid = null, 1606 .text_boxes = .empty, 1607 .rects = .empty, 1608 .arena = std.heap.ArenaAllocator.init(allocator), 1609 }; 1610 } 1611 1612 pub fn deinit(self: *TagFlow, allocator: std.mem.Allocator) void { 1613 self.focus.destroy(allocator); 1614 if (self.grid) |*grid| { 1615 grid.deinit(); 1616 self.grid = null; 1617 } 1618 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1619 self.text_boxes.deinit(allocator); 1620 self.rects.deinit(allocator); 1621 self.arena.deinit(); 1622 } 1623 1624 // both the item text and the link are copied into the arena, so the caller's 1625 // slices needn't outlive this call. 1626 pub fn setItems(self: *TagFlow, allocator: std.mem.Allocator, items: []const Item) !void { 1627 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1628 self.text_boxes.clearAndFree(allocator); 1629 1630 _ = self.arena.reset(.retain_capacity); 1631 const aa = self.arena.allocator(); 1632 1633 self.focus.clear(); 1634 self.focus.child_id = null; 1635 1636 for (items) |item| { 1637 const line = try aa.dupe(u8, item.text); 1638 1639 var text_box = try wgt.TextBox(Widget).init(allocator, line, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1640 errdefer text_box.deinit(allocator); 1641 text_box.getFocus().focusable = true; 1642 1643 if (item.link.len > 0) { 1644 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 1645 } 1646 1647 try self.text_boxes.append(allocator, text_box); 1648 } 1649 1650 if (self.text_boxes.items.len > 0) { 1651 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 1652 } 1653 } 1654 1655 pub fn build(self: *TagFlow, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1656 self.clearGrid(); 1657 self.focus.clear(); 1658 1659 if (self.text_boxes.items.len == 0) return; 1660 const max_width = constraint.max_size.width orelse 80; 1661 if (max_width == 0) return; 1662 1663 // lay the items out left to right, wrapping to a new row when the next 1664 // one wouldn't fit. 1665 self.rects.clearRetainingCapacity(); 1666 var x: usize = 0; 1667 var y: usize = 0; 1668 var row_height: usize = 0; 1669 var total_width: usize = 0; 1670 for (self.text_boxes.items) |*tb| { 1671 try tb.build(allocator, .{ 1672 .min_size = .{ .width = null, .height = null }, 1673 .max_size = .{ .width = max_width, .height = null }, 1674 }, root_focus); 1675 const tb_grid_maybe = tb.getGrid(); 1676 const w: usize = if (tb_grid_maybe) |g| g.size.width else 0; 1677 const h: usize = if (tb_grid_maybe) |g| g.size.height else 0; 1678 if (x > 0 and x + w > max_width) { 1679 x = 0; 1680 y += row_height; 1681 row_height = 0; 1682 } 1683 try self.rects.append(allocator, .{ .x = @intCast(x), .y = @intCast(y), .size = .{ .width = w, .height = h } }); 1684 if (h > row_height) row_height = h; 1685 x += w; 1686 if (x > total_width) total_width = x; 1687 } 1688 1689 const total_height = y + row_height; 1690 if (total_width == 0 or total_height == 0) return; 1691 1692 var grid = try Grid.init(allocator, .{ .width = total_width, .height = total_height }); 1693 errdefer grid.deinit(); 1694 1695 for (self.text_boxes.items, self.rects.items) |*tb, rect| { 1696 const tb_grid = tb.getGrid() orelse continue; 1697 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, @intCast(rect.x), @intCast(rect.y)); 1698 try grid.drawGrid(tb_grid, @intCast(rect.x), @intCast(rect.y)); 1699 } 1700 1701 self.grid = grid; 1702 } 1703 1704 pub fn input(self: *TagFlow, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1705 _ = self; 1706 _ = allocator; 1707 _ = key; 1708 _ = root_focus; 1709 } 1710 1711 pub fn clearGrid(self: *TagFlow) void { 1712 if (self.grid) |*grid| { 1713 grid.deinit(); 1714 self.grid = null; 1715 } 1716 for (self.text_boxes.items) |*tb| tb.clearGrid(); 1717 } 1718 1719 pub fn getGrid(self: TagFlow) ?Grid { 1720 return self.grid; 1721 } 1722 1723 pub fn getFocus(self: *TagFlow) *Focus { 1724 return self.focus; 1725 } 1726 1727 pub fn indexOfFocusId(self: TagFlow, focus_id: usize) ?usize { 1728 for (self.text_boxes.items, 0..) |tb, i| { 1729 if (tb.box.focus.id == focus_id) return i; 1730 } 1731 return null; 1732 } 1733 1734 // the first item of the row below (when `downward`) or above, or null at 1735 // the flow's edge. items are laid out in order, so y is nondecreasing. 1736 pub fn rowStep(self: TagFlow, index: usize, downward: bool) ?usize { 1737 const rects = self.rects.items; 1738 if (index >= rects.len) return null; 1739 const cur_y = rects[index].y; 1740 if (downward) { 1741 for (rects[index + 1 ..], index + 1..) |r, i| { 1742 if (r.y > cur_y) return i; 1743 } 1744 return null; 1745 } 1746 // walk the earlier rows; the last one visited is the row above. 1747 var prev_first: ?usize = null; 1748 var i: usize = 0; 1749 while (i < rects.len and rects[i].y < cur_y) { 1750 prev_first = i; 1751 const y = rects[i].y; 1752 while (i < rects.len and rects[i].y == y) i += 1; 1753 } 1754 return prev_first; 1755 } 1756 }; 1757 1758 // an invisible widget that fills the horizontal space granted by its parent. 1759 // used inside Box(horiz) with a min_size so the box reserves space for the 1760 // children that follow, pushing them to the right. 1761 pub const Spacer = struct { 1762 focus: *Focus, 1763 grid: ?Grid, 1764 1765 pub fn init(allocator: std.mem.Allocator) !Spacer { 1766 return .{ 1767 .focus = try Focus.create(allocator, .container), 1768 .grid = null, 1769 }; 1770 } 1771 1772 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 1773 self.focus.destroy(allocator); 1774 if (self.grid) |*grid| { 1775 grid.deinit(); 1776 self.grid = null; 1777 } 1778 } 1779 1780 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1781 _ = root_focus; 1782 self.clearGrid(); 1783 const width = constraint.max_size.width orelse return; 1784 if (width == 0) return; 1785 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1786 } 1787 1788 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1789 _ = self; 1790 _ = allocator; 1791 _ = key; 1792 _ = root_focus; 1793 } 1794 1795 pub fn clearGrid(self: *Spacer) void { 1796 if (self.grid) |*grid| { 1797 grid.deinit(); 1798 self.grid = null; 1799 } 1800 } 1801 1802 pub fn getGrid(self: Spacer) ?Grid { 1803 return self.grid; 1804 } 1805 1806 pub fn getFocus(self: *Spacer) *Focus { 1807 return self.focus; 1808 } 1809 }; 1810 1811 // a one-row, non-focusable status bar showing the current page's url 1812 pub const Footer = struct { 1813 focus: *Focus, 1814 grid: ?Grid, 1815 session: *Session, 1816 arena: std.heap.ArenaAllocator, 1817 1818 pub fn init(allocator: std.mem.Allocator, session: *Session) !Footer { 1819 return .{ 1820 .focus = try Focus.create(allocator, .container), 1821 .grid = null, 1822 .session = session, 1823 .arena = std.heap.ArenaAllocator.init(allocator), 1824 }; 1825 } 1826 1827 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 1828 self.focus.destroy(allocator); 1829 if (self.grid) |*grid| grid.deinit(); 1830 self.arena.deinit(); 1831 } 1832 1833 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1834 _ = root_focus; 1835 self.clearGrid(); 1836 const width = constraint.max_size.width orelse return; 1837 if (width == 0) return; 1838 1839 _ = self.arena.reset(.retain_capacity); 1840 const aa = self.arena.allocator(); 1841 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 1842 const text = if (self.session.web_port) |port| 1843 std.fmt.allocPrint(aa, "http://localhost:{d}{s}", .{ port, path }) catch return 1844 else 1845 path; 1846 1847 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1848 errdefer grid.deinit(); 1849 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 1850 var i: usize = 0; 1851 while (utf8.nextCodepointSlice()) |ch| { 1852 if (i == width) break; 1853 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 1854 i += 1; 1855 } 1856 self.grid = grid; 1857 } 1858 1859 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1860 _ = self; 1861 _ = allocator; 1862 _ = key; 1863 _ = root_focus; 1864 } 1865 1866 pub fn clearGrid(self: *Footer) void { 1867 if (self.grid) |*grid| { 1868 grid.deinit(); 1869 self.grid = null; 1870 } 1871 } 1872 1873 pub fn getGrid(self: Footer) ?Grid { 1874 return self.grid; 1875 } 1876 1877 pub fn getFocus(self: *Footer) *Focus { 1878 return self.focus; 1879 } 1880 }; 1881 1882 // a single-child wrapper that builds the child at its natural size and 1883 // positions its grid in the middle of the area granted by the parent 1884 pub const Center = struct { 1885 focus: *Focus, 1886 grid: ?Grid, 1887 child: *Widget, 1888 1889 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 1890 const child = try allocator.create(Widget); 1891 errdefer allocator.destroy(child); 1892 child.* = child_widget; 1893 return .{ 1894 .focus = try Focus.create(allocator, .container), 1895 .grid = null, 1896 .child = child, 1897 }; 1898 } 1899 1900 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1901 self.focus.destroy(allocator); 1902 if (self.grid) |*grid| { 1903 grid.deinit(); 1904 self.grid = null; 1905 } 1906 self.child.deinit(allocator); 1907 allocator.destroy(self.child); 1908 } 1909 1910 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1911 self.clearGrid(); 1912 self.getFocus().clear(); 1913 1914 // build the child without forcing it to fill min_size; it sizes 1915 // itself to its content within the available max. 1916 try self.child.build(allocator, .{ 1917 .min_size = .{ .width = null, .height = null }, 1918 .max_size = constraint.max_size, 1919 }, root_focus); 1920 1921 const child_grid = self.child.getGrid() orelse return; 1922 1923 // prefer max when given; otherwise grow to max(min, child) so that 1924 // when only a min is set (e.g. the wasm path passes viewport rows 1925 // as min_height with no max), we can still vertically center while 1926 // letting taller content extend past the min. 1927 const width = if (constraint.max_size.width) |w| w else if (constraint.min_size.width) |min_w| @max(min_w, child_grid.size.width) else child_grid.size.width; 1928 const height = if (constraint.max_size.height) |h| h else if (constraint.min_size.height) |min_h| @max(min_h, child_grid.size.height) else child_grid.size.height; 1929 if (width == 0 or height == 0) return; 1930 1931 const offset_x: usize = (width -| child_grid.size.width) / 2; 1932 const offset_y: usize = (height -| child_grid.size.height) / 2; 1933 1934 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1935 errdefer grid.deinit(); 1936 try grid.drawGrid(child_grid, offset_x, offset_y); 1937 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1938 self.getFocus().child_id = self.child.getFocus().id; 1939 1940 self.grid = grid; 1941 } 1942 1943 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1944 try self.child.input(allocator, key, root_focus); 1945 } 1946 1947 pub fn clearGrid(self: *Center) void { 1948 if (self.grid) |*grid| { 1949 grid.deinit(); 1950 self.grid = null; 1951 } 1952 self.child.clearGrid(); 1953 } 1954 1955 pub fn getGrid(self: Center) ?Grid { 1956 return self.grid; 1957 } 1958 1959 pub fn getFocus(self: *Center) *Focus { 1960 return self.focus; 1961 } 1962 }; 1963 1964 // renders truecolor ANSI art 1965 pub const AnsiArt = struct { 1966 focus: *Focus, 1967 grid: ?Grid, 1968 content: []const u8, 1969 1970 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1971 return .{ 1972 .focus = try Focus.create(allocator, .container), 1973 .grid = null, 1974 .content = content, 1975 }; 1976 } 1977 1978 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1979 self.focus.destroy(allocator); 1980 if (self.grid) |*grid| { 1981 grid.deinit(); 1982 self.grid = null; 1983 } 1984 } 1985 1986 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1987 _ = root_focus; 1988 self.clearGrid(); 1989 1990 // parse into rows of cells, then copy into a rectangular grid 1991 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1992 defer { 1993 for (rows.items) |*row| row.deinit(allocator); 1994 rows.deinit(allocator); 1995 } 1996 var row: std.ArrayList(Grid.Cell) = .empty; 1997 errdefer row.deinit(allocator); 1998 1999 var style: Grid.Style = .{}; 2000 var width: usize = 0; 2001 const content = self.content; 2002 var i: usize = 0; 2003 while (i < content.len) { 2004 const byte = content[i]; 2005 if (byte == '\n') { 2006 width = @max(width, row.items.len); 2007 try rows.append(allocator, row); 2008 row = .empty; 2009 i += 1; 2010 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 2011 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 2012 var j = i + 2; 2013 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 2014 if (j >= content.len) { 2015 i = content.len; // malformed trailing escape; stop 2016 } else { 2017 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 2018 i = j + 1; 2019 } 2020 } else { 2021 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 2022 const end = @min(content.len, i + len); 2023 const rune = content[i..end]; 2024 const transparent = end - i == 1 and rune[0] == ' ' and 2025 style.fg == null and style.bg == null and !style.inverted; 2026 try row.append(allocator, .{ 2027 .rune = if (transparent) null else rune, 2028 .style = style, 2029 }); 2030 i = end; 2031 } 2032 } 2033 // a trailing row with content but no closing newline 2034 if (row.items.len > 0) { 2035 width = @max(width, row.items.len); 2036 try rows.append(allocator, row); 2037 } else { 2038 row.deinit(allocator); 2039 } 2040 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 2041 2042 const height = rows.items.len; 2043 if (width == 0 or height == 0) return; 2044 2045 const clamped_w = @min(width, constraint.max_size.width orelse width); 2046 const clamped_h = @min(height, constraint.max_size.height orelse height); 2047 2048 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 2049 errdefer grid.deinit(); 2050 for (rows.items[0..clamped_h], 0..) |r, y| { 2051 const n = @min(r.items.len, clamped_w); 2052 for (r.items[0..n], 0..) |cell, x| { 2053 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 2054 } 2055 } 2056 self.grid = grid; 2057 } 2058 2059 pub fn clearGrid(self: *AnsiArt) void { 2060 if (self.grid) |*grid| { 2061 grid.deinit(); 2062 self.grid = null; 2063 } 2064 } 2065 2066 pub fn getGrid(self: AnsiArt) ?Grid { 2067 return self.grid; 2068 } 2069 2070 pub fn getFocus(self: *AnsiArt) *Focus { 2071 return self.focus; 2072 } 2073 2074 fn applySgr(style: *Grid.Style, params: []const u8) void { 2075 var nums: [16]u32 = undefined; 2076 var n: usize = 0; 2077 var it = std.mem.splitScalar(u8, params, ';'); 2078 while (it.next()) |tok| { 2079 if (n >= nums.len) break; 2080 // an empty parameter (e.g. bare "\x1b[m") means 0 2081 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 2082 n += 1; 2083 } 2084 if (n == 0) { 2085 style.* = .{}; 2086 return; 2087 } 2088 var i: usize = 0; 2089 while (i < n) : (i += 1) { 2090 switch (nums[i]) { 2091 0 => style.* = .{}, 2092 7 => style.inverted = true, 2093 27 => style.inverted = false, 2094 39 => style.fg = null, 2095 49 => style.bg = null, 2096 38, 48 => { 2097 // truecolor form: 38;2;r;g;b — anything else is ignored 2098 if (i + 4 < n and nums[i + 1] == 2) { 2099 const c = Grid.Color{ 2100 .r = @truncate(nums[i + 2]), 2101 .g = @truncate(nums[i + 3]), 2102 .b = @truncate(nums[i + 4]), 2103 }; 2104 if (nums[i] == 38) style.fg = c else style.bg = c; 2105 i += 4; 2106 } 2107 }, 2108 else => {}, 2109 } 2110 } 2111 } 2112 }; 2113 2114 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 2115 pub const AnsiBackground = struct { 2116 grid: ?Grid, 2117 child: *Widget, 2118 art: AnsiArt, 2119 session: *Session, 2120 2121 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 2122 var cw = child_widget; 2123 const child = allocator.create(Widget) catch |e| { 2124 cw.deinit(allocator); 2125 return e; 2126 }; 2127 child.* = cw; 2128 errdefer { 2129 child.deinit(allocator); 2130 allocator.destroy(child); 2131 } 2132 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2133 } 2134 2135 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2136 if (self.grid) |*grid| grid.deinit(); 2137 self.art.deinit(allocator); 2138 self.child.deinit(allocator); 2139 allocator.destroy(self.child); 2140 } 2141 2142 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2143 self.clearGrid(); 2144 2145 // make the wrapped page fill the whole available area 2146 try self.child.build(allocator, .{ 2147 .min_size = .{ 2148 .width = constraint.max_size.width orelse constraint.min_size.width, 2149 .height = constraint.max_size.height orelse constraint.min_size.height, 2150 }, 2151 .max_size = constraint.max_size, 2152 }, root_focus); 2153 2154 if (self.session.data.enable_ansi) { 2155 if (self.child.getGrid()) |fg| { 2156 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 2157 } 2158 } 2159 } 2160 2161 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2162 try self.child.input(allocator, key, root_focus); 2163 } 2164 2165 pub fn clearGrid(self: *AnsiBackground) void { 2166 if (self.grid) |*grid| { 2167 grid.deinit(); 2168 self.grid = null; 2169 } 2170 self.child.clearGrid(); 2171 self.art.clearGrid(); 2172 } 2173 2174 pub fn getGrid(self: AnsiBackground) ?Grid { 2175 return self.grid orelse self.child.getGrid(); 2176 } 2177 2178 pub fn getFocus(self: *AnsiBackground) *Focus { 2179 return self.child.getFocus(); 2180 } 2181 2182 // which top corner the art hugs. 2183 const ArtAnchor = enum { top_left, top_right }; 2184 2185 // a foreground cell counts as blank if it's empty or a bare space without styling 2186 fn cellIsBlank(cell: Grid.Cell) bool { 2187 if (cell.rune) |rune| { 2188 return std.mem.eql(u8, rune, " ") and 2189 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2190 } 2191 return true; 2192 } 2193 2194 // the art is dimmed to this fraction of its brightness so foreground text 2195 // stays legible over it 2196 const art_brightness = 45; // percent 2197 2198 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2199 const c = color orelse return null; 2200 return .{ 2201 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2202 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2203 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2204 }; 2205 } 2206 2207 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2208 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2209 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2210 // snap such channels clear of that range so the backdrop doesn't blink or 2211 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2212 // actually render truecolor. 2213 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2214 const c = color orelse return null; 2215 const snap = struct { 2216 fn f(v: u8) u8 { 2217 return if (v >= 1 and v <= 9) 10 else v; 2218 } 2219 }.f; 2220 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2221 } 2222 2223 // composites ANSI art behind a foreground grid 2224 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 2225 art.clearGrid(); 2226 try art.build(allocator, .{ 2227 .min_size = .{ .width = null, .height = null }, 2228 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2229 }, root_focus); 2230 2231 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2232 errdefer out.deinit(); 2233 2234 if (art.getGrid()) |art_grid| { 2235 const anchor_x = switch (anchor) { 2236 .top_left => 0, 2237 .top_right => foreground.size.width -| art_grid.size.width, 2238 }; 2239 for (0..art_grid.size.height) |y| { 2240 for (0..art_grid.size.width) |x| { 2241 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2242 if (src.rune == null) continue; 2243 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2244 const dst = &out.cells.items[idx]; 2245 // composite the dimmed art behind every cell so it shows 2246 // through the whole UI: a blank cell becomes the art, while a 2247 // cell with a glyph keeps the glyph but takes the dimmed art as 2248 // its background (replacing whatever background it had, which 2249 // would otherwise obscure the art). 2250 if (cellIsBlank(dst.*)) { 2251 dst.* = src; 2252 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2253 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2254 } else { 2255 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2256 } 2257 } 2258 } 2259 } 2260 return out; 2261 } 2262 }; 2263