╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
368 pub fn input(self: *Scroll, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 369 _ = allocator; 370 const in = self.inner(); 371 const child_id = in.focus.child_id orelse return; 372 const current_index = in.indexOfFocusId(child_id) orelse return; 373 const count = in.text_boxes.items.len; 374 if (count == 0) return; 375 const cols = in.last_cols; 376 const slot_height = in.options.cell_height + FlowBox.border_rows; 377 378 var index = current_index; 379 switch (key) { 380 .arrow_up => index -|= cols, 381 .arrow_down => if (index + cols < count) { 382 index += cols; 383 }, 384 .arrow_left => index -|= 1, 385 .arrow_right => if (index + 1 < count) { 386 index += 1; 387 }, 388 .home => index = 0, 389 .end => index = count - 1, 390 .page_up => { 391 if (self.scroll.grid) |grid| if (slot_height > 0) { 392 const rows_per_page = grid.size.height / slot_height; 393 index -|= rows_per_page * cols; 394 }; 395 }, 396 .page_down => { 397 if (self.scroll.grid) |grid| if (slot_height > 0) { 398 const rows_per_page = grid.size.height / slot_height; 399 index = @min(index + rows_per_page * cols, count - 1); 400 }; 401 }, 402 // scroll wheel moves the focused cell by a full row so the 403 // viewport (via scrollToRect) follows in row-sized steps, 404 // matching how a scroll wheel feels in a grid view. 405 .mouse => |mouse| switch (mouse.action) { 406 .scroll => |dir| switch (dir) { 407 .up => index -|= cols, 408 .down => if (index + cols < count) { 409 index += cols; 410 }, 411 }, 412 else => {}, 413 }, 414 else => {}, 415 } 416 417 if (index != current_index) { 418 root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 419 if (in.cellRect(index)) |rect| { 420 self.scroll.scrollToRect(rect); 421 } 422 } 423 } 424 425 pub fn clearGrid(self: *Scroll) void { 426 self.scroll.clearGrid(); 427 } 428 429 pub fn getGrid(self: Scroll) ?Grid { 430 return self.scroll.getGrid(); 431 } 432 433 pub fn getFocus(self: *Scroll) *Focus { 434 return self.scroll.getFocus(); 435 } 436 437 pub fn atTop(self: Scroll) bool { 438 const in = self.scroll.child.flow_box; 439 const child_id = in.focus.child_id orelse return false; 440 return in.indexOfFocusId(child_id) == 0; 441 } 442 443 fn inner(self: *Scroll) *FlowBox { 444 return &self.scroll.child.flow_box; 445 } 446 }; 447 }; 448 449 // a left-to-right flow of variable-width focusable items that wraps at the 450 // available width, like words in a paragraph. unlike FlowBox there is no cell 451 // grid: each item is as wide as its text. selection movement is driven by the 452 // owning view via indexOfFocusId/verticalNeighbor. 453 pub const TagFlow = struct { 454 focus: *Focus, 455 grid: ?Grid, 456 text_boxes: std.ArrayList(wgt.TextBox), 457 // last build's item rects (content space), for vertical navigation and 458 // scroll-into-view. 459 rects: std.ArrayList(layout.IRect), 460 // backs each link item's `.custom` focus kind. 461 arena: std.heap.ArenaAllocator, 462 463 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 464 465 pub fn init(allocator: std.mem.Allocator) !TagFlow { 466 return .{ 467 .focus = try Focus.create(allocator, .container), 468 .grid = null, 469 .text_boxes = .empty, 470 .rects = .empty, 471 .arena = std.heap.ArenaAllocator.init(allocator), 472 }; 473 } 474 475 pub fn deinit(self: *TagFlow, allocator: std.mem.Allocator) void { 476 self.focus.destroy(allocator); 477 if (self.grid) |*grid| { 478 grid.deinit(); 479 self.grid = null; 480 } 481 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 482 self.text_boxes.deinit(allocator); 483 self.rects.deinit(allocator); 484 self.arena.deinit(); 485 } 486 487 // the text box copies the item text; links are copied into the arena, so the 488 // caller's slices needn't outlive this call. 489 pub fn setItems(self: *TagFlow, allocator: std.mem.Allocator, items: []const Item) !void { 490 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 491 self.text_boxes.clearAndFree(allocator); 492 493 _ = self.arena.reset(.retain_capacity); 494 const aa = self.arena.allocator(); 495 496 self.focus.clear(); 497 self.focus.child_id = null; 498 499 for (items) |item| { 500 var text_box = try wgt.TextBox.init(allocator, item.text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 501 errdefer text_box.deinit(allocator); 502 text_box.getFocus().mode = .all; 503 504 if (item.link.len > 0) { 505 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 506 } 507 508 try self.text_boxes.append(allocator, text_box); 509 } 510 511 if (self.text_boxes.items.len > 0) { 512 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 513 } 514 } 515 516 pub fn build(self: *TagFlow, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 517 self.clearGrid(); 518 self.focus.clear(); 519 520 if (self.text_boxes.items.len == 0) return; 521 const max_width = constraint.max_size.width orelse 80; 522 if (max_width == 0) return; 523 524 // lay the items out left to right, wrapping to a new row when the next 525 // one wouldn't fit. 526 self.rects.clearRetainingCapacity(); 527 var x: usize = 0; 528 var y: usize = 0; 529 var row_height: usize = 0; 530 var total_width: usize = 0; 531 for (self.text_boxes.items) |*tb| { 532 try tb.build(allocator, .{ 533 .min_size = .{ .width = null, .height = null }, 534 .max_size = .{ .width = max_width, .height = null }, 535 }, root_focus); 536 const tb_grid_maybe = tb.getGrid(); 537 const w: usize = if (tb_grid_maybe) |g| g.size.width else 0; 538 const h: usize = if (tb_grid_maybe) |g| g.size.height else 0; 539 if (x > 0 and x + w > max_width) { 540 x = 0; 541 y += row_height; 542 row_height = 0; 543 } 544 try self.rects.append(allocator, .{ .x = @intCast(x), .y = @intCast(y), .size = .{ .width = w, .height = h } }); 545 if (h > row_height) row_height = h; 546 x += w; 547 if (x > total_width) total_width = x; 548 } 549 550 const total_height = y + row_height; 551 if (total_width == 0 or total_height == 0) return; 552 553 var grid = try Grid.init(allocator, .{ .width = total_width, .height = total_height }); 554 errdefer grid.deinit(); 555 556 for (self.text_boxes.items, self.rects.items) |*tb, rect| { 557 const tb_grid = tb.getGrid() orelse continue; 558 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, @intCast(rect.x), @intCast(rect.y)); 559 try grid.drawGrid(tb_grid, @intCast(rect.x), @intCast(rect.y)); 560 } 561 562 self.grid = grid; 563 } 564 565 pub fn input(self: *TagFlow, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 566 _ = self; 567 _ = allocator; 568 _ = key; 569 _ = root_focus; 570 } 571 572 pub fn clearGrid(self: *TagFlow) void { 573 if (self.grid) |*grid| { 574 grid.deinit(); 575 self.grid = null; 576 } 577 for (self.text_boxes.items) |*tb| tb.clearGrid(); 578 } 579 580 pub fn getGrid(self: TagFlow) ?Grid { 581 return self.grid; 582 } 583 584 pub fn getFocus(self: *TagFlow) *Focus { 585 return self.focus; 586 } 587 588 pub fn indexOfFocusId(self: TagFlow, focus_id: usize) ?usize { 589 for (self.text_boxes.items, 0..) |tb, i| { 590 if (tb.focus.id == focus_id) return i; 591 } 592 return null; 593 } 594 595 // the first item of the row below (when `downward`) or above, or null at 596 // the flow's edge. items are laid out in order, so y is nondecreasing. 597 pub fn rowStep(self: TagFlow, index: usize, downward: bool) ?usize { 598 const rects = self.rects.items; 599 if (index >= rects.len) return null; 600 const cur_y = rects[index].y; 601 if (downward) { 602 for (rects[index + 1 ..], index + 1..) |r, i| { 603 if (r.y > cur_y) return i; 604 } 605 return null; 606 } 607 // walk the earlier rows; the last one visited is the row above. 608 var prev_first: ?usize = null; 609 var i: usize = 0; 610 while (i < rects.len and rects[i].y < cur_y) { 611 prev_first = i; 612 const y = rects[i].y; 613 while (i < rects.len and rects[i].y == y) i += 1; 614 } 615 return prev_first; 616 } 617 }; 618 619 // an invisible widget that fills the horizontal space granted by its parent. 620 // used inside Box(horiz) with a min_size so the box reserves space for the 621 // children that follow, pushing them to the right. 622 pub const Spacer = struct { 623 focus: *Focus, 624 grid: ?Grid, 625 626 pub fn init(allocator: std.mem.Allocator) !Spacer { 627 return .{ 628 .focus = try Focus.create(allocator, .container), 629 .grid = null, 630 }; 631 } 632 633 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 634 self.focus.destroy(allocator); 635 if (self.grid) |*grid| { 636 grid.deinit(); 637 self.grid = null; 638 } 639 } 640 641 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 642 _ = root_focus; 643 self.clearGrid(); 644 const width = constraint.max_size.width orelse return; 645 if (width == 0) return; 646 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 647 } 648 649 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 650 _ = self; 651 _ = allocator; 652 _ = key; 653 _ = root_focus; 654 } 655 656 pub fn clearGrid(self: *Spacer) void { 657 if (self.grid) |*grid| { 658 grid.deinit(); 659 self.grid = null; 660 } 661 } 662 663 pub fn getGrid(self: Spacer) ?Grid { 664 return self.grid; 665 } 666 667 pub fn getFocus(self: *Spacer) *Focus { 668 return self.focus; 669 } 670 }; 671 672 // a sub-header's search box: a labeled text input whose enter the owning view 673 // turns into a navigation. shared by the files, commits and thread tabs. 674 pub const SearchBox = struct { 675 text_input: wgt.TextInput, 676 session: *ui.Session, 677 678 // the room the box asks its row for 679 pub const min_size: layout.MaybeSize = .{ .width = 20, .height = 3 }; 680 681 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, label: []const u8, name: []const u8, text_value: ?[]const u8) !SearchBox { 682 var text_input = try wgt.TextInput.init(allocator, .{ .label = label, .name = name, .rounded_corners = true, .visible_width = 18, .render_content = session.is_terminal }); 683 errdefer text_input.deinit(allocator); 684 text_input.getFocus().mode = .all; 685 if (text_value) |value| try text_input.setContent(allocator, value); 686 return .{ .text_input = text_input, .session = session }; 687 } 688 689 pub fn deinit(self: *SearchBox, allocator: std.mem.Allocator) void { 690 self.text_input.deinit(allocator); 691 } 692 693 pub fn build(self: *SearchBox, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 694 self.text_input.options.bottom_label = if (root_focus.grandchild_id == self.getFocus().id) " press enter " else ""; 695 try self.text_input.build(allocator, constraint, root_focus); 696 if (!self.session.is_terminal) { 697 try self.session.text_inputs.put(self.session.arena.allocator(), self.getFocus().id, &self.text_input); 698 } 699 } 700 701 pub fn input(self: *SearchBox, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 702 try self.text_input.input(allocator, key, root_focus); 703 } 704 705 // the typed text; the caller owns it 706 pub fn text(self: *SearchBox, allocator: std.mem.Allocator) ![]u8 { 707 return self.text_input.text(allocator); 708 } 709 710 // whether the cursor sits past the last character, where right-arrow leaves 711 // the box rather than moving within it 712 pub fn atEnd(self: *SearchBox) bool { 713 return self.text_input.cursor == self.text_input.content.items.len; 714 } 715 716 pub fn clearGrid(self: *SearchBox) void { 717 self.text_input.clearGrid(); 718 } 719 720 pub fn getGrid(self: SearchBox) ?Grid { 721 return self.text_input.getGrid(); 722 } 723 724 pub fn getFocus(self: *SearchBox) *Focus { 725 return self.text_input.getFocus(); 726 } 727 }; 728 729 // a tab's sub-header: an optional search box on the left, then a spacer, then 730 // the clone url on the right. the owning view turns a submitted query into a 731 // route, so route building stays with the tab that knows its own url. 732 pub const SearchHeader = struct { 733 box: wgt.Box(Widget), 734 session: *ui.Session, 735 736 pub fn init( 737 allocator: std.mem.Allocator, 738 session: *ui.Session, 739 location: ui.RoutablePage.RepoLocation, 740 label: []const u8, 741 name: []const u8, 742 text: ?[]const u8, 743 show_input: bool, 744 ) !SearchHeader { 745 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 746 errdefer box.deinit(allocator); 747 748 // the search box only appears where an index can serve it. 749 if (show_input) { 750 var search_box = try SearchBox.init(allocator, session, label, name, text); 751 errdefer search_box.deinit(allocator); 752 box.getFocus().child_id = search_box.getFocus().id; 753 try box.children.put(allocator, search_box.getFocus().id, .{ .widget = .{ .search_box = search_box }, .rect = null, .min_size = SearchBox.min_size }); 754 } 755 756 // the spacer pushes the clone url to the right edge. 757 { 758 var spacer = try Spacer.init(allocator); 759 errdefer spacer.deinit(allocator); 760 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 761 } 762 763 if (session.data.host_kind == .server) { 764 if (try CopyableText.initClone(allocator, session, location)) |value| { 765 var clone_url = value; 766 errdefer clone_url.deinit(allocator); 767 const min_width = clone_url.minWidth(); 768 if (box.getFocus().child_id == null) box.getFocus().child_id = clone_url.getFocus().id; 769 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 } }); 770 } 771 } 772 773 return .{ .box = box, .session = session }; 774 } 775 776 pub fn deinit(self: *SearchHeader, allocator: std.mem.Allocator) void { 777 self.box.deinit(allocator); 778 } 779 780 pub fn build(self: *SearchHeader, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 781 self.clearGrid(); 782 try self.box.build(allocator, constraint, root_focus); 783 } 784 785 pub fn input(self: *SearchHeader, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 786 const current = self.box.getFocus().child_id orelse return; 787 if (self.searchBox()) |search_box| { 788 if (current == search_box.getFocus().id) { 789 // tab, and right-arrow past the last character, step to the 790 // clone url beside the box 791 if (key == .tab or (key == .arrow_right and search_box.atEnd())) { 792 if (self.cloneUrl()) |clone_url| { 793 root_focus.setFocus(clone_url.getFocus().id); 794 return; 795 } 796 } 797 return search_box.input(allocator, key, root_focus); 798 } 799 } 800 const clone_url = self.cloneUrl() orelse return; 801 if (key == .back_tab or (key == .arrow_left and clone_url.selected == 0)) { 802 if (self.searchBox()) |search_box| { 803 root_focus.setFocus(search_box.getFocus().id); 804 return; 805 } 806 } 807 try clone_url.input(allocator, key, root_focus); 808 } 809 810 // the typed query when enter should submit it: the box exists and holds 811 // focus. the caller owns the returned text. 812 pub fn submittedText(self: *SearchHeader, allocator: std.mem.Allocator) !?[]u8 { 813 const search_box = self.searchBox() orelse return null; 814 if (self.box.getFocus().child_id != search_box.getFocus().id) return null; 815 return try search_box.text(allocator); 816 } 817 818 // enter the header, preferring the search box. 819 pub fn focusHeader(self: *SearchHeader, root_focus: *Focus) bool { 820 if (self.searchBox()) |search_box| { 821 root_focus.setFocus(search_box.getFocus().id); 822 return true; 823 } 824 const clone_url = self.cloneUrl() orelse return false; 825 root_focus.setFocus(clone_url.getFocus().id); 826 return true; 827 } 828 829 pub fn hasFocusable(self: *SearchHeader) bool { 830 return self.cloneUrl() != null or self.searchBox() != null; 831 } 832 833 fn cloneUrl(self: *SearchHeader) ?*CopyableText { 834 for (self.box.children.values()) |*child| switch (child.widget) { 835 .copyable_text => |*copyable_text| return copyable_text, 836 else => {}, 837 }; 838 return null; 839 } 840 841 fn searchBox(self: *SearchHeader) ?*SearchBox { 842 for (self.box.children.values()) |*child| switch (child.widget) { 843 .search_box => |*search_box| return search_box, 844 else => {}, 845 }; 846 return null; 847 } 848 849 pub fn clearGrid(self: *SearchHeader) void { 850 self.box.clearGrid(); 851 } 852 853 pub fn getGrid(self: SearchHeader) ?Grid { 854 return self.box.getGrid(); 855 } 856 857 pub fn getFocus(self: *SearchHeader) *Focus { 858 return self.box.getFocus(); 859 } 860 }; 861 862 // a one-row, non-focusable status bar showing the current page's url 863 pub const Footer = struct { 864 focus: *Focus, 865 grid: ?Grid, 866 session: *ui.Session, 867 arena: std.heap.ArenaAllocator, 868 url: []const u8 = "", 869 url_width: usize = 0, 870 871 pub fn init(allocator: std.mem.Allocator, session: *ui.Session) !Footer { 872 return .{ 873 .focus = try Focus.create(allocator, .container), 874 .grid = null, 875 .session = session, 876 .arena = std.heap.ArenaAllocator.init(allocator), 877 }; 878 } 879 880 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 881 self.focus.destroy(allocator); 882 if (self.grid) |*grid| grid.deinit(); 883 self.arena.deinit(); 884 } 885 886 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 887 _ = root_focus; 888 self.clearGrid(); 889 const width = constraint.max_size.width orelse return; 890 if (width == 0) return; 891 892 _ = self.arena.reset(.retain_capacity); 893 self.url = ""; 894 self.url_width = 0; 895 const aa = self.arena.allocator(); 896 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 897 const text = ui.terminalWebUrl(aa, self.session, path) catch return; 898 self.url = text; 899 900 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 901 errdefer grid.deinit(); 902 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 903 var i: usize = 0; 904 while (utf8.nextCodepoint()) |ch| { 905 const rune_width = xitui.width.cellWidth(ch); 906 if (i + rune_width > width) break; 907 try grid.setRune(i, 0, ch); 908 i += rune_width; 909 } 910 self.url_width = i; 911 self.grid = grid; 912 } 913 914 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 915 _ = allocator; 916 switch (key) { 917 .mouse => |mouse| { 918 if (mouse.ctrl or !inp.leftClickOn(root_focus, self.getFocus().id, mouse)) return; 919 const rect = (root_focus.children.get(self.getFocus().id) orelse return).rect; 920 if (mouse.x < rect.x + self.url_width) self.session.host_request = .{ .show_copyable_text = self.url }; 921 }, 922 else => {}, 923 } 924 } 925 926 pub fn clearGrid(self: *Footer) void { 927 if (self.grid) |*grid| { 928 grid.deinit(); 929 self.grid = null; 930 } 931 } 932 933 pub fn getGrid(self: Footer) ?Grid { 934 return self.grid; 935 } 936 937 pub fn getFocus(self: *Footer) *Focus { 938 return self.focus; 939 } 940 }; 941 942 // a single-child wrapper that builds the child at its natural size and 943 // positions its grid in the middle of the area granted by the parent 944 // a bordered label, indented past the content it precedes 945 pub const SectionLabel = struct { 946 box: wgt.Box(Widget), 947 948 pub fn init(allocator: std.mem.Allocator, content: []const u8) !SectionLabel { 949 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 950 errdefer box.deinit(allocator); 951 952 { 953 var gap = try wgt.Text.init(allocator, " "); 954 errdefer gap.deinit(allocator); 955 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 956 } 957 958 { 959 var tb = try wgt.TextBox.init(allocator, content, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 960 errdefer tb.deinit(allocator); 961 tb.getFocus().mode = .all; 962 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 963 } 964 965 box.getFocus().child_id = box.children.keys()[1]; 966 return .{ .box = box }; 967 } 968 969 pub fn deinit(self: *SectionLabel, allocator: std.mem.Allocator) void { 970 self.box.deinit(allocator); 971 } 972 973 pub fn build(self: *SectionLabel, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 974 try self.box.build(allocator, constraint, root_focus); 975 } 976 977 pub fn input(self: *SectionLabel, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 978 _ = self; 979 _ = allocator; 980 _ = key; 981 _ = root_focus; 982 } 983 984 pub fn clearGrid(self: *SectionLabel) void { 985 self.box.clearGrid(); 986 } 987 988 pub fn getGrid(self: SectionLabel) ?Grid { 989 return self.box.getGrid(); 990 } 991 992 pub fn getFocus(self: *SectionLabel) *Focus { 993 return self.box.getFocus(); 994 } 995 }; 996 997 // a submit button, indented past its form's content. the web form POSTs 998 // the button's form; terminal hosts detect presses through `buttonId`. 999 pub const SubmitButton = struct { 1000 box: wgt.Box(Widget), 1001 1002 pub fn init(allocator: std.mem.Allocator) !SubmitButton { 1003 return initLabeled(allocator, "submit"); 1004 } 1005 1006 pub fn initLabeled(allocator: std.mem.Allocator, label: []const u8) !SubmitButton { 1007 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1008 errdefer box.deinit(allocator); 1009 1010 { 1011 var gap = try wgt.Text.init(allocator, " "); 1012 errdefer gap.deinit(allocator); 1013 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 1014 } 1015 1016 { 1017 var button = try wgt.TextBox.init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1018 errdefer button.deinit(allocator); 1019 button.getFocus().mode = .all; 1020 // the renderer distinguishes plain clickables from buttons that 1021 // should POST to a server route by this kind. 1022 button.getFocus().kind = .{ .custom = "submit" }; 1023 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 1024 } 1025 1026 box.getFocus().child_id = box.children.keys()[1]; 1027 return .{ .box = box }; 1028 } 1029 1030 pub fn deinit(self: *SubmitButton, allocator: std.mem.Allocator) void { 1031 self.box.deinit(allocator); 1032 } 1033 1034 pub fn setLabel(self: *SubmitButton, allocator: std.mem.Allocator, label: []const u8) !void { 1035 const button = &self.box.children.values()[1]; 1036 try button.widget.text_box.setContent(allocator, label); 1037 button.min_size = .{ .width = label.len + 2, .height = null }; 1038 } 1039 1040 pub fn build(self: *SubmitButton, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1041 try self.box.build(allocator, constraint, root_focus); 1042 } 1043 1044 pub fn input(self: *SubmitButton, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1045 _ = self; 1046 _ = allocator; 1047 _ = key; 1048 _ = root_focus; 1049 } 1050 1051 pub fn clearGrid(self: *SubmitButton) void { 1052 self.box.clearGrid(); 1053 } 1054 1055 pub fn getGrid(self: SubmitButton) ?Grid { 1056 return self.box.getGrid(); 1057 } 1058 1059 pub fn getFocus(self: *SubmitButton) *Focus { 1060 return self.box.getFocus(); 1061 } 1062 1063 // the button's focus id, for click hit-testing 1064 pub fn buttonId(self: *const SubmitButton) usize { 1065 return self.box.children.keys()[1]; 1066 } 1067 }; 1068 1069 pub const Center = struct { 1070 focus: *Focus, 1071 grid: ?Grid, 1072 child: *Widget, 1073 1074 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 1075 const child = try allocator.create(Widget); 1076 errdefer allocator.destroy(child); 1077 child.* = child_widget; 1078 return .{ 1079 .focus = try Focus.create(allocator, .container), 1080 .grid = null, 1081 .child = child, 1082 }; 1083 } 1084 1085 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1086 self.focus.destroy(allocator); 1087 if (self.grid) |*grid| { 1088 grid.deinit(); 1089 self.grid = null; 1090 } 1091 self.child.deinit(allocator); 1092 allocator.destroy(self.child); 1093 } 1094 1095 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1096 self.clearGrid(); 1097 self.getFocus().clear(); 1098 1099 // build the child without forcing it to fill min_size; it sizes 1100 // itself to its content within the available max. 1101 try self.child.build(allocator, .{ 1102 .min_size = .{ .width = null, .height = null }, 1103 .max_size = constraint.max_size, 1104 }, root_focus); 1105 1106 const child_grid = self.child.getGrid() orelse return; 1107 1108 // prefer max when given; otherwise grow to max(min, child) so that 1109 // when only a min is set (e.g. the wasm path passes viewport rows 1110 // as min_height with no max), we can still vertically center while 1111 // letting taller content extend past the min. 1112 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; 1113 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; 1114 if (width == 0 or height == 0) return; 1115 1116 const offset_x: usize = (width -| child_grid.size.width) / 2; 1117 const offset_y: usize = (height -| child_grid.size.height) / 2; 1118 1119 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1120 errdefer grid.deinit(); 1121 try grid.drawGrid(child_grid, offset_x, offset_y); 1122 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1123 self.getFocus().child_id = self.child.getFocus().id; 1124 1125 self.grid = grid; 1126 } 1127 1128 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1129 try self.child.input(allocator, key, root_focus); 1130 } 1131 1132 pub fn clearGrid(self: *Center) void { 1133 if (self.grid) |*grid| { 1134 grid.deinit(); 1135 self.grid = null; 1136 } 1137 self.child.clearGrid(); 1138 } 1139 1140 pub fn getGrid(self: Center) ?Grid { 1141 return self.grid; 1142 } 1143 1144 pub fn getFocus(self: *Center) *Focus { 1145 return self.focus; 1146 } 1147 }; 1148 1149 // renders truecolor ANSI art 1150 pub const AnsiArt = struct { 1151 focus: *Focus, 1152 grid: ?Grid, 1153 content: []const u8, 1154 1155 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1156 return .{ 1157 .focus = try Focus.create(allocator, .container), 1158 .grid = null, 1159 .content = content, 1160 }; 1161 } 1162 1163 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1164 self.focus.destroy(allocator); 1165 if (self.grid) |*grid| { 1166 grid.deinit(); 1167 self.grid = null; 1168 } 1169 } 1170 1171 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1172 _ = root_focus; 1173 self.clearGrid(); 1174 1175 // parse into rows of cells, then copy into a rectangular grid 1176 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1177 defer { 1178 for (rows.items) |*row| row.deinit(allocator); 1179 rows.deinit(allocator); 1180 } 1181 var row: std.ArrayList(Grid.Cell) = .empty; 1182 errdefer row.deinit(allocator); 1183 1184 var style: Grid.Style = .{}; 1185 var width: usize = 0; 1186 const content = self.content; 1187 var i: usize = 0; 1188 while (i < content.len) { 1189 const byte = content[i]; 1190 if (byte == '\n') { 1191 width = @max(width, row.items.len); 1192 try rows.append(allocator, row); 1193 row = .empty; 1194 i += 1; 1195 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1196 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1197 var j = i + 2; 1198 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1199 if (j >= content.len) { 1200 i = content.len; // malformed trailing escape; stop 1201 } else { 1202 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1203 i = j + 1; 1204 } 1205 } else { 1206 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1207 const end = @min(content.len, i + len); 1208 const rune = content[i..end]; 1209 const codepoint = std.unicode.utf8Decode(rune) catch '�'; 1210 // a foreground color does not make a space visible. only an 1211 // effective background makes it opaque; foreground state often 1212 // remains active through the padding at the end of a row. 1213 const effective_background = if (style.inverted) style.fg else style.bg; 1214 const transparent = end - i == 1 and rune[0] == ' ' and effective_background == null; 1215 try row.append(allocator, .{ 1216 .rune = if (transparent) null else codepoint, 1217 .style = style, 1218 }); 1219 if (!transparent and xitui.width.cellWidth(codepoint) == 2) { 1220 try row.append(allocator, .{ .rune = null, .style = style, .continuation = true }); 1221 } 1222 i = end; 1223 } 1224 } 1225 // a trailing row with content but no closing newline 1226 if (row.items.len > 0) { 1227 width = @max(width, row.items.len); 1228 try rows.append(allocator, row); 1229 } else { 1230 row.deinit(allocator); 1231 } 1232 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1233 1234 const height = rows.items.len; 1235 if (width == 0 or height == 0) return; 1236 1237 const clamped_w = @min(width, constraint.max_size.width orelse width); 1238 const clamped_h = @min(height, constraint.max_size.height orelse height); 1239 1240 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1241 errdefer grid.deinit(); 1242 for (rows.items[0..clamped_h], 0..) |r, y| { 1243 const n = @min(r.items.len, clamped_w); 1244 for (r.items[0..n], 0..) |cell, x| { 1245 if (cell.continuation) continue; 1246 (try grid.cell(x, y)).style = cell.style; 1247 try grid.setRune(x, y, cell.rune); 1248 } 1249 } 1250 self.grid = grid; 1251 } 1252 1253 pub fn clearGrid(self: *AnsiArt) void { 1254 if (self.grid) |*grid| { 1255 grid.deinit(); 1256 self.grid = null; 1257 } 1258 } 1259 1260 pub fn getGrid(self: AnsiArt) ?Grid { 1261 return self.grid; 1262 } 1263 1264 pub fn getFocus(self: *AnsiArt) *Focus { 1265 return self.focus; 1266 } 1267 1268 fn applySgr(style: *Grid.Style, params: []const u8) void { 1269 var nums: [16]u32 = undefined; 1270 var n: usize = 0; 1271 var it = std.mem.splitScalar(u8, params, ';'); 1272 while (it.next()) |tok| { 1273 if (n >= nums.len) break; 1274 // an empty parameter (e.g. bare "\x1b[m") means 0 1275 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1276 n += 1; 1277 } 1278 if (n == 0) { 1279 style.* = .{}; 1280 return; 1281 } 1282 var i: usize = 0; 1283 while (i < n) : (i += 1) { 1284 switch (nums[i]) { 1285 0 => style.* = .{}, 1286 7 => style.inverted = true, 1287 27 => style.inverted = false, 1288 39 => style.fg = null, 1289 49 => style.bg = null, 1290 38, 48 => { 1291 // truecolor form: 38;2;r;g;b — anything else is ignored 1292 if (i + 4 < n and nums[i + 1] == 2) { 1293 const c = Grid.Color{ 1294 .r = @truncate(nums[i + 2]), 1295 .g = @truncate(nums[i + 3]), 1296 .b = @truncate(nums[i + 4]), 1297 }; 1298 if (nums[i] == 38) style.fg = c else style.bg = c; 1299 i += 4; 1300 } 1301 }, 1302 else => {}, 1303 } 1304 } 1305 } 1306 }; 1307 1308 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1309 pub const AnsiBackground = struct { 1310 grid: ?Grid, 1311 child: *Widget, 1312 art: AnsiArt, 1313 session: *ui.Session, 1314 1315 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, session: *ui.Session) !AnsiBackground { 1316 var cw = child_widget; 1317 const child = allocator.create(Widget) catch |e| { 1318 cw.deinit(allocator); 1319 return e; 1320 }; 1321 child.* = cw; 1322 errdefer { 1323 child.deinit(allocator); 1324 allocator.destroy(child); 1325 } 1326 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, session.data.ansi_art), .session = session }; 1327 } 1328 1329 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1330 if (self.grid) |*grid| grid.deinit(); 1331 self.art.deinit(allocator); 1332 self.child.deinit(allocator); 1333 allocator.destroy(self.child); 1334 } 1335 1336 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1337 self.clearGrid(); 1338 1339 // make the wrapped page fill the whole available area 1340 try self.child.build(allocator, .{ 1341 .min_size = .{ 1342 .width = constraint.max_size.width orelse constraint.min_size.width, 1343 .height = constraint.max_size.height orelse constraint.min_size.height, 1344 }, 1345 .max_size = constraint.max_size, 1346 }, root_focus); 1347 1348 if (!self.session.data.enable_ansi) return; 1349 const foreground = self.child.getGrid() orelse return; 1350 self.art.content = self.session.data.ansi_art; 1351 try self.buildArt(allocator, foreground.size, root_focus); 1352 if (!self.session.is_terminal) return; 1353 const art_grid = self.art.getGrid() orelse return; 1354 self.grid = try artBehind(allocator, foreground, art_grid); 1355 } 1356 1357 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1358 try self.child.input(allocator, key, root_focus); 1359 } 1360 1361 pub fn clearGrid(self: *AnsiBackground) void { 1362 if (self.grid) |*grid| { 1363 grid.deinit(); 1364 self.grid = null; 1365 } 1366 self.child.clearGrid(); 1367 self.art.clearGrid(); 1368 } 1369 1370 pub fn getGrid(self: AnsiBackground) ?Grid { 1371 return self.grid orelse self.child.getGrid(); 1372 } 1373 1374 pub fn getFocus(self: *AnsiBackground) *Focus { 1375 return self.child.getFocus(); 1376 } 1377 1378 // a foreground cell counts as blank if it's empty or a bare space without styling 1379 fn cellIsBlank(cell: Grid.Cell) bool { 1380 if (cell.continuation) return false; 1381 if (cell.rune) |rune| { 1382 return rune == ' ' and 1383 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1384 } 1385 return true; 1386 } 1387 1388 // the art is dimmed to this fraction of its brightness so foreground text 1389 // stays legible over it 1390 const art_brightness = 35; // percent 1391 1392 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1393 const c = color orelse return null; 1394 return .{ 1395 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 1396 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 1397 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 1398 }; 1399 } 1400 1401 fn buildArt(self: *AnsiBackground, allocator: std.mem.Allocator, size: layout.Size, root_focus: *Focus) !void { 1402 try self.art.build(allocator, .{ 1403 .min_size = .{ .width = null, .height = null }, 1404 .max_size = .{ .width = size.width, .height = size.height }, 1405 }, root_focus); 1406 if (self.art.grid) |*grid| { 1407 for (grid.cells) |*cell| { 1408 cell.style.fg = dimColor(cell.style.fg); 1409 cell.style.bg = dimColor(cell.style.bg); 1410 } 1411 } 1412 } 1413 1414 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1415 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1416 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1417 // snap such channels clear of that range so the backdrop doesn't blink or 1418 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1419 // actually render truecolor. 1420 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1421 const c = color orelse return null; 1422 const snap = struct { 1423 fn f(v: u8) u8 { 1424 return if (v >= 1 and v <= 9) 10 else v; 1425 } 1426 }.f; 1427 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1428 } 1429 1430 fn applyArtBackground(dst: *Grid.Cell, src: Grid.Cell) void { 1431 const background_maybe = sgrSafe(src.style.bg orelse src.style.fg); 1432 dst.style.bg = background_maybe; 1433 // change unstyled text to contrast with the art behind it 1434 if (dst.style.fg == null) { 1435 if (background_maybe) |background| { 1436 // use near-black and near-white, which is easier on the eyes 1437 // while retaining high contrast 1438 const luminance = (@as(u32, background.r) * 299 + 1439 @as(u32, background.g) * 587 + 1440 @as(u32, background.b) * 114) / 1000; 1441 dst.style.fg = if (luminance >= 128) 1442 .{ .r = 16, .g = 16, .b = 16 } 1443 else 1444 .{ .r = 240, .g = 240, .b = 240 }; 1445 } 1446 } 1447 } 1448 1449 // composites ANSI art behind a foreground grid 1450 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 1451 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1452 errdefer out.deinit(); 1453 1454 const anchor_x = foreground.size.width -| art_grid.size.width; 1455 for (0..art_grid.size.height) |y| { 1456 for (0..art_grid.size.width) |x| { 1457 const src = (try art_grid.cell(x, y)).*; 1458 if (src.rune == null) continue; 1459 const dst = try out.cell(anchor_x + x, y); 1460 const wide = x + 1 < art_grid.size.width and (try art_grid.cell(x + 1, y)).continuation; 1461 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 1462 // blank cells take the art; occupied cells take its background 1463 if (cellIsBlank(dst.*) and (if (next_dst) |next| cellIsBlank(next.*) else true)) { 1464 dst.style = src.style; 1465 dst.style.fg = sgrSafe(dst.style.fg); 1466 dst.style.bg = sgrSafe(dst.style.bg); 1467 try out.setRune(anchor_x + x, y, src.rune); 1468 } else { 1469 applyArtBackground(dst, src); 1470 if (next_dst) |next| applyArtBackground(next, src); 1471 } 1472 } 1473 } 1474 return out; 1475 } 1476 }; 1477 1478 pub const CopyableText = struct { 1479 pub const Choice = struct { 1480 selector: []const u8 = "", 1481 text: []const u8, 1482 copyable_text: ?[]const u8 = null, 1483 label: []const u8 = "", 1484 bottom_label: []const u8 = "", 1485 }; 1486 1487 box: wgt.Box(Widget), 1488 session: *ui.Session, 1489 choices: []Choice, 1490 selected: usize = 0, 1491 1492 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice) !CopyableText { 1493 if (choices.len == 0) return error.MissingCopyableText; 1494 1495 const owned_choices = try allocator.dupe(Choice, choices); 1496 errdefer allocator.free(owned_choices); 1497 1498 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1499 errdefer box.deinit(allocator); 1500 1501 if (choices.len > 1) for (choices) |choice| { 1502 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 1503 errdefer selector_box.deinit(allocator); 1504 selector_box.getFocus().mode = .all; 1505 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 } }); 1506 }; 1507 1508 var text_input = try wgt.TextInput.init(allocator, .{ 1509 .border_style = .single, 1510 .rounded_corners = true, 1511 .label = choices[0].label, 1512 .bottom_label = choices[0].bottom_label, 1513 .read_only = true, 1514 .render_content = session.is_terminal, 1515 .visible_width = null, 1516 }); 1517 errdefer text_input.deinit(allocator); 1518 text_input.getFocus().mode = .all; 1519 try text_input.setContent(allocator, choices[0].text); 1520 box.getFocus().child_id = text_input.getFocus().id; 1521 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1522 1523 return .{ .box = box, .session = session, .choices = owned_choices }; 1524 } 1525 1526 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, location: ui.RoutablePage.RepoLocation) !?CopyableText { 1527 const aa = session.page_arena.allocator(); 1528 var choices: [2]Choice = undefined; 1529 var count: usize = 0; 1530 switch (location) { 1531 .repo => |identity| { 1532 if (session.data.git_http_port) |port| { 1533 const url = try std.fmt.allocPrint(aa, "http://localhost:{d}/repo/{s}", .{ port, identity }); 1534 choices[count] = .{ 1535 .selector = "http", 1536 .text = url, 1537 .copyable_text = try std.fmt.allocPrint(aa, "git clone {s}", .{url}), 1538 .label = " clone ", 1539 }; 1540 count += 1; 1541 } 1542 if (session.data.git_ssh_port) |port| { 1543 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}", .{ port, identity }); 1544 choices[count] = .{ 1545 .selector = "ssh", 1546 .text = url, 1547 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1548 .label = " clone ", 1549 }; 1550 count += 1; 1551 } 1552 }, 1553 .fork => |f| if (session.data.git_ssh_port) |port| { 1554 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/fork/{s}/patch:{s}", .{ port, f.identity, f.id }); 1555 choices[count] = .{ 1556 .text = url, 1557 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1558 .label = " clone ", 1559 }; 1560 count += 1; 1561 }, 1562 } 1563 if (count == 0) return null; 1564 return @as(?CopyableText, try init(allocator, session, choices[0..count])); 1565 } 1566 1567 pub fn minWidth(self: *const CopyableText) usize { 1568 var width = maxWidth(self.choices) + 2; 1569 if (self.choices.len > 1) { 1570 for (self.choices) |choice| width += choice.selector.len + 2; 1571 } 1572 return width; 1573 } 1574 1575 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1576 self.box.deinit(allocator); 1577 allocator.free(self.choices); 1578 } 1579 1580 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1581 self.clearGrid(); 1582 1583 if (self.choices.len > 1) { 1584 if (root_focus.grandchild_id) |id| { 1585 for (0..self.choices.len) |i| { 1586 if (id != self.selector(i).getFocus().id) continue; 1587 if (i != self.selected) try self.select(allocator, i); 1588 root_focus.setFocus(self.textInput().getFocus().id); 1589 break; 1590 } 1591 } 1592 1593 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 1594 for (0..self.choices.len) |i| { 1595 self.selector(i).options.border_style = if (i == self.selected) 1596 (if (focused) .double else .single) 1597 else 1598 .hidden; 1599 } 1600 } 1601 self.textInput().options.border_style = .single; 1602 self.textInput().cursor = 0; 1603 self.textInput().scroll_offset = 0; 1604 try self.box.build(allocator, constraint, root_focus); 1605 1606 if (!self.session.is_terminal) { 1607 const aa = self.session.arena.allocator(); 1608 const id = self.textInput().getFocus().id; 1609 try self.session.text_inputs.put(aa, id, self.textInput()); 1610 } 1611 } 1612 1613 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1614 switch (key) { 1615 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 1616 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 1617 .enter => if (self.session.is_terminal) self.show(), 1618 .mouse => |mouse| { 1619 if (self.choices.len > 1) for (0..self.choices.len) |i| { 1620 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 1621 try self.select(allocator, i); 1622 root_focus.setFocus(self.textInput().getFocus().id); 1623 return; 1624 }; 1625 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 1626 }, 1627 else => {}, 1628 } 1629 } 1630 1631 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 1632 self.selected = selected; 1633 const choice = self.choices[selected]; 1634 const text_input = self.textInput(); 1635 text_input.options.label = choice.label; 1636 text_input.options.bottom_label = choice.bottom_label; 1637 try text_input.setContent(allocator, choice.text); 1638 } 1639 1640 fn show(self: *CopyableText) void { 1641 const choice = self.choices[self.selected]; 1642 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 1643 } 1644 1645 fn textInput(self: *CopyableText) *wgt.TextInput { 1646 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 1647 } 1648 1649 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 1650 return &self.box.children.values()[index].widget.text_box; 1651 } 1652 1653 fn maxWidth(choices: []const Choice) usize { 1654 var width: usize = 0; 1655 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 1656 return width; 1657 } 1658 1659 pub fn clearGrid(self: *CopyableText) void { 1660 self.box.clearGrid(); 1661 } 1662 1663 pub fn getGrid(self: CopyableText) ?Grid { 1664 return self.box.getGrid(); 1665 } 1666 1667 pub fn getFocus(self: *CopyableText) *Focus { 1668 return self.box.getFocus(); 1669 } 1670 }; 1671