╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1142 session.page_arena = arena; 1143 const route = session.data.current_page; 1144 1145 const page = try arena.allocator().create(Page); 1146 page.* = try Page.init(arena, session, route); 1147 return .{ 1148 .root = try initRoot(allocator, page, session), 1149 .route = route, 1150 .arena = arena, 1151 .history = .empty, 1152 }; 1153 } 1154 1155 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 1156 self.root.deinit(allocator); 1157 freeArena(allocator, self.arena); 1158 for (self.history.items) |*entry| { 1159 entry.root.deinit(allocator); 1160 freeArena(allocator, entry.arena); 1161 } 1162 self.history.deinit(allocator); 1163 } 1164 1165 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 1166 arena.deinit(); 1167 allocator.destroy(arena); 1168 } 1169 1170 // reconcile the displayed root with the session's navigation state. a 1171 // forward nav (current_page moved to a different parent page) pushes the 1172 // current root and builds the new page in a fresh arena; a back request 1173 // frees the current page and restores the previous one. when escape is 1174 // pressed with no history left we switch to the quit tab instead of quitting. 1175 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 1176 // refresh: rebuild the current page in place from a current moment 1177 if (session.refresh_requested) { 1178 session.refresh_requested = false; 1179 if (session.haxy_moment != null or session.local != null) { 1180 const arena = try allocator.create(std.heap.ArenaAllocator); 1181 arena.* = std.heap.ArenaAllocator.init(allocator); 1182 errdefer freeArena(allocator, arena); 1183 1184 session.page_arena = arena; 1185 1186 const page = try arena.allocator().create(Page); 1187 page.* = try Page.init(arena, session, self.route); 1188 const new_root = try initRoot(allocator, page, session); 1189 1190 self.root.deinit(allocator); 1191 freeArena(allocator, self.arena); 1192 self.root = new_root; 1193 self.arena = arena; 1194 } 1195 return; 1196 } 1197 1198 if (session.nav_back) { 1199 session.nav_back = false; 1200 if (self.history.pop()) |entry| { 1201 self.root.deinit(allocator); 1202 freeArena(allocator, self.arena); 1203 self.root = entry.root; 1204 self.route = entry.route; 1205 self.arena = entry.arena; 1206 session.page_arena = entry.arena; 1207 session.data.current_page = entry.route; 1208 return; 1209 } 1210 // nothing to go back to; switch to the quit confirmation 1211 const root_focus = self.root.getFocus(); 1212 var iter = root_focus.children.iterator(); 1213 while (iter.next()) |entry| { 1214 switch (entry.value_ptr.focus.kind) { 1215 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 1216 root_focus.setFocus(entry.key_ptr.*); 1217 // a Stack only builds its selected child, so the quit 1218 // button isn't in the focus tree until a build runs with 1219 // the quit tab selected. build once, then send a 1220 // synthetic arrow_down to drop focus from the tab onto 1221 // the button so it's ready to confirm. 1222 try self.root.build(allocator, .{ 1223 .min_size = .{ .width = null, .height = 40 }, 1224 .max_size = .{ .width = 80, .height = null }, 1225 }, root_focus); 1226 try self.root.input(allocator, .arrow_down, root_focus); 1227 break; 1228 }, 1229 else => {}, 1230 } 1231 } 1232 return; 1233 } 1234 1235 // forward navigation: navigate() set next_page to the page to move to (a 1236 // cross-page link or tab change crossing pages) 1237 if (session.next_page) |route| { 1238 session.next_page = null; 1239 if (session.haxy_moment == null and session.local == null) return; 1240 // the page we navigated to becomes the current page 1241 session.data.current_page = route; 1242 1243 const arena = try allocator.create(std.heap.ArenaAllocator); 1244 arena.* = std.heap.ArenaAllocator.init(allocator); 1245 errdefer freeArena(allocator, arena); 1246 1247 session.page_arena = arena; 1248 1249 const page = try arena.allocator().create(Page); 1250 page.* = try Page.init(arena, session, route); 1251 const new_root = try initRoot(allocator, page, session); 1252 1253 try self.history.append(allocator, .{ .root = self.root, .route = self.route, .arena = self.arena }); 1254 // drop the oldest entry (freeing its widget tree and arena) once over cap 1255 if (self.history.items.len > max_history) { 1256 var oldest = self.history.orderedRemove(0); 1257 oldest.root.deinit(allocator); 1258 freeArena(allocator, oldest.arena); 1259 } 1260 self.root = new_root; 1261 self.route = route; 1262 self.arena = arena; 1263 } 1264 } 1265 }; 1266 1267 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 1268 const page_widget: Widget = switch (page.*) { 1269 .home => |*p| .{ .home = try .init(allocator, p, session) }, 1270 .user => |*p| .{ .user = try .init(allocator, p, session) }, 1271 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 1272 }; 1273 1274 const demon_art = @embedFile("embed/demon.ans"); 1275 1276 // on the TUI/SSH, the page sits above a one-row footer showing the url 1277 var root = if (session.is_terminal) blk: { 1278 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1279 errdefer box.deinit(allocator); 1280 const bg_id = bg_blk: { 1281 var background = try AnsiBackground.init(allocator, page_widget, demon_art, session); 1282 errdefer background.deinit(allocator); 1283 const id = background.getFocus().id; 1284 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 1285 break :bg_blk id; 1286 }; 1287 { 1288 var footer = try Footer.init(allocator, session); 1289 errdefer footer.deinit(allocator); 1290 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 1291 } 1292 box.getFocus().child_id = bg_id; 1293 break :blk Widget{ .box = box }; 1294 } else Widget{ .background = try AnsiBackground.init(allocator, page_widget, demon_art, session) }; 1295 errdefer root.deinit(allocator); 1296 1297 // input-owning views build their TextInputs in init — so reset the 1298 // focus-id -> *TextInput map here 1299 session.text_inputs.clearRetainingCapacity(); 1300 session.input_values.clearRetainingCapacity(); 1301 1302 try root.build(allocator, .{ 1303 .min_size = .{ .width = null, .height = 40 }, 1304 .max_size = .{ .width = 100, .height = null }, 1305 }, root.getFocus()); 1306 1307 return root; 1308 } 1309 1310 pub const Widget = union(enum) { 1311 text: wgt.Text(Widget), 1312 box: wgt.Box(Widget), 1313 text_box: wgt.TextBox(Widget), 1314 text_input: wgt.TextInput(Widget), 1315 scroll: wgt.Scroll(Widget), 1316 stack: wgt.Stack(Widget), 1317 flow_box: FlowBox, 1318 flow_box_scroll: FlowBox.Scroll, 1319 tag_flow: TagFlow, 1320 spacer: Spacer, 1321 center: Center, 1322 background: AnsiBackground, 1323 home: Home.View, 1324 user: User.View, 1325 repo: Repo.View, 1326 quit: Quit.View, 1327 title: Title.View, 1328 sub_title: SubTitle.View, 1329 home_header: Home.Header.View, 1330 user_header: User.Header.View, 1331 repo_header: Repo.Header.View, 1332 repo_files_header: Repo.Files.Header.View, 1333 repo_commits_header: Repo.Commits.Header.View, 1334 repo_issues_header: Repo.Issues.Header, 1335 repo_files: Repo.Files.View, 1336 repo_commits: Repo.Commits.View, 1337 repo_refs: Repo.Refs.View, 1338 repo_issues: Repo.Issues.View, 1339 home_users: Home.Users.View, 1340 home_repos: Home.Repos.View, 1341 auth_tab: Home.Header.AuthTab.View, 1342 home_settings: Home.Settings.View, 1343 home_auth: Home.Auth.View, 1344 footer: Footer, 1345 1346 pub fn deinit(self: *Widget, allocator: std.mem.Allocator) void { 1347 switch (self.*) { 1348 inline else => |*case| case.deinit(allocator), 1349 } 1350 } 1351 1352 pub fn build(self: *Widget, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) anyerror!void { 1353 switch (self.*) { 1354 inline else => |*case| try case.build(allocator, constraint, root_focus), 1355 } 1356 } 1357 1358 pub fn input(self: *Widget, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) anyerror!void { 1359 switch (self.*) { 1360 inline else => |*case| try case.input(allocator, key, root_focus), 1361 } 1362 } 1363 1364 pub fn clearGrid(self: *Widget) void { 1365 switch (self.*) { 1366 inline else => |*case| case.clearGrid(), 1367 } 1368 } 1369 1370 pub fn getGrid(self: Widget) ?Grid { 1371 switch (self) { 1372 inline else => |*case| return case.getGrid(), 1373 } 1374 } 1375 1376 pub fn getFocus(self: *Widget) *Focus { 1377 switch (self.*) { 1378 inline else => |*case| return case.getFocus(), 1379 } 1380 } 1381 }; 1382 1383 // move the focused row in `box` by `delta`, clamped to the ends, scrolling 1384 // `scroll` to keep it visible. 1385 pub fn moveRowFocus(box: *wgt.Box(Widget), scroll: *wgt.Scroll(Widget), root_focus: *Focus, delta: isize) void { 1386 const keys = box.children.keys(); 1387 if (keys.len == 0) return; 1388 const cur_id = box.getFocus().child_id orelse return; 1389 const cur: isize = @intCast(box.children.getIndex(cur_id) orelse return); 1390 const last: isize = @intCast(keys.len - 1); 1391 const next: usize = @intCast(std.math.clamp(cur + delta, 0, last)); 1392 if (next == @as(usize, @intCast(cur))) return; 1393 root_focus.setFocus(keys[next]); 1394 if (box.children.values()[next].rect) |rect| scroll.scrollToRect(rect); 1395 } 1396 1397 pub const FlowBox = struct { 1398 focus: *Focus, 1399 grid: ?Grid, 1400 text_boxes: std.ArrayList(wgt.TextBox(Widget)), 1401 // backs the per-item strings the text boxes borrow: each box's `content` 1402 // and, for link items, its `.custom` focus kind (e.g. "a:/user/foo"). reset 1403 // wholesale on each setItems so there's no per-string ownership to track. 1404 arena: std.heap.ArenaAllocator, 1405 // column count from the last build — FlowBox.Scroll.input uses it so arrow 1406 // up/down can step by a row's worth of items. 1407 last_cols: usize, 1408 options: Options, 1409 1410 const border_rows: usize = 2; 1411 1412 pub const Options = struct { 1413 cell_width: usize = 40, 1414 cell_height: usize = 3, 1415 }; 1416 1417 pub fn init(allocator: std.mem.Allocator, options: Options) !FlowBox { 1418 return .{ 1419 .focus = try Focus.create(allocator, .container), 1420 .grid = null, 1421 .text_boxes = .empty, 1422 .arena = std.heap.ArenaAllocator.init(allocator), 1423 .last_cols = 1, 1424 .options = options, 1425 }; 1426 } 1427 1428 pub fn deinit(self: *FlowBox, allocator: std.mem.Allocator) void { 1429 self.focus.destroy(allocator); 1430 if (self.grid) |*grid| { 1431 grid.deinit(); 1432 self.grid = null; 1433 } 1434 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1435 self.text_boxes.deinit(allocator); 1436 self.arena.deinit(); 1437 } 1438 1439 // one flow item: its display text plus an optional link. a non-empty `link` 1440 // sets the item's focus kind such as `.{ .custom = "a:/user/foo" }`, which 1441 // the web renderer turns into an anchor. 1442 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 1443 1444 // both the item text and the link are copied into the arena, so the caller's 1445 // slices needn't outlive this call. 1446 pub fn setItems(self: *FlowBox, allocator: std.mem.Allocator, items: []const Item) !void { 1447 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1448 self.text_boxes.clearAndFree(allocator); 1449 1450 // the old text boxes (and their borrowed content/links) are gone now, so 1451 // the arena that backed those strings can be reclaimed. 1452 _ = self.arena.reset(.retain_capacity); 1453 const aa = self.arena.allocator(); 1454 1455 self.focus.clear(); 1456 self.focus.child_id = null; 1457 1458 for (items) |item| { 1459 const line = try aa.dupe(u8, item.text); 1460 1461 var text_box = try wgt.TextBox(Widget).init(allocator, line, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 1462 errdefer text_box.deinit(allocator); 1463 text_box.getFocus().focusable = true; 1464 1465 if (item.link.len > 0) { 1466 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 1467 } 1468 1469 try self.text_boxes.append(allocator, text_box); 1470 } 1471 1472 if (self.text_boxes.items.len > 0) { 1473 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 1474 } 1475 } 1476 1477 pub fn build(self: *FlowBox, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1478 self.clearGrid(); 1479 self.focus.clear(); 1480 1481 const cell_width = self.options.cell_width; 1482 const max_width = constraint.max_size.width orelse cell_width; 1483 const cols = if (cell_width == 0) 1 else @max(1, max_width / cell_width); 1484 self.last_cols = cols; 1485 1486 const count = self.text_boxes.items.len; 1487 if (count == 0) return; 1488 const slot_height = self.options.cell_height + border_rows; 1489 if (slot_height == 0) return; 1490 1491 // build at the slot size so every text box fits a single grid cell 1492 for (self.text_boxes.items) |*tb| { 1493 tb.options.border_style = if (self.focus.child_id == tb.getFocus().id) .single else .hidden; 1494 try tb.build(allocator, .{ 1495 .min_size = .{ .width = cell_width, .height = null }, 1496 .max_size = .{ .width = cell_width, .height = slot_height }, 1497 }, root_focus); 1498 } 1499 1500 const rows = (count + cols - 1) / cols; 1501 const content_height = rows * slot_height; 1502 const total_width = cols * cell_width; 1503 if (total_width == 0 or content_height == 0) return; 1504 1505 var grid = try Grid.init(allocator, .{ .width = total_width, .height = content_height }); 1506 errdefer grid.deinit(); 1507 1508 for (self.text_boxes.items, 0..) |*tb, i| { 1509 const tb_grid = tb.getGrid() orelse continue; 1510 const col = i % cols; 1511 const row = i / cols; 1512 const cell_x = col * cell_width; 1513 const cell_y = row * slot_height; 1514 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, cell_x, cell_y); 1515 try grid.drawGrid(tb_grid, cell_x, cell_y); 1516 } 1517 1518 self.grid = grid; 1519 } 1520 1521 pub fn input(self: *FlowBox, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1522 _ = self; 1523 _ = allocator; 1524 _ = key; 1525 _ = root_focus; 1526 } 1527 1528 pub fn clearGrid(self: *FlowBox) void { 1529 if (self.grid) |*grid| { 1530 grid.deinit(); 1531 self.grid = null; 1532 } 1533 for (self.text_boxes.items) |*tb| tb.clearGrid(); 1534 } 1535 1536 pub fn getGrid(self: FlowBox) ?Grid { 1537 return self.grid; 1538 } 1539 1540 pub fn getFocus(self: *FlowBox) *Focus { 1541 return self.focus; 1542 } 1543 1544 pub fn cellRect(self: FlowBox, index: usize) ?layout.IRect { 1545 if (self.last_cols == 0 or index >= self.text_boxes.items.len) return null; 1546 const slot_height = self.options.cell_height + border_rows; 1547 const col = index % self.last_cols; 1548 const row = index / self.last_cols; 1549 return .{ 1550 .x = @intCast(col * self.options.cell_width), 1551 .y = @intCast(row * slot_height), 1552 .size = .{ .width = self.options.cell_width, .height = slot_height }, 1553 }; 1554 } 1555 1556 pub fn indexOfFocusId(self: FlowBox, focus_id: usize) ?usize { 1557 for (self.text_boxes.items, 0..) |tb, i| { 1558 if (tb.box.focus.id == focus_id) return i; 1559 } 1560 return null; 1561 } 1562 1563 pub const Scroll = struct { 1564 scroll: wgt.Scroll(Widget), 1565 1566 pub fn init(allocator: std.mem.Allocator, options: FlowBox.Options, web_native: bool) !Scroll { 1567 var layout_inner = try FlowBox.init(allocator, options); 1568 errdefer layout_inner.deinit(allocator); 1569 var scroll = try wgt.Scroll(Widget).init(allocator, .{ .flow_box = layout_inner }, .{ .direction = .vert, .web_native = web_native }); 1570 errdefer scroll.deinit(allocator); 1571 return .{ .scroll = scroll }; 1572 } 1573 1574 pub fn deinit(self: *Scroll, allocator: std.mem.Allocator) void { 1575 self.scroll.deinit(allocator); 1576 } 1577 1578 pub fn setItems(self: *Scroll, allocator: std.mem.Allocator, items: []const Item) !void { 1579 self.scroll.x = 0; 1580 self.scroll.y = 0; 1581 try self.inner().setItems(allocator, items); 1582 } 1583 1584 pub fn build(self: *Scroll, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1585 try self.scroll.build(allocator, constraint, root_focus); 1586 } 1587 1588 pub fn input(self: *Scroll, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1589 _ = allocator; 1590 const in = self.inner(); 1591 const child_id = in.focus.child_id orelse return; 1592 const current_index = in.indexOfFocusId(child_id) orelse return; 1593 const count = in.text_boxes.items.len; 1594 if (count == 0) return; 1595 const cols = in.last_cols; 1596 const slot_height = in.options.cell_height + FlowBox.border_rows; 1597 1598 var index = current_index; 1599 switch (key) { 1600 .arrow_up => index -|= cols, 1601 .arrow_down => if (index + cols < count) { 1602 index += cols; 1603 }, 1604 .arrow_left => index -|= 1, 1605 .arrow_right => if (index + 1 < count) { 1606 index += 1; 1607 }, 1608 .home => index = 0, 1609 .end => index = count - 1, 1610 .page_up => { 1611 if (self.scroll.grid) |grid| if (slot_height > 0) { 1612 const rows_per_page = grid.size.height / slot_height; 1613 index -|= rows_per_page * cols; 1614 }; 1615 }, 1616 .page_down => { 1617 if (self.scroll.grid) |grid| if (slot_height > 0) { 1618 const rows_per_page = grid.size.height / slot_height; 1619 index = @min(index + rows_per_page * cols, count - 1); 1620 }; 1621 }, 1622 // scroll wheel moves the focused cell by a full row so the 1623 // viewport (via scrollToRect) follows in row-sized steps, 1624 // matching how a scroll wheel feels in a grid view. 1625 .mouse => |mouse| switch (mouse.action) { 1626 .scroll => |dir| switch (dir) { 1627 .up => index -|= cols, 1628 .down => if (index + cols < count) { 1629 index += cols; 1630 }, 1631 }, 1632 else => {}, 1633 }, 1634 else => {}, 1635 } 1636 1637 if (index != current_index) { 1638 root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 1639 if (in.cellRect(index)) |rect| { 1640 self.scroll.scrollToRect(rect); 1641 } 1642 } 1643 } 1644 1645 pub fn clearGrid(self: *Scroll) void { 1646 self.scroll.clearGrid(); 1647 } 1648 1649 pub fn getGrid(self: Scroll) ?Grid { 1650 return self.scroll.getGrid(); 1651 } 1652 1653 pub fn getFocus(self: *Scroll) *Focus { 1654 return self.scroll.getFocus(); 1655 } 1656 1657 pub fn getSelectedIndex(self: Scroll) ?usize { 1658 const in = self.scroll.child.flow_box; 1659 const child_id = in.focus.child_id orelse return null; 1660 return in.indexOfFocusId(child_id); 1661 } 1662 1663 fn inner(self: *Scroll) *FlowBox { 1664 return &self.scroll.child.flow_box; 1665 } 1666 }; 1667 }; 1668 1669 // a left-to-right flow of variable-width focusable items that wraps at the 1670 // available width, like words in a paragraph. unlike FlowBox there is no cell 1671 // grid: each item is as wide as its text. selection movement is driven by the 1672 // owning view via indexOfFocusId/verticalNeighbor. 1673 pub const TagFlow = struct { 1674 focus: *Focus, 1675 grid: ?Grid, 1676 text_boxes: std.ArrayList(wgt.TextBox(Widget)), 1677 // last build's item rects (content space), for vertical navigation and 1678 // scroll-into-view. 1679 rects: std.ArrayList(layout.IRect), 1680 // backs the per-item strings the text boxes borrow. 1681 arena: std.heap.ArenaAllocator, 1682 1683 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 1684 1685 pub fn init(allocator: std.mem.Allocator) !TagFlow { 1686 return .{ 1687 .focus = try Focus.create(allocator, .container), 1688 .grid = null, 1689 .text_boxes = .empty, 1690 .rects = .empty, 1691 .arena = std.heap.ArenaAllocator.init(allocator), 1692 }; 1693 } 1694 1695 pub fn deinit(self: *TagFlow, allocator: std.mem.Allocator) void { 1696 self.focus.destroy(allocator); 1697 if (self.grid) |*grid| { 1698 grid.deinit(); 1699 self.grid = null; 1700 } 1701 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1702 self.text_boxes.deinit(allocator); 1703 self.rects.deinit(allocator); 1704 self.arena.deinit(); 1705 } 1706 1707 // both the item text and the link are copied into the arena, so the caller's 1708 // slices needn't outlive this call. 1709 pub fn setItems(self: *TagFlow, allocator: std.mem.Allocator, items: []const Item) !void { 1710 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1711 self.text_boxes.clearAndFree(allocator); 1712 1713 _ = self.arena.reset(.retain_capacity); 1714 const aa = self.arena.allocator(); 1715 1716 self.focus.clear(); 1717 self.focus.child_id = null; 1718 1719 for (items) |item| { 1720 const line = try aa.dupe(u8, item.text); 1721 1722 var text_box = try wgt.TextBox(Widget).init(allocator, line, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1723 errdefer text_box.deinit(allocator); 1724 text_box.getFocus().focusable = true; 1725 1726 if (item.link.len > 0) { 1727 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 1728 } 1729 1730 try self.text_boxes.append(allocator, text_box); 1731 } 1732 1733 if (self.text_boxes.items.len > 0) { 1734 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 1735 } 1736 } 1737 1738 pub fn build(self: *TagFlow, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1739 self.clearGrid(); 1740 self.focus.clear(); 1741 1742 if (self.text_boxes.items.len == 0) return; 1743 const max_width = constraint.max_size.width orelse 80; 1744 if (max_width == 0) return; 1745 1746 // lay the items out left to right, wrapping to a new row when the next 1747 // one wouldn't fit. 1748 self.rects.clearRetainingCapacity(); 1749 var x: usize = 0; 1750 var y: usize = 0; 1751 var row_height: usize = 0; 1752 var total_width: usize = 0; 1753 for (self.text_boxes.items) |*tb| { 1754 try tb.build(allocator, .{ 1755 .min_size = .{ .width = null, .height = null }, 1756 .max_size = .{ .width = max_width, .height = null }, 1757 }, root_focus); 1758 const tb_grid_maybe = tb.getGrid(); 1759 const w: usize = if (tb_grid_maybe) |g| g.size.width else 0; 1760 const h: usize = if (tb_grid_maybe) |g| g.size.height else 0; 1761 if (x > 0 and x + w > max_width) { 1762 x = 0; 1763 y += row_height; 1764 row_height = 0; 1765 } 1766 try self.rects.append(allocator, .{ .x = @intCast(x), .y = @intCast(y), .size = .{ .width = w, .height = h } }); 1767 if (h > row_height) row_height = h; 1768 x += w; 1769 if (x > total_width) total_width = x; 1770 } 1771 1772 const total_height = y + row_height; 1773 if (total_width == 0 or total_height == 0) return; 1774 1775 var grid = try Grid.init(allocator, .{ .width = total_width, .height = total_height }); 1776 errdefer grid.deinit(); 1777 1778 for (self.text_boxes.items, self.rects.items) |*tb, rect| { 1779 const tb_grid = tb.getGrid() orelse continue; 1780 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, @intCast(rect.x), @intCast(rect.y)); 1781 try grid.drawGrid(tb_grid, @intCast(rect.x), @intCast(rect.y)); 1782 } 1783 1784 self.grid = grid; 1785 } 1786 1787 pub fn input(self: *TagFlow, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1788 _ = self; 1789 _ = allocator; 1790 _ = key; 1791 _ = root_focus; 1792 } 1793 1794 pub fn clearGrid(self: *TagFlow) void { 1795 if (self.grid) |*grid| { 1796 grid.deinit(); 1797 self.grid = null; 1798 } 1799 for (self.text_boxes.items) |*tb| tb.clearGrid(); 1800 } 1801 1802 pub fn getGrid(self: TagFlow) ?Grid { 1803 return self.grid; 1804 } 1805 1806 pub fn getFocus(self: *TagFlow) *Focus { 1807 return self.focus; 1808 } 1809 1810 pub fn indexOfFocusId(self: TagFlow, focus_id: usize) ?usize { 1811 for (self.text_boxes.items, 0..) |tb, i| { 1812 if (tb.box.focus.id == focus_id) return i; 1813 } 1814 return null; 1815 } 1816 1817 // the first item of the row below (when `downward`) or above, or null at 1818 // the flow's edge. items are laid out in order, so y is nondecreasing. 1819 pub fn rowStep(self: TagFlow, index: usize, downward: bool) ?usize { 1820 const rects = self.rects.items; 1821 if (index >= rects.len) return null; 1822 const cur_y = rects[index].y; 1823 if (downward) { 1824 for (rects[index + 1 ..], index + 1..) |r, i| { 1825 if (r.y > cur_y) return i; 1826 } 1827 return null; 1828 } 1829 // walk the earlier rows; the last one visited is the row above. 1830 var prev_first: ?usize = null; 1831 var i: usize = 0; 1832 while (i < rects.len and rects[i].y < cur_y) { 1833 prev_first = i; 1834 const y = rects[i].y; 1835 while (i < rects.len and rects[i].y == y) i += 1; 1836 } 1837 return prev_first; 1838 } 1839 }; 1840 1841 // an invisible widget that fills the horizontal space granted by its parent. 1842 // used inside Box(horiz) with a min_size so the box reserves space for the 1843 // children that follow, pushing them to the right. 1844 pub const Spacer = struct { 1845 focus: *Focus, 1846 grid: ?Grid, 1847 1848 pub fn init(allocator: std.mem.Allocator) !Spacer { 1849 return .{ 1850 .focus = try Focus.create(allocator, .container), 1851 .grid = null, 1852 }; 1853 } 1854 1855 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 1856 self.focus.destroy(allocator); 1857 if (self.grid) |*grid| { 1858 grid.deinit(); 1859 self.grid = null; 1860 } 1861 } 1862 1863 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1864 _ = root_focus; 1865 self.clearGrid(); 1866 const width = constraint.max_size.width orelse return; 1867 if (width == 0) return; 1868 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1869 } 1870 1871 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1872 _ = self; 1873 _ = allocator; 1874 _ = key; 1875 _ = root_focus; 1876 } 1877 1878 pub fn clearGrid(self: *Spacer) void { 1879 if (self.grid) |*grid| { 1880 grid.deinit(); 1881 self.grid = null; 1882 } 1883 } 1884 1885 pub fn getGrid(self: Spacer) ?Grid { 1886 return self.grid; 1887 } 1888 1889 pub fn getFocus(self: *Spacer) *Focus { 1890 return self.focus; 1891 } 1892 }; 1893 1894 // a one-row, non-focusable status bar showing the current page's url 1895 pub const Footer = struct { 1896 focus: *Focus, 1897 grid: ?Grid, 1898 session: *Session, 1899 arena: std.heap.ArenaAllocator, 1900 1901 pub fn init(allocator: std.mem.Allocator, session: *Session) !Footer { 1902 return .{ 1903 .focus = try Focus.create(allocator, .container), 1904 .grid = null, 1905 .session = session, 1906 .arena = std.heap.ArenaAllocator.init(allocator), 1907 }; 1908 } 1909 1910 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 1911 self.focus.destroy(allocator); 1912 if (self.grid) |*grid| grid.deinit(); 1913 self.arena.deinit(); 1914 } 1915 1916 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1917 _ = root_focus; 1918 self.clearGrid(); 1919 const width = constraint.max_size.width orelse return; 1920 if (width == 0) return; 1921 1922 _ = self.arena.reset(.retain_capacity); 1923 const aa = self.arena.allocator(); 1924 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 1925 const text = if (self.session.web_port) |port| 1926 std.fmt.allocPrint(aa, "http://localhost:{d}{s}", .{ port, path }) catch return 1927 else 1928 path; 1929 1930 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1931 errdefer grid.deinit(); 1932 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 1933 var i: usize = 0; 1934 while (utf8.nextCodepointSlice()) |ch| { 1935 if (i == width) break; 1936 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 1937 i += 1; 1938 } 1939 self.grid = grid; 1940 } 1941 1942 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1943 _ = self; 1944 _ = allocator; 1945 _ = key; 1946 _ = root_focus; 1947 } 1948 1949 pub fn clearGrid(self: *Footer) void { 1950 if (self.grid) |*grid| { 1951 grid.deinit(); 1952 self.grid = null; 1953 } 1954 } 1955 1956 pub fn getGrid(self: Footer) ?Grid { 1957 return self.grid; 1958 } 1959 1960 pub fn getFocus(self: *Footer) *Focus { 1961 return self.focus; 1962 } 1963 }; 1964 1965 // a single-child wrapper that builds the child at its natural size and 1966 // positions its grid in the middle of the area granted by the parent 1967 pub const Center = struct { 1968 focus: *Focus, 1969 grid: ?Grid, 1970 child: *Widget, 1971 1972 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 1973 const child = try allocator.create(Widget); 1974 errdefer allocator.destroy(child); 1975 child.* = child_widget; 1976 return .{ 1977 .focus = try Focus.create(allocator, .container), 1978 .grid = null, 1979 .child = child, 1980 }; 1981 } 1982 1983 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1984 self.focus.destroy(allocator); 1985 if (self.grid) |*grid| { 1986 grid.deinit(); 1987 self.grid = null; 1988 } 1989 self.child.deinit(allocator); 1990 allocator.destroy(self.child); 1991 } 1992 1993 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1994 self.clearGrid(); 1995 self.getFocus().clear(); 1996 1997 // build the child without forcing it to fill min_size; it sizes 1998 // itself to its content within the available max. 1999 try self.child.build(allocator, .{ 2000 .min_size = .{ .width = null, .height = null }, 2001 .max_size = constraint.max_size, 2002 }, root_focus); 2003 2004 const child_grid = self.child.getGrid() orelse return; 2005 2006 // prefer max when given; otherwise grow to max(min, child) so that 2007 // when only a min is set (e.g. the wasm path passes viewport rows 2008 // as min_height with no max), we can still vertically center while 2009 // letting taller content extend past the min. 2010 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; 2011 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; 2012 if (width == 0 or height == 0) return; 2013 2014 const offset_x: usize = (width -| child_grid.size.width) / 2; 2015 const offset_y: usize = (height -| child_grid.size.height) / 2; 2016 2017 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 2018 errdefer grid.deinit(); 2019 try grid.drawGrid(child_grid, offset_x, offset_y); 2020 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 2021 self.getFocus().child_id = self.child.getFocus().id; 2022 2023 self.grid = grid; 2024 } 2025 2026 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2027 try self.child.input(allocator, key, root_focus); 2028 } 2029 2030 pub fn clearGrid(self: *Center) void { 2031 if (self.grid) |*grid| { 2032 grid.deinit(); 2033 self.grid = null; 2034 } 2035 self.child.clearGrid(); 2036 } 2037 2038 pub fn getGrid(self: Center) ?Grid { 2039 return self.grid; 2040 } 2041 2042 pub fn getFocus(self: *Center) *Focus { 2043 return self.focus; 2044 } 2045 }; 2046 2047 // renders truecolor ANSI art 2048 pub const AnsiArt = struct { 2049 focus: *Focus, 2050 grid: ?Grid, 2051 content: []const u8, 2052 2053 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 2054 return .{ 2055 .focus = try Focus.create(allocator, .container), 2056 .grid = null, 2057 .content = content, 2058 }; 2059 } 2060 2061 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 2062 self.focus.destroy(allocator); 2063 if (self.grid) |*grid| { 2064 grid.deinit(); 2065 self.grid = null; 2066 } 2067 } 2068 2069 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2070 _ = root_focus; 2071 self.clearGrid(); 2072 2073 // parse into rows of cells, then copy into a rectangular grid 2074 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 2075 defer { 2076 for (rows.items) |*row| row.deinit(allocator); 2077 rows.deinit(allocator); 2078 } 2079 var row: std.ArrayList(Grid.Cell) = .empty; 2080 errdefer row.deinit(allocator); 2081 2082 var style: Grid.Style = .{}; 2083 var width: usize = 0; 2084 const content = self.content; 2085 var i: usize = 0; 2086 while (i < content.len) { 2087 const byte = content[i]; 2088 if (byte == '\n') { 2089 width = @max(width, row.items.len); 2090 try rows.append(allocator, row); 2091 row = .empty; 2092 i += 1; 2093 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 2094 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 2095 var j = i + 2; 2096 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 2097 if (j >= content.len) { 2098 i = content.len; // malformed trailing escape; stop 2099 } else { 2100 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 2101 i = j + 1; 2102 } 2103 } else { 2104 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 2105 const end = @min(content.len, i + len); 2106 const rune = content[i..end]; 2107 const transparent = end - i == 1 and rune[0] == ' ' and 2108 style.fg == null and style.bg == null and !style.inverted; 2109 try row.append(allocator, .{ 2110 .rune = if (transparent) null else rune, 2111 .style = style, 2112 }); 2113 i = end; 2114 } 2115 } 2116 // a trailing row with content but no closing newline 2117 if (row.items.len > 0) { 2118 width = @max(width, row.items.len); 2119 try rows.append(allocator, row); 2120 } else { 2121 row.deinit(allocator); 2122 } 2123 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 2124 2125 const height = rows.items.len; 2126 if (width == 0 or height == 0) return; 2127 2128 const clamped_w = @min(width, constraint.max_size.width orelse width); 2129 const clamped_h = @min(height, constraint.max_size.height orelse height); 2130 2131 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 2132 errdefer grid.deinit(); 2133 for (rows.items[0..clamped_h], 0..) |r, y| { 2134 const n = @min(r.items.len, clamped_w); 2135 for (r.items[0..n], 0..) |cell, x| { 2136 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 2137 } 2138 } 2139 self.grid = grid; 2140 } 2141 2142 pub fn clearGrid(self: *AnsiArt) void { 2143 if (self.grid) |*grid| { 2144 grid.deinit(); 2145 self.grid = null; 2146 } 2147 } 2148 2149 pub fn getGrid(self: AnsiArt) ?Grid { 2150 return self.grid; 2151 } 2152 2153 pub fn getFocus(self: *AnsiArt) *Focus { 2154 return self.focus; 2155 } 2156 2157 fn applySgr(style: *Grid.Style, params: []const u8) void { 2158 var nums: [16]u32 = undefined; 2159 var n: usize = 0; 2160 var it = std.mem.splitScalar(u8, params, ';'); 2161 while (it.next()) |tok| { 2162 if (n >= nums.len) break; 2163 // an empty parameter (e.g. bare "\x1b[m") means 0 2164 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 2165 n += 1; 2166 } 2167 if (n == 0) { 2168 style.* = .{}; 2169 return; 2170 } 2171 var i: usize = 0; 2172 while (i < n) : (i += 1) { 2173 switch (nums[i]) { 2174 0 => style.* = .{}, 2175 7 => style.inverted = true, 2176 27 => style.inverted = false, 2177 39 => style.fg = null, 2178 49 => style.bg = null, 2179 38, 48 => { 2180 // truecolor form: 38;2;r;g;b — anything else is ignored 2181 if (i + 4 < n and nums[i + 1] == 2) { 2182 const c = Grid.Color{ 2183 .r = @truncate(nums[i + 2]), 2184 .g = @truncate(nums[i + 3]), 2185 .b = @truncate(nums[i + 4]), 2186 }; 2187 if (nums[i] == 38) style.fg = c else style.bg = c; 2188 i += 4; 2189 } 2190 }, 2191 else => {}, 2192 } 2193 } 2194 } 2195 }; 2196 2197 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 2198 pub const AnsiBackground = struct { 2199 grid: ?Grid, 2200 child: *Widget, 2201 art: AnsiArt, 2202 session: *Session, 2203 2204 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 2205 var cw = child_widget; 2206 const child = allocator.create(Widget) catch |e| { 2207 cw.deinit(allocator); 2208 return e; 2209 }; 2210 child.* = cw; 2211 errdefer { 2212 child.deinit(allocator); 2213 allocator.destroy(child); 2214 } 2215 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2216 } 2217 2218 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2219 if (self.grid) |*grid| grid.deinit(); 2220 self.art.deinit(allocator); 2221 self.child.deinit(allocator); 2222 allocator.destroy(self.child); 2223 } 2224 2225 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2226 self.clearGrid(); 2227 2228 // make the wrapped page fill the whole available area 2229 try self.child.build(allocator, .{ 2230 .min_size = .{ 2231 .width = constraint.max_size.width orelse constraint.min_size.width, 2232 .height = constraint.max_size.height orelse constraint.min_size.height, 2233 }, 2234 .max_size = constraint.max_size, 2235 }, root_focus); 2236 2237 if (self.session.data.enable_ansi) { 2238 if (self.child.getGrid()) |fg| { 2239 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 2240 } 2241 } 2242 } 2243 2244 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2245 try self.child.input(allocator, key, root_focus); 2246 } 2247 2248 pub fn clearGrid(self: *AnsiBackground) void { 2249 if (self.grid) |*grid| { 2250 grid.deinit(); 2251 self.grid = null; 2252 } 2253 self.child.clearGrid(); 2254 self.art.clearGrid(); 2255 } 2256 2257 pub fn getGrid(self: AnsiBackground) ?Grid { 2258 return self.grid orelse self.child.getGrid(); 2259 } 2260 2261 pub fn getFocus(self: *AnsiBackground) *Focus { 2262 return self.child.getFocus(); 2263 } 2264 2265 // which top corner the art hugs. 2266 const ArtAnchor = enum { top_left, top_right }; 2267 2268 // a foreground cell counts as blank if it's empty or a bare space without styling 2269 fn cellIsBlank(cell: Grid.Cell) bool { 2270 if (cell.rune) |rune| { 2271 return std.mem.eql(u8, rune, " ") and 2272 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2273 } 2274 return true; 2275 } 2276 2277 // the art is dimmed to this fraction of its brightness so foreground text 2278 // stays legible over it 2279 const art_brightness = 45; // percent 2280 2281 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2282 const c = color orelse return null; 2283 return .{ 2284 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2285 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2286 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2287 }; 2288 } 2289 2290 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2291 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2292 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2293 // snap such channels clear of that range so the backdrop doesn't blink or 2294 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2295 // actually render truecolor. 2296 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2297 const c = color orelse return null; 2298 const snap = struct { 2299 fn f(v: u8) u8 { 2300 return if (v >= 1 and v <= 9) 10 else v; 2301 } 2302 }.f; 2303 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2304 } 2305 2306 // composites ANSI art behind a foreground grid 2307 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 2308 art.clearGrid(); 2309 try art.build(allocator, .{ 2310 .min_size = .{ .width = null, .height = null }, 2311 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2312 }, root_focus); 2313 2314 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2315 errdefer out.deinit(); 2316 2317 if (art.getGrid()) |art_grid| { 2318 const anchor_x = switch (anchor) { 2319 .top_left => 0, 2320 .top_right => foreground.size.width -| art_grid.size.width, 2321 }; 2322 for (0..art_grid.size.height) |y| { 2323 for (0..art_grid.size.width) |x| { 2324 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2325 if (src.rune == null) continue; 2326 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2327 const dst = &out.cells.items[idx]; 2328 // composite the dimmed art behind every cell so it shows 2329 // through the whole UI: a blank cell becomes the art, while a 2330 // cell with a glyph keeps the glyph but takes the dimmed art as 2331 // its background (replacing whatever background it had, which 2332 // would otherwise obscure the art). 2333 if (cellIsBlank(dst.*)) { 2334 dst.* = src; 2335 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2336 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2337 } else { 2338 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2339 } 2340 } 2341 } 2342 } 2343 return out; 2344 } 2345 }; 2346