╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
740 const arena = try allocator.create(std.heap.ArenaAllocator); 741 arena.* = std.heap.ArenaAllocator.init(allocator); 742 errdefer freeArena(allocator, arena); 743 744 session.page_arena = arena; 745 746 const page = try arena.allocator().create(Page); 747 page.* = try Page.init(arena, session, route); 748 const new_root = try initRoot(allocator, page, session); 749 750 try self.history.append(allocator, .{ .root = self.root, .route = self.route, .arena = self.arena }); 751 // drop the oldest entry (freeing its widget tree and arena) once over cap 752 if (self.history.items.len > max_history) { 753 var oldest = self.history.orderedRemove(0); 754 oldest.root.deinit(allocator); 755 freeArena(allocator, oldest.arena); 756 } 757 self.root = new_root; 758 self.route = route; 759 self.arena = arena; 760 } 761 } 762 }; 763 764 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 765 const page_widget: Widget = switch (page.*) { 766 .home => |*p| .{ .home = try .init(allocator, p, session) }, 767 .user => |*p| .{ .user = try .init(allocator, p, session) }, 768 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 769 }; 770 771 const demon_art = @embedFile("embed/demon.ans"); 772 773 // on the TUI/SSH, the page sits above a one-row footer showing the url 774 var root = if (session.is_terminal) blk: { 775 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 776 errdefer box.deinit(allocator); 777 const bg_id = bg_blk: { 778 var background = try AnsiBackground.init(allocator, page_widget, demon_art, session); 779 errdefer background.deinit(allocator); 780 const id = background.getFocus().id; 781 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 782 break :bg_blk id; 783 }; 784 { 785 var footer = try Footer.init(allocator, session); 786 errdefer footer.deinit(allocator); 787 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 788 } 789 box.getFocus().child_id = bg_id; 790 break :blk Widget{ .box = box }; 791 } else Widget{ .background = try AnsiBackground.init(allocator, page_widget, demon_art, session) }; 792 errdefer root.deinit(allocator); 793 794 // input-owning views build their TextInputs in init — so reset the 795 // focus-id -> *TextInput map here 796 session.text_inputs.clearRetainingCapacity(); 797 798 try root.build(allocator, .{ 799 .min_size = .{ .width = null, .height = 40 }, 800 .max_size = .{ .width = 80, .height = null }, 801 }, root.getFocus()); 802 if (root.getFocus().child_id) |child_id| { 803 try root.getFocus().setFocus(child_id); 804 } 805 806 return root; 807 } 808 809 pub const Widget = union(enum) { 810 text: wgt.Text(Widget), 811 box: wgt.Box(Widget), 812 text_box: wgt.TextBox(Widget), 813 text_input: wgt.TextInput(Widget), 814 scroll: wgt.Scroll(Widget), 815 stack: wgt.Stack(Widget), 816 flow_box: FlowBox, 817 flow_box_scroll: FlowBox.Scroll, 818 spacer: Spacer, 819 center: Center, 820 ansi_art: AnsiArt, 821 background: AnsiBackground, 822 home: Home.View, 823 user: User.View, 824 repo: Repo.View, 825 quit: Quit.View, 826 title: Title.View, 827 home_header: Home.Header.View, 828 user_header: User.Header.View, 829 repo_header: Repo.Header.View, 830 repo_files: Repo.Files.View, 831 repo_commits: Repo.Commits.View, 832 home_users: Home.Users.View, 833 home_repos: Home.Repos.View, 834 auth_tab: Home.Header.AuthTab.View, 835 home_settings: Home.Settings.View, 836 home_auth: Home.Auth.View, 837 footer: Footer, 838 839 pub fn deinit(self: *Widget, allocator: std.mem.Allocator) void { 840 switch (self.*) { 841 inline else => |*case| case.deinit(allocator), 842 } 843 } 844 845 pub fn build(self: *Widget, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) anyerror!void { 846 switch (self.*) { 847 inline else => |*case| try case.build(allocator, constraint, root_focus), 848 } 849 } 850 851 pub fn input(self: *Widget, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) anyerror!void { 852 switch (self.*) { 853 inline else => |*case| try case.input(allocator, key, root_focus), 854 } 855 } 856 857 pub fn clearGrid(self: *Widget) void { 858 switch (self.*) { 859 inline else => |*case| case.clearGrid(), 860 } 861 } 862 863 pub fn getGrid(self: Widget) ?Grid { 864 switch (self) { 865 inline else => |*case| return case.getGrid(), 866 } 867 } 868 869 pub fn getFocus(self: *Widget) *Focus { 870 switch (self.*) { 871 inline else => |*case| return case.getFocus(), 872 } 873 } 874 }; 875 876 pub const FlowBox = struct { 877 focus: *Focus, 878 grid: ?Grid, 879 text_boxes: std.ArrayList(wgt.TextBox(Widget)), 880 // backs the per-item strings the text boxes borrow: each box's `content` 881 // and, for link items, its `.custom` focus kind (e.g. "a:/user/foo"). reset 882 // wholesale on each setItems so there's no per-string ownership to track. 883 arena: std.heap.ArenaAllocator, 884 // column count from the last build — FlowBox.Scroll.input uses it so arrow 885 // up/down can step by a row's worth of items. 886 last_cols: usize, 887 options: Options, 888 889 const border_rows: usize = 2; 890 891 pub const Options = struct { 892 cell_width: usize = 40, 893 cell_height: usize = 3, 894 }; 895 896 pub fn init(allocator: std.mem.Allocator, options: Options) !FlowBox { 897 return .{ 898 .focus = try Focus.create(allocator, .container), 899 .grid = null, 900 .text_boxes = .empty, 901 .arena = std.heap.ArenaAllocator.init(allocator), 902 .last_cols = 1, 903 .options = options, 904 }; 905 } 906 907 pub fn deinit(self: *FlowBox, allocator: std.mem.Allocator) void { 908 self.focus.destroy(allocator); 909 if (self.grid) |*grid| { 910 grid.deinit(); 911 self.grid = null; 912 } 913 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 914 self.text_boxes.deinit(allocator); 915 self.arena.deinit(); 916 } 917 918 // `links`, when given, is parallel to `items`: a non-empty entry sets that 919 // item's focus kind to `.{ .custom = link }` (e.g. "a:/user/foo"), which the 920 // web renderer turns into an anchor. pass null when items aren't links. 921 // both the item text and the link are copied into the arena, so the caller's 922 // slices needn't outlive this call. 923 pub fn setItems(self: *FlowBox, allocator: std.mem.Allocator, items: []const []const u8, links: ?[]const []const u8) !void { 924 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 925 self.text_boxes.clearAndFree(allocator); 926 927 // the old text boxes (and their borrowed content/links) are gone now, so 928 // the arena that backed those strings can be reclaimed. 929 _ = self.arena.reset(.retain_capacity); 930 const aa = self.arena.allocator(); 931 932 self.focus.clear(); 933 self.focus.child_id = null; 934 935 for (items, 0..) |item, i| { 936 const line = try aa.dupe(u8, item); 937 938 var text_box = try wgt.TextBox(Widget).init(allocator, line, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 939 errdefer text_box.deinit(allocator); 940 text_box.getFocus().focusable = true; 941 942 if (links) |link_items| { 943 if (i < link_items.len and link_items[i].len > 0) { 944 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, link_items[i]) }; 945 } 946 } 947 948 try self.text_boxes.append(allocator, text_box); 949 } 950 951 if (self.text_boxes.items.len > 0) { 952 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 953 } 954 } 955 956 pub fn build(self: *FlowBox, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 957 self.clearGrid(); 958 self.focus.clear(); 959 960 const cell_width = self.options.cell_width; 961 const max_width = constraint.max_size.width orelse cell_width; 962 const cols = if (cell_width == 0) 1 else @max(1, max_width / cell_width); 963 self.last_cols = cols; 964 965 const count = self.text_boxes.items.len; 966 if (count == 0) return; 967 const slot_height = self.options.cell_height + border_rows; 968 if (slot_height == 0) return; 969 970 // build at the slot size so every text box fits a single grid cell 971 for (self.text_boxes.items) |*tb| { 972 tb.options.border_style = if (self.focus.child_id == tb.getFocus().id) .single else .hidden; 973 try tb.build(allocator, .{ 974 .min_size = .{ .width = cell_width, .height = null }, 975 .max_size = .{ .width = cell_width, .height = slot_height }, 976 }, root_focus); 977 } 978 979 const rows = (count + cols - 1) / cols; 980 const content_height = rows * slot_height; 981 const total_width = cols * cell_width; 982 if (total_width == 0 or content_height == 0) return; 983 984 var grid = try Grid.init(allocator, .{ .width = total_width, .height = content_height }); 985 errdefer grid.deinit(); 986 987 for (self.text_boxes.items, 0..) |*tb, i| { 988 const tb_grid = tb.getGrid() orelse continue; 989 const col = i % cols; 990 const row = i / cols; 991 const cell_x = col * cell_width; 992 const cell_y = row * slot_height; 993 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, cell_x, cell_y); 994 try grid.drawGrid(tb_grid, cell_x, cell_y); 995 } 996 997 self.grid = grid; 998 } 999 1000 pub fn input(self: *FlowBox, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1001 _ = self; 1002 _ = allocator; 1003 _ = key; 1004 _ = root_focus; 1005 } 1006 1007 pub fn clearGrid(self: *FlowBox) void { 1008 if (self.grid) |*grid| { 1009 grid.deinit(); 1010 self.grid = null; 1011 } 1012 for (self.text_boxes.items) |*tb| tb.clearGrid(); 1013 } 1014 1015 pub fn getGrid(self: FlowBox) ?Grid { 1016 return self.grid; 1017 } 1018 1019 pub fn getFocus(self: *FlowBox) *Focus { 1020 return self.focus; 1021 } 1022 1023 pub fn cellRect(self: FlowBox, index: usize) ?layout.IRect { 1024 if (self.last_cols == 0 or index >= self.text_boxes.items.len) return null; 1025 const slot_height = self.options.cell_height + border_rows; 1026 const col = index % self.last_cols; 1027 const row = index / self.last_cols; 1028 return .{ 1029 .x = @intCast(col * self.options.cell_width), 1030 .y = @intCast(row * slot_height), 1031 .size = .{ .width = self.options.cell_width, .height = slot_height }, 1032 }; 1033 } 1034 1035 pub fn indexOfFocusId(self: FlowBox, focus_id: usize) ?usize { 1036 for (self.text_boxes.items, 0..) |tb, i| { 1037 if (tb.box.focus.id == focus_id) return i; 1038 } 1039 return null; 1040 } 1041 1042 pub const Scroll = struct { 1043 scroll: wgt.Scroll(Widget), 1044 1045 pub fn init(allocator: std.mem.Allocator, options: FlowBox.Options) !Scroll { 1046 var layout_inner = try FlowBox.init(allocator, options); 1047 errdefer layout_inner.deinit(allocator); 1048 var scroll = try wgt.Scroll(Widget).init(allocator, .{ .flow_box = layout_inner }, .{ .direction = .vert }); 1049 errdefer scroll.deinit(allocator); 1050 return .{ .scroll = scroll }; 1051 } 1052 1053 pub fn deinit(self: *Scroll, allocator: std.mem.Allocator) void { 1054 self.scroll.deinit(allocator); 1055 } 1056 1057 pub fn setItems(self: *Scroll, allocator: std.mem.Allocator, items: []const []const u8, links: ?[]const []const u8) !void { 1058 self.scroll.x = 0; 1059 self.scroll.y = 0; 1060 try self.inner().setItems(allocator, items, links); 1061 } 1062 1063 pub fn build(self: *Scroll, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1064 try self.scroll.build(allocator, constraint, root_focus); 1065 } 1066 1067 pub fn input(self: *Scroll, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1068 _ = allocator; 1069 const in = self.inner(); 1070 const child_id = in.focus.child_id orelse return; 1071 const current_index = in.indexOfFocusId(child_id) orelse return; 1072 const count = in.text_boxes.items.len; 1073 if (count == 0) return; 1074 const cols = in.last_cols; 1075 const slot_height = in.options.cell_height + FlowBox.border_rows; 1076 1077 var index = current_index; 1078 switch (key) { 1079 .arrow_up => index -|= cols, 1080 .arrow_down => if (index + cols < count) { 1081 index += cols; 1082 }, 1083 .arrow_left => index -|= 1, 1084 .arrow_right => if (index + 1 < count) { 1085 index += 1; 1086 }, 1087 .home => index = 0, 1088 .end => index = count - 1, 1089 .page_up => { 1090 if (self.scroll.grid) |grid| if (slot_height > 0) { 1091 const rows_per_page = grid.size.height / slot_height; 1092 index -|= rows_per_page * cols; 1093 }; 1094 }, 1095 .page_down => { 1096 if (self.scroll.grid) |grid| if (slot_height > 0) { 1097 const rows_per_page = grid.size.height / slot_height; 1098 index = @min(index + rows_per_page * cols, count - 1); 1099 }; 1100 }, 1101 // scroll wheel moves the focused cell by a full row so the 1102 // viewport (via scrollToRect) follows in row-sized steps, 1103 // matching how a scroll wheel feels in a grid view. 1104 .mouse => |mouse| switch (mouse.action) { 1105 .scroll => |dir| switch (dir) { 1106 .up => index -|= cols, 1107 .down => if (index + cols < count) { 1108 index += cols; 1109 }, 1110 }, 1111 else => {}, 1112 }, 1113 else => {}, 1114 } 1115 1116 if (index != current_index) { 1117 try root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 1118 if (in.cellRect(index)) |rect| { 1119 self.scroll.scrollToRect(rect); 1120 } 1121 } 1122 } 1123 1124 pub fn clearGrid(self: *Scroll) void { 1125 self.scroll.clearGrid(); 1126 } 1127 1128 pub fn getGrid(self: Scroll) ?Grid { 1129 return self.scroll.getGrid(); 1130 } 1131 1132 pub fn getFocus(self: *Scroll) *Focus { 1133 return self.scroll.getFocus(); 1134 } 1135 1136 pub fn getSelectedIndex(self: Scroll) ?usize { 1137 const in = self.scroll.child.flow_box; 1138 const child_id = in.focus.child_id orelse return null; 1139 return in.indexOfFocusId(child_id); 1140 } 1141 1142 fn inner(self: *Scroll) *FlowBox { 1143 return &self.scroll.child.flow_box; 1144 } 1145 }; 1146 }; 1147 1148 // an invisible widget that fills the horizontal space granted by its parent. 1149 // used inside Box(horiz) with a min_size so the box reserves space for the 1150 // children that follow, pushing them to the right. 1151 pub const Spacer = struct { 1152 focus: *Focus, 1153 grid: ?Grid, 1154 1155 pub fn init(allocator: std.mem.Allocator) !Spacer { 1156 return .{ 1157 .focus = try Focus.create(allocator, .container), 1158 .grid = null, 1159 }; 1160 } 1161 1162 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 1163 self.focus.destroy(allocator); 1164 if (self.grid) |*grid| { 1165 grid.deinit(); 1166 self.grid = null; 1167 } 1168 } 1169 1170 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1171 _ = root_focus; 1172 self.clearGrid(); 1173 const width = constraint.max_size.width orelse return; 1174 if (width == 0) return; 1175 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1176 } 1177 1178 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1179 _ = self; 1180 _ = allocator; 1181 _ = key; 1182 _ = root_focus; 1183 } 1184 1185 pub fn clearGrid(self: *Spacer) void { 1186 if (self.grid) |*grid| { 1187 grid.deinit(); 1188 self.grid = null; 1189 } 1190 } 1191 1192 pub fn getGrid(self: Spacer) ?Grid { 1193 return self.grid; 1194 } 1195 1196 pub fn getFocus(self: *Spacer) *Focus { 1197 return self.focus; 1198 } 1199 }; 1200 1201 // a one-row, non-focusable status bar showing the current page's url 1202 pub const Footer = struct { 1203 focus: *Focus, 1204 grid: ?Grid, 1205 session: *Session, 1206 arena: std.heap.ArenaAllocator, 1207 1208 pub fn init(allocator: std.mem.Allocator, session: *Session) !Footer { 1209 return .{ 1210 .focus = try Focus.create(allocator, .container), 1211 .grid = null, 1212 .session = session, 1213 .arena = std.heap.ArenaAllocator.init(allocator), 1214 }; 1215 } 1216 1217 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 1218 self.focus.destroy(allocator); 1219 if (self.grid) |*grid| grid.deinit(); 1220 self.arena.deinit(); 1221 } 1222 1223 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1224 _ = root_focus; 1225 self.clearGrid(); 1226 const width = constraint.max_size.width orelse return; 1227 if (width == 0) return; 1228 1229 _ = self.arena.reset(.retain_capacity); 1230 const aa = self.arena.allocator(); 1231 const path = self.session.data.current_page.urlAlloc(&self.arena) catch return; 1232 const text = if (self.session.web_port) |port| 1233 std.fmt.allocPrint(aa, "http://localhost:{d}{s}", .{ port, path }) catch return 1234 else 1235 path; 1236 1237 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1238 errdefer grid.deinit(); 1239 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 1240 var i: usize = 0; 1241 while (utf8.nextCodepointSlice()) |ch| { 1242 if (i == width) break; 1243 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 1244 i += 1; 1245 } 1246 self.grid = grid; 1247 } 1248 1249 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1250 _ = self; 1251 _ = allocator; 1252 _ = key; 1253 _ = root_focus; 1254 } 1255 1256 pub fn clearGrid(self: *Footer) void { 1257 if (self.grid) |*grid| { 1258 grid.deinit(); 1259 self.grid = null; 1260 } 1261 } 1262 1263 pub fn getGrid(self: Footer) ?Grid { 1264 return self.grid; 1265 } 1266 1267 pub fn getFocus(self: *Footer) *Focus { 1268 return self.focus; 1269 } 1270 }; 1271 1272 // a single-child wrapper that builds the child at its natural size and 1273 // positions its grid in the middle of the area granted by the parent 1274 pub const Center = struct { 1275 focus: *Focus, 1276 grid: ?Grid, 1277 child: *Widget, 1278 direction: Direction, 1279 1280 pub const Direction = enum { both, horiz, vert }; 1281 1282 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, direction: Direction) !Center { 1283 const child = try allocator.create(Widget); 1284 errdefer allocator.destroy(child); 1285 child.* = child_widget; 1286 return .{ 1287 .focus = try Focus.create(allocator, .container), 1288 .grid = null, 1289 .child = child, 1290 .direction = direction, 1291 }; 1292 } 1293 1294 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1295 self.focus.destroy(allocator); 1296 if (self.grid) |*grid| { 1297 grid.deinit(); 1298 self.grid = null; 1299 } 1300 self.child.deinit(allocator); 1301 allocator.destroy(self.child); 1302 } 1303 1304 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1305 self.clearGrid(); 1306 self.getFocus().clear(); 1307 1308 // build the child without forcing it to fill min_size; it sizes 1309 // itself to its content within the available max. 1310 try self.child.build(allocator, .{ 1311 .min_size = .{ .width = null, .height = null }, 1312 .max_size = constraint.max_size, 1313 }, root_focus); 1314 1315 const child_grid = self.child.getGrid() orelse return; 1316 1317 // prefer max when given; otherwise grow to max(min, child) so that 1318 // when only a min is set (e.g. the wasm path passes viewport rows 1319 // as min_height with no max), we can still vertically center while 1320 // letting taller content extend past the min. 1321 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; 1322 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; 1323 if (width == 0 or height == 0) return; 1324 1325 const offset_x: usize = if (self.direction == .horiz or self.direction == .both) 1326 (width -| child_grid.size.width) / 2 1327 else 1328 0; 1329 const offset_y: usize = if (self.direction == .vert or self.direction == .both) 1330 (height -| child_grid.size.height) / 2 1331 else 1332 0; 1333 1334 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1335 errdefer grid.deinit(); 1336 try grid.drawGrid(child_grid, offset_x, offset_y); 1337 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1338 self.getFocus().child_id = self.child.getFocus().id; 1339 1340 self.grid = grid; 1341 } 1342 1343 pub fn input(self: *Center, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1344 try self.child.input(allocator, key, root_focus); 1345 } 1346 1347 pub fn clearGrid(self: *Center) void { 1348 if (self.grid) |*grid| { 1349 grid.deinit(); 1350 self.grid = null; 1351 } 1352 self.child.clearGrid(); 1353 } 1354 1355 pub fn getGrid(self: Center) ?Grid { 1356 return self.grid; 1357 } 1358 1359 pub fn getFocus(self: *Center) *Focus { 1360 return self.focus; 1361 } 1362 }; 1363 1364 // renders truecolor ANSI art 1365 pub const AnsiArt = struct { 1366 focus: *Focus, 1367 grid: ?Grid, 1368 content: []const u8, 1369 1370 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1371 return .{ 1372 .focus = try Focus.create(allocator, .container), 1373 .grid = null, 1374 .content = content, 1375 }; 1376 } 1377 1378 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1379 self.focus.destroy(allocator); 1380 if (self.grid) |*grid| { 1381 grid.deinit(); 1382 self.grid = null; 1383 } 1384 } 1385 1386 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1387 _ = root_focus; 1388 self.clearGrid(); 1389 1390 // parse into rows of cells, then copy into a rectangular grid 1391 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1392 defer { 1393 for (rows.items) |*row| row.deinit(allocator); 1394 rows.deinit(allocator); 1395 } 1396 var row: std.ArrayList(Grid.Cell) = .empty; 1397 errdefer row.deinit(allocator); 1398 1399 var style: Grid.Style = .{}; 1400 var width: usize = 0; 1401 const content = self.content; 1402 var i: usize = 0; 1403 while (i < content.len) { 1404 const byte = content[i]; 1405 if (byte == '\n') { 1406 width = @max(width, row.items.len); 1407 try rows.append(allocator, row); 1408 row = .empty; 1409 i += 1; 1410 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1411 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1412 var j = i + 2; 1413 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1414 if (j >= content.len) { 1415 i = content.len; // malformed trailing escape; stop 1416 } else { 1417 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1418 i = j + 1; 1419 } 1420 } else { 1421 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1422 const end = @min(content.len, i + len); 1423 const rune = content[i..end]; 1424 const transparent = end - i == 1 and rune[0] == ' ' and 1425 style.fg == null and style.bg == null and !style.inverted; 1426 try row.append(allocator, .{ 1427 .rune = if (transparent) null else rune, 1428 .style = style, 1429 }); 1430 i = end; 1431 } 1432 } 1433 // a trailing row with content but no closing newline 1434 if (row.items.len > 0) { 1435 width = @max(width, row.items.len); 1436 try rows.append(allocator, row); 1437 } else { 1438 row.deinit(allocator); 1439 } 1440 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1441 1442 const height = rows.items.len; 1443 if (width == 0 or height == 0) return; 1444 1445 const clamped_w = @min(width, constraint.max_size.width orelse width); 1446 const clamped_h = @min(height, constraint.max_size.height orelse height); 1447 1448 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1449 errdefer grid.deinit(); 1450 for (rows.items[0..clamped_h], 0..) |r, y| { 1451 const n = @min(r.items.len, clamped_w); 1452 for (r.items[0..n], 0..) |cell, x| { 1453 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 1454 } 1455 } 1456 self.grid = grid; 1457 } 1458 1459 pub fn input(self: *AnsiArt, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1460 _ = self; 1461 _ = allocator; 1462 _ = key; 1463 _ = root_focus; 1464 } 1465 1466 pub fn clearGrid(self: *AnsiArt) void { 1467 if (self.grid) |*grid| { 1468 grid.deinit(); 1469 self.grid = null; 1470 } 1471 } 1472 1473 pub fn getGrid(self: AnsiArt) ?Grid { 1474 return self.grid; 1475 } 1476 1477 pub fn getFocus(self: *AnsiArt) *Focus { 1478 return self.focus; 1479 } 1480 1481 fn applySgr(style: *Grid.Style, params: []const u8) void { 1482 var nums: [16]u32 = undefined; 1483 var n: usize = 0; 1484 var it = std.mem.splitScalar(u8, params, ';'); 1485 while (it.next()) |tok| { 1486 if (n >= nums.len) break; 1487 // an empty parameter (e.g. bare "\x1b[m") means 0 1488 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1489 n += 1; 1490 } 1491 if (n == 0) { 1492 style.* = .{}; 1493 return; 1494 } 1495 var i: usize = 0; 1496 while (i < n) : (i += 1) { 1497 switch (nums[i]) { 1498 0 => style.* = .{}, 1499 7 => style.inverted = true, 1500 27 => style.inverted = false, 1501 39 => style.fg = null, 1502 49 => style.bg = null, 1503 38, 48 => { 1504 // truecolor form: 38;2;r;g;b — anything else is ignored 1505 if (i + 4 < n and nums[i + 1] == 2) { 1506 const c = Grid.Color{ 1507 .r = @truncate(nums[i + 2]), 1508 .g = @truncate(nums[i + 3]), 1509 .b = @truncate(nums[i + 4]), 1510 }; 1511 if (nums[i] == 38) style.fg = c else style.bg = c; 1512 i += 4; 1513 } 1514 }, 1515 else => {}, 1516 } 1517 } 1518 } 1519 }; 1520 1521 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1522 pub const AnsiBackground = struct { 1523 grid: ?Grid, 1524 child: *Widget, 1525 art: AnsiArt, 1526 session: *Session, 1527 1528 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 1529 var cw = child_widget; 1530 const child = allocator.create(Widget) catch |e| { 1531 cw.deinit(allocator); 1532 return e; 1533 }; 1534 child.* = cw; 1535 errdefer { 1536 child.deinit(allocator); 1537 allocator.destroy(child); 1538 } 1539 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 1540 } 1541 1542 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1543 if (self.grid) |*grid| grid.deinit(); 1544 self.art.deinit(allocator); 1545 self.child.deinit(allocator); 1546 allocator.destroy(self.child); 1547 } 1548 1549 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1550 self.clearGrid(); 1551 1552 // make the wrapped page fill the whole available area 1553 try self.child.build(allocator, .{ 1554 .min_size = .{ 1555 .width = constraint.max_size.width orelse constraint.min_size.width, 1556 .height = constraint.max_size.height orelse constraint.min_size.height, 1557 }, 1558 .max_size = constraint.max_size, 1559 }, root_focus); 1560 1561 if (self.session.data.enable_ansi) { 1562 if (self.child.getGrid()) |fg| { 1563 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 1564 } 1565 } 1566 } 1567 1568 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1569 try self.child.input(allocator, key, root_focus); 1570 } 1571 1572 pub fn clearGrid(self: *AnsiBackground) void { 1573 if (self.grid) |*grid| { 1574 grid.deinit(); 1575 self.grid = null; 1576 } 1577 self.child.clearGrid(); 1578 self.art.clearGrid(); 1579 } 1580 1581 pub fn getGrid(self: AnsiBackground) ?Grid { 1582 return self.grid orelse self.child.getGrid(); 1583 } 1584 1585 pub fn getFocus(self: *AnsiBackground) *Focus { 1586 return self.child.getFocus(); 1587 } 1588 1589 // which top corner the art hugs. 1590 const ArtAnchor = enum { top_left, top_right }; 1591 1592 // a foreground cell counts as blank if it's empty or a bare space without styling 1593 fn cellIsBlank(cell: Grid.Cell) bool { 1594 if (cell.rune) |rune| { 1595 return std.mem.eql(u8, rune, " ") and 1596 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1597 } 1598 return true; 1599 } 1600 1601 // a single representative color for an art cell 1602 fn artCellColor(style: Grid.Style) ?Grid.Color { 1603 const f = style.fg orelse return style.bg; 1604 const b = style.bg orelse return f; 1605 return .{ 1606 .r = @intCast((@as(u16, f.r) + b.r) / 2), 1607 .g = @intCast((@as(u16, f.g) + b.g) / 2), 1608 .b = @intCast((@as(u16, f.b) + b.b) / 2), 1609 }; 1610 } 1611 1612 // behind a visible glyph the art color is dimmed to this fraction of its 1613 // brightness so light terminal text stays legible on top of it 1614 const glyph_bg_brightness = 45; // percent 1615 1616 // a glyph sitting over the art also gets its foreground pinned to this color 1617 // (unless it set its own), so the text stays legible 1618 const glyph_fg_over_art = Grid.Color{ .r = 235, .g = 235, .b = 235 }; 1619 1620 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1621 const c = color orelse return null; 1622 return .{ 1623 .r = @intCast(@as(u16, c.r) * glyph_bg_brightness / 100), 1624 .g = @intCast(@as(u16, c.g) * glyph_bg_brightness / 100), 1625 .b = @intCast(@as(u16, c.b) * glyph_bg_brightness / 100), 1626 }; 1627 } 1628 1629 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1630 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1631 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1632 // snap such channels clear of that range so the backdrop doesn't blink or 1633 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1634 // actually render truecolor. 1635 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1636 const c = color orelse return null; 1637 const snap = struct { 1638 fn f(v: u8) u8 { 1639 return if (v >= 1 and v <= 9) 10 else v; 1640 } 1641 }.f; 1642 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1643 } 1644 1645 // composites ANSI art behind a foreground grid 1646 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 1647 art.clearGrid(); 1648 try art.build(allocator, .{ 1649 .min_size = .{ .width = null, .height = null }, 1650 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1651 }, root_focus); 1652 1653 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1654 errdefer out.deinit(); 1655 1656 if (art.getGrid()) |art_grid| { 1657 const anchor_x = switch (anchor) { 1658 .top_left => 0, 1659 .top_right => foreground.size.width -| art_grid.size.width, 1660 }; 1661 for (0..art_grid.size.height) |y| { 1662 for (0..art_grid.size.width) |x| { 1663 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 1664 if (src.rune == null) continue; 1665 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 1666 const dst = &out.cells.items[idx]; 1667 if (cellIsBlank(dst.*)) { 1668 dst.* = src; 1669 dst.style.fg = sgrSafe(dst.style.fg); 1670 dst.style.bg = sgrSafe(dst.style.bg); 1671 } else if (dst.style.bg == null) { 1672 dst.style.bg = sgrSafe(dimColor(artCellColor(src.style))); 1673 if (dst.style.fg == null) dst.style.fg = glyph_fg_over_art; 1674 } 1675 } 1676 } 1677 } 1678 return out; 1679 } 1680 }; 1681