╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1754 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 1755 // blank cells take the art; occupied cells take its background 1756 if (cellIsBlank(dst.*) and (if (next_dst) |next| cellIsBlank(next.*) else true)) { 1757 dst.style = src.style; 1758 dst.style.fg = sgrSafe(dst.style.fg); 1759 dst.style.bg = sgrSafe(dst.style.bg); 1760 try out.setRune(anchor_x + x, y, src.rune); 1761 } else { 1762 applyArtBackground(dst, src); 1763 if (next_dst) |next| applyArtBackground(next, src); 1764 } 1765 } 1766 } 1767 return out; 1768 } 1769 }; 1770 1771 pub const CopyableText = struct { 1772 pub const Choice = struct { 1773 selector: []const u8 = "", 1774 text: []const u8, 1775 copyable_text: ?[]const u8 = null, 1776 label: []const u8 = "", 1777 bottom_label: []const u8 = "", 1778 }; 1779 1780 box: wgt.Box(Widget), 1781 session: *ui.Session, 1782 choices: []Choice, 1783 selected: usize = 0, 1784 1785 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice) !CopyableText { 1786 if (choices.len == 0) return error.MissingCopyableText; 1787 1788 const owned_choices = try allocator.dupe(Choice, choices); 1789 errdefer allocator.free(owned_choices); 1790 1791 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1792 errdefer box.deinit(allocator); 1793 1794 if (choices.len > 1) for (choices) |choice| { 1795 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border_style = .hidden, .round_corners = true, .wrap_kind = .none }); 1796 errdefer selector_box.deinit(allocator); 1797 selector_box.getFocus().mode = .all; 1798 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 } }); 1799 }; 1800 1801 var text_input = try wgt.TextInput.init(allocator, .{ 1802 .border_style = .single, 1803 .round_corners = true, 1804 .label = choices[0].label, 1805 .bottom_label = choices[0].bottom_label, 1806 .read_only = true, 1807 .render_content = session.is_terminal, 1808 .visible_width = null, 1809 }); 1810 errdefer text_input.deinit(allocator); 1811 text_input.getFocus().mode = .all; 1812 try text_input.setContent(allocator, choices[0].text); 1813 box.getFocus().child_id = text_input.getFocus().id; 1814 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1815 1816 return .{ .box = box, .session = session, .choices = owned_choices }; 1817 } 1818 1819 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, location: ui.RoutablePage.RepoLocation) !?CopyableText { 1820 const aa = session.page_arena.allocator(); 1821 var choices: [2]Choice = undefined; 1822 var count: usize = 0; 1823 switch (location) { 1824 .repo => |identity| { 1825 if (session.data.git_http_port) |port| { 1826 const url = try std.fmt.allocPrint(aa, "http://localhost:{d}/repo/{s}", .{ port, identity }); 1827 choices[count] = .{ 1828 .selector = "http", 1829 .text = url, 1830 .copyable_text = try std.fmt.allocPrint(aa, "git clone {s}", .{url}), 1831 .label = " clone ", 1832 }; 1833 count += 1; 1834 } 1835 if (session.data.git_ssh_port) |port| { 1836 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}", .{ port, identity }); 1837 choices[count] = .{ 1838 .selector = "ssh", 1839 .text = url, 1840 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1841 .label = " clone ", 1842 }; 1843 count += 1; 1844 } 1845 }, 1846 .fork => |f| if (session.data.git_ssh_port) |port| { 1847 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/fork/{s}/patch:{s}", .{ port, f.identity, f.id }); 1848 choices[count] = .{ 1849 .text = url, 1850 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1851 .label = " clone ", 1852 }; 1853 count += 1; 1854 }, 1855 } 1856 if (count == 0) return null; 1857 return @as(?CopyableText, try init(allocator, session, choices[0..count])); 1858 } 1859 1860 pub fn minWidth(self: *const CopyableText) usize { 1861 var width = maxWidth(self.choices) + 2; 1862 if (self.choices.len > 1) { 1863 for (self.choices) |choice| width += choice.selector.len + 2; 1864 } 1865 return width; 1866 } 1867 1868 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1869 self.box.deinit(allocator); 1870 allocator.free(self.choices); 1871 } 1872 1873 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1874 self.clearGrid(); 1875 1876 if (self.choices.len > 1) { 1877 if (root_focus.grandchild_id) |id| { 1878 for (0..self.choices.len) |i| { 1879 if (id != self.selector(i).getFocus().id) continue; 1880 if (i != self.selected) try self.select(allocator, i); 1881 root_focus.setFocus(self.textInput().getFocus().id); 1882 break; 1883 } 1884 } 1885 1886 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 1887 for (0..self.choices.len) |i| { 1888 self.selector(i).options.border_style = if (i == self.selected) 1889 (if (focused) .double else .single) 1890 else 1891 .hidden; 1892 } 1893 } 1894 self.textInput().options.border_style = .single; 1895 self.textInput().cursor = 0; 1896 self.textInput().scroll_offset = 0; 1897 try self.box.build(allocator, constraint, root_focus); 1898 1899 if (!self.session.is_terminal) { 1900 const aa = self.session.arena.allocator(); 1901 const id = self.textInput().getFocus().id; 1902 try self.session.text_inputs.put(aa, id, self.textInput()); 1903 } 1904 } 1905 1906 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1907 switch (key) { 1908 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 1909 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 1910 .enter => if (self.session.is_terminal) self.show(), 1911 .mouse => |mouse| { 1912 if (self.choices.len > 1) for (0..self.choices.len) |i| { 1913 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 1914 try self.select(allocator, i); 1915 root_focus.setFocus(self.textInput().getFocus().id); 1916 return; 1917 }; 1918 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 1919 }, 1920 else => {}, 1921 } 1922 } 1923 1924 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 1925 self.selected = selected; 1926 const choice = self.choices[selected]; 1927 const text_input = self.textInput(); 1928 text_input.options.label = choice.label; 1929 text_input.options.bottom_label = choice.bottom_label; 1930 try text_input.setContent(allocator, choice.text); 1931 } 1932 1933 fn show(self: *CopyableText) void { 1934 const choice = self.choices[self.selected]; 1935 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 1936 } 1937 1938 fn textInput(self: *CopyableText) *wgt.TextInput { 1939 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 1940 } 1941 1942 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 1943 return &self.box.children.values()[index].widget.text_box; 1944 } 1945 1946 fn maxWidth(choices: []const Choice) usize { 1947 var width: usize = 0; 1948 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 1949 return width; 1950 } 1951 1952 pub fn clearGrid(self: *CopyableText) void { 1953 self.box.clearGrid(); 1954 } 1955 1956 pub fn getGrid(self: CopyableText) ?Grid { 1957 return self.box.getGrid(); 1958 } 1959 1960 pub fn getFocus(self: *CopyableText) *Focus { 1961 return self.box.getFocus(); 1962 } 1963 }; 1964