╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1813 const c = color orelse return null; 1814 return switch (c) { 1815 .rgb => |v| v, 1816 else => null, 1817 }; 1818 } 1819 1820 fn fadeColor(color: ?Grid.Color, backdrop: Grid.Color.Rgb) ?Grid.Color { 1821 const v = artRgb(color) orelse return color; 1822 const blend = struct { 1823 fn f(c: u8, b: u8) u8 { 1824 return @intCast((@as(u16, c) * art_opacity + @as(u16, b) * (100 - art_opacity)) / 100); 1825 } 1826 }.f; 1827 return .{ .rgb = .{ .r = blend(v.r, backdrop.r), .g = blend(v.g, backdrop.g), .b = blend(v.b, backdrop.b) } }; 1828 } 1829 1830 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1831 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1832 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1833 // snap such channels clear of that range so the backdrop doesn't blink or 1834 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1835 // actually render truecolor. 1836 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1837 const v = artRgb(color) orelse return color; 1838 const snap = struct { 1839 fn f(c: u8) u8 { 1840 return if (c >= 1 and c <= 9) 10 else c; 1841 } 1842 }.f; 1843 return .{ .rgb = .{ .r = snap(v.r), .g = snap(v.g), .b = snap(v.b) } }; 1844 } 1845 1846 // composites ANSI art behind a foreground grid 1847 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 1848 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1849 errdefer out.deinit(); 1850 1851 const anchor_x = foreground.size.width -| art_grid.size.width; 1852 for (0..art_grid.size.height) |y| { 1853 for (0..art_grid.size.width) |x| { 1854 const src = (try art_grid.cell(x, y)).*; 1855 if (src.rune == null) continue; 1856 const dst = try out.cell(anchor_x + x, y); 1857 const wide = x + 1 < art_grid.size.width and (try art_grid.cell(x + 1, y)).continuation; 1858 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 1859 // blank cells take the art; occupied cells take its background 1860 if (cellIsBlank(dst.*) and (if (next_dst) |next| cellIsBlank(next.*) else true)) { 1861 dst.style = src.style; 1862 dst.style.fg = sgrSafe(dst.style.fg); 1863 dst.style.bg = sgrSafe(dst.style.bg); 1864 try out.setRune(anchor_x + x, y, src.rune); 1865 } else { 1866 const art_bg = sgrSafe(src.style.bg orelse src.style.fg); 1867 dst.style.bg = art_bg; 1868 if (next_dst) |next| next.style.bg = art_bg; 1869 } 1870 } 1871 } 1872 return out; 1873 } 1874 }; 1875 1876 pub const CopyableText = struct { 1877 pub const Choice = struct { 1878 selector: []const u8 = "", 1879 text: []const u8, 1880 copyable_text: ?[]const u8 = null, 1881 label: []const u8 = "", 1882 bottom_label: []const u8 = "", 1883 }; 1884 1885 box: wgt.Box(Widget), 1886 session: *ui.Session, 1887 choices: []Choice, 1888 selected: usize = 0, 1889 1890 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice, initial: usize) !CopyableText { 1891 if (choices.len == 0) return error.MissingCopyableText; 1892 1893 const owned_choices = try allocator.dupe(Choice, choices); 1894 errdefer allocator.free(owned_choices); 1895 1896 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 1897 errdefer box.deinit(allocator); 1898 1899 if (choices.len > 1) for (choices) |choice| { 1900 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border = .hidden, .round_corners = true, .wrap_kind = .none }); 1901 errdefer selector_box.deinit(allocator); 1902 selector_box.getFocus().mode = .all; 1903 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 } }); 1904 }; 1905 1906 var text_input = try wgt.TextInput.init(allocator, .{ 1907 .border = .single, 1908 .round_corners = true, 1909 .top_label = .{ .text = choices[initial].label }, 1910 .bottom_label = .{ .text = choices[initial].bottom_label }, 1911 .read_only = true, 1912 .render_content = session.is_terminal, 1913 .visible_width = null, 1914 }); 1915 errdefer text_input.deinit(allocator); 1916 text_input.getFocus().mode = .all; 1917 try text_input.setContent(allocator, choices[initial].text); 1918 box.getFocus().child_id = text_input.getFocus().id; 1919 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1920 1921 return .{ .box = box, .session = session, .choices = owned_choices, .selected = initial }; 1922 } 1923 1924 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, handle: ui.RepoHandle) !?CopyableText { 1925 const aa = session.page_arena.allocator(); 1926 var choices: [2]Choice = undefined; 1927 var count: usize = 0; 1928 var ssh_index: ?usize = null; 1929 switch (handle.location) { 1930 .repo => |identity| { 1931 if (session.data.git_http_port) |port| { 1932 const url = try aa.print("http://localhost:{d}/{s}", .{ port, identity }); 1933 choices[count] = .{ 1934 .selector = "http", 1935 .text = url, 1936 .copyable_text = try aa.print("git clone {s}", .{url}), 1937 .label = " clone ", 1938 }; 1939 count += 1; 1940 } 1941 if (session.data.git_ssh_port) |port| { 1942 ssh_index = count; 1943 const url = try aa.print("ssh://localhost:{d}/{s}", .{ port, identity }); 1944 choices[count] = .{ 1945 .selector = "ssh", 1946 .text = url, 1947 .copyable_text = try aa.print("{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1948 .label = " clone ", 1949 }; 1950 count += 1; 1951 } 1952 }, 1953 .fork => |f| if (session.data.git_ssh_port) |port| { 1954 const url = try aa.print("ssh://localhost:{d}/{s}+{s}", .{ port, f.identity, f.id }); 1955 choices[count] = .{ 1956 .text = url, 1957 .copyable_text = try aa.print("{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1958 .label = " clone ", 1959 }; 1960 count += 1; 1961 }, 1962 } 1963 if (count == 0) return null; 1964 // a viewer who can push starts on the ssh url 1965 const initial = if (handle.canWrite()) ssh_index orelse 0 else 0; 1966 return @as(?CopyableText, try init(allocator, session, choices[0..count], initial)); 1967 } 1968 1969 pub fn minWidth(self: *const CopyableText) usize { 1970 var width = maxWidth(self.choices) + 2; 1971 if (self.choices.len > 1) { 1972 for (self.choices) |choice| width += choice.selector.len + 2; 1973 } 1974 return width; 1975 } 1976 1977 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1978 self.box.deinit(allocator); 1979 allocator.free(self.choices); 1980 } 1981 1982 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1983 self.clearGrid(); 1984 1985 if (self.choices.len > 1) { 1986 if (root_focus.grandchild_id) |id| { 1987 for (0..self.choices.len) |i| { 1988 if (id != self.selector(i).getFocus().id) continue; 1989 if (i != self.selected) try self.select(allocator, i); 1990 root_focus.setFocus(self.textInput().getFocus().id); 1991 break; 1992 } 1993 } 1994 1995 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 1996 for (0..self.choices.len) |i| { 1997 self.selector(i).options.border = if (i == self.selected) 1998 (if (focused) .double else .single) 1999 else 2000 .hidden; 2001 } 2002 } 2003 self.textInput().options.border = .single; 2004 self.textInput().cursor = 0; 2005 self.textInput().scroll_offset = 0; 2006 try self.box.build(allocator, constraint, root_focus); 2007 2008 if (!self.session.is_terminal) { 2009 const aa = self.session.arena.allocator(); 2010 const id = self.textInput().getFocus().id; 2011 try self.session.text_inputs.put(aa, id, self.textInput()); 2012 } 2013 } 2014 2015 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2016 switch (key) { 2017 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 2018 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 2019 .enter => if (self.session.is_terminal) self.show(), 2020 .mouse => |mouse| { 2021 if (self.choices.len > 1) for (0..self.choices.len) |i| { 2022 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 2023 try self.select(allocator, i); 2024 root_focus.setFocus(self.textInput().getFocus().id); 2025 return; 2026 }; 2027 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 2028 }, 2029 else => {}, 2030 } 2031 } 2032 2033 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 2034 self.selected = selected; 2035 const choice = self.choices[selected]; 2036 const text_input = self.textInput(); 2037 text_input.options.top_label.text = choice.label; 2038 text_input.options.bottom_label.text = choice.bottom_label; 2039 try text_input.setContent(allocator, choice.text); 2040 } 2041 2042 fn show(self: *CopyableText) void { 2043 const choice = self.choices[self.selected]; 2044 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 2045 } 2046 2047 fn textInput(self: *CopyableText) *wgt.TextInput { 2048 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 2049 } 2050 2051 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 2052 return &self.box.children.values()[index].widget.text_box; 2053 } 2054 2055 fn maxWidth(choices: []const Choice) usize { 2056 var width: usize = 0; 2057 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 2058 return width; 2059 } 2060 2061 pub fn clearGrid(self: *CopyableText) void { 2062 self.box.clearGrid(); 2063 } 2064 2065 pub fn getGrid(self: CopyableText) ?Grid { 2066 return self.box.getGrid(); 2067 } 2068 2069 pub fn getFocus(self: *CopyableText) *Focus { 2070 return self.box.getFocus(); 2071 } 2072 }; 2073