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