╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Undo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
317 } 318 } 319 320 // discard every earlier transaction by compacting the database. the patch 321 // revisions events point at are unreachable from the refs, so they are named 322 // as extra roots to survive the collection. 323 pub fn clearHistory(io: std.Io, allocator: std.mem.Allocator, source: ui.RepoSource) !void { 324 if (source.repo_kind != .xit) return error.NotFound; 325 var any_repo = try rp.AnyRepo(.xit, .{}).open(io, allocator, source.localInitOpts()); 326 defer any_repo.deinit(io, allocator); 327 switch (any_repo) { 328 inline else => |*repo| { 329 const opts = repo.self_repo_opts; 330 // a repo that has consumed no events has no revisions to protect 331 const roots = if (evt.currentMoment(opts, repo)) |moment| 332 try evt.PatchRev.gcRoots(rp.Repo(.xit, opts).DB, opts.hash, allocator, moment) 333 else |_| 334 &.{}; 335 defer allocator.free(roots); 336 _ = try repo.garbageCollect(io, allocator, .{ .extra_roots = roots }); 337 }, 338 } 339 } 340 341 // keep action failures on the current tab without rebuilding a stale route. 342 pub fn handleRequest(allocator: std.mem.Allocator, session: *ui.Session, target: ui.RoutablePage.RepoUndoRoute) void { 343 perform(allocator, session, target) catch |err| { 344 session.data.undo_failure = @errorName(err); 345 return; 346 }; 347 session.data.undo_failure = null; 348 } 349 350 // both terminal hosts resolve and authorize the request afresh before opening 351 // the repository; the web handler uses its matching http authorization path. 352 pub fn perform(allocator: std.mem.Allocator, session: *ui.Session, target: ui.RoutablePage.RepoUndoRoute) !void { 353 const identity = target.name.slice(); 354 const index = if (target.clear) null else target.index orelse return error.InvalidHistoryIndex; 355 const source = try authorizedSource(session, identity) orelse return error.Forbidden; 356 const io = session.io orelse return error.NotFound; 357 if (index) |value| try execute(io, allocator, source, value) else try clearHistory(io, allocator, source); 358 try session.navigate(ui.RoutablePage.repoUndoRoute(identity, null) orelse return error.RouteTooLong); 359 } 360 361 // the repo an owner may act on, or null when they may not 362 fn authorizedSource(session: *ui.Session, identity: []const u8) !?ui.RepoSource { 363 if (try session.authorize(identity, .owner, null) == null) return null; 364 if (session.local) |local| return local; 365 const admin_repo = session.admin_repo orelse return error.NotFound; 366 const moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 367 const pair = ui.RoutablePage.RepoIdentity.parse(identity) orelse return error.NotFound; 368 const io = session.io orelse return error.NotFound; 369 const users_dir = session.users_dir orelse return error.NotFound; 370 const found = try evt.readRepoByOwnerAndName(io, session.page_arena.child_allocator, session.page_arena, moment, users_dir, pair.owner, pair.name, null) orelse return error.NotFound; 371 return .{ .path = try evt.repoPath(session.page_arena.allocator(), users_dir, found.repo.event.user_id, &found.event_id), .repo_kind = .xit }; 372 } 373 374 pub const View = struct { 375 box: wgt.Box(ui.Widget), 376 data: *const Self, 377 session: *ui.Session, 378 detailed_index: ?usize = null, 379 shown_failure: ?[]const u8 = null, 380 381 const list_max_width = 20; 382 const detail_min_width = 40; 383 const header_index = 0; 384 const content_index = 1; 385 // the undo button and timestamp stay; everything below them is rebuilt 386 const fixed_rows = 2; 387 388 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 389 var outer = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert }); 390 errdefer outer.deinit(allocator); 391 392 // the sub header carries the tab's only action 393 { 394 var header = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 395 errdefer header.deinit(allocator); 396 const route = ui.RoutablePage.repoUndoClearRoute(data.identity) orelse return error.RouteTooLong; 397 const link = try session.page_arena.allocator().print("a:{s}", .{try route.toUrl(session.page_arena)}); 398 try addText(allocator, &header, "clear undo history", link, .single); 399 header.getFocus().child_id = header.children.keys()[0]; 400 // a row taller than the header, leaving a blank line beneath it 401 try outer.children.put(allocator, header.getFocus().id, .{ .widget = .{ .box = header }, .rect = null, .min_size = .{ .width = null, .height = 4 } }); 402 } 403 404 if (data.clear) { 405 var center = try initClearForm(allocator, data, session); 406 errdefer center.deinit(allocator); 407 try outer.children.put(allocator, center.getFocus().id, .{ .widget = .{ .center = center }, .rect = null, .min_size = null }); 408 outer.getFocus().child_id = outer.children.keys()[content_index]; 409 return .{ .box = outer, .data = data, .session = session }; 410 } 411 412 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 413 errdefer box.deinit(allocator); 414 { 415 var scroll = blk: { 416 var rows = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert, .stretch = true }); 417 errdefer rows.deinit(allocator); 418 for (data.items) |item| { 419 try addText(allocator, &rows, item.action, item.link, .hidden); 420 // a transaction a later undo discarded says so on its border 421 if (item.undone) rows.children.values()[rows.children.count() - 1].widget.text_box.options.bottom_label.text = "(undone)"; 422 } 423 if (data.next) |next| { 424 const route = ui.RoutablePage.repoUndoRoute(data.identity, next) orelse return error.RouteTooLong; 425 try addText(allocator, &rows, "next →", try session.page_arena.allocator().print("a:{s}", .{try route.toUrl(session.page_arena)}), .hidden); 426 } 427 if (data.items.len == 0) try addText(allocator, &rows, "no undo history", null, .hidden); 428 rows.getFocus().child_id = rows.children.keys()[0]; 429 break :blk try wgt.Scroll(ui.Widget).init(allocator, .{ .box = rows }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 430 }; 431 errdefer scroll.deinit(allocator); 432 try box.children.put(allocator, scroll.getFocus().id, .{ .widget = .{ .scroll = scroll }, .rect = null, .min_size = .{ .width = list_max_width, .height = null }, .max_size = .{ .width = list_max_width, .height = null } }); 433 } 434 { 435 var scroll = blk: { 436 var details = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert }); 437 errdefer details.deinit(allocator); 438 if (data.items.len > 0) { 439 try addText(allocator, &details, "", null, .single); 440 try addText(allocator, &details, "", null, .single); 441 details.getFocus().child_id = details.children.keys()[0]; 442 } 443 break :blk try wgt.Scroll(ui.Widget).init(allocator, .{ .box = details }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 444 }; 445 errdefer scroll.deinit(allocator); 446 scroll.getFocus().mode = .mouse; 447 try box.children.put(allocator, scroll.getFocus().id, .{ .widget = .{ .scroll = scroll }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 448 } 449 box.getFocus().child_id = box.children.keys()[if (data.items.len > 0) 1 else 0]; 450 try outer.children.put(allocator, box.getFocus().id, .{ .widget = .{ .box = box }, .rect = null, .min_size = null }); 451 outer.getFocus().child_id = outer.children.keys()[content_index]; 452 return .{ .box = outer, .data = data, .session = session }; 453 } 454 455 // the same shape the thread views use to confirm a removal 456 fn initClearForm(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !ui.widget.Center { 457 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .round_corners = true, .direction = .vert }); 458 errdefer box.deinit(allocator); 459 const route = ui.RoutablePage.repoUndoClearRoute(data.identity) orelse return error.RouteTooLong; 460 box.getFocus().kind = .{ .custom = try session.page_arena.allocator().print("form:{s}", .{try route.toUrl(session.page_arena)}) }; 461 462 var prompt = try wgt.Text.init(allocator, "are you sure?"); 463 errdefer prompt.deinit(allocator); 464 try box.children.put(allocator, prompt.getFocus().id, .{ .widget = .{ .text = prompt }, .rect = null, .min_size = null }); 465 466 var button = try wgt.TextBox.init(allocator, "clear undo history", .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 467 errdefer button.deinit(allocator); 468 button.getFocus().mode = .all; 469 button.getFocus().kind = if (data.handle.canUndo()) .{ .custom = "submit" } else .text_box; 470 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 471 box.getFocus().child_id = button.getFocus().id; 472 473 return ui.widget.Center.init(allocator, .{ .box = box }); 474 } 475 476 fn addText(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), text: []const u8, link: ?[]const u8, border: wgt.Border) !void { 477 var row = try wgt.TextBox.init(allocator, text, .{ .border = border, .round_corners = true, .wrap_kind = .word }); 478 errdefer row.deinit(allocator); 479 row.getFocus().mode = .all; 480 if (link) |value| row.getFocus().kind = .{ .custom = value }; 481 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null }); 482 } 483 484 // a labeled box, added as the selected action needs it 485 fn addLabeled(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, text: []const u8, link: ?[]const u8) !void { 486 try addText(allocator, box, text, link, .single); 487 box.children.values()[box.children.count() - 1].widget.text_box.options.top_label.text = label; 488 } 489 490 // enter counts only on the focused button; a click counts anywhere on it 491 fn activated(root_focus: *Focus, button_id: usize, key: Key) bool { 492 return switch (key) { 493 .enter => root_focus.grandchild_id == button_id, 494 .mouse => |mouse| inp.leftClickOn(root_focus, button_id, mouse), 495 else => false, 496 }; 497 } 498 499 // the terminal hosts run the action after rendering; the web renderer 500 // posts the form instead, and wasm has no repository to act on 501 fn requestUndo(self: *View, route: ui.RoutablePage) void { 502 if (builtin.target.cpu.arch == .wasm32) return; 503 if (!self.session.is_terminal or self.session.host_request != null) return; 504 self.session.host_request = .{ .undo = route.repo_undo }; 505 } 506 507 fn headerBox(self: *View) *wgt.Box(ui.Widget) { 508 return &self.box.children.values()[header_index].widget.box; 509 } 510 fn headerActive(self: *View) bool { 511 return self.box.getFocus().child_id == self.headerBox().getFocus().id; 512 } 513 fn contentBox(self: *View) *wgt.Box(ui.Widget) { 514 return &self.box.children.values()[content_index].widget.box; 515 } 516 fn listScroll(self: *View) *wgt.Scroll(ui.Widget) { 517 return &self.contentBox().children.values()[0].widget.scroll; 518 } 519 fn detailScroll(self: *View) *wgt.Scroll(ui.Widget) { 520 return &self.contentBox().children.values()[1].widget.scroll; 521 } 522 fn selected(self: *View) ?usize { 523 const rows = &self.listScroll().child.box; 524 const index = rows.children.getIndex(rows.getFocus().child_id orelse return null) orelse return null; 525 return if (index < self.data.items.len) index else null; 526 } 527 fn detailActive(self: *View) bool { 528 return self.contentBox().getFocus().child_id == self.detailScroll().getFocus().id; 529 } 530 531 fn refreshDetail(self: *View, allocator: std.mem.Allocator) !void { 532 const index = self.selected() orelse return; 533 const failure = self.session.data.undo_failure orelse self.data.failure; 534 if (self.detailed_index == index and std.meta.eql(self.shown_failure, failure)) return; 535 const details = &self.detailScroll().child.box; 536 while (details.children.count() > fixed_rows) { 537 var entry = details.children.pop() orelse break; 538 entry.value.widget.deinit(allocator); 539 } 540 const item = self.data.items[index]; 541 const enabled = self.data.handle.canUndo() and item.index > 0; 542 543 // the fixed rows are filled first, since adding the ones below 544 // invalidates every pointer into the box 545 { 546 const button = &details.children.values()[0].widget.text_box; 547 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"); 548 button.getFocus().kind = if (enabled) .{ .custom = "submit" } else .text_box; 549 } 550 const timestamp = try ui.timestamp.format(self.session.page_arena.allocator(), item.timestamp, try self.session.nowSeconds()); 551 try details.children.values()[1].widget.text_box.setContent(allocator, timestamp.relative); 552 details.children.values()[1].widget.text_box.options.bottom_label.text = timestamp.exact; 553 554 // some actions put buttons where the description would go 555 if (item.buttons.len == 0) { 556 try addLabeled(allocator, details, " description ", item.description, null); 557 } else for (item.buttons) |detail_button| { 558 try addLabeled(allocator, details, detail_button.label, detail_button.text, detail_button.link); 559 } 560 if (failure) |message| try addText(allocator, details, try self.session.page_arena.allocator().print("error: {s}", .{message}), null, .single); 561 self.shown_failure = failure; 562 details.getFocus().kind = if (enabled) .{ .custom = item.form } else .container; 563 details.getFocus().child_id = details.children.keys()[0]; 564 self.detailScroll().x = 0; 565 self.detailScroll().y = 0; 566 self.detailScroll().getFocus().version +%= 1; 567 self.detailed_index = index; 568 } 569 570 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 571 self.clearGrid(); 572 if (self.data.clear) return try self.box.build(allocator, constraint, root_focus); 573 try self.refreshDetail(allocator); 574 const rows = &self.listScroll().child.box; 575 if (self.data.items.len == 0) { 576 const failure = self.session.data.undo_failure orelse self.data.failure; 577 if (!std.meta.eql(self.shown_failure, failure)) { 578 const message = if (failure) |name| try self.session.page_arena.allocator().print("error: {s}", .{name}) else "no undo history"; 579 try rows.children.values()[0].widget.text_box.setContent(allocator, message); 580 self.shown_failure = failure; 581 } 582 } 583 for (rows.children.keys(), rows.children.values()) |id, *child| ui.widget.markSelected(&child.widget.text_box, rows.getFocus().child_id == id); 584 const both_fit = if (constraint.max_size.width) |width| width >= list_max_width + detail_min_width else true; 585 self.contentBox().children.values()[0].max_size = if (both_fit) .{ .width = list_max_width, .height = null } else null; 586 const width = if (constraint.max_size.width) |value| if (both_fit) value - list_max_width else value else detail_min_width; 587 self.contentBox().children.values()[1].min_size = .{ .width = width, .height = null }; 588 for (self.detailScroll().child.box.children.values()) |*child| child.max_size = .{ .width = width -| 2, .height = null }; 589 try self.box.build(allocator, constraint, root_focus); 590 } 591 592 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 593 // the sub header sits above whichever content the route selects. 594 // scrolling crosses the boundary the same way the arrow keys do 595 const direction = inp.vertDirection(key); 596 if (self.headerActive()) { 597 if (direction == .down) root_focus.setFocus(self.box.children.values()[content_index].widget.getFocus().id); 598 return; 599 } 600 if (self.data.clear) { 601 if (direction == .up) root_focus.setFocus(self.headerBox().getFocus().id) else try self.clearInput(key, root_focus); 602 return; 603 } 604 if (direction == .up and self.contentAtTop()) { 605 root_focus.setFocus(self.headerBox().getFocus().id); 606 return; 607 } 608 try self.refreshDetail(allocator); 609 if (self.detailActive()) { 610 const details = &self.detailScroll().child.box; 611 if (self.selected()) |index| { 612 if (activated(root_focus, details.children.keys()[0], key) and self.data.handle.canUndo() and self.data.items[index].index > 0) { 613 self.requestUndo(ui.RoutablePage.repoUndoRoute(self.data.identity, self.data.items[index].index) orelse return error.RouteTooLong); 614 return; 615 } 616 } 617 if (inp.rowDelta(key, @intCast(details.children.count()))) |delta| { 618 ui.widget.moveRowFocus(details, self.detailScroll(), root_focus, delta); 619 } else if (key == .arrow_left) { 620 root_focus.setFocus(self.listScroll().getFocus().id); 621 } 622 } else if (inp.rowDelta(key, @intCast(self.listScroll().child.box.children.count()))) |delta| { 623 ui.widget.moveRowFocus(&self.listScroll().child.box, self.listScroll(), root_focus, delta); 624 } else switch (key) { 625 .arrow_right, .enter => if (self.selected() != null) { 626 root_focus.setFocus(self.detailScroll().getFocus().id); 627 }, 628 else => {}, 629 } 630 } 631 632 // the confirm button submits the form the web renderer posts 633 fn clearInput(self: *View, key: Key, root_focus: *Focus) !void { 634 const center = &self.box.children.values()[content_index].widget.center; 635 const button_id = center.child.box.getFocus().child_id orelse return; 636 if (!activated(root_focus, button_id, key) or !self.data.handle.canUndo()) return; 637 self.requestUndo(ui.RoutablePage.repoUndoClearRoute(self.data.identity) orelse return error.RouteTooLong); 638 } 639 640 fn contentAtTop(self: *View) bool { 641 const scroll = if (self.detailActive()) self.detailScroll() else self.listScroll(); 642 const rows = &scroll.child.box; 643 const id = rows.getFocus().child_id orelse return true; 644 return rows.children.getIndex(id) == 0 and scroll.y == 0; 645 } 646 647 // moving up from the sub header returns to the page's tabs 648 pub fn atTop(self: *View) bool { 649 return self.headerActive(); 650 } 651 652 // arriving from the page's tabs lands on the sub header, not the content 653 pub fn focusHeader(self: *View, root_focus: *Focus) bool { 654 root_focus.setFocus(self.headerBox().getFocus().id); 655 return true; 656 } 657 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 658 self.box.deinit(allocator); 659 } 660 pub fn clearGrid(self: *View) void { 661 self.box.clearGrid(); 662 } 663 pub fn getGrid(self: View) ?Grid { 664 return self.box.getGrid(); 665 } 666 pub fn getFocus(self: *View) *Focus { 667 return self.box.getFocus(); 668 } 669 }; 670