╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1921 } 1922 1923 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1924 self.box.deinit(allocator); 1925 allocator.free(self.choices); 1926 } 1927 1928 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1929 self.clearGrid(); 1930 1931 if (self.choices.len > 1) { 1932 if (root_focus.grandchild_id) |id| { 1933 for (0..self.choices.len) |i| { 1934 if (id != self.selector(i).getFocus().id) continue; 1935 if (i != self.selected) try self.select(allocator, i); 1936 root_focus.setFocus(self.textInput().getFocus().id); 1937 break; 1938 } 1939 } 1940 1941 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 1942 for (0..self.choices.len) |i| { 1943 self.selector(i).options.border_style = if (i == self.selected) 1944 (if (focused) .double else .single) 1945 else 1946 .hidden; 1947 } 1948 } 1949 self.textInput().options.border_style = .single; 1950 self.textInput().cursor = 0; 1951 self.textInput().scroll_offset = 0; 1952 try self.box.build(allocator, constraint, root_focus); 1953 1954 if (!self.session.is_terminal) { 1955 const aa = self.session.arena.allocator(); 1956 const id = self.textInput().getFocus().id; 1957 try self.session.text_inputs.put(aa, id, self.textInput()); 1958 } 1959 } 1960 1961 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1962 switch (key) { 1963 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 1964 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 1965 .enter => if (self.session.is_terminal) self.show(), 1966 .mouse => |mouse| { 1967 if (self.choices.len > 1) for (0..self.choices.len) |i| { 1968 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 1969 try self.select(allocator, i); 1970 root_focus.setFocus(self.textInput().getFocus().id); 1971 return; 1972 }; 1973 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 1974 }, 1975 else => {}, 1976 } 1977 } 1978 1979 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 1980 self.selected = selected; 1981 const choice = self.choices[selected]; 1982 const text_input = self.textInput(); 1983 text_input.options.label = choice.label; 1984 text_input.options.bottom_label = choice.bottom_label; 1985 try text_input.setContent(allocator, choice.text); 1986 } 1987 1988 fn show(self: *CopyableText) void { 1989 const choice = self.choices[self.selected]; 1990 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 1991 } 1992 1993 fn textInput(self: *CopyableText) *wgt.TextInput { 1994 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 1995 } 1996 1997 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 1998 return &self.box.children.values()[index].widget.text_box; 1999 } 2000 2001 fn maxWidth(choices: []const Choice) usize { 2002 var width: usize = 0; 2003 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 2004 return width; 2005 } 2006 2007 pub fn clearGrid(self: *CopyableText) void { 2008 self.box.clearGrid(); 2009 } 2010 2011 pub fn getGrid(self: CopyableText) ?Grid { 2012 return self.box.getGrid(); 2013 } 2014 2015 pub fn getFocus(self: *CopyableText) *Focus { 2016 return self.box.getFocus(); 2017 } 2018 }; 2019