╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
2178 for (self.choices) |choice| width += choice.selector.len + 2; 2179 } 2180 return width; 2181 } 2182 2183 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 2184 self.box.deinit(allocator); 2185 allocator.free(self.choices); 2186 } 2187 2188 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2189 self.clearGrid(); 2190 2191 if (self.choices.len > 1) { 2192 if (root_focus.grandchild_id) |id| { 2193 for (0..self.choices.len) |i| { 2194 if (id != self.selector(i).getFocus().id) continue; 2195 if (i != self.selected) try self.select(allocator, i); 2196 root_focus.setFocus(self.textInput().getFocus().id); 2197 break; 2198 } 2199 } 2200 2201 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 2202 for (0..self.choices.len) |i| { 2203 self.selector(i).options.border = if (i == self.selected) 2204 (if (focused) .double else .single) 2205 else 2206 .hidden; 2207 } 2208 } 2209 self.textInput().options.border = .single; 2210 self.textInput().cursor = 0; 2211 self.textInput().scroll_offset = 0; 2212 try self.box.build(allocator, constraint, root_focus); 2213 2214 if (!self.session.is_terminal) { 2215 const aa = self.session.arena.allocator(); 2216 const id = self.textInput().getFocus().id; 2217 try self.session.text_inputs.put(aa, id, self.textInput()); 2218 } 2219 } 2220 2221 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2222 switch (key) { 2223 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 2224 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 2225 .enter => if (self.session.is_terminal) self.show(), 2226 .mouse => |mouse| { 2227 if (self.choices.len > 1) for (0..self.choices.len) |i| { 2228 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 2229 try self.select(allocator, i); 2230 root_focus.setFocus(self.textInput().getFocus().id); 2231 return; 2232 }; 2233 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 2234 }, 2235 else => {}, 2236 } 2237 } 2238 2239 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 2240 self.selected = selected; 2241 const choice = self.choices[selected]; 2242 const text_input = self.textInput(); 2243 text_input.options.top_label.text = choice.label; 2244 text_input.options.bottom_label.text = choice.bottom_label; 2245 try text_input.setContent(allocator, choice.text); 2246 } 2247 2248 fn show(self: *CopyableText) void { 2249 const choice = self.choices[self.selected]; 2250 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 2251 } 2252 2253 fn textInput(self: *CopyableText) *wgt.TextInput { 2254 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 2255 } 2256 2257 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 2258 return &self.box.children.values()[index].widget.text_box; 2259 } 2260 2261 fn maxWidth(choices: []const Choice) usize { 2262 var width: usize = 0; 2263 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 2264 return width; 2265 } 2266 2267 pub fn clearGrid(self: *CopyableText) void { 2268 self.box.clearGrid(); 2269 } 2270 2271 pub fn getGrid(self: CopyableText) ?Grid { 2272 return self.box.getGrid(); 2273 } 2274 2275 pub fn getFocus(self: *CopyableText) *Focus { 2276 return self.box.getFocus(); 2277 } 2278 }; 2279