╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1182 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1183 } 1184 return true; 1185 } 1186 1187 // the art is dimmed to this fraction of its brightness so foreground text 1188 // stays legible over it 1189 const art_brightness = 35; // percent 1190 1191 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1192 const c = color orelse return null; 1193 return .{ 1194 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 1195 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 1196 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 1197 }; 1198 } 1199 1200 fn buildArt(self: *AnsiBackground, allocator: std.mem.Allocator, size: layout.Size, root_focus: *Focus) !void { 1201 try self.art.build(allocator, .{ 1202 .min_size = .{ .width = null, .height = null }, 1203 .max_size = .{ .width = size.width, .height = size.height }, 1204 }, root_focus); 1205 if (self.art.grid) |*grid| { 1206 for (grid.cells) |*cell| { 1207 cell.style.fg = dimColor(cell.style.fg); 1208 cell.style.bg = dimColor(cell.style.bg); 1209 } 1210 } 1211 } 1212 1213 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1214 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1215 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1216 // snap such channels clear of that range so the backdrop doesn't blink or 1217 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1218 // actually render truecolor. 1219 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1220 const c = color orelse return null; 1221 const snap = struct { 1222 fn f(v: u8) u8 { 1223 return if (v >= 1 and v <= 9) 10 else v; 1224 } 1225 }.f; 1226 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1227 } 1228 1229 fn applyArtBackground(dst: *Grid.Cell, src: Grid.Cell) void { 1230 const background_maybe = sgrSafe(src.style.bg orelse src.style.fg); 1231 dst.style.bg = background_maybe; 1232 // change unstyled text to contrast with the art behind it 1233 if (dst.style.fg == null) { 1234 if (background_maybe) |background| { 1235 // use near-black and near-white, which is easier on the eyes 1236 // while retaining high contrast 1237 const luminance = (@as(u32, background.r) * 299 + 1238 @as(u32, background.g) * 587 + 1239 @as(u32, background.b) * 114) / 1000; 1240 dst.style.fg = if (luminance >= 128) 1241 .{ .r = 16, .g = 16, .b = 16 } 1242 else 1243 .{ .r = 240, .g = 240, .b = 240 }; 1244 } 1245 } 1246 } 1247 1248 // composites ANSI art behind a foreground grid 1249 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 1250 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1251 errdefer out.deinit(); 1252 1253 const anchor_x = foreground.size.width -| art_grid.size.width; 1254 for (0..art_grid.size.height) |y| { 1255 for (0..art_grid.size.width) |x| { 1256 const src = (try art_grid.cell(x, y)).*; 1257 if (src.rune == null) continue; 1258 const dst = try out.cell(anchor_x + x, y); 1259 const wide = x + 1 < art_grid.size.width and (try art_grid.cell(x + 1, y)).continuation; 1260 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 1261 // blank cells take the art; occupied cells take its background 1262 if (cellIsBlank(dst.*) and (if (next_dst) |next| cellIsBlank(next.*) else true)) { 1263 dst.style = src.style; 1264 dst.style.fg = sgrSafe(dst.style.fg); 1265 dst.style.bg = sgrSafe(dst.style.bg); 1266 try out.setRune(anchor_x + x, y, src.rune); 1267 } else { 1268 applyArtBackground(dst, src); 1269 if (next_dst) |next| applyArtBackground(next, src); 1270 } 1271 } 1272 } 1273 return out; 1274 } 1275 }; 1276 1277 pub const CopyableText = struct { 1278 pub const Choice = struct { 1279 selector: []const u8 = "", 1280 text: []const u8, 1281 copyable_text: ?[]const u8 = null, 1282 label: []const u8 = "", 1283 bottom_label: []const u8 = "", 1284 }; 1285 1286 box: wgt.Box(Widget), 1287 session: *ui.Session, 1288 choices: []Choice, 1289 selected: usize = 0, 1290 1291 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice) !CopyableText { 1292 if (choices.len == 0) return error.MissingCopyableText; 1293 1294 const owned_choices = try allocator.dupe(Choice, choices); 1295 errdefer allocator.free(owned_choices); 1296 1297 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1298 errdefer box.deinit(allocator); 1299 1300 if (choices.len > 1) for (choices) |choice| { 1301 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 1302 errdefer selector_box.deinit(allocator); 1303 selector_box.getFocus().mode = .all; 1304 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 } }); 1305 }; 1306 1307 var text_input = try wgt.TextInput.init(allocator, .{ 1308 .border_style = .single, 1309 .rounded_corners = true, 1310 .label = choices[0].label, 1311 .bottom_label = choices[0].bottom_label, 1312 .read_only = true, 1313 .render_content = session.is_terminal, 1314 .visible_width = null, 1315 }); 1316 errdefer text_input.deinit(allocator); 1317 text_input.getFocus().mode = .all; 1318 try text_input.setContent(allocator, choices[0].text); 1319 box.getFocus().child_id = text_input.getFocus().id; 1320 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1321 1322 return .{ .box = box, .session = session, .choices = owned_choices }; 1323 } 1324 1325 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, location: ui.RoutablePage.RepoLocation) !?CopyableText { 1326 const aa = session.page_arena.allocator(); 1327 var choices: [2]Choice = undefined; 1328 var count: usize = 0; 1329 switch (location) { 1330 .repo => |identity| { 1331 if (session.data.git_http_port) |port| { 1332 const url = try std.fmt.allocPrint(aa, "http://localhost:{d}/repo/{s}", .{ port, identity }); 1333 choices[count] = .{ 1334 .selector = "http", 1335 .text = url, 1336 .copyable_text = try std.fmt.allocPrint(aa, "git clone {s}", .{url}), 1337 .label = " clone ", 1338 }; 1339 count += 1; 1340 } 1341 if (session.data.git_ssh_port) |port| { 1342 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}", .{ port, identity }); 1343 choices[count] = .{ 1344 .selector = "ssh", 1345 .text = url, 1346 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1347 .label = " clone ", 1348 }; 1349 count += 1; 1350 } 1351 }, 1352 .fork => |f| if (session.data.git_ssh_port) |port| { 1353 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/fork/{s}/patch:{s}", .{ port, f.identity, f.id }); 1354 choices[count] = .{ 1355 .text = url, 1356 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1357 .label = " clone ", 1358 }; 1359 count += 1; 1360 }, 1361 } 1362 if (count == 0) return null; 1363 return @as(?CopyableText, try init(allocator, session, choices[0..count])); 1364 } 1365 1366 pub fn minWidth(self: *const CopyableText) usize { 1367 var width = maxWidth(self.choices) + 2; 1368 if (self.choices.len > 1) { 1369 for (self.choices) |choice| width += choice.selector.len + 2; 1370 } 1371 return width; 1372 } 1373 1374 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1375 self.box.deinit(allocator); 1376 allocator.free(self.choices); 1377 } 1378 1379 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1380 self.clearGrid(); 1381 1382 if (self.choices.len > 1) { 1383 if (root_focus.grandchild_id) |id| { 1384 for (0..self.choices.len) |i| { 1385 if (id != self.selector(i).getFocus().id) continue; 1386 if (i != self.selected) try self.select(allocator, i); 1387 root_focus.setFocus(self.textInput().getFocus().id); 1388 break; 1389 } 1390 } 1391 1392 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 1393 for (0..self.choices.len) |i| { 1394 self.selector(i).options.border_style = if (i == self.selected) 1395 (if (focused) .double else .single) 1396 else 1397 .hidden; 1398 } 1399 } 1400 self.textInput().options.border_style = .single; 1401 self.textInput().cursor = 0; 1402 self.textInput().scroll_offset = 0; 1403 try self.box.build(allocator, constraint, root_focus); 1404 1405 if (!self.session.is_terminal) { 1406 const aa = self.session.arena.allocator(); 1407 const id = self.textInput().getFocus().id; 1408 try self.session.text_inputs.put(aa, id, self.textInput()); 1409 } 1410 } 1411 1412 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1413 switch (key) { 1414 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 1415 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 1416 .enter => if (self.session.is_terminal) self.show(), 1417 .mouse => |mouse| { 1418 if (self.choices.len > 1) for (0..self.choices.len) |i| { 1419 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 1420 try self.select(allocator, i); 1421 root_focus.setFocus(self.textInput().getFocus().id); 1422 return; 1423 }; 1424 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 1425 }, 1426 else => {}, 1427 } 1428 } 1429 1430 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 1431 self.selected = selected; 1432 const choice = self.choices[selected]; 1433 const text_input = self.textInput(); 1434 text_input.options.label = choice.label; 1435 text_input.options.bottom_label = choice.bottom_label; 1436 try text_input.setContent(allocator, choice.text); 1437 } 1438 1439 fn show(self: *CopyableText) void { 1440 const choice = self.choices[self.selected]; 1441 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 1442 } 1443 1444 fn textInput(self: *CopyableText) *wgt.TextInput { 1445 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 1446 } 1447 1448 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 1449 return &self.box.children.values()[index].widget.text_box; 1450 } 1451 1452 fn maxWidth(choices: []const Choice) usize { 1453 var width: usize = 0; 1454 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 1455 return width; 1456 } 1457 1458 pub fn clearGrid(self: *CopyableText) void { 1459 self.box.clearGrid(); 1460 } 1461 1462 pub fn getGrid(self: CopyableText) ?Grid { 1463 return self.box.getGrid(); 1464 } 1465 1466 pub fn getFocus(self: *CopyableText) *Focus { 1467 return self.box.getFocus(); 1468 } 1469 }; 1470