╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1489 choices: []Choice, 1490 selected: usize = 0, 1491 1492 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice) !CopyableText { 1493 if (choices.len == 0) return error.MissingCopyableText; 1494 1495 const owned_choices = try allocator.dupe(Choice, choices); 1496 errdefer allocator.free(owned_choices); 1497 1498 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1499 errdefer box.deinit(allocator); 1500 1501 if (choices.len > 1) for (choices) |choice| { 1502 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 1503 errdefer selector_box.deinit(allocator); 1504 selector_box.getFocus().mode = .all; 1505 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 } }); 1506 }; 1507 1508 var text_input = try wgt.TextInput.init(allocator, .{ 1509 .border_style = .single, 1510 .rounded_corners = true, 1511 .label = choices[0].label, 1512 .bottom_label = choices[0].bottom_label, 1513 .read_only = true, 1514 .render_content = session.is_terminal, 1515 .visible_width = null, 1516 }); 1517 errdefer text_input.deinit(allocator); 1518 text_input.getFocus().mode = .all; 1519 try text_input.setContent(allocator, choices[0].text); 1520 box.getFocus().child_id = text_input.getFocus().id; 1521 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1522 1523 return .{ .box = box, .session = session, .choices = owned_choices }; 1524 } 1525 1526 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, location: ui.RoutablePage.RepoLocation) !?CopyableText { 1527 const aa = session.page_arena.allocator(); 1528 var choices: [2]Choice = undefined; 1529 var count: usize = 0; 1530 switch (location) { 1531 .repo => |identity| { 1532 if (session.data.git_http_port) |port| { 1533 const url = try std.fmt.allocPrint(aa, "http://localhost:{d}/repo/{s}", .{ port, identity }); 1534 choices[count] = .{ 1535 .selector = "http", 1536 .text = url, 1537 .copyable_text = try std.fmt.allocPrint(aa, "git clone {s}", .{url}), 1538 .label = " clone ", 1539 }; 1540 count += 1; 1541 } 1542 if (session.data.git_ssh_port) |port| { 1543 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}", .{ port, identity }); 1544 choices[count] = .{ 1545 .selector = "ssh", 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 .fork => |f| if (session.data.git_ssh_port) |port| { 1554 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/fork/{s}/patch:{s}", .{ port, f.identity, f.id }); 1555 choices[count] = .{ 1556 .text = url, 1557 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1558 .label = " clone ", 1559 }; 1560 count += 1; 1561 }, 1562 } 1563 if (count == 0) return null; 1564 return @as(?CopyableText, try init(allocator, session, choices[0..count])); 1565 } 1566 1567 pub fn minWidth(self: *const CopyableText) usize { 1568 var width = maxWidth(self.choices) + 2; 1569 if (self.choices.len > 1) { 1570 for (self.choices) |choice| width += choice.selector.len + 2; 1571 } 1572 return width; 1573 } 1574 1575 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1576 self.box.deinit(allocator); 1577 allocator.free(self.choices); 1578 } 1579 1580 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1581 self.clearGrid(); 1582 1583 if (self.choices.len > 1) { 1584 if (root_focus.grandchild_id) |id| { 1585 for (0..self.choices.len) |i| { 1586 if (id != self.selector(i).getFocus().id) continue; 1587 if (i != self.selected) try self.select(allocator, i); 1588 root_focus.setFocus(self.textInput().getFocus().id); 1589 break; 1590 } 1591 } 1592 1593 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 1594 for (0..self.choices.len) |i| { 1595 self.selector(i).options.border_style = if (i == self.selected) 1596 (if (focused) .double else .single) 1597 else 1598 .hidden; 1599 } 1600 } 1601 self.textInput().options.border_style = .single; 1602 self.textInput().cursor = 0; 1603 self.textInput().scroll_offset = 0; 1604 try self.box.build(allocator, constraint, root_focus); 1605 1606 if (!self.session.is_terminal) { 1607 const aa = self.session.arena.allocator(); 1608 const id = self.textInput().getFocus().id; 1609 try self.session.text_inputs.put(aa, id, self.textInput()); 1610 } 1611 } 1612 1613 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1614 switch (key) { 1615 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 1616 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 1617 .enter => if (self.session.is_terminal) self.show(), 1618 .mouse => |mouse| { 1619 if (self.choices.len > 1) for (0..self.choices.len) |i| { 1620 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 1621 try self.select(allocator, i); 1622 root_focus.setFocus(self.textInput().getFocus().id); 1623 return; 1624 }; 1625 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 1626 }, 1627 else => {}, 1628 } 1629 } 1630 1631 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 1632 self.selected = selected; 1633 const choice = self.choices[selected]; 1634 const text_input = self.textInput(); 1635 text_input.options.label = choice.label; 1636 text_input.options.bottom_label = choice.bottom_label; 1637 try text_input.setContent(allocator, choice.text); 1638 } 1639 1640 fn show(self: *CopyableText) void { 1641 const choice = self.choices[self.selected]; 1642 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 1643 } 1644 1645 fn textInput(self: *CopyableText) *wgt.TextInput { 1646 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 1647 } 1648 1649 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 1650 return &self.box.children.values()[index].widget.text_box; 1651 } 1652 1653 fn maxWidth(choices: []const Choice) usize { 1654 var width: usize = 0; 1655 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 1656 return width; 1657 } 1658 1659 pub fn clearGrid(self: *CopyableText) void { 1660 self.box.clearGrid(); 1661 } 1662 1663 pub fn getGrid(self: CopyableText) ?Grid { 1664 return self.box.getGrid(); 1665 } 1666 1667 pub fn getFocus(self: *CopyableText) *Focus { 1668 return self.box.getFocus(); 1669 } 1670 }; 1671