╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
93 inline else => |*case| try case.input(allocator, key, root_focus), 94 } 95 } 96 97 pub fn clearGrid(self: *Widget) void { 98 switch (self.*) { 99 inline else => |*case| case.clearGrid(), 100 } 101 } 102 103 pub fn getGrid(self: Widget) ?Grid { 104 switch (self) { 105 inline else => |*case| return case.getGrid(), 106 } 107 } 108 109 pub fn getFocus(self: *Widget) *Focus { 110 switch (self.*) { 111 inline else => |*case| return case.getFocus(), 112 } 113 } 114 115 // whether moving up should return to the page header 116 pub fn atTop(self: *Widget, root_focus: *Focus) bool { 117 return switch (self.*) { 118 inline .home_about, .home_users, .user_repos, .flow_box_scroll, .new_repo, .new_user, .auth_login, .auth_logout, .quit, .repo_files, .repo_commits, .diff_view, .repo_refs, .repo_issues, .repo_patches, .repo_discussions, .repo_events, .repo_undo => |*view| view.atTop(), 119 inline .home_auth, .repo_patch_detail => |*view| view.atTop(root_focus), 120 else => false, 121 }; 122 } 123 }; 124 125 pub const back_button_width = 3; 126 const back_button_height = 3; 127 const back_kind = "back"; 128 129 // draw a text box as the selected one of its group: bordered and inverted, 130 // while the rest stay borderless 131 pub fn markSelected(text_box: *wgt.TextBox, selected: bool) void { 132 text_box.options.border_style = if (selected) .single else .hidden; 133 text_box.options.invert = selected; 134 } 135 136 pub fn addBackButton(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session) !void { 137 if (!session.is_terminal) return; 138 var back = try wgt.TextBox.init(allocator, "←", .{ .border_style = .hidden, .wrap_kind = .none }); 139 errdefer back.deinit(allocator); 140 back.getFocus().kind = .{ .custom = back_kind }; 141 back.getFocus().mode = .mouse; 142 try box.children.put(allocator, back.getFocus().id, .{ 143 .widget = .{ .text_box = back }, 144 .rect = null, 145 .min_size = .{ .width = back_button_width, .height = back_button_height }, 146 .max_size = .{ .width = back_button_width, .height = back_button_height }, 147 .hidden = session.back != .available, 148 }); 149 } 150 151 pub fn setBackButtonVisible(box: *wgt.Box(Widget), visible: bool) void { 152 for (box.children.values()) |*child| { 153 if (isBackButton(child.widget.getFocus())) { 154 child.hidden = !visible; 155 continue; 156 } 157 switch (child.widget) { 158 .box => |*nested| setBackButtonVisible(nested, visible), 159 else => {}, 160 } 161 } 162 } 163 164 pub fn isBackButton(focus: *Focus) bool { 165 return switch (focus.kind) { 166 .custom => |kind| std.mem.eql(u8, kind, back_kind), 167 else => false, 168 }; 169 } 170 171 // move the focused row in `box` by `delta`, clamped to the ends, scrolling 172 // `scroll` to keep it visible. 173 pub fn moveRowFocus(box: *wgt.Box(Widget), scroll: *wgt.Scroll(Widget), root_focus: *Focus, delta: isize) void { 174 const keys = box.children.keys(); 175 if (keys.len == 0) return; 176 const cur_id = box.getFocus().child_id orelse return; 177 const cur: isize = @intCast(box.children.getIndex(cur_id) orelse return); 178 const last: isize = @intCast(keys.len - 1); 179 const next: usize = @intCast(std.math.clamp(cur + delta, 0, last)); 180 if (next == @as(usize, @intCast(cur))) return; 181 root_focus.setFocus(keys[next]); 182 if (box.children.values()[next].rect) |rect| scroll.scrollToRect(rect); 183 } 184 185 // focus the box beside the selected one in `row`, false when there is none that way. 186 pub fn moveInRow(row: *wgt.Box(Widget), root_focus: *Focus, right: bool) bool { 187 const selected = row.getFocus().child_id orelse return false; 188 const index = row.children.getIndex(selected) orelse return false; 189 const target = if (right) index + 1 else index -| 1; 190 if (target == index or target >= row.children.count()) return false; 191 root_focus.setFocus(row.children.keys()[target]); 192 return true; 193 } 194 195 // a repo tab's placeholder while the repo has no commits, with the remote to 196 // push to for a viewer who can 197 pub fn initNoCommits(allocator: std.mem.Allocator, session: *ui.Session, handle: ui.RepoHandle) !Center { 198 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 199 errdefer box.deinit(allocator); 200 201 // built first so the text can center over it 202 var remote_maybe: ?CopyableText = if (session.data.host_kind == .server and handle.canWrite()) if (session.data.git_ssh_port) |port| blk: { 203 // only repo pages flag a repo without commits 204 const identity = switch (handle.location) { 205 .repo => |identity| identity, 206 .fork => unreachable, 207 }; 208 const aa = session.page_arena.allocator(); 209 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}", .{ port, identity }); 210 break :blk try CopyableText.init(allocator, session, &.{.{ 211 .text = url, 212 .copyable_text = try std.fmt.allocPrint(aa, "git remote add origin {s}", .{url}), 213 .label = " git remote add origin ", 214 }}, 0); 215 } else null else null; 216 errdefer if (remote_maybe) |*remote| remote.deinit(allocator); 217 218 { 219 var text_widget: Widget = blk: { 220 var text_box = try wgt.TextBox.init(allocator, "no commits yet", .{ .border_style = null, .wrap_kind = .none }); 221 errdefer text_box.deinit(allocator); 222 break :blk if (remote_maybe != null) .{ .center = try Center.init(allocator, .{ .text_box = text_box }) } else .{ .text_box = text_box }; 223 }; 224 errdefer text_widget.deinit(allocator); 225 // over the remote widget, the text centers across its width 226 const size: ?layout.MaybeSize = if (remote_maybe) |*remote| .{ .width = remote.minWidth(), .height = 1 } else null; 227 try box.children.put(allocator, text_widget.getFocus().id, .{ .widget = text_widget, .rect = null, .min_size = size, .max_size = size }); 228 } 229 230 if (remote_maybe) |*remote| { 231 { 232 var gap = try wgt.Text.init(allocator, " "); 233 errdefer gap.deinit(allocator); 234 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 235 } 236 const min_width = remote.minWidth(); 237 box.getFocus().child_id = remote.getFocus().id; 238 try box.children.put(allocator, remote.getFocus().id, .{ .widget = .{ .copyable_text = remote.* }, .rect = null, .min_size = .{ .width = min_width, .height = 3 }, .max_size = .{ .width = min_width, .height = 3 } }); 239 // the box owns it now 240 remote_maybe = null; 241 } 242 243 return Center.init(allocator, .{ .box = box }); 244 } 245 246 pub const FlowBox = struct { 247 focus: *Focus, 248 grid: ?Grid, 249 text_boxes: std.ArrayList(wgt.TextBox), 250 // backs each link item's `.custom` focus kind (e.g. "a:/user/foo"). reset 251 // wholesale on each setItems so there's no per-string ownership to track. 252 arena: std.heap.ArenaAllocator, 253 // column count from the last build — FlowBox.Scroll.input uses it so arrow 254 // up/down can step by a row's worth of items. 255 last_cols: usize, 256 options: Options, 257 258 const border_rows: usize = 2; 259 260 pub const Options = struct { 261 cell_width: usize = 40, 262 cell_height: usize = 3, 263 }; 264 265 pub fn init(allocator: std.mem.Allocator, options: Options) !FlowBox { 266 return .{ 267 .focus = try Focus.create(allocator, .container), 268 .grid = null, 269 .text_boxes = .empty, 270 .arena = std.heap.ArenaAllocator.init(allocator), 271 .last_cols = 1, 272 .options = options, 273 }; 274 } 275 276 pub fn deinit(self: *FlowBox, allocator: std.mem.Allocator) void { 277 self.focus.destroy(allocator); 278 if (self.grid) |*grid| { 279 grid.deinit(); 280 self.grid = null; 281 } 282 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 283 self.text_boxes.deinit(allocator); 284 self.arena.deinit(); 285 } 286 287 // one flow item: its display text plus an optional link and bottom label. a 288 // non-empty `link` sets the item's focus kind such as `.{ .custom = "a:/user/foo" }`, 289 // which the web renderer turns into an anchor. 290 pub const Item = struct { text: []const u8, link: []const u8 = "", bottom_label: []const u8 = "" }; 291 292 // the text box copies the item text; links and bottom labels are copied into 293 // the arena, so the caller's slices needn't outlive this call. 294 pub fn setItems(self: *FlowBox, allocator: std.mem.Allocator, items: []const Item) !void { 295 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 296 self.text_boxes.clearAndFree(allocator); 297 298 // the old text boxes and their borrowed links are gone now, so the link 299 // arena can be reclaimed. 300 _ = self.arena.reset(.retain_capacity); 301 const aa = self.arena.allocator(); 302 303 self.focus.clear(); 304 self.focus.child_id = null; 305 306 for (items) |item| { 307 var text_box = try wgt.TextBox.init(allocator, item.text, .{ .border_style = .hidden, .round_corners = true, .wrap_kind = .word, .bottom_label = try aa.dupe(u8, item.bottom_label) }); 308 errdefer text_box.deinit(allocator); 309 text_box.getFocus().mode = .all; 310 311 if (item.link.len > 0) { 312 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 313 } 314 315 try self.text_boxes.append(allocator, text_box); 316 } 317 318 if (self.text_boxes.items.len > 0) { 319 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 320 } 321 } 322 323 pub fn build(self: *FlowBox, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 324 self.clearGrid(); 325 self.focus.clear(); 326 327 const cell_width = self.options.cell_width; 328 const max_width = constraint.max_size.width orelse cell_width; 329 const cols = if (cell_width == 0) 1 else @max(1, max_width / cell_width); 330 self.last_cols = cols; 331 332 const count = self.text_boxes.items.len; 333 if (count == 0) return; 334 const slot_height = self.options.cell_height + border_rows; 335 if (slot_height == 0) return; 336 337 // build at the slot size so every text box fits a single grid cell 338 for (self.text_boxes.items) |*tb| { 339 tb.options.border_style = if (root_focus.grandchild_id == tb.getFocus().id) .double else .hidden; 340 try tb.build(allocator, .{ 341 .min_size = .{ .width = cell_width, .height = null }, 342 .max_size = .{ .width = cell_width, .height = slot_height }, 343 }, root_focus); 344 } 345 346 const rows = (count + cols - 1) / cols; 347 const content_height = rows * slot_height; 348 const total_width = cols * cell_width; 349 if (total_width == 0 or content_height == 0) return; 350 351 var grid = try Grid.init(allocator, .{ .width = total_width, .height = content_height }); 352 errdefer grid.deinit(); 353 354 for (self.text_boxes.items, 0..) |*tb, i| { 355 const tb_grid = tb.getGrid() orelse continue; 356 const col = i % cols; 357 const row = i / cols; 358 const cell_x = col * cell_width; 359 const cell_y = row * slot_height; 360 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, cell_x, cell_y); 361 try grid.drawGrid(tb_grid, cell_x, cell_y); 362 } 363 364 self.grid = grid; 365 } 366 367 pub fn input(self: *FlowBox, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 368 _ = self; 369 _ = allocator; 370 _ = key; 371 _ = root_focus; 372 } 373 374 pub fn clearGrid(self: *FlowBox) void { 375 if (self.grid) |*grid| { 376 grid.deinit(); 377 self.grid = null; 378 } 379 for (self.text_boxes.items) |*tb| tb.clearGrid(); 380 } 381 382 pub fn getGrid(self: FlowBox) ?Grid { 383 return self.grid; 384 } 385 386 pub fn getFocus(self: *FlowBox) *Focus { 387 return self.focus; 388 } 389 390 pub fn cellRect(self: FlowBox, index: usize) ?layout.IRect { 391 if (self.last_cols == 0 or index >= self.text_boxes.items.len) return null; 392 const slot_height = self.options.cell_height + border_rows; 393 const col = index % self.last_cols; 394 const row = index / self.last_cols; 395 return .{ 396 .x = @intCast(col * self.options.cell_width), 397 .y = @intCast(row * slot_height), 398 .size = .{ .width = self.options.cell_width, .height = slot_height }, 399 }; 400 } 401 402 pub fn indexOfFocusId(self: FlowBox, focus_id: usize) ?usize { 403 for (self.text_boxes.items, 0..) |tb, i| { 404 if (tb.focus.id == focus_id) return i; 405 } 406 return null; 407 } 408 409 pub const Scroll = struct { 410 scroll: wgt.Scroll(Widget), 411 412 pub fn init(allocator: std.mem.Allocator, options: FlowBox.Options, web_native: bool) !Scroll { 413 var layout_inner = try FlowBox.init(allocator, options); 414 errdefer layout_inner.deinit(allocator); 415 var scroll = try wgt.Scroll(Widget).init(allocator, .{ .flow_box = layout_inner }, .{ .direction = .vert, .web_native = web_native }); 416 errdefer scroll.deinit(allocator); 417 return .{ .scroll = scroll }; 418 } 419 420 pub fn deinit(self: *Scroll, allocator: std.mem.Allocator) void { 421 self.scroll.deinit(allocator); 422 } 423 424 pub fn setItems(self: *Scroll, allocator: std.mem.Allocator, items: []const Item) !void { 425 self.scroll.x = 0; 426 self.scroll.y = 0; 427 try self.inner().setItems(allocator, items); 428 } 429 430 pub fn build(self: *Scroll, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 431 try self.scroll.build(allocator, constraint, root_focus); 432 } 433 434 pub fn input(self: *Scroll, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 435 _ = allocator; 436 const in = self.inner(); 437 const child_id = in.focus.child_id orelse return; 438 const current_index = in.indexOfFocusId(child_id) orelse return; 439 const count = in.text_boxes.items.len; 440 if (count == 0) return; 441 const cols = in.last_cols; 442 const slot_height = in.options.cell_height + FlowBox.border_rows; 443 444 var index = current_index; 445 switch (key) { 446 .arrow_up => index -|= cols, 447 .arrow_down => if (index + cols < count) { 448 index += cols; 449 }, 450 .arrow_left => index -|= 1, 451 .arrow_right => if (index + 1 < count) { 452 index += 1; 453 }, 454 .home => index = 0, 455 .end => index = count - 1, 456 .page_up => { 457 if (self.scroll.grid) |grid| if (slot_height > 0) { 458 const rows_per_page = grid.size.height / slot_height; 459 index -|= rows_per_page * cols; 460 }; 461 }, 462 .page_down => { 463 if (self.scroll.grid) |grid| if (slot_height > 0) { 464 const rows_per_page = grid.size.height / slot_height; 465 index = @min(index + rows_per_page * cols, count - 1); 466 }; 467 }, 468 // scroll wheel moves the focused cell by a full row so the 469 // viewport (via scrollToRect) follows in row-sized steps, 470 // matching how a scroll wheel feels in a grid view. 471 .mouse => |mouse| switch (mouse.action) { 472 .scroll => |dir| switch (dir) { 473 .up => index -|= cols, 474 .down => if (index + cols < count) { 475 index += cols; 476 }, 477 }, 478 else => {}, 479 }, 480 else => {}, 481 } 482 483 if (index != current_index) { 484 root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 485 if (in.cellRect(index)) |rect| { 486 self.scroll.scrollToRect(rect); 487 } 488 } 489 } 490 491 pub fn clearGrid(self: *Scroll) void { 492 self.scroll.clearGrid(); 493 } 494 495 pub fn getGrid(self: Scroll) ?Grid { 496 return self.scroll.getGrid(); 497 } 498 499 pub fn getFocus(self: *Scroll) *Focus { 500 return self.scroll.getFocus(); 501 } 502 503 pub fn isEmpty(self: Scroll) bool { 504 return self.scroll.child.flow_box.text_boxes.items.len == 0; 505 } 506 507 pub fn atTop(self: Scroll) bool { 508 const in = self.scroll.child.flow_box; 509 const child_id = in.focus.child_id orelse return false; 510 return in.indexOfFocusId(child_id) == 0; 511 } 512 513 fn inner(self: *Scroll) *FlowBox { 514 return &self.scroll.child.flow_box; 515 } 516 }; 517 }; 518 519 // a left-to-right flow of variable-width focusable items that wraps at the 520 // available width, like words in a paragraph. unlike FlowBox there is no cell 521 // grid: each item is as wide as its text. selection movement is driven by the 522 // owning view via indexOfFocusId/verticalNeighbor. 523 pub const WordFlow = struct { 524 focus: *Focus, 525 grid: ?Grid, 526 text_boxes: std.ArrayList(wgt.TextBox), 527 // last build's item rects (content space), for vertical navigation and 528 // scroll-into-view. 529 rects: std.ArrayList(layout.IRect), 530 // backs each link item's `.custom` focus kind. 531 arena: std.heap.ArenaAllocator, 532 533 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 534 535 pub fn init(allocator: std.mem.Allocator) !WordFlow { 536 return .{ 537 .focus = try Focus.create(allocator, .container), 538 .grid = null, 539 .text_boxes = .empty, 540 .rects = .empty, 541 .arena = std.heap.ArenaAllocator.init(allocator), 542 }; 543 } 544 545 pub fn deinit(self: *WordFlow, allocator: std.mem.Allocator) void { 546 self.focus.destroy(allocator); 547 if (self.grid) |*grid| { 548 grid.deinit(); 549 self.grid = null; 550 } 551 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 552 self.text_boxes.deinit(allocator); 553 self.rects.deinit(allocator); 554 self.arena.deinit(); 555 } 556 557 // the text box copies the item text; links are copied into the arena, so the 558 // caller's slices needn't outlive this call. 559 pub fn setItems(self: *WordFlow, allocator: std.mem.Allocator, items: []const Item) !void { 560 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 561 self.text_boxes.clearAndFree(allocator); 562 563 _ = self.arena.reset(.retain_capacity); 564 const aa = self.arena.allocator(); 565 566 self.focus.clear(); 567 self.focus.child_id = null; 568 569 for (items) |item| { 570 var text_box = try wgt.TextBox.init(allocator, item.text, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 571 errdefer text_box.deinit(allocator); 572 text_box.getFocus().mode = .all; 573 574 if (item.link.len > 0) { 575 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 576 } 577 578 try self.text_boxes.append(allocator, text_box); 579 } 580 581 if (self.text_boxes.items.len > 0) { 582 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 583 } 584 } 585 586 pub fn build(self: *WordFlow, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 587 self.clearGrid(); 588 self.focus.clear(); 589 590 if (self.text_boxes.items.len == 0) return; 591 const max_width = constraint.max_size.width orelse 80; 592 if (max_width == 0) return; 593 594 // lay the items out left to right, wrapping to a new row when the next 595 // one wouldn't fit. 596 self.rects.clearRetainingCapacity(); 597 var x: usize = 0; 598 var y: usize = 0; 599 var row_height: usize = 0; 600 var total_width: usize = 0; 601 for (self.text_boxes.items) |*tb| { 602 try tb.build(allocator, .{ 603 .min_size = .{ .width = null, .height = null }, 604 .max_size = .{ .width = max_width, .height = null }, 605 }, root_focus); 606 const tb_grid_maybe = tb.getGrid(); 607 const w: usize = if (tb_grid_maybe) |g| g.size.width else 0; 608 const h: usize = if (tb_grid_maybe) |g| g.size.height else 0; 609 if (x > 0 and x + w > max_width) { 610 x = 0; 611 y += row_height; 612 row_height = 0; 613 } 614 try self.rects.append(allocator, .{ .x = @intCast(x), .y = @intCast(y), .size = .{ .width = w, .height = h } }); 615 if (h > row_height) row_height = h; 616 x += w; 617 if (x > total_width) total_width = x; 618 } 619 620 const total_height = y + row_height; 621 if (total_width == 0 or total_height == 0) return; 622 623 var grid = try Grid.init(allocator, .{ .width = total_width, .height = total_height }); 624 errdefer grid.deinit(); 625 626 for (self.text_boxes.items, self.rects.items) |*tb, rect| { 627 const tb_grid = tb.getGrid() orelse continue; 628 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, @intCast(rect.x), @intCast(rect.y)); 629 try grid.drawGrid(tb_grid, @intCast(rect.x), @intCast(rect.y)); 630 } 631 632 self.grid = grid; 633 } 634 635 pub fn input(self: *WordFlow, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 636 _ = self; 637 _ = allocator; 638 _ = key; 639 _ = root_focus; 640 } 641 642 pub fn clearGrid(self: *WordFlow) void { 643 if (self.grid) |*grid| { 644 grid.deinit(); 645 self.grid = null; 646 } 647 for (self.text_boxes.items) |*tb| tb.clearGrid(); 648 } 649 650 pub fn getGrid(self: WordFlow) ?Grid { 651 return self.grid; 652 } 653 654 pub fn getFocus(self: *WordFlow) *Focus { 655 return self.focus; 656 } 657 658 pub fn indexOfFocusId(self: WordFlow, focus_id: usize) ?usize { 659 for (self.text_boxes.items, 0..) |tb, i| { 660 if (tb.focus.id == focus_id) return i; 661 } 662 return null; 663 } 664 665 // the first item of the row below (when `downward`) or above, or null at 666 // the flow's edge. items are laid out in order, so y is nondecreasing. 667 pub fn rowStep(self: WordFlow, index: usize, downward: bool) ?usize { 668 const rects = self.rects.items; 669 if (index >= rects.len) return null; 670 const cur_y = rects[index].y; 671 if (downward) { 672 for (rects[index + 1 ..], index + 1..) |r, i| { 673 if (r.y > cur_y) return i; 674 } 675 return null; 676 } 677 // walk the earlier rows; the last one visited is the row above. 678 var prev_first: ?usize = null; 679 var i: usize = 0; 680 while (i < rects.len and rects[i].y < cur_y) { 681 prev_first = i; 682 const y = rects[i].y; 683 while (i < rects.len and rects[i].y == y) i += 1; 684 } 685 return prev_first; 686 } 687 }; 688 689 // a word-wrapped paragraph of styled runs. each line a link touches is a 690 // focusable piece, and a focused link draws all its pieces inverted. 691 pub const RichText = struct { 692 focus: *Focus, 693 grid: ?Grid, 694 // backs the decoded content and the link targets 695 arena: std.heap.ArenaAllocator, 696 runes: []const u21, 697 // each rune's span 698 span_of: []const usize, 699 styles: []const Grid.Style, 700 // each span's link index, null outside a link 701 span_links: []const ?usize, 702 links: []const []const u8, 703 // each link's piece focuses in line order, kept across builds so a link 704 // keeps its ids when the text rewraps 705 link_focuses: []std.ArrayList(*Focus), 706 lines: std.ArrayList(wgt.Line), 707 // scratch for each build's link pieces 708 pieces: std.ArrayList(Piece), 709 710 pub const Span = struct { 711 text: []const u8, 712 style: Grid.Style = .{}, 713 // the focus kind a click or enter follows; empty for plain text 714 link: []const u8 = "", 715 }; 716 717 const Piece = struct { 718 link: usize, 719 rect: layout.URect, 720 }; 721 722 pub fn init(allocator: std.mem.Allocator, spans: []const Span) !RichText { 723 var arena = std.heap.ArenaAllocator.init(allocator); 724 errdefer arena.deinit(); 725 const aa = arena.allocator(); 726 727 var runes: std.ArrayList(u21) = .empty; 728 var span_of: std.ArrayList(usize) = .empty; 729 const styles = try aa.alloc(Grid.Style, spans.len); 730 const span_links = try aa.alloc(?usize, spans.len); 731 var links: std.ArrayList([]const u8) = .empty; 732 for (spans, 0..) |span, i| { 733 styles[i] = span.style; 734 span_links[i] = null; 735 if (span.link.len > 0) { 736 // adjacent spans with the same target are one link 737 const same = i > 0 and spans[i - 1].link.len > 0 and std.mem.eql(u8, spans[i - 1].link, span.link); 738 if (!same) try links.append(aa, try aa.dupe(u8, span.link)); 739 span_links[i] = links.items.len - 1; 740 } 741 var utf8 = (try std.unicode.Utf8View.init(span.text)).iterator(); 742 while (utf8.nextCodepoint()) |codepoint| { 743 try runes.append(aa, codepoint); 744 try span_of.append(aa, i); 745 } 746 } 747 748 const link_focuses = try aa.alloc(std.ArrayList(*Focus), links.items.len); 749 @memset(link_focuses, .empty); 750 751 return .{ 752 .focus = try Focus.create(allocator, .container), 753 .grid = null, 754 .arena = arena, 755 .runes = runes.items, 756 .span_of = span_of.items, 757 .styles = styles, 758 .span_links = span_links, 759 .links = links.items, 760 .link_focuses = link_focuses, 761 .lines = .empty, 762 .pieces = .empty, 763 }; 764 } 765 766 pub fn deinit(self: *RichText, allocator: std.mem.Allocator) void { 767 self.focus.destroy(allocator); 768 self.clearGrid(); 769 for (self.link_focuses) |*focuses| { 770 for (focuses.items) |focus| focus.destroy(allocator); 771 focuses.deinit(allocator); 772 } 773 self.pieces.deinit(allocator); 774 self.lines.deinit(allocator); 775 self.arena.deinit(); 776 } 777 778 // the url a terminal can open for a link: raw links to absolute urls 779 fn terminalLink(link: []const u8) ?[]const u8 { 780 if (!std.mem.startsWith(u8, link, ui.raw_link_prefix)) return null; 781 const url = link[ui.raw_link_prefix.len..]; 782 return if (std.mem.startsWith(u8, url, "/")) null else url; 783 } 784 785 // the link one of whose pieces has focus id `id` 786 fn linkOf(self: *const RichText, id: ?usize) ?usize { 787 for (self.link_focuses, 0..) |focuses, l| { 788 for (focuses.items) |focus| if (focus.id == id) return l; 789 } 790 return null; 791 } 792 793 pub fn build(self: *RichText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 794 self.clearGrid(); 795 self.focus.clear(); 796 const max_width = constraint.max_size.width; 797 if (max_width == 0 or constraint.max_size.height == 0) return; 798 const focused_link = self.linkOf(root_focus.grandchild_id); 799 800 try wgt.wrapLines(allocator, &self.lines, self.runes, .word, max_width); 801 // trailing spaces are dropped so a wrapped link doesn't highlight them 802 for (self.lines.items) |*line| { 803 while (line.end > line.start and self.runes[line.end - 1] == ' ') { 804 line.end -= 1; 805 line.width -= 1; 806 } 807 } 808 const visible = @min(self.lines.items.len, constraint.max_size.height orelse self.lines.items.len); 809 var width: usize = 1; 810 for (self.lines.items[0..visible]) |line| width = @max(width, line.width); 811 if (max_width) |w| width = @min(width, w); 812 813 var grid = try Grid.init(allocator, .{ .width = width, .height = visible }); 814 errdefer grid.deinit(); 815 self.pieces.clearRetainingCapacity(); 816 for (self.lines.items[0..visible], 0..) |line, y| { 817 var x: usize = 0; 818 for (self.runes[line.start..line.end], line.start..) |rune, i| { 819 const rune_width = xitui.width.cellWidth(rune); 820 if (x + rune_width > width) break; 821 const link = self.span_links[self.span_of[i]]; 822 const cell = try grid.cell(x, y); 823 cell.style = self.styles[self.span_of[i]]; 824 if (link) |l| { 825 cell.link = terminalLink(self.links[l]); 826 if (l == focused_link) cell.style.inverted = true; 827 // extend this line's piece of the link, or start one 828 const items = self.pieces.items; 829 if (items.len > 0 and items[items.len - 1].link == l and items[items.len - 1].rect.y == y) { 830 items[items.len - 1].rect.size.width += rune_width; 831 } else { 832 try self.pieces.append(allocator, .{ .link = l, .rect = .{ .x = x, .y = y, .size = .{ .width = rune_width, .height = 1 } } }); 833 } 834 } 835 try grid.setRune(x, y, rune); 836 x += rune_width; 837 } 838 } 839 840 // a link's pieces are consecutive, since its runes are 841 var piece_index: usize = 0; 842 for (self.pieces.items, 0..) |piece, i| { 843 piece_index = if (i > 0 and self.pieces.items[i - 1].link == piece.link) piece_index + 1 else 0; 844 const focuses = &self.link_focuses[piece.link]; 845 if (piece_index == focuses.items.len) { 846 const focus = try Focus.create(allocator, .{ .custom = self.links[piece.link] }); 847 errdefer focus.destroy(allocator); 848 focus.mode = .all; 849 try focuses.append(allocator, focus); 850 } 851 try self.focus.addChild(allocator, focuses.items[piece_index], piece.rect.size, piece.rect.x, piece.rect.y); 852 } 853 854 // focus rests on a link's first piece, which stays while the link shows 855 if (focused_link) |l| if (self.link_focuses[l].items.len > 0) { 856 const first = self.link_focuses[l].items[0].id; 857 if (self.focus.children.contains(first)) { 858 root_focus.grandchild_id = first; 859 self.focus.child_id = first; 860 } 861 }; 862 863 self.grid = grid; 864 } 865 866 pub fn input(self: *RichText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 867 _ = self; 868 _ = allocator; 869 _ = key; 870 _ = root_focus; 871 } 872 873 pub fn clearGrid(self: *RichText) void { 874 if (self.grid) |*grid| { 875 grid.deinit(); 876 self.grid = null; 877 } 878 } 879 880 pub fn getGrid(self: RichText) ?Grid { 881 return self.grid; 882 } 883 884 pub fn getFocus(self: *RichText) *Focus { 885 return self.focus; 886 } 887 }; 888 889 // an invisible widget that fills the horizontal space granted by its parent. 890 // used inside Box(horiz) with a min_size so the box reserves space for the 891 // children that follow, pushing them to the right. 892 pub const Spacer = struct { 893 focus: *Focus, 894 grid: ?Grid, 895 896 pub fn init(allocator: std.mem.Allocator) !Spacer { 897 return .{ 898 .focus = try Focus.create(allocator, .container), 899 .grid = null, 900 }; 901 } 902 903 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 904 self.focus.destroy(allocator); 905 if (self.grid) |*grid| { 906 grid.deinit(); 907 self.grid = null; 908 } 909 } 910 911 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 912 _ = root_focus; 913 self.clearGrid(); 914 const width = constraint.max_size.width orelse return; 915 if (width == 0) return; 916 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 917 } 918 919 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 920 _ = self; 921 _ = allocator; 922 _ = key; 923 _ = root_focus; 924 } 925 926 pub fn clearGrid(self: *Spacer) void { 927 if (self.grid) |*grid| { 928 grid.deinit(); 929 self.grid = null; 930 } 931 } 932 933 pub fn getGrid(self: Spacer) ?Grid { 934 return self.grid; 935 } 936 937 pub fn getFocus(self: *Spacer) *Focus { 938 return self.focus; 939 } 940 }; 941 942 // a sub-header's search box: a labeled text input whose enter the owning view 943 // turns into a navigation. shared by the files, commits and thread tabs. 944 pub const SearchBox = struct { 945 text_input: wgt.TextInput, 946 session: *ui.Session, 947 948 // the room the box asks its row for 949 pub const min_size: layout.MaybeSize = .{ .width = 20, .height = 3 }; 950 951 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, label: []const u8, name: []const u8, text_value: ?[]const u8) !SearchBox { 952 var text_input = try wgt.TextInput.init(allocator, .{ .label = label, .name = name, .round_corners = true, .visible_width = 18, .render_content = session.is_terminal }); 953 errdefer text_input.deinit(allocator); 954 text_input.getFocus().mode = .all; 955 if (text_value) |value| try text_input.setContent(allocator, value); 956 return .{ .text_input = text_input, .session = session }; 957 } 958 959 pub fn deinit(self: *SearchBox, allocator: std.mem.Allocator) void { 960 self.text_input.deinit(allocator); 961 } 962 963 pub fn build(self: *SearchBox, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 964 self.text_input.options.bottom_label = if (root_focus.grandchild_id == self.getFocus().id) " press enter " else ""; 965 try self.text_input.build(allocator, constraint, root_focus); 966 if (!self.session.is_terminal) { 967 try self.session.text_inputs.put(self.session.arena.allocator(), self.getFocus().id, &self.text_input); 968 } 969 } 970 971 pub fn input(self: *SearchBox, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 972 try self.text_input.input(allocator, key, root_focus); 973 } 974 975 // the typed text; the caller owns it 976 pub fn text(self: *SearchBox, allocator: std.mem.Allocator) ![]u8 { 977 return self.text_input.text(allocator); 978 } 979 980 // whether the cursor sits past the last character, where right-arrow leaves 981 // the box rather than moving within it 982 pub fn atEnd(self: *SearchBox) bool { 983 return self.text_input.cursor == self.text_input.content.items.len; 984 } 985 986 pub fn clearGrid(self: *SearchBox) void { 987 self.text_input.clearGrid(); 988 } 989 990 pub fn getGrid(self: SearchBox) ?Grid { 991 return self.text_input.getGrid(); 992 } 993 994 pub fn getFocus(self: *SearchBox) *Focus { 995 return self.text_input.getFocus(); 996 } 997 }; 998 999 // a row of bordered options posted as one form field. the selection is the 1000 // focused option, so a click selects on the web too. 1001 pub const Radio = struct { 1002 box: wgt.Box(Widget), 1003 name: []const u8, 1004 values: []const []const u8, 1005 1006 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, name: []const u8, values: []const []const u8, initial: []const u8) !Radio { 1007 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1008 errdefer box.deinit(allocator); 1009 for (values) |value| { 1010 var option = try wgt.TextBox.init(allocator, value, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 1011 errdefer option.deinit(allocator); 1012 option.getFocus().mode = .all; 1013 option.getFocus().kind = .{ .custom = try std.fmt.allocPrint(session.page_arena.allocator(), ui.radio_prefix ++ "{s}={s}", .{ name, value }) }; 1014 if (std.mem.eql(u8, value, initial)) box.getFocus().child_id = option.getFocus().id; 1015 try box.children.put(allocator, option.getFocus().id, .{ .widget = .{ .text_box = option }, .rect = null, .min_size = .{ .width = try xitui.width.displayWidth(value) + 2, .height = 3 } }); 1016 } 1017 return .{ .box = box, .name = name, .values = values }; 1018 } 1019 1020 pub fn deinit(self: *Radio, allocator: std.mem.Allocator) void { 1021 self.box.deinit(allocator); 1022 } 1023 1024 // the focused option's value 1025 pub fn selected(self: *Radio) []const u8 { 1026 const index = self.box.children.getIndex(self.box.getFocus().child_id orelse unreachable) orelse unreachable; 1027 return self.values[index]; 1028 } 1029 1030 pub fn build(self: *Radio, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1031 self.clearGrid(); 1032 const selected_id = self.box.getFocus().child_id; 1033 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 1034 child.widget.text_box.options.border_style = if (id == selected_id) .single else .hidden; 1035 } 1036 try self.box.build(allocator, constraint, root_focus); 1037 } 1038 1039 pub fn input(self: *Radio, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1040 _ = allocator; 1041 const ids = self.box.children.keys(); 1042 const index = self.box.children.getIndex(self.box.getFocus().child_id orelse return) orelse return; 1043 switch (key) { 1044 .arrow_left => if (index > 0) root_focus.setFocus(ids[index - 1]), 1045 .arrow_right => if (index + 1 < ids.len) root_focus.setFocus(ids[index + 1]), 1046 else => {}, 1047 } 1048 } 1049 1050 pub fn clearGrid(self: *Radio) void { 1051 self.box.clearGrid(); 1052 } 1053 1054 pub fn getGrid(self: Radio) ?Grid { 1055 return self.box.getGrid(); 1056 } 1057 1058 pub fn getFocus(self: *Radio) *Focus { 1059 return self.box.getFocus(); 1060 } 1061 }; 1062 1063 // a tab's sub-header: an optional search box on the left, then a spacer, then 1064 // the clone url on the right. the owning view turns a submitted query into a 1065 // route, so route building stays with the tab that knows its own url. 1066 pub const SearchHeader = struct { 1067 box: wgt.Box(Widget), 1068 session: *ui.Session, 1069 1070 pub fn init( 1071 allocator: std.mem.Allocator, 1072 session: *ui.Session, 1073 handle: ui.RepoHandle, 1074 label: []const u8, 1075 name: []const u8, 1076 text: ?[]const u8, 1077 show_input: bool, 1078 ) !SearchHeader { 1079 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1080 errdefer box.deinit(allocator); 1081 1082 // the search box only appears where an index can serve it. 1083 if (show_input) { 1084 var search_box = try SearchBox.init(allocator, session, label, name, text); 1085 errdefer search_box.deinit(allocator); 1086 box.getFocus().child_id = search_box.getFocus().id; 1087 try box.children.put(allocator, search_box.getFocus().id, .{ .widget = .{ .search_box = search_box }, .rect = null, .min_size = SearchBox.min_size }); 1088 } 1089 1090 // the spacer pushes the clone url to the right edge. 1091 { 1092 var spacer = try Spacer.init(allocator); 1093 errdefer spacer.deinit(allocator); 1094 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1095 } 1096 1097 if (session.data.host_kind == .server) { 1098 if (try CopyableText.initClone(allocator, session, handle)) |value| { 1099 var clone_url = value; 1100 errdefer clone_url.deinit(allocator); 1101 const min_width = clone_url.minWidth(); 1102 if (box.getFocus().child_id == null) box.getFocus().child_id = clone_url.getFocus().id; 1103 try box.children.put(allocator, clone_url.getFocus().id, .{ .widget = .{ .copyable_text = clone_url }, .rect = null, .min_size = .{ .width = min_width, .height = 3 }, .max_size = .{ .width = min_width, .height = 3 } }); 1104 } 1105 } 1106 1107 return .{ .box = box, .session = session }; 1108 } 1109 1110 pub fn deinit(self: *SearchHeader, allocator: std.mem.Allocator) void { 1111 self.box.deinit(allocator); 1112 } 1113 1114 pub fn build(self: *SearchHeader, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1115 self.clearGrid(); 1116 try self.box.build(allocator, constraint, root_focus); 1117 } 1118 1119 pub fn input(self: *SearchHeader, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1120 const current = self.box.getFocus().child_id orelse return; 1121 if (self.searchBox()) |search_box| { 1122 if (current == search_box.getFocus().id) { 1123 // tab, and right-arrow past the last character, step to the 1124 // clone url beside the box 1125 if (key == .tab or (key == .arrow_right and search_box.atEnd())) { 1126 if (self.cloneUrl()) |clone_url| { 1127 root_focus.setFocus(clone_url.getFocus().id); 1128 return; 1129 } 1130 } 1131 return search_box.input(allocator, key, root_focus); 1132 } 1133 } 1134 const clone_url = self.cloneUrl() orelse return; 1135 if (key == .back_tab or (key == .arrow_left and clone_url.selected == 0)) { 1136 if (self.searchBox()) |search_box| { 1137 root_focus.setFocus(search_box.getFocus().id); 1138 return; 1139 } 1140 } 1141 try clone_url.input(allocator, key, root_focus); 1142 } 1143 1144 // the typed query when enter should submit it: the box exists and holds 1145 // focus. the caller owns the returned text. 1146 pub fn submittedText(self: *SearchHeader, allocator: std.mem.Allocator) !?[]u8 { 1147 const search_box = self.searchBox() orelse return null; 1148 if (self.box.getFocus().child_id != search_box.getFocus().id) return null; 1149 return try search_box.text(allocator); 1150 } 1151 1152 // enter the header, preferring the search box. 1153 pub fn focusHeader(self: *SearchHeader, root_focus: *Focus) bool { 1154 if (self.searchBox()) |search_box| { 1155 root_focus.setFocus(search_box.getFocus().id); 1156 return true; 1157 } 1158 const clone_url = self.cloneUrl() orelse return false; 1159 root_focus.setFocus(clone_url.getFocus().id); 1160 return true; 1161 } 1162 1163 pub fn hasFocusable(self: *SearchHeader) bool { 1164 return self.cloneUrl() != null or self.searchBox() != null; 1165 } 1166 1167 fn cloneUrl(self: *SearchHeader) ?*CopyableText { 1168 for (self.box.children.values()) |*child| switch (child.widget) { 1169 .copyable_text => |*copyable_text| return copyable_text, 1170 else => {}, 1171 }; 1172 return null; 1173 } 1174 1175 fn searchBox(self: *SearchHeader) ?*SearchBox { 1176 for (self.box.children.values()) |*child| switch (child.widget) { 1177 .search_box => |*search_box| return search_box, 1178 else => {}, 1179 }; 1180 return null; 1181 } 1182 1183 pub fn clearGrid(self: *SearchHeader) void { 1184 self.box.clearGrid(); 1185 } 1186 1187 pub fn getGrid(self: SearchHeader) ?Grid { 1188 return self.box.getGrid(); 1189 } 1190 1191 pub fn getFocus(self: *SearchHeader) *Focus { 1192 return self.box.getFocus(); 1193 } 1194 }; 1195 1196 // a one-row, non-focusable status bar showing the current page's url 1197 pub const Footer = struct { 1198 focus: *Focus, 1199 grid: ?Grid, 1200 session: *ui.Session, 1201 arena: std.heap.ArenaAllocator, 1202 url: []const u8 = "", 1203 url_width: usize = 0, 1204 1205 pub fn init(allocator: std.mem.Allocator, session: *ui.Session) !Footer { 1206 return .{ 1207 .focus = try Focus.create(allocator, .container), 1208 .grid = null, 1209 .session = session, 1210 .arena = std.heap.ArenaAllocator.init(allocator), 1211 }; 1212 } 1213 1214 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 1215 self.focus.destroy(allocator); 1216 if (self.grid) |*grid| grid.deinit(); 1217 self.arena.deinit(); 1218 } 1219 1220 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1221 _ = root_focus; 1222 self.clearGrid(); 1223 const width = constraint.max_size.width orelse return; 1224 if (width == 0) return; 1225 1226 _ = self.arena.reset(.retain_capacity); 1227 self.url = ""; 1228 self.url_width = 0; 1229 const aa = self.arena.allocator(); 1230 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 1231 const text = ui.terminalWebUrl(aa, self.session, path) catch return; 1232 self.url = text; 1233 1234 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1235 errdefer grid.deinit(); 1236 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 1237 var i: usize = 0; 1238 while (utf8.nextCodepoint()) |ch| { 1239 const rune_width = xitui.width.cellWidth(ch); 1240 if (i + rune_width > width) break; 1241 // a terminal hyperlink to the page 1242 (try grid.cell(i, 0)).link = text; 1243 try grid.setRune(i, 0, ch); 1244 i += rune_width; 1245 } 1246 self.url_width = i; 1247 self.grid = grid; 1248 } 1249 1250 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1251 _ = allocator; 1252 switch (key) { 1253 .mouse => |mouse| { 1254 if (mouse.ctrl or !inp.leftClickOn(root_focus, self.getFocus().id, mouse)) return; 1255 const rect = (root_focus.children.get(self.getFocus().id) orelse return).rect; 1256 if (mouse.x < rect.x + self.url_width) self.session.host_request = .{ .show_copyable_text = self.url }; 1257 }, 1258 else => {}, 1259 } 1260 } 1261 1262 pub fn clearGrid(self: *Footer) void { 1263 if (self.grid) |*grid| { 1264 grid.deinit(); 1265 self.grid = null; 1266 } 1267 } 1268 1269 pub fn getGrid(self: Footer) ?Grid { 1270 return self.grid; 1271 } 1272 1273 pub fn getFocus(self: *Footer) *Focus { 1274 return self.focus; 1275 } 1276 }; 1277 1278 // a single-child wrapper that builds the child at its natural size and 1279 // positions its grid in the middle of the area granted by the parent 1280 // a bordered label, indented past the content it precedes 1281 pub const SectionLabel = struct { 1282 box: wgt.Box(Widget), 1283 1284 pub fn init(allocator: std.mem.Allocator, content: []const u8) !SectionLabel { 1285 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1286 errdefer box.deinit(allocator); 1287 1288 { 1289 var gap = try wgt.Text.init(allocator, " "); 1290 errdefer gap.deinit(allocator); 1291 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 1292 } 1293 1294 { 1295 var tb = try wgt.TextBox.init(allocator, content, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 1296 errdefer tb.deinit(allocator); 1297 tb.getFocus().mode = .all; 1298 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1299 } 1300 1301 box.getFocus().child_id = box.children.keys()[1]; 1302 return .{ .box = box }; 1303 } 1304 1305 pub fn deinit(self: *SectionLabel, allocator: std.mem.Allocator) void { 1306 self.box.deinit(allocator); 1307 } 1308 1309 pub fn build(self: *SectionLabel, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1310 try self.box.build(allocator, constraint, root_focus); 1311 } 1312 1313 pub fn input(self: *SectionLabel, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1314 _ = self; 1315 _ = allocator; 1316 _ = key; 1317 _ = root_focus; 1318 } 1319 1320 pub fn clearGrid(self: *SectionLabel) void { 1321 self.box.clearGrid(); 1322 } 1323 1324 pub fn getGrid(self: SectionLabel) ?Grid { 1325 return self.box.getGrid(); 1326 } 1327 1328 pub fn getFocus(self: *SectionLabel) *Focus { 1329 return self.box.getFocus(); 1330 } 1331 }; 1332 1333 // a submit button, indented past its form's content. the web form POSTs 1334 // the button's form; terminal hosts detect presses through `buttonId`. 1335 pub const SubmitButton = struct { 1336 box: wgt.Box(Widget), 1337 1338 pub fn init(allocator: std.mem.Allocator) !SubmitButton { 1339 return initLabeled(allocator, "submit"); 1340 } 1341 1342 pub fn initLabeled(allocator: std.mem.Allocator, label: []const u8) !SubmitButton { 1343 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1344 errdefer box.deinit(allocator); 1345 1346 { 1347 var gap = try wgt.Text.init(allocator, " "); 1348 errdefer gap.deinit(allocator); 1349 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 1350 } 1351 1352 { 1353 var button = try wgt.TextBox.init(allocator, label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 1354 errdefer button.deinit(allocator); 1355 button.getFocus().mode = .all; 1356 // the renderer distinguishes plain clickables from buttons that 1357 // should POST to a server route by this kind. 1358 button.getFocus().kind = .{ .custom = "submit" }; 1359 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 1360 } 1361 1362 box.getFocus().child_id = box.children.keys()[1]; 1363 return .{ .box = box }; 1364 } 1365 1366 pub fn deinit(self: *SubmitButton, allocator: std.mem.Allocator) void { 1367 self.box.deinit(allocator); 1368 } 1369 1370 pub fn setLabel(self: *SubmitButton, allocator: std.mem.Allocator, label: []const u8) !void { 1371 const button = &self.box.children.values()[1]; 1372 try button.widget.text_box.setContent(allocator, label); 1373 button.min_size = .{ .width = label.len + 2, .height = null }; 1374 } 1375 1376 pub fn build(self: *SubmitButton, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1377 try self.box.build(allocator, constraint, root_focus); 1378 } 1379 1380 pub fn input(self: *SubmitButton, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1381 _ = self; 1382 _ = allocator; 1383 _ = key; 1384 _ = root_focus; 1385 } 1386 1387 pub fn clearGrid(self: *SubmitButton) void { 1388 self.box.clearGrid(); 1389 } 1390 1391 pub fn getGrid(self: SubmitButton) ?Grid { 1392 return self.box.getGrid(); 1393 } 1394 1395 pub fn getFocus(self: *SubmitButton) *Focus { 1396 return self.box.getFocus(); 1397 } 1398 1399 // the button's focus id, for click hit-testing 1400 pub fn buttonId(self: *const SubmitButton) usize { 1401 return self.box.children.keys()[1]; 1402 } 1403 }; 1404 1405 pub const Center = struct { 1406 focus: *Focus, 1407 grid: ?Grid, 1408 child: *Widget, 1409 1410 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 1411 const child = try allocator.create(Widget); 1412 errdefer allocator.destroy(child); 1413 child.* = child_widget; 1414 return .{ 1415 .focus = try Focus.create(allocator, .container), 1416 .grid = null, 1417 .child = child, 1418 }; 1419 } 1420 1421 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1422 self.focus.destroy(allocator); 1423 if (self.grid) |*grid| { 1424 grid.deinit(); 1425 self.grid = null; 1426 } 1427 self.child.deinit(allocator); 1428 allocator.destroy(self.child); 1429 } 1430 1431 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1432 self.clearGrid(); 1433 self.getFocus().clear(); 1434 1435 // build the child without forcing it to fill min_size; it sizes 1436 // itself to its content within the available max. 1437 try self.child.build(allocator, .{ 1438 .min_size = .{ .width = null, .height = null }, 1439 .max_size = constraint.max_size, 1440 }, root_focus); 1441 1442 const child_grid = self.child.getGrid() orelse return; 1443 1444 // prefer max when given; otherwise grow to max(min, child) so that 1445 // when only a min is set (e.g. the wasm path passes viewport rows 1446 // as min_height with no max), we can still vertically center while 1447 // letting taller content extend past the min. 1448 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; 1449 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; 1450 if (width == 0 or height == 0) return; 1451 1452 const offset_x: usize = (width -| child_grid.size.width) / 2; 1453 const offset_y: usize = (height -| child_grid.size.height) / 2; 1454 1455 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1456 errdefer grid.deinit(); 1457 try grid.drawGrid(child_grid, offset_x, offset_y); 1458 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1459 self.getFocus().child_id = self.child.getFocus().id; 1460 1461 self.grid = grid; 1462 } 1463 1464 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1465 try self.child.input(allocator, key, root_focus); 1466 } 1467 1468 pub fn clearGrid(self: *Center) void { 1469 if (self.grid) |*grid| { 1470 grid.deinit(); 1471 self.grid = null; 1472 } 1473 self.child.clearGrid(); 1474 } 1475 1476 pub fn getGrid(self: Center) ?Grid { 1477 return self.grid; 1478 } 1479 1480 pub fn getFocus(self: *Center) *Focus { 1481 return self.focus; 1482 } 1483 }; 1484 1485 // renders truecolor ANSI art 1486 pub const AnsiArt = struct { 1487 focus: *Focus, 1488 grid: ?Grid, 1489 content: []const u8, 1490 1491 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1492 return .{ 1493 .focus = try Focus.create(allocator, .container), 1494 .grid = null, 1495 .content = content, 1496 }; 1497 } 1498 1499 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1500 self.focus.destroy(allocator); 1501 if (self.grid) |*grid| { 1502 grid.deinit(); 1503 self.grid = null; 1504 } 1505 } 1506 1507 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1508 _ = root_focus; 1509 self.clearGrid(); 1510 1511 // parse into rows of cells, then copy into a rectangular grid 1512 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1513 defer { 1514 for (rows.items) |*row| row.deinit(allocator); 1515 rows.deinit(allocator); 1516 } 1517 var row: std.ArrayList(Grid.Cell) = .empty; 1518 errdefer row.deinit(allocator); 1519 1520 var style: Grid.Style = .{}; 1521 var width: usize = 0; 1522 const content = self.content; 1523 var i: usize = 0; 1524 while (i < content.len) { 1525 const byte = content[i]; 1526 if (byte == '\n') { 1527 width = @max(width, row.items.len); 1528 try rows.append(allocator, row); 1529 row = .empty; 1530 i += 1; 1531 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1532 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1533 var j = i + 2; 1534 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1535 if (j >= content.len) { 1536 i = content.len; // malformed trailing escape; stop 1537 } else { 1538 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1539 i = j + 1; 1540 } 1541 } else { 1542 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1543 const end = @min(content.len, i + len); 1544 const rune = content[i..end]; 1545 const codepoint = std.unicode.utf8Decode(rune) catch '�'; 1546 // a foreground color does not make a space visible. only an 1547 // effective background makes it opaque; foreground state often 1548 // remains active through the padding at the end of a row. 1549 const effective_background = if (style.inverted) style.fg else style.bg; 1550 const transparent = end - i == 1 and rune[0] == ' ' and effective_background == null; 1551 try row.append(allocator, .{ 1552 .rune = if (transparent) null else codepoint, 1553 .style = style, 1554 }); 1555 if (!transparent and xitui.width.cellWidth(codepoint) == 2) { 1556 try row.append(allocator, .{ .rune = null, .style = style, .continuation = true }); 1557 } 1558 i = end; 1559 } 1560 } 1561 // a trailing row with content but no closing newline 1562 if (row.items.len > 0) { 1563 width = @max(width, row.items.len); 1564 try rows.append(allocator, row); 1565 } else { 1566 row.deinit(allocator); 1567 } 1568 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1569 1570 const height = rows.items.len; 1571 if (width == 0 or height == 0) return; 1572 1573 const clamped_w = @min(width, constraint.max_size.width orelse width); 1574 const clamped_h = @min(height, constraint.max_size.height orelse height); 1575 1576 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1577 errdefer grid.deinit(); 1578 for (rows.items[0..clamped_h], 0..) |r, y| { 1579 const n = @min(r.items.len, clamped_w); 1580 for (r.items[0..n], 0..) |cell, x| { 1581 if (cell.continuation) continue; 1582 (try grid.cell(x, y)).style = cell.style; 1583 try grid.setRune(x, y, cell.rune); 1584 } 1585 } 1586 self.grid = grid; 1587 } 1588 1589 pub fn clearGrid(self: *AnsiArt) void { 1590 if (self.grid) |*grid| { 1591 grid.deinit(); 1592 self.grid = null; 1593 } 1594 } 1595 1596 pub fn getGrid(self: AnsiArt) ?Grid { 1597 return self.grid; 1598 } 1599 1600 pub fn getFocus(self: *AnsiArt) *Focus { 1601 return self.focus; 1602 } 1603 1604 fn applySgr(style: *Grid.Style, params: []const u8) void { 1605 var nums: [16]u32 = undefined; 1606 var n: usize = 0; 1607 var it = std.mem.splitScalar(u8, params, ';'); 1608 while (it.next()) |tok| { 1609 if (n >= nums.len) break; 1610 // an empty parameter (e.g. bare "\x1b[m") means 0 1611 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1612 n += 1; 1613 } 1614 if (n == 0) { 1615 style.* = .{}; 1616 return; 1617 } 1618 var i: usize = 0; 1619 while (i < n) : (i += 1) { 1620 switch (nums[i]) { 1621 0 => style.* = .{}, 1622 7 => style.inverted = true, 1623 27 => style.inverted = false, 1624 39 => style.fg = null, 1625 49 => style.bg = null, 1626 38, 48 => { 1627 // truecolor form: 38;2;r;g;b — anything else is ignored 1628 if (i + 4 < n and nums[i + 1] == 2) { 1629 const c = Grid.Color{ .rgb = .{ 1630 .r = @truncate(nums[i + 2]), 1631 .g = @truncate(nums[i + 3]), 1632 .b = @truncate(nums[i + 4]), 1633 } }; 1634 if (nums[i] == 38) style.fg = c else style.bg = c; 1635 i += 4; 1636 } 1637 }, 1638 else => {}, 1639 } 1640 } 1641 } 1642 }; 1643 1644 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1645 pub const AnsiBackground = struct { 1646 grid: ?Grid, 1647 child: *Widget, 1648 art: AnsiArt, 1649 session: *ui.Session, 1650 1651 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, session: *ui.Session) !AnsiBackground { 1652 var cw = child_widget; 1653 const child = allocator.create(Widget) catch |e| { 1654 cw.deinit(allocator); 1655 return e; 1656 }; 1657 child.* = cw; 1658 errdefer { 1659 child.deinit(allocator); 1660 allocator.destroy(child); 1661 } 1662 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, session.data.ansi_art), .session = session }; 1663 } 1664 1665 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1666 if (self.grid) |*grid| grid.deinit(); 1667 self.art.deinit(allocator); 1668 self.child.deinit(allocator); 1669 allocator.destroy(self.child); 1670 } 1671 1672 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1673 self.clearGrid(); 1674 1675 // make the wrapped page fill the whole available area 1676 try self.child.build(allocator, .{ 1677 .min_size = .{ 1678 .width = constraint.max_size.width orelse constraint.min_size.width, 1679 .height = constraint.max_size.height orelse constraint.min_size.height, 1680 }, 1681 .max_size = constraint.max_size, 1682 }, root_focus); 1683 1684 if (!self.session.data.enable_ansi) return; 1685 const foreground = self.child.getGrid() orelse return; 1686 self.art.content = self.session.data.ansi_art; 1687 try self.buildArt(allocator, foreground.size, root_focus); 1688 if (!self.session.is_terminal) return; 1689 const art_grid = self.art.getGrid() orelse return; 1690 self.grid = try artBehind(allocator, foreground, art_grid); 1691 } 1692 1693 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1694 try self.child.input(allocator, key, root_focus); 1695 } 1696 1697 pub fn clearGrid(self: *AnsiBackground) void { 1698 if (self.grid) |*grid| { 1699 grid.deinit(); 1700 self.grid = null; 1701 } 1702 self.child.clearGrid(); 1703 self.art.clearGrid(); 1704 } 1705 1706 pub fn getGrid(self: AnsiBackground) ?Grid { 1707 return self.grid orelse self.child.getGrid(); 1708 } 1709 1710 pub fn getFocus(self: *AnsiBackground) *Focus { 1711 return self.child.getFocus(); 1712 } 1713 1714 // a foreground cell counts as blank if it's empty or a bare space without styling 1715 fn cellIsBlank(cell: Grid.Cell) bool { 1716 if (cell.continuation) return false; 1717 if (cell.rune) |rune| { 1718 return rune == ' ' and 1719 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1720 } 1721 return true; 1722 } 1723 1724 // the art is dimmed to this fraction of its brightness so foreground text 1725 // stays legible over it 1726 const art_brightness = 35; // percent 1727 1728 // the art parser only emits truecolor, so a palette color never occurs 1729 fn artRgb(color: ?Grid.Color) ?Grid.Color.Rgb { 1730 const c = color orelse return null; 1731 return switch (c) { 1732 .rgb => |v| v, 1733 else => null, 1734 }; 1735 } 1736 1737 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1738 const v = artRgb(color) orelse return color; 1739 return .{ .rgb = .{ 1740 .r = @intCast(@as(u16, v.r) * art_brightness / 100), 1741 .g = @intCast(@as(u16, v.g) * art_brightness / 100), 1742 .b = @intCast(@as(u16, v.b) * art_brightness / 100), 1743 } }; 1744 } 1745 1746 fn buildArt(self: *AnsiBackground, allocator: std.mem.Allocator, size: layout.Size, root_focus: *Focus) !void { 1747 try self.art.build(allocator, .{ 1748 .min_size = .{ .width = null, .height = null }, 1749 .max_size = .{ .width = size.width, .height = size.height }, 1750 }, root_focus); 1751 if (self.art.grid) |*grid| { 1752 for (grid.cells) |*cell| { 1753 cell.style.fg = dimColor(cell.style.fg); 1754 cell.style.bg = dimColor(cell.style.bg); 1755 } 1756 } 1757 } 1758 1759 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1760 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1761 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1762 // snap such channels clear of that range so the backdrop doesn't blink or 1763 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1764 // actually render truecolor. 1765 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1766 const v = artRgb(color) orelse return color; 1767 const snap = struct { 1768 fn f(c: u8) u8 { 1769 return if (c >= 1 and c <= 9) 10 else c; 1770 } 1771 }.f; 1772 return .{ .rgb = .{ .r = snap(v.r), .g = snap(v.g), .b = snap(v.b) } }; 1773 } 1774 1775 fn applyArtBackground(dst: *Grid.Cell, src: Grid.Cell) void { 1776 const background_maybe = sgrSafe(src.style.bg orelse src.style.fg); 1777 dst.style.bg = background_maybe; 1778 // change unstyled text to contrast with the art behind it 1779 if (dst.style.fg == null) { 1780 if (artRgb(background_maybe)) |v| { 1781 // use near-black and near-white, which is easier on the eyes 1782 // while retaining high contrast 1783 const luminance = (@as(u32, v.r) * 299 + 1784 @as(u32, v.g) * 587 + 1785 @as(u32, v.b) * 114) / 1000; 1786 dst.style.fg = if (luminance >= 128) 1787 .{ .rgb = .{ .r = 16, .g = 16, .b = 16 } } 1788 else 1789 .{ .rgb = .{ .r = 240, .g = 240, .b = 240 } }; 1790 } 1791 } 1792 } 1793 1794 // composites ANSI art behind a foreground grid 1795 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 1796 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1797 errdefer out.deinit(); 1798 1799 const anchor_x = foreground.size.width -| art_grid.size.width; 1800 for (0..art_grid.size.height) |y| { 1801 for (0..art_grid.size.width) |x| { 1802 const src = (try art_grid.cell(x, y)).*; 1803 if (src.rune == null) continue; 1804 const dst = try out.cell(anchor_x + x, y); 1805 const wide = x + 1 < art_grid.size.width and (try art_grid.cell(x + 1, y)).continuation; 1806 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 1807 // blank cells take the art; occupied cells take its background 1808 if (cellIsBlank(dst.*) and (if (next_dst) |next| cellIsBlank(next.*) else true)) { 1809 dst.style = src.style; 1810 dst.style.fg = sgrSafe(dst.style.fg); 1811 dst.style.bg = sgrSafe(dst.style.bg); 1812 try out.setRune(anchor_x + x, y, src.rune); 1813 } else { 1814 applyArtBackground(dst, src); 1815 if (next_dst) |next| applyArtBackground(next, src); 1816 } 1817 } 1818 } 1819 return out; 1820 } 1821 }; 1822 1823 pub const CopyableText = struct { 1824 pub const Choice = struct { 1825 selector: []const u8 = "", 1826 text: []const u8, 1827 copyable_text: ?[]const u8 = null, 1828 label: []const u8 = "", 1829 bottom_label: []const u8 = "", 1830 }; 1831 1832 box: wgt.Box(Widget), 1833 session: *ui.Session, 1834 choices: []Choice, 1835 selected: usize = 0, 1836 1837 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice, initial: usize) !CopyableText { 1838 if (choices.len == 0) return error.MissingCopyableText; 1839 1840 const owned_choices = try allocator.dupe(Choice, choices); 1841 errdefer allocator.free(owned_choices); 1842 1843 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1844 errdefer box.deinit(allocator); 1845 1846 if (choices.len > 1) for (choices) |choice| { 1847 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border_style = .hidden, .round_corners = true, .wrap_kind = .none }); 1848 errdefer selector_box.deinit(allocator); 1849 selector_box.getFocus().mode = .all; 1850 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 } }); 1851 }; 1852 1853 var text_input = try wgt.TextInput.init(allocator, .{ 1854 .border_style = .single, 1855 .round_corners = true, 1856 .label = choices[initial].label, 1857 .bottom_label = choices[initial].bottom_label, 1858 .read_only = true, 1859 .render_content = session.is_terminal, 1860 .visible_width = null, 1861 }); 1862 errdefer text_input.deinit(allocator); 1863 text_input.getFocus().mode = .all; 1864 try text_input.setContent(allocator, choices[initial].text); 1865 box.getFocus().child_id = text_input.getFocus().id; 1866 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1867 1868 return .{ .box = box, .session = session, .choices = owned_choices, .selected = initial }; 1869 } 1870 1871 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, handle: ui.RepoHandle) !?CopyableText { 1872 const aa = session.page_arena.allocator(); 1873 var choices: [2]Choice = undefined; 1874 var count: usize = 0; 1875 var ssh_index: ?usize = null; 1876 switch (handle.location) { 1877 .repo => |identity| { 1878 if (session.data.git_http_port) |port| { 1879 const url = try std.fmt.allocPrint(aa, "http://localhost:{d}/repo/{s}", .{ port, identity }); 1880 choices[count] = .{ 1881 .selector = "http", 1882 .text = url, 1883 .copyable_text = try std.fmt.allocPrint(aa, "git clone {s}", .{url}), 1884 .label = " clone ", 1885 }; 1886 count += 1; 1887 } 1888 if (session.data.git_ssh_port) |port| { 1889 ssh_index = count; 1890 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}", .{ port, identity }); 1891 choices[count] = .{ 1892 .selector = "ssh", 1893 .text = url, 1894 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1895 .label = " clone ", 1896 }; 1897 count += 1; 1898 } 1899 }, 1900 .fork => |f| if (session.data.git_ssh_port) |port| { 1901 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/fork/{s}/patch:{s}", .{ port, f.identity, f.id }); 1902 choices[count] = .{ 1903 .text = url, 1904 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1905 .label = " clone ", 1906 }; 1907 count += 1; 1908 }, 1909 } 1910 if (count == 0) return null; 1911 // a viewer who can push starts on the ssh url 1912 const initial = if (handle.canWrite()) ssh_index orelse 0 else 0; 1913 return @as(?CopyableText, try init(allocator, session, choices[0..count], initial)); 1914 } 1915 1916 pub fn minWidth(self: *const CopyableText) usize { 1917 var width = maxWidth(self.choices) + 2; 1918 if (self.choices.len > 1) { 1919 for (self.choices) |choice| width += choice.selector.len + 2; 1920 } 1921 return width; 1922 } 1923 1924 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1925 self.box.deinit(allocator); 1926 allocator.free(self.choices); 1927 } 1928 1929 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1930 self.clearGrid(); 1931 1932 if (self.choices.len > 1) { 1933 if (root_focus.grandchild_id) |id| { 1934 for (0..self.choices.len) |i| { 1935 if (id != self.selector(i).getFocus().id) continue; 1936 if (i != self.selected) try self.select(allocator, i); 1937 root_focus.setFocus(self.textInput().getFocus().id); 1938 break; 1939 } 1940 } 1941 1942 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 1943 for (0..self.choices.len) |i| { 1944 self.selector(i).options.border_style = if (i == self.selected) 1945 (if (focused) .double else .single) 1946 else 1947 .hidden; 1948 } 1949 } 1950 self.textInput().options.border_style = .single; 1951 self.textInput().cursor = 0; 1952 self.textInput().scroll_offset = 0; 1953 try self.box.build(allocator, constraint, root_focus); 1954 1955 if (!self.session.is_terminal) { 1956 const aa = self.session.arena.allocator(); 1957 const id = self.textInput().getFocus().id; 1958 try self.session.text_inputs.put(aa, id, self.textInput()); 1959 } 1960 } 1961 1962 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1963 switch (key) { 1964 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 1965 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 1966 .enter => if (self.session.is_terminal) self.show(), 1967 .mouse => |mouse| { 1968 if (self.choices.len > 1) for (0..self.choices.len) |i| { 1969 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 1970 try self.select(allocator, i); 1971 root_focus.setFocus(self.textInput().getFocus().id); 1972 return; 1973 }; 1974 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 1975 }, 1976 else => {}, 1977 } 1978 } 1979 1980 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 1981 self.selected = selected; 1982 const choice = self.choices[selected]; 1983 const text_input = self.textInput(); 1984 text_input.options.label = choice.label; 1985 text_input.options.bottom_label = choice.bottom_label; 1986 try text_input.setContent(allocator, choice.text); 1987 } 1988 1989 fn show(self: *CopyableText) void { 1990 const choice = self.choices[self.selected]; 1991 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 1992 } 1993 1994 fn textInput(self: *CopyableText) *wgt.TextInput { 1995 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 1996 } 1997 1998 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 1999 return &self.box.children.values()[index].widget.text_box; 2000 } 2001 2002 fn maxWidth(choices: []const Choice) usize { 2003 var width: usize = 0; 2004 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 2005 return width; 2006 } 2007 2008 pub fn clearGrid(self: *CopyableText) void { 2009 self.box.clearGrid(); 2010 } 2011 2012 pub fn getGrid(self: CopyableText) ?Grid { 2013 return self.box.getGrid(); 2014 } 2015 2016 pub fn getFocus(self: *CopyableText) *Focus { 2017 return self.box.getFocus(); 2018 } 2019 }; 2020