╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1995 .text = url, 1996 .copyable_text = try aa.print("{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1997 .label = " clone ", 1998 }; 1999 count += 1; 2000 } 2001 }, 2002 .fork => |f| if (session.data.git_ssh_port) |port| { 2003 const url = try aa.print("ssh://localhost:{d}/{s}+{s}", .{ port, f.identity, f.id }); 2004 choices[count] = .{ 2005 .text = url, 2006 .copyable_text = try aa.print("{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 2007 .label = " clone ", 2008 }; 2009 count += 1; 2010 }, 2011 } 2012 if (count == 0) return null; 2013 // a viewer who can push starts on the ssh url 2014 const initial = if (handle.canWrite()) ssh_index orelse 0 else 0; 2015 return @as(?CopyableText, try init(allocator, session, choices[0..count], initial)); 2016 } 2017 2018 pub fn minWidth(self: *const CopyableText) usize { 2019 var width = maxWidth(self.choices) + 2; 2020 if (self.choices.len > 1) { 2021 for (self.choices) |choice| width += choice.selector.len + 2; 2022 } 2023 return width; 2024 } 2025 2026 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 2027 self.box.deinit(allocator); 2028 allocator.free(self.choices); 2029 } 2030 2031 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2032 self.clearGrid(); 2033 2034 if (self.choices.len > 1) { 2035 if (root_focus.grandchild_id) |id| { 2036 for (0..self.choices.len) |i| { 2037 if (id != self.selector(i).getFocus().id) continue; 2038 if (i != self.selected) try self.select(allocator, i); 2039 root_focus.setFocus(self.textInput().getFocus().id); 2040 break; 2041 } 2042 } 2043 2044 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 2045 for (0..self.choices.len) |i| { 2046 self.selector(i).options.border = if (i == self.selected) 2047 (if (focused) .double else .single) 2048 else 2049 .hidden; 2050 } 2051 } 2052 self.textInput().options.border = .single; 2053 self.textInput().cursor = 0; 2054 self.textInput().scroll_offset = 0; 2055 try self.box.build(allocator, constraint, root_focus); 2056 2057 if (!self.session.is_terminal) { 2058 const aa = self.session.arena.allocator(); 2059 const id = self.textInput().getFocus().id; 2060 try self.session.text_inputs.put(aa, id, self.textInput()); 2061 } 2062 } 2063 2064 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2065 switch (key) { 2066 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 2067 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 2068 .enter => if (self.session.is_terminal) self.show(), 2069 .mouse => |mouse| { 2070 if (self.choices.len > 1) for (0..self.choices.len) |i| { 2071 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 2072 try self.select(allocator, i); 2073 root_focus.setFocus(self.textInput().getFocus().id); 2074 return; 2075 }; 2076 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 2077 }, 2078 else => {}, 2079 } 2080 } 2081 2082 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 2083 self.selected = selected; 2084 const choice = self.choices[selected]; 2085 const text_input = self.textInput(); 2086 text_input.options.top_label.text = choice.label; 2087 text_input.options.bottom_label.text = choice.bottom_label; 2088 try text_input.setContent(allocator, choice.text); 2089 } 2090 2091 fn show(self: *CopyableText) void { 2092 const choice = self.choices[self.selected]; 2093 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 2094 } 2095 2096 fn textInput(self: *CopyableText) *wgt.TextInput { 2097 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 2098 } 2099 2100 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 2101 return &self.box.children.values()[index].widget.text_box; 2102 } 2103 2104 fn maxWidth(choices: []const Choice) usize { 2105 var width: usize = 0; 2106 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 2107 return width; 2108 } 2109 2110 pub fn clearGrid(self: *CopyableText) void { 2111 self.box.clearGrid(); 2112 } 2113 2114 pub fn getGrid(self: CopyableText) ?Grid { 2115 return self.box.getGrid(); 2116 } 2117 2118 pub fn getFocus(self: *CopyableText) *Focus { 2119 return self.box.getFocus(); 2120 } 2121 }; 2122