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