╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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