╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
814 { 815 var button = try wgt.TextBox.init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 816 errdefer button.deinit(allocator); 817 button.getFocus().mode = .all; 818 // the renderer distinguishes plain clickables from buttons that 819 // should POST to a server route by this kind. 820 button.getFocus().kind = .{ .custom = "submit" }; 821 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 822 } 823 824 box.getFocus().child_id = box.children.keys()[1]; 825 return .{ .box = box }; 826 } 827 828 pub fn deinit(self: *SubmitButton, allocator: std.mem.Allocator) void { 829 self.box.deinit(allocator); 830 } 831 832 pub fn build(self: *SubmitButton, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 833 try self.box.build(allocator, constraint, root_focus); 834 } 835 836 pub fn input(self: *SubmitButton, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 837 _ = self; 838 _ = allocator; 839 _ = key; 840 _ = root_focus; 841 } 842 843 pub fn clearGrid(self: *SubmitButton) void { 844 self.box.clearGrid(); 845 } 846 847 pub fn getGrid(self: SubmitButton) ?Grid { 848 return self.box.getGrid(); 849 } 850 851 pub fn getFocus(self: *SubmitButton) *Focus { 852 return self.box.getFocus(); 853 } 854 855 // the button's focus id, for click hit-testing 856 pub fn buttonId(self: *const SubmitButton) usize { 857 return self.box.children.keys()[1]; 858 } 859 }; 860 861 pub const Center = struct { 862 focus: *Focus, 863 grid: ?Grid, 864 child: *Widget, 865 866 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 867 const child = try allocator.create(Widget); 868 errdefer allocator.destroy(child); 869 child.* = child_widget; 870 return .{ 871 .focus = try Focus.create(allocator, .container), 872 .grid = null, 873 .child = child, 874 }; 875 } 876 877 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 878 self.focus.destroy(allocator); 879 if (self.grid) |*grid| { 880 grid.deinit(); 881 self.grid = null; 882 } 883 self.child.deinit(allocator); 884 allocator.destroy(self.child); 885 } 886 887 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 888 self.clearGrid(); 889 self.getFocus().clear(); 890 891 // build the child without forcing it to fill min_size; it sizes 892 // itself to its content within the available max. 893 try self.child.build(allocator, .{ 894 .min_size = .{ .width = null, .height = null }, 895 .max_size = constraint.max_size, 896 }, root_focus); 897 898 const child_grid = self.child.getGrid() orelse return; 899 900 // prefer max when given; otherwise grow to max(min, child) so that 901 // when only a min is set (e.g. the wasm path passes viewport rows 902 // as min_height with no max), we can still vertically center while 903 // letting taller content extend past the min. 904 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; 905 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; 906 if (width == 0 or height == 0) return; 907 908 const offset_x: usize = (width -| child_grid.size.width) / 2; 909 const offset_y: usize = (height -| child_grid.size.height) / 2; 910 911 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 912 errdefer grid.deinit(); 913 try grid.drawGrid(child_grid, offset_x, offset_y); 914 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 915 self.getFocus().child_id = self.child.getFocus().id; 916 917 self.grid = grid; 918 } 919 920 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 921 try self.child.input(allocator, key, root_focus); 922 } 923 924 pub fn clearGrid(self: *Center) void { 925 if (self.grid) |*grid| { 926 grid.deinit(); 927 self.grid = null; 928 } 929 self.child.clearGrid(); 930 } 931 932 pub fn getGrid(self: Center) ?Grid { 933 return self.grid; 934 } 935 936 pub fn getFocus(self: *Center) *Focus { 937 return self.focus; 938 } 939 }; 940 941 // renders truecolor ANSI art 942 pub const AnsiArt = struct { 943 focus: *Focus, 944 grid: ?Grid, 945 content: []const u8, 946 947 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 948 return .{ 949 .focus = try Focus.create(allocator, .container), 950 .grid = null, 951 .content = content, 952 }; 953 } 954 955 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 956 self.focus.destroy(allocator); 957 if (self.grid) |*grid| { 958 grid.deinit(); 959 self.grid = null; 960 } 961 } 962 963 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 964 _ = root_focus; 965 self.clearGrid(); 966 967 // parse into rows of cells, then copy into a rectangular grid 968 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 969 defer { 970 for (rows.items) |*row| row.deinit(allocator); 971 rows.deinit(allocator); 972 } 973 var row: std.ArrayList(Grid.Cell) = .empty; 974 errdefer row.deinit(allocator); 975 976 var style: Grid.Style = .{}; 977 var width: usize = 0; 978 const content = self.content; 979 var i: usize = 0; 980 while (i < content.len) { 981 const byte = content[i]; 982 if (byte == '\n') { 983 width = @max(width, row.items.len); 984 try rows.append(allocator, row); 985 row = .empty; 986 i += 1; 987 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 988 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 989 var j = i + 2; 990 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 991 if (j >= content.len) { 992 i = content.len; // malformed trailing escape; stop 993 } else { 994 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 995 i = j + 1; 996 } 997 } else { 998 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 999 const end = @min(content.len, i + len); 1000 const rune = content[i..end]; 1001 const codepoint = std.unicode.utf8Decode(rune) catch '�'; 1002 // a foreground color does not make a space visible. only an 1003 // effective background makes it opaque; foreground state often 1004 // remains active through the padding at the end of a row. 1005 const effective_background = if (style.inverted) style.fg else style.bg; 1006 const transparent = end - i == 1 and rune[0] == ' ' and effective_background == null; 1007 try row.append(allocator, .{ 1008 .rune = if (transparent) null else codepoint, 1009 .style = style, 1010 }); 1011 if (!transparent and xitui.width.cellWidth(codepoint) == 2) { 1012 try row.append(allocator, .{ .rune = null, .style = style, .continuation = true }); 1013 } 1014 i = end; 1015 } 1016 } 1017 // a trailing row with content but no closing newline 1018 if (row.items.len > 0) { 1019 width = @max(width, row.items.len); 1020 try rows.append(allocator, row); 1021 } else { 1022 row.deinit(allocator); 1023 } 1024 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1025 1026 const height = rows.items.len; 1027 if (width == 0 or height == 0) return; 1028 1029 const clamped_w = @min(width, constraint.max_size.width orelse width); 1030 const clamped_h = @min(height, constraint.max_size.height orelse height); 1031 1032 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1033 errdefer grid.deinit(); 1034 for (rows.items[0..clamped_h], 0..) |r, y| { 1035 const n = @min(r.items.len, clamped_w); 1036 for (r.items[0..n], 0..) |cell, x| { 1037 if (cell.continuation) continue; 1038 (try grid.cell(x, y)).style = cell.style; 1039 try grid.setRune(x, y, cell.rune); 1040 } 1041 } 1042 self.grid = grid; 1043 } 1044 1045 pub fn clearGrid(self: *AnsiArt) void { 1046 if (self.grid) |*grid| { 1047 grid.deinit(); 1048 self.grid = null; 1049 } 1050 } 1051 1052 pub fn getGrid(self: AnsiArt) ?Grid { 1053 return self.grid; 1054 } 1055 1056 pub fn getFocus(self: *AnsiArt) *Focus { 1057 return self.focus; 1058 } 1059 1060 fn applySgr(style: *Grid.Style, params: []const u8) void { 1061 var nums: [16]u32 = undefined; 1062 var n: usize = 0; 1063 var it = std.mem.splitScalar(u8, params, ';'); 1064 while (it.next()) |tok| { 1065 if (n >= nums.len) break; 1066 // an empty parameter (e.g. bare "\x1b[m") means 0 1067 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1068 n += 1; 1069 } 1070 if (n == 0) { 1071 style.* = .{}; 1072 return; 1073 } 1074 var i: usize = 0; 1075 while (i < n) : (i += 1) { 1076 switch (nums[i]) { 1077 0 => style.* = .{}, 1078 7 => style.inverted = true, 1079 27 => style.inverted = false, 1080 39 => style.fg = null, 1081 49 => style.bg = null, 1082 38, 48 => { 1083 // truecolor form: 38;2;r;g;b — anything else is ignored 1084 if (i + 4 < n and nums[i + 1] == 2) { 1085 const c = Grid.Color{ 1086 .r = @truncate(nums[i + 2]), 1087 .g = @truncate(nums[i + 3]), 1088 .b = @truncate(nums[i + 4]), 1089 }; 1090 if (nums[i] == 38) style.fg = c else style.bg = c; 1091 i += 4; 1092 } 1093 }, 1094 else => {}, 1095 } 1096 } 1097 } 1098 }; 1099 1100 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1101 pub const AnsiBackground = struct { 1102 grid: ?Grid, 1103 child: *Widget, 1104 art: AnsiArt, 1105 session: *ui.Session, 1106 1107 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, session: *ui.Session) !AnsiBackground { 1108 var cw = child_widget; 1109 const child = allocator.create(Widget) catch |e| { 1110 cw.deinit(allocator); 1111 return e; 1112 }; 1113 child.* = cw; 1114 errdefer { 1115 child.deinit(allocator); 1116 allocator.destroy(child); 1117 } 1118 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, session.data.ansi_art), .session = session }; 1119 } 1120 1121 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1122 if (self.grid) |*grid| grid.deinit(); 1123 self.art.deinit(allocator); 1124 self.child.deinit(allocator); 1125 allocator.destroy(self.child); 1126 } 1127 1128 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1129 self.clearGrid(); 1130 1131 // make the wrapped page fill the whole available area 1132 try self.child.build(allocator, .{ 1133 .min_size = .{ 1134 .width = constraint.max_size.width orelse constraint.min_size.width, 1135 .height = constraint.max_size.height orelse constraint.min_size.height, 1136 }, 1137 .max_size = constraint.max_size, 1138 }, root_focus); 1139 1140 if (!self.session.data.enable_ansi) return; 1141 const foreground = self.child.getGrid() orelse return; 1142 self.art.content = self.session.data.ansi_art; 1143 try self.buildArt(allocator, foreground.size, root_focus); 1144 if (!self.session.is_terminal) return; 1145 const art_grid = self.art.getGrid() orelse return; 1146 self.grid = try artBehind(allocator, foreground, art_grid); 1147 } 1148 1149 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1150 try self.child.input(allocator, key, root_focus); 1151 } 1152 1153 pub fn clearGrid(self: *AnsiBackground) void { 1154 if (self.grid) |*grid| { 1155 grid.deinit(); 1156 self.grid = null; 1157 } 1158 self.child.clearGrid(); 1159 self.art.clearGrid(); 1160 } 1161 1162 pub fn getGrid(self: AnsiBackground) ?Grid { 1163 return self.grid orelse self.child.getGrid(); 1164 } 1165 1166 pub fn getFocus(self: *AnsiBackground) *Focus { 1167 return self.child.getFocus(); 1168 } 1169 1170 // a foreground cell counts as blank if it's empty or a bare space without styling 1171 fn cellIsBlank(cell: Grid.Cell) bool { 1172 if (cell.continuation) return false; 1173 if (cell.rune) |rune| { 1174 return rune == ' ' and 1175 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1176 } 1177 return true; 1178 } 1179 1180 // the art is dimmed to this fraction of its brightness so foreground text 1181 // stays legible over it 1182 const art_brightness = 35; // percent 1183 1184 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1185 const c = color orelse return null; 1186 return .{ 1187 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 1188 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 1189 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 1190 }; 1191 } 1192 1193 fn buildArt(self: *AnsiBackground, allocator: std.mem.Allocator, size: layout.Size, root_focus: *Focus) !void { 1194 try self.art.build(allocator, .{ 1195 .min_size = .{ .width = null, .height = null }, 1196 .max_size = .{ .width = size.width, .height = size.height }, 1197 }, root_focus); 1198 if (self.art.grid) |*grid| { 1199 for (grid.cells) |*cell| { 1200 cell.style.fg = dimColor(cell.style.fg); 1201 cell.style.bg = dimColor(cell.style.bg); 1202 } 1203 } 1204 } 1205 1206 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1207 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1208 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1209 // snap such channels clear of that range so the backdrop doesn't blink or 1210 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1211 // actually render truecolor. 1212 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1213 const c = color orelse return null; 1214 const snap = struct { 1215 fn f(v: u8) u8 { 1216 return if (v >= 1 and v <= 9) 10 else v; 1217 } 1218 }.f; 1219 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1220 } 1221 1222 fn applyArtBackground(dst: *Grid.Cell, src: Grid.Cell) void { 1223 const background_maybe = sgrSafe(src.style.bg orelse src.style.fg); 1224 dst.style.bg = background_maybe; 1225 // change unstyled text to contrast with the art behind it 1226 if (dst.style.fg == null) { 1227 if (background_maybe) |background| { 1228 // use near-black and near-white, which is easier on the eyes 1229 // while retaining high contrast 1230 const luminance = (@as(u32, background.r) * 299 + 1231 @as(u32, background.g) * 587 + 1232 @as(u32, background.b) * 114) / 1000; 1233 dst.style.fg = if (luminance >= 128) 1234 .{ .r = 16, .g = 16, .b = 16 } 1235 else 1236 .{ .r = 240, .g = 240, .b = 240 }; 1237 } 1238 } 1239 } 1240 1241 // composites ANSI art behind a foreground grid 1242 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 1243 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1244 errdefer out.deinit(); 1245 1246 const anchor_x = foreground.size.width -| art_grid.size.width; 1247 for (0..art_grid.size.height) |y| { 1248 for (0..art_grid.size.width) |x| { 1249 const src = (try art_grid.cell(x, y)).*; 1250 if (src.rune == null) continue; 1251 const dst = try out.cell(anchor_x + x, y); 1252 const wide = x + 1 < art_grid.size.width and (try art_grid.cell(x + 1, y)).continuation; 1253 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 1254 // blank cells take the art; occupied cells take its background 1255 if (cellIsBlank(dst.*) and (next_dst == null or cellIsBlank(next_dst.?.*))) { 1256 dst.style = src.style; 1257 dst.style.fg = sgrSafe(dst.style.fg); 1258 dst.style.bg = sgrSafe(dst.style.bg); 1259 try out.setRune(anchor_x + x, y, src.rune); 1260 } else { 1261 applyArtBackground(dst, src); 1262 if (next_dst) |next| applyArtBackground(next, src); 1263 } 1264 } 1265 } 1266 return out; 1267 } 1268 }; 1269 1270 pub const CopyableText = struct { 1271 pub const Choice = struct { 1272 selector: []const u8 = "", 1273 text: []const u8, 1274 copyable_text: ?[]const u8 = null, 1275 label: []const u8 = "", 1276 bottom_label: []const u8 = "", 1277 }; 1278 1279 box: wgt.Box(Widget), 1280 session: *ui.Session, 1281 choices: []Choice, 1282 selected: usize = 0, 1283 1284 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice) !CopyableText { 1285 if (choices.len == 0) return error.MissingCopyableText; 1286 1287 const owned_choices = try allocator.dupe(Choice, choices); 1288 errdefer allocator.free(owned_choices); 1289 1290 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1291 errdefer box.deinit(allocator); 1292 1293 if (choices.len > 1) for (choices) |choice| { 1294 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 1295 errdefer selector_box.deinit(allocator); 1296 selector_box.getFocus().mode = .all; 1297 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 } }); 1298 }; 1299 1300 var text_input = try wgt.TextInput.init(allocator, .{ 1301 .border_style = .single, 1302 .rounded_corners = true, 1303 .label = choices[0].label, 1304 .bottom_label = choices[0].bottom_label, 1305 .read_only = true, 1306 .render_content = session.is_terminal, 1307 .visible_width = null, 1308 }); 1309 errdefer text_input.deinit(allocator); 1310 text_input.getFocus().mode = .all; 1311 try text_input.setContent(allocator, choices[0].text); 1312 box.getFocus().child_id = text_input.getFocus().id; 1313 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1314 1315 return .{ .box = box, .session = session, .choices = owned_choices }; 1316 } 1317 1318 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, location: ui.RoutablePage.RepoLocation) !?CopyableText { 1319 const aa = session.page_arena.allocator(); 1320 var choices: [2]Choice = undefined; 1321 var count: usize = 0; 1322 switch (location) { 1323 .repo => |identity| { 1324 if (session.data.git_http_port) |port| { 1325 const url = try std.fmt.allocPrint(aa, "http://localhost:{d}/repo/{s}", .{ port, identity }); 1326 choices[count] = .{ 1327 .selector = "http", 1328 .text = url, 1329 .copyable_text = try std.fmt.allocPrint(aa, "git clone {s}", .{url}), 1330 .label = " clone ", 1331 }; 1332 count += 1; 1333 } 1334 if (session.data.git_ssh_port) |port| { 1335 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}", .{ port, identity }); 1336 choices[count] = .{ 1337 .selector = "ssh", 1338 .text = url, 1339 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1340 .label = " clone ", 1341 }; 1342 count += 1; 1343 } 1344 }, 1345 .fork => |f| if (session.data.git_ssh_port) |port| { 1346 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/fork/{s}/patch:{s}", .{ port, f.identity, f.id }); 1347 choices[count] = .{ 1348 .text = url, 1349 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1350 .label = " clone ", 1351 }; 1352 count += 1; 1353 }, 1354 } 1355 if (count == 0) return null; 1356 return @as(?CopyableText, try init(allocator, session, choices[0..count])); 1357 } 1358 1359 pub fn minWidth(self: *const CopyableText) usize { 1360 var width = maxWidth(self.choices) + 2; 1361 if (self.choices.len > 1) { 1362 for (self.choices) |choice| width += choice.selector.len + 2; 1363 } 1364 return width; 1365 } 1366 1367 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1368 self.box.deinit(allocator); 1369 allocator.free(self.choices); 1370 } 1371 1372 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1373 self.clearGrid(); 1374 1375 if (self.choices.len > 1) { 1376 if (root_focus.grandchild_id) |id| { 1377 for (0..self.choices.len) |i| { 1378 if (id != self.selector(i).getFocus().id) continue; 1379 if (i != self.selected) try self.select(allocator, i); 1380 root_focus.setFocus(self.textInput().getFocus().id); 1381 break; 1382 } 1383 } 1384 1385 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 1386 for (0..self.choices.len) |i| { 1387 self.selector(i).options.border_style = if (i == self.selected) 1388 (if (focused) .double else .single) 1389 else 1390 .hidden; 1391 } 1392 } 1393 self.textInput().options.border_style = .single; 1394 self.textInput().cursor = 0; 1395 self.textInput().scroll_offset = 0; 1396 try self.box.build(allocator, constraint, root_focus); 1397 1398 if (!self.session.is_terminal) { 1399 const aa = self.session.arena.allocator(); 1400 const id = self.textInput().getFocus().id; 1401 try self.session.text_inputs.put(aa, id, self.textInput()); 1402 } 1403 } 1404 1405 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1406 switch (key) { 1407 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 1408 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 1409 .enter => if (self.session.is_terminal) self.show(), 1410 .mouse => |mouse| { 1411 if (self.choices.len > 1) for (0..self.choices.len) |i| { 1412 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 1413 try self.select(allocator, i); 1414 root_focus.setFocus(self.textInput().getFocus().id); 1415 return; 1416 }; 1417 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 1418 }, 1419 else => {}, 1420 } 1421 } 1422 1423 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 1424 self.selected = selected; 1425 const choice = self.choices[selected]; 1426 const text_input = self.textInput(); 1427 text_input.options.label = choice.label; 1428 text_input.options.bottom_label = choice.bottom_label; 1429 try text_input.setContent(allocator, choice.text); 1430 } 1431 1432 fn show(self: *CopyableText) void { 1433 const choice = self.choices[self.selected]; 1434 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 1435 } 1436 1437 fn textInput(self: *CopyableText) *wgt.TextInput { 1438 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 1439 } 1440 1441 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 1442 return &self.box.children.values()[index].widget.text_box; 1443 } 1444 1445 fn maxWidth(choices: []const Choice) usize { 1446 var width: usize = 0; 1447 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 1448 return width; 1449 } 1450 1451 pub fn clearGrid(self: *CopyableText) void { 1452 self.box.clearGrid(); 1453 } 1454 1455 pub fn getGrid(self: CopyableText) ?Grid { 1456 return self.box.getGrid(); 1457 } 1458 1459 pub fn getFocus(self: *CopyableText) *Focus { 1460 return self.box.getFocus(); 1461 } 1462 }; 1463