╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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