╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
764 const cell = try grid.cell(x, y); 765 cell.style = self.styles[self.span_of[i]]; 766 if (link) |l| { 767 cell.link = terminalLink(self.links[l]); 768 if (l == focused_link) cell.style.inverted = true; 769 // extend this line's piece of the link, or start one 770 const items = self.pieces.items; 771 if (items.len > 0 and items[items.len - 1].link == l and items[items.len - 1].rect.y == y) { 772 items[items.len - 1].rect.size.width += rune_width; 773 } else { 774 try self.pieces.append(allocator, .{ .link = l, .rect = .{ .x = x, .y = y, .size = .{ .width = rune_width, .height = 1 } } }); 775 } 776 } 777 try grid.setRune(x, y, rune); 778 x += rune_width; 779 } 780 } 781 782 // a link's pieces are consecutive, since its runes are 783 var piece_index: usize = 0; 784 for (self.pieces.items, 0..) |piece, i| { 785 piece_index = if (i > 0 and self.pieces.items[i - 1].link == piece.link) piece_index + 1 else 0; 786 const focuses = &self.link_focuses[piece.link]; 787 if (piece_index == focuses.items.len) { 788 const focus = try Focus.create(allocator, .{ .custom = self.links[piece.link] }); 789 errdefer focus.destroy(allocator); 790 focus.mode = .all; 791 try focuses.append(allocator, focus); 792 } 793 try self.focus.addChild(allocator, focuses.items[piece_index], piece.rect.size, piece.rect.x, piece.rect.y); 794 } 795 796 // focus rests on a link's first piece, which stays while the link shows 797 if (focused_link) |l| if (self.link_focuses[l].items.len > 0) { 798 const first = self.link_focuses[l].items[0].id; 799 if (self.focus.children.contains(first)) { 800 root_focus.grandchild_id = first; 801 self.focus.child_id = first; 802 } 803 }; 804 805 self.grid = grid; 806 } 807 808 pub fn input(self: *RichText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 809 _ = self; 810 _ = allocator; 811 _ = key; 812 _ = root_focus; 813 } 814 815 pub fn clearGrid(self: *RichText) void { 816 if (self.grid) |*grid| { 817 grid.deinit(); 818 self.grid = null; 819 } 820 } 821 822 pub fn getGrid(self: RichText) ?Grid { 823 return self.grid; 824 } 825 826 pub fn getFocus(self: *RichText) *Focus { 827 return self.focus; 828 } 829 }; 830 831 // an invisible widget that fills the horizontal space granted by its parent. 832 // used inside Box(horiz) with a min_size so the box reserves space for the 833 // children that follow, pushing them to the right. 834 pub const Spacer = struct { 835 focus: *Focus, 836 grid: ?Grid, 837 838 pub fn init(allocator: std.mem.Allocator) !Spacer { 839 return .{ 840 .focus = try Focus.create(allocator, .container), 841 .grid = null, 842 }; 843 } 844 845 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 846 self.focus.destroy(allocator); 847 if (self.grid) |*grid| { 848 grid.deinit(); 849 self.grid = null; 850 } 851 } 852 853 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 854 _ = root_focus; 855 self.clearGrid(); 856 const width = constraint.max_size.width orelse return; 857 if (width == 0) return; 858 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 859 } 860 861 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 862 _ = self; 863 _ = allocator; 864 _ = key; 865 _ = root_focus; 866 } 867 868 pub fn clearGrid(self: *Spacer) void { 869 if (self.grid) |*grid| { 870 grid.deinit(); 871 self.grid = null; 872 } 873 } 874 875 pub fn getGrid(self: Spacer) ?Grid { 876 return self.grid; 877 } 878 879 pub fn getFocus(self: *Spacer) *Focus { 880 return self.focus; 881 } 882 }; 883 884 // a sub-header's search box: a labeled text input whose enter the owning view 885 // turns into a navigation. shared by the files, commits and thread tabs. 886 pub const SearchBox = struct { 887 text_input: wgt.TextInput, 888 session: *ui.Session, 889 890 // the room the box asks its row for 891 pub const min_size: layout.MaybeSize = .{ .width = 20, .height = 3 }; 892 893 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, label: []const u8, name: []const u8, text_value: ?[]const u8) !SearchBox { 894 var text_input = try wgt.TextInput.init(allocator, .{ .label = label, .name = name, .round_corners = true, .visible_width = 18, .render_content = session.is_terminal }); 895 errdefer text_input.deinit(allocator); 896 text_input.getFocus().mode = .all; 897 if (text_value) |value| try text_input.setContent(allocator, value); 898 return .{ .text_input = text_input, .session = session }; 899 } 900 901 pub fn deinit(self: *SearchBox, allocator: std.mem.Allocator) void { 902 self.text_input.deinit(allocator); 903 } 904 905 pub fn build(self: *SearchBox, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 906 self.text_input.options.bottom_label = if (root_focus.grandchild_id == self.getFocus().id) " press enter " else ""; 907 try self.text_input.build(allocator, constraint, root_focus); 908 if (!self.session.is_terminal) { 909 try self.session.text_inputs.put(self.session.arena.allocator(), self.getFocus().id, &self.text_input); 910 } 911 } 912 913 pub fn input(self: *SearchBox, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 914 try self.text_input.input(allocator, key, root_focus); 915 } 916 917 // the typed text; the caller owns it 918 pub fn text(self: *SearchBox, allocator: std.mem.Allocator) ![]u8 { 919 return self.text_input.text(allocator); 920 } 921 922 // whether the cursor sits past the last character, where right-arrow leaves 923 // the box rather than moving within it 924 pub fn atEnd(self: *SearchBox) bool { 925 return self.text_input.cursor == self.text_input.content.items.len; 926 } 927 928 pub fn clearGrid(self: *SearchBox) void { 929 self.text_input.clearGrid(); 930 } 931 932 pub fn getGrid(self: SearchBox) ?Grid { 933 return self.text_input.getGrid(); 934 } 935 936 pub fn getFocus(self: *SearchBox) *Focus { 937 return self.text_input.getFocus(); 938 } 939 }; 940 941 // a tab's sub-header: an optional search box on the left, then a spacer, then 942 // the clone url on the right. the owning view turns a submitted query into a 943 // route, so route building stays with the tab that knows its own url. 944 pub const SearchHeader = struct { 945 box: wgt.Box(Widget), 946 session: *ui.Session, 947 948 pub fn init( 949 allocator: std.mem.Allocator, 950 session: *ui.Session, 951 location: ui.RoutablePage.RepoLocation, 952 label: []const u8, 953 name: []const u8, 954 text: ?[]const u8, 955 show_input: bool, 956 ) !SearchHeader { 957 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 958 errdefer box.deinit(allocator); 959 960 // the search box only appears where an index can serve it. 961 if (show_input) { 962 var search_box = try SearchBox.init(allocator, session, label, name, text); 963 errdefer search_box.deinit(allocator); 964 box.getFocus().child_id = search_box.getFocus().id; 965 try box.children.put(allocator, search_box.getFocus().id, .{ .widget = .{ .search_box = search_box }, .rect = null, .min_size = SearchBox.min_size }); 966 } 967 968 // the spacer pushes the clone url to the right edge. 969 { 970 var spacer = try Spacer.init(allocator); 971 errdefer spacer.deinit(allocator); 972 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 973 } 974 975 if (session.data.host_kind == .server) { 976 if (try CopyableText.initClone(allocator, session, location)) |value| { 977 var clone_url = value; 978 errdefer clone_url.deinit(allocator); 979 const min_width = clone_url.minWidth(); 980 if (box.getFocus().child_id == null) box.getFocus().child_id = clone_url.getFocus().id; 981 try box.children.put(allocator, clone_url.getFocus().id, .{ .widget = .{ .copyable_text = clone_url }, .rect = null, .min_size = .{ .width = min_width, .height = 3 }, .max_size = .{ .width = min_width, .height = 3 } }); 982 } 983 } 984 985 return .{ .box = box, .session = session }; 986 } 987 988 pub fn deinit(self: *SearchHeader, allocator: std.mem.Allocator) void { 989 self.box.deinit(allocator); 990 } 991 992 pub fn build(self: *SearchHeader, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 993 self.clearGrid(); 994 try self.box.build(allocator, constraint, root_focus); 995 } 996 997 pub fn input(self: *SearchHeader, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 998 const current = self.box.getFocus().child_id orelse return; 999 if (self.searchBox()) |search_box| { 1000 if (current == search_box.getFocus().id) { 1001 // tab, and right-arrow past the last character, step to the 1002 // clone url beside the box 1003 if (key == .tab or (key == .arrow_right and search_box.atEnd())) { 1004 if (self.cloneUrl()) |clone_url| { 1005 root_focus.setFocus(clone_url.getFocus().id); 1006 return; 1007 } 1008 } 1009 return search_box.input(allocator, key, root_focus); 1010 } 1011 } 1012 const clone_url = self.cloneUrl() orelse return; 1013 if (key == .back_tab or (key == .arrow_left and clone_url.selected == 0)) { 1014 if (self.searchBox()) |search_box| { 1015 root_focus.setFocus(search_box.getFocus().id); 1016 return; 1017 } 1018 } 1019 try clone_url.input(allocator, key, root_focus); 1020 } 1021 1022 // the typed query when enter should submit it: the box exists and holds 1023 // focus. the caller owns the returned text. 1024 pub fn submittedText(self: *SearchHeader, allocator: std.mem.Allocator) !?[]u8 { 1025 const search_box = self.searchBox() orelse return null; 1026 if (self.box.getFocus().child_id != search_box.getFocus().id) return null; 1027 return try search_box.text(allocator); 1028 } 1029 1030 // enter the header, preferring the search box. 1031 pub fn focusHeader(self: *SearchHeader, root_focus: *Focus) bool { 1032 if (self.searchBox()) |search_box| { 1033 root_focus.setFocus(search_box.getFocus().id); 1034 return true; 1035 } 1036 const clone_url = self.cloneUrl() orelse return false; 1037 root_focus.setFocus(clone_url.getFocus().id); 1038 return true; 1039 } 1040 1041 pub fn hasFocusable(self: *SearchHeader) bool { 1042 return self.cloneUrl() != null or self.searchBox() != null; 1043 } 1044 1045 fn cloneUrl(self: *SearchHeader) ?*CopyableText { 1046 for (self.box.children.values()) |*child| switch (child.widget) { 1047 .copyable_text => |*copyable_text| return copyable_text, 1048 else => {}, 1049 }; 1050 return null; 1051 } 1052 1053 fn searchBox(self: *SearchHeader) ?*SearchBox { 1054 for (self.box.children.values()) |*child| switch (child.widget) { 1055 .search_box => |*search_box| return search_box, 1056 else => {}, 1057 }; 1058 return null; 1059 } 1060 1061 pub fn clearGrid(self: *SearchHeader) void { 1062 self.box.clearGrid(); 1063 } 1064 1065 pub fn getGrid(self: SearchHeader) ?Grid { 1066 return self.box.getGrid(); 1067 } 1068 1069 pub fn getFocus(self: *SearchHeader) *Focus { 1070 return self.box.getFocus(); 1071 } 1072 }; 1073 1074 // a one-row, non-focusable status bar showing the current page's url 1075 pub const Footer = struct { 1076 focus: *Focus, 1077 grid: ?Grid, 1078 session: *ui.Session, 1079 arena: std.heap.ArenaAllocator, 1080 url: []const u8 = "", 1081 url_width: usize = 0, 1082 1083 pub fn init(allocator: std.mem.Allocator, session: *ui.Session) !Footer { 1084 return .{ 1085 .focus = try Focus.create(allocator, .container), 1086 .grid = null, 1087 .session = session, 1088 .arena = std.heap.ArenaAllocator.init(allocator), 1089 }; 1090 } 1091 1092 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 1093 self.focus.destroy(allocator); 1094 if (self.grid) |*grid| grid.deinit(); 1095 self.arena.deinit(); 1096 } 1097 1098 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1099 _ = root_focus; 1100 self.clearGrid(); 1101 const width = constraint.max_size.width orelse return; 1102 if (width == 0) return; 1103 1104 _ = self.arena.reset(.retain_capacity); 1105 self.url = ""; 1106 self.url_width = 0; 1107 const aa = self.arena.allocator(); 1108 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 1109 const text = ui.terminalWebUrl(aa, self.session, path) catch return; 1110 self.url = text; 1111 1112 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1113 errdefer grid.deinit(); 1114 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 1115 var i: usize = 0; 1116 while (utf8.nextCodepoint()) |ch| { 1117 const rune_width = xitui.width.cellWidth(ch); 1118 if (i + rune_width > width) break; 1119 // a terminal hyperlink to the page 1120 (try grid.cell(i, 0)).link = text; 1121 try grid.setRune(i, 0, ch); 1122 i += rune_width; 1123 } 1124 self.url_width = i; 1125 self.grid = grid; 1126 } 1127 1128 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1129 _ = allocator; 1130 switch (key) { 1131 .mouse => |mouse| { 1132 if (mouse.ctrl or !inp.leftClickOn(root_focus, self.getFocus().id, mouse)) return; 1133 const rect = (root_focus.children.get(self.getFocus().id) orelse return).rect; 1134 if (mouse.x < rect.x + self.url_width) self.session.host_request = .{ .show_copyable_text = self.url }; 1135 }, 1136 else => {}, 1137 } 1138 } 1139 1140 pub fn clearGrid(self: *Footer) void { 1141 if (self.grid) |*grid| { 1142 grid.deinit(); 1143 self.grid = null; 1144 } 1145 } 1146 1147 pub fn getGrid(self: Footer) ?Grid { 1148 return self.grid; 1149 } 1150 1151 pub fn getFocus(self: *Footer) *Focus { 1152 return self.focus; 1153 } 1154 }; 1155 1156 // a single-child wrapper that builds the child at its natural size and 1157 // positions its grid in the middle of the area granted by the parent 1158 // a bordered label, indented past the content it precedes 1159 pub const SectionLabel = struct { 1160 box: wgt.Box(Widget), 1161 1162 pub fn init(allocator: std.mem.Allocator, content: []const u8) !SectionLabel { 1163 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1164 errdefer box.deinit(allocator); 1165 1166 { 1167 var gap = try wgt.Text.init(allocator, " "); 1168 errdefer gap.deinit(allocator); 1169 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 1170 } 1171 1172 { 1173 var tb = try wgt.TextBox.init(allocator, content, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 1174 errdefer tb.deinit(allocator); 1175 tb.getFocus().mode = .all; 1176 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1177 } 1178 1179 box.getFocus().child_id = box.children.keys()[1]; 1180 return .{ .box = box }; 1181 } 1182 1183 pub fn deinit(self: *SectionLabel, allocator: std.mem.Allocator) void { 1184 self.box.deinit(allocator); 1185 } 1186 1187 pub fn build(self: *SectionLabel, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1188 try self.box.build(allocator, constraint, root_focus); 1189 } 1190 1191 pub fn input(self: *SectionLabel, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1192 _ = self; 1193 _ = allocator; 1194 _ = key; 1195 _ = root_focus; 1196 } 1197 1198 pub fn clearGrid(self: *SectionLabel) void { 1199 self.box.clearGrid(); 1200 } 1201 1202 pub fn getGrid(self: SectionLabel) ?Grid { 1203 return self.box.getGrid(); 1204 } 1205 1206 pub fn getFocus(self: *SectionLabel) *Focus { 1207 return self.box.getFocus(); 1208 } 1209 }; 1210 1211 // a submit button, indented past its form's content. the web form POSTs 1212 // the button's form; terminal hosts detect presses through `buttonId`. 1213 pub const SubmitButton = struct { 1214 box: wgt.Box(Widget), 1215 1216 pub fn init(allocator: std.mem.Allocator) !SubmitButton { 1217 return initLabeled(allocator, "submit"); 1218 } 1219 1220 pub fn initLabeled(allocator: std.mem.Allocator, label: []const u8) !SubmitButton { 1221 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1222 errdefer box.deinit(allocator); 1223 1224 { 1225 var gap = try wgt.Text.init(allocator, " "); 1226 errdefer gap.deinit(allocator); 1227 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 1228 } 1229 1230 { 1231 var button = try wgt.TextBox.init(allocator, label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 1232 errdefer button.deinit(allocator); 1233 button.getFocus().mode = .all; 1234 // the renderer distinguishes plain clickables from buttons that 1235 // should POST to a server route by this kind. 1236 button.getFocus().kind = .{ .custom = "submit" }; 1237 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 1238 } 1239 1240 box.getFocus().child_id = box.children.keys()[1]; 1241 return .{ .box = box }; 1242 } 1243 1244 pub fn deinit(self: *SubmitButton, allocator: std.mem.Allocator) void { 1245 self.box.deinit(allocator); 1246 } 1247 1248 pub fn setLabel(self: *SubmitButton, allocator: std.mem.Allocator, label: []const u8) !void { 1249 const button = &self.box.children.values()[1]; 1250 try button.widget.text_box.setContent(allocator, label); 1251 button.min_size = .{ .width = label.len + 2, .height = null }; 1252 } 1253 1254 pub fn build(self: *SubmitButton, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1255 try self.box.build(allocator, constraint, root_focus); 1256 } 1257 1258 pub fn input(self: *SubmitButton, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1259 _ = self; 1260 _ = allocator; 1261 _ = key; 1262 _ = root_focus; 1263 } 1264 1265 pub fn clearGrid(self: *SubmitButton) void { 1266 self.box.clearGrid(); 1267 } 1268 1269 pub fn getGrid(self: SubmitButton) ?Grid { 1270 return self.box.getGrid(); 1271 } 1272 1273 pub fn getFocus(self: *SubmitButton) *Focus { 1274 return self.box.getFocus(); 1275 } 1276 1277 // the button's focus id, for click hit-testing 1278 pub fn buttonId(self: *const SubmitButton) usize { 1279 return self.box.children.keys()[1]; 1280 } 1281 }; 1282 1283 pub const Center = struct { 1284 focus: *Focus, 1285 grid: ?Grid, 1286 child: *Widget, 1287 1288 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 1289 const child = try allocator.create(Widget); 1290 errdefer allocator.destroy(child); 1291 child.* = child_widget; 1292 return .{ 1293 .focus = try Focus.create(allocator, .container), 1294 .grid = null, 1295 .child = child, 1296 }; 1297 } 1298 1299 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1300 self.focus.destroy(allocator); 1301 if (self.grid) |*grid| { 1302 grid.deinit(); 1303 self.grid = null; 1304 } 1305 self.child.deinit(allocator); 1306 allocator.destroy(self.child); 1307 } 1308 1309 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1310 self.clearGrid(); 1311 self.getFocus().clear(); 1312 1313 // build the child without forcing it to fill min_size; it sizes 1314 // itself to its content within the available max. 1315 try self.child.build(allocator, .{ 1316 .min_size = .{ .width = null, .height = null }, 1317 .max_size = constraint.max_size, 1318 }, root_focus); 1319 1320 const child_grid = self.child.getGrid() orelse return; 1321 1322 // prefer max when given; otherwise grow to max(min, child) so that 1323 // when only a min is set (e.g. the wasm path passes viewport rows 1324 // as min_height with no max), we can still vertically center while 1325 // letting taller content extend past the min. 1326 const width = if (constraint.max_size.width) |w| w else if (constraint.min_size.width) |min_w| @max(min_w, child_grid.size.width) else child_grid.size.width; 1327 const height = if (constraint.max_size.height) |h| h else if (constraint.min_size.height) |min_h| @max(min_h, child_grid.size.height) else child_grid.size.height; 1328 if (width == 0 or height == 0) return; 1329 1330 const offset_x: usize = (width -| child_grid.size.width) / 2; 1331 const offset_y: usize = (height -| child_grid.size.height) / 2; 1332 1333 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1334 errdefer grid.deinit(); 1335 try grid.drawGrid(child_grid, offset_x, offset_y); 1336 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1337 self.getFocus().child_id = self.child.getFocus().id; 1338 1339 self.grid = grid; 1340 } 1341 1342 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1343 try self.child.input(allocator, key, root_focus); 1344 } 1345 1346 pub fn clearGrid(self: *Center) void { 1347 if (self.grid) |*grid| { 1348 grid.deinit(); 1349 self.grid = null; 1350 } 1351 self.child.clearGrid(); 1352 } 1353 1354 pub fn getGrid(self: Center) ?Grid { 1355 return self.grid; 1356 } 1357 1358 pub fn getFocus(self: *Center) *Focus { 1359 return self.focus; 1360 } 1361 }; 1362 1363 // renders truecolor ANSI art 1364 pub const AnsiArt = struct { 1365 focus: *Focus, 1366 grid: ?Grid, 1367 content: []const u8, 1368 1369 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1370 return .{ 1371 .focus = try Focus.create(allocator, .container), 1372 .grid = null, 1373 .content = content, 1374 }; 1375 } 1376 1377 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1378 self.focus.destroy(allocator); 1379 if (self.grid) |*grid| { 1380 grid.deinit(); 1381 self.grid = null; 1382 } 1383 } 1384 1385 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1386 _ = root_focus; 1387 self.clearGrid(); 1388 1389 // parse into rows of cells, then copy into a rectangular grid 1390 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1391 defer { 1392 for (rows.items) |*row| row.deinit(allocator); 1393 rows.deinit(allocator); 1394 } 1395 var row: std.ArrayList(Grid.Cell) = .empty; 1396 errdefer row.deinit(allocator); 1397 1398 var style: Grid.Style = .{}; 1399 var width: usize = 0; 1400 const content = self.content; 1401 var i: usize = 0; 1402 while (i < content.len) { 1403 const byte = content[i]; 1404 if (byte == '\n') { 1405 width = @max(width, row.items.len); 1406 try rows.append(allocator, row); 1407 row = .empty; 1408 i += 1; 1409 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1410 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1411 var j = i + 2; 1412 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1413 if (j >= content.len) { 1414 i = content.len; // malformed trailing escape; stop 1415 } else { 1416 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1417 i = j + 1; 1418 } 1419 } else { 1420 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1421 const end = @min(content.len, i + len); 1422 const rune = content[i..end]; 1423 const codepoint = std.unicode.utf8Decode(rune) catch '�'; 1424 // a foreground color does not make a space visible. only an 1425 // effective background makes it opaque; foreground state often 1426 // remains active through the padding at the end of a row. 1427 const effective_background = if (style.inverted) style.fg else style.bg; 1428 const transparent = end - i == 1 and rune[0] == ' ' and effective_background == null; 1429 try row.append(allocator, .{ 1430 .rune = if (transparent) null else codepoint, 1431 .style = style, 1432 }); 1433 if (!transparent and xitui.width.cellWidth(codepoint) == 2) { 1434 try row.append(allocator, .{ .rune = null, .style = style, .continuation = true }); 1435 } 1436 i = end; 1437 } 1438 } 1439 // a trailing row with content but no closing newline 1440 if (row.items.len > 0) { 1441 width = @max(width, row.items.len); 1442 try rows.append(allocator, row); 1443 } else { 1444 row.deinit(allocator); 1445 } 1446 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1447 1448 const height = rows.items.len; 1449 if (width == 0 or height == 0) return; 1450 1451 const clamped_w = @min(width, constraint.max_size.width orelse width); 1452 const clamped_h = @min(height, constraint.max_size.height orelse height); 1453 1454 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1455 errdefer grid.deinit(); 1456 for (rows.items[0..clamped_h], 0..) |r, y| { 1457 const n = @min(r.items.len, clamped_w); 1458 for (r.items[0..n], 0..) |cell, x| { 1459 if (cell.continuation) continue; 1460 (try grid.cell(x, y)).style = cell.style; 1461 try grid.setRune(x, y, cell.rune); 1462 } 1463 } 1464 self.grid = grid; 1465 } 1466 1467 pub fn clearGrid(self: *AnsiArt) void { 1468 if (self.grid) |*grid| { 1469 grid.deinit(); 1470 self.grid = null; 1471 } 1472 } 1473 1474 pub fn getGrid(self: AnsiArt) ?Grid { 1475 return self.grid; 1476 } 1477 1478 pub fn getFocus(self: *AnsiArt) *Focus { 1479 return self.focus; 1480 } 1481 1482 fn applySgr(style: *Grid.Style, params: []const u8) void { 1483 var nums: [16]u32 = undefined; 1484 var n: usize = 0; 1485 var it = std.mem.splitScalar(u8, params, ';'); 1486 while (it.next()) |tok| { 1487 if (n >= nums.len) break; 1488 // an empty parameter (e.g. bare "\x1b[m") means 0 1489 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1490 n += 1; 1491 } 1492 if (n == 0) { 1493 style.* = .{}; 1494 return; 1495 } 1496 var i: usize = 0; 1497 while (i < n) : (i += 1) { 1498 switch (nums[i]) { 1499 0 => style.* = .{}, 1500 7 => style.inverted = true, 1501 27 => style.inverted = false, 1502 39 => style.fg = null, 1503 49 => style.bg = null, 1504 38, 48 => { 1505 // truecolor form: 38;2;r;g;b — anything else is ignored 1506 if (i + 4 < n and nums[i + 1] == 2) { 1507 const c = Grid.Color{ .rgb = .{ 1508 .r = @truncate(nums[i + 2]), 1509 .g = @truncate(nums[i + 3]), 1510 .b = @truncate(nums[i + 4]), 1511 } }; 1512 if (nums[i] == 38) style.fg = c else style.bg = c; 1513 i += 4; 1514 } 1515 }, 1516 else => {}, 1517 } 1518 } 1519 } 1520 }; 1521 1522 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1523 pub const AnsiBackground = struct { 1524 grid: ?Grid, 1525 child: *Widget, 1526 art: AnsiArt, 1527 session: *ui.Session, 1528 1529 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, session: *ui.Session) !AnsiBackground { 1530 var cw = child_widget; 1531 const child = allocator.create(Widget) catch |e| { 1532 cw.deinit(allocator); 1533 return e; 1534 }; 1535 child.* = cw; 1536 errdefer { 1537 child.deinit(allocator); 1538 allocator.destroy(child); 1539 } 1540 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, session.data.ansi_art), .session = session }; 1541 } 1542 1543 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1544 if (self.grid) |*grid| grid.deinit(); 1545 self.art.deinit(allocator); 1546 self.child.deinit(allocator); 1547 allocator.destroy(self.child); 1548 } 1549 1550 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1551 self.clearGrid(); 1552 1553 // make the wrapped page fill the whole available area 1554 try self.child.build(allocator, .{ 1555 .min_size = .{ 1556 .width = constraint.max_size.width orelse constraint.min_size.width, 1557 .height = constraint.max_size.height orelse constraint.min_size.height, 1558 }, 1559 .max_size = constraint.max_size, 1560 }, root_focus); 1561 1562 if (!self.session.data.enable_ansi) return; 1563 const foreground = self.child.getGrid() orelse return; 1564 self.art.content = self.session.data.ansi_art; 1565 try self.buildArt(allocator, foreground.size, root_focus); 1566 if (!self.session.is_terminal) return; 1567 const art_grid = self.art.getGrid() orelse return; 1568 self.grid = try artBehind(allocator, foreground, art_grid); 1569 } 1570 1571 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1572 try self.child.input(allocator, key, root_focus); 1573 } 1574 1575 pub fn clearGrid(self: *AnsiBackground) void { 1576 if (self.grid) |*grid| { 1577 grid.deinit(); 1578 self.grid = null; 1579 } 1580 self.child.clearGrid(); 1581 self.art.clearGrid(); 1582 } 1583 1584 pub fn getGrid(self: AnsiBackground) ?Grid { 1585 return self.grid orelse self.child.getGrid(); 1586 } 1587 1588 pub fn getFocus(self: *AnsiBackground) *Focus { 1589 return self.child.getFocus(); 1590 } 1591 1592 // a foreground cell counts as blank if it's empty or a bare space without styling 1593 fn cellIsBlank(cell: Grid.Cell) bool { 1594 if (cell.continuation) return false; 1595 if (cell.rune) |rune| { 1596 return rune == ' ' and 1597 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1598 } 1599 return true; 1600 } 1601 1602 // the art is dimmed to this fraction of its brightness so foreground text 1603 // stays legible over it 1604 const art_brightness = 35; // percent 1605 1606 // the art parser only emits truecolor, so a palette color never occurs 1607 fn artRgb(color: ?Grid.Color) ?Grid.Color.Rgb { 1608 const c = color orelse return null; 1609 return switch (c) { 1610 .rgb => |v| v, 1611 else => null, 1612 }; 1613 } 1614 1615 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1616 const v = artRgb(color) orelse return color; 1617 return .{ .rgb = .{ 1618 .r = @intCast(@as(u16, v.r) * art_brightness / 100), 1619 .g = @intCast(@as(u16, v.g) * art_brightness / 100), 1620 .b = @intCast(@as(u16, v.b) * art_brightness / 100), 1621 } }; 1622 } 1623 1624 fn buildArt(self: *AnsiBackground, allocator: std.mem.Allocator, size: layout.Size, root_focus: *Focus) !void { 1625 try self.art.build(allocator, .{ 1626 .min_size = .{ .width = null, .height = null }, 1627 .max_size = .{ .width = size.width, .height = size.height }, 1628 }, root_focus); 1629 if (self.art.grid) |*grid| { 1630 for (grid.cells) |*cell| { 1631 cell.style.fg = dimColor(cell.style.fg); 1632 cell.style.bg = dimColor(cell.style.bg); 1633 } 1634 } 1635 } 1636 1637 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1638 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1639 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1640 // snap such channels clear of that range so the backdrop doesn't blink or 1641 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1642 // actually render truecolor. 1643 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1644 const v = artRgb(color) orelse return color; 1645 const snap = struct { 1646 fn f(c: u8) u8 { 1647 return if (c >= 1 and c <= 9) 10 else c; 1648 } 1649 }.f; 1650 return .{ .rgb = .{ .r = snap(v.r), .g = snap(v.g), .b = snap(v.b) } }; 1651 } 1652 1653 fn applyArtBackground(dst: *Grid.Cell, src: Grid.Cell) void { 1654 const background_maybe = sgrSafe(src.style.bg orelse src.style.fg); 1655 dst.style.bg = background_maybe; 1656 // change unstyled text to contrast with the art behind it 1657 if (dst.style.fg == null) { 1658 if (artRgb(background_maybe)) |v| { 1659 // use near-black and near-white, which is easier on the eyes 1660 // while retaining high contrast 1661 const luminance = (@as(u32, v.r) * 299 + 1662 @as(u32, v.g) * 587 + 1663 @as(u32, v.b) * 114) / 1000; 1664 dst.style.fg = if (luminance >= 128) 1665 .{ .rgb = .{ .r = 16, .g = 16, .b = 16 } } 1666 else 1667 .{ .rgb = .{ .r = 240, .g = 240, .b = 240 } }; 1668 } 1669 } 1670 } 1671 1672 // composites ANSI art behind a foreground grid 1673 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 1674 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1675 errdefer out.deinit(); 1676 1677 const anchor_x = foreground.size.width -| art_grid.size.width; 1678 for (0..art_grid.size.height) |y| { 1679 for (0..art_grid.size.width) |x| { 1680 const src = (try art_grid.cell(x, y)).*; 1681 if (src.rune == null) continue; 1682 const dst = try out.cell(anchor_x + x, y); 1683 const wide = x + 1 < art_grid.size.width and (try art_grid.cell(x + 1, y)).continuation; 1684 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 1685 // blank cells take the art; occupied cells take its background 1686 if (cellIsBlank(dst.*) and (if (next_dst) |next| cellIsBlank(next.*) else true)) { 1687 dst.style = src.style; 1688 dst.style.fg = sgrSafe(dst.style.fg); 1689 dst.style.bg = sgrSafe(dst.style.bg); 1690 try out.setRune(anchor_x + x, y, src.rune); 1691 } else { 1692 applyArtBackground(dst, src); 1693 if (next_dst) |next| applyArtBackground(next, src); 1694 } 1695 } 1696 } 1697 return out; 1698 } 1699 }; 1700 1701 pub const CopyableText = struct { 1702 pub const Choice = struct { 1703 selector: []const u8 = "", 1704 text: []const u8, 1705 copyable_text: ?[]const u8 = null, 1706 label: []const u8 = "", 1707 bottom_label: []const u8 = "", 1708 }; 1709 1710 box: wgt.Box(Widget), 1711 session: *ui.Session, 1712 choices: []Choice, 1713 selected: usize = 0, 1714 1715 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice) !CopyableText { 1716 if (choices.len == 0) return error.MissingCopyableText; 1717 1718 const owned_choices = try allocator.dupe(Choice, choices); 1719 errdefer allocator.free(owned_choices); 1720 1721 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1722 errdefer box.deinit(allocator); 1723 1724 if (choices.len > 1) for (choices) |choice| { 1725 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border_style = .hidden, .round_corners = true, .wrap_kind = .none }); 1726 errdefer selector_box.deinit(allocator); 1727 selector_box.getFocus().mode = .all; 1728 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 } }); 1729 }; 1730 1731 var text_input = try wgt.TextInput.init(allocator, .{ 1732 .border_style = .single, 1733 .round_corners = true, 1734 .label = choices[0].label, 1735 .bottom_label = choices[0].bottom_label, 1736 .read_only = true, 1737 .render_content = session.is_terminal, 1738 .visible_width = null, 1739 }); 1740 errdefer text_input.deinit(allocator); 1741 text_input.getFocus().mode = .all; 1742 try text_input.setContent(allocator, choices[0].text); 1743 box.getFocus().child_id = text_input.getFocus().id; 1744 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1745 1746 return .{ .box = box, .session = session, .choices = owned_choices }; 1747 } 1748 1749 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, location: ui.RoutablePage.RepoLocation) !?CopyableText { 1750 const aa = session.page_arena.allocator(); 1751 var choices: [2]Choice = undefined; 1752 var count: usize = 0; 1753 switch (location) { 1754 .repo => |identity| { 1755 if (session.data.git_http_port) |port| { 1756 const url = try std.fmt.allocPrint(aa, "http://localhost:{d}/repo/{s}", .{ port, identity }); 1757 choices[count] = .{ 1758 .selector = "http", 1759 .text = url, 1760 .copyable_text = try std.fmt.allocPrint(aa, "git clone {s}", .{url}), 1761 .label = " clone ", 1762 }; 1763 count += 1; 1764 } 1765 if (session.data.git_ssh_port) |port| { 1766 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}", .{ port, identity }); 1767 choices[count] = .{ 1768 .selector = "ssh", 1769 .text = url, 1770 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1771 .label = " clone ", 1772 }; 1773 count += 1; 1774 } 1775 }, 1776 .fork => |f| if (session.data.git_ssh_port) |port| { 1777 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/fork/{s}/patch:{s}", .{ port, f.identity, f.id }); 1778 choices[count] = .{ 1779 .text = url, 1780 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1781 .label = " clone ", 1782 }; 1783 count += 1; 1784 }, 1785 } 1786 if (count == 0) return null; 1787 return @as(?CopyableText, try init(allocator, session, choices[0..count])); 1788 } 1789 1790 pub fn minWidth(self: *const CopyableText) usize { 1791 var width = maxWidth(self.choices) + 2; 1792 if (self.choices.len > 1) { 1793 for (self.choices) |choice| width += choice.selector.len + 2; 1794 } 1795 return width; 1796 } 1797 1798 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1799 self.box.deinit(allocator); 1800 allocator.free(self.choices); 1801 } 1802 1803 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1804 self.clearGrid(); 1805 1806 if (self.choices.len > 1) { 1807 if (root_focus.grandchild_id) |id| { 1808 for (0..self.choices.len) |i| { 1809 if (id != self.selector(i).getFocus().id) continue; 1810 if (i != self.selected) try self.select(allocator, i); 1811 root_focus.setFocus(self.textInput().getFocus().id); 1812 break; 1813 } 1814 } 1815 1816 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 1817 for (0..self.choices.len) |i| { 1818 self.selector(i).options.border_style = if (i == self.selected) 1819 (if (focused) .double else .single) 1820 else 1821 .hidden; 1822 } 1823 } 1824 self.textInput().options.border_style = .single; 1825 self.textInput().cursor = 0; 1826 self.textInput().scroll_offset = 0; 1827 try self.box.build(allocator, constraint, root_focus); 1828 1829 if (!self.session.is_terminal) { 1830 const aa = self.session.arena.allocator(); 1831 const id = self.textInput().getFocus().id; 1832 try self.session.text_inputs.put(aa, id, self.textInput()); 1833 } 1834 } 1835 1836 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1837 switch (key) { 1838 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 1839 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 1840 .enter => if (self.session.is_terminal) self.show(), 1841 .mouse => |mouse| { 1842 if (self.choices.len > 1) for (0..self.choices.len) |i| { 1843 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 1844 try self.select(allocator, i); 1845 root_focus.setFocus(self.textInput().getFocus().id); 1846 return; 1847 }; 1848 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 1849 }, 1850 else => {}, 1851 } 1852 } 1853 1854 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 1855 self.selected = selected; 1856 const choice = self.choices[selected]; 1857 const text_input = self.textInput(); 1858 text_input.options.label = choice.label; 1859 text_input.options.bottom_label = choice.bottom_label; 1860 try text_input.setContent(allocator, choice.text); 1861 } 1862 1863 fn show(self: *CopyableText) void { 1864 const choice = self.choices[self.selected]; 1865 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 1866 } 1867 1868 fn textInput(self: *CopyableText) *wgt.TextInput { 1869 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 1870 } 1871 1872 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 1873 return &self.box.children.values()[index].widget.text_box; 1874 } 1875 1876 fn maxWidth(choices: []const Choice) usize { 1877 var width: usize = 0; 1878 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 1879 return width; 1880 } 1881 1882 pub fn clearGrid(self: *CopyableText) void { 1883 self.box.clearGrid(); 1884 } 1885 1886 pub fn getGrid(self: CopyableText) ?Grid { 1887 return self.box.getGrid(); 1888 } 1889 1890 pub fn getFocus(self: *CopyableText) *Focus { 1891 return self.box.getFocus(); 1892 } 1893 }; 1894