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