╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1623 .g = @intCast(@as(u16, v.g) * art_brightness / 100), 1624 .b = @intCast(@as(u16, v.b) * art_brightness / 100), 1625 } }; 1626 } 1627 1628 fn buildArt(self: *AnsiBackground, allocator: std.mem.Allocator, size: layout.Size, root_focus: *Focus) !void { 1629 try self.art.build(allocator, .{ 1630 .min_size = .{ .width = null, .height = null }, 1631 .max_size = .{ .width = size.width, .height = size.height }, 1632 }, root_focus); 1633 if (self.art.grid) |*grid| { 1634 for (grid.cells) |*cell| { 1635 cell.style.fg = dimColor(cell.style.fg); 1636 cell.style.bg = dimColor(cell.style.bg); 1637 } 1638 } 1639 } 1640 1641 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1642 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1643 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1644 // snap such channels clear of that range so the backdrop doesn't blink or 1645 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1646 // actually render truecolor. 1647 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1648 const v = artRgb(color) orelse return color; 1649 const snap = struct { 1650 fn f(c: u8) u8 { 1651 return if (c >= 1 and c <= 9) 10 else c; 1652 } 1653 }.f; 1654 return .{ .rgb = .{ .r = snap(v.r), .g = snap(v.g), .b = snap(v.b) } }; 1655 } 1656 1657 fn applyArtBackground(dst: *Grid.Cell, src: Grid.Cell) void { 1658 const background_maybe = sgrSafe(src.style.bg orelse src.style.fg); 1659 dst.style.bg = background_maybe; 1660 // change unstyled text to contrast with the art behind it 1661 if (dst.style.fg == null) { 1662 if (artRgb(background_maybe)) |v| { 1663 // use near-black and near-white, which is easier on the eyes 1664 // while retaining high contrast 1665 const luminance = (@as(u32, v.r) * 299 + 1666 @as(u32, v.g) * 587 + 1667 @as(u32, v.b) * 114) / 1000; 1668 dst.style.fg = if (luminance >= 128) 1669 .{ .rgb = .{ .r = 16, .g = 16, .b = 16 } } 1670 else 1671 .{ .rgb = .{ .r = 240, .g = 240, .b = 240 } }; 1672 } 1673 } 1674 } 1675 1676 // composites ANSI art behind a foreground grid 1677 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 1678 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1679 errdefer out.deinit(); 1680 1681 const anchor_x = foreground.size.width -| art_grid.size.width; 1682 for (0..art_grid.size.height) |y| { 1683 for (0..art_grid.size.width) |x| { 1684 const src = (try art_grid.cell(x, y)).*; 1685 if (src.rune == null) continue; 1686 const dst = try out.cell(anchor_x + x, y); 1687 const wide = x + 1 < art_grid.size.width and (try art_grid.cell(x + 1, y)).continuation; 1688 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 1689 // blank cells take the art; occupied cells take its background 1690 if (cellIsBlank(dst.*) and (if (next_dst) |next| cellIsBlank(next.*) else true)) { 1691 dst.style = src.style; 1692 dst.style.fg = sgrSafe(dst.style.fg); 1693 dst.style.bg = sgrSafe(dst.style.bg); 1694 try out.setRune(anchor_x + x, y, src.rune); 1695 } else { 1696 applyArtBackground(dst, src); 1697 if (next_dst) |next| applyArtBackground(next, src); 1698 } 1699 } 1700 } 1701 return out; 1702 } 1703 }; 1704 1705 pub const CopyableText = struct { 1706 pub const Choice = struct { 1707 selector: []const u8 = "", 1708 text: []const u8, 1709 copyable_text: ?[]const u8 = null, 1710 label: []const u8 = "", 1711 bottom_label: []const u8 = "", 1712 }; 1713 1714 box: wgt.Box(Widget), 1715 session: *ui.Session, 1716 choices: []Choice, 1717 selected: usize = 0, 1718 1719 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice) !CopyableText { 1720 if (choices.len == 0) return error.MissingCopyableText; 1721 1722 const owned_choices = try allocator.dupe(Choice, choices); 1723 errdefer allocator.free(owned_choices); 1724 1725 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1726 errdefer box.deinit(allocator); 1727 1728 if (choices.len > 1) for (choices) |choice| { 1729 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border_style = .hidden, .round_corners = true, .wrap_kind = .none }); 1730 errdefer selector_box.deinit(allocator); 1731 selector_box.getFocus().mode = .all; 1732 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 } }); 1733 }; 1734 1735 var text_input = try wgt.TextInput.init(allocator, .{ 1736 .border_style = .single, 1737 .round_corners = true, 1738 .label = choices[0].label, 1739 .bottom_label = choices[0].bottom_label, 1740 .read_only = true, 1741 .render_content = session.is_terminal, 1742 .visible_width = null, 1743 }); 1744 errdefer text_input.deinit(allocator); 1745 text_input.getFocus().mode = .all; 1746 try text_input.setContent(allocator, choices[0].text); 1747 box.getFocus().child_id = text_input.getFocus().id; 1748 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1749 1750 return .{ .box = box, .session = session, .choices = owned_choices }; 1751 } 1752 1753 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, location: ui.RoutablePage.RepoLocation) !?CopyableText { 1754 const aa = session.page_arena.allocator(); 1755 var choices: [2]Choice = undefined; 1756 var count: usize = 0; 1757 switch (location) { 1758 .repo => |identity| { 1759 if (session.data.git_http_port) |port| { 1760 const url = try std.fmt.allocPrint(aa, "http://localhost:{d}/repo/{s}", .{ port, identity }); 1761 choices[count] = .{ 1762 .selector = "http", 1763 .text = url, 1764 .copyable_text = try std.fmt.allocPrint(aa, "git clone {s}", .{url}), 1765 .label = " clone ", 1766 }; 1767 count += 1; 1768 } 1769 if (session.data.git_ssh_port) |port| { 1770 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}", .{ port, identity }); 1771 choices[count] = .{ 1772 .selector = "ssh", 1773 .text = url, 1774 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1775 .label = " clone ", 1776 }; 1777 count += 1; 1778 } 1779 }, 1780 .fork => |f| if (session.data.git_ssh_port) |port| { 1781 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/fork/{s}/patch:{s}", .{ port, f.identity, f.id }); 1782 choices[count] = .{ 1783 .text = url, 1784 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1785 .label = " clone ", 1786 }; 1787 count += 1; 1788 }, 1789 } 1790 if (count == 0) return null; 1791 return @as(?CopyableText, try init(allocator, session, choices[0..count])); 1792 } 1793 1794 pub fn minWidth(self: *const CopyableText) usize { 1795 var width = maxWidth(self.choices) + 2; 1796 if (self.choices.len > 1) { 1797 for (self.choices) |choice| width += choice.selector.len + 2; 1798 } 1799 return width; 1800 } 1801 1802 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1803 self.box.deinit(allocator); 1804 allocator.free(self.choices); 1805 } 1806 1807 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1808 self.clearGrid(); 1809 1810 if (self.choices.len > 1) { 1811 if (root_focus.grandchild_id) |id| { 1812 for (0..self.choices.len) |i| { 1813 if (id != self.selector(i).getFocus().id) continue; 1814 if (i != self.selected) try self.select(allocator, i); 1815 root_focus.setFocus(self.textInput().getFocus().id); 1816 break; 1817 } 1818 } 1819 1820 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 1821 for (0..self.choices.len) |i| { 1822 self.selector(i).options.border_style = if (i == self.selected) 1823 (if (focused) .double else .single) 1824 else 1825 .hidden; 1826 } 1827 } 1828 self.textInput().options.border_style = .single; 1829 self.textInput().cursor = 0; 1830 self.textInput().scroll_offset = 0; 1831 try self.box.build(allocator, constraint, root_focus); 1832 1833 if (!self.session.is_terminal) { 1834 const aa = self.session.arena.allocator(); 1835 const id = self.textInput().getFocus().id; 1836 try self.session.text_inputs.put(aa, id, self.textInput()); 1837 } 1838 } 1839 1840 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1841 switch (key) { 1842 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 1843 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 1844 .enter => if (self.session.is_terminal) self.show(), 1845 .mouse => |mouse| { 1846 if (self.choices.len > 1) for (0..self.choices.len) |i| { 1847 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 1848 try self.select(allocator, i); 1849 root_focus.setFocus(self.textInput().getFocus().id); 1850 return; 1851 }; 1852 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 1853 }, 1854 else => {}, 1855 } 1856 } 1857 1858 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 1859 self.selected = selected; 1860 const choice = self.choices[selected]; 1861 const text_input = self.textInput(); 1862 text_input.options.label = choice.label; 1863 text_input.options.bottom_label = choice.bottom_label; 1864 try text_input.setContent(allocator, choice.text); 1865 } 1866 1867 fn show(self: *CopyableText) void { 1868 const choice = self.choices[self.selected]; 1869 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 1870 } 1871 1872 fn textInput(self: *CopyableText) *wgt.TextInput { 1873 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 1874 } 1875 1876 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 1877 return &self.box.children.values()[index].widget.text_box; 1878 } 1879 1880 fn maxWidth(choices: []const Choice) usize { 1881 var width: usize = 0; 1882 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 1883 return width; 1884 } 1885 1886 pub fn clearGrid(self: *CopyableText) void { 1887 self.box.clearGrid(); 1888 } 1889 1890 pub fn getGrid(self: CopyableText) ?Grid { 1891 return self.box.getGrid(); 1892 } 1893 1894 pub fn getFocus(self: *CopyableText) *Focus { 1895 return self.box.getFocus(); 1896 } 1897 }; 1898