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