╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1288 .label = " clone ", 1289 }; 1290 count += 1; 1291 } 1292 }, 1293 .fork => |f| if (session.data.git_ssh_port) |port| { 1294 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/fork/{s}/patch:{s}", .{ port, f.identity, f.id }); 1295 choices[count] = .{ 1296 .text = url, 1297 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1298 .label = " clone ", 1299 }; 1300 count += 1; 1301 }, 1302 } 1303 if (count == 0) return null; 1304 return @as(?CopyableText, try init(allocator, session, choices[0..count])); 1305 } 1306 1307 pub fn minWidth(self: *const CopyableText) usize { 1308 var width = maxWidth(self.choices) + 2; 1309 if (self.choices.len > 1) { 1310 for (self.choices) |choice| width += choice.selector.len + 2; 1311 } 1312 return width; 1313 } 1314 1315 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1316 self.box.deinit(allocator); 1317 allocator.free(self.choices); 1318 } 1319 1320 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1321 self.clearGrid(); 1322 1323 if (self.choices.len > 1) { 1324 if (root_focus.grandchild_id) |id| { 1325 for (0..self.choices.len) |i| { 1326 if (id != self.selector(i).getFocus().id) continue; 1327 if (i != self.selected) try self.select(allocator, i); 1328 root_focus.setFocus(self.textInput().getFocus().id); 1329 break; 1330 } 1331 } 1332 1333 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 1334 for (0..self.choices.len) |i| { 1335 self.selector(i).options.border_style = if (i == self.selected) 1336 (if (focused) .double else .single) 1337 else 1338 .hidden; 1339 } 1340 } 1341 self.textInput().options.border_style = .single; 1342 self.textInput().cursor = 0; 1343 self.textInput().scroll_offset = 0; 1344 try self.box.build(allocator, constraint, root_focus); 1345 1346 if (!self.session.is_terminal) { 1347 const aa = self.session.arena.allocator(); 1348 const id = self.textInput().getFocus().id; 1349 try self.session.text_inputs.put(aa, id, self.textInput()); 1350 } 1351 } 1352 1353 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1354 switch (key) { 1355 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 1356 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 1357 .enter => if (self.session.is_terminal) self.show(), 1358 .mouse => |mouse| { 1359 if (self.choices.len > 1) for (0..self.choices.len) |i| { 1360 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 1361 try self.select(allocator, i); 1362 root_focus.setFocus(self.textInput().getFocus().id); 1363 return; 1364 }; 1365 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 1366 }, 1367 else => {}, 1368 } 1369 } 1370 1371 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 1372 self.selected = selected; 1373 const choice = self.choices[selected]; 1374 const text_input = self.textInput(); 1375 text_input.options.label = choice.label; 1376 text_input.options.bottom_label = choice.bottom_label; 1377 try text_input.setContent(allocator, choice.text); 1378 } 1379 1380 fn show(self: *CopyableText) void { 1381 const choice = self.choices[self.selected]; 1382 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 1383 } 1384 1385 fn textInput(self: *CopyableText) *wgt.TextInput { 1386 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 1387 } 1388 1389 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 1390 return &self.box.children.values()[index].widget.text_box; 1391 } 1392 1393 fn maxWidth(choices: []const Choice) usize { 1394 var width: usize = 0; 1395 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 1396 return width; 1397 } 1398 1399 pub fn clearGrid(self: *CopyableText) void { 1400 self.box.clearGrid(); 1401 } 1402 1403 pub fn getGrid(self: CopyableText) ?Grid { 1404 return self.box.getGrid(); 1405 } 1406 1407 pub fn getFocus(self: *CopyableText) *Focus { 1408 return self.box.getFocus(); 1409 } 1410 }; 1411