╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1906 1907 const anchor_x = foreground.size.width -| art_grid.size.width; 1908 for (0..art_grid.size.height) |y| { 1909 for (0..art_grid.size.width) |x| { 1910 const src = (try art_grid.cell(x, y)).*; 1911 if (src.rune == null) continue; 1912 const dst = try out.cell(anchor_x + x, y); 1913 const wide = x + 1 < art_grid.size.width and (try art_grid.cell(x + 1, y)).continuation; 1914 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 1915 // blank cells take the art; occupied cells take its background 1916 if (cellIsBlank(dst.*) and (if (next_dst) |next| cellIsBlank(next.*) else true)) { 1917 dst.style = src.style; 1918 dst.style.fg = sgrSafe(dst.style.fg); 1919 dst.style.bg = sgrSafe(dst.style.bg); 1920 try out.setRune(anchor_x + x, y, src.rune); 1921 } else { 1922 const art_bg = sgrSafe(src.style.bg orelse src.style.fg); 1923 dst.style.bg = art_bg; 1924 if (next_dst) |next| next.style.bg = art_bg; 1925 } 1926 } 1927 } 1928 return out; 1929 } 1930 }; 1931 1932 pub const CopyableText = struct { 1933 pub const Choice = struct { 1934 selector: []const u8 = "", 1935 text: []const u8, 1936 copyable_text: ?[]const u8 = null, 1937 label: []const u8 = "", 1938 bottom_label: []const u8 = "", 1939 }; 1940 1941 box: wgt.Box(Widget), 1942 session: *ui.Session, 1943 choices: []Choice, 1944 selected: usize = 0, 1945 1946 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice, initial: usize) !CopyableText { 1947 if (choices.len == 0) return error.MissingCopyableText; 1948 1949 const owned_choices = try allocator.dupe(Choice, choices); 1950 errdefer allocator.free(owned_choices); 1951 1952 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 1953 errdefer box.deinit(allocator); 1954 1955 if (choices.len > 1) for (choices) |choice| { 1956 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border = .hidden, .round_corners = true, .wrap_kind = .none }); 1957 errdefer selector_box.deinit(allocator); 1958 selector_box.getFocus().mode = .all; 1959 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 } }); 1960 }; 1961 1962 var text_input = try wgt.TextInput.init(allocator, .{ 1963 .border = .single, 1964 .round_corners = true, 1965 .top_label = .{ .text = choices[initial].label }, 1966 .bottom_label = .{ .text = choices[initial].bottom_label }, 1967 .read_only = true, 1968 .render_content = session.is_terminal, 1969 .visible_width = null, 1970 }); 1971 errdefer text_input.deinit(allocator); 1972 text_input.getFocus().mode = .all; 1973 try text_input.setContent(allocator, choices[initial].text); 1974 box.getFocus().child_id = text_input.getFocus().id; 1975 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1976 1977 return .{ .box = box, .session = session, .choices = owned_choices, .selected = initial }; 1978 } 1979 1980 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, handle: ui.RepoHandle) !?CopyableText { 1981 const aa = session.page_arena.allocator(); 1982 var choices: [2]Choice = undefined; 1983 var count: usize = 0; 1984 var ssh_index: ?usize = null; 1985 switch (handle.location) { 1986 .repo => |identity| { 1987 if (session.data.git_http_port) |port| { 1988 const url = try aa.print("http://localhost:{d}/{s}", .{ port, identity }); 1989 choices[count] = .{ 1990 .selector = "http", 1991 .text = url, 1992 .copyable_text = try aa.print("git clone {s}", .{url}), 1993 .label = " clone ", 1994 }; 1995 count += 1; 1996 } 1997 if (session.data.git_ssh_port) |port| { 1998 ssh_index = count; 1999 const url = try aa.print("ssh://localhost:{d}/{s}", .{ port, identity }); 2000 choices[count] = .{ 2001 .selector = "ssh", 2002 .text = url, 2003 .copyable_text = try aa.print("{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 2004 .label = " clone ", 2005 }; 2006 count += 1; 2007 } 2008 }, 2009 .fork => |f| if (session.data.git_ssh_port) |port| { 2010 const url = try aa.print("ssh://localhost:{d}/{s}+{s}", .{ port, f.identity, f.id }); 2011 choices[count] = .{ 2012 .text = url, 2013 .copyable_text = try aa.print("{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 2014 .label = " clone ", 2015 }; 2016 count += 1; 2017 }, 2018 } 2019 if (count == 0) return null; 2020 // a viewer who can push starts on the ssh url 2021 const initial = if (handle.canWrite()) ssh_index orelse 0 else 0; 2022 return @as(?CopyableText, try init(allocator, session, choices[0..count], initial)); 2023 } 2024 2025 pub fn minWidth(self: *const CopyableText) usize { 2026 var width = maxWidth(self.choices) + 2; 2027 if (self.choices.len > 1) { 2028 for (self.choices) |choice| width += choice.selector.len + 2; 2029 } 2030 return width; 2031 } 2032 2033 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 2034 self.box.deinit(allocator); 2035 allocator.free(self.choices); 2036 } 2037 2038 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2039 self.clearGrid(); 2040 2041 if (self.choices.len > 1) { 2042 if (root_focus.grandchild_id) |id| { 2043 for (0..self.choices.len) |i| { 2044 if (id != self.selector(i).getFocus().id) continue; 2045 if (i != self.selected) try self.select(allocator, i); 2046 root_focus.setFocus(self.textInput().getFocus().id); 2047 break; 2048 } 2049 } 2050 2051 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 2052 for (0..self.choices.len) |i| { 2053 self.selector(i).options.border = if (i == self.selected) 2054 (if (focused) .double else .single) 2055 else 2056 .hidden; 2057 } 2058 } 2059 self.textInput().options.border = .single; 2060 self.textInput().cursor = 0; 2061 self.textInput().scroll_offset = 0; 2062 try self.box.build(allocator, constraint, root_focus); 2063 2064 if (!self.session.is_terminal) { 2065 const aa = self.session.arena.allocator(); 2066 const id = self.textInput().getFocus().id; 2067 try self.session.text_inputs.put(aa, id, self.textInput()); 2068 } 2069 } 2070 2071 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2072 switch (key) { 2073 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 2074 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 2075 .enter => if (self.session.is_terminal) self.show(), 2076 .mouse => |mouse| { 2077 if (self.choices.len > 1) for (0..self.choices.len) |i| { 2078 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 2079 try self.select(allocator, i); 2080 root_focus.setFocus(self.textInput().getFocus().id); 2081 return; 2082 }; 2083 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 2084 }, 2085 else => {}, 2086 } 2087 } 2088 2089 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 2090 self.selected = selected; 2091 const choice = self.choices[selected]; 2092 const text_input = self.textInput(); 2093 text_input.options.top_label.text = choice.label; 2094 text_input.options.bottom_label.text = choice.bottom_label; 2095 try text_input.setContent(allocator, choice.text); 2096 } 2097 2098 fn show(self: *CopyableText) void { 2099 const choice = self.choices[self.selected]; 2100 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 2101 } 2102 2103 fn textInput(self: *CopyableText) *wgt.TextInput { 2104 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 2105 } 2106 2107 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 2108 return &self.box.children.values()[index].widget.text_box; 2109 } 2110 2111 fn maxWidth(choices: []const Choice) usize { 2112 var width: usize = 0; 2113 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 2114 return width; 2115 } 2116 2117 pub fn clearGrid(self: *CopyableText) void { 2118 self.box.clearGrid(); 2119 } 2120 2121 pub fn getGrid(self: CopyableText) ?Grid { 2122 return self.box.getGrid(); 2123 } 2124 2125 pub fn getFocus(self: *CopyableText) *Focus { 2126 return self.box.getFocus(); 2127 } 2128 }; 2129