╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1921 .round_corners = true, 1922 .top_label = .{ .text = choices[initial].label }, 1923 .bottom_label = .{ .text = choices[initial].bottom_label }, 1924 .read_only = true, 1925 .render_content = session.is_terminal, 1926 .visible_width = null, 1927 }); 1928 errdefer text_input.deinit(allocator); 1929 text_input.getFocus().mode = .all; 1930 try text_input.setContent(allocator, choices[initial].text); 1931 box.getFocus().child_id = text_input.getFocus().id; 1932 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1933 1934 return .{ .box = box, .session = session, .choices = owned_choices, .selected = initial }; 1935 } 1936 1937 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, handle: ui.RepoHandle) !?CopyableText { 1938 const aa = session.page_arena.allocator(); 1939 var choices: [2]Choice = undefined; 1940 var count: usize = 0; 1941 var ssh_index: ?usize = null; 1942 switch (handle.location) { 1943 .repo => |identity| { 1944 if (session.data.git_http_port) |port| { 1945 const url = try aa.print("http://localhost:{d}/{s}", .{ port, identity }); 1946 choices[count] = .{ 1947 .selector = "http", 1948 .text = url, 1949 .copyable_text = try aa.print("git clone {s}", .{url}), 1950 .label = " clone ", 1951 }; 1952 count += 1; 1953 } 1954 if (session.data.git_ssh_port) |port| { 1955 ssh_index = count; 1956 const url = try aa.print("ssh://localhost:{d}/{s}", .{ port, identity }); 1957 choices[count] = .{ 1958 .selector = "ssh", 1959 .text = url, 1960 .copyable_text = try aa.print("{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1961 .label = " clone ", 1962 }; 1963 count += 1; 1964 } 1965 }, 1966 .fork => |f| if (session.data.git_ssh_port) |port| { 1967 const url = try aa.print("ssh://localhost:{d}/{s}+{s}", .{ port, f.identity, f.id }); 1968 choices[count] = .{ 1969 .text = url, 1970 .copyable_text = try aa.print("{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1971 .label = " clone ", 1972 }; 1973 count += 1; 1974 }, 1975 } 1976 if (count == 0) return null; 1977 // a viewer who can push starts on the ssh url 1978 const initial = if (handle.canWrite()) ssh_index orelse 0 else 0; 1979 return @as(?CopyableText, try init(allocator, session, choices[0..count], initial)); 1980 } 1981 1982 pub fn minWidth(self: *const CopyableText) usize { 1983 var width = maxWidth(self.choices) + 2; 1984 if (self.choices.len > 1) { 1985 for (self.choices) |choice| width += choice.selector.len + 2; 1986 } 1987 return width; 1988 } 1989 1990 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1991 self.box.deinit(allocator); 1992 allocator.free(self.choices); 1993 } 1994 1995 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1996 self.clearGrid(); 1997 1998 if (self.choices.len > 1) { 1999 if (root_focus.grandchild_id) |id| { 2000 for (0..self.choices.len) |i| { 2001 if (id != self.selector(i).getFocus().id) continue; 2002 if (i != self.selected) try self.select(allocator, i); 2003 root_focus.setFocus(self.textInput().getFocus().id); 2004 break; 2005 } 2006 } 2007 2008 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 2009 for (0..self.choices.len) |i| { 2010 self.selector(i).options.border = if (i == self.selected) 2011 (if (focused) .double else .single) 2012 else 2013 .hidden; 2014 } 2015 } 2016 self.textInput().options.border = .single; 2017 self.textInput().cursor = 0; 2018 self.textInput().scroll_offset = 0; 2019 try self.box.build(allocator, constraint, root_focus); 2020 2021 if (!self.session.is_terminal) { 2022 const aa = self.session.arena.allocator(); 2023 const id = self.textInput().getFocus().id; 2024 try self.session.text_inputs.put(aa, id, self.textInput()); 2025 } 2026 } 2027 2028 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2029 switch (key) { 2030 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 2031 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 2032 .enter => if (self.session.is_terminal) self.show(), 2033 .mouse => |mouse| { 2034 if (self.choices.len > 1) for (0..self.choices.len) |i| { 2035 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 2036 try self.select(allocator, i); 2037 root_focus.setFocus(self.textInput().getFocus().id); 2038 return; 2039 }; 2040 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 2041 }, 2042 else => {}, 2043 } 2044 } 2045 2046 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 2047 self.selected = selected; 2048 const choice = self.choices[selected]; 2049 const text_input = self.textInput(); 2050 text_input.options.top_label.text = choice.label; 2051 text_input.options.bottom_label.text = choice.bottom_label; 2052 try text_input.setContent(allocator, choice.text); 2053 } 2054 2055 fn show(self: *CopyableText) void { 2056 const choice = self.choices[self.selected]; 2057 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 2058 } 2059 2060 fn textInput(self: *CopyableText) *wgt.TextInput { 2061 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 2062 } 2063 2064 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 2065 return &self.box.children.values()[index].widget.text_box; 2066 } 2067 2068 fn maxWidth(choices: []const Choice) usize { 2069 var width: usize = 0; 2070 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 2071 return width; 2072 } 2073 2074 pub fn clearGrid(self: *CopyableText) void { 2075 self.box.clearGrid(); 2076 } 2077 2078 pub fn getGrid(self: CopyableText) ?Grid { 2079 return self.box.getGrid(); 2080 } 2081 2082 pub fn getFocus(self: *CopyableText) *Focus { 2083 return self.box.getFocus(); 2084 } 2085 }; 2086