╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1065 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1066 self.clearGrid(); 1067 1068 // make the wrapped page fill the whole available area 1069 try self.child.build(allocator, .{ 1070 .min_size = .{ 1071 .width = constraint.max_size.width orelse constraint.min_size.width, 1072 .height = constraint.max_size.height orelse constraint.min_size.height, 1073 }, 1074 .max_size = constraint.max_size, 1075 }, root_focus); 1076 1077 if (!self.session.data.enable_ansi) return; 1078 const foreground = self.child.getGrid() orelse return; 1079 self.art.content = self.session.data.ansi_art; 1080 try self.buildArt(allocator, foreground.size, root_focus); 1081 if (!self.session.is_terminal) return; 1082 const art_grid = self.art.getGrid() orelse return; 1083 self.grid = try artBehind(allocator, foreground, art_grid); 1084 } 1085 1086 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1087 try self.child.input(allocator, key, root_focus); 1088 } 1089 1090 pub fn clearGrid(self: *AnsiBackground) void { 1091 if (self.grid) |*grid| { 1092 grid.deinit(); 1093 self.grid = null; 1094 } 1095 self.child.clearGrid(); 1096 self.art.clearGrid(); 1097 } 1098 1099 pub fn getGrid(self: AnsiBackground) ?Grid { 1100 return self.grid orelse self.child.getGrid(); 1101 } 1102 1103 pub fn getFocus(self: *AnsiBackground) *Focus { 1104 return self.child.getFocus(); 1105 } 1106 1107 // a foreground cell counts as blank if it's empty or a bare space without styling 1108 fn cellIsBlank(cell: Grid.Cell) bool { 1109 if (cell.rune) |rune| { 1110 return std.mem.eql(u8, rune, " ") and 1111 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1112 } 1113 return true; 1114 } 1115 1116 // the art is dimmed to this fraction of its brightness so foreground text 1117 // stays legible over it 1118 const art_brightness = 35; // percent 1119 1120 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1121 const c = color orelse return null; 1122 return .{ 1123 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 1124 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 1125 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 1126 }; 1127 } 1128 1129 fn buildArt(self: *AnsiBackground, allocator: std.mem.Allocator, size: layout.Size, root_focus: *Focus) !void { 1130 try self.art.build(allocator, .{ 1131 .min_size = .{ .width = null, .height = null }, 1132 .max_size = .{ .width = size.width, .height = size.height }, 1133 }, root_focus); 1134 if (self.art.grid) |*grid| { 1135 for (grid.cells.items) |*cell| { 1136 cell.style.fg = dimColor(cell.style.fg); 1137 cell.style.bg = dimColor(cell.style.bg); 1138 } 1139 } 1140 } 1141 1142 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1143 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1144 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1145 // snap such channels clear of that range so the backdrop doesn't blink or 1146 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1147 // actually render truecolor. 1148 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1149 const c = color orelse return null; 1150 const snap = struct { 1151 fn f(v: u8) u8 { 1152 return if (v >= 1 and v <= 9) 10 else v; 1153 } 1154 }.f; 1155 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1156 } 1157 1158 // composites ANSI art behind a foreground grid 1159 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 1160 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1161 errdefer out.deinit(); 1162 1163 const anchor_x = foreground.size.width -| art_grid.size.width; 1164 for (0..art_grid.size.height) |y| { 1165 for (0..art_grid.size.width) |x| { 1166 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 1167 if (src.rune == null) continue; 1168 const idx = try out.cells.at(.{ y, anchor_x + x }); 1169 const dst = &out.cells.items[idx]; 1170 // blank cells take the art; occupied cells take its background 1171 if (cellIsBlank(dst.*)) { 1172 dst.* = src; 1173 dst.style.fg = sgrSafe(dst.style.fg); 1174 dst.style.bg = sgrSafe(dst.style.bg); 1175 } else { 1176 const background_maybe = sgrSafe(src.style.bg orelse src.style.fg); 1177 dst.style.bg = background_maybe; 1178 // change unstyled text to contrast with the art behind it 1179 if (dst.style.fg == null) { 1180 if (background_maybe) |background| { 1181 // use near-black and near-white, which is easier on the eyes 1182 // while retaining high contrast 1183 const luminance = (@as(u32, background.r) * 299 + 1184 @as(u32, background.g) * 587 + 1185 @as(u32, background.b) * 114) / 1000; 1186 dst.style.fg = if (luminance >= 128) 1187 .{ .r = 16, .g = 16, .b = 16 } 1188 else 1189 .{ .r = 240, .g = 240, .b = 240 }; 1190 } 1191 } 1192 } 1193 } 1194 } 1195 return out; 1196 } 1197 }; 1198 1199 pub const CopyableText = struct { 1200 pub const Choice = struct { 1201 selector: []const u8 = "", 1202 text: []const u8, 1203 copyable_text: ?[]const u8 = null, 1204 label: []const u8 = "", 1205 bottom_label: []const u8 = "", 1206 }; 1207 1208 box: wgt.Box(Widget), 1209 session: *ui.Session, 1210 choices: []Choice, 1211 selected: usize = 0, 1212 1213 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice) !CopyableText { 1214 if (choices.len == 0) return error.MissingCopyableText; 1215 1216 const owned_choices = try allocator.dupe(Choice, choices); 1217 errdefer allocator.free(owned_choices); 1218 1219 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1220 errdefer box.deinit(allocator); 1221 1222 if (choices.len > 1) for (choices) |choice| { 1223 var selector_box = try wgt.TextBox(Widget).init(allocator, choice.selector, .{ .border_style = .hidden, .wrap_kind = .none }); 1224 errdefer selector_box.deinit(allocator); 1225 selector_box.getFocus().focusable = true; 1226 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 } }); 1227 }; 1228 1229 var text_input = try wgt.TextInput(Widget).init(allocator, .{ 1230 .border_style = .single, 1231 .label = choices[0].label, 1232 .bottom_label = choices[0].bottom_label, 1233 .read_only = true, 1234 .render_content = session.is_terminal, 1235 .visible_width = null, 1236 }); 1237 errdefer text_input.deinit(allocator); 1238 text_input.getFocus().focusable = true; 1239 try text_input.setContent(allocator, choices[0].text); 1240 box.getFocus().child_id = text_input.getFocus().id; 1241 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1242 1243 return .{ .box = box, .session = session, .choices = owned_choices }; 1244 } 1245 1246 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, identity: []const u8) !CopyableText { 1247 const aa = session.page_arena.allocator(); 1248 var choices: [2]Choice = undefined; 1249 var count: usize = 0; 1250 if (session.data.git_http_port) |port| { 1251 const url = try std.fmt.allocPrint(aa, "http://localhost:{d}/repo/{s}", .{ port, identity }); 1252 choices[count] = .{ 1253 .selector = "http", 1254 .text = url, 1255 .copyable_text = try std.fmt.allocPrint(aa, "git clone {s}", .{url}), 1256 .label = " clone ", 1257 }; 1258 count += 1; 1259 } 1260 if (session.data.git_ssh_port) |port| { 1261 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}", .{ port, identity }); 1262 choices[count] = .{ 1263 .selector = "ssh", 1264 .text = url, 1265 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1266 .label = " clone ", 1267 }; 1268 count += 1; 1269 } 1270 if (count == 0) return error.MissingGitPort; 1271 return init(allocator, session, choices[0..count]); 1272 } 1273 1274 pub fn minWidth(self: *const CopyableText) usize { 1275 var width = maxWidth(self.choices) + 2; 1276 if (self.choices.len > 1) { 1277 for (self.choices) |choice| width += choice.selector.len + 2; 1278 } 1279 return width; 1280 } 1281 1282 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1283 self.box.deinit(allocator); 1284 allocator.free(self.choices); 1285 } 1286 1287 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1288 self.clearGrid(); 1289 1290 if (self.choices.len > 1) { 1291 if (root_focus.grandchild_id) |id| { 1292 for (0..self.choices.len) |i| { 1293 if (id != self.selector(i).getFocus().id) continue; 1294 if (i != self.selected) try self.select(allocator, i); 1295 root_focus.setFocus(self.textInput().getFocus().id); 1296 break; 1297 } 1298 } 1299 1300 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 1301 for (0..self.choices.len) |i| { 1302 self.selector(i).options.border_style = if (i == self.selected) 1303 (if (focused) .double else .single) 1304 else 1305 .hidden; 1306 } 1307 } 1308 self.textInput().options.border_style = .single; 1309 self.textInput().cursor = 0; 1310 self.textInput().scroll_offset = 0; 1311 try self.box.build(allocator, constraint, root_focus); 1312 1313 if (!self.session.is_terminal) { 1314 const aa = self.session.arena.allocator(); 1315 const id = self.textInput().getFocus().id; 1316 try self.session.text_inputs.put(aa, id, self.textInput()); 1317 try self.session.input_values.put(aa, id, self.choices[self.selected].text); 1318 } 1319 } 1320 1321 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1322 switch (key) { 1323 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 1324 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 1325 .enter => if (self.session.is_terminal) self.show(), 1326 .mouse => |mouse| { 1327 if (self.choices.len > 1) for (0..self.choices.len) |i| { 1328 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 1329 try self.select(allocator, i); 1330 root_focus.setFocus(self.textInput().getFocus().id); 1331 return; 1332 }; 1333 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 1334 }, 1335 else => {}, 1336 } 1337 } 1338 1339 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 1340 self.selected = selected; 1341 const choice = self.choices[selected]; 1342 const text_input = self.textInput(); 1343 text_input.options.label = choice.label; 1344 text_input.options.bottom_label = choice.bottom_label; 1345 try text_input.setContent(allocator, choice.text); 1346 } 1347 1348 fn show(self: *CopyableText) void { 1349 const choice = self.choices[self.selected]; 1350 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 1351 } 1352 1353 fn textInput(self: *CopyableText) *wgt.TextInput(Widget) { 1354 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 1355 } 1356 1357 fn selector(self: *CopyableText, index: usize) *wgt.TextBox(Widget) { 1358 return &self.box.children.values()[index].widget.text_box; 1359 } 1360 1361 fn maxWidth(choices: []const Choice) usize { 1362 var width: usize = 0; 1363 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 1364 return width; 1365 } 1366 1367 pub fn clearGrid(self: *CopyableText) void { 1368 self.box.clearGrid(); 1369 } 1370 1371 pub fn getGrid(self: CopyableText) ?Grid { 1372 return self.box.getGrid(); 1373 } 1374 1375 pub fn getFocus(self: *CopyableText) *Focus { 1376 return self.box.getFocus(); 1377 } 1378 }; 1379