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