╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1520 switch (location) { 1521 .repo => |identity| { 1522 if (session.data.git_http_port) |port| { 1523 const url = try std.fmt.allocPrint(aa, "http://localhost:{d}/repo/{s}", .{ port, identity }); 1524 choices[count] = .{ 1525 .selector = "http", 1526 .text = url, 1527 .copyable_text = try std.fmt.allocPrint(aa, "git clone {s}", .{url}), 1528 .label = " clone ", 1529 }; 1530 count += 1; 1531 } 1532 if (session.data.git_ssh_port) |port| { 1533 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}", .{ port, identity }); 1534 choices[count] = .{ 1535 .selector = "ssh", 1536 .text = url, 1537 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1538 .label = " clone ", 1539 }; 1540 count += 1; 1541 } 1542 }, 1543 .fork => |f| if (session.data.git_ssh_port) |port| { 1544 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/fork/{s}/patch:{s}", .{ port, f.identity, f.id }); 1545 choices[count] = .{ 1546 .text = url, 1547 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1548 .label = " clone ", 1549 }; 1550 count += 1; 1551 }, 1552 } 1553 if (count == 0) return null; 1554 return @as(?CopyableText, try init(allocator, session, choices[0..count])); 1555 } 1556 1557 pub fn minWidth(self: *const CopyableText) usize { 1558 var width = maxWidth(self.choices) + 2; 1559 if (self.choices.len > 1) { 1560 for (self.choices) |choice| width += choice.selector.len + 2; 1561 } 1562 return width; 1563 } 1564 1565 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1566 self.box.deinit(allocator); 1567 allocator.free(self.choices); 1568 } 1569 1570 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1571 self.clearGrid(); 1572 1573 if (self.choices.len > 1) { 1574 if (root_focus.grandchild_id) |id| { 1575 for (0..self.choices.len) |i| { 1576 if (id != self.selector(i).getFocus().id) continue; 1577 if (i != self.selected) try self.select(allocator, i); 1578 root_focus.setFocus(self.textInput().getFocus().id); 1579 break; 1580 } 1581 } 1582 1583 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 1584 for (0..self.choices.len) |i| { 1585 self.selector(i).options.border_style = if (i == self.selected) 1586 (if (focused) .double else .single) 1587 else 1588 .hidden; 1589 } 1590 } 1591 self.textInput().options.border_style = .single; 1592 self.textInput().cursor = 0; 1593 self.textInput().scroll_offset = 0; 1594 try self.box.build(allocator, constraint, root_focus); 1595 1596 if (!self.session.is_terminal) { 1597 const aa = self.session.arena.allocator(); 1598 const id = self.textInput().getFocus().id; 1599 try self.session.text_inputs.put(aa, id, self.textInput()); 1600 } 1601 } 1602 1603 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1604 switch (key) { 1605 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 1606 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 1607 .enter => if (self.session.is_terminal) self.show(), 1608 .mouse => |mouse| { 1609 if (self.choices.len > 1) for (0..self.choices.len) |i| { 1610 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 1611 try self.select(allocator, i); 1612 root_focus.setFocus(self.textInput().getFocus().id); 1613 return; 1614 }; 1615 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 1616 }, 1617 else => {}, 1618 } 1619 } 1620 1621 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 1622 self.selected = selected; 1623 const choice = self.choices[selected]; 1624 const text_input = self.textInput(); 1625 text_input.options.label = choice.label; 1626 text_input.options.bottom_label = choice.bottom_label; 1627 try text_input.setContent(allocator, choice.text); 1628 } 1629 1630 fn show(self: *CopyableText) void { 1631 const choice = self.choices[self.selected]; 1632 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 1633 } 1634 1635 fn textInput(self: *CopyableText) *wgt.TextInput { 1636 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 1637 } 1638 1639 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 1640 return &self.box.children.values()[index].widget.text_box; 1641 } 1642 1643 fn maxWidth(choices: []const Choice) usize { 1644 var width: usize = 0; 1645 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 1646 return width; 1647 } 1648 1649 pub fn clearGrid(self: *CopyableText) void { 1650 self.box.clearGrid(); 1651 } 1652 1653 pub fn getGrid(self: CopyableText) ?Grid { 1654 return self.box.getGrid(); 1655 } 1656 1657 pub fn getFocus(self: *CopyableText) *Focus { 1658 return self.box.getFocus(); 1659 } 1660 }; 1661