╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1204 } else { 1205 applyArtBackground(dst, src); 1206 if (next_dst) |next| applyArtBackground(next, src); 1207 } 1208 } 1209 } 1210 return out; 1211 } 1212 }; 1213 1214 pub const CopyableText = struct { 1215 pub const Choice = struct { 1216 selector: []const u8 = "", 1217 text: []const u8, 1218 copyable_text: ?[]const u8 = null, 1219 label: []const u8 = "", 1220 bottom_label: []const u8 = "", 1221 }; 1222 1223 box: wgt.Box(Widget), 1224 session: *ui.Session, 1225 choices: []Choice, 1226 selected: usize = 0, 1227 1228 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice) !CopyableText { 1229 if (choices.len == 0) return error.MissingCopyableText; 1230 1231 const owned_choices = try allocator.dupe(Choice, choices); 1232 errdefer allocator.free(owned_choices); 1233 1234 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1235 errdefer box.deinit(allocator); 1236 1237 if (choices.len > 1) for (choices) |choice| { 1238 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border_style = .hidden, .wrap_kind = .none }); 1239 errdefer selector_box.deinit(allocator); 1240 selector_box.getFocus().focusable = true; 1241 try box.children.put(allocator, selector_box.getFocus().id, .{ .widget = .{ .text_box = selector_box }, .rect = null, .min_size = .{ .width = choice.selector.len + 2, .height = 3 } }); 1242 }; 1243 1244 var text_input = try wgt.TextInput.init(allocator, .{ 1245 .border_style = .single, 1246 .label = choices[0].label, 1247 .bottom_label = choices[0].bottom_label, 1248 .read_only = true, 1249 .render_content = session.is_terminal, 1250 .visible_width = null, 1251 }); 1252 errdefer text_input.deinit(allocator); 1253 text_input.getFocus().focusable = true; 1254 try text_input.setContent(allocator, choices[0].text); 1255 box.getFocus().child_id = text_input.getFocus().id; 1256 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1257 1258 return .{ .box = box, .session = session, .choices = owned_choices }; 1259 } 1260 1261 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, identity: []const u8) !CopyableText { 1262 const aa = session.page_arena.allocator(); 1263 var choices: [2]Choice = undefined; 1264 var count: usize = 0; 1265 if (session.data.git_http_port) |port| { 1266 const url = try std.fmt.allocPrint(aa, "http://localhost:{d}/repo/{s}", .{ port, identity }); 1267 choices[count] = .{ 1268 .selector = "http", 1269 .text = url, 1270 .copyable_text = try std.fmt.allocPrint(aa, "git clone {s}", .{url}), 1271 .label = " clone ", 1272 }; 1273 count += 1; 1274 } 1275 if (session.data.git_ssh_port) |port| { 1276 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}", .{ port, identity }); 1277 choices[count] = .{ 1278 .selector = "ssh", 1279 .text = url, 1280 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1281 .label = " clone ", 1282 }; 1283 count += 1; 1284 } 1285 if (count == 0) return error.MissingGitPort; 1286 return init(allocator, session, choices[0..count]); 1287 } 1288 1289 pub fn minWidth(self: *const CopyableText) usize { 1290 var width = maxWidth(self.choices) + 2; 1291 if (self.choices.len > 1) { 1292 for (self.choices) |choice| width += choice.selector.len + 2; 1293 } 1294 return width; 1295 } 1296 1297 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1298 self.box.deinit(allocator); 1299 allocator.free(self.choices); 1300 } 1301 1302 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1303 self.clearGrid(); 1304 1305 if (self.choices.len > 1) { 1306 if (root_focus.grandchild_id) |id| { 1307 for (0..self.choices.len) |i| { 1308 if (id != self.selector(i).getFocus().id) continue; 1309 if (i != self.selected) try self.select(allocator, i); 1310 root_focus.setFocus(self.textInput().getFocus().id); 1311 break; 1312 } 1313 } 1314 1315 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 1316 for (0..self.choices.len) |i| { 1317 self.selector(i).options.border_style = if (i == self.selected) 1318 (if (focused) .double else .single) 1319 else 1320 .hidden; 1321 } 1322 } 1323 self.textInput().options.border_style = .single; 1324 self.textInput().cursor = 0; 1325 self.textInput().scroll_offset = 0; 1326 try self.box.build(allocator, constraint, root_focus); 1327 1328 if (!self.session.is_terminal) { 1329 const aa = self.session.arena.allocator(); 1330 const id = self.textInput().getFocus().id; 1331 try self.session.text_inputs.put(aa, id, self.textInput()); 1332 } 1333 } 1334 1335 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1336 switch (key) { 1337 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 1338 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 1339 .enter => if (self.session.is_terminal) self.show(), 1340 .mouse => |mouse| { 1341 if (self.choices.len > 1) for (0..self.choices.len) |i| { 1342 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 1343 try self.select(allocator, i); 1344 root_focus.setFocus(self.textInput().getFocus().id); 1345 return; 1346 }; 1347 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 1348 }, 1349 else => {}, 1350 } 1351 } 1352 1353 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 1354 self.selected = selected; 1355 const choice = self.choices[selected]; 1356 const text_input = self.textInput(); 1357 text_input.options.label = choice.label; 1358 text_input.options.bottom_label = choice.bottom_label; 1359 try text_input.setContent(allocator, choice.text); 1360 } 1361 1362 fn show(self: *CopyableText) void { 1363 const choice = self.choices[self.selected]; 1364 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 1365 } 1366 1367 fn textInput(self: *CopyableText) *wgt.TextInput { 1368 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 1369 } 1370 1371 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 1372 return &self.box.children.values()[index].widget.text_box; 1373 } 1374 1375 fn maxWidth(choices: []const Choice) usize { 1376 var width: usize = 0; 1377 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 1378 return width; 1379 } 1380 1381 pub fn clearGrid(self: *CopyableText) void { 1382 self.box.clearGrid(); 1383 } 1384 1385 pub fn getGrid(self: CopyableText) ?Grid { 1386 return self.box.getGrid(); 1387 } 1388 1389 pub fn getFocus(self: *CopyableText) *Focus { 1390 return self.box.getFocus(); 1391 } 1392 }; 1393