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