╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1123 grid.deinit(); 1124 self.grid = null; 1125 } 1126 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1127 self.text_boxes.deinit(allocator); 1128 self.arena.deinit(); 1129 } 1130 1131 // one flow item: its display text plus an optional link. a non-empty `link` 1132 // sets the item's focus kind such as `.{ .custom = "a:/user/foo" }`, which 1133 // the web renderer turns into an anchor. 1134 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 1135 1136 // both the item text and the link are copied into the arena, so the caller's 1137 // slices needn't outlive this call. 1138 pub fn setItems(self: *FlowBox, allocator: std.mem.Allocator, items: []const Item) !void { 1139 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1140 self.text_boxes.clearAndFree(allocator); 1141 1142 // the old text boxes (and their borrowed content/links) are gone now, so 1143 // the arena that backed those strings can be reclaimed. 1144 _ = self.arena.reset(.retain_capacity); 1145 const aa = self.arena.allocator(); 1146 1147 self.focus.clear(); 1148 self.focus.child_id = null; 1149 1150 for (items) |item| { 1151 const line = try aa.dupe(u8, item.text); 1152 1153 var text_box = try wgt.TextBox(Widget).init(allocator, line, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 1154 errdefer text_box.deinit(allocator); 1155 text_box.getFocus().focusable = true; 1156 1157 if (item.link.len > 0) { 1158 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 1159 } 1160 1161 try self.text_boxes.append(allocator, text_box); 1162 } 1163 1164 if (self.text_boxes.items.len > 0) { 1165 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 1166 } 1167 } 1168 1169 pub fn build(self: *FlowBox, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1170 self.clearGrid(); 1171 self.focus.clear(); 1172 1173 const cell_width = self.options.cell_width; 1174 const max_width = constraint.max_size.width orelse cell_width; 1175 const cols = if (cell_width == 0) 1 else @max(1, max_width / cell_width); 1176 self.last_cols = cols; 1177 1178 const count = self.text_boxes.items.len; 1179 if (count == 0) return; 1180 const slot_height = self.options.cell_height + border_rows; 1181 if (slot_height == 0) return; 1182 1183 // build at the slot size so every text box fits a single grid cell 1184 for (self.text_boxes.items) |*tb| { 1185 tb.options.border_style = if (self.focus.child_id == tb.getFocus().id) .single else .hidden; 1186 try tb.build(allocator, .{ 1187 .min_size = .{ .width = cell_width, .height = null }, 1188 .max_size = .{ .width = cell_width, .height = slot_height }, 1189 }, root_focus); 1190 } 1191 1192 const rows = (count + cols - 1) / cols; 1193 const content_height = rows * slot_height; 1194 const total_width = cols * cell_width; 1195 if (total_width == 0 or content_height == 0) return; 1196 1197 var grid = try Grid.init(allocator, .{ .width = total_width, .height = content_height }); 1198 errdefer grid.deinit(); 1199 1200 for (self.text_boxes.items, 0..) |*tb, i| { 1201 const tb_grid = tb.getGrid() orelse continue; 1202 const col = i % cols; 1203 const row = i / cols; 1204 const cell_x = col * cell_width; 1205 const cell_y = row * slot_height; 1206 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, cell_x, cell_y); 1207 try grid.drawGrid(tb_grid, cell_x, cell_y); 1208 } 1209 1210 self.grid = grid; 1211 } 1212 1213 pub fn input(self: *FlowBox, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1214 _ = self; 1215 _ = allocator; 1216 _ = key; 1217 _ = root_focus; 1218 } 1219 1220 pub fn clearGrid(self: *FlowBox) void { 1221 if (self.grid) |*grid| { 1222 grid.deinit(); 1223 self.grid = null; 1224 } 1225 for (self.text_boxes.items) |*tb| tb.clearGrid(); 1226 } 1227 1228 pub fn getGrid(self: FlowBox) ?Grid { 1229 return self.grid; 1230 } 1231 1232 pub fn getFocus(self: *FlowBox) *Focus { 1233 return self.focus; 1234 } 1235 1236 pub fn cellRect(self: FlowBox, index: usize) ?layout.IRect { 1237 if (self.last_cols == 0 or index >= self.text_boxes.items.len) return null; 1238 const slot_height = self.options.cell_height + border_rows; 1239 const col = index % self.last_cols; 1240 const row = index / self.last_cols; 1241 return .{ 1242 .x = @intCast(col * self.options.cell_width), 1243 .y = @intCast(row * slot_height), 1244 .size = .{ .width = self.options.cell_width, .height = slot_height }, 1245 }; 1246 } 1247 1248 pub fn indexOfFocusId(self: FlowBox, focus_id: usize) ?usize { 1249 for (self.text_boxes.items, 0..) |tb, i| { 1250 if (tb.box.focus.id == focus_id) return i; 1251 } 1252 return null; 1253 } 1254 1255 pub const Scroll = struct { 1256 scroll: wgt.Scroll(Widget), 1257 1258 pub fn init(allocator: std.mem.Allocator, options: FlowBox.Options, web_native: bool) !Scroll { 1259 var layout_inner = try FlowBox.init(allocator, options); 1260 errdefer layout_inner.deinit(allocator); 1261 var scroll = try wgt.Scroll(Widget).init(allocator, .{ .flow_box = layout_inner }, .{ .direction = .vert, .web_native = web_native }); 1262 errdefer scroll.deinit(allocator); 1263 return .{ .scroll = scroll }; 1264 } 1265 1266 pub fn deinit(self: *Scroll, allocator: std.mem.Allocator) void { 1267 self.scroll.deinit(allocator); 1268 } 1269 1270 pub fn setItems(self: *Scroll, allocator: std.mem.Allocator, items: []const Item) !void { 1271 self.scroll.x = 0; 1272 self.scroll.y = 0; 1273 try self.inner().setItems(allocator, items); 1274 } 1275 1276 pub fn build(self: *Scroll, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1277 try self.scroll.build(allocator, constraint, root_focus); 1278 } 1279 1280 pub fn input(self: *Scroll, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1281 _ = allocator; 1282 const in = self.inner(); 1283 const child_id = in.focus.child_id orelse return; 1284 const current_index = in.indexOfFocusId(child_id) orelse return; 1285 const count = in.text_boxes.items.len; 1286 if (count == 0) return; 1287 const cols = in.last_cols; 1288 const slot_height = in.options.cell_height + FlowBox.border_rows; 1289 1290 var index = current_index; 1291 switch (key) { 1292 .arrow_up => index -|= cols, 1293 .arrow_down => if (index + cols < count) { 1294 index += cols; 1295 }, 1296 .arrow_left => index -|= 1, 1297 .arrow_right => if (index + 1 < count) { 1298 index += 1; 1299 }, 1300 .home => index = 0, 1301 .end => index = count - 1, 1302 .page_up => { 1303 if (self.scroll.grid) |grid| if (slot_height > 0) { 1304 const rows_per_page = grid.size.height / slot_height; 1305 index -|= rows_per_page * cols; 1306 }; 1307 }, 1308 .page_down => { 1309 if (self.scroll.grid) |grid| if (slot_height > 0) { 1310 const rows_per_page = grid.size.height / slot_height; 1311 index = @min(index + rows_per_page * cols, count - 1); 1312 }; 1313 }, 1314 // scroll wheel moves the focused cell by a full row so the 1315 // viewport (via scrollToRect) follows in row-sized steps, 1316 // matching how a scroll wheel feels in a grid view. 1317 .mouse => |mouse| switch (mouse.action) { 1318 .scroll => |dir| switch (dir) { 1319 .up => index -|= cols, 1320 .down => if (index + cols < count) { 1321 index += cols; 1322 }, 1323 }, 1324 else => {}, 1325 }, 1326 else => {}, 1327 } 1328 1329 if (index != current_index) { 1330 root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 1331 if (in.cellRect(index)) |rect| { 1332 self.scroll.scrollToRect(rect); 1333 } 1334 } 1335 } 1336 1337 pub fn clearGrid(self: *Scroll) void { 1338 self.scroll.clearGrid(); 1339 } 1340 1341 pub fn getGrid(self: Scroll) ?Grid { 1342 return self.scroll.getGrid(); 1343 } 1344 1345 pub fn getFocus(self: *Scroll) *Focus { 1346 return self.scroll.getFocus(); 1347 } 1348 1349 pub fn getSelectedIndex(self: Scroll) ?usize { 1350 const in = self.scroll.child.flow_box; 1351 const child_id = in.focus.child_id orelse return null; 1352 return in.indexOfFocusId(child_id); 1353 } 1354 1355 fn inner(self: *Scroll) *FlowBox { 1356 return &self.scroll.child.flow_box; 1357 } 1358 }; 1359 }; 1360 1361 // an invisible widget that fills the horizontal space granted by its parent. 1362 // used inside Box(horiz) with a min_size so the box reserves space for the 1363 // children that follow, pushing them to the right. 1364 pub const Spacer = struct { 1365 focus: *Focus, 1366 grid: ?Grid, 1367 1368 pub fn init(allocator: std.mem.Allocator) !Spacer { 1369 return .{ 1370 .focus = try Focus.create(allocator, .container), 1371 .grid = null, 1372 }; 1373 } 1374 1375 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 1376 self.focus.destroy(allocator); 1377 if (self.grid) |*grid| { 1378 grid.deinit(); 1379 self.grid = null; 1380 } 1381 } 1382 1383 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1384 _ = root_focus; 1385 self.clearGrid(); 1386 const width = constraint.max_size.width orelse return; 1387 if (width == 0) return; 1388 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1389 } 1390 1391 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1392 _ = self; 1393 _ = allocator; 1394 _ = key; 1395 _ = root_focus; 1396 } 1397 1398 pub fn clearGrid(self: *Spacer) void { 1399 if (self.grid) |*grid| { 1400 grid.deinit(); 1401 self.grid = null; 1402 } 1403 } 1404 1405 pub fn getGrid(self: Spacer) ?Grid { 1406 return self.grid; 1407 } 1408 1409 pub fn getFocus(self: *Spacer) *Focus { 1410 return self.focus; 1411 } 1412 }; 1413 1414 // a one-row, non-focusable status bar showing the current page's url 1415 pub const Footer = struct { 1416 focus: *Focus, 1417 grid: ?Grid, 1418 session: *Session, 1419 arena: std.heap.ArenaAllocator, 1420 1421 pub fn init(allocator: std.mem.Allocator, session: *Session) !Footer { 1422 return .{ 1423 .focus = try Focus.create(allocator, .container), 1424 .grid = null, 1425 .session = session, 1426 .arena = std.heap.ArenaAllocator.init(allocator), 1427 }; 1428 } 1429 1430 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 1431 self.focus.destroy(allocator); 1432 if (self.grid) |*grid| grid.deinit(); 1433 self.arena.deinit(); 1434 } 1435 1436 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1437 _ = root_focus; 1438 self.clearGrid(); 1439 const width = constraint.max_size.width orelse return; 1440 if (width == 0) return; 1441 1442 _ = self.arena.reset(.retain_capacity); 1443 const aa = self.arena.allocator(); 1444 const path = self.session.data.current_page.urlAlloc(&self.arena) catch return; 1445 const text = if (self.session.web_port) |port| 1446 std.fmt.allocPrint(aa, "http://localhost:{d}{s}", .{ port, path }) catch return 1447 else 1448 path; 1449 1450 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1451 errdefer grid.deinit(); 1452 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 1453 var i: usize = 0; 1454 while (utf8.nextCodepointSlice()) |ch| { 1455 if (i == width) break; 1456 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 1457 i += 1; 1458 } 1459 self.grid = grid; 1460 } 1461 1462 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1463 _ = self; 1464 _ = allocator; 1465 _ = key; 1466 _ = root_focus; 1467 } 1468 1469 pub fn clearGrid(self: *Footer) void { 1470 if (self.grid) |*grid| { 1471 grid.deinit(); 1472 self.grid = null; 1473 } 1474 } 1475 1476 pub fn getGrid(self: Footer) ?Grid { 1477 return self.grid; 1478 } 1479 1480 pub fn getFocus(self: *Footer) *Focus { 1481 return self.focus; 1482 } 1483 }; 1484 1485 // a single-child wrapper that builds the child at its natural size and 1486 // positions its grid in the middle of the area granted by the parent 1487 pub const Center = struct { 1488 focus: *Focus, 1489 grid: ?Grid, 1490 child: *Widget, 1491 direction: Direction, 1492 1493 pub const Direction = enum { both, horiz, vert }; 1494 1495 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, direction: Direction) !Center { 1496 const child = try allocator.create(Widget); 1497 errdefer allocator.destroy(child); 1498 child.* = child_widget; 1499 return .{ 1500 .focus = try Focus.create(allocator, .container), 1501 .grid = null, 1502 .child = child, 1503 .direction = direction, 1504 }; 1505 } 1506 1507 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1508 self.focus.destroy(allocator); 1509 if (self.grid) |*grid| { 1510 grid.deinit(); 1511 self.grid = null; 1512 } 1513 self.child.deinit(allocator); 1514 allocator.destroy(self.child); 1515 } 1516 1517 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1518 self.clearGrid(); 1519 self.getFocus().clear(); 1520 1521 // build the child without forcing it to fill min_size; it sizes 1522 // itself to its content within the available max. 1523 try self.child.build(allocator, .{ 1524 .min_size = .{ .width = null, .height = null }, 1525 .max_size = constraint.max_size, 1526 }, root_focus); 1527 1528 const child_grid = self.child.getGrid() orelse return; 1529 1530 // prefer max when given; otherwise grow to max(min, child) so that 1531 // when only a min is set (e.g. the wasm path passes viewport rows 1532 // as min_height with no max), we can still vertically center while 1533 // letting taller content extend past the min. 1534 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; 1535 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; 1536 if (width == 0 or height == 0) return; 1537 1538 const offset_x: usize = if (self.direction == .horiz or self.direction == .both) 1539 (width -| child_grid.size.width) / 2 1540 else 1541 0; 1542 const offset_y: usize = if (self.direction == .vert or self.direction == .both) 1543 (height -| child_grid.size.height) / 2 1544 else 1545 0; 1546 1547 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1548 errdefer grid.deinit(); 1549 try grid.drawGrid(child_grid, offset_x, offset_y); 1550 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1551 self.getFocus().child_id = self.child.getFocus().id; 1552 1553 self.grid = grid; 1554 } 1555 1556 pub fn input(self: *Center, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1557 try self.child.input(allocator, key, root_focus); 1558 } 1559 1560 pub fn clearGrid(self: *Center) void { 1561 if (self.grid) |*grid| { 1562 grid.deinit(); 1563 self.grid = null; 1564 } 1565 self.child.clearGrid(); 1566 } 1567 1568 pub fn getGrid(self: Center) ?Grid { 1569 return self.grid; 1570 } 1571 1572 pub fn getFocus(self: *Center) *Focus { 1573 return self.focus; 1574 } 1575 }; 1576 1577 // renders truecolor ANSI art 1578 pub const AnsiArt = struct { 1579 focus: *Focus, 1580 grid: ?Grid, 1581 content: []const u8, 1582 1583 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1584 return .{ 1585 .focus = try Focus.create(allocator, .container), 1586 .grid = null, 1587 .content = content, 1588 }; 1589 } 1590 1591 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1592 self.focus.destroy(allocator); 1593 if (self.grid) |*grid| { 1594 grid.deinit(); 1595 self.grid = null; 1596 } 1597 } 1598 1599 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1600 _ = root_focus; 1601 self.clearGrid(); 1602 1603 // parse into rows of cells, then copy into a rectangular grid 1604 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1605 defer { 1606 for (rows.items) |*row| row.deinit(allocator); 1607 rows.deinit(allocator); 1608 } 1609 var row: std.ArrayList(Grid.Cell) = .empty; 1610 errdefer row.deinit(allocator); 1611 1612 var style: Grid.Style = .{}; 1613 var width: usize = 0; 1614 const content = self.content; 1615 var i: usize = 0; 1616 while (i < content.len) { 1617 const byte = content[i]; 1618 if (byte == '\n') { 1619 width = @max(width, row.items.len); 1620 try rows.append(allocator, row); 1621 row = .empty; 1622 i += 1; 1623 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1624 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1625 var j = i + 2; 1626 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1627 if (j >= content.len) { 1628 i = content.len; // malformed trailing escape; stop 1629 } else { 1630 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1631 i = j + 1; 1632 } 1633 } else { 1634 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1635 const end = @min(content.len, i + len); 1636 const rune = content[i..end]; 1637 const transparent = end - i == 1 and rune[0] == ' ' and 1638 style.fg == null and style.bg == null and !style.inverted; 1639 try row.append(allocator, .{ 1640 .rune = if (transparent) null else rune, 1641 .style = style, 1642 }); 1643 i = end; 1644 } 1645 } 1646 // a trailing row with content but no closing newline 1647 if (row.items.len > 0) { 1648 width = @max(width, row.items.len); 1649 try rows.append(allocator, row); 1650 } else { 1651 row.deinit(allocator); 1652 } 1653 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1654 1655 const height = rows.items.len; 1656 if (width == 0 or height == 0) return; 1657 1658 const clamped_w = @min(width, constraint.max_size.width orelse width); 1659 const clamped_h = @min(height, constraint.max_size.height orelse height); 1660 1661 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1662 errdefer grid.deinit(); 1663 for (rows.items[0..clamped_h], 0..) |r, y| { 1664 const n = @min(r.items.len, clamped_w); 1665 for (r.items[0..n], 0..) |cell, x| { 1666 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 1667 } 1668 } 1669 self.grid = grid; 1670 } 1671 1672 pub fn input(self: *AnsiArt, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1673 _ = self; 1674 _ = allocator; 1675 _ = key; 1676 _ = root_focus; 1677 } 1678 1679 pub fn clearGrid(self: *AnsiArt) void { 1680 if (self.grid) |*grid| { 1681 grid.deinit(); 1682 self.grid = null; 1683 } 1684 } 1685 1686 pub fn getGrid(self: AnsiArt) ?Grid { 1687 return self.grid; 1688 } 1689 1690 pub fn getFocus(self: *AnsiArt) *Focus { 1691 return self.focus; 1692 } 1693 1694 fn applySgr(style: *Grid.Style, params: []const u8) void { 1695 var nums: [16]u32 = undefined; 1696 var n: usize = 0; 1697 var it = std.mem.splitScalar(u8, params, ';'); 1698 while (it.next()) |tok| { 1699 if (n >= nums.len) break; 1700 // an empty parameter (e.g. bare "\x1b[m") means 0 1701 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1702 n += 1; 1703 } 1704 if (n == 0) { 1705 style.* = .{}; 1706 return; 1707 } 1708 var i: usize = 0; 1709 while (i < n) : (i += 1) { 1710 switch (nums[i]) { 1711 0 => style.* = .{}, 1712 7 => style.inverted = true, 1713 27 => style.inverted = false, 1714 39 => style.fg = null, 1715 49 => style.bg = null, 1716 38, 48 => { 1717 // truecolor form: 38;2;r;g;b — anything else is ignored 1718 if (i + 4 < n and nums[i + 1] == 2) { 1719 const c = Grid.Color{ 1720 .r = @truncate(nums[i + 2]), 1721 .g = @truncate(nums[i + 3]), 1722 .b = @truncate(nums[i + 4]), 1723 }; 1724 if (nums[i] == 38) style.fg = c else style.bg = c; 1725 i += 4; 1726 } 1727 }, 1728 else => {}, 1729 } 1730 } 1731 } 1732 }; 1733 1734 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1735 pub const AnsiBackground = struct { 1736 grid: ?Grid, 1737 child: *Widget, 1738 art: AnsiArt, 1739 session: *Session, 1740 1741 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 1742 var cw = child_widget; 1743 const child = allocator.create(Widget) catch |e| { 1744 cw.deinit(allocator); 1745 return e; 1746 }; 1747 child.* = cw; 1748 errdefer { 1749 child.deinit(allocator); 1750 allocator.destroy(child); 1751 } 1752 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 1753 } 1754 1755 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1756 if (self.grid) |*grid| grid.deinit(); 1757 self.art.deinit(allocator); 1758 self.child.deinit(allocator); 1759 allocator.destroy(self.child); 1760 } 1761 1762 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1763 self.clearGrid(); 1764 1765 // make the wrapped page fill the whole available area 1766 try self.child.build(allocator, .{ 1767 .min_size = .{ 1768 .width = constraint.max_size.width orelse constraint.min_size.width, 1769 .height = constraint.max_size.height orelse constraint.min_size.height, 1770 }, 1771 .max_size = constraint.max_size, 1772 }, root_focus); 1773 1774 if (self.session.data.enable_ansi) { 1775 if (self.child.getGrid()) |fg| { 1776 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 1777 } 1778 } 1779 } 1780 1781 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1782 try self.child.input(allocator, key, root_focus); 1783 } 1784 1785 pub fn clearGrid(self: *AnsiBackground) void { 1786 if (self.grid) |*grid| { 1787 grid.deinit(); 1788 self.grid = null; 1789 } 1790 self.child.clearGrid(); 1791 self.art.clearGrid(); 1792 } 1793 1794 pub fn getGrid(self: AnsiBackground) ?Grid { 1795 return self.grid orelse self.child.getGrid(); 1796 } 1797 1798 pub fn getFocus(self: *AnsiBackground) *Focus { 1799 return self.child.getFocus(); 1800 } 1801 1802 // which top corner the art hugs. 1803 const ArtAnchor = enum { top_left, top_right }; 1804 1805 // a foreground cell counts as blank if it's empty or a bare space without styling 1806 fn cellIsBlank(cell: Grid.Cell) bool { 1807 if (cell.rune) |rune| { 1808 return std.mem.eql(u8, rune, " ") and 1809 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1810 } 1811 return true; 1812 } 1813 1814 // the art is dimmed to this fraction of its brightness so foreground text 1815 // stays legible over it 1816 const art_brightness = 45; // percent 1817 1818 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1819 const c = color orelse return null; 1820 return .{ 1821 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 1822 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 1823 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 1824 }; 1825 } 1826 1827 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1828 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1829 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1830 // snap such channels clear of that range so the backdrop doesn't blink or 1831 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1832 // actually render truecolor. 1833 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1834 const c = color orelse return null; 1835 const snap = struct { 1836 fn f(v: u8) u8 { 1837 return if (v >= 1 and v <= 9) 10 else v; 1838 } 1839 }.f; 1840 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1841 } 1842 1843 // composites ANSI art behind a foreground grid 1844 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 1845 art.clearGrid(); 1846 try art.build(allocator, .{ 1847 .min_size = .{ .width = null, .height = null }, 1848 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1849 }, root_focus); 1850 1851 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1852 errdefer out.deinit(); 1853 1854 if (art.getGrid()) |art_grid| { 1855 const anchor_x = switch (anchor) { 1856 .top_left => 0, 1857 .top_right => foreground.size.width -| art_grid.size.width, 1858 }; 1859 for (0..art_grid.size.height) |y| { 1860 for (0..art_grid.size.width) |x| { 1861 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 1862 if (src.rune == null) continue; 1863 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 1864 const dst = &out.cells.items[idx]; 1865 // composite the dimmed art behind every cell so it shows 1866 // through the whole UI: a blank cell becomes the art, while a 1867 // cell with a glyph keeps the glyph but takes the dimmed art as 1868 // its background (replacing whatever background it had, which 1869 // would otherwise obscure the art). 1870 if (cellIsBlank(dst.*)) { 1871 dst.* = src; 1872 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 1873 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 1874 } else { 1875 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 1876 } 1877 } 1878 } 1879 } 1880 return out; 1881 } 1882 }; 1883