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