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