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