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