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