╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
818 // after that, all remaining reads are non-blocking so 819 // we can process the rest of the queued inputs. 820 // 821 // if the grid *did* change, then only do non-blocking 822 // reads. we do not want to sleep the thread because 823 // there may be an animation that requires more looping. 824 var blocking = !grid_changed; 825 while (try terminal.readKey(io, blocking)) |key| { 826 blocking = false; 827 try inputKey(allocator, &nav.root, key, session); 828 } 829 830 // local run has no repo to persist to, so just apply in-memory 831 session.applyPending(); 832 833 // pick up data written by other handles so the next navigation 834 // builds its page from a current moment (local mode has no admin repo) 835 if (repo_maybe) |repo| try session.reloadMoment(repo); 836 837 // reconcile navigation: forward to a new page, or back on escape. 838 try nav.sync(allocator, session); 839 840 // the quit button (on the quit tab) asks the host to tear down. 841 if (session.quit_requested) terminal.requestQuit(); 842 843 try nav.root.build(allocator, .{ 844 .min_size = .{ .width = null, .height = null }, 845 .max_size = .{ .width = last_size.width, .height = last_size.height }, 846 }, nav.root.getFocus()); 847 } 848 } 849 850 pub fn inputKey(allocator: std.mem.Allocator, root: *Widget, key: Key, session: *Session) !void { 851 switch (key) { 852 // request a navigation pop; the host's Nav.sync goes back a page, or 853 // quits when there's no history left. 854 .escape => session.nav_back = true, 855 .ctrl => |letter| switch (letter) { 856 'r' => session.refresh_requested = true, 857 else => try root.input(allocator, key, root.getFocus()), 858 }, 859 .enter => { 860 const root_focus = root.getFocus(); 861 if (root_focus.grandchild_id) |gid| { 862 // follow a cross-page link 863 if (crossPageLink(root_focus, gid, session.data.current_page)) |route| { 864 return session.navigate(route); 865 } 866 } 867 try root.input(allocator, key, root_focus); 868 }, 869 .mouse => |mouse| { 870 if (mouse.action == .press and mouse.action.press == .left) { 871 const root_focus = root.getFocus(); 872 var clicked: ?usize = null; 873 var iter = root_focus.children.iterator(); 874 while (iter.next()) |entry| { 875 const child = entry.value_ptr.*; 876 if (!child.focus.focusable) continue; 877 const r = child.rect; 878 if (mouse.x >= r.x and mouse.y >= r.y and 879 mouse.x < r.x + r.size.width and mouse.y < r.y + r.size.height) 880 { 881 clicked = entry.key_ptr.*; 882 break; 883 } 884 } 885 if (clicked) |focus_id| { 886 // follow a cross-page link 887 if (crossPageLink(root_focus, focus_id, session.data.current_page)) |route| { 888 return session.navigate(route); 889 } 890 root_focus.setFocus(focus_id); 891 } 892 // forward the press into the widget tree so buttons (and any 893 // future click-aware widgets) can react. widgets that don't 894 // care about presses ignore it. 895 try root.input(allocator, key, root.getFocus()); 896 } else { 897 try root.input(allocator, key, root.getFocus()); 898 } 899 }, 900 else => try root.input(allocator, key, root.getFocus()), 901 } 902 } 903 904 // if the focus target at focus_id is an `a:` link that should navigate (a 905 // different parent page, or a different files directory within the repo page), 906 // return its route; otherwise null. lets a host turn a click / enter on such a 907 // link into a navigation rather than a focus change. 908 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, current: RoutablePage) ?RoutablePage { 909 const child = root_focus.children.get(focus_id) orelse return null; 910 const custom = switch (child.focus.kind) { 911 .custom => |c| c, 912 else => return null, 913 }; 914 const a_prefix = "a:"; 915 if (!std.mem.startsWith(u8, custom, a_prefix)) return null; 916 const url = custom[a_prefix.len..]; 917 const q = std.mem.indexOfScalar(u8, url, '?'); 918 const route = RoutablePage.fromUrl(if (q) |i| url[0..i] else url, if (q) |i| url[i + 1 ..] else null) orelse return null; 919 // a link to a different parent page always navigates; within a page, a 920 // files-directory / commits-page / list-window change navigates while tab 921 // links stay in-page (header-handled). 922 if (route.parent() != current.parent() or RoutablePage.repoPageChanged(route, current) or RoutablePage.homePageChanged(route, current) or RoutablePage.userPageChanged(route, current)) return route; 923 return null; 924 } 925 926 // native-TUI navigation 927 pub const Nav = struct { 928 root: Widget, 929 route: RoutablePage, 930 // backs the current page (its Page data and the page-scoped strings its 931 // widgets build). owned here, swapped on each navigation. session.page_arena 932 // tracks whichever of these is current so widgets allocate into it. 933 arena: *std.heap.ArenaAllocator, 934 history: std.ArrayList(Entry), 935 936 // each retained page keeps its own arena, freed when the page leaves the 937 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 938 // long-lived session from accumulating every page it ever visited. 939 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator }; 940 941 // cap on retained back-history so the chain can't grow memory without bound 942 const max_history: usize = 16; 943 944 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 945 const arena = try allocator.create(std.heap.ArenaAllocator); 946 arena.* = std.heap.ArenaAllocator.init(allocator); 947 errdefer { 948 arena.deinit(); 949 allocator.destroy(arena); 950 } 951 952 session.page_arena = arena; 953 const route = session.data.current_page; 954 955 const page = try arena.allocator().create(Page); 956 page.* = try Page.init(arena, session, route); 957 return .{ 958 .root = try initRoot(allocator, page, session), 959 .route = route, 960 .arena = arena, 961 .history = .empty, 962 }; 963 } 964 965 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 966 self.root.deinit(allocator); 967 freeArena(allocator, self.arena); 968 for (self.history.items) |*entry| { 969 entry.root.deinit(allocator); 970 freeArena(allocator, entry.arena); 971 } 972 self.history.deinit(allocator); 973 } 974 975 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 976 arena.deinit(); 977 allocator.destroy(arena); 978 } 979 980 // reconcile the displayed root with the session's navigation state. a 981 // forward nav (current_page moved to a different parent page) pushes the 982 // current root and builds the new page in a fresh arena; a back request 983 // frees the current page and restores the previous one. when escape is 984 // pressed with no history left we switch to the quit tab instead of quitting. 985 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 986 // refresh: rebuild the current page in place from a current moment 987 if (session.refresh_requested) { 988 session.refresh_requested = false; 989 if (session.haxy_moment != null or session.local != null) { 990 const arena = try allocator.create(std.heap.ArenaAllocator); 991 arena.* = std.heap.ArenaAllocator.init(allocator); 992 errdefer freeArena(allocator, arena); 993 994 session.page_arena = arena; 995 996 const page = try arena.allocator().create(Page); 997 page.* = try Page.init(arena, session, self.route); 998 const new_root = try initRoot(allocator, page, session); 999 1000 self.root.deinit(allocator); 1001 freeArena(allocator, self.arena); 1002 self.root = new_root; 1003 self.arena = arena; 1004 } 1005 return; 1006 } 1007 1008 if (session.nav_back) { 1009 session.nav_back = false; 1010 if (self.history.pop()) |entry| { 1011 self.root.deinit(allocator); 1012 freeArena(allocator, self.arena); 1013 self.root = entry.root; 1014 self.route = entry.route; 1015 self.arena = entry.arena; 1016 session.page_arena = entry.arena; 1017 session.data.current_page = entry.route; 1018 return; 1019 } 1020 // nothing to go back to; switch to the quit confirmation 1021 const root_focus = self.root.getFocus(); 1022 var iter = root_focus.children.iterator(); 1023 while (iter.next()) |entry| { 1024 switch (entry.value_ptr.focus.kind) { 1025 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 1026 root_focus.setFocus(entry.key_ptr.*); 1027 // a Stack only builds its selected child, so the quit 1028 // button isn't in the focus tree until a build runs with 1029 // the quit tab selected. build once, then send a 1030 // synthetic arrow_down to drop focus from the tab onto 1031 // the button so it's ready to confirm. 1032 try self.root.build(allocator, .{ 1033 .min_size = .{ .width = null, .height = 40 }, 1034 .max_size = .{ .width = 80, .height = null }, 1035 }, root_focus); 1036 try self.root.input(allocator, .arrow_down, root_focus); 1037 break; 1038 }, 1039 else => {}, 1040 } 1041 } 1042 return; 1043 } 1044 1045 // forward navigation: navigate() set next_page to the page to move to (a 1046 // cross-page link or tab change crossing pages) 1047 if (session.next_page) |route| { 1048 session.next_page = null; 1049 if (session.haxy_moment == null and session.local == null) return; 1050 // the page we navigated to becomes the current page 1051 session.data.current_page = route; 1052 1053 const arena = try allocator.create(std.heap.ArenaAllocator); 1054 arena.* = std.heap.ArenaAllocator.init(allocator); 1055 errdefer freeArena(allocator, arena); 1056 1057 session.page_arena = arena; 1058 1059 const page = try arena.allocator().create(Page); 1060 page.* = try Page.init(arena, session, route); 1061 const new_root = try initRoot(allocator, page, session); 1062 1063 try self.history.append(allocator, .{ .root = self.root, .route = self.route, .arena = self.arena }); 1064 // drop the oldest entry (freeing its widget tree and arena) once over cap 1065 if (self.history.items.len > max_history) { 1066 var oldest = self.history.orderedRemove(0); 1067 oldest.root.deinit(allocator); 1068 freeArena(allocator, oldest.arena); 1069 } 1070 self.root = new_root; 1071 self.route = route; 1072 self.arena = arena; 1073 } 1074 } 1075 }; 1076 1077 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 1078 const page_widget: Widget = switch (page.*) { 1079 .home => |*p| .{ .home = try .init(allocator, p, session) }, 1080 .user => |*p| .{ .user = try .init(allocator, p, session) }, 1081 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 1082 }; 1083 1084 const demon_art = @embedFile("embed/demon.ans"); 1085 1086 // on the TUI/SSH, the page sits above a one-row footer showing the url 1087 var root = if (session.is_terminal) blk: { 1088 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1089 errdefer box.deinit(allocator); 1090 const bg_id = bg_blk: { 1091 var background = try AnsiBackground.init(allocator, page_widget, demon_art, session); 1092 errdefer background.deinit(allocator); 1093 const id = background.getFocus().id; 1094 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 1095 break :bg_blk id; 1096 }; 1097 { 1098 var footer = try Footer.init(allocator, session); 1099 errdefer footer.deinit(allocator); 1100 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 1101 } 1102 box.getFocus().child_id = bg_id; 1103 break :blk Widget{ .box = box }; 1104 } else Widget{ .background = try AnsiBackground.init(allocator, page_widget, demon_art, session) }; 1105 errdefer root.deinit(allocator); 1106 1107 // input-owning views build their TextInputs in init — so reset the 1108 // focus-id -> *TextInput map here 1109 session.text_inputs.clearRetainingCapacity(); 1110 1111 try root.build(allocator, .{ 1112 .min_size = .{ .width = null, .height = 40 }, 1113 .max_size = .{ .width = 100, .height = null }, 1114 }, root.getFocus()); 1115 1116 return root; 1117 } 1118 1119 pub const Widget = union(enum) { 1120 text: wgt.Text(Widget), 1121 box: wgt.Box(Widget), 1122 text_box: wgt.TextBox(Widget), 1123 text_input: wgt.TextInput(Widget), 1124 scroll: wgt.Scroll(Widget), 1125 stack: wgt.Stack(Widget), 1126 flow_box: FlowBox, 1127 flow_box_scroll: FlowBox.Scroll, 1128 tag_flow: TagFlow, 1129 spacer: Spacer, 1130 center: Center, 1131 background: AnsiBackground, 1132 home: Home.View, 1133 user: User.View, 1134 repo: Repo.View, 1135 quit: Quit.View, 1136 title: Title.View, 1137 sub_title: SubTitle.View, 1138 home_header: Home.Header.View, 1139 user_header: User.Header.View, 1140 repo_header: Repo.Header.View, 1141 repo_sub_header: Repo.SubHeader.View, 1142 repo_files: Repo.Files.View, 1143 repo_commits: Repo.Commits.View, 1144 repo_refs: Repo.Refs.View, 1145 repo_issues: Repo.Issues.View, 1146 home_users: Home.Users.View, 1147 home_repos: Home.Repos.View, 1148 auth_tab: Home.Header.AuthTab.View, 1149 home_settings: Home.Settings.View, 1150 home_auth: Home.Auth.View, 1151 footer: Footer, 1152 1153 pub fn deinit(self: *Widget, allocator: std.mem.Allocator) void { 1154 switch (self.*) { 1155 inline else => |*case| case.deinit(allocator), 1156 } 1157 } 1158 1159 pub fn build(self: *Widget, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) anyerror!void { 1160 switch (self.*) { 1161 inline else => |*case| try case.build(allocator, constraint, root_focus), 1162 } 1163 } 1164 1165 pub fn input(self: *Widget, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) anyerror!void { 1166 switch (self.*) { 1167 inline else => |*case| try case.input(allocator, key, root_focus), 1168 } 1169 } 1170 1171 pub fn clearGrid(self: *Widget) void { 1172 switch (self.*) { 1173 inline else => |*case| case.clearGrid(), 1174 } 1175 } 1176 1177 pub fn getGrid(self: Widget) ?Grid { 1178 switch (self) { 1179 inline else => |*case| return case.getGrid(), 1180 } 1181 } 1182 1183 pub fn getFocus(self: *Widget) *Focus { 1184 switch (self.*) { 1185 inline else => |*case| return case.getFocus(), 1186 } 1187 } 1188 }; 1189 1190 // move the focused row in `box` by `delta`, clamped to the ends, scrolling 1191 // `scroll` to keep it visible. 1192 pub fn moveRowFocus(box: *wgt.Box(Widget), scroll: *wgt.Scroll(Widget), root_focus: *Focus, delta: isize) void { 1193 const keys = box.children.keys(); 1194 if (keys.len == 0) return; 1195 const cur_id = box.getFocus().child_id orelse return; 1196 const cur: isize = @intCast(box.children.getIndex(cur_id) orelse return); 1197 const last: isize = @intCast(keys.len - 1); 1198 const next: usize = @intCast(std.math.clamp(cur + delta, 0, last)); 1199 if (next == @as(usize, @intCast(cur))) return; 1200 root_focus.setFocus(keys[next]); 1201 if (box.children.values()[next].rect) |rect| scroll.scrollToRect(rect); 1202 } 1203 1204 pub const FlowBox = struct { 1205 focus: *Focus, 1206 grid: ?Grid, 1207 text_boxes: std.ArrayList(wgt.TextBox(Widget)), 1208 // backs the per-item strings the text boxes borrow: each box's `content` 1209 // and, for link items, its `.custom` focus kind (e.g. "a:/user/foo"). reset 1210 // wholesale on each setItems so there's no per-string ownership to track. 1211 arena: std.heap.ArenaAllocator, 1212 // column count from the last build — FlowBox.Scroll.input uses it so arrow 1213 // up/down can step by a row's worth of items. 1214 last_cols: usize, 1215 options: Options, 1216 1217 const border_rows: usize = 2; 1218 1219 pub const Options = struct { 1220 cell_width: usize = 40, 1221 cell_height: usize = 3, 1222 }; 1223 1224 pub fn init(allocator: std.mem.Allocator, options: Options) !FlowBox { 1225 return .{ 1226 .focus = try Focus.create(allocator, .container), 1227 .grid = null, 1228 .text_boxes = .empty, 1229 .arena = std.heap.ArenaAllocator.init(allocator), 1230 .last_cols = 1, 1231 .options = options, 1232 }; 1233 } 1234 1235 pub fn deinit(self: *FlowBox, allocator: std.mem.Allocator) void { 1236 self.focus.destroy(allocator); 1237 if (self.grid) |*grid| { 1238 grid.deinit(); 1239 self.grid = null; 1240 } 1241 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1242 self.text_boxes.deinit(allocator); 1243 self.arena.deinit(); 1244 } 1245 1246 // one flow item: its display text plus an optional link. a non-empty `link` 1247 // sets the item's focus kind such as `.{ .custom = "a:/user/foo" }`, which 1248 // the web renderer turns into an anchor. 1249 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 1250 1251 // both the item text and the link are copied into the arena, so the caller's 1252 // slices needn't outlive this call. 1253 pub fn setItems(self: *FlowBox, allocator: std.mem.Allocator, items: []const Item) !void { 1254 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1255 self.text_boxes.clearAndFree(allocator); 1256 1257 // the old text boxes (and their borrowed content/links) are gone now, so 1258 // the arena that backed those strings can be reclaimed. 1259 _ = self.arena.reset(.retain_capacity); 1260 const aa = self.arena.allocator(); 1261 1262 self.focus.clear(); 1263 self.focus.child_id = null; 1264 1265 for (items) |item| { 1266 const line = try aa.dupe(u8, item.text); 1267 1268 var text_box = try wgt.TextBox(Widget).init(allocator, line, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 1269 errdefer text_box.deinit(allocator); 1270 text_box.getFocus().focusable = true; 1271 1272 if (item.link.len > 0) { 1273 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 1274 } 1275 1276 try self.text_boxes.append(allocator, text_box); 1277 } 1278 1279 if (self.text_boxes.items.len > 0) { 1280 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 1281 } 1282 } 1283 1284 pub fn build(self: *FlowBox, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1285 self.clearGrid(); 1286 self.focus.clear(); 1287 1288 const cell_width = self.options.cell_width; 1289 const max_width = constraint.max_size.width orelse cell_width; 1290 const cols = if (cell_width == 0) 1 else @max(1, max_width / cell_width); 1291 self.last_cols = cols; 1292 1293 const count = self.text_boxes.items.len; 1294 if (count == 0) return; 1295 const slot_height = self.options.cell_height + border_rows; 1296 if (slot_height == 0) return; 1297 1298 // build at the slot size so every text box fits a single grid cell 1299 for (self.text_boxes.items) |*tb| { 1300 tb.options.border_style = if (self.focus.child_id == tb.getFocus().id) .single else .hidden; 1301 try tb.build(allocator, .{ 1302 .min_size = .{ .width = cell_width, .height = null }, 1303 .max_size = .{ .width = cell_width, .height = slot_height }, 1304 }, root_focus); 1305 } 1306 1307 const rows = (count + cols - 1) / cols; 1308 const content_height = rows * slot_height; 1309 const total_width = cols * cell_width; 1310 if (total_width == 0 or content_height == 0) return; 1311 1312 var grid = try Grid.init(allocator, .{ .width = total_width, .height = content_height }); 1313 errdefer grid.deinit(); 1314 1315 for (self.text_boxes.items, 0..) |*tb, i| { 1316 const tb_grid = tb.getGrid() orelse continue; 1317 const col = i % cols; 1318 const row = i / cols; 1319 const cell_x = col * cell_width; 1320 const cell_y = row * slot_height; 1321 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, cell_x, cell_y); 1322 try grid.drawGrid(tb_grid, cell_x, cell_y); 1323 } 1324 1325 self.grid = grid; 1326 } 1327 1328 pub fn input(self: *FlowBox, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1329 _ = self; 1330 _ = allocator; 1331 _ = key; 1332 _ = root_focus; 1333 } 1334 1335 pub fn clearGrid(self: *FlowBox) void { 1336 if (self.grid) |*grid| { 1337 grid.deinit(); 1338 self.grid = null; 1339 } 1340 for (self.text_boxes.items) |*tb| tb.clearGrid(); 1341 } 1342 1343 pub fn getGrid(self: FlowBox) ?Grid { 1344 return self.grid; 1345 } 1346 1347 pub fn getFocus(self: *FlowBox) *Focus { 1348 return self.focus; 1349 } 1350 1351 pub fn cellRect(self: FlowBox, index: usize) ?layout.IRect { 1352 if (self.last_cols == 0 or index >= self.text_boxes.items.len) return null; 1353 const slot_height = self.options.cell_height + border_rows; 1354 const col = index % self.last_cols; 1355 const row = index / self.last_cols; 1356 return .{ 1357 .x = @intCast(col * self.options.cell_width), 1358 .y = @intCast(row * slot_height), 1359 .size = .{ .width = self.options.cell_width, .height = slot_height }, 1360 }; 1361 } 1362 1363 pub fn indexOfFocusId(self: FlowBox, focus_id: usize) ?usize { 1364 for (self.text_boxes.items, 0..) |tb, i| { 1365 if (tb.box.focus.id == focus_id) return i; 1366 } 1367 return null; 1368 } 1369 1370 pub const Scroll = struct { 1371 scroll: wgt.Scroll(Widget), 1372 1373 pub fn init(allocator: std.mem.Allocator, options: FlowBox.Options, web_native: bool) !Scroll { 1374 var layout_inner = try FlowBox.init(allocator, options); 1375 errdefer layout_inner.deinit(allocator); 1376 var scroll = try wgt.Scroll(Widget).init(allocator, .{ .flow_box = layout_inner }, .{ .direction = .vert, .web_native = web_native }); 1377 errdefer scroll.deinit(allocator); 1378 return .{ .scroll = scroll }; 1379 } 1380 1381 pub fn deinit(self: *Scroll, allocator: std.mem.Allocator) void { 1382 self.scroll.deinit(allocator); 1383 } 1384 1385 pub fn setItems(self: *Scroll, allocator: std.mem.Allocator, items: []const Item) !void { 1386 self.scroll.x = 0; 1387 self.scroll.y = 0; 1388 try self.inner().setItems(allocator, items); 1389 } 1390 1391 pub fn build(self: *Scroll, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1392 try self.scroll.build(allocator, constraint, root_focus); 1393 } 1394 1395 pub fn input(self: *Scroll, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1396 _ = allocator; 1397 const in = self.inner(); 1398 const child_id = in.focus.child_id orelse return; 1399 const current_index = in.indexOfFocusId(child_id) orelse return; 1400 const count = in.text_boxes.items.len; 1401 if (count == 0) return; 1402 const cols = in.last_cols; 1403 const slot_height = in.options.cell_height + FlowBox.border_rows; 1404 1405 var index = current_index; 1406 switch (key) { 1407 .arrow_up => index -|= cols, 1408 .arrow_down => if (index + cols < count) { 1409 index += cols; 1410 }, 1411 .arrow_left => index -|= 1, 1412 .arrow_right => if (index + 1 < count) { 1413 index += 1; 1414 }, 1415 .home => index = 0, 1416 .end => index = count - 1, 1417 .page_up => { 1418 if (self.scroll.grid) |grid| if (slot_height > 0) { 1419 const rows_per_page = grid.size.height / slot_height; 1420 index -|= rows_per_page * cols; 1421 }; 1422 }, 1423 .page_down => { 1424 if (self.scroll.grid) |grid| if (slot_height > 0) { 1425 const rows_per_page = grid.size.height / slot_height; 1426 index = @min(index + rows_per_page * cols, count - 1); 1427 }; 1428 }, 1429 // scroll wheel moves the focused cell by a full row so the 1430 // viewport (via scrollToRect) follows in row-sized steps, 1431 // matching how a scroll wheel feels in a grid view. 1432 .mouse => |mouse| switch (mouse.action) { 1433 .scroll => |dir| switch (dir) { 1434 .up => index -|= cols, 1435 .down => if (index + cols < count) { 1436 index += cols; 1437 }, 1438 }, 1439 else => {}, 1440 }, 1441 else => {}, 1442 } 1443 1444 if (index != current_index) { 1445 root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 1446 if (in.cellRect(index)) |rect| { 1447 self.scroll.scrollToRect(rect); 1448 } 1449 } 1450 } 1451 1452 pub fn clearGrid(self: *Scroll) void { 1453 self.scroll.clearGrid(); 1454 } 1455 1456 pub fn getGrid(self: Scroll) ?Grid { 1457 return self.scroll.getGrid(); 1458 } 1459 1460 pub fn getFocus(self: *Scroll) *Focus { 1461 return self.scroll.getFocus(); 1462 } 1463 1464 pub fn getSelectedIndex(self: Scroll) ?usize { 1465 const in = self.scroll.child.flow_box; 1466 const child_id = in.focus.child_id orelse return null; 1467 return in.indexOfFocusId(child_id); 1468 } 1469 1470 fn inner(self: *Scroll) *FlowBox { 1471 return &self.scroll.child.flow_box; 1472 } 1473 }; 1474 }; 1475 1476 // a left-to-right flow of variable-width focusable items that wraps at the 1477 // available width, like words in a paragraph. unlike FlowBox there is no cell 1478 // grid: each item is as wide as its text. selection movement is driven by the 1479 // owning view via indexOfFocusId/verticalNeighbor. 1480 pub const TagFlow = struct { 1481 focus: *Focus, 1482 grid: ?Grid, 1483 text_boxes: std.ArrayList(wgt.TextBox(Widget)), 1484 // last build's item rects (content space), for vertical navigation and 1485 // scroll-into-view. 1486 rects: std.ArrayList(layout.IRect), 1487 // backs the per-item strings the text boxes borrow. 1488 arena: std.heap.ArenaAllocator, 1489 1490 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 1491 1492 pub fn init(allocator: std.mem.Allocator) !TagFlow { 1493 return .{ 1494 .focus = try Focus.create(allocator, .container), 1495 .grid = null, 1496 .text_boxes = .empty, 1497 .rects = .empty, 1498 .arena = std.heap.ArenaAllocator.init(allocator), 1499 }; 1500 } 1501 1502 pub fn deinit(self: *TagFlow, allocator: std.mem.Allocator) void { 1503 self.focus.destroy(allocator); 1504 if (self.grid) |*grid| { 1505 grid.deinit(); 1506 self.grid = null; 1507 } 1508 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1509 self.text_boxes.deinit(allocator); 1510 self.rects.deinit(allocator); 1511 self.arena.deinit(); 1512 } 1513 1514 // both the item text and the link are copied into the arena, so the caller's 1515 // slices needn't outlive this call. 1516 pub fn setItems(self: *TagFlow, allocator: std.mem.Allocator, items: []const Item) !void { 1517 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1518 self.text_boxes.clearAndFree(allocator); 1519 1520 _ = self.arena.reset(.retain_capacity); 1521 const aa = self.arena.allocator(); 1522 1523 self.focus.clear(); 1524 self.focus.child_id = null; 1525 1526 for (items) |item| { 1527 const line = try aa.dupe(u8, item.text); 1528 1529 var text_box = try wgt.TextBox(Widget).init(allocator, line, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 1530 errdefer text_box.deinit(allocator); 1531 text_box.getFocus().focusable = true; 1532 1533 if (item.link.len > 0) { 1534 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 1535 } 1536 1537 try self.text_boxes.append(allocator, text_box); 1538 } 1539 1540 if (self.text_boxes.items.len > 0) { 1541 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 1542 } 1543 } 1544 1545 pub fn build(self: *TagFlow, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1546 self.clearGrid(); 1547 self.focus.clear(); 1548 1549 if (self.text_boxes.items.len == 0) return; 1550 const max_width = constraint.max_size.width orelse 80; 1551 if (max_width == 0) return; 1552 1553 // lay the items out left to right, wrapping to a new row when the next 1554 // one wouldn't fit. 1555 self.rects.clearRetainingCapacity(); 1556 var x: usize = 0; 1557 var y: usize = 0; 1558 var row_height: usize = 0; 1559 var total_width: usize = 0; 1560 for (self.text_boxes.items) |*tb| { 1561 tb.options.border_style = if (self.focus.child_id == tb.getFocus().id) .single else .hidden; 1562 try tb.build(allocator, .{ 1563 .min_size = .{ .width = null, .height = null }, 1564 .max_size = .{ .width = max_width, .height = null }, 1565 }, root_focus); 1566 const tb_grid_maybe = tb.getGrid(); 1567 const w: usize = if (tb_grid_maybe) |g| g.size.width else 0; 1568 const h: usize = if (tb_grid_maybe) |g| g.size.height else 0; 1569 if (x > 0 and x + w > max_width) { 1570 x = 0; 1571 y += row_height; 1572 row_height = 0; 1573 } 1574 try self.rects.append(allocator, .{ .x = @intCast(x), .y = @intCast(y), .size = .{ .width = w, .height = h } }); 1575 if (h > row_height) row_height = h; 1576 x += w; 1577 if (x > total_width) total_width = x; 1578 } 1579 1580 const total_height = y + row_height; 1581 if (total_width == 0 or total_height == 0) return; 1582 1583 var grid = try Grid.init(allocator, .{ .width = total_width, .height = total_height }); 1584 errdefer grid.deinit(); 1585 1586 for (self.text_boxes.items, self.rects.items) |*tb, rect| { 1587 const tb_grid = tb.getGrid() orelse continue; 1588 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, @intCast(rect.x), @intCast(rect.y)); 1589 try grid.drawGrid(tb_grid, @intCast(rect.x), @intCast(rect.y)); 1590 } 1591 1592 self.grid = grid; 1593 } 1594 1595 pub fn input(self: *TagFlow, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1596 _ = self; 1597 _ = allocator; 1598 _ = key; 1599 _ = root_focus; 1600 } 1601 1602 pub fn clearGrid(self: *TagFlow) void { 1603 if (self.grid) |*grid| { 1604 grid.deinit(); 1605 self.grid = null; 1606 } 1607 for (self.text_boxes.items) |*tb| tb.clearGrid(); 1608 } 1609 1610 pub fn getGrid(self: TagFlow) ?Grid { 1611 return self.grid; 1612 } 1613 1614 pub fn getFocus(self: *TagFlow) *Focus { 1615 return self.focus; 1616 } 1617 1618 pub fn indexOfFocusId(self: TagFlow, focus_id: usize) ?usize { 1619 for (self.text_boxes.items, 0..) |tb, i| { 1620 if (tb.box.focus.id == focus_id) return i; 1621 } 1622 return null; 1623 } 1624 1625 // the first item of the row below (when `downward`) or above, or null at 1626 // the flow's edge. items are laid out in order, so y is nondecreasing. 1627 pub fn rowStep(self: TagFlow, index: usize, downward: bool) ?usize { 1628 const rects = self.rects.items; 1629 if (index >= rects.len) return null; 1630 const cur_y = rects[index].y; 1631 if (downward) { 1632 for (rects[index + 1 ..], index + 1..) |r, i| { 1633 if (r.y > cur_y) return i; 1634 } 1635 return null; 1636 } 1637 // walk the earlier rows; the last one visited is the row above. 1638 var prev_first: ?usize = null; 1639 var i: usize = 0; 1640 while (i < rects.len and rects[i].y < cur_y) { 1641 prev_first = i; 1642 const y = rects[i].y; 1643 while (i < rects.len and rects[i].y == y) i += 1; 1644 } 1645 return prev_first; 1646 } 1647 }; 1648 1649 // an invisible widget that fills the horizontal space granted by its parent. 1650 // used inside Box(horiz) with a min_size so the box reserves space for the 1651 // children that follow, pushing them to the right. 1652 pub const Spacer = struct { 1653 focus: *Focus, 1654 grid: ?Grid, 1655 1656 pub fn init(allocator: std.mem.Allocator) !Spacer { 1657 return .{ 1658 .focus = try Focus.create(allocator, .container), 1659 .grid = null, 1660 }; 1661 } 1662 1663 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 1664 self.focus.destroy(allocator); 1665 if (self.grid) |*grid| { 1666 grid.deinit(); 1667 self.grid = null; 1668 } 1669 } 1670 1671 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1672 _ = root_focus; 1673 self.clearGrid(); 1674 const width = constraint.max_size.width orelse return; 1675 if (width == 0) return; 1676 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1677 } 1678 1679 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1680 _ = self; 1681 _ = allocator; 1682 _ = key; 1683 _ = root_focus; 1684 } 1685 1686 pub fn clearGrid(self: *Spacer) void { 1687 if (self.grid) |*grid| { 1688 grid.deinit(); 1689 self.grid = null; 1690 } 1691 } 1692 1693 pub fn getGrid(self: Spacer) ?Grid { 1694 return self.grid; 1695 } 1696 1697 pub fn getFocus(self: *Spacer) *Focus { 1698 return self.focus; 1699 } 1700 }; 1701 1702 // a one-row, non-focusable status bar showing the current page's url 1703 pub const Footer = struct { 1704 focus: *Focus, 1705 grid: ?Grid, 1706 session: *Session, 1707 arena: std.heap.ArenaAllocator, 1708 1709 pub fn init(allocator: std.mem.Allocator, session: *Session) !Footer { 1710 return .{ 1711 .focus = try Focus.create(allocator, .container), 1712 .grid = null, 1713 .session = session, 1714 .arena = std.heap.ArenaAllocator.init(allocator), 1715 }; 1716 } 1717 1718 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 1719 self.focus.destroy(allocator); 1720 if (self.grid) |*grid| grid.deinit(); 1721 self.arena.deinit(); 1722 } 1723 1724 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1725 _ = root_focus; 1726 self.clearGrid(); 1727 const width = constraint.max_size.width orelse return; 1728 if (width == 0) return; 1729 1730 _ = self.arena.reset(.retain_capacity); 1731 const aa = self.arena.allocator(); 1732 const page = self.session.data.current_page; 1733 const path = (if (self.session.local != null) page.urlAllocLocal(&self.arena) else page.urlAlloc(&self.arena)) catch return; 1734 const text = if (self.session.web_port) |port| 1735 std.fmt.allocPrint(aa, "http://localhost:{d}{s}", .{ port, path }) catch return 1736 else 1737 path; 1738 1739 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1740 errdefer grid.deinit(); 1741 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 1742 var i: usize = 0; 1743 while (utf8.nextCodepointSlice()) |ch| { 1744 if (i == width) break; 1745 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 1746 i += 1; 1747 } 1748 self.grid = grid; 1749 } 1750 1751 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1752 _ = self; 1753 _ = allocator; 1754 _ = key; 1755 _ = root_focus; 1756 } 1757 1758 pub fn clearGrid(self: *Footer) void { 1759 if (self.grid) |*grid| { 1760 grid.deinit(); 1761 self.grid = null; 1762 } 1763 } 1764 1765 pub fn getGrid(self: Footer) ?Grid { 1766 return self.grid; 1767 } 1768 1769 pub fn getFocus(self: *Footer) *Focus { 1770 return self.focus; 1771 } 1772 }; 1773 1774 // a single-child wrapper that builds the child at its natural size and 1775 // positions its grid in the middle of the area granted by the parent 1776 pub const Center = struct { 1777 focus: *Focus, 1778 grid: ?Grid, 1779 child: *Widget, 1780 1781 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 1782 const child = try allocator.create(Widget); 1783 errdefer allocator.destroy(child); 1784 child.* = child_widget; 1785 return .{ 1786 .focus = try Focus.create(allocator, .container), 1787 .grid = null, 1788 .child = child, 1789 }; 1790 } 1791 1792 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1793 self.focus.destroy(allocator); 1794 if (self.grid) |*grid| { 1795 grid.deinit(); 1796 self.grid = null; 1797 } 1798 self.child.deinit(allocator); 1799 allocator.destroy(self.child); 1800 } 1801 1802 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1803 self.clearGrid(); 1804 self.getFocus().clear(); 1805 1806 // build the child without forcing it to fill min_size; it sizes 1807 // itself to its content within the available max. 1808 try self.child.build(allocator, .{ 1809 .min_size = .{ .width = null, .height = null }, 1810 .max_size = constraint.max_size, 1811 }, root_focus); 1812 1813 const child_grid = self.child.getGrid() orelse return; 1814 1815 // prefer max when given; otherwise grow to max(min, child) so that 1816 // when only a min is set (e.g. the wasm path passes viewport rows 1817 // as min_height with no max), we can still vertically center while 1818 // letting taller content extend past the min. 1819 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; 1820 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; 1821 if (width == 0 or height == 0) return; 1822 1823 const offset_x: usize = (width -| child_grid.size.width) / 2; 1824 const offset_y: usize = (height -| child_grid.size.height) / 2; 1825 1826 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1827 errdefer grid.deinit(); 1828 try grid.drawGrid(child_grid, offset_x, offset_y); 1829 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1830 self.getFocus().child_id = self.child.getFocus().id; 1831 1832 self.grid = grid; 1833 } 1834 1835 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1836 try self.child.input(allocator, key, root_focus); 1837 } 1838 1839 pub fn clearGrid(self: *Center) void { 1840 if (self.grid) |*grid| { 1841 grid.deinit(); 1842 self.grid = null; 1843 } 1844 self.child.clearGrid(); 1845 } 1846 1847 pub fn getGrid(self: Center) ?Grid { 1848 return self.grid; 1849 } 1850 1851 pub fn getFocus(self: *Center) *Focus { 1852 return self.focus; 1853 } 1854 }; 1855 1856 // renders truecolor ANSI art 1857 pub const AnsiArt = struct { 1858 focus: *Focus, 1859 grid: ?Grid, 1860 content: []const u8, 1861 1862 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1863 return .{ 1864 .focus = try Focus.create(allocator, .container), 1865 .grid = null, 1866 .content = content, 1867 }; 1868 } 1869 1870 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1871 self.focus.destroy(allocator); 1872 if (self.grid) |*grid| { 1873 grid.deinit(); 1874 self.grid = null; 1875 } 1876 } 1877 1878 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1879 _ = root_focus; 1880 self.clearGrid(); 1881 1882 // parse into rows of cells, then copy into a rectangular grid 1883 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1884 defer { 1885 for (rows.items) |*row| row.deinit(allocator); 1886 rows.deinit(allocator); 1887 } 1888 var row: std.ArrayList(Grid.Cell) = .empty; 1889 errdefer row.deinit(allocator); 1890 1891 var style: Grid.Style = .{}; 1892 var width: usize = 0; 1893 const content = self.content; 1894 var i: usize = 0; 1895 while (i < content.len) { 1896 const byte = content[i]; 1897 if (byte == '\n') { 1898 width = @max(width, row.items.len); 1899 try rows.append(allocator, row); 1900 row = .empty; 1901 i += 1; 1902 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1903 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1904 var j = i + 2; 1905 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1906 if (j >= content.len) { 1907 i = content.len; // malformed trailing escape; stop 1908 } else { 1909 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1910 i = j + 1; 1911 } 1912 } else { 1913 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1914 const end = @min(content.len, i + len); 1915 const rune = content[i..end]; 1916 const transparent = end - i == 1 and rune[0] == ' ' and 1917 style.fg == null and style.bg == null and !style.inverted; 1918 try row.append(allocator, .{ 1919 .rune = if (transparent) null else rune, 1920 .style = style, 1921 }); 1922 i = end; 1923 } 1924 } 1925 // a trailing row with content but no closing newline 1926 if (row.items.len > 0) { 1927 width = @max(width, row.items.len); 1928 try rows.append(allocator, row); 1929 } else { 1930 row.deinit(allocator); 1931 } 1932 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1933 1934 const height = rows.items.len; 1935 if (width == 0 or height == 0) return; 1936 1937 const clamped_w = @min(width, constraint.max_size.width orelse width); 1938 const clamped_h = @min(height, constraint.max_size.height orelse height); 1939 1940 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1941 errdefer grid.deinit(); 1942 for (rows.items[0..clamped_h], 0..) |r, y| { 1943 const n = @min(r.items.len, clamped_w); 1944 for (r.items[0..n], 0..) |cell, x| { 1945 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 1946 } 1947 } 1948 self.grid = grid; 1949 } 1950 1951 pub fn clearGrid(self: *AnsiArt) void { 1952 if (self.grid) |*grid| { 1953 grid.deinit(); 1954 self.grid = null; 1955 } 1956 } 1957 1958 pub fn getGrid(self: AnsiArt) ?Grid { 1959 return self.grid; 1960 } 1961 1962 pub fn getFocus(self: *AnsiArt) *Focus { 1963 return self.focus; 1964 } 1965 1966 fn applySgr(style: *Grid.Style, params: []const u8) void { 1967 var nums: [16]u32 = undefined; 1968 var n: usize = 0; 1969 var it = std.mem.splitScalar(u8, params, ';'); 1970 while (it.next()) |tok| { 1971 if (n >= nums.len) break; 1972 // an empty parameter (e.g. bare "\x1b[m") means 0 1973 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1974 n += 1; 1975 } 1976 if (n == 0) { 1977 style.* = .{}; 1978 return; 1979 } 1980 var i: usize = 0; 1981 while (i < n) : (i += 1) { 1982 switch (nums[i]) { 1983 0 => style.* = .{}, 1984 7 => style.inverted = true, 1985 27 => style.inverted = false, 1986 39 => style.fg = null, 1987 49 => style.bg = null, 1988 38, 48 => { 1989 // truecolor form: 38;2;r;g;b — anything else is ignored 1990 if (i + 4 < n and nums[i + 1] == 2) { 1991 const c = Grid.Color{ 1992 .r = @truncate(nums[i + 2]), 1993 .g = @truncate(nums[i + 3]), 1994 .b = @truncate(nums[i + 4]), 1995 }; 1996 if (nums[i] == 38) style.fg = c else style.bg = c; 1997 i += 4; 1998 } 1999 }, 2000 else => {}, 2001 } 2002 } 2003 } 2004 }; 2005 2006 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 2007 pub const AnsiBackground = struct { 2008 grid: ?Grid, 2009 child: *Widget, 2010 art: AnsiArt, 2011 session: *Session, 2012 2013 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 2014 var cw = child_widget; 2015 const child = allocator.create(Widget) catch |e| { 2016 cw.deinit(allocator); 2017 return e; 2018 }; 2019 child.* = cw; 2020 errdefer { 2021 child.deinit(allocator); 2022 allocator.destroy(child); 2023 } 2024 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2025 } 2026 2027 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2028 if (self.grid) |*grid| grid.deinit(); 2029 self.art.deinit(allocator); 2030 self.child.deinit(allocator); 2031 allocator.destroy(self.child); 2032 } 2033 2034 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2035 self.clearGrid(); 2036 2037 // make the wrapped page fill the whole available area 2038 try self.child.build(allocator, .{ 2039 .min_size = .{ 2040 .width = constraint.max_size.width orelse constraint.min_size.width, 2041 .height = constraint.max_size.height orelse constraint.min_size.height, 2042 }, 2043 .max_size = constraint.max_size, 2044 }, root_focus); 2045 2046 if (self.session.data.enable_ansi) { 2047 if (self.child.getGrid()) |fg| { 2048 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 2049 } 2050 } 2051 } 2052 2053 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2054 try self.child.input(allocator, key, root_focus); 2055 } 2056 2057 pub fn clearGrid(self: *AnsiBackground) void { 2058 if (self.grid) |*grid| { 2059 grid.deinit(); 2060 self.grid = null; 2061 } 2062 self.child.clearGrid(); 2063 self.art.clearGrid(); 2064 } 2065 2066 pub fn getGrid(self: AnsiBackground) ?Grid { 2067 return self.grid orelse self.child.getGrid(); 2068 } 2069 2070 pub fn getFocus(self: *AnsiBackground) *Focus { 2071 return self.child.getFocus(); 2072 } 2073 2074 // which top corner the art hugs. 2075 const ArtAnchor = enum { top_left, top_right }; 2076 2077 // a foreground cell counts as blank if it's empty or a bare space without styling 2078 fn cellIsBlank(cell: Grid.Cell) bool { 2079 if (cell.rune) |rune| { 2080 return std.mem.eql(u8, rune, " ") and 2081 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2082 } 2083 return true; 2084 } 2085 2086 // the art is dimmed to this fraction of its brightness so foreground text 2087 // stays legible over it 2088 const art_brightness = 45; // percent 2089 2090 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2091 const c = color orelse return null; 2092 return .{ 2093 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2094 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2095 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2096 }; 2097 } 2098 2099 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2100 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2101 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2102 // snap such channels clear of that range so the backdrop doesn't blink or 2103 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2104 // actually render truecolor. 2105 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2106 const c = color orelse return null; 2107 const snap = struct { 2108 fn f(v: u8) u8 { 2109 return if (v >= 1 and v <= 9) 10 else v; 2110 } 2111 }.f; 2112 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2113 } 2114 2115 // composites ANSI art behind a foreground grid 2116 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 2117 art.clearGrid(); 2118 try art.build(allocator, .{ 2119 .min_size = .{ .width = null, .height = null }, 2120 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2121 }, root_focus); 2122 2123 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2124 errdefer out.deinit(); 2125 2126 if (art.getGrid()) |art_grid| { 2127 const anchor_x = switch (anchor) { 2128 .top_left => 0, 2129 .top_right => foreground.size.width -| art_grid.size.width, 2130 }; 2131 for (0..art_grid.size.height) |y| { 2132 for (0..art_grid.size.width) |x| { 2133 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2134 if (src.rune == null) continue; 2135 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2136 const dst = &out.cells.items[idx]; 2137 // composite the dimmed art behind every cell so it shows 2138 // through the whole UI: a blank cell becomes the art, while a 2139 // cell with a glyph keeps the glyph but takes the dimmed art as 2140 // its background (replacing whatever background it had, which 2141 // would otherwise obscure the art). 2142 if (cellIsBlank(dst.*)) { 2143 dst.* = src; 2144 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2145 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2146 } else { 2147 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2148 } 2149 } 2150 } 2151 } 2152 return out; 2153 } 2154 }; 2155