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