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