╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1428 } 1429 1430 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1431 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1432 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1433 // snap such channels clear of that range so the backdrop doesn't blink or 1434 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1435 // actually render truecolor. 1436 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1437 const v = artRgb(color) orelse return color; 1438 const snap = struct { 1439 fn f(c: u8) u8 { 1440 return if (c >= 1 and c <= 9) 10 else c; 1441 } 1442 }.f; 1443 return .{ .rgb = .{ .r = snap(v.r), .g = snap(v.g), .b = snap(v.b) } }; 1444 } 1445 1446 fn applyArtBackground(dst: *Grid.Cell, src: Grid.Cell) void { 1447 const background_maybe = sgrSafe(src.style.bg orelse src.style.fg); 1448 dst.style.bg = background_maybe; 1449 // change unstyled text to contrast with the art behind it 1450 if (dst.style.fg == null) { 1451 if (artRgb(background_maybe)) |v| { 1452 // use near-black and near-white, which is easier on the eyes 1453 // while retaining high contrast 1454 const luminance = (@as(u32, v.r) * 299 + 1455 @as(u32, v.g) * 587 + 1456 @as(u32, v.b) * 114) / 1000; 1457 dst.style.fg = if (luminance >= 128) 1458 .{ .rgb = .{ .r = 16, .g = 16, .b = 16 } } 1459 else 1460 .{ .rgb = .{ .r = 240, .g = 240, .b = 240 } }; 1461 } 1462 } 1463 } 1464 1465 // composites ANSI art behind a foreground grid 1466 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 1467 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1468 errdefer out.deinit(); 1469 1470 const anchor_x = foreground.size.width -| art_grid.size.width; 1471 for (0..art_grid.size.height) |y| { 1472 for (0..art_grid.size.width) |x| { 1473 const src = (try art_grid.cell(x, y)).*; 1474 if (src.rune == null) continue; 1475 const dst = try out.cell(anchor_x + x, y); 1476 const wide = x + 1 < art_grid.size.width and (try art_grid.cell(x + 1, y)).continuation; 1477 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 1478 // blank cells take the art; occupied cells take its background 1479 if (cellIsBlank(dst.*) and (if (next_dst) |next| cellIsBlank(next.*) else true)) { 1480 dst.style = src.style; 1481 dst.style.fg = sgrSafe(dst.style.fg); 1482 dst.style.bg = sgrSafe(dst.style.bg); 1483 try out.setRune(anchor_x + x, y, src.rune); 1484 } else { 1485 applyArtBackground(dst, src); 1486 if (next_dst) |next| applyArtBackground(next, src); 1487 } 1488 } 1489 } 1490 return out; 1491 } 1492 }; 1493 1494 pub const CopyableText = struct { 1495 pub const Choice = struct { 1496 selector: []const u8 = "", 1497 text: []const u8, 1498 copyable_text: ?[]const u8 = null, 1499 label: []const u8 = "", 1500 bottom_label: []const u8 = "", 1501 }; 1502 1503 box: wgt.Box(Widget), 1504 session: *ui.Session, 1505 choices: []Choice, 1506 selected: usize = 0, 1507 1508 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice) !CopyableText { 1509 if (choices.len == 0) return error.MissingCopyableText; 1510 1511 const owned_choices = try allocator.dupe(Choice, choices); 1512 errdefer allocator.free(owned_choices); 1513 1514 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1515 errdefer box.deinit(allocator); 1516 1517 if (choices.len > 1) for (choices) |choice| { 1518 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border_style = .hidden, .round_corners = true, .wrap_kind = .none }); 1519 errdefer selector_box.deinit(allocator); 1520 selector_box.getFocus().mode = .all; 1521 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 } }); 1522 }; 1523 1524 var text_input = try wgt.TextInput.init(allocator, .{ 1525 .border_style = .single, 1526 .round_corners = true, 1527 .label = choices[0].label, 1528 .bottom_label = choices[0].bottom_label, 1529 .read_only = true, 1530 .render_content = session.is_terminal, 1531 .visible_width = null, 1532 }); 1533 errdefer text_input.deinit(allocator); 1534 text_input.getFocus().mode = .all; 1535 try text_input.setContent(allocator, choices[0].text); 1536 box.getFocus().child_id = text_input.getFocus().id; 1537 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1538 1539 return .{ .box = box, .session = session, .choices = owned_choices }; 1540 } 1541 1542 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, location: ui.RoutablePage.RepoLocation) !?CopyableText { 1543 const aa = session.page_arena.allocator(); 1544 var choices: [2]Choice = undefined; 1545 var count: usize = 0; 1546 switch (location) { 1547 .repo => |identity| { 1548 if (session.data.git_http_port) |port| { 1549 const url = try std.fmt.allocPrint(aa, "http://localhost:{d}/repo/{s}", .{ port, identity }); 1550 choices[count] = .{ 1551 .selector = "http", 1552 .text = url, 1553 .copyable_text = try std.fmt.allocPrint(aa, "git clone {s}", .{url}), 1554 .label = " clone ", 1555 }; 1556 count += 1; 1557 } 1558 if (session.data.git_ssh_port) |port| { 1559 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}", .{ port, identity }); 1560 choices[count] = .{ 1561 .selector = "ssh", 1562 .text = url, 1563 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1564 .label = " clone ", 1565 }; 1566 count += 1; 1567 } 1568 }, 1569 .fork => |f| if (session.data.git_ssh_port) |port| { 1570 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/fork/{s}/patch:{s}", .{ port, f.identity, f.id }); 1571 choices[count] = .{ 1572 .text = url, 1573 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1574 .label = " clone ", 1575 }; 1576 count += 1; 1577 }, 1578 } 1579 if (count == 0) return null; 1580 return @as(?CopyableText, try init(allocator, session, choices[0..count])); 1581 } 1582 1583 pub fn minWidth(self: *const CopyableText) usize { 1584 var width = maxWidth(self.choices) + 2; 1585 if (self.choices.len > 1) { 1586 for (self.choices) |choice| width += choice.selector.len + 2; 1587 } 1588 return width; 1589 } 1590 1591 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1592 self.box.deinit(allocator); 1593 allocator.free(self.choices); 1594 } 1595 1596 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1597 self.clearGrid(); 1598 1599 if (self.choices.len > 1) { 1600 if (root_focus.grandchild_id) |id| { 1601 for (0..self.choices.len) |i| { 1602 if (id != self.selector(i).getFocus().id) continue; 1603 if (i != self.selected) try self.select(allocator, i); 1604 root_focus.setFocus(self.textInput().getFocus().id); 1605 break; 1606 } 1607 } 1608 1609 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 1610 for (0..self.choices.len) |i| { 1611 self.selector(i).options.border_style = if (i == self.selected) 1612 (if (focused) .double else .single) 1613 else 1614 .hidden; 1615 } 1616 } 1617 self.textInput().options.border_style = .single; 1618 self.textInput().cursor = 0; 1619 self.textInput().scroll_offset = 0; 1620 try self.box.build(allocator, constraint, root_focus); 1621 1622 if (!self.session.is_terminal) { 1623 const aa = self.session.arena.allocator(); 1624 const id = self.textInput().getFocus().id; 1625 try self.session.text_inputs.put(aa, id, self.textInput()); 1626 } 1627 } 1628 1629 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1630 switch (key) { 1631 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 1632 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 1633 .enter => if (self.session.is_terminal) self.show(), 1634 .mouse => |mouse| { 1635 if (self.choices.len > 1) for (0..self.choices.len) |i| { 1636 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 1637 try self.select(allocator, i); 1638 root_focus.setFocus(self.textInput().getFocus().id); 1639 return; 1640 }; 1641 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 1642 }, 1643 else => {}, 1644 } 1645 } 1646 1647 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 1648 self.selected = selected; 1649 const choice = self.choices[selected]; 1650 const text_input = self.textInput(); 1651 text_input.options.label = choice.label; 1652 text_input.options.bottom_label = choice.bottom_label; 1653 try text_input.setContent(allocator, choice.text); 1654 } 1655 1656 fn show(self: *CopyableText) void { 1657 const choice = self.choices[self.selected]; 1658 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 1659 } 1660 1661 fn textInput(self: *CopyableText) *wgt.TextInput { 1662 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 1663 } 1664 1665 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 1666 return &self.box.children.values()[index].widget.text_box; 1667 } 1668 1669 fn maxWidth(choices: []const Choice) usize { 1670 var width: usize = 0; 1671 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 1672 return width; 1673 } 1674 1675 pub fn clearGrid(self: *CopyableText) void { 1676 self.box.clearGrid(); 1677 } 1678 1679 pub fn getGrid(self: CopyableText) ?Grid { 1680 return self.box.getGrid(); 1681 } 1682 1683 pub fn getFocus(self: *CopyableText) *Focus { 1684 return self.box.getFocus(); 1685 } 1686 }; 1687