╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1921 1922 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1923 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1924 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1925 // snap such channels clear of that range so the backdrop doesn't blink or 1926 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1927 // actually render truecolor. 1928 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1929 const v = artRgb(color) orelse return color; 1930 const snap = struct { 1931 fn f(c: u8) u8 { 1932 return if (c >= 1 and c <= 9) 10 else c; 1933 } 1934 }.f; 1935 return .{ .rgb = .{ .r = snap(v.r), .g = snap(v.g), .b = snap(v.b) } }; 1936 } 1937 1938 // composites ANSI art behind a foreground grid 1939 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 1940 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1941 errdefer out.deinit(); 1942 1943 const anchor_x = foreground.size.width -| art_grid.size.width; 1944 for (0..art_grid.size.height) |y| { 1945 for (0..art_grid.size.width) |x| { 1946 const src = (try art_grid.cell(x, y)).*; 1947 if (src.rune == null) continue; 1948 const dst = try out.cell(anchor_x + x, y); 1949 const wide = x + 1 < art_grid.size.width and (try art_grid.cell(x + 1, y)).continuation; 1950 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 1951 // blank cells take the art; occupied cells take its background 1952 if (cellIsBlank(dst.*) and (if (next_dst) |next| cellIsBlank(next.*) else true)) { 1953 dst.style = src.style; 1954 dst.style.fg = sgrSafe(dst.style.fg); 1955 dst.style.bg = sgrSafe(dst.style.bg); 1956 try out.setRune(anchor_x + x, y, src.rune); 1957 } else { 1958 const art_bg = sgrSafe(src.style.bg orelse src.style.fg); 1959 dst.style.bg = art_bg; 1960 if (next_dst) |next| next.style.bg = art_bg; 1961 } 1962 } 1963 } 1964 return out; 1965 } 1966 }; 1967 1968 pub const CopyableText = struct { 1969 pub const Choice = struct { 1970 selector: []const u8 = "", 1971 text: []const u8, 1972 copyable_text: ?[]const u8 = null, 1973 label: []const u8 = "", 1974 bottom_label: []const u8 = "", 1975 }; 1976 1977 box: wgt.Box(Widget), 1978 session: *ui.Session, 1979 choices: []Choice, 1980 selected: usize = 0, 1981 1982 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice, initial: usize) !CopyableText { 1983 if (choices.len == 0) return error.MissingCopyableText; 1984 1985 const owned_choices = try allocator.dupe(Choice, choices); 1986 errdefer allocator.free(owned_choices); 1987 1988 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 1989 errdefer box.deinit(allocator); 1990 1991 if (choices.len > 1) for (choices) |choice| { 1992 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border = .hidden, .round_corners = true, .wrap_kind = .none }); 1993 errdefer selector_box.deinit(allocator); 1994 selector_box.getFocus().mode = .all; 1995 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 } }); 1996 }; 1997 1998 var text_input = try wgt.TextInput.init(allocator, .{ 1999 .border = .single, 2000 .round_corners = true, 2001 .top_label = .{ .text = choices[initial].label }, 2002 .bottom_label = .{ .text = choices[initial].bottom_label }, 2003 .read_only = true, 2004 .render_content = session.is_terminal, 2005 .visible_width = null, 2006 }); 2007 errdefer text_input.deinit(allocator); 2008 text_input.getFocus().mode = .all; 2009 try text_input.setContent(allocator, choices[initial].text); 2010 box.getFocus().child_id = text_input.getFocus().id; 2011 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 2012 2013 return .{ .box = box, .session = session, .choices = owned_choices, .selected = initial }; 2014 } 2015 2016 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, handle: ui.RepoHandle) !?CopyableText { 2017 const aa = session.page_arena.allocator(); 2018 var choices: [2]Choice = undefined; 2019 var count: usize = 0; 2020 var ssh_index: ?usize = null; 2021 switch (handle.location) { 2022 .repo => |identity| { 2023 if (session.data.git_http_port) |port| { 2024 const url = try aa.print("http://localhost:{d}/{s}", .{ port, identity }); 2025 choices[count] = .{ 2026 .selector = "http", 2027 .text = url, 2028 .copyable_text = try aa.print("git clone {s}", .{url}), 2029 .label = " clone ", 2030 }; 2031 count += 1; 2032 } 2033 if (session.data.git_ssh_port) |port| { 2034 ssh_index = count; 2035 const url = try aa.print("ssh://localhost:{d}/{s}", .{ port, identity }); 2036 choices[count] = .{ 2037 .selector = "ssh", 2038 .text = url, 2039 .copyable_text = try aa.print("{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 2040 .label = " clone ", 2041 }; 2042 count += 1; 2043 } 2044 }, 2045 .fork => |f| if (session.data.git_ssh_port) |port| { 2046 const url = try aa.print("ssh://localhost:{d}/{s}+{s}", .{ port, f.identity, f.id }); 2047 choices[count] = .{ 2048 .text = url, 2049 .copyable_text = try aa.print("{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 2050 .label = " clone ", 2051 }; 2052 count += 1; 2053 }, 2054 } 2055 if (count == 0) return null; 2056 // a viewer who can push starts on the ssh url 2057 const initial = if (handle.canWrite()) ssh_index orelse 0 else 0; 2058 return @as(?CopyableText, try init(allocator, session, choices[0..count], initial)); 2059 } 2060 2061 pub fn minWidth(self: *const CopyableText) usize { 2062 var width = maxWidth(self.choices) + 2; 2063 if (self.choices.len > 1) { 2064 for (self.choices) |choice| width += choice.selector.len + 2; 2065 } 2066 return width; 2067 } 2068 2069 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 2070 self.box.deinit(allocator); 2071 allocator.free(self.choices); 2072 } 2073 2074 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2075 self.clearGrid(); 2076 2077 if (self.choices.len > 1) { 2078 if (root_focus.grandchild_id) |id| { 2079 for (0..self.choices.len) |i| { 2080 if (id != self.selector(i).getFocus().id) continue; 2081 if (i != self.selected) try self.select(allocator, i); 2082 root_focus.setFocus(self.textInput().getFocus().id); 2083 break; 2084 } 2085 } 2086 2087 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 2088 for (0..self.choices.len) |i| { 2089 self.selector(i).options.border = if (i == self.selected) 2090 (if (focused) .double else .single) 2091 else 2092 .hidden; 2093 } 2094 } 2095 self.textInput().options.border = .single; 2096 self.textInput().cursor = 0; 2097 self.textInput().scroll_offset = 0; 2098 try self.box.build(allocator, constraint, root_focus); 2099 2100 if (!self.session.is_terminal) { 2101 const aa = self.session.arena.allocator(); 2102 const id = self.textInput().getFocus().id; 2103 try self.session.text_inputs.put(aa, id, self.textInput()); 2104 } 2105 } 2106 2107 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2108 switch (key) { 2109 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 2110 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 2111 .enter => if (self.session.is_terminal) self.show(), 2112 .mouse => |mouse| { 2113 if (self.choices.len > 1) for (0..self.choices.len) |i| { 2114 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 2115 try self.select(allocator, i); 2116 root_focus.setFocus(self.textInput().getFocus().id); 2117 return; 2118 }; 2119 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 2120 }, 2121 else => {}, 2122 } 2123 } 2124 2125 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 2126 self.selected = selected; 2127 const choice = self.choices[selected]; 2128 const text_input = self.textInput(); 2129 text_input.options.top_label.text = choice.label; 2130 text_input.options.bottom_label.text = choice.bottom_label; 2131 try text_input.setContent(allocator, choice.text); 2132 } 2133 2134 fn show(self: *CopyableText) void { 2135 const choice = self.choices[self.selected]; 2136 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 2137 } 2138 2139 fn textInput(self: *CopyableText) *wgt.TextInput { 2140 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 2141 } 2142 2143 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 2144 return &self.box.children.values()[index].widget.text_box; 2145 } 2146 2147 fn maxWidth(choices: []const Choice) usize { 2148 var width: usize = 0; 2149 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 2150 return width; 2151 } 2152 2153 pub fn clearGrid(self: *CopyableText) void { 2154 self.box.clearGrid(); 2155 } 2156 2157 pub fn getGrid(self: CopyableText) ?Grid { 2158 return self.box.getGrid(); 2159 } 2160 2161 pub fn getFocus(self: *CopyableText) *Focus { 2162 return self.box.getFocus(); 2163 } 2164 }; 2165