╭ 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 const inner = self.detailInner(i); 679 for (inner.children.keys(), inner.children.values()) |id, *child| { 680 switch (child.widget) { 681 .text_box => |*tb| tb.options.border_style = if (inner.getFocus().child_id == id) .single else .hidden, 682 else => {}, 683 } 684 } 685 if (self.tagFlow(i)) |tf| tf.selected = self.tagsFocused(i); 686 687 // cap the list at list_max_width only while the detail pane fits 688 // beside it. the box drops the detail when the width can't hold 689 // both minimums, so when it's that narrow we lift the cap and let 690 // the list fill the whole width. 691 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 692 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 693 694 // stretch the detail pane across the rest of the width so it fills 695 // the area rather than shrinking to its content; its scroll fills 696 // the pane. 697 if (constraint.max_size.width) |w| { 698 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 699 } else { 700 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 701 } 702 } 703 704 self.tagsView().selected = self.tagsViewActive(); 705 706 // refresh the form inputs' entries in the session's focus-id -> input 707 // map with this frame's addresses, so the web/wasm form handling can 708 // find them by focus id 709 const form = self.newForm(); 710 const inputs_arena = self.session.arena.allocator(); 711 for (form.children.values()) |*child| switch (child.widget) { 712 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 713 else => {}, 714 }; 715 716 try self.box.build(allocator, constraint, root_focus); 717 } 718 719 fn refreshDetail(self: *View, allocator: std.mem.Allocator, index: usize) !void { 720 const sel = self.selectedIssueIndex(index) orelse return; 721 if (self.detailed_index[index]) |d| if (d == sel) return; 722 try self.populateDetail(allocator, index, sel); 723 self.detailed_index[index] = sel; 724 } 725 726 fn populateDetail(self: *View, allocator: std.mem.Allocator, index: usize, sel: usize) !void { 727 const entry = self.window(index).issues[sel]; 728 const inner = self.detailInner(index); 729 730 for (inner.children.values()) |*child| child.widget.deinit(allocator); 731 inner.children.clearAndFree(allocator); 732 inner.getFocus().child_id = null; 733 734 // the open/close button 735 { 736 const action: []const u8 = switch (entry.issue.status) { 737 .open => "close", 738 .closed => "open", 739 }; 740 var row = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 741 errdefer row.deinit(allocator); 742 const pa = self.session.page_arena.allocator(); 743 row.getFocus().kind = .{ .custom = if (self.data.identity.len == 0) 744 try std.fmt.allocPrint(pa, "form:/issues/{s}/{s}", .{ entry.id, action }) 745 else 746 try std.fmt.allocPrint(pa, "form:/repo/{s}/issues/{s}/{s}", .{ self.data.identity, entry.id, action }) }; 747 748 { 749 var spacer = try ui.Spacer.init(allocator); 750 errdefer spacer.deinit(allocator); 751 try row.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 752 } 753 754 { 755 var button = try wgt.TextBox(ui.Widget).init(allocator, action, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 756 errdefer button.deinit(allocator); 757 button.getFocus().focusable = true; 758 // the renderer distinguishes plain clickables from buttons that 759 // should POST to a server route by this kind. 760 button.getFocus().kind = .{ .custom = "submit" }; 761 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = action.len + 2, .height = null } }); 762 } 763 764 row.getFocus().child_id = row.children.keys()[button_in_row_index]; 765 try inner.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 766 } 767 768 // the issue's tags, each linking to this status's list filtered to 769 // that tag. 770 { 771 var items: std.ArrayList(ui.TagFlow.Item) = .empty; 772 defer items.deinit(allocator); 773 var tag_iter = evt.Issue.tagIterator(entry.issue.tags); 774 while (tag_iter.next()) |tag| { 775 if (tag.len == 0) continue; 776 try items.append(allocator, .{ .text = tag, .link = try tagLink(self.session.page_arena, self.data.identity, splitStatus(index), tag) }); 777 } 778 if (items.items.len > 0) { 779 var tf = try ui.TagFlow.init(allocator); 780 errdefer tf.deinit(allocator); 781 try tf.setItems(allocator, items.items); 782 try inner.children.put(allocator, tf.getFocus().id, .{ .widget = .{ .tag_flow = tf }, .rect = null, .min_size = null }); 783 } 784 } 785 786 // the description as a focusable word-wrapped text box. its hidden 787 // border reserves the space the border occupies when focused, so 788 // focusing doesn't shift layout. 789 self.description_id[index] = blk: { 790 const description = if (entry.issue.description.len == 0) "(no description)" else entry.issue.description; 791 var tb = try wgt.TextBox(ui.Widget).init(allocator, description, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 792 errdefer tb.deinit(allocator); 793 tb.getFocus().focusable = true; 794 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 795 break :blk tb.getFocus().id; 796 }; 797 798 // select the description by default 799 inner.getFocus().child_id = self.description_id[index]; 800 801 // reset the scroll to the top for the newly-shown issue: directly on the 802 // terminal (the wasm offset), and via a version bump on the web (so the 803 // renderer's scroll id changes and JS drops the preserved position). 804 const sc = self.detailScroll(index); 805 sc.x = 0; 806 sc.y = 0; 807 sc.getFocus().version +%= 1; 808 } 809 810 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 811 if (self.headerActive()) { 812 // down from the tabs re-enters the stack if the selected view has 813 // something to focus; other keys move the tabs. 814 if (inp.vertDirection(key) == .down) { 815 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 816 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 817 else => true, 818 } else false; 819 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 820 } else { 821 try self.header().input(allocator, key, root_focus); 822 } 823 return; 824 } 825 if (self.tagsViewActive()) { 826 self.tagsViewInput(key, root_focus); 827 return; 828 } 829 if (self.newViewActive()) { 830 try self.newViewInput(allocator, key, root_focus); 831 return; 832 } 833 const i = self.selectedSplitIndex() orelse return; 834 if (self.detailActive(i)) { 835 try self.detailInput(allocator, i, key, root_focus); 836 } else { 837 try self.listInput(i, key, root_focus); 838 } 839 } 840 841 // arrow keys move the tag selection; up from the top row crosses to the 842 // header tabs. 843 fn tagsViewInput(self: *View, key: Key, root_focus: *Focus) void { 844 const tf = self.tagsView(); 845 const cid = tf.focus.child_id orelse return; 846 const cur = tf.indexOfFocusId(cid) orelse return; 847 const count = tf.text_boxes.items.len; 848 switch (key) { 849 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 850 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 851 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 852 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 853 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 854 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 855 else => {}, 856 } 857 } 858 859 fn listInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 860 // up/down (and the scroll wheel) move the selection a row; page up/down 861 // jump a fixed amount. right/Enter cross into the detail pane. up from 862 // the top row crosses into the header tabs. 863 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 864 const lb = self.listBox(index); 865 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 866 if (delta < 0 and at_top) return self.focusHeader(root_focus); 867 ui.moveRowFocus(lb, self.listScroll(index), root_focus, delta); 868 return; 869 } 870 switch (key) { 871 .enter, .arrow_right => try self.focusDetail(index, root_focus), 872 else => {}, 873 } 874 } 875 876 fn detailInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 877 if (self.statusButtonFocused(index)) { 878 try self.statusButtonInput(allocator, index, key, root_focus); 879 } else if (self.tagsFocused(index)) { 880 try self.tagsInput(index, key, root_focus); 881 } else { 882 try self.descriptionInput(index, key, root_focus); 883 } 884 } 885 886 // the open/close button: enter or a click flips the issue's status; 887 // arrows cross to the neighboring widgets. 888 fn statusButtonInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 889 switch (key) { 890 .arrow_left => try self.focusList(index, root_focus), 891 .arrow_up => self.focusHeader(root_focus), 892 .arrow_down => if (self.tagFlow(index) != null) try self.focusTags(index, root_focus) else try self.focusDescription(index, root_focus), 893 .enter => try self.toggleIssueStatus(allocator, index), 894 .mouse => |mouse| switch (mouse.action) { 895 .scroll => |dir| { 896 const sc = self.detailScroll(index); 897 sc.y += if (dir == .up) @as(isize, -1) else 1; 898 sc.clampToContent(); 899 }, 900 else => if (self.statusButton(index)) |button| { 901 if (inp.leftClickOn(root_focus, button.getFocus().id, mouse)) try self.toggleIssueStatus(allocator, index); 902 }, 903 }, 904 else => {}, 905 } 906 } 907 908 fn descriptionInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 909 const sc = self.detailScroll(index); 910 switch (key) { 911 .arrow_left => return self.focusList(index, root_focus), 912 // once the scroll can't move further, cross into the tags (or the 913 // open/close button when the issue has none). 914 .arrow_up => { 915 const before = sc.y; 916 sc.y -= 1; 917 sc.clampToContent(); 918 if (sc.y == before) { 919 if (self.tagFlow(index) != null) try self.focusTags(index, root_focus) else self.focusStatusButton(index, root_focus); 920 } 921 return; 922 }, 923 .arrow_down => sc.y += 1, 924 .page_up => sc.y -= 10, 925 .page_down => sc.y += 10, 926 .home => sc.y = 0, 927 .end => sc.y = std.math.maxInt(isize), 928 .mouse => |mouse| switch (mouse.action) { 929 .scroll => |dir| sc.y += if (dir == .up) @as(isize, -1) else 1, 930 else => {}, 931 }, 932 else => return, 933 } 934 sc.clampToContent(); 935 } 936 937 // up/down (and tab/shift+tab) move between the form's fields; up from the 938 // title crosses into the header tabs. the multiline description keeps 939 // enter and any up/down that has a row to move to. 940 fn newViewInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 941 const form = self.newForm(); 942 const cid = form.getFocus().child_id orelse return; 943 const cur = form.children.getIndex(cid) orelse return; 944 945 if (cur == description_field_index) { 946 const child = &form.children.values()[cur]; 947 const description = &child.widget.text_input; 948 switch (key) { 949 .enter => return child.widget.input(allocator, key, root_focus), 950 .arrow_up => if (!try description.cursorOnFirstRow(allocator)) 951 return child.widget.input(allocator, key, root_focus), 952 .arrow_down => if (!try description.cursorOnLastRow(allocator)) 953 return child.widget.input(allocator, key, root_focus), 954 else => {}, 955 } 956 } 957 958 switch (key) { 959 .arrow_up, .back_tab => if (cur > 0) 960 root_focus.setFocus(form.children.keys()[cur - 1]) 961 else 962 self.focusHeader(root_focus), 963 .arrow_down, .tab => if (cur < submit_field_index) { 964 root_focus.setFocus(form.children.keys()[cur + 1]); 965 }, 966 .enter => if (cur == submit_field_index) try self.submitNewIssue(allocator), 967 .mouse => |mouse| if (cur == submit_field_index and inp.leftClickOn(root_focus, cid, mouse)) { 968 try self.submitNewIssue(allocator); 969 }, 970 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 971 } 972 } 973 974 // commit the new issue to the repo's events branch and navigate to it. 975 // this is the terminal path; the web posts the form to the issue route, 976 // so the wasm side never runs (or compiles) the repo access below. 977 fn submitNewIssue(self: *View, allocator: std.mem.Allocator) !void { 978 if (comptime wasm) return; 979 const io = self.session.io orelse return; 980 const src = self.data.repo_source orelse return; 981 982 const form = self.newForm(); 983 const title_input = &form.children.values()[title_field_index].widget.text_input; 984 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 985 const description_input = &form.children.values()[description_field_index].widget.text_input; 986 987 const title = try title_input.text(allocator); 988 defer allocator.free(title); 989 const tags = try tags_input.text(allocator); 990 defer allocator.free(tags); 991 const description = try description_input.text(allocator); 992 defer allocator.free(description); 993 994 // a title is required, and a too-long tag would fail consumption 995 // after the event is already committed 996 if (title.len == 0) return; 997 var tag_iter = evt.Issue.tagIterator(tags); 998 while (tag_iter.next()) |tag| { 999 if (tag.len > evt.Issue.tag_max_len) return; 1000 } 1001 1002 var id_bytes: [evt.event_id_size]u8 = undefined; 1003 io.random(&id_bytes); 1004 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 1005 1006 const event = evt.EventWithId{ 1007 .id = event_id_hex, 1008 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 1009 .event = .{ .issue = .{ 1010 .title = title, 1011 .description = description, 1012 .tags = tags, 1013 } }, 1014 }; 1015 1016 switch (src.repo_kind) { 1017 inline else => |repo_kind| { 1018 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1019 defer any_repo.deinit(io, allocator); 1020 switch (any_repo) { 1021 inline else => |*repo| try evt.commitAndConsume(repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 1022 } 1023 }, 1024 } 1025 1026 // wipe the form so a return visit starts fresh 1027 title_input.clear(allocator); 1028 tags_input.clear(allocator); 1029 description_input.clear(allocator); 1030 1031 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, "", &event_id_hex) orelse return; 1032 try self.session.navigate(route); 1033 } 1034 1035 // flip the shown issue's status by re-emitting its event, then reload the 1036 // page rooted at the issue so the view reflects the change. this is the 1037 // terminal path; the web posts the button's form to the status route. 1038 fn toggleIssueStatus(self: *View, allocator: std.mem.Allocator, index: usize) !void { 1039 if (comptime wasm) return; 1040 const io = self.session.io orelse return; 1041 const src = self.data.repo_source orelse return; 1042 const sel = self.detailed_index[index] orelse return; 1043 const entry = self.window(index).issues[sel]; 1044 1045 var updated = entry.issue; 1046 updated.status = switch (entry.issue.status) { 1047 .open => .closed, 1048 .closed => .open, 1049 }; 1050 1051 const event = evt.EventWithId{ 1052 .id = entry.id[0 .. evt.event_id_size * 2].*, 1053 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 1054 .event = .{ .issue = updated }, 1055 }; 1056 1057 switch (src.repo_kind) { 1058 inline else => |repo_kind| { 1059 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1060 defer any_repo.deinit(io, allocator); 1061 switch (any_repo) { 1062 inline else => |*repo| try evt.commitAndConsume(repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 1063 } 1064 }, 1065 } 1066 1067 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, updated.status, "", entry.id) orelse return; 1068 try self.session.navigate(route); 1069 } 1070 1071 // arrow keys move the tag selection; at the flow's edges focus crosses to 1072 // the neighboring widgets. 1073 fn tagsInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1074 const tf = self.tagFlow(index) orelse return; 1075 const cid = tf.focus.child_id orelse return; 1076 const cur = tf.indexOfFocusId(cid) orelse return; 1077 const count = tf.text_boxes.items.len; 1078 const sc = self.detailScroll(index); 1079 switch (key) { 1080 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 1081 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 1082 .arrow_up => if (tf.rowStep(cur, false)) |i| self.focusTag(index, tf, root_focus, i) else self.focusStatusButton(index, root_focus), 1083 .arrow_down => if (tf.rowStep(cur, true)) |i| self.focusTag(index, tf, root_focus, i) else try self.focusDescription(index, root_focus), 1084 .home => self.focusTag(index, tf, root_focus, 0), 1085 .end => self.focusTag(index, tf, root_focus, count - 1), 1086 .mouse => |mouse| switch (mouse.action) { 1087 .scroll => |dir| { 1088 sc.y += if (dir == .up) @as(isize, -1) else 1; 1089 sc.clampToContent(); 1090 }, 1091 else => {}, 1092 }, 1093 else => {}, 1094 } 1095 } 1096 1097 const button_row_index: usize = 0; 1098 const button_in_row_index: usize = 1; 1099 const tags_child_index: usize = 1; 1100 1101 fn tagFlow(self: *View, index: usize) ?*ui.TagFlow { 1102 const inner = self.detailInner(index); 1103 if (inner.children.count() <= tags_child_index) return null; 1104 return switch (inner.children.values()[tags_child_index].widget) { 1105 .tag_flow => |*tf| tf, 1106 else => null, 1107 }; 1108 } 1109 1110 fn tagsFocused(self: *View, index: usize) bool { 1111 const inner = self.detailInner(index); 1112 const cid = inner.getFocus().child_id orelse return false; 1113 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 1114 } 1115 1116 // the open/close button inside the detail pane's leading row. 1117 fn statusButton(self: *View, index: usize) ?*wgt.TextBox(ui.Widget) { 1118 const inner = self.detailInner(index); 1119 if (inner.children.count() == 0) return null; 1120 const row = &inner.children.values()[button_row_index].widget.box; 1121 return &row.children.values()[button_in_row_index].widget.text_box; 1122 } 1123 1124 fn statusButtonFocused(self: *View, index: usize) bool { 1125 const inner = self.detailInner(index); 1126 const cid = inner.getFocus().child_id orelse return false; 1127 return inner.children.getIndex(cid) == button_row_index; 1128 } 1129 1130 fn focusTag(self: *View, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 1131 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 1132 // keep the tag visible on the terminal: its rect offset by the flow's 1133 // position in the pane. 1134 if (self.session.is_terminal and item < tf.rects.items.len) { 1135 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 1136 var rect = tf.rects.items[item]; 1137 rect.x += flow_rect.x; 1138 rect.y += flow_rect.y; 1139 self.detailScroll(index).scrollToRect(rect); 1140 } 1141 } 1142 } 1143 1144 fn focusTags(self: *View, index: usize, root_focus: *Focus) !void { 1145 const tf = self.tagFlow(index) orelse return; 1146 if (tf.text_boxes.items.len == 0) return; 1147 const cid = tf.focus.child_id orelse tf.text_boxes.items[0].getFocus().id; 1148 const item = tf.indexOfFocusId(cid) orelse 0; 1149 self.focusTag(index, tf, root_focus, item); 1150 } 1151 1152 fn focusDescription(self: *View, index: usize, root_focus: *Focus) !void { 1153 if (self.description_id[index]) |id| root_focus.setFocus(id); 1154 } 1155 1156 fn focusStatusButton(self: *View, index: usize, root_focus: *Focus) void { 1157 if (self.statusButton(index)) |button| root_focus.setFocus(button.getFocus().id); 1158 } 1159 1160 // enter the detail pane. an empty pane (no issues) can't be entered. 1161 fn focusDetail(self: *View, index: usize, root_focus: *Focus) !void { 1162 if (self.detailInner(index).children.count() == 0) return; 1163 root_focus.setFocus(self.detailOuter(index).getFocus().id); 1164 } 1165 1166 // return to the list. 1167 fn focusList(self: *View, index: usize, root_focus: *Focus) !void { 1168 root_focus.setFocus(self.listScroll(index).getFocus().id); 1169 } 1170 1171 // cross to the header tabs above the stack. 1172 fn focusHeader(self: *View, root_focus: *Focus) void { 1173 root_focus.setFocus(self.box.children.keys()[header_index]); 1174 } 1175 1176 pub fn clearGrid(self: *View) void { 1177 self.box.clearGrid(); 1178 } 1179 1180 pub fn getGrid(self: View) ?Grid { 1181 return self.box.getGrid(); 1182 } 1183 1184 pub fn getFocus(self: *View) *Focus { 1185 return self.box.getFocus(); 1186 } 1187 1188 // for the parent's "scroll up at the top jumps to the header" check: at the 1189 // top only while the header tabs hold focus, so up from the split 1190 // crosses into the tabs first. 1191 pub fn getSelectedIndex(self: *View) ?usize { 1192 return if (self.headerActive()) 0 else 1; 1193 } 1194 }; 1195 1196 // the "a:" navigation link for `status`'s issues page filtered to the 1197 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 1198 // a rooted url ignores `status` (it derives its view from the issue's own); 1199 // with an empty `id` this is the bare list link. 1200 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 1201 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 1202 const url = try route.toUrl(page_arena); 1203 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1204 } 1205 1206 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 1207 // href is only followed with js off. 1208 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 1209 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 1210 const url = try route.toUrl(page_arena); 1211 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 1212 } 1213 1214 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 1215 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 1216 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 1217 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 1218 const url = try route.toUrl(page_arena); 1219 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1220 } 1221 1222 // tabs switching between the issues page's views. 1223 pub const Header = struct { 1224 box: wgt.Box(ui.Widget), 1225 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 1226 1227 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 1228 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1229 errdefer box.deinit(allocator); 1230 1231 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 1232 errdefer tab_ids.deinit(allocator); 1233 1234 const aa = session.page_arena.allocator(); 1235 1236 // a list tab per status, labeled with its listing's issue count 1237 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 1238 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 1239 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 1240 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 1241 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1242 errdefer text_box.deinit(allocator); 1243 text_box.getFocus().focusable = true; 1244 text_box.getFocus().kind = .{ .custom = link }; 1245 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1246 try box.children.put(allocator, text_box.getFocus().id, .{ 1247 .widget = .{ .text_box = text_box }, 1248 .rect = null, 1249 .min_size = .{ .width = label.len + 2, .height = null }, 1250 }); 1251 } 1252 1253 // tags tab, labeled with the active tag filter 1254 { 1255 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 1256 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 1257 const label = if (data.tag.len == 0) "tags" else blk: { 1258 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 1259 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 1260 }; 1261 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1262 errdefer text_box.deinit(allocator); 1263 text_box.getFocus().focusable = true; 1264 text_box.getFocus().kind = .{ .custom = tags_link }; 1265 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1266 try box.children.put(allocator, text_box.getFocus().id, .{ 1267 .widget = .{ .text_box = text_box }, 1268 .rect = null, 1269 .min_size = .{ .width = label.len + 2, .height = null }, 1270 }); 1271 } 1272 1273 // new-issue tab 1274 { 1275 const new_route = ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong; 1276 const new_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try new_route.toUrl(session.page_arena)}); 1277 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "new", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1278 errdefer text_box.deinit(allocator); 1279 text_box.getFocus().focusable = true; 1280 text_box.getFocus().kind = .{ .custom = new_link }; 1281 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1282 try box.children.put(allocator, text_box.getFocus().id, .{ 1283 .widget = .{ .text_box = text_box }, 1284 .rect = null, 1285 .min_size = .{ .width = "new".len + 2, .height = null }, 1286 }); 1287 } 1288 1289 var self = Header{ .box = box, .tab_ids = tab_ids }; 1290 // the tab matching the page's view is selected initially. 1291 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 1292 return self; 1293 } 1294 1295 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 1296 self.box.deinit(allocator); 1297 self.tab_ids.deinit(allocator); 1298 } 1299 1300 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1301 self.clearGrid(); 1302 // only the selected tab shows its border 1303 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 1304 switch (child.widget) { 1305 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 1306 else => {}, 1307 } 1308 } 1309 try self.box.build(allocator, constraint, root_focus); 1310 } 1311 1312 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1313 _ = allocator; 1314 const current_tab = self.currentTabIndex() orelse return; 1315 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 1316 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 1317 } 1318 } 1319 1320 pub fn clearGrid(self: *Header) void { 1321 self.box.clearGrid(); 1322 } 1323 1324 pub fn getGrid(self: Header) ?Grid { 1325 return self.box.getGrid(); 1326 } 1327 1328 pub fn getFocus(self: *Header) *Focus { 1329 return self.box.getFocus(); 1330 } 1331 1332 pub fn getSelectedIndex(self: Header) ?usize { 1333 return self.currentTabIndex(); 1334 } 1335 1336 fn currentTabIndex(self: Header) ?usize { 1337 const child_id = self.box.focus.child_id orelse return null; 1338 return self.tab_ids.getIndex(child_id); 1339 } 1340 }; 1341