╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Undo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
451 details.getFocus().child_id = details.children.keys()[0]; 452 } 453 break :blk try wgt.Scroll(ui.Widget).init(allocator, .{ .box = details }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 454 }; 455 errdefer scroll.deinit(allocator); 456 scroll.getFocus().mode = .mouse; 457 try box.children.put(allocator, scroll.getFocus().id, .{ .widget = .{ .scroll = scroll }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 458 } 459 box.getFocus().child_id = box.children.keys()[0]; 460 try outer.children.put(allocator, box.getFocus().id, .{ .widget = .{ .box = box }, .rect = null, .min_size = null }); 461 outer.getFocus().child_id = outer.children.keys()[content_index]; 462 return .{ .box = outer, .data = data, .session = session }; 463 } 464 465 // the same shape the thread views use to confirm a removal 466 fn initClearForm(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !ui.widget.Center { 467 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .round_corners = true, .direction = .vert }); 468 errdefer box.deinit(allocator); 469 const route = ui.RoutablePage.repoUndoClearRoute(data.identity) orelse return error.RouteTooLong; 470 box.getFocus().kind = .{ .custom = try std.fmt.allocPrint(session.page_arena.allocator(), "form:{s}", .{try route.toUrl(session.page_arena)}) }; 471 472 var prompt = try wgt.Text.init(allocator, "are you sure?"); 473 errdefer prompt.deinit(allocator); 474 try box.children.put(allocator, prompt.getFocus().id, .{ .widget = .{ .text = prompt }, .rect = null, .min_size = null }); 475 476 var button = try wgt.TextBox.init(allocator, "clear undo history", .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 477 errdefer button.deinit(allocator); 478 button.getFocus().mode = .all; 479 button.getFocus().kind = if (data.handle.canUndo()) .{ .custom = "submit" } else .text_box; 480 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 481 box.getFocus().child_id = button.getFocus().id; 482 483 return ui.widget.Center.init(allocator, .{ .box = box }); 484 } 485 486 fn addText(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), text: []const u8, link: ?[]const u8, border: wgt.BorderStyle) !void { 487 var row = try wgt.TextBox.init(allocator, text, .{ .border_style = border, .round_corners = true, .wrap_kind = .word }); 488 errdefer row.deinit(allocator); 489 row.getFocus().mode = .all; 490 if (link) |value| row.getFocus().kind = .{ .custom = value }; 491 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null }); 492 } 493 494 // a labeled row below the timestamp, added as the selected action needs it 495 fn addLabeled(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, text: []const u8, link: ?[]const u8) !void { 496 try addText(allocator, box, text, link, .single); 497 box.children.values()[box.children.count() - 1].widget.text_box.options.label = label; 498 } 499 500 // enter counts only on the focused button; a click counts anywhere on it 501 fn activated(root_focus: *Focus, button_id: usize, key: Key) bool { 502 return switch (key) { 503 .enter => root_focus.grandchild_id == button_id, 504 .mouse => |mouse| inp.leftClickOn(root_focus, button_id, mouse), 505 else => false, 506 }; 507 } 508 509 // the terminal hosts run the action after rendering; the web renderer 510 // posts the form instead, and wasm has no repository to act on 511 fn requestUndo(self: *View, route: ui.RoutablePage) void { 512 if (builtin.target.cpu.arch == .wasm32) return; 513 if (!self.session.is_terminal or self.session.host_request != null) return; 514 self.session.host_request = .{ .undo = route.repo_undo }; 515 } 516 517 fn headerBox(self: *View) *wgt.Box(ui.Widget) { 518 return &self.box.children.values()[header_index].widget.box; 519 } 520 fn headerActive(self: *View) bool { 521 return self.box.getFocus().child_id == self.headerBox().getFocus().id; 522 } 523 fn contentBox(self: *View) *wgt.Box(ui.Widget) { 524 return &self.box.children.values()[content_index].widget.box; 525 } 526 fn listScroll(self: *View) *wgt.Scroll(ui.Widget) { 527 return &self.contentBox().children.values()[0].widget.scroll; 528 } 529 fn detailScroll(self: *View) *wgt.Scroll(ui.Widget) { 530 return &self.contentBox().children.values()[1].widget.scroll; 531 } 532 fn selected(self: *View) ?usize { 533 const rows = &self.listScroll().child.box; 534 const index = rows.children.getIndex(rows.getFocus().child_id orelse return null) orelse return null; 535 return if (index < self.data.items.len) index else null; 536 } 537 fn detailActive(self: *View) bool { 538 return self.contentBox().getFocus().child_id == self.detailScroll().getFocus().id; 539 } 540 541 fn refreshDetail(self: *View, allocator: std.mem.Allocator) !void { 542 const index = self.selected() orelse return; 543 const failure = self.session.data.undo_failure orelse self.data.failure; 544 if (self.detailed_index == index and std.meta.eql(self.shown_failure, failure)) return; 545 const details = &self.detailScroll().child.box; 546 while (details.children.count() > fixed_rows) { 547 var entry = details.children.pop() orelse break; 548 entry.value.widget.deinit(allocator); 549 } 550 const item = self.data.items[index]; 551 const enabled = self.data.handle.canUndo() and item.index > 0; 552 553 // the fixed rows are filled first, since adding the ones below 554 // invalidates every pointer into the box 555 { 556 const button = &details.children.values()[0].widget.text_box; 557 try button.setContent(allocator, if (item.index == 0) "initial state cannot be undone" else if (item.index == self.data.count - 1) "undo this" else "undo this and all above it"); 558 button.getFocus().kind = if (enabled) .{ .custom = "submit" } else .text_box; 559 } 560 try details.children.values()[1].widget.text_box.setContent(allocator, item.timestamp); 561 562 // some actions put buttons where the description would go 563 if (item.buttons.len == 0) { 564 try addLabeled(allocator, details, " description ", item.description, null); 565 } else for (item.buttons) |detail_button| { 566 try addLabeled(allocator, details, detail_button.label, detail_button.text, detail_button.link); 567 } 568 if (failure) |message| try addText(allocator, details, try std.fmt.allocPrint(self.session.page_arena.allocator(), "error: {s}", .{message}), null, .single); 569 self.shown_failure = failure; 570 details.getFocus().kind = if (enabled) .{ .custom = item.form } else .container; 571 details.getFocus().child_id = details.children.keys()[0]; 572 self.detailScroll().x = 0; 573 self.detailScroll().y = 0; 574 self.detailScroll().getFocus().version +%= 1; 575 self.detailed_index = index; 576 } 577 578 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 579 self.clearGrid(); 580 if (self.data.clear) return try self.box.build(allocator, constraint, root_focus); 581 try self.refreshDetail(allocator); 582 const rows = &self.listScroll().child.box; 583 if (self.data.items.len == 0) { 584 const failure = self.session.data.undo_failure orelse self.data.failure; 585 if (!std.meta.eql(self.shown_failure, failure)) { 586 const message = if (failure) |name| try std.fmt.allocPrint(self.session.page_arena.allocator(), "error: {s}", .{name}) else "no undo history"; 587 try rows.children.values()[0].widget.text_box.setContent(allocator, message); 588 self.shown_failure = failure; 589 } 590 } 591 for (rows.children.keys(), rows.children.values()) |id, *child| ui.widget.markSelected(&child.widget.text_box, rows.getFocus().child_id == id); 592 const both_fit = if (constraint.max_size.width) |width| width >= list_max_width + detail_min_width else true; 593 self.contentBox().children.values()[0].max_size = if (both_fit) .{ .width = list_max_width, .height = null } else null; 594 const width = if (constraint.max_size.width) |value| if (both_fit) value - list_max_width else value else detail_min_width; 595 self.contentBox().children.values()[1].min_size = .{ .width = width, .height = null }; 596 for (self.detailScroll().child.box.children.values()) |*child| child.max_size = .{ .width = width -| 2, .height = null }; 597 try self.box.build(allocator, constraint, root_focus); 598 } 599 600 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 601 // the sub header sits above whichever content the route selects. 602 // scrolling crosses the boundary the same way the arrow keys do 603 const direction = inp.vertDirection(key); 604 if (self.headerActive()) { 605 if (direction == .down) root_focus.setFocus(self.box.children.values()[content_index].widget.getFocus().id); 606 return; 607 } 608 if (self.data.clear) { 609 if (direction == .up) root_focus.setFocus(self.headerBox().getFocus().id) else try self.clearInput(key, root_focus); 610 return; 611 } 612 if (direction == .up and self.contentAtTop()) { 613 root_focus.setFocus(self.headerBox().getFocus().id); 614 return; 615 } 616 try self.refreshDetail(allocator); 617 if (self.detailActive()) { 618 const details = &self.detailScroll().child.box; 619 if (self.selected()) |index| { 620 if (activated(root_focus, details.children.keys()[0], key) and self.data.handle.canUndo() and self.data.items[index].index > 0) { 621 self.requestUndo(ui.RoutablePage.repoUndoRoute(self.data.identity, self.data.items[index].index) orelse return error.RouteTooLong); 622 return; 623 } 624 } 625 if (key == .arrow_left) root_focus.setFocus(self.listScroll().getFocus().id) else if (inp.rowDelta(key, @intCast(details.children.count()))) |delta| ui.widget.moveRowFocus(details, self.detailScroll(), root_focus, delta); 626 } else if (inp.rowDelta(key, @intCast(self.listScroll().child.box.children.count()))) |delta| { 627 ui.widget.moveRowFocus(&self.listScroll().child.box, self.listScroll(), root_focus, delta); 628 } else switch (key) { 629 .arrow_right, .enter => if (self.selected() != null) { 630 root_focus.setFocus(self.detailScroll().getFocus().id); 631 }, 632 else => {}, 633 } 634 } 635 636 // the confirm button submits the form the web renderer posts 637 fn clearInput(self: *View, key: Key, root_focus: *Focus) !void { 638 const center = &self.box.children.values()[content_index].widget.center; 639 const button_id = center.child.box.getFocus().child_id orelse return; 640 if (!activated(root_focus, button_id, key) or !self.data.handle.canUndo()) return; 641 self.requestUndo(ui.RoutablePage.repoUndoClearRoute(self.data.identity) orelse return error.RouteTooLong); 642 } 643 644 fn contentAtTop(self: *View) bool { 645 const scroll = if (self.detailActive()) self.detailScroll() else self.listScroll(); 646 const rows = &scroll.child.box; 647 const id = rows.getFocus().child_id orelse return true; 648 return rows.children.getIndex(id) == 0 and scroll.y == 0; 649 } 650 651 // moving up from the sub header returns to the page's tabs 652 pub fn atTop(self: *View) bool { 653 return self.headerActive(); 654 } 655 656 // arriving from the page's tabs lands on the sub header, not the content 657 pub fn focusHeader(self: *View, root_focus: *Focus) bool { 658 root_focus.setFocus(self.headerBox().getFocus().id); 659 return true; 660 } 661 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 662 self.box.deinit(allocator); 663 } 664 pub fn clearGrid(self: *View) void { 665 self.box.clearGrid(); 666 } 667 pub fn getGrid(self: View) ?Grid { 668 return self.box.getGrid(); 669 } 670 pub fn getFocus(self: *View) *Focus { 671 return self.box.getFocus(); 672 } 673 }; 674