╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const builtin = @import("builtin"); 3 const evt = @import("../../event.zig"); 4 const ui = @import("../../ui.zig"); 5 const xit = @import("xit"); 6 const rp = xit.repo; 7 const hash = xit.hash; 8 const xitui = xit.xitui; 9 const wgt = xitui.widget; 10 const layout = xitui.layout; 11 const Key = xitui.input.Key; 12 const Grid = xitui.grid.Grid; 13 const Focus = xitui.focus.Focus; 14 const inp = @import("../input.zig"); 15 16 const wasm = builtin.target.cpu.arch == .wasm32; 17 18 // how many issues one window shows before a "next" link appears. 19 pub const page_size = 20; 20 21 // how many tags the tags view shows at most. 22 pub const max_tags = 1000; 23 24 // one issue from the repo's consumed event database, with its hex event id 25 // (the id lives in the event envelope, not the payload). 26 pub const IssueWithId = struct { 27 id: []const u8, 28 issue: evt.Issue, 29 }; 30 31 // one status's windowed listing. 32 pub const Window = struct { 33 issues: []const IssueWithId, 34 // the id of the previous window's first issue, or null when this window 35 // is already the first. 36 prev_id: ?[]const u8, 37 // the id of the next window's first issue, or null when this is the last 38 // window. 39 next_id: ?[]const u8, 40 // how many issues the listing holds across all windows. 41 count: usize, 42 43 pub const empty: Window = .{ .issues = &.{}, .prev_id = null, .next_id = null, .count = 0 }; 44 }; 45 46 // "owner/name", so the view can build /repo/owner/name/issues/... links. 47 identity: []const u8, 48 // the url-encoded tag the lists are filtered to ("" = unfiltered). 49 tag: []const u8, 50 // the hex event id of the issue its status's window is rooted at ("" = the 51 // first window), mirrored into the url. 52 selected_id: []const u8, 53 open: Window, 54 closed: Window, 55 // the view the page shows initially (a selected issue's status overrides the route's) 56 view: ui.RoutablePage.IssuesView, 57 // every tag in the repo, in sorted order, for the tags view. 58 tags: []const []const u8, 59 // the on-disk repo this page was read from, for the terminal submit path 60 // (the web posts the new-issue form to the issue route instead). 61 repo_source: ?ui.RepoSource = null, 62 63 const Self = @This(); 64 65 // `status`'s windowed listing. 66 pub fn window(self: *const Self, status: evt.Issue.Status) *const Window { 67 return switch (status) { 68 .open => &self.open, 69 .closed => &self.closed, 70 }; 71 } 72 73 // an empty listing, for the wasm / no-repo paths. 74 pub fn emptyResult(aa: std.mem.Allocator, identity: []const u8, tag: []const u8, selected_id: []const u8, view: ui.RoutablePage.IssuesView) !Self { 75 return .{ 76 .identity = try aa.dupe(u8, identity), 77 .tag = try aa.dupe(u8, tag), 78 .selected_id = try aa.dupe(u8, selected_id), 79 .open = .empty, 80 .closed = .empty, 81 .view = view, 82 .tags = &.{}, 83 }; 84 } 85 86 // read one window per status of an opened repo's issues (filtered to `tag` 87 // when set), ordered by creation time (newest first). the window of the issue 88 // `selected_id` names starts at it ("" = the beginning). a git repo reads the 89 // event db next to it (synced from the events branch on each page build); a 90 // xit repo reads its own db. 91 pub fn init( 92 comptime repo_kind: rp.RepoKind, 93 comptime repo_opts: rp.RepoOpts(repo_kind), 94 arena: *std.heap.ArenaAllocator, 95 repo: *rp.Repo(repo_kind, repo_opts), 96 io: std.Io, 97 identity: []const u8, 98 tag: []const u8, 99 selected_id: []const u8, 100 view: ui.RoutablePage.IssuesView, 101 ) !Self { 102 const empty = try emptyResult(arena.allocator(), identity, tag, selected_id, view); 103 104 const aa = arena.allocator(); 105 const DB = evt.EventDB(repo_opts.hash); 106 const rooted = empty.selected_id.len != 0; 107 const tagged = empty.tag.len != 0; 108 // an explicitly named issue or tag that doesn't exist is a bad url 109 // (NotFound -> 404); the bare route falls through to an empty listing. 110 const strict = rooted or tagged; 111 112 // a repo with no consumed events has no moment yet. 113 const gpa = arena.child_allocator; 114 var event_db_maybe: ?evt.LocalEventDB(repo_opts.hash) = if (repo_kind == .git) try evt.LocalEventDB(repo_opts.hash).openReadOnly(io, gpa, repo.core.repo_dir) else null; 115 defer if (event_db_maybe) |*event_db| event_db.deinit(io, gpa); 116 const haxy_moment = (if (event_db_maybe) |*event_db| 117 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 118 else if (repo_kind == .git) 119 return empty 120 else 121 evt.currentMoment(repo_opts, repo)) catch { 122 if (strict) return error.NotFound; 123 return empty; 124 }; 125 126 // the sorted sets to window, per status: the tag's sets when filtered, 127 // else the top-level per-status sets. a missing set is an empty window. 128 var open_set: ?DB.SortedSet(.read_only) = null; 129 var closed_set: ?DB.SortedSet(.read_only) = null; 130 if (tagged) { 131 const tag_to_issues_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "tag+status->issue-id-set")) orelse return error.NotFound; 132 const tag_to_issues = try DB.SortedMap(.read_only).init(tag_to_issues_cursor); 133 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, empty.tag)); 134 open_set = try tagStatusSet(DB, tag_to_issues, decoded, .open); 135 closed_set = try tagStatusSet(DB, tag_to_issues, decoded, .closed); 136 // a tag no issue carries is a bad url 137 if (open_set == null and closed_set == null) return error.NotFound; 138 } else if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "status->issue-id-set"))) |status_to_issues_cursor| { 139 const status_to_issues = try DB.SortedMap(.read_only).init(status_to_issues_cursor); 140 open_set = try statusSet(DB, status_to_issues, .open); 141 closed_set = try statusSet(DB, status_to_issues, .closed); 142 } else if (rooted) return error.NotFound; 143 144 const event_id_to_issue_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "event-id->issue")) orelse { 145 if (strict) return error.NotFound; 146 return empty; 147 }; 148 const event_id_to_issue = try DB.HashMap(.read_only).init(event_id_to_issue_cursor); 149 150 // a named issue roots its own status's window at itself; the other window 151 // starts at the beginning. 152 var resolved_view = empty.view; 153 var open_root: ?[]const u8 = null; 154 var closed_root: ?[]const u8 = null; 155 if (rooted) { 156 if (empty.selected_id.len != evt.event_id_size * 2) return error.NotFound; 157 var id_bytes: [evt.event_id_size]u8 = undefined; 158 _ = std.fmt.hexToBytes(&id_bytes, empty.selected_id) catch return error.NotFound; 159 const issue_cursor = try event_id_to_issue.getCursor(hash.hashInt(repo_opts.hash, &id_bytes)) orelse return error.NotFound; 160 const issue_map = try DB.HashMap(.read_only).init(issue_cursor); 161 const issue_event = try evt.read(evt.Issue, DB, repo_opts.hash, arena, issue_map); 162 const order_key = try aa.dupe(u8, &evt.orderKeyDesc(issue_event.created_ts, &id_bytes)); 163 164 // the named issue must be in its windowed set (a tag url can name an 165 // issue that doesn't carry the tag). 166 const set = (switch (issue_event.status) { 167 .open => open_set, 168 .closed => closed_set, 169 }) orelse return error.NotFound; 170 if (!try set.contains(order_key)) return error.NotFound; 171 172 switch (issue_event.status) { 173 .open => { 174 open_root = order_key; 175 resolved_view = .open; 176 }, 177 .closed => { 178 closed_root = order_key; 179 resolved_view = .closed; 180 }, 181 } 182 } 183 184 const open_window = try loadWindow(repo_opts.hash, arena, event_id_to_issue, open_set, open_root); 185 const closed_window = try loadWindow(repo_opts.hash, arena, event_id_to_issue, closed_set, closed_root); 186 187 // every tag in the repo, in the tag map's sorted order. the keys are 188 // "tag,status", so a tag's entries are adjacent and dedup by prefix. 189 var tags: std.ArrayList([]const u8) = .empty; 190 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "tag+status->issue-id-set"))) |tag_to_issues_cursor| { 191 const tag_to_issues = try DB.SortedMap(.read_only).init(tag_to_issues_cursor); 192 var tag_iter = try tag_to_issues.iterator(); 193 while (try tag_iter.next()) |kv_pair_cursor| { 194 if (tags.items.len == max_tags) break; 195 var kv_cursor = kv_pair_cursor; 196 const kv_pair = try kv_cursor.readKeyValuePair(); 197 const key = try kv_pair.key_cursor.readBytesAlloc(aa, null); 198 const space = std.mem.indexOfScalar(u8, key, ' ') orelse continue; 199 const key_tag = key[0..space]; 200 if (tags.getLastOrNull()) |last| { 201 if (std.mem.eql(u8, last, key_tag)) continue; 202 } 203 try tags.append(aa, key_tag); 204 } 205 } 206 207 return .{ 208 .identity = empty.identity, 209 .tag = empty.tag, 210 .selected_id = empty.selected_id, 211 .open = open_window, 212 .closed = closed_window, 213 .view = resolved_view, 214 .tags = tags.items, 215 }; 216 } 217 218 // `status`'s sorted set within `statuses` (null when the status has no issues) 219 fn statusSet( 220 comptime DB: type, 221 statuses: DB.SortedMap(.read_only), 222 status: evt.Issue.Status, 223 ) !?DB.SortedSet(.read_only) { 224 const cursor = (try statuses.getCursor(@tagName(status))) orelse return null; 225 return try DB.SortedSet(.read_only).init(cursor); 226 } 227 228 // the set at `tag_to_issues`'s "tag,status" key (null when no issue carries 229 // `tag` with `status`, or the tag is too long to exist) 230 fn tagStatusSet( 231 comptime DB: type, 232 tag_to_issues: DB.SortedMap(.read_only), 233 tag: []const u8, 234 status: evt.Issue.Status, 235 ) !?DB.SortedSet(.read_only) { 236 var key_buffer: evt.Issue.TagStatusKey = undefined; 237 const key = evt.Issue.tagStatusKey(&key_buffer, tag, status) catch return null; 238 const cursor = (try tag_to_issues.getCursor(key)) orelse return null; 239 return try DB.SortedSet(.read_only).init(cursor); 240 } 241 242 // read one window of `set_maybe` (null = an empty listing) starting at 243 // `root_key` (null = the beginning) 244 fn loadWindow( 245 comptime hash_kind: hash.HashKind, 246 arena: *std.heap.ArenaAllocator, 247 event_id_to_issue: evt.EventDB(hash_kind).HashMap(.read_only), 248 set_maybe: ?evt.EventDB(hash_kind).SortedSet(.read_only), 249 root_key: ?[]const u8, 250 ) !Window { 251 const set = set_maybe orelse return .empty; 252 const DB = evt.EventDB(hash_kind); 253 const aa = arena.allocator(); 254 255 // seek once to the window start: the root key, or the set's first entry. 256 var prev_id: ?[]const u8 = null; 257 var iter = if (root_key) |key| blk: { 258 // the previous window starts page_size ranks back ("" = the first 259 // window, linked as the bare list route). 260 const rank = try set.rank(key); 261 if (rank > 0 and rank <= page_size) { 262 prev_id = ""; 263 } else if (rank > page_size) { 264 const kv = try set.getIndexKeyValuePair(@intCast(rank - page_size)) orelse return error.NotFound; 265 var prev_key: [@sizeOf(u64) + evt.event_id_size]u8 = undefined; 266 _ = try kv.key_cursor.readBytes(&prev_key); 267 const prev_hex = std.fmt.bytesToHex(prev_key[@sizeOf(u64)..].*, .lower); 268 prev_id = try aa.dupe(u8, &prev_hex); 269 } 270 break :blk try set.iteratorFrom(key); 271 } else try set.iteratorFromIndex(0); 272 273 // collect this window's issues, plus a peek at the one after it (its id is 274 // the next window's start). the trailing bytes of each set key are the 275 // issue event id. 276 var issues: std.ArrayList(IssueWithId) = .empty; 277 var next_id: ?[]const u8 = null; 278 while (try iter.next()) |id_cursor_val| { 279 var id_cursor = id_cursor_val; 280 const id_kv = try id_cursor.readKeyValuePair(); 281 var order_key: [@sizeOf(u64) + evt.event_id_size]u8 = undefined; 282 _ = try id_kv.key_cursor.readBytes(&order_key); 283 const id_hex = std.fmt.bytesToHex(order_key[@sizeOf(u64)..].*, .lower); 284 if (issues.items.len == page_size) { 285 next_id = try aa.dupe(u8, &id_hex); 286 break; 287 } 288 const issue_cursor = try event_id_to_issue.getCursor(hash.hashInt(hash_kind, order_key[@sizeOf(u64)..])) orelse continue; 289 const issue_map = try DB.HashMap(.read_only).init(issue_cursor); 290 try issues.append(aa, .{ 291 .id = try aa.dupe(u8, &id_hex), 292 .issue = try evt.read(evt.Issue, DB, hash_kind, arena, issue_map), 293 }); 294 } 295 296 return .{ 297 .issues = issues.items, 298 .prev_id = prev_id, 299 .next_id = next_id, 300 .count = @intCast(try set.count()), 301 }; 302 } 303 304 pub const View = struct { 305 // a vertical box: the header tabs on top, then a stack holding a 306 // master-detail split (issue list + description pane) per status list, 307 // plus the tags view. 308 box: wgt.Box(ui.Widget), // vert: [header_index] = tabs, [stack_index] = stack 309 data: *const Self, 310 session: *ui.Session, 311 // per-split state, indexed like the stack's split children: the issue the 312 // pane shows, and its description text box's focus id. 313 detailed_index: [split_count]?usize, 314 description_id: [split_count]?usize, 315 316 const header_index: usize = 0; 317 const stack_index: usize = 1; 318 // indices within the stack, 1:1 with the header tabs. 319 const open_view_index: usize = 0; 320 const closed_view_index: usize = 1; 321 const tags_view_index: usize = 2; 322 const new_view_index: usize = 3; 323 const split_count: usize = 2; 324 // indices within a split (the horizontal box inside the stack). 325 const list_index: usize = 0; 326 const detail_index: usize = 1; 327 const list_max_width: usize = 40; 328 const detail_min_width: usize = 40; 329 // indices within the new-issue form. 330 const title_field_index: usize = 0; 331 const tags_field_index: usize = 1; 332 const description_field_index: usize = 2; 333 const submit_field_index: usize = 3; 334 335 fn viewIndex(view: ui.RoutablePage.IssuesView) usize { 336 return switch (view) { 337 .open => open_view_index, 338 .closed => closed_view_index, 339 .tags => tags_view_index, 340 .new => new_view_index, 341 }; 342 } 343 344 fn splitStatus(index: usize) evt.Issue.Status { 345 return if (index == open_view_index) .open else .closed; 346 } 347 348 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 349 var outer = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 350 errdefer outer.deinit(allocator); 351 352 // the tabs at the top. 353 { 354 var hdr = try Header.init(allocator, session, data); 355 errdefer hdr.deinit(allocator); 356 try outer.children.put(allocator, hdr.getFocus().id, .{ .widget = .{ .repo_issues_header = hdr }, .rect = null, .min_size = null }); 357 } 358 359 // the stack enters `outer` before its children enter it, so an error 360 // frees each child exactly once. 361 { 362 var stack = try wgt.Stack(ui.Widget).init(allocator); 363 errdefer stack.deinit(allocator); 364 try outer.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 365 } 366 const stack = &outer.children.values()[stack_index].widget.stack; 367 368 // a master-detail split per status list. 369 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 370 var split = try initSplit(allocator, session, data, status); 371 errdefer split.deinit(allocator); 372 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 373 } 374 375 // the tags view 376 { 377 var tf = try ui.TagFlow.init(allocator); 378 errdefer tf.deinit(allocator); 379 var items: std.ArrayList(ui.TagFlow.Item) = .empty; 380 defer items.deinit(allocator); 381 // when filtered, the first item clears the filter 382 if (data.tag.len != 0) 383 try items.append(allocator, .{ .text = "╳", .link = try issuesLink(session.page_arena, data.identity, .open, "", "") }); 384 for (data.tags) |tag| 385 try items.append(allocator, .{ .text = tag, .link = try tagLink(session.page_arena, data.identity, .open, tag) }); 386 try tf.setItems(allocator, items.items); 387 try stack.children.put(allocator, tf.getFocus().id, .{ .tag_flow = tf }); 388 } 389 390 // the new-issue form 391 { 392 var form = try initNewForm(allocator, session, data); 393 errdefer form.deinit(allocator); 394 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 395 } 396 397 // the stack starts on the page's view. 398 stack.getFocus().child_id = stack.children.keys()[viewIndex(data.view)]; 399 400 // focus entering the view lands on the tabs first. 401 outer.getFocus().child_id = outer.children.keys()[header_index]; 402 403 return .{ 404 .box = outer, 405 .data = data, 406 .session = session, 407 .detailed_index = .{ null, null }, 408 .description_id = .{ null, null }, 409 }; 410 } 411 412 // the master-detail split showing `status`'s window. 413 fn initSplit(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, status: evt.Issue.Status) !wgt.Box(ui.Widget) { 414 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 415 errdefer box.deinit(allocator); 416 417 const win = data.window(status); 418 419 // the issue list (one focusable row per title), plus a "next" link that 420 // reloads the page rooted at the following issue. 421 { 422 var list_scroll = blk: { 423 var list_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 424 errdefer list_box.deinit(allocator); 425 if (win.prev_id) |prev| 426 try addRow(allocator, &list_box, "← previous", try issuesLink(session.page_arena, data.identity, status, data.tag, prev)); 427 for (win.issues) |entry| 428 try addRow(allocator, &list_box, entry.issue.title, try issueRowLink(session.page_arena, data.identity, entry.id)); 429 if (win.next_id) |next| 430 try addRow(allocator, &list_box, "next →", try issuesLink(session.page_arena, data.identity, status, data.tag, next)); 431 // select the window's first issue (past a leading "previous" 432 // row) so its description shows on load. 433 if (win.issues.len > 0) 434 list_box.getFocus().child_id = list_box.children.keys()[if (win.prev_id != null) 1 else 0] 435 else if (list_box.children.count() > 0) 436 list_box.getFocus().child_id = list_box.children.keys()[0]; 437 break :blk try wgt.Scroll(ui.Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal }); 438 }; 439 errdefer list_scroll.deinit(allocator); 440 try box.children.put(allocator, list_scroll.getFocus().id, .{ .widget = .{ .scroll = list_scroll }, .rect = null, .min_size = .{ .width = list_max_width, .height = null }, .max_size = .{ .width = list_max_width, .height = null } }); 441 } 442 443 // the detail pane — a frame around a scroll of the description 444 { 445 var detail_outer = blk: { 446 var detail_scroll = blk2: { 447 var detail_inner = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 448 errdefer detail_inner.deinit(allocator); 449 // fill the pane (content top-left, scroll bar pinned to the 450 // edge) rather than shrinking to the description. 451 break :blk2 try wgt.Scroll(ui.Widget).init(allocator, .{ .box = detail_inner }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 452 }; 453 errdefer detail_scroll.deinit(allocator); 454 var frame = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .hidden, .direction = .vert }); 455 errdefer frame.deinit(allocator); 456 // the frame's selected child is its scroll, so the focus chain 457 // reaches the description (populateDetail points the scroll's 458 // inner box at it), letting focus recovery descend into the pane. 459 frame.getFocus().child_id = detail_scroll.getFocus().id; 460 try frame.children.put(allocator, detail_scroll.getFocus().id, .{ .widget = .{ .scroll = detail_scroll }, .rect = null, .min_size = null }); 461 break :blk frame; 462 }; 463 errdefer detail_outer.deinit(allocator); 464 try box.children.put(allocator, detail_outer.getFocus().id, .{ .widget = .{ .box = detail_outer }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 465 } 466 467 box.getFocus().child_id = box.children.keys()[list_index]; 468 return box; 469 } 470 471 // the new-issue form: title/tags/description inputs and a submit button. 472 // its form: subtree makes the web overlay wrap them in a <form> POSTing to 473 // the issue route. 474 fn initNewForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !wgt.Box(ui.Widget) { 475 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 476 errdefer box.deinit(allocator); 477 box.getFocus().kind = .{ .custom = if (data.identity.len == 0) 478 "form:/issue" 479 else 480 try std.fmt.allocPrint(session.page_arena.allocator(), "form:/repo/{s}/issue", .{data.identity}) }; 481 482 { 483 var title = try wgt.TextInput(ui.Widget).init(allocator, .{ .label = " title ", .name = "title", .visible_width = null, .rounded_corners = true, .render_content = !wasm }); 484 errdefer title.deinit(allocator); 485 title.getFocus().focusable = true; 486 try box.children.put(allocator, title.getFocus().id, .{ .widget = .{ .text_input = title }, .rect = null, .min_size = null }); 487 } 488 489 { 490 var tags = try wgt.TextInput(ui.Widget).init(allocator, .{ .label = " tags (separate with spaces) ", .name = "tags", .visible_width = null, .rounded_corners = true, .render_content = !wasm }); 491 errdefer tags.deinit(allocator); 492 tags.getFocus().focusable = true; 493 try box.children.put(allocator, tags.getFocus().id, .{ .widget = .{ .text_input = tags }, .rect = null, .min_size = null }); 494 } 495 496 { 497 var description = try wgt.TextInput(ui.Widget).init(allocator, .{ .label = " description ", .name = "description", .visible_width = null, .rounded_corners = true, .render_content = !wasm, .multiline = true, .visible_height = 5 }); 498 errdefer description.deinit(allocator); 499 description.getFocus().focusable = true; 500 try box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .text_input = description }, .rect = null, .min_size = null }); 501 } 502 503 { 504 var button = try wgt.TextBox(ui.Widget).init(allocator, "submit", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 505 errdefer button.deinit(allocator); 506 button.getFocus().focusable = true; 507 // the renderer distinguishes plain clickables from buttons that 508 // should POST to a server route by this kind. 509 button.getFocus().kind = .{ .custom = "submit" }; 510 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 511 } 512 513 // absorbs the leftover min-height the box hands its last child, so 514 // the button keeps its natural height 515 { 516 var spacer = try ui.Spacer.init(allocator); 517 errdefer spacer.deinit(allocator); 518 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 519 } 520 521 box.getFocus().child_id = box.children.keys()[title_field_index]; 522 return box; 523 } 524 525 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, link: []const u8) !void { 526 var row = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 527 errdefer row.deinit(allocator); 528 row.getFocus().focusable = true; 529 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 530 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 531 } 532 533 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 534 self.box.deinit(allocator); 535 } 536 537 fn header(self: *View) *Header { 538 return &self.box.children.values()[header_index].widget.repo_issues_header; 539 } 540 541 fn viewStack(self: *View) *wgt.Stack(ui.Widget) { 542 return &self.box.children.values()[stack_index].widget.stack; 543 } 544 545 // `index`'s master-detail split inside the stack. 546 fn resultsBox(self: *View, index: usize) *wgt.Box(ui.Widget) { 547 return &self.viewStack().children.values()[index].box; 548 } 549 550 // the tags view's flow inside the stack. 551 fn tagsView(self: *View) *ui.TagFlow { 552 return &self.viewStack().children.values()[tags_view_index].tag_flow; 553 } 554 555 // the new-issue form inside the stack. 556 fn newForm(self: *View) *wgt.Box(ui.Widget) { 557 return &self.viewStack().children.values()[new_view_index].box; 558 } 559 560 fn listScroll(self: *View, index: usize) *wgt.Scroll(ui.Widget) { 561 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 562 } 563 564 fn listBox(self: *View, index: usize) *wgt.Box(ui.Widget) { 565 return &self.listScroll(index).child.box; 566 } 567 568 fn detailOuter(self: *View, index: usize) *wgt.Box(ui.Widget) { 569 return &self.resultsBox(index).children.values()[detail_index].widget.box; 570 } 571 572 fn detailScroll(self: *View, index: usize) *wgt.Scroll(ui.Widget) { 573 return &self.detailOuter(index).children.values()[0].widget.scroll; 574 } 575 576 fn detailInner(self: *View, index: usize) *wgt.Box(ui.Widget) { 577 return &self.detailScroll(index).child.box; 578 } 579 580 fn window(self: *View, index: usize) *const Window { 581 return self.data.window(splitStatus(index)); 582 } 583 584 fn stackSelectedIndex(self: *View) ?usize { 585 const stack = self.viewStack(); 586 const cid = stack.getFocus().child_id orelse return null; 587 return stack.children.getIndex(cid); 588 } 589 590 // the stack's selected master-detail split (null when the tags view or the 591 // new-issue form shows). 592 fn selectedSplitIndex(self: *View) ?usize { 593 const idx = self.stackSelectedIndex() orelse return null; 594 return if (idx < split_count) idx else null; 595 } 596 597 fn detailActive(self: *View, index: usize) bool { 598 const rb = self.resultsBox(index); 599 const cid = rb.getFocus().child_id orelse return false; 600 return rb.children.getIndex(cid) == detail_index; 601 } 602 603 fn headerActive(self: *View) bool { 604 const cid = self.box.getFocus().child_id orelse return false; 605 return self.box.children.getIndex(cid) == header_index; 606 } 607 608 fn tagsViewActive(self: *View) bool { 609 if (self.headerActive()) return false; 610 return self.stackSelectedIndex() == tags_view_index; 611 } 612 613 fn newViewActive(self: *View) bool { 614 if (self.headerActive()) return false; 615 return self.stackSelectedIndex() == new_view_index; 616 } 617 618 // the selected issue's index, or null when a window-navigation row is 619 // selected (a leading "previous" row shifts the issue rows down by one). 620 fn selectedIssueIndex(self: *View, index: usize) ?usize { 621 const lb = self.listBox(index); 622 const cid = lb.getFocus().child_id orelse return null; 623 const idx = lb.children.getIndex(cid) orelse return null; 624 const win = self.window(index); 625 const lead: usize = if (win.prev_id != null) 1 else 0; 626 if (idx < lead or idx - lead >= win.issues.len) return null; 627 return idx - lead; 628 } 629 630 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 631 self.clearGrid(); 632 633 // each header tab maps 1:1 to a stack child by position. mirror the 634 // selection into the url: the view the page's window is rooted at 635 // keeps its rooted url, the others get their list route (keeping the 636 // tag filter). 637 if (self.header().getSelectedIndex()) |index| { 638 const stack = self.viewStack(); 639 stack.getFocus().child_id = stack.children.keys()[index]; 640 self.session.data.current_page = (if (index == viewIndex(self.data.view) and self.data.selected_id.len != 0) 641 ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, self.data.tag, self.data.selected_id) 642 else if (index == tags_view_index) 643 ui.RoutablePage.repoIssuesTagsRoute(self.data.identity, self.data.tag) 644 else if (index == new_view_index) 645 ui.RoutablePage.repoIssuesNewRoute(self.data.identity) 646 else 647 ui.RoutablePage.repoIssuesRoute(self.data.identity, splitStatus(index), self.data.tag, "")) orelse self.session.data.current_page; 648 } 649 650 if (self.selectedSplitIndex()) |i| { 651 // swap the detail pane to the selected issue when it changes. 652 try self.refreshDetail(allocator, i); 653 654 // mirror the focused issue into the url, but only while focus is 655 // inside the split. an issue's url is the same whether or not the 656 // list is filtered, so the mirror drops the tag. 657 if (root_focus.grandchild_id) |g| { 658 if (self.resultsBox(i).getFocus().children.contains(g)) { 659 if (self.selectedIssueIndex(i)) |sel| { 660 if (ui.RoutablePage.repoIssuesRoute(self.data.identity, splitStatus(i), "", self.window(i).issues[sel].id)) |route| 661 self.session.data.current_page = route; 662 } 663 } 664 } 665 666 // the selected list row shows a border (the focused TextBox 667 // upgrades it to a double border itself); the rest stay borderless. 668 const lb = self.listBox(i); 669 for (lb.children.keys(), lb.children.values()) |id, *child| { 670 switch (child.widget) { 671 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 672 else => {}, 673 } 674 } 675 676 // the pane's selected child shows the selection border: the 677 // description directly, the tags via the flow's selected item. 678 // cap the list at list_max_width only while the detail pane fits 679 // beside it. the box drops the detail when the width can't hold 680 // both minimums, so when it's that narrow we lift the cap and let 681 // the list fill the whole width. 682 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 683 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 684 685 // stretch the detail pane across the rest of the width so it fills 686 // the area rather than shrinking to its content; its scroll fills 687 // the pane. 688 if (constraint.max_size.width) |w| { 689 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 690 } else { 691 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 692 } 693 } 694 695 // refresh the form inputs' entries in the session's focus-id -> input 696 // map with this frame's addresses, so the web/wasm form handling can 697 // find them by focus id 698 const form = self.newForm(); 699 const inputs_arena = self.session.arena.allocator(); 700 for (form.children.values()) |*child| switch (child.widget) { 701 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 702 else => {}, 703 }; 704 705 try self.box.build(allocator, constraint, root_focus); 706 } 707 708 fn refreshDetail(self: *View, allocator: std.mem.Allocator, index: usize) !void { 709 const sel = self.selectedIssueIndex(index) orelse return; 710 if (self.detailed_index[index]) |d| if (d == sel) return; 711 try self.populateDetail(allocator, index, sel); 712 self.detailed_index[index] = sel; 713 } 714 715 fn populateDetail(self: *View, allocator: std.mem.Allocator, index: usize, sel: usize) !void { 716 const entry = self.window(index).issues[sel]; 717 const inner = self.detailInner(index); 718 719 for (inner.children.values()) |*child| child.widget.deinit(allocator); 720 inner.children.clearAndFree(allocator); 721 inner.getFocus().child_id = null; 722 723 // the open/close button 724 { 725 const action: []const u8 = switch (entry.issue.status) { 726 .open => "close", 727 .closed => "open", 728 }; 729 var row = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 730 errdefer row.deinit(allocator); 731 const pa = self.session.page_arena.allocator(); 732 row.getFocus().kind = .{ .custom = if (self.data.identity.len == 0) 733 try std.fmt.allocPrint(pa, "form:/issues/{s}/{s}", .{ entry.id, action }) 734 else 735 try std.fmt.allocPrint(pa, "form:/repo/{s}/issues/{s}/{s}", .{ self.data.identity, entry.id, action }) }; 736 737 { 738 var spacer = try ui.Spacer.init(allocator); 739 errdefer spacer.deinit(allocator); 740 try row.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 741 } 742 743 { 744 var button = try wgt.TextBox(ui.Widget).init(allocator, action, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 745 errdefer button.deinit(allocator); 746 button.getFocus().focusable = true; 747 // the renderer distinguishes plain clickables from buttons that 748 // should POST to a server route by this kind. 749 button.getFocus().kind = .{ .custom = "submit" }; 750 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = action.len + 2, .height = null } }); 751 } 752 753 row.getFocus().child_id = row.children.keys()[button_in_row_index]; 754 try inner.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 755 } 756 757 // the issue's tags, each linking to this status's list filtered to 758 // that tag. 759 { 760 var items: std.ArrayList(ui.TagFlow.Item) = .empty; 761 defer items.deinit(allocator); 762 var tag_iter = evt.Issue.tagIterator(entry.issue.tags); 763 while (tag_iter.next()) |tag| { 764 if (tag.len == 0) continue; 765 try items.append(allocator, .{ .text = tag, .link = try tagLink(self.session.page_arena, self.data.identity, splitStatus(index), tag) }); 766 } 767 if (items.items.len > 0) { 768 var tf = try ui.TagFlow.init(allocator); 769 errdefer tf.deinit(allocator); 770 try tf.setItems(allocator, items.items); 771 try inner.children.put(allocator, tf.getFocus().id, .{ .widget = .{ .tag_flow = tf }, .rect = null, .min_size = null }); 772 } 773 } 774 775 // the description as a focusable word-wrapped text box. 776 self.description_id[index] = blk: { 777 const description = if (entry.issue.description.len == 0) "(no description)" else entry.issue.description; 778 var tb = try wgt.TextBox(ui.Widget).init(allocator, description, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word }); 779 errdefer tb.deinit(allocator); 780 tb.getFocus().focusable = true; 781 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 782 break :blk tb.getFocus().id; 783 }; 784 785 // select the description by default 786 inner.getFocus().child_id = self.description_id[index]; 787 788 // reset the scroll to the top for the newly-shown issue: directly on the 789 // terminal (the wasm offset), and via a version bump on the web (so the 790 // renderer's scroll id changes and JS drops the preserved position). 791 const sc = self.detailScroll(index); 792 sc.x = 0; 793 sc.y = 0; 794 sc.getFocus().version +%= 1; 795 } 796 797 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 798 if (self.headerActive()) { 799 // down from the tabs re-enters the stack if the selected view has 800 // something to focus; other keys move the tabs. 801 if (inp.vertDirection(key) == .down) { 802 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 803 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 804 else => true, 805 } else false; 806 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 807 } else { 808 try self.header().input(allocator, key, root_focus); 809 } 810 return; 811 } 812 if (self.tagsViewActive()) { 813 self.tagsViewInput(key, root_focus); 814 return; 815 } 816 if (self.newViewActive()) { 817 try self.newViewInput(allocator, key, root_focus); 818 return; 819 } 820 const i = self.selectedSplitIndex() orelse return; 821 if (self.detailActive(i)) { 822 try self.detailInput(allocator, i, key, root_focus); 823 } else { 824 try self.listInput(i, key, root_focus); 825 } 826 } 827 828 // arrow keys move the tag selection; up from the top row crosses to the 829 // header tabs. 830 fn tagsViewInput(self: *View, key: Key, root_focus: *Focus) void { 831 const tf = self.tagsView(); 832 const cid = tf.focus.child_id orelse return; 833 const cur = tf.indexOfFocusId(cid) orelse return; 834 const count = tf.text_boxes.items.len; 835 switch (key) { 836 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 837 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 838 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 839 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 840 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 841 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 842 else => {}, 843 } 844 } 845 846 fn listInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 847 // up/down (and the scroll wheel) move the selection a row; page up/down 848 // jump a fixed amount. right/Enter cross into the detail pane. up from 849 // the top row crosses into the header tabs. 850 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 851 const lb = self.listBox(index); 852 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 853 if (delta < 0 and at_top) return self.focusHeader(root_focus); 854 ui.moveRowFocus(lb, self.listScroll(index), root_focus, delta); 855 return; 856 } 857 switch (key) { 858 .enter, .arrow_right => try self.focusDetail(index, root_focus), 859 else => {}, 860 } 861 } 862 863 fn detailInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 864 if (self.statusButtonFocused(index)) { 865 try self.statusButtonInput(allocator, index, key, root_focus); 866 } else if (self.tagsFocused(index)) { 867 try self.tagsInput(index, key, root_focus); 868 } else { 869 try self.descriptionInput(index, key, root_focus); 870 } 871 } 872 873 // the open/close button: enter or a click flips the issue's status; 874 // arrows cross to the neighboring widgets. 875 fn statusButtonInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 876 switch (key) { 877 .arrow_left => try self.focusList(index, root_focus), 878 .arrow_up => self.focusHeader(root_focus), 879 .arrow_down => if (self.tagFlow(index) != null) try self.focusTags(index, root_focus) else try self.focusDescription(index, root_focus), 880 .enter => try self.toggleIssueStatus(allocator, index), 881 .mouse => |mouse| switch (mouse.action) { 882 .scroll => |dir| { 883 const sc = self.detailScroll(index); 884 sc.y += if (dir == .up) @as(isize, -1) else 1; 885 sc.clampToContent(); 886 }, 887 else => if (self.statusButton(index)) |button| { 888 if (inp.leftClickOn(root_focus, button.getFocus().id, mouse)) try self.toggleIssueStatus(allocator, index); 889 }, 890 }, 891 else => {}, 892 } 893 } 894 895 fn descriptionInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 896 const sc = self.detailScroll(index); 897 switch (key) { 898 .arrow_left => return self.focusList(index, root_focus), 899 // once the scroll can't move further, cross into the tags (or the 900 // open/close button when the issue has none). 901 .arrow_up => { 902 const before = sc.y; 903 sc.y -= 1; 904 sc.clampToContent(); 905 if (sc.y == before) { 906 if (self.tagFlow(index) != null) try self.focusTags(index, root_focus) else self.focusStatusButton(index, root_focus); 907 } 908 return; 909 }, 910 .arrow_down => sc.y += 1, 911 .page_up => sc.y -= 10, 912 .page_down => sc.y += 10, 913 .home => sc.y = 0, 914 .end => sc.y = std.math.maxInt(isize), 915 .mouse => |mouse| switch (mouse.action) { 916 .scroll => |dir| sc.y += if (dir == .up) @as(isize, -1) else 1, 917 else => {}, 918 }, 919 else => return, 920 } 921 sc.clampToContent(); 922 } 923 924 // up/down (and tab/shift+tab) move between the form's fields; up from the 925 // title crosses into the header tabs. the multiline description keeps 926 // enter and any up/down that has a row to move to. 927 fn newViewInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 928 const form = self.newForm(); 929 const cid = form.getFocus().child_id orelse return; 930 const cur = form.children.getIndex(cid) orelse return; 931 932 if (cur == description_field_index) { 933 const child = &form.children.values()[cur]; 934 const description = &child.widget.text_input; 935 switch (key) { 936 .enter => return child.widget.input(allocator, key, root_focus), 937 .arrow_up => if (!try description.cursorOnFirstRow(allocator)) 938 return child.widget.input(allocator, key, root_focus), 939 .arrow_down => if (!try description.cursorOnLastRow(allocator)) 940 return child.widget.input(allocator, key, root_focus), 941 else => {}, 942 } 943 } 944 945 switch (key) { 946 .arrow_up, .back_tab => if (cur > 0) 947 root_focus.setFocus(form.children.keys()[cur - 1]) 948 else 949 self.focusHeader(root_focus), 950 .arrow_down, .tab => if (cur < submit_field_index) { 951 root_focus.setFocus(form.children.keys()[cur + 1]); 952 }, 953 .enter => if (cur == submit_field_index) try self.submitNewIssue(allocator), 954 .mouse => |mouse| if (cur == submit_field_index and inp.leftClickOn(root_focus, cid, mouse)) { 955 try self.submitNewIssue(allocator); 956 }, 957 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 958 } 959 } 960 961 // commit the new issue to the repo's events branch and navigate to it. 962 // this is the terminal path; the web posts the form to the issue route, 963 // so the wasm side never runs (or compiles) the repo access below. 964 fn submitNewIssue(self: *View, allocator: std.mem.Allocator) !void { 965 if (comptime wasm) return; 966 const io = self.session.io orelse return; 967 const src = self.data.repo_source orelse return; 968 969 const form = self.newForm(); 970 const title_input = &form.children.values()[title_field_index].widget.text_input; 971 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 972 const description_input = &form.children.values()[description_field_index].widget.text_input; 973 974 const title = try title_input.text(allocator); 975 defer allocator.free(title); 976 const tags = try tags_input.text(allocator); 977 defer allocator.free(tags); 978 const description = try description_input.text(allocator); 979 defer allocator.free(description); 980 981 // a title is required, and a too-long tag would fail consumption 982 // after the event is already committed 983 if (title.len == 0) return; 984 var tag_iter = evt.Issue.tagIterator(tags); 985 while (tag_iter.next()) |tag| { 986 if (tag.len > evt.Issue.tag_max_len) return; 987 } 988 989 var id_bytes: [evt.event_id_size]u8 = undefined; 990 io.random(&id_bytes); 991 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 992 993 const event = evt.EventWithId{ 994 .id = event_id_hex, 995 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 996 .event = .{ .issue = .{ 997 .title = title, 998 .description = description, 999 .tags = tags, 1000 } }, 1001 }; 1002 1003 switch (src.repo_kind) { 1004 inline else => |repo_kind| { 1005 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1006 defer any_repo.deinit(io, allocator); 1007 switch (any_repo) { 1008 inline else => |*repo| try evt.commitAndConsume(repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 1009 } 1010 }, 1011 } 1012 1013 // wipe the form so a return visit starts fresh 1014 title_input.clear(allocator); 1015 tags_input.clear(allocator); 1016 description_input.clear(allocator); 1017 1018 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, "", &event_id_hex) orelse return; 1019 try self.session.navigate(route); 1020 } 1021 1022 // flip the shown issue's status by re-emitting its event, then reload the 1023 // page rooted at the issue so the view reflects the change. this is the 1024 // terminal path; the web posts the button's form to the status route. 1025 fn toggleIssueStatus(self: *View, allocator: std.mem.Allocator, index: usize) !void { 1026 if (comptime wasm) return; 1027 const io = self.session.io orelse return; 1028 const src = self.data.repo_source orelse return; 1029 const sel = self.detailed_index[index] orelse return; 1030 const entry = self.window(index).issues[sel]; 1031 1032 var updated = entry.issue; 1033 updated.status = switch (entry.issue.status) { 1034 .open => .closed, 1035 .closed => .open, 1036 }; 1037 1038 const event = evt.EventWithId{ 1039 .id = entry.id[0 .. evt.event_id_size * 2].*, 1040 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 1041 .event = .{ .issue = updated }, 1042 }; 1043 1044 switch (src.repo_kind) { 1045 inline else => |repo_kind| { 1046 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1047 defer any_repo.deinit(io, allocator); 1048 switch (any_repo) { 1049 inline else => |*repo| try evt.commitAndConsume(repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 1050 } 1051 }, 1052 } 1053 1054 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, updated.status, "", entry.id) orelse return; 1055 try self.session.navigate(route); 1056 } 1057 1058 // arrow keys move the tag selection; at the flow's edges focus crosses to 1059 // the neighboring widgets. 1060 fn tagsInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1061 const tf = self.tagFlow(index) orelse return; 1062 const cid = tf.focus.child_id orelse return; 1063 const cur = tf.indexOfFocusId(cid) orelse return; 1064 const count = tf.text_boxes.items.len; 1065 const sc = self.detailScroll(index); 1066 switch (key) { 1067 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 1068 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 1069 .arrow_up => if (tf.rowStep(cur, false)) |i| self.focusTag(index, tf, root_focus, i) else self.focusStatusButton(index, root_focus), 1070 .arrow_down => if (tf.rowStep(cur, true)) |i| self.focusTag(index, tf, root_focus, i) else try self.focusDescription(index, root_focus), 1071 .home => self.focusTag(index, tf, root_focus, 0), 1072 .end => self.focusTag(index, tf, root_focus, count - 1), 1073 .mouse => |mouse| switch (mouse.action) { 1074 .scroll => |dir| { 1075 sc.y += if (dir == .up) @as(isize, -1) else 1; 1076 sc.clampToContent(); 1077 }, 1078 else => {}, 1079 }, 1080 else => {}, 1081 } 1082 } 1083 1084 const button_row_index: usize = 0; 1085 const button_in_row_index: usize = 1; 1086 const tags_child_index: usize = 1; 1087 1088 fn tagFlow(self: *View, index: usize) ?*ui.TagFlow { 1089 const inner = self.detailInner(index); 1090 if (inner.children.count() <= tags_child_index) return null; 1091 return switch (inner.children.values()[tags_child_index].widget) { 1092 .tag_flow => |*tf| tf, 1093 else => null, 1094 }; 1095 } 1096 1097 fn tagsFocused(self: *View, index: usize) bool { 1098 const inner = self.detailInner(index); 1099 const cid = inner.getFocus().child_id orelse return false; 1100 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 1101 } 1102 1103 // the open/close button inside the detail pane's leading row. 1104 fn statusButton(self: *View, index: usize) ?*wgt.TextBox(ui.Widget) { 1105 const inner = self.detailInner(index); 1106 if (inner.children.count() == 0) return null; 1107 const row = &inner.children.values()[button_row_index].widget.box; 1108 return &row.children.values()[button_in_row_index].widget.text_box; 1109 } 1110 1111 fn statusButtonFocused(self: *View, index: usize) bool { 1112 const inner = self.detailInner(index); 1113 const cid = inner.getFocus().child_id orelse return false; 1114 return inner.children.getIndex(cid) == button_row_index; 1115 } 1116 1117 fn focusTag(self: *View, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 1118 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 1119 // keep the tag visible on the terminal: its rect offset by the flow's 1120 // position in the pane. 1121 if (self.session.is_terminal and item < tf.rects.items.len) { 1122 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 1123 var rect = tf.rects.items[item]; 1124 rect.x += flow_rect.x; 1125 rect.y += flow_rect.y; 1126 self.detailScroll(index).scrollToRect(rect); 1127 } 1128 } 1129 } 1130 1131 fn focusTags(self: *View, index: usize, root_focus: *Focus) !void { 1132 const tf = self.tagFlow(index) orelse return; 1133 if (tf.text_boxes.items.len == 0) return; 1134 const cid = tf.focus.child_id orelse tf.text_boxes.items[0].getFocus().id; 1135 const item = tf.indexOfFocusId(cid) orelse 0; 1136 self.focusTag(index, tf, root_focus, item); 1137 } 1138 1139 fn focusDescription(self: *View, index: usize, root_focus: *Focus) !void { 1140 if (self.description_id[index]) |id| root_focus.setFocus(id); 1141 } 1142 1143 fn focusStatusButton(self: *View, index: usize, root_focus: *Focus) void { 1144 if (self.statusButton(index)) |button| root_focus.setFocus(button.getFocus().id); 1145 } 1146 1147 // enter the detail pane. an empty pane (no issues) can't be entered. 1148 fn focusDetail(self: *View, index: usize, root_focus: *Focus) !void { 1149 if (self.detailInner(index).children.count() == 0) return; 1150 root_focus.setFocus(self.detailOuter(index).getFocus().id); 1151 } 1152 1153 // return to the list. 1154 fn focusList(self: *View, index: usize, root_focus: *Focus) !void { 1155 root_focus.setFocus(self.listScroll(index).getFocus().id); 1156 } 1157 1158 // cross to the header tabs above the stack. 1159 fn focusHeader(self: *View, root_focus: *Focus) void { 1160 root_focus.setFocus(self.box.children.keys()[header_index]); 1161 } 1162 1163 pub fn clearGrid(self: *View) void { 1164 self.box.clearGrid(); 1165 } 1166 1167 pub fn getGrid(self: View) ?Grid { 1168 return self.box.getGrid(); 1169 } 1170 1171 pub fn getFocus(self: *View) *Focus { 1172 return self.box.getFocus(); 1173 } 1174 1175 // for the parent's "scroll up at the top jumps to the header" check: at the 1176 // top only while the header tabs hold focus, so up from the split 1177 // crosses into the tabs first. 1178 pub fn getSelectedIndex(self: *View) ?usize { 1179 return if (self.headerActive()) 0 else 1; 1180 } 1181 }; 1182 1183 // the "a:" navigation link for `status`'s issues page filtered to the 1184 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 1185 // a rooted url ignores `status` (it derives its view from the issue's own); 1186 // with an empty `id` this is the bare list link. 1187 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 1188 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 1189 const url = try route.toUrl(page_arena); 1190 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1191 } 1192 1193 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 1194 // href is only followed with js off. 1195 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 1196 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 1197 const url = try route.toUrl(page_arena); 1198 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 1199 } 1200 1201 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 1202 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 1203 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 1204 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 1205 const url = try route.toUrl(page_arena); 1206 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1207 } 1208 1209 // tabs switching between the issues page's views. 1210 pub const Header = struct { 1211 box: wgt.Box(ui.Widget), 1212 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 1213 1214 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 1215 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1216 errdefer box.deinit(allocator); 1217 1218 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 1219 errdefer tab_ids.deinit(allocator); 1220 1221 const aa = session.page_arena.allocator(); 1222 1223 // a list tab per status, labeled with its listing's issue count 1224 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 1225 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 1226 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 1227 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 1228 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1229 errdefer text_box.deinit(allocator); 1230 text_box.getFocus().focusable = true; 1231 text_box.getFocus().kind = .{ .custom = link }; 1232 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1233 try box.children.put(allocator, text_box.getFocus().id, .{ 1234 .widget = .{ .text_box = text_box }, 1235 .rect = null, 1236 .min_size = .{ .width = label.len + 2, .height = null }, 1237 }); 1238 } 1239 1240 // tags tab, labeled with the active tag filter 1241 { 1242 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 1243 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 1244 const label = if (data.tag.len == 0) "tags" else blk: { 1245 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 1246 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 1247 }; 1248 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1249 errdefer text_box.deinit(allocator); 1250 text_box.getFocus().focusable = true; 1251 text_box.getFocus().kind = .{ .custom = tags_link }; 1252 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1253 try box.children.put(allocator, text_box.getFocus().id, .{ 1254 .widget = .{ .text_box = text_box }, 1255 .rect = null, 1256 .min_size = .{ .width = label.len + 2, .height = null }, 1257 }); 1258 } 1259 1260 // new-issue tab 1261 { 1262 const new_route = ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong; 1263 const new_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try new_route.toUrl(session.page_arena)}); 1264 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "new", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1265 errdefer text_box.deinit(allocator); 1266 text_box.getFocus().focusable = true; 1267 text_box.getFocus().kind = .{ .custom = new_link }; 1268 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1269 try box.children.put(allocator, text_box.getFocus().id, .{ 1270 .widget = .{ .text_box = text_box }, 1271 .rect = null, 1272 .min_size = .{ .width = "new".len + 2, .height = null }, 1273 }); 1274 } 1275 1276 var self = Header{ .box = box, .tab_ids = tab_ids }; 1277 // the tab matching the page's view is selected initially. 1278 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 1279 return self; 1280 } 1281 1282 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 1283 self.box.deinit(allocator); 1284 self.tab_ids.deinit(allocator); 1285 } 1286 1287 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1288 self.clearGrid(); 1289 // only the selected tab shows its border 1290 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 1291 switch (child.widget) { 1292 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 1293 else => {}, 1294 } 1295 } 1296 try self.box.build(allocator, constraint, root_focus); 1297 } 1298 1299 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1300 _ = allocator; 1301 const current_tab = self.currentTabIndex() orelse return; 1302 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 1303 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 1304 } 1305 } 1306 1307 pub fn clearGrid(self: *Header) void { 1308 self.box.clearGrid(); 1309 } 1310 1311 pub fn getGrid(self: Header) ?Grid { 1312 return self.box.getGrid(); 1313 } 1314 1315 pub fn getFocus(self: *Header) *Focus { 1316 return self.box.getFocus(); 1317 } 1318 1319 pub fn getSelectedIndex(self: Header) ?usize { 1320 return self.currentTabIndex(); 1321 } 1322 1323 fn currentTabIndex(self: Header) ?usize { 1324 const child_id = self.box.focus.child_id orelse return null; 1325 return self.tab_ids.getIndex(child_id); 1326 } 1327 }; 1328