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