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