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