╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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