╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2448 const aa = self.arena.allocator(); 2449 2450 self.focus.clear(); 2451 self.focus.child_id = null; 2452 2453 for (items) |item| { 2454 var text_box = try wgt.TextBox(Widget).init(allocator, item.text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2455 errdefer text_box.deinit(allocator); 2456 text_box.getFocus().focusable = true; 2457 2458 if (item.link.len > 0) { 2459 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 2460 } 2461 2462 try self.text_boxes.append(allocator, text_box); 2463 } 2464 2465 if (self.text_boxes.items.len > 0) { 2466 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 2467 } 2468 } 2469 2470 pub fn build(self: *TagFlow, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2471 self.clearGrid(); 2472 self.focus.clear(); 2473 2474 if (self.text_boxes.items.len == 0) return; 2475 const max_width = constraint.max_size.width orelse 80; 2476 if (max_width == 0) return; 2477 2478 // lay the items out left to right, wrapping to a new row when the next 2479 // one wouldn't fit. 2480 self.rects.clearRetainingCapacity(); 2481 var x: usize = 0; 2482 var y: usize = 0; 2483 var row_height: usize = 0; 2484 var total_width: usize = 0; 2485 for (self.text_boxes.items) |*tb| { 2486 try tb.build(allocator, .{ 2487 .min_size = .{ .width = null, .height = null }, 2488 .max_size = .{ .width = max_width, .height = null }, 2489 }, root_focus); 2490 const tb_grid_maybe = tb.getGrid(); 2491 const w: usize = if (tb_grid_maybe) |g| g.size.width else 0; 2492 const h: usize = if (tb_grid_maybe) |g| g.size.height else 0; 2493 if (x > 0 and x + w > max_width) { 2494 x = 0; 2495 y += row_height; 2496 row_height = 0; 2497 } 2498 try self.rects.append(allocator, .{ .x = @intCast(x), .y = @intCast(y), .size = .{ .width = w, .height = h } }); 2499 if (h > row_height) row_height = h; 2500 x += w; 2501 if (x > total_width) total_width = x; 2502 } 2503 2504 const total_height = y + row_height; 2505 if (total_width == 0 or total_height == 0) return; 2506 2507 var grid = try Grid.init(allocator, .{ .width = total_width, .height = total_height }); 2508 errdefer grid.deinit(); 2509 2510 for (self.text_boxes.items, self.rects.items) |*tb, rect| { 2511 const tb_grid = tb.getGrid() orelse continue; 2512 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, @intCast(rect.x), @intCast(rect.y)); 2513 try grid.drawGrid(tb_grid, @intCast(rect.x), @intCast(rect.y)); 2514 } 2515 2516 self.grid = grid; 2517 } 2518 2519 pub fn input(self: *TagFlow, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2520 _ = self; 2521 _ = allocator; 2522 _ = key; 2523 _ = root_focus; 2524 } 2525 2526 pub fn clearGrid(self: *TagFlow) void { 2527 if (self.grid) |*grid| { 2528 grid.deinit(); 2529 self.grid = null; 2530 } 2531 for (self.text_boxes.items) |*tb| tb.clearGrid(); 2532 } 2533 2534 pub fn getGrid(self: TagFlow) ?Grid { 2535 return self.grid; 2536 } 2537 2538 pub fn getFocus(self: *TagFlow) *Focus { 2539 return self.focus; 2540 } 2541 2542 pub fn indexOfFocusId(self: TagFlow, focus_id: usize) ?usize { 2543 for (self.text_boxes.items, 0..) |tb, i| { 2544 if (tb.box.focus.id == focus_id) return i; 2545 } 2546 return null; 2547 } 2548 2549 // the first item of the row below (when `downward`) or above, or null at 2550 // the flow's edge. items are laid out in order, so y is nondecreasing. 2551 pub fn rowStep(self: TagFlow, index: usize, downward: bool) ?usize { 2552 const rects = self.rects.items; 2553 if (index >= rects.len) return null; 2554 const cur_y = rects[index].y; 2555 if (downward) { 2556 for (rects[index + 1 ..], index + 1..) |r, i| { 2557 if (r.y > cur_y) return i; 2558 } 2559 return null; 2560 } 2561 // walk the earlier rows; the last one visited is the row above. 2562 var prev_first: ?usize = null; 2563 var i: usize = 0; 2564 while (i < rects.len and rects[i].y < cur_y) { 2565 prev_first = i; 2566 const y = rects[i].y; 2567 while (i < rects.len and rects[i].y == y) i += 1; 2568 } 2569 return prev_first; 2570 } 2571 }; 2572 2573 // an invisible widget that fills the horizontal space granted by its parent. 2574 // used inside Box(horiz) with a min_size so the box reserves space for the 2575 // children that follow, pushing them to the right. 2576 pub const Spacer = struct { 2577 focus: *Focus, 2578 grid: ?Grid, 2579 2580 pub fn init(allocator: std.mem.Allocator) !Spacer { 2581 return .{ 2582 .focus = try Focus.create(allocator, .container), 2583 .grid = null, 2584 }; 2585 } 2586 2587 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 2588 self.focus.destroy(allocator); 2589 if (self.grid) |*grid| { 2590 grid.deinit(); 2591 self.grid = null; 2592 } 2593 } 2594 2595 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2596 _ = root_focus; 2597 self.clearGrid(); 2598 const width = constraint.max_size.width orelse return; 2599 if (width == 0) return; 2600 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 2601 } 2602 2603 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2604 _ = self; 2605 _ = allocator; 2606 _ = key; 2607 _ = root_focus; 2608 } 2609 2610 pub fn clearGrid(self: *Spacer) void { 2611 if (self.grid) |*grid| { 2612 grid.deinit(); 2613 self.grid = null; 2614 } 2615 } 2616 2617 pub fn getGrid(self: Spacer) ?Grid { 2618 return self.grid; 2619 } 2620 2621 pub fn getFocus(self: *Spacer) *Focus { 2622 return self.focus; 2623 } 2624 }; 2625 2626 // a one-row, non-focusable status bar showing the current page's url 2627 pub const Footer = struct { 2628 focus: *Focus, 2629 grid: ?Grid, 2630 session: *Session, 2631 arena: std.heap.ArenaAllocator, 2632 url: []const u8 = "", 2633 url_width: usize = 0, 2634 2635 pub fn init(allocator: std.mem.Allocator, session: *Session) !Footer { 2636 return .{ 2637 .focus = try Focus.create(allocator, .container), 2638 .grid = null, 2639 .session = session, 2640 .arena = std.heap.ArenaAllocator.init(allocator), 2641 }; 2642 } 2643 2644 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 2645 self.focus.destroy(allocator); 2646 if (self.grid) |*grid| grid.deinit(); 2647 self.arena.deinit(); 2648 } 2649 2650 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2651 _ = root_focus; 2652 self.clearGrid(); 2653 const width = constraint.max_size.width orelse return; 2654 if (width == 0) return; 2655 2656 _ = self.arena.reset(.retain_capacity); 2657 self.url = ""; 2658 self.url_width = 0; 2659 const aa = self.arena.allocator(); 2660 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 2661 const text = terminalWebUrl(aa, self.session, path) catch return; 2662 self.url = text; 2663 2664 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 2665 errdefer grid.deinit(); 2666 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 2667 var i: usize = 0; 2668 while (utf8.nextCodepointSlice()) |ch| { 2669 if (i == width) break; 2670 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 2671 i += 1; 2672 } 2673 self.url_width = i; 2674 self.grid = grid; 2675 } 2676 2677 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2678 _ = allocator; 2679 switch (key) { 2680 .mouse => |mouse| { 2681 if (mouse.ctrl or !inp.leftClickOn(root_focus, self.getFocus().id, mouse)) return; 2682 const rect = (root_focus.children.get(self.getFocus().id) orelse return).rect; 2683 if (mouse.x < rect.x + self.url_width) self.session.host_request = .{ .show_copyable_text = self.url }; 2684 }, 2685 else => {}, 2686 } 2687 } 2688 2689 pub fn clearGrid(self: *Footer) void { 2690 if (self.grid) |*grid| { 2691 grid.deinit(); 2692 self.grid = null; 2693 } 2694 } 2695 2696 pub fn getGrid(self: Footer) ?Grid { 2697 return self.grid; 2698 } 2699 2700 pub fn getFocus(self: *Footer) *Focus { 2701 return self.focus; 2702 } 2703 }; 2704 2705 // a single-child wrapper that builds the child at its natural size and 2706 // positions its grid in the middle of the area granted by the parent 2707 // a bordered label, indented past the content it precedes 2708 pub const SectionLabel = struct { 2709 box: wgt.Box(Widget), 2710 2711 pub fn init(allocator: std.mem.Allocator, content: []const u8) !SectionLabel { 2712 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2713 errdefer box.deinit(allocator); 2714 2715 { 2716 var gap = try wgt.Text(Widget).init(allocator, " "); 2717 errdefer gap.deinit(allocator); 2718 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 2719 } 2720 2721 { 2722 var tb = try wgt.TextBox(Widget).init(allocator, content, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2723 errdefer tb.deinit(allocator); 2724 tb.getFocus().focusable = true; 2725 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2726 } 2727 2728 box.getFocus().child_id = box.children.keys()[1]; 2729 return .{ .box = box }; 2730 } 2731 2732 pub fn deinit(self: *SectionLabel, allocator: std.mem.Allocator) void { 2733 self.box.deinit(allocator); 2734 } 2735 2736 pub fn build(self: *SectionLabel, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2737 try self.box.build(allocator, constraint, root_focus); 2738 } 2739 2740 pub fn input(self: *SectionLabel, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2741 _ = self; 2742 _ = allocator; 2743 _ = key; 2744 _ = root_focus; 2745 } 2746 2747 pub fn clearGrid(self: *SectionLabel) void { 2748 self.box.clearGrid(); 2749 } 2750 2751 pub fn getGrid(self: SectionLabel) ?Grid { 2752 return self.box.getGrid(); 2753 } 2754 2755 pub fn getFocus(self: *SectionLabel) *Focus { 2756 return self.box.getFocus(); 2757 } 2758 }; 2759 2760 // a submit button, indented past its form's content. the web overlay POSTs 2761 // the button's form; terminal hosts detect presses through `buttonId`. 2762 pub const SubmitButton = struct { 2763 box: wgt.Box(Widget), 2764 2765 pub fn init(allocator: std.mem.Allocator) !SubmitButton { 2766 return initLabeled(allocator, "submit"); 2767 } 2768 2769 pub fn initLabeled(allocator: std.mem.Allocator, label: []const u8) !SubmitButton { 2770 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2771 errdefer box.deinit(allocator); 2772 2773 { 2774 var gap = try wgt.Text(Widget).init(allocator, " "); 2775 errdefer gap.deinit(allocator); 2776 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 2777 } 2778 2779 { 2780 var button = try wgt.TextBox(Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2781 errdefer button.deinit(allocator); 2782 button.getFocus().focusable = true; 2783 // the renderer distinguishes plain clickables from buttons that 2784 // should POST to a server route by this kind. 2785 button.getFocus().kind = .{ .custom = "submit" }; 2786 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 2787 } 2788 2789 box.getFocus().child_id = box.children.keys()[1]; 2790 return .{ .box = box }; 2791 } 2792 2793 pub fn deinit(self: *SubmitButton, allocator: std.mem.Allocator) void { 2794 self.box.deinit(allocator); 2795 } 2796 2797 pub fn build(self: *SubmitButton, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2798 try self.box.build(allocator, constraint, root_focus); 2799 } 2800 2801 pub fn input(self: *SubmitButton, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2802 _ = self; 2803 _ = allocator; 2804 _ = key; 2805 _ = root_focus; 2806 } 2807 2808 pub fn clearGrid(self: *SubmitButton) void { 2809 self.box.clearGrid(); 2810 } 2811 2812 pub fn getGrid(self: SubmitButton) ?Grid { 2813 return self.box.getGrid(); 2814 } 2815 2816 pub fn getFocus(self: *SubmitButton) *Focus { 2817 return self.box.getFocus(); 2818 } 2819 2820 // the button's focus id, for click hit-testing 2821 pub fn buttonId(self: *const SubmitButton) usize { 2822 return self.box.children.keys()[1]; 2823 } 2824 }; 2825 2826 pub const Center = struct { 2827 focus: *Focus, 2828 grid: ?Grid, 2829 child: *Widget, 2830 2831 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 2832 const child = try allocator.create(Widget); 2833 errdefer allocator.destroy(child); 2834 child.* = child_widget; 2835 return .{ 2836 .focus = try Focus.create(allocator, .container), 2837 .grid = null, 2838 .child = child, 2839 }; 2840 } 2841 2842 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 2843 self.focus.destroy(allocator); 2844 if (self.grid) |*grid| { 2845 grid.deinit(); 2846 self.grid = null; 2847 } 2848 self.child.deinit(allocator); 2849 allocator.destroy(self.child); 2850 } 2851 2852 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2853 self.clearGrid(); 2854 self.getFocus().clear(); 2855 2856 // build the child without forcing it to fill min_size; it sizes 2857 // itself to its content within the available max. 2858 try self.child.build(allocator, .{ 2859 .min_size = .{ .width = null, .height = null }, 2860 .max_size = constraint.max_size, 2861 }, root_focus); 2862 2863 const child_grid = self.child.getGrid() orelse return; 2864 2865 // prefer max when given; otherwise grow to max(min, child) so that 2866 // when only a min is set (e.g. the wasm path passes viewport rows 2867 // as min_height with no max), we can still vertically center while 2868 // letting taller content extend past the min. 2869 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; 2870 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; 2871 if (width == 0 or height == 0) return; 2872 2873 const offset_x: usize = (width -| child_grid.size.width) / 2; 2874 const offset_y: usize = (height -| child_grid.size.height) / 2; 2875 2876 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 2877 errdefer grid.deinit(); 2878 try grid.drawGrid(child_grid, offset_x, offset_y); 2879 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 2880 self.getFocus().child_id = self.child.getFocus().id; 2881 2882 self.grid = grid; 2883 } 2884 2885 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2886 try self.child.input(allocator, key, root_focus); 2887 } 2888 2889 pub fn clearGrid(self: *Center) void { 2890 if (self.grid) |*grid| { 2891 grid.deinit(); 2892 self.grid = null; 2893 } 2894 self.child.clearGrid(); 2895 } 2896 2897 pub fn getGrid(self: Center) ?Grid { 2898 return self.grid; 2899 } 2900 2901 pub fn getFocus(self: *Center) *Focus { 2902 return self.focus; 2903 } 2904 }; 2905 2906 // renders truecolor ANSI art 2907 pub const AnsiArt = struct { 2908 focus: *Focus, 2909 grid: ?Grid, 2910 content: []const u8, 2911 2912 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 2913 return .{ 2914 .focus = try Focus.create(allocator, .container), 2915 .grid = null, 2916 .content = content, 2917 }; 2918 } 2919 2920 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 2921 self.focus.destroy(allocator); 2922 if (self.grid) |*grid| { 2923 grid.deinit(); 2924 self.grid = null; 2925 } 2926 } 2927 2928 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2929 _ = root_focus; 2930 self.clearGrid(); 2931 2932 // parse into rows of cells, then copy into a rectangular grid 2933 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 2934 defer { 2935 for (rows.items) |*row| row.deinit(allocator); 2936 rows.deinit(allocator); 2937 } 2938 var row: std.ArrayList(Grid.Cell) = .empty; 2939 errdefer row.deinit(allocator); 2940 2941 var style: Grid.Style = .{}; 2942 var width: usize = 0; 2943 const content = self.content; 2944 var i: usize = 0; 2945 while (i < content.len) { 2946 const byte = content[i]; 2947 if (byte == '\n') { 2948 width = @max(width, row.items.len); 2949 try rows.append(allocator, row); 2950 row = .empty; 2951 i += 1; 2952 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 2953 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 2954 var j = i + 2; 2955 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 2956 if (j >= content.len) { 2957 i = content.len; // malformed trailing escape; stop 2958 } else { 2959 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 2960 i = j + 1; 2961 } 2962 } else { 2963 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 2964 const end = @min(content.len, i + len); 2965 const rune = content[i..end]; 2966 // a foreground color does not make a space visible. only an 2967 // effective background makes it opaque; foreground state often 2968 // remains active through the padding at the end of a row. 2969 const effective_background = if (style.inverted) style.fg else style.bg; 2970 const transparent = end - i == 1 and rune[0] == ' ' and effective_background == null; 2971 try row.append(allocator, .{ 2972 .rune = if (transparent) null else rune, 2973 .style = style, 2974 }); 2975 i = end; 2976 } 2977 } 2978 // a trailing row with content but no closing newline 2979 if (row.items.len > 0) { 2980 width = @max(width, row.items.len); 2981 try rows.append(allocator, row); 2982 } else { 2983 row.deinit(allocator); 2984 } 2985 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 2986 2987 const height = rows.items.len; 2988 if (width == 0 or height == 0) return; 2989 2990 const clamped_w = @min(width, constraint.max_size.width orelse width); 2991 const clamped_h = @min(height, constraint.max_size.height orelse height); 2992 2993 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 2994 errdefer grid.deinit(); 2995 for (rows.items[0..clamped_h], 0..) |r, y| { 2996 const n = @min(r.items.len, clamped_w); 2997 for (r.items[0..n], 0..) |cell, x| { 2998 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 2999 } 3000 } 3001 self.grid = grid; 3002 } 3003 3004 pub fn clearGrid(self: *AnsiArt) void { 3005 if (self.grid) |*grid| { 3006 grid.deinit(); 3007 self.grid = null; 3008 } 3009 } 3010 3011 pub fn getGrid(self: AnsiArt) ?Grid { 3012 return self.grid; 3013 } 3014 3015 pub fn getFocus(self: *AnsiArt) *Focus { 3016 return self.focus; 3017 } 3018 3019 fn applySgr(style: *Grid.Style, params: []const u8) void { 3020 var nums: [16]u32 = undefined; 3021 var n: usize = 0; 3022 var it = std.mem.splitScalar(u8, params, ';'); 3023 while (it.next()) |tok| { 3024 if (n >= nums.len) break; 3025 // an empty parameter (e.g. bare "\x1b[m") means 0 3026 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 3027 n += 1; 3028 } 3029 if (n == 0) { 3030 style.* = .{}; 3031 return; 3032 } 3033 var i: usize = 0; 3034 while (i < n) : (i += 1) { 3035 switch (nums[i]) { 3036 0 => style.* = .{}, 3037 7 => style.inverted = true, 3038 27 => style.inverted = false, 3039 39 => style.fg = null, 3040 49 => style.bg = null, 3041 38, 48 => { 3042 // truecolor form: 38;2;r;g;b — anything else is ignored 3043 if (i + 4 < n and nums[i + 1] == 2) { 3044 const c = Grid.Color{ 3045 .r = @truncate(nums[i + 2]), 3046 .g = @truncate(nums[i + 3]), 3047 .b = @truncate(nums[i + 4]), 3048 }; 3049 if (nums[i] == 38) style.fg = c else style.bg = c; 3050 i += 4; 3051 } 3052 }, 3053 else => {}, 3054 } 3055 } 3056 } 3057 }; 3058 3059 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 3060 pub const AnsiBackground = struct { 3061 grid: ?Grid, 3062 child: *Widget, 3063 art: AnsiArt, 3064 session: *Session, 3065 3066 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, session: *Session) !AnsiBackground { 3067 var cw = child_widget; 3068 const child = allocator.create(Widget) catch |e| { 3069 cw.deinit(allocator); 3070 return e; 3071 }; 3072 child.* = cw; 3073 errdefer { 3074 child.deinit(allocator); 3075 allocator.destroy(child); 3076 } 3077 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, session.data.ansi_art), .session = session }; 3078 } 3079 3080 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 3081 if (self.grid) |*grid| grid.deinit(); 3082 self.art.deinit(allocator); 3083 self.child.deinit(allocator); 3084 allocator.destroy(self.child); 3085 } 3086 3087 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 3088 self.clearGrid(); 3089 3090 // make the wrapped page fill the whole available area 3091 try self.child.build(allocator, .{ 3092 .min_size = .{ 3093 .width = constraint.max_size.width orelse constraint.min_size.width, 3094 .height = constraint.max_size.height orelse constraint.min_size.height, 3095 }, 3096 .max_size = constraint.max_size, 3097 }, root_focus); 3098 3099 if (self.session.data.enable_ansi) { 3100 self.art.content = self.session.data.ansi_art; 3101 if (self.child.getGrid()) |fg| { 3102 self.grid = try artBehind(allocator, fg, &self.art, .top_right, self.session.is_terminal, root_focus); 3103 } 3104 } 3105 } 3106 3107 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 3108 try self.child.input(allocator, key, root_focus); 3109 } 3110 3111 pub fn clearGrid(self: *AnsiBackground) void { 3112 if (self.grid) |*grid| { 3113 grid.deinit(); 3114 self.grid = null; 3115 } 3116 self.child.clearGrid(); 3117 self.art.clearGrid(); 3118 } 3119 3120 pub fn getGrid(self: AnsiBackground) ?Grid { 3121 return self.grid orelse self.child.getGrid(); 3122 } 3123 3124 pub fn getFocus(self: *AnsiBackground) *Focus { 3125 return self.child.getFocus(); 3126 } 3127 3128 // which top corner the art hugs. 3129 const ArtAnchor = enum { top_left, top_right }; 3130 3131 // a foreground cell counts as blank if it's empty or a bare space without styling 3132 fn cellIsBlank(cell: Grid.Cell) bool { 3133 if (cell.rune) |rune| { 3134 return std.mem.eql(u8, rune, " ") and 3135 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 3136 } 3137 return true; 3138 } 3139 3140 // the art is dimmed to this fraction of its brightness so foreground text 3141 // stays legible over it 3142 const art_brightness = 35; // percent 3143 3144 fn dimColor(color: ?Grid.Color) ?Grid.Color { 3145 const c = color orelse return null; 3146 return .{ 3147 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 3148 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 3149 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 3150 }; 3151 } 3152 3153 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 3154 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 3155 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 3156 // snap such channels clear of that range so the backdrop doesn't blink or 3157 // hide text there; the ≤9/255 shift is imperceptible on terminals that 3158 // actually render truecolor. 3159 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 3160 const c = color orelse return null; 3161 const snap = struct { 3162 fn f(v: u8) u8 { 3163 return if (v >= 1 and v <= 9) 10 else v; 3164 } 3165 }.f; 3166 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 3167 } 3168 3169 // composites ANSI art behind a foreground grid 3170 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, is_terminal: bool, root_focus: *Focus) !Grid { 3171 art.clearGrid(); 3172 try art.build(allocator, .{ 3173 .min_size = .{ .width = null, .height = null }, 3174 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 3175 }, root_focus); 3176 3177 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 3178 errdefer out.deinit(); 3179 3180 if (art.getGrid()) |art_grid| { 3181 const anchor_x = switch (anchor) { 3182 .top_left => 0, 3183 .top_right => foreground.size.width -| art_grid.size.width, 3184 }; 3185 for (0..art_grid.size.height) |y| { 3186 for (0..art_grid.size.width) |x| { 3187 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 3188 if (src.rune == null) continue; 3189 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 3190 const dst = &out.cells.items[idx]; 3191 // composite the dimmed art behind every cell so it shows 3192 // through the whole UI: a blank cell becomes the art, while a 3193 // cell with a glyph keeps the glyph but takes the dimmed art as 3194 // its background (replacing whatever background it had, which 3195 // would otherwise obscure the art). 3196 if (cellIsBlank(dst.*)) { 3197 dst.* = src; 3198 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 3199 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 3200 } else { 3201 const background_maybe = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 3202 dst.style.bg = background_maybe; 3203 // change the color of text overlaying ANSI art to ensure 3204 // it is readable. don't do this in the web UI, since we 3205 // have direct control of the theme there (also, inner 3206 // scroll panes allow text to move without a re-render 3207 // by the TUI, which would break this). also don't do this 3208 // if the text is explicitly styled. 3209 if (is_terminal and dst.style.fg == null) { 3210 if (background_maybe) |background| { 3211 // use near-black and near-white, which is easier on the eyes 3212 // while retaining high contrast 3213 const luminance = (@as(u32, background.r) * 299 + 3214 @as(u32, background.g) * 587 + 3215 @as(u32, background.b) * 114) / 1000; 3216 dst.style.fg = if (luminance >= 128) 3217 .{ .r = 16, .g = 16, .b = 16 } 3218 else 3219 .{ .r = 240, .g = 240, .b = 240 }; 3220 } 3221 } 3222 } 3223 } 3224 } 3225 } 3226 return out; 3227 } 3228 }; 3229