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