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