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