╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1147 allocator: std.mem.Allocator, 1148 session: *ui.Session, 1149 handle: ui.RepoHandle, 1150 label: []const u8, 1151 name: []const u8, 1152 text: ?[]const u8, 1153 show_input: bool, 1154 ) !SearchHeader { 1155 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 1156 errdefer box.deinit(allocator); 1157 1158 // the search box only appears where an index can serve it. 1159 if (show_input) { 1160 var search_box = try SearchBox.init(allocator, session, label, name, text); 1161 errdefer search_box.deinit(allocator); 1162 box.getFocus().child_id = search_box.getFocus().id; 1163 try box.children.put(allocator, search_box.getFocus().id, .{ .widget = .{ .search_box = search_box }, .rect = null, .min_size = SearchBox.min_size }); 1164 } 1165 1166 // the spacer pushes the clone url to the right edge. 1167 { 1168 var spacer = try Spacer.init(allocator); 1169 errdefer spacer.deinit(allocator); 1170 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1171 } 1172 1173 if (session.data.host_kind == .server) { 1174 if (try CopyableText.initClone(allocator, session, handle)) |value| { 1175 var clone_url = value; 1176 errdefer clone_url.deinit(allocator); 1177 const min_width = clone_url.minWidth(); 1178 if (box.getFocus().child_id == null) box.getFocus().child_id = clone_url.getFocus().id; 1179 try box.children.put(allocator, clone_url.getFocus().id, .{ .widget = .{ .copyable_text = clone_url }, .rect = null, .min_size = .{ .width = min_width, .height = 3 }, .max_size = .{ .width = min_width, .height = 3 } }); 1180 } 1181 } 1182 1183 return .{ .box = box, .session = session }; 1184 } 1185 1186 pub fn deinit(self: *SearchHeader, allocator: std.mem.Allocator) void { 1187 self.box.deinit(allocator); 1188 } 1189 1190 pub fn build(self: *SearchHeader, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1191 self.clearGrid(); 1192 try self.box.build(allocator, constraint, root_focus); 1193 } 1194 1195 pub fn input(self: *SearchHeader, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1196 const current = self.box.getFocus().child_id orelse return; 1197 if (self.searchBox()) |search_box| { 1198 if (current == search_box.getFocus().id) { 1199 // tab, and right-arrow past the last character, step to the 1200 // clone url beside the box 1201 if (key == .tab or (key == .arrow_right and search_box.atEnd())) { 1202 if (self.cloneUrl()) |clone_url| { 1203 root_focus.setFocus(clone_url.getFocus().id); 1204 return; 1205 } 1206 } 1207 return search_box.input(allocator, key, root_focus); 1208 } 1209 } 1210 const clone_url = self.cloneUrl() orelse return; 1211 if (key == .back_tab or (key == .arrow_left and clone_url.selected == 0)) { 1212 if (self.searchBox()) |search_box| { 1213 root_focus.setFocus(search_box.getFocus().id); 1214 return; 1215 } 1216 } 1217 try clone_url.input(allocator, key, root_focus); 1218 } 1219 1220 // the typed query when enter should submit it: the box exists and holds 1221 // focus. the caller owns the returned text. 1222 pub fn submittedText(self: *SearchHeader, allocator: std.mem.Allocator) !?[]u8 { 1223 const search_box = self.searchBox() orelse return null; 1224 if (self.box.getFocus().child_id != search_box.getFocus().id) return null; 1225 return try search_box.text(allocator); 1226 } 1227 1228 // enter the header, preferring the search box. 1229 pub fn focusHeader(self: *SearchHeader, root_focus: *Focus) bool { 1230 if (self.searchBox()) |search_box| { 1231 root_focus.setFocus(search_box.getFocus().id); 1232 return true; 1233 } 1234 const clone_url = self.cloneUrl() orelse return false; 1235 root_focus.setFocus(clone_url.getFocus().id); 1236 return true; 1237 } 1238 1239 pub fn hasFocusable(self: *SearchHeader) bool { 1240 return self.cloneUrl() != null or self.searchBox() != null; 1241 } 1242 1243 fn cloneUrl(self: *SearchHeader) ?*CopyableText { 1244 for (self.box.children.values()) |*child| switch (child.widget) { 1245 .copyable_text => |*copyable_text| return copyable_text, 1246 else => {}, 1247 }; 1248 return null; 1249 } 1250 1251 fn searchBox(self: *SearchHeader) ?*SearchBox { 1252 for (self.box.children.values()) |*child| switch (child.widget) { 1253 .search_box => |*search_box| return search_box, 1254 else => {}, 1255 }; 1256 return null; 1257 } 1258 1259 pub fn clearGrid(self: *SearchHeader) void { 1260 self.box.clearGrid(); 1261 } 1262 1263 pub fn getGrid(self: SearchHeader) ?Grid { 1264 return self.box.getGrid(); 1265 } 1266 1267 pub fn getFocus(self: *SearchHeader) *Focus { 1268 return self.box.getFocus(); 1269 } 1270 }; 1271 1272 // a one-row, non-focusable status bar showing the current page's url 1273 pub const Footer = struct { 1274 focus: *Focus, 1275 grid: ?Grid, 1276 session: *ui.Session, 1277 arena: std.heap.ArenaAllocator, 1278 url: []const u8 = "", 1279 url_width: usize = 0, 1280 1281 pub fn init(allocator: std.mem.Allocator, session: *ui.Session) !Footer { 1282 return .{ 1283 .focus = try Focus.create(allocator, .container), 1284 .grid = null, 1285 .session = session, 1286 .arena = std.heap.ArenaAllocator.init(allocator), 1287 }; 1288 } 1289 1290 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 1291 self.focus.destroy(allocator); 1292 if (self.grid) |*grid| grid.deinit(); 1293 self.arena.deinit(); 1294 } 1295 1296 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1297 _ = root_focus; 1298 self.clearGrid(); 1299 const width = constraint.max_size.width orelse return; 1300 if (width == 0) return; 1301 1302 _ = self.arena.reset(.retain_capacity); 1303 self.url = ""; 1304 self.url_width = 0; 1305 const aa = self.arena.allocator(); 1306 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 1307 const text = ui.terminalWebUrl(aa, self.session, path) catch return; 1308 self.url = text; 1309 1310 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1311 errdefer grid.deinit(); 1312 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 1313 var i: usize = 0; 1314 while (utf8.nextCodepoint()) |ch| { 1315 const rune_width = xitui.width.cellWidth(ch); 1316 if (i + rune_width > width) break; 1317 // a terminal hyperlink to the page 1318 (try grid.cell(i, 0)).link = text; 1319 try grid.setRune(i, 0, ch); 1320 i += rune_width; 1321 } 1322 self.url_width = i; 1323 self.grid = grid; 1324 } 1325 1326 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1327 _ = allocator; 1328 switch (key) { 1329 .mouse => |mouse| { 1330 if (mouse.ctrl or !inp.leftClickOn(root_focus, self.getFocus().id, mouse)) return; 1331 const rect = (root_focus.children.get(self.getFocus().id) orelse return).rect; 1332 if (mouse.x < rect.x + self.url_width) self.session.host_request = .{ .show_copyable_text = self.url }; 1333 }, 1334 else => {}, 1335 } 1336 } 1337 1338 pub fn clearGrid(self: *Footer) void { 1339 if (self.grid) |*grid| { 1340 grid.deinit(); 1341 self.grid = null; 1342 } 1343 } 1344 1345 pub fn getGrid(self: Footer) ?Grid { 1346 return self.grid; 1347 } 1348 1349 pub fn getFocus(self: *Footer) *Focus { 1350 return self.focus; 1351 } 1352 }; 1353 1354 // a single-child wrapper that builds the child at its natural size and 1355 // positions its grid in the middle of the area granted by the parent 1356 // a bordered label, indented past the content it precedes 1357 pub const SectionLabel = struct { 1358 box: wgt.Box(Widget), 1359 1360 pub fn init(allocator: std.mem.Allocator, content: []const u8) !SectionLabel { 1361 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 1362 errdefer box.deinit(allocator); 1363 1364 { 1365 var gap = try wgt.Text.init(allocator, " "); 1366 errdefer gap.deinit(allocator); 1367 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 1368 } 1369 1370 { 1371 var tb = try wgt.TextBox.init(allocator, content, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 1372 errdefer tb.deinit(allocator); 1373 tb.getFocus().mode = .all; 1374 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1375 } 1376 1377 box.getFocus().child_id = box.children.keys()[1]; 1378 return .{ .box = box }; 1379 } 1380 1381 pub fn deinit(self: *SectionLabel, allocator: std.mem.Allocator) void { 1382 self.box.deinit(allocator); 1383 } 1384 1385 pub fn build(self: *SectionLabel, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1386 try self.box.build(allocator, constraint, root_focus); 1387 } 1388 1389 pub fn input(self: *SectionLabel, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1390 _ = self; 1391 _ = allocator; 1392 _ = key; 1393 _ = root_focus; 1394 } 1395 1396 pub fn clearGrid(self: *SectionLabel) void { 1397 self.box.clearGrid(); 1398 } 1399 1400 pub fn getGrid(self: SectionLabel) ?Grid { 1401 return self.box.getGrid(); 1402 } 1403 1404 pub fn getFocus(self: *SectionLabel) *Focus { 1405 return self.box.getFocus(); 1406 } 1407 }; 1408 1409 // a submit button, indented past its form's content. the web form POSTs 1410 // the button's form; terminal hosts detect presses through `buttonId`. 1411 pub const SubmitButton = struct { 1412 box: wgt.Box(Widget), 1413 1414 pub fn init(allocator: std.mem.Allocator) !SubmitButton { 1415 return initLabeled(allocator, "submit"); 1416 } 1417 1418 pub fn initLabeled(allocator: std.mem.Allocator, label: []const u8) !SubmitButton { 1419 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 1420 errdefer box.deinit(allocator); 1421 1422 { 1423 var gap = try wgt.Text.init(allocator, " "); 1424 errdefer gap.deinit(allocator); 1425 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 1426 } 1427 1428 { 1429 var button = try wgt.TextBox.init(allocator, label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 1430 errdefer button.deinit(allocator); 1431 button.getFocus().mode = .all; 1432 // the renderer distinguishes plain clickables from buttons that 1433 // should POST to a server route by this kind. 1434 button.getFocus().kind = .{ .custom = "submit" }; 1435 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 1436 } 1437 1438 box.getFocus().child_id = box.children.keys()[1]; 1439 return .{ .box = box }; 1440 } 1441 1442 pub fn deinit(self: *SubmitButton, allocator: std.mem.Allocator) void { 1443 self.box.deinit(allocator); 1444 } 1445 1446 pub fn setLabel(self: *SubmitButton, allocator: std.mem.Allocator, label: []const u8) !void { 1447 const button = &self.box.children.values()[1]; 1448 try button.widget.text_box.setContent(allocator, label); 1449 button.min_size = .{ .width = label.len + 2, .height = null }; 1450 } 1451 1452 pub fn build(self: *SubmitButton, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1453 try self.box.build(allocator, constraint, root_focus); 1454 } 1455 1456 pub fn input(self: *SubmitButton, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1457 _ = self; 1458 _ = allocator; 1459 _ = key; 1460 _ = root_focus; 1461 } 1462 1463 pub fn clearGrid(self: *SubmitButton) void { 1464 self.box.clearGrid(); 1465 } 1466 1467 pub fn getGrid(self: SubmitButton) ?Grid { 1468 return self.box.getGrid(); 1469 } 1470 1471 pub fn getFocus(self: *SubmitButton) *Focus { 1472 return self.box.getFocus(); 1473 } 1474 1475 // the button's focus id, for click hit-testing 1476 pub fn buttonId(self: *const SubmitButton) usize { 1477 return self.box.children.keys()[1]; 1478 } 1479 }; 1480 1481 // keep `rect`, in a centered widget's space, in view within its scroll 1482 pub fn scrollToCenteredRect(scroll: *wgt.Scroll(Widget), rect: layout.IRect) void { 1483 const center = &scroll.child.center; 1484 const center_grid = center.getGrid() orelse return; 1485 const child_grid = center.child.getGrid() orelse return; 1486 const offset_y: isize = @intCast((center_grid.size.height -| child_grid.size.height) / 2); 1487 scroll.scrollToRect(.{ .x = rect.x, .y = rect.y + offset_y, .size = rect.size }); 1488 } 1489 1490 pub const Center = struct { 1491 focus: *Focus, 1492 grid: ?Grid, 1493 child: *Widget, 1494 1495 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !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 }; 1504 } 1505 1506 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1507 self.focus.destroy(allocator); 1508 if (self.grid) |*grid| { 1509 grid.deinit(); 1510 self.grid = null; 1511 } 1512 self.child.deinit(allocator); 1513 allocator.destroy(self.child); 1514 } 1515 1516 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1517 self.clearGrid(); 1518 self.getFocus().clear(); 1519 1520 // build the child without forcing it to fill min_size; it sizes 1521 // itself to its content within the available max. 1522 try self.child.build(allocator, .{ 1523 .min_size = .{ .width = null, .height = null }, 1524 .max_size = constraint.max_size, 1525 }, root_focus); 1526 1527 const child_grid = self.child.getGrid() orelse return; 1528 1529 // prefer max when given; otherwise grow to max(min, child) so that 1530 // when only a min is set (e.g. the wasm path passes viewport rows 1531 // as min_height with no max), we can still vertically center while 1532 // letting taller content extend past the min. 1533 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; 1534 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; 1535 if (width == 0 or height == 0) return; 1536 1537 const offset_x: usize = (width -| child_grid.size.width) / 2; 1538 const offset_y: usize = (height -| child_grid.size.height) / 2; 1539 1540 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1541 errdefer grid.deinit(); 1542 try grid.drawGrid(child_grid, offset_x, offset_y); 1543 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1544 self.getFocus().child_id = self.child.getFocus().id; 1545 1546 self.grid = grid; 1547 } 1548 1549 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1550 try self.child.input(allocator, key, root_focus); 1551 } 1552 1553 pub fn clearGrid(self: *Center) void { 1554 if (self.grid) |*grid| { 1555 grid.deinit(); 1556 self.grid = null; 1557 } 1558 self.child.clearGrid(); 1559 } 1560 1561 pub fn getGrid(self: Center) ?Grid { 1562 return self.grid; 1563 } 1564 1565 pub fn getFocus(self: *Center) *Focus { 1566 return self.focus; 1567 } 1568 }; 1569 1570 // renders truecolor ANSI art 1571 pub const AnsiArt = struct { 1572 focus: *Focus, 1573 grid: ?Grid, 1574 content: []const u8, 1575 1576 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1577 return .{ 1578 .focus = try Focus.create(allocator, .container), 1579 .grid = null, 1580 .content = content, 1581 }; 1582 } 1583 1584 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1585 self.focus.destroy(allocator); 1586 if (self.grid) |*grid| { 1587 grid.deinit(); 1588 self.grid = null; 1589 } 1590 } 1591 1592 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1593 _ = root_focus; 1594 self.clearGrid(); 1595 1596 // parse into rows of cells, then copy into a rectangular grid 1597 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1598 defer { 1599 for (rows.items) |*row| row.deinit(allocator); 1600 rows.deinit(allocator); 1601 } 1602 var row: std.ArrayList(Grid.Cell) = .empty; 1603 errdefer row.deinit(allocator); 1604 1605 var style: Grid.Style = .{}; 1606 var width: usize = 0; 1607 const content = self.content; 1608 var i: usize = 0; 1609 while (i < content.len) { 1610 const byte = content[i]; 1611 if (byte == '\n') { 1612 width = @max(width, row.items.len); 1613 try rows.append(allocator, row); 1614 row = .empty; 1615 i += 1; 1616 } else if (byte == '\r') { 1617 // git may checkout the ansi art with crlf line endings on windows 1618 i += 1; 1619 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1620 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1621 var j = i + 2; 1622 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1623 if (j >= content.len) { 1624 i = content.len; // malformed trailing escape; stop 1625 } else { 1626 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1627 i = j + 1; 1628 } 1629 } else { 1630 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1631 const end = @min(content.len, i + len); 1632 const rune = content[i..end]; 1633 const codepoint = std.unicode.utf8Decode(rune) catch '�'; 1634 // a foreground color does not make a space visible. only an 1635 // effective background makes it opaque; foreground state often 1636 // remains active through the padding at the end of a row. 1637 const effective_background = if (style.inverted) style.fg else style.bg; 1638 const transparent = end - i == 1 and rune[0] == ' ' and effective_background == null; 1639 try row.append(allocator, .{ 1640 .rune = if (transparent) null else codepoint, 1641 .style = style, 1642 }); 1643 if (!transparent and xitui.width.cellWidth(codepoint) == 2) { 1644 try row.append(allocator, .{ .rune = null, .style = style, .continuation = true }); 1645 } 1646 i = end; 1647 } 1648 } 1649 // a trailing row with content but no closing newline 1650 if (row.items.len > 0) { 1651 width = @max(width, row.items.len); 1652 try rows.append(allocator, row); 1653 } else { 1654 row.deinit(allocator); 1655 } 1656 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1657 1658 const height = rows.items.len; 1659 if (width == 0 or height == 0) return; 1660 1661 const clamped_w = @min(width, constraint.max_size.width orelse width); 1662 const clamped_h = @min(height, constraint.max_size.height orelse height); 1663 1664 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1665 errdefer grid.deinit(); 1666 for (rows.items[0..clamped_h], 0..) |r, y| { 1667 const n = @min(r.items.len, clamped_w); 1668 for (r.items[0..n], 0..) |cell, x| { 1669 if (cell.continuation) continue; 1670 (try grid.cell(x, y)).style = cell.style; 1671 try grid.setRune(x, y, cell.rune); 1672 } 1673 } 1674 self.grid = grid; 1675 } 1676 1677 pub fn clearGrid(self: *AnsiArt) void { 1678 if (self.grid) |*grid| { 1679 grid.deinit(); 1680 self.grid = null; 1681 } 1682 } 1683 1684 pub fn getGrid(self: AnsiArt) ?Grid { 1685 return self.grid; 1686 } 1687 1688 pub fn getFocus(self: *AnsiArt) *Focus { 1689 return self.focus; 1690 } 1691 1692 fn applySgr(style: *Grid.Style, params: []const u8) void { 1693 var nums: [16]u32 = undefined; 1694 var n: usize = 0; 1695 var it = std.mem.splitScalar(u8, params, ';'); 1696 while (it.next()) |tok| { 1697 if (n >= nums.len) break; 1698 // an empty parameter (e.g. bare "\x1b[m") means 0 1699 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1700 n += 1; 1701 } 1702 if (n == 0) { 1703 style.* = .{}; 1704 return; 1705 } 1706 var i: usize = 0; 1707 while (i < n) : (i += 1) { 1708 switch (nums[i]) { 1709 0 => style.* = .{}, 1710 7 => style.inverted = true, 1711 27 => style.inverted = false, 1712 39 => style.fg = null, 1713 49 => style.bg = null, 1714 38, 48 => { 1715 // truecolor form: 38;2;r;g;b — anything else is ignored 1716 if (i + 4 < n and nums[i + 1] == 2) { 1717 const c = Grid.Color{ .rgb = .{ 1718 .r = @truncate(nums[i + 2]), 1719 .g = @truncate(nums[i + 3]), 1720 .b = @truncate(nums[i + 4]), 1721 } }; 1722 if (nums[i] == 38) style.fg = c else style.bg = c; 1723 i += 4; 1724 } 1725 }, 1726 else => {}, 1727 } 1728 } 1729 } 1730 }; 1731 1732 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1733 pub const AnsiBackground = struct { 1734 grid: ?Grid, 1735 child: *Widget, 1736 art: AnsiArt, 1737 session: *ui.Session, 1738 1739 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, session: *ui.Session) !AnsiBackground { 1740 var cw = child_widget; 1741 const child = allocator.create(Widget) catch |e| { 1742 cw.deinit(allocator); 1743 return e; 1744 }; 1745 child.* = cw; 1746 errdefer { 1747 child.deinit(allocator); 1748 allocator.destroy(child); 1749 } 1750 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, session.data.ansi_art), .session = session }; 1751 } 1752 1753 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1754 if (self.grid) |*grid| grid.deinit(); 1755 self.art.deinit(allocator); 1756 self.child.deinit(allocator); 1757 allocator.destroy(self.child); 1758 } 1759 1760 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1761 self.clearGrid(); 1762 1763 // make the wrapped page fill the whole available area 1764 try self.child.build(allocator, .{ 1765 .min_size = .{ 1766 .width = constraint.max_size.width orelse constraint.min_size.width, 1767 .height = constraint.max_size.height orelse constraint.min_size.height, 1768 }, 1769 .max_size = constraint.max_size, 1770 }, root_focus); 1771 1772 const foreground = self.child.getGrid() orelse return; 1773 if (!self.session.color) return; 1774 // terminals show the art only once they report a background to fade it toward 1775 const backdrop: ?Grid.Color.Rgb = if (self.session.is_terminal) self.session.terminal_background orelse return else null; 1776 self.art.content = self.session.data.ansi_art; 1777 try self.art.build(allocator, .{ 1778 .min_size = .{ .width = null, .height = null }, 1779 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1780 }, root_focus); 1781 // the web fades the art with css opacity instead 1782 const art_backdrop = backdrop orelse return; 1783 if (self.art.grid) |*art_grid| { 1784 for (art_grid.cells) |*cell| { 1785 cell.style.fg = fadeColor(cell.style.fg, art_backdrop); 1786 cell.style.bg = fadeColor(cell.style.bg, art_backdrop); 1787 } 1788 self.grid = try artBehind(allocator, foreground, art_grid.*); 1789 } 1790 } 1791 1792 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1793 try self.child.input(allocator, key, root_focus); 1794 } 1795 1796 pub fn clearGrid(self: *AnsiBackground) void { 1797 if (self.grid) |*grid| { 1798 grid.deinit(); 1799 self.grid = null; 1800 } 1801 self.child.clearGrid(); 1802 self.art.clearGrid(); 1803 } 1804 1805 pub fn getGrid(self: AnsiBackground) ?Grid { 1806 return self.grid orelse self.child.getGrid(); 1807 } 1808 1809 pub fn getFocus(self: *AnsiBackground) *Focus { 1810 return self.child.getFocus(); 1811 } 1812 1813 // a foreground cell counts as blank if it's empty or a bare space without styling 1814 fn cellIsBlank(cell: Grid.Cell) bool { 1815 if (cell.continuation) return false; 1816 if (cell.rune) |rune| { 1817 return rune == ' ' and 1818 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1819 } 1820 return true; 1821 } 1822 1823 // the art is faded toward the background at this opacity so foreground 1824 // text stays legible over it 1825 pub const art_opacity = 25; // percent 1826 1827 // the art parser only emits truecolor, so a palette color never occurs 1828 fn artRgb(color: ?Grid.Color) ?Grid.Color.Rgb { 1829 const c = color orelse return null; 1830 return switch (c) { 1831 .rgb => |v| v, 1832 else => null, 1833 }; 1834 } 1835 1836 fn fadeColor(color: ?Grid.Color, backdrop: Grid.Color.Rgb) ?Grid.Color { 1837 const v = artRgb(color) orelse return color; 1838 const blend = struct { 1839 fn f(c: u8, b: u8) u8 { 1840 return @intCast((@as(u16, c) * art_opacity + @as(u16, b) * (100 - art_opacity)) / 100); 1841 } 1842 }.f; 1843 return .{ .rgb = .{ .r = blend(v.r, backdrop.r), .g = blend(v.g, backdrop.g), .b = blend(v.b, backdrop.b) } }; 1844 } 1845 1846 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1847 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1848 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1849 // snap such channels clear of that range so the backdrop doesn't blink or 1850 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1851 // actually render truecolor. 1852 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1853 const v = artRgb(color) orelse return color; 1854 const snap = struct { 1855 fn f(c: u8) u8 { 1856 return if (c >= 1 and c <= 9) 10 else c; 1857 } 1858 }.f; 1859 return .{ .rgb = .{ .r = snap(v.r), .g = snap(v.g), .b = snap(v.b) } }; 1860 } 1861 1862 // composites ANSI art behind a foreground grid 1863 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 1864 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1865 errdefer out.deinit(); 1866 1867 const anchor_x = foreground.size.width -| art_grid.size.width; 1868 for (0..art_grid.size.height) |y| { 1869 for (0..art_grid.size.width) |x| { 1870 const src = (try art_grid.cell(x, y)).*; 1871 if (src.rune == null) continue; 1872 const dst = try out.cell(anchor_x + x, y); 1873 const wide = x + 1 < art_grid.size.width and (try art_grid.cell(x + 1, y)).continuation; 1874 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 1875 // blank cells take the art; occupied cells take its background 1876 if (cellIsBlank(dst.*) and (if (next_dst) |next| cellIsBlank(next.*) else true)) { 1877 dst.style = src.style; 1878 dst.style.fg = sgrSafe(dst.style.fg); 1879 dst.style.bg = sgrSafe(dst.style.bg); 1880 try out.setRune(anchor_x + x, y, src.rune); 1881 } else { 1882 const art_bg = sgrSafe(src.style.bg orelse src.style.fg); 1883 dst.style.bg = art_bg; 1884 if (next_dst) |next| next.style.bg = art_bg; 1885 } 1886 } 1887 } 1888 return out; 1889 } 1890 }; 1891 1892 pub const CopyableText = struct { 1893 pub const Choice = struct { 1894 selector: []const u8 = "", 1895 text: []const u8, 1896 copyable_text: ?[]const u8 = null, 1897 label: []const u8 = "", 1898 bottom_label: []const u8 = "", 1899 }; 1900 1901 box: wgt.Box(Widget), 1902 session: *ui.Session, 1903 choices: []Choice, 1904 selected: usize = 0, 1905 1906 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice, initial: usize) !CopyableText { 1907 if (choices.len == 0) return error.MissingCopyableText; 1908 1909 const owned_choices = try allocator.dupe(Choice, choices); 1910 errdefer allocator.free(owned_choices); 1911 1912 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 1913 errdefer box.deinit(allocator); 1914 1915 if (choices.len > 1) for (choices) |choice| { 1916 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border = .hidden, .round_corners = true, .wrap_kind = .none }); 1917 errdefer selector_box.deinit(allocator); 1918 selector_box.getFocus().mode = .all; 1919 try box.children.put(allocator, selector_box.getFocus().id, .{ .widget = .{ .text_box = selector_box }, .rect = null, .min_size = .{ .width = choice.selector.len + 2, .height = 3 } }); 1920 }; 1921 1922 var text_input = try wgt.TextInput.init(allocator, .{ 1923 .border = .single, 1924 .round_corners = true, 1925 .top_label = .{ .text = choices[initial].label }, 1926 .bottom_label = .{ .text = choices[initial].bottom_label }, 1927 .read_only = true, 1928 .render_content = session.is_terminal, 1929 .visible_width = null, 1930 }); 1931 errdefer text_input.deinit(allocator); 1932 text_input.getFocus().mode = .all; 1933 try text_input.setContent(allocator, choices[initial].text); 1934 box.getFocus().child_id = text_input.getFocus().id; 1935 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1936 1937 return .{ .box = box, .session = session, .choices = owned_choices, .selected = initial }; 1938 } 1939 1940 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, handle: ui.RepoHandle) !?CopyableText { 1941 const aa = session.page_arena.allocator(); 1942 var choices: [2]Choice = undefined; 1943 var count: usize = 0; 1944 var ssh_index: ?usize = null; 1945 switch (handle.location) { 1946 .repo => |identity| { 1947 if (session.data.git_http_port) |port| { 1948 const url = try aa.print("http://localhost:{d}/{s}", .{ port, identity }); 1949 choices[count] = .{ 1950 .selector = "http", 1951 .text = url, 1952 .copyable_text = try aa.print("git clone {s}", .{url}), 1953 .label = " clone ", 1954 }; 1955 count += 1; 1956 } 1957 if (session.data.git_ssh_port) |port| { 1958 ssh_index = count; 1959 const url = try aa.print("ssh://localhost:{d}/{s}", .{ port, identity }); 1960 choices[count] = .{ 1961 .selector = "ssh", 1962 .text = url, 1963 .copyable_text = try aa.print("{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1964 .label = " clone ", 1965 }; 1966 count += 1; 1967 } 1968 }, 1969 .fork => |f| if (session.data.git_ssh_port) |port| { 1970 const url = try aa.print("ssh://localhost:{d}/{s}+{s}", .{ port, f.identity, f.id }); 1971 choices[count] = .{ 1972 .text = url, 1973 .copyable_text = try aa.print("{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1974 .label = " clone ", 1975 }; 1976 count += 1; 1977 }, 1978 } 1979 if (count == 0) return null; 1980 // a viewer who can push starts on the ssh url 1981 const initial = if (handle.canWrite()) ssh_index orelse 0 else 0; 1982 return @as(?CopyableText, try init(allocator, session, choices[0..count], initial)); 1983 } 1984 1985 pub fn minWidth(self: *const CopyableText) usize { 1986 var width = maxWidth(self.choices) + 2; 1987 if (self.choices.len > 1) { 1988 for (self.choices) |choice| width += choice.selector.len + 2; 1989 } 1990 return width; 1991 } 1992 1993 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1994 self.box.deinit(allocator); 1995 allocator.free(self.choices); 1996 } 1997 1998 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1999 self.clearGrid(); 2000 2001 if (self.choices.len > 1) { 2002 if (root_focus.grandchild_id) |id| { 2003 for (0..self.choices.len) |i| { 2004 if (id != self.selector(i).getFocus().id) continue; 2005 if (i != self.selected) try self.select(allocator, i); 2006 root_focus.setFocus(self.textInput().getFocus().id); 2007 break; 2008 } 2009 } 2010 2011 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 2012 for (0..self.choices.len) |i| { 2013 self.selector(i).options.border = if (i == self.selected) 2014 (if (focused) .double else .single) 2015 else 2016 .hidden; 2017 } 2018 } 2019 self.textInput().options.border = .single; 2020 self.textInput().cursor = 0; 2021 self.textInput().scroll_offset = 0; 2022 try self.box.build(allocator, constraint, root_focus); 2023 2024 if (!self.session.is_terminal) { 2025 const aa = self.session.arena.allocator(); 2026 const id = self.textInput().getFocus().id; 2027 try self.session.text_inputs.put(aa, id, self.textInput()); 2028 } 2029 } 2030 2031 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2032 switch (key) { 2033 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 2034 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 2035 .enter => if (self.session.is_terminal) self.show(), 2036 .mouse => |mouse| { 2037 if (self.choices.len > 1) for (0..self.choices.len) |i| { 2038 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 2039 try self.select(allocator, i); 2040 root_focus.setFocus(self.textInput().getFocus().id); 2041 return; 2042 }; 2043 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 2044 }, 2045 else => {}, 2046 } 2047 } 2048 2049 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 2050 self.selected = selected; 2051 const choice = self.choices[selected]; 2052 const text_input = self.textInput(); 2053 text_input.options.top_label.text = choice.label; 2054 text_input.options.bottom_label.text = choice.bottom_label; 2055 try text_input.setContent(allocator, choice.text); 2056 } 2057 2058 fn show(self: *CopyableText) void { 2059 const choice = self.choices[self.selected]; 2060 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 2061 } 2062 2063 fn textInput(self: *CopyableText) *wgt.TextInput { 2064 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 2065 } 2066 2067 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 2068 return &self.box.children.values()[index].widget.text_box; 2069 } 2070 2071 fn maxWidth(choices: []const Choice) usize { 2072 var width: usize = 0; 2073 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 2074 return width; 2075 } 2076 2077 pub fn clearGrid(self: *CopyableText) void { 2078 self.box.clearGrid(); 2079 } 2080 2081 pub fn getGrid(self: CopyableText) ?Grid { 2082 return self.box.getGrid(); 2083 } 2084 2085 pub fn getFocus(self: *CopyableText) *Focus { 2086 return self.box.getFocus(); 2087 } 2088 }; 2089