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