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