╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
261 var grid = try Grid.init(allocator, .{ .width = total_width, .height = content_height }); 262 errdefer grid.deinit(); 263 264 for (self.text_boxes.items, 0..) |*tb, i| { 265 const tb_grid = tb.getGrid() orelse continue; 266 const col = i % cols; 267 const row = i / cols; 268 const cell_x = col * cell_width; 269 const cell_y = row * slot_height; 270 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, cell_x, cell_y); 271 try grid.drawGrid(tb_grid, cell_x, cell_y); 272 } 273 274 self.grid = grid; 275 } 276 277 pub fn input(self: *FlowBox, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 278 _ = self; 279 _ = allocator; 280 _ = key; 281 _ = root_focus; 282 } 283 284 pub fn clearGrid(self: *FlowBox) void { 285 if (self.grid) |*grid| { 286 grid.deinit(); 287 self.grid = null; 288 } 289 for (self.text_boxes.items) |*tb| tb.clearGrid(); 290 } 291 292 pub fn getGrid(self: FlowBox) ?Grid { 293 return self.grid; 294 } 295 296 pub fn getFocus(self: *FlowBox) *Focus { 297 return self.focus; 298 } 299 300 pub fn cellRect(self: FlowBox, index: usize) ?layout.IRect { 301 if (self.last_cols == 0 or index >= self.text_boxes.items.len) return null; 302 const slot_height = self.options.cell_height + border_rows; 303 const col = index % self.last_cols; 304 const row = index / self.last_cols; 305 return .{ 306 .x = @intCast(col * self.options.cell_width), 307 .y = @intCast(row * slot_height), 308 .size = .{ .width = self.options.cell_width, .height = slot_height }, 309 }; 310 } 311 312 pub fn indexOfFocusId(self: FlowBox, focus_id: usize) ?usize { 313 for (self.text_boxes.items, 0..) |tb, i| { 314 if (tb.focus.id == focus_id) return i; 315 } 316 return null; 317 } 318 319 pub const Scroll = struct { 320 scroll: wgt.Scroll(Widget), 321 322 pub fn init(allocator: std.mem.Allocator, options: FlowBox.Options, web_native: bool) !Scroll { 323 var layout_inner = try FlowBox.init(allocator, options); 324 errdefer layout_inner.deinit(allocator); 325 var scroll = try wgt.Scroll(Widget).init(allocator, .{ .flow_box = layout_inner }, .{ .direction = .vert, .web_native = web_native }); 326 errdefer scroll.deinit(allocator); 327 return .{ .scroll = scroll }; 328 } 329 330 pub fn deinit(self: *Scroll, allocator: std.mem.Allocator) void { 331 self.scroll.deinit(allocator); 332 } 333 334 pub fn setItems(self: *Scroll, allocator: std.mem.Allocator, items: []const Item) !void { 335 self.scroll.x = 0; 336 self.scroll.y = 0; 337 try self.inner().setItems(allocator, items); 338 } 339 340 pub fn build(self: *Scroll, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 341 try self.scroll.build(allocator, constraint, root_focus); 342 } 343 344 pub fn input(self: *Scroll, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 345 _ = allocator; 346 const in = self.inner(); 347 const child_id = in.focus.child_id orelse return; 348 const current_index = in.indexOfFocusId(child_id) orelse return; 349 const count = in.text_boxes.items.len; 350 if (count == 0) return; 351 const cols = in.last_cols; 352 const slot_height = in.options.cell_height + FlowBox.border_rows; 353 354 var index = current_index; 355 switch (key) { 356 .arrow_up => index -|= cols, 357 .arrow_down => if (index + cols < count) { 358 index += cols; 359 }, 360 .arrow_left => index -|= 1, 361 .arrow_right => if (index + 1 < count) { 362 index += 1; 363 }, 364 .home => index = 0, 365 .end => index = count - 1, 366 .page_up => { 367 if (self.scroll.grid) |grid| if (slot_height > 0) { 368 const rows_per_page = grid.size.height / slot_height; 369 index -|= rows_per_page * cols; 370 }; 371 }, 372 .page_down => { 373 if (self.scroll.grid) |grid| if (slot_height > 0) { 374 const rows_per_page = grid.size.height / slot_height; 375 index = @min(index + rows_per_page * cols, count - 1); 376 }; 377 }, 378 // scroll wheel moves the focused cell by a full row so the 379 // viewport (via scrollToRect) follows in row-sized steps, 380 // matching how a scroll wheel feels in a grid view. 381 .mouse => |mouse| switch (mouse.action) { 382 .scroll => |dir| switch (dir) { 383 .up => index -|= cols, 384 .down => if (index + cols < count) { 385 index += cols; 386 }, 387 }, 388 else => {}, 389 }, 390 else => {}, 391 } 392 393 if (index != current_index) { 394 root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 395 if (in.cellRect(index)) |rect| { 396 self.scroll.scrollToRect(rect); 397 } 398 } 399 } 400 401 pub fn clearGrid(self: *Scroll) void { 402 self.scroll.clearGrid(); 403 } 404 405 pub fn getGrid(self: Scroll) ?Grid { 406 return self.scroll.getGrid(); 407 } 408 409 pub fn getFocus(self: *Scroll) *Focus { 410 return self.scroll.getFocus(); 411 } 412 413 pub fn getSelectedIndex(self: Scroll) ?usize { 414 const in = self.scroll.child.flow_box; 415 const child_id = in.focus.child_id orelse return null; 416 return in.indexOfFocusId(child_id); 417 } 418 419 fn inner(self: *Scroll) *FlowBox { 420 return &self.scroll.child.flow_box; 421 } 422 }; 423 }; 424 425 // a left-to-right flow of variable-width focusable items that wraps at the 426 // available width, like words in a paragraph. unlike FlowBox there is no cell 427 // grid: each item is as wide as its text. selection movement is driven by the 428 // owning view via indexOfFocusId/verticalNeighbor. 429 pub const TagFlow = struct { 430 focus: *Focus, 431 grid: ?Grid, 432 text_boxes: std.ArrayList(wgt.TextBox), 433 // last build's item rects (content space), for vertical navigation and 434 // scroll-into-view. 435 rects: std.ArrayList(layout.IRect), 436 // backs each link item's `.custom` focus kind. 437 arena: std.heap.ArenaAllocator, 438 439 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 440 441 pub fn init(allocator: std.mem.Allocator) !TagFlow { 442 return .{ 443 .focus = try Focus.create(allocator, .container), 444 .grid = null, 445 .text_boxes = .empty, 446 .rects = .empty, 447 .arena = std.heap.ArenaAllocator.init(allocator), 448 }; 449 } 450 451 pub fn deinit(self: *TagFlow, allocator: std.mem.Allocator) void { 452 self.focus.destroy(allocator); 453 if (self.grid) |*grid| { 454 grid.deinit(); 455 self.grid = null; 456 } 457 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 458 self.text_boxes.deinit(allocator); 459 self.rects.deinit(allocator); 460 self.arena.deinit(); 461 } 462 463 // the text box copies the item text; links are copied into the arena, so the 464 // caller's slices needn't outlive this call. 465 pub fn setItems(self: *TagFlow, allocator: std.mem.Allocator, items: []const Item) !void { 466 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 467 self.text_boxes.clearAndFree(allocator); 468 469 _ = self.arena.reset(.retain_capacity); 470 const aa = self.arena.allocator(); 471 472 self.focus.clear(); 473 self.focus.child_id = null; 474 475 for (items) |item| { 476 var text_box = try wgt.TextBox.init(allocator, item.text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 477 errdefer text_box.deinit(allocator); 478 text_box.getFocus().mode = .all; 479 480 if (item.link.len > 0) { 481 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 482 } 483 484 try self.text_boxes.append(allocator, text_box); 485 } 486 487 if (self.text_boxes.items.len > 0) { 488 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 489 } 490 } 491 492 pub fn build(self: *TagFlow, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 493 self.clearGrid(); 494 self.focus.clear(); 495 496 if (self.text_boxes.items.len == 0) return; 497 const max_width = constraint.max_size.width orelse 80; 498 if (max_width == 0) return; 499 500 // lay the items out left to right, wrapping to a new row when the next 501 // one wouldn't fit. 502 self.rects.clearRetainingCapacity(); 503 var x: usize = 0; 504 var y: usize = 0; 505 var row_height: usize = 0; 506 var total_width: usize = 0; 507 for (self.text_boxes.items) |*tb| { 508 try tb.build(allocator, .{ 509 .min_size = .{ .width = null, .height = null }, 510 .max_size = .{ .width = max_width, .height = null }, 511 }, root_focus); 512 const tb_grid_maybe = tb.getGrid(); 513 const w: usize = if (tb_grid_maybe) |g| g.size.width else 0; 514 const h: usize = if (tb_grid_maybe) |g| g.size.height else 0; 515 if (x > 0 and x + w > max_width) { 516 x = 0; 517 y += row_height; 518 row_height = 0; 519 } 520 try self.rects.append(allocator, .{ .x = @intCast(x), .y = @intCast(y), .size = .{ .width = w, .height = h } }); 521 if (h > row_height) row_height = h; 522 x += w; 523 if (x > total_width) total_width = x; 524 } 525 526 const total_height = y + row_height; 527 if (total_width == 0 or total_height == 0) return; 528 529 var grid = try Grid.init(allocator, .{ .width = total_width, .height = total_height }); 530 errdefer grid.deinit(); 531 532 for (self.text_boxes.items, self.rects.items) |*tb, rect| { 533 const tb_grid = tb.getGrid() orelse continue; 534 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, @intCast(rect.x), @intCast(rect.y)); 535 try grid.drawGrid(tb_grid, @intCast(rect.x), @intCast(rect.y)); 536 } 537 538 self.grid = grid; 539 } 540 541 pub fn input(self: *TagFlow, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 542 _ = self; 543 _ = allocator; 544 _ = key; 545 _ = root_focus; 546 } 547 548 pub fn clearGrid(self: *TagFlow) void { 549 if (self.grid) |*grid| { 550 grid.deinit(); 551 self.grid = null; 552 } 553 for (self.text_boxes.items) |*tb| tb.clearGrid(); 554 } 555 556 pub fn getGrid(self: TagFlow) ?Grid { 557 return self.grid; 558 } 559 560 pub fn getFocus(self: *TagFlow) *Focus { 561 return self.focus; 562 } 563 564 pub fn indexOfFocusId(self: TagFlow, focus_id: usize) ?usize { 565 for (self.text_boxes.items, 0..) |tb, i| { 566 if (tb.focus.id == focus_id) return i; 567 } 568 return null; 569 } 570 571 // the first item of the row below (when `downward`) or above, or null at 572 // the flow's edge. items are laid out in order, so y is nondecreasing. 573 pub fn rowStep(self: TagFlow, index: usize, downward: bool) ?usize { 574 const rects = self.rects.items; 575 if (index >= rects.len) return null; 576 const cur_y = rects[index].y; 577 if (downward) { 578 for (rects[index + 1 ..], index + 1..) |r, i| { 579 if (r.y > cur_y) return i; 580 } 581 return null; 582 } 583 // walk the earlier rows; the last one visited is the row above. 584 var prev_first: ?usize = null; 585 var i: usize = 0; 586 while (i < rects.len and rects[i].y < cur_y) { 587 prev_first = i; 588 const y = rects[i].y; 589 while (i < rects.len and rects[i].y == y) i += 1; 590 } 591 return prev_first; 592 } 593 }; 594 595 // an invisible widget that fills the horizontal space granted by its parent. 596 // used inside Box(horiz) with a min_size so the box reserves space for the 597 // children that follow, pushing them to the right. 598 pub const Spacer = struct { 599 focus: *Focus, 600 grid: ?Grid, 601 602 pub fn init(allocator: std.mem.Allocator) !Spacer { 603 return .{ 604 .focus = try Focus.create(allocator, .container), 605 .grid = null, 606 }; 607 } 608 609 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 610 self.focus.destroy(allocator); 611 if (self.grid) |*grid| { 612 grid.deinit(); 613 self.grid = null; 614 } 615 } 616 617 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 618 _ = root_focus; 619 self.clearGrid(); 620 const width = constraint.max_size.width orelse return; 621 if (width == 0) return; 622 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 623 } 624 625 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 626 _ = self; 627 _ = allocator; 628 _ = key; 629 _ = root_focus; 630 } 631 632 pub fn clearGrid(self: *Spacer) void { 633 if (self.grid) |*grid| { 634 grid.deinit(); 635 self.grid = null; 636 } 637 } 638 639 pub fn getGrid(self: Spacer) ?Grid { 640 return self.grid; 641 } 642 643 pub fn getFocus(self: *Spacer) *Focus { 644 return self.focus; 645 } 646 }; 647 648 // a one-row, non-focusable status bar showing the current page's url 649 pub const Footer = struct { 650 focus: *Focus, 651 grid: ?Grid, 652 session: *ui.Session, 653 arena: std.heap.ArenaAllocator, 654 url: []const u8 = "", 655 url_width: usize = 0, 656 657 pub fn init(allocator: std.mem.Allocator, session: *ui.Session) !Footer { 658 return .{ 659 .focus = try Focus.create(allocator, .container), 660 .grid = null, 661 .session = session, 662 .arena = std.heap.ArenaAllocator.init(allocator), 663 }; 664 } 665 666 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 667 self.focus.destroy(allocator); 668 if (self.grid) |*grid| grid.deinit(); 669 self.arena.deinit(); 670 } 671 672 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 673 _ = root_focus; 674 self.clearGrid(); 675 const width = constraint.max_size.width orelse return; 676 if (width == 0) return; 677 678 _ = self.arena.reset(.retain_capacity); 679 self.url = ""; 680 self.url_width = 0; 681 const aa = self.arena.allocator(); 682 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 683 const text = ui.terminalWebUrl(aa, self.session, path) catch return; 684 self.url = text; 685 686 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 687 errdefer grid.deinit(); 688 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 689 var i: usize = 0; 690 while (utf8.nextCodepoint()) |ch| { 691 const rune_width = xitui.width.cellWidth(ch); 692 if (i + rune_width > width) break; 693 try grid.setRune(i, 0, ch); 694 i += rune_width; 695 } 696 self.url_width = i; 697 self.grid = grid; 698 } 699 700 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 701 _ = allocator; 702 switch (key) { 703 .mouse => |mouse| { 704 if (mouse.ctrl or !inp.leftClickOn(root_focus, self.getFocus().id, mouse)) return; 705 const rect = (root_focus.children.get(self.getFocus().id) orelse return).rect; 706 if (mouse.x < rect.x + self.url_width) self.session.host_request = .{ .show_copyable_text = self.url }; 707 }, 708 else => {}, 709 } 710 } 711 712 pub fn clearGrid(self: *Footer) void { 713 if (self.grid) |*grid| { 714 grid.deinit(); 715 self.grid = null; 716 } 717 } 718 719 pub fn getGrid(self: Footer) ?Grid { 720 return self.grid; 721 } 722 723 pub fn getFocus(self: *Footer) *Focus { 724 return self.focus; 725 } 726 }; 727 728 // a single-child wrapper that builds the child at its natural size and 729 // positions its grid in the middle of the area granted by the parent 730 // a bordered label, indented past the content it precedes 731 pub const SectionLabel = struct { 732 box: wgt.Box(Widget), 733 734 pub fn init(allocator: std.mem.Allocator, content: []const u8) !SectionLabel { 735 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 736 errdefer box.deinit(allocator); 737 738 { 739 var gap = try wgt.Text.init(allocator, " "); 740 errdefer gap.deinit(allocator); 741 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 742 } 743 744 { 745 var tb = try wgt.TextBox.init(allocator, content, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 746 errdefer tb.deinit(allocator); 747 tb.getFocus().mode = .all; 748 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 749 } 750 751 box.getFocus().child_id = box.children.keys()[1]; 752 return .{ .box = box }; 753 } 754 755 pub fn deinit(self: *SectionLabel, allocator: std.mem.Allocator) void { 756 self.box.deinit(allocator); 757 } 758 759 pub fn build(self: *SectionLabel, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 760 try self.box.build(allocator, constraint, root_focus); 761 } 762 763 pub fn input(self: *SectionLabel, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 764 _ = self; 765 _ = allocator; 766 _ = key; 767 _ = root_focus; 768 } 769 770 pub fn clearGrid(self: *SectionLabel) void { 771 self.box.clearGrid(); 772 } 773 774 pub fn getGrid(self: SectionLabel) ?Grid { 775 return self.box.getGrid(); 776 } 777 778 pub fn getFocus(self: *SectionLabel) *Focus { 779 return self.box.getFocus(); 780 } 781 }; 782 783 // a submit button, indented past its form's content. the web form POSTs 784 // the button's form; terminal hosts detect presses through `buttonId`. 785 pub const SubmitButton = struct { 786 box: wgt.Box(Widget), 787 788 pub fn init(allocator: std.mem.Allocator) !SubmitButton { 789 return initLabeled(allocator, "submit"); 790 } 791 792 pub fn initLabeled(allocator: std.mem.Allocator, label: []const u8) !SubmitButton { 793 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 794 errdefer box.deinit(allocator); 795 796 { 797 var gap = try wgt.Text.init(allocator, " "); 798 errdefer gap.deinit(allocator); 799 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 800 } 801 802 { 803 var button = try wgt.TextBox.init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 804 errdefer button.deinit(allocator); 805 button.getFocus().mode = .all; 806 // the renderer distinguishes plain clickables from buttons that 807 // should POST to a server route by this kind. 808 button.getFocus().kind = .{ .custom = "submit" }; 809 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 810 } 811 812 box.getFocus().child_id = box.children.keys()[1]; 813 return .{ .box = box }; 814 } 815 816 pub fn deinit(self: *SubmitButton, allocator: std.mem.Allocator) void { 817 self.box.deinit(allocator); 818 } 819 820 pub fn build(self: *SubmitButton, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 821 try self.box.build(allocator, constraint, root_focus); 822 } 823 824 pub fn input(self: *SubmitButton, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 825 _ = self; 826 _ = allocator; 827 _ = key; 828 _ = root_focus; 829 } 830 831 pub fn clearGrid(self: *SubmitButton) void { 832 self.box.clearGrid(); 833 } 834 835 pub fn getGrid(self: SubmitButton) ?Grid { 836 return self.box.getGrid(); 837 } 838 839 pub fn getFocus(self: *SubmitButton) *Focus { 840 return self.box.getFocus(); 841 } 842 843 // the button's focus id, for click hit-testing 844 pub fn buttonId(self: *const SubmitButton) usize { 845 return self.box.children.keys()[1]; 846 } 847 }; 848 849 pub const Center = struct { 850 focus: *Focus, 851 grid: ?Grid, 852 child: *Widget, 853 854 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 855 const child = try allocator.create(Widget); 856 errdefer allocator.destroy(child); 857 child.* = child_widget; 858 return .{ 859 .focus = try Focus.create(allocator, .container), 860 .grid = null, 861 .child = child, 862 }; 863 } 864 865 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 866 self.focus.destroy(allocator); 867 if (self.grid) |*grid| { 868 grid.deinit(); 869 self.grid = null; 870 } 871 self.child.deinit(allocator); 872 allocator.destroy(self.child); 873 } 874 875 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 876 self.clearGrid(); 877 self.getFocus().clear(); 878 879 // build the child without forcing it to fill min_size; it sizes 880 // itself to its content within the available max. 881 try self.child.build(allocator, .{ 882 .min_size = .{ .width = null, .height = null }, 883 .max_size = constraint.max_size, 884 }, root_focus); 885 886 const child_grid = self.child.getGrid() orelse return; 887 888 // prefer max when given; otherwise grow to max(min, child) so that 889 // when only a min is set (e.g. the wasm path passes viewport rows 890 // as min_height with no max), we can still vertically center while 891 // letting taller content extend past the min. 892 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; 893 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; 894 if (width == 0 or height == 0) return; 895 896 const offset_x: usize = (width -| child_grid.size.width) / 2; 897 const offset_y: usize = (height -| child_grid.size.height) / 2; 898 899 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 900 errdefer grid.deinit(); 901 try grid.drawGrid(child_grid, offset_x, offset_y); 902 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 903 self.getFocus().child_id = self.child.getFocus().id; 904 905 self.grid = grid; 906 } 907 908 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 909 try self.child.input(allocator, key, root_focus); 910 } 911 912 pub fn clearGrid(self: *Center) void { 913 if (self.grid) |*grid| { 914 grid.deinit(); 915 self.grid = null; 916 } 917 self.child.clearGrid(); 918 } 919 920 pub fn getGrid(self: Center) ?Grid { 921 return self.grid; 922 } 923 924 pub fn getFocus(self: *Center) *Focus { 925 return self.focus; 926 } 927 }; 928 929 // renders truecolor ANSI art 930 pub const AnsiArt = struct { 931 focus: *Focus, 932 grid: ?Grid, 933 content: []const u8, 934 935 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 936 return .{ 937 .focus = try Focus.create(allocator, .container), 938 .grid = null, 939 .content = content, 940 }; 941 } 942 943 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 944 self.focus.destroy(allocator); 945 if (self.grid) |*grid| { 946 grid.deinit(); 947 self.grid = null; 948 } 949 } 950 951 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 952 _ = root_focus; 953 self.clearGrid(); 954 955 // parse into rows of cells, then copy into a rectangular grid 956 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 957 defer { 958 for (rows.items) |*row| row.deinit(allocator); 959 rows.deinit(allocator); 960 } 961 var row: std.ArrayList(Grid.Cell) = .empty; 962 errdefer row.deinit(allocator); 963 964 var style: Grid.Style = .{}; 965 var width: usize = 0; 966 const content = self.content; 967 var i: usize = 0; 968 while (i < content.len) { 969 const byte = content[i]; 970 if (byte == '\n') { 971 width = @max(width, row.items.len); 972 try rows.append(allocator, row); 973 row = .empty; 974 i += 1; 975 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 976 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 977 var j = i + 2; 978 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 979 if (j >= content.len) { 980 i = content.len; // malformed trailing escape; stop 981 } else { 982 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 983 i = j + 1; 984 } 985 } else { 986 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 987 const end = @min(content.len, i + len); 988 const rune = content[i..end]; 989 const codepoint = std.unicode.utf8Decode(rune) catch '�'; 990 // a foreground color does not make a space visible. only an 991 // effective background makes it opaque; foreground state often 992 // remains active through the padding at the end of a row. 993 const effective_background = if (style.inverted) style.fg else style.bg; 994 const transparent = end - i == 1 and rune[0] == ' ' and effective_background == null; 995 try row.append(allocator, .{ 996 .rune = if (transparent) null else codepoint, 997 .style = style, 998 }); 999 if (!transparent and xitui.width.cellWidth(codepoint) == 2) { 1000 try row.append(allocator, .{ .rune = null, .style = style, .continuation = true }); 1001 } 1002 i = end; 1003 } 1004 } 1005 // a trailing row with content but no closing newline 1006 if (row.items.len > 0) { 1007 width = @max(width, row.items.len); 1008 try rows.append(allocator, row); 1009 } else { 1010 row.deinit(allocator); 1011 } 1012 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1013 1014 const height = rows.items.len; 1015 if (width == 0 or height == 0) return; 1016 1017 const clamped_w = @min(width, constraint.max_size.width orelse width); 1018 const clamped_h = @min(height, constraint.max_size.height orelse height); 1019 1020 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1021 errdefer grid.deinit(); 1022 for (rows.items[0..clamped_h], 0..) |r, y| { 1023 const n = @min(r.items.len, clamped_w); 1024 for (r.items[0..n], 0..) |cell, x| { 1025 if (cell.continuation) continue; 1026 (try grid.cell(x, y)).style = cell.style; 1027 try grid.setRune(x, y, cell.rune); 1028 } 1029 } 1030 self.grid = grid; 1031 } 1032 1033 pub fn clearGrid(self: *AnsiArt) void { 1034 if (self.grid) |*grid| { 1035 grid.deinit(); 1036 self.grid = null; 1037 } 1038 } 1039 1040 pub fn getGrid(self: AnsiArt) ?Grid { 1041 return self.grid; 1042 } 1043 1044 pub fn getFocus(self: *AnsiArt) *Focus { 1045 return self.focus; 1046 } 1047 1048 fn applySgr(style: *Grid.Style, params: []const u8) void { 1049 var nums: [16]u32 = undefined; 1050 var n: usize = 0; 1051 var it = std.mem.splitScalar(u8, params, ';'); 1052 while (it.next()) |tok| { 1053 if (n >= nums.len) break; 1054 // an empty parameter (e.g. bare "\x1b[m") means 0 1055 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1056 n += 1; 1057 } 1058 if (n == 0) { 1059 style.* = .{}; 1060 return; 1061 } 1062 var i: usize = 0; 1063 while (i < n) : (i += 1) { 1064 switch (nums[i]) { 1065 0 => style.* = .{}, 1066 7 => style.inverted = true, 1067 27 => style.inverted = false, 1068 39 => style.fg = null, 1069 49 => style.bg = null, 1070 38, 48 => { 1071 // truecolor form: 38;2;r;g;b — anything else is ignored 1072 if (i + 4 < n and nums[i + 1] == 2) { 1073 const c = Grid.Color{ 1074 .r = @truncate(nums[i + 2]), 1075 .g = @truncate(nums[i + 3]), 1076 .b = @truncate(nums[i + 4]), 1077 }; 1078 if (nums[i] == 38) style.fg = c else style.bg = c; 1079 i += 4; 1080 } 1081 }, 1082 else => {}, 1083 } 1084 } 1085 } 1086 }; 1087 1088 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1089 pub const AnsiBackground = struct { 1090 grid: ?Grid, 1091 child: *Widget, 1092 art: AnsiArt, 1093 session: *ui.Session, 1094 1095 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, session: *ui.Session) !AnsiBackground { 1096 var cw = child_widget; 1097 const child = allocator.create(Widget) catch |e| { 1098 cw.deinit(allocator); 1099 return e; 1100 }; 1101 child.* = cw; 1102 errdefer { 1103 child.deinit(allocator); 1104 allocator.destroy(child); 1105 } 1106 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, session.data.ansi_art), .session = session }; 1107 } 1108 1109 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1110 if (self.grid) |*grid| grid.deinit(); 1111 self.art.deinit(allocator); 1112 self.child.deinit(allocator); 1113 allocator.destroy(self.child); 1114 } 1115 1116 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1117 self.clearGrid(); 1118 1119 // make the wrapped page fill the whole available area 1120 try self.child.build(allocator, .{ 1121 .min_size = .{ 1122 .width = constraint.max_size.width orelse constraint.min_size.width, 1123 .height = constraint.max_size.height orelse constraint.min_size.height, 1124 }, 1125 .max_size = constraint.max_size, 1126 }, root_focus); 1127 1128 if (!self.session.data.enable_ansi) return; 1129 const foreground = self.child.getGrid() orelse return; 1130 self.art.content = self.session.data.ansi_art; 1131 try self.buildArt(allocator, foreground.size, root_focus); 1132 if (!self.session.is_terminal) return; 1133 const art_grid = self.art.getGrid() orelse return; 1134 self.grid = try artBehind(allocator, foreground, art_grid); 1135 } 1136 1137 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1138 try self.child.input(allocator, key, root_focus); 1139 } 1140 1141 pub fn clearGrid(self: *AnsiBackground) void { 1142 if (self.grid) |*grid| { 1143 grid.deinit(); 1144 self.grid = null; 1145 } 1146 self.child.clearGrid(); 1147 self.art.clearGrid(); 1148 } 1149 1150 pub fn getGrid(self: AnsiBackground) ?Grid { 1151 return self.grid orelse self.child.getGrid(); 1152 } 1153 1154 pub fn getFocus(self: *AnsiBackground) *Focus { 1155 return self.child.getFocus(); 1156 } 1157 1158 // a foreground cell counts as blank if it's empty or a bare space without styling 1159 fn cellIsBlank(cell: Grid.Cell) bool { 1160 if (cell.continuation) return false; 1161 if (cell.rune) |rune| { 1162 return rune == ' ' and 1163 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1164 } 1165 return true; 1166 } 1167 1168 // the art is dimmed to this fraction of its brightness so foreground text 1169 // stays legible over it 1170 const art_brightness = 35; // percent 1171 1172 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1173 const c = color orelse return null; 1174 return .{ 1175 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 1176 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 1177 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 1178 }; 1179 } 1180 1181 fn buildArt(self: *AnsiBackground, allocator: std.mem.Allocator, size: layout.Size, root_focus: *Focus) !void { 1182 try self.art.build(allocator, .{ 1183 .min_size = .{ .width = null, .height = null }, 1184 .max_size = .{ .width = size.width, .height = size.height }, 1185 }, root_focus); 1186 if (self.art.grid) |*grid| { 1187 for (grid.cells) |*cell| { 1188 cell.style.fg = dimColor(cell.style.fg); 1189 cell.style.bg = dimColor(cell.style.bg); 1190 } 1191 } 1192 } 1193 1194 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1195 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1196 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1197 // snap such channels clear of that range so the backdrop doesn't blink or 1198 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1199 // actually render truecolor. 1200 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1201 const c = color orelse return null; 1202 const snap = struct { 1203 fn f(v: u8) u8 { 1204 return if (v >= 1 and v <= 9) 10 else v; 1205 } 1206 }.f; 1207 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1208 } 1209 1210 fn applyArtBackground(dst: *Grid.Cell, src: Grid.Cell) void { 1211 const background_maybe = sgrSafe(src.style.bg orelse src.style.fg); 1212 dst.style.bg = background_maybe; 1213 // change unstyled text to contrast with the art behind it 1214 if (dst.style.fg == null) { 1215 if (background_maybe) |background| { 1216 // use near-black and near-white, which is easier on the eyes 1217 // while retaining high contrast 1218 const luminance = (@as(u32, background.r) * 299 + 1219 @as(u32, background.g) * 587 + 1220 @as(u32, background.b) * 114) / 1000; 1221 dst.style.fg = if (luminance >= 128) 1222 .{ .r = 16, .g = 16, .b = 16 } 1223 else 1224 .{ .r = 240, .g = 240, .b = 240 }; 1225 } 1226 } 1227 } 1228 1229 // composites ANSI art behind a foreground grid 1230 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 1231 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1232 errdefer out.deinit(); 1233 1234 const anchor_x = foreground.size.width -| art_grid.size.width; 1235 for (0..art_grid.size.height) |y| { 1236 for (0..art_grid.size.width) |x| { 1237 const src = (try art_grid.cell(x, y)).*; 1238 if (src.rune == null) continue; 1239 const dst = try out.cell(anchor_x + x, y); 1240 const wide = x + 1 < art_grid.size.width and (try art_grid.cell(x + 1, y)).continuation; 1241 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 1242 // blank cells take the art; occupied cells take its background 1243 if (cellIsBlank(dst.*) and (next_dst == null or cellIsBlank(next_dst.?.*))) { 1244 dst.style = src.style; 1245 dst.style.fg = sgrSafe(dst.style.fg); 1246 dst.style.bg = sgrSafe(dst.style.bg); 1247 try out.setRune(anchor_x + x, y, src.rune); 1248 } else { 1249 applyArtBackground(dst, src); 1250 if (next_dst) |next| applyArtBackground(next, src); 1251 } 1252 } 1253 } 1254 return out; 1255 } 1256 }; 1257 1258 pub const CopyableText = struct { 1259 pub const Choice = struct { 1260 selector: []const u8 = "", 1261 text: []const u8, 1262 copyable_text: ?[]const u8 = null, 1263 label: []const u8 = "", 1264 bottom_label: []const u8 = "", 1265 }; 1266 1267 box: wgt.Box(Widget), 1268 session: *ui.Session, 1269 choices: []Choice, 1270 selected: usize = 0, 1271 1272 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice) !CopyableText { 1273 if (choices.len == 0) return error.MissingCopyableText; 1274 1275 const owned_choices = try allocator.dupe(Choice, choices); 1276 errdefer allocator.free(owned_choices); 1277 1278 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1279 errdefer box.deinit(allocator); 1280 1281 if (choices.len > 1) for (choices) |choice| { 1282 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border_style = .hidden, .wrap_kind = .none }); 1283 errdefer selector_box.deinit(allocator); 1284 selector_box.getFocus().mode = .all; 1285 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 } }); 1286 }; 1287 1288 var text_input = try wgt.TextInput.init(allocator, .{ 1289 .border_style = .single, 1290 .label = choices[0].label, 1291 .bottom_label = choices[0].bottom_label, 1292 .read_only = true, 1293 .render_content = session.is_terminal, 1294 .visible_width = null, 1295 }); 1296 errdefer text_input.deinit(allocator); 1297 text_input.getFocus().mode = .all; 1298 try text_input.setContent(allocator, choices[0].text); 1299 box.getFocus().child_id = text_input.getFocus().id; 1300 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1301 1302 return .{ .box = box, .session = session, .choices = owned_choices }; 1303 } 1304 1305 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, location: ui.RoutablePage.RepoLocation) !?CopyableText { 1306 const aa = session.page_arena.allocator(); 1307 var choices: [2]Choice = undefined; 1308 var count: usize = 0; 1309 switch (location) { 1310 .repo => |identity| { 1311 if (session.data.git_http_port) |port| { 1312 const url = try std.fmt.allocPrint(aa, "http://localhost:{d}/repo/{s}", .{ port, identity }); 1313 choices[count] = .{ 1314 .selector = "http", 1315 .text = url, 1316 .copyable_text = try std.fmt.allocPrint(aa, "git clone {s}", .{url}), 1317 .label = " clone ", 1318 }; 1319 count += 1; 1320 } 1321 if (session.data.git_ssh_port) |port| { 1322 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}", .{ port, identity }); 1323 choices[count] = .{ 1324 .selector = "ssh", 1325 .text = url, 1326 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1327 .label = " clone ", 1328 }; 1329 count += 1; 1330 } 1331 }, 1332 .fork => |f| if (session.data.git_ssh_port) |port| { 1333 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/fork/{s}/patch:{s}", .{ port, f.identity, f.id }); 1334 choices[count] = .{ 1335 .text = url, 1336 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1337 .label = " clone ", 1338 }; 1339 count += 1; 1340 }, 1341 } 1342 if (count == 0) return null; 1343 return @as(?CopyableText, try init(allocator, session, choices[0..count])); 1344 } 1345 1346 pub fn minWidth(self: *const CopyableText) usize { 1347 var width = maxWidth(self.choices) + 2; 1348 if (self.choices.len > 1) { 1349 for (self.choices) |choice| width += choice.selector.len + 2; 1350 } 1351 return width; 1352 } 1353 1354 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1355 self.box.deinit(allocator); 1356 allocator.free(self.choices); 1357 } 1358 1359 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1360 self.clearGrid(); 1361 1362 if (self.choices.len > 1) { 1363 if (root_focus.grandchild_id) |id| { 1364 for (0..self.choices.len) |i| { 1365 if (id != self.selector(i).getFocus().id) continue; 1366 if (i != self.selected) try self.select(allocator, i); 1367 root_focus.setFocus(self.textInput().getFocus().id); 1368 break; 1369 } 1370 } 1371 1372 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 1373 for (0..self.choices.len) |i| { 1374 self.selector(i).options.border_style = if (i == self.selected) 1375 (if (focused) .double else .single) 1376 else 1377 .hidden; 1378 } 1379 } 1380 self.textInput().options.border_style = .single; 1381 self.textInput().cursor = 0; 1382 self.textInput().scroll_offset = 0; 1383 try self.box.build(allocator, constraint, root_focus); 1384 1385 if (!self.session.is_terminal) { 1386 const aa = self.session.arena.allocator(); 1387 const id = self.textInput().getFocus().id; 1388 try self.session.text_inputs.put(aa, id, self.textInput()); 1389 } 1390 } 1391 1392 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1393 switch (key) { 1394 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 1395 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 1396 .enter => if (self.session.is_terminal) self.show(), 1397 .mouse => |mouse| { 1398 if (self.choices.len > 1) for (0..self.choices.len) |i| { 1399 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 1400 try self.select(allocator, i); 1401 root_focus.setFocus(self.textInput().getFocus().id); 1402 return; 1403 }; 1404 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 1405 }, 1406 else => {}, 1407 } 1408 } 1409 1410 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 1411 self.selected = selected; 1412 const choice = self.choices[selected]; 1413 const text_input = self.textInput(); 1414 text_input.options.label = choice.label; 1415 text_input.options.bottom_label = choice.bottom_label; 1416 try text_input.setContent(allocator, choice.text); 1417 } 1418 1419 fn show(self: *CopyableText) void { 1420 const choice = self.choices[self.selected]; 1421 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 1422 } 1423 1424 fn textInput(self: *CopyableText) *wgt.TextInput { 1425 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 1426 } 1427 1428 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 1429 return &self.box.children.values()[index].widget.text_box; 1430 } 1431 1432 fn maxWidth(choices: []const Choice) usize { 1433 var width: usize = 0; 1434 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 1435 return width; 1436 } 1437 1438 pub fn clearGrid(self: *CopyableText) void { 1439 self.box.clearGrid(); 1440 } 1441 1442 pub fn getGrid(self: CopyableText) ?Grid { 1443 return self.box.getGrid(); 1444 } 1445 1446 pub fn getFocus(self: *CopyableText) *Focus { 1447 return self.box.getFocus(); 1448 } 1449 }; 1450