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