╭ 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 tool row sits above the scroll (populateDetail fills 457 // it per issue) so it can't scroll out from under the web 458 // overlay <form>, whose position doesn't track pane scrolling. 459 { 460 var row = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 461 errdefer row.deinit(allocator); 462 try frame.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 463 } 464 // the frame's selected child is its scroll, so the focus chain 465 // reaches the description (populateDetail points the scroll's 466 // inner box at it), letting focus recovery descend into the pane. 467 frame.getFocus().child_id = detail_scroll.getFocus().id; 468 try frame.children.put(allocator, detail_scroll.getFocus().id, .{ .widget = .{ .scroll = detail_scroll }, .rect = null, .min_size = null }); 469 break :blk frame; 470 }; 471 errdefer detail_outer.deinit(allocator); 472 try box.children.put(allocator, detail_outer.getFocus().id, .{ .widget = .{ .box = detail_outer }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 473 } 474 475 box.getFocus().child_id = box.children.keys()[list_index]; 476 return box; 477 } 478 479 // the new-issue form: title/tags/description inputs and a submit button. 480 // its form: subtree makes the web overlay wrap them in a <form> POSTing to 481 // the issue route. 482 fn initNewForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !wgt.Box(ui.Widget) { 483 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 484 errdefer box.deinit(allocator); 485 box.getFocus().kind = .{ .custom = if (data.identity.len == 0) 486 "form:/issue" 487 else 488 try std.fmt.allocPrint(session.page_arena.allocator(), "form:/repo/{s}/issue", .{data.identity}) }; 489 490 { 491 var title = try wgt.TextInput(ui.Widget).init(allocator, .{ .label = " title ", .name = "title", .visible_width = null, .rounded_corners = true, .render_content = !wasm }); 492 errdefer title.deinit(allocator); 493 title.getFocus().focusable = true; 494 try box.children.put(allocator, title.getFocus().id, .{ .widget = .{ .text_input = title }, .rect = null, .min_size = null }); 495 } 496 497 { 498 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 }); 499 errdefer tags.deinit(allocator); 500 tags.getFocus().focusable = true; 501 try box.children.put(allocator, tags.getFocus().id, .{ .widget = .{ .text_input = tags }, .rect = null, .min_size = null }); 502 } 503 504 { 505 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 }); 506 errdefer description.deinit(allocator); 507 description.getFocus().focusable = true; 508 try box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .text_input = description }, .rect = null, .min_size = null }); 509 } 510 511 { 512 var button = try wgt.TextBox(ui.Widget).init(allocator, "submit", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 513 errdefer button.deinit(allocator); 514 button.getFocus().focusable = true; 515 // the renderer distinguishes plain clickables from buttons that 516 // should POST to a server route by this kind. 517 button.getFocus().kind = .{ .custom = "submit" }; 518 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 519 } 520 521 // absorbs the leftover min-height the box hands its last child, so 522 // the button keeps its natural height 523 { 524 var spacer = try ui.Spacer.init(allocator); 525 errdefer spacer.deinit(allocator); 526 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 527 } 528 529 box.getFocus().child_id = box.children.keys()[title_field_index]; 530 return box; 531 } 532 533 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, link: []const u8) !void { 534 var row = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 535 errdefer row.deinit(allocator); 536 row.getFocus().focusable = true; 537 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 538 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 539 } 540 541 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 542 self.box.deinit(allocator); 543 } 544 545 fn header(self: *View) *Header { 546 return &self.box.children.values()[header_index].widget.repo_issues_header; 547 } 548 549 fn viewStack(self: *View) *wgt.Stack(ui.Widget) { 550 return &self.box.children.values()[stack_index].widget.stack; 551 } 552 553 // `index`'s master-detail split inside the stack. 554 fn resultsBox(self: *View, index: usize) *wgt.Box(ui.Widget) { 555 return &self.viewStack().children.values()[index].box; 556 } 557 558 // the tags view's flow inside the stack. 559 fn tagsView(self: *View) *ui.TagFlow { 560 return &self.viewStack().children.values()[tags_view_index].tag_flow; 561 } 562 563 // the new-issue form inside the stack. 564 fn newForm(self: *View) *wgt.Box(ui.Widget) { 565 return &self.viewStack().children.values()[new_view_index].box; 566 } 567 568 fn listScroll(self: *View, index: usize) *wgt.Scroll(ui.Widget) { 569 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 570 } 571 572 fn listBox(self: *View, index: usize) *wgt.Box(ui.Widget) { 573 return &self.listScroll(index).child.box; 574 } 575 576 // the detail frame's children: the tool row above the scroll pane. 577 const tool_row_index: usize = 0; 578 const detail_scroll_index: usize = 1; 579 580 fn detailOuter(self: *View, index: usize) *wgt.Box(ui.Widget) { 581 return &self.resultsBox(index).children.values()[detail_index].widget.box; 582 } 583 584 fn toolRow(self: *View, index: usize) *wgt.Box(ui.Widget) { 585 return &self.detailOuter(index).children.values()[tool_row_index].widget.box; 586 } 587 588 fn detailScroll(self: *View, index: usize) *wgt.Scroll(ui.Widget) { 589 return &self.detailOuter(index).children.values()[detail_scroll_index].widget.scroll; 590 } 591 592 fn detailInner(self: *View, index: usize) *wgt.Box(ui.Widget) { 593 return &self.detailScroll(index).child.box; 594 } 595 596 fn window(self: *View, index: usize) *const Window { 597 return self.data.window(splitStatus(index)); 598 } 599 600 fn stackSelectedIndex(self: *View) ?usize { 601 const stack = self.viewStack(); 602 const cid = stack.getFocus().child_id orelse return null; 603 return stack.children.getIndex(cid); 604 } 605 606 // the stack's selected master-detail split (null when the tags view or the 607 // new-issue form shows). 608 fn selectedSplitIndex(self: *View) ?usize { 609 const idx = self.stackSelectedIndex() orelse return null; 610 return if (idx < split_count) idx else null; 611 } 612 613 fn detailActive(self: *View, index: usize) bool { 614 const rb = self.resultsBox(index); 615 const cid = rb.getFocus().child_id orelse return false; 616 return rb.children.getIndex(cid) == detail_index; 617 } 618 619 fn headerActive(self: *View) bool { 620 const cid = self.box.getFocus().child_id orelse return false; 621 return self.box.children.getIndex(cid) == header_index; 622 } 623 624 fn tagsViewActive(self: *View) bool { 625 if (self.headerActive()) return false; 626 return self.stackSelectedIndex() == tags_view_index; 627 } 628 629 fn newViewActive(self: *View) bool { 630 if (self.headerActive()) return false; 631 return self.stackSelectedIndex() == new_view_index; 632 } 633 634 // the selected issue's index, or null when a window-navigation row is 635 // selected (a leading "previous" row shifts the issue rows down by one). 636 fn selectedIssueIndex(self: *View, index: usize) ?usize { 637 const lb = self.listBox(index); 638 const cid = lb.getFocus().child_id orelse return null; 639 const idx = lb.children.getIndex(cid) orelse return null; 640 const win = self.window(index); 641 const lead: usize = if (win.prev_id != null) 1 else 0; 642 if (idx < lead or idx - lead >= win.issues.len) return null; 643 return idx - lead; 644 } 645 646 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 647 self.clearGrid(); 648 649 // each header tab maps 1:1 to a stack child by position. mirror the 650 // selection into the url: the view the page's window is rooted at 651 // keeps its rooted url, the others get their list route (keeping the 652 // tag filter). 653 if (self.header().getSelectedIndex()) |index| { 654 const stack = self.viewStack(); 655 stack.getFocus().child_id = stack.children.keys()[index]; 656 self.session.data.current_page = (if (index == viewIndex(self.data.view) and self.data.selected_id.len != 0) 657 ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, self.data.tag, self.data.selected_id) 658 else if (index == tags_view_index) 659 ui.RoutablePage.repoIssuesTagsRoute(self.data.identity, self.data.tag) 660 else if (index == new_view_index) 661 ui.RoutablePage.repoIssuesNewRoute(self.data.identity) 662 else 663 ui.RoutablePage.repoIssuesRoute(self.data.identity, splitStatus(index), self.data.tag, "")) orelse self.session.data.current_page; 664 } 665 666 if (self.selectedSplitIndex()) |i| { 667 // swap the detail pane to the selected issue when it changes. 668 try self.refreshDetail(allocator, i); 669 670 // mirror the focused issue into the url, but only while focus is 671 // inside the split. an issue's url is the same whether or not the 672 // list is filtered, so the mirror drops the tag. 673 if (root_focus.grandchild_id) |g| { 674 if (self.resultsBox(i).getFocus().children.contains(g)) { 675 if (self.selectedIssueIndex(i)) |sel| { 676 if (ui.RoutablePage.repoIssuesRoute(self.data.identity, splitStatus(i), "", self.window(i).issues[sel].id)) |route| 677 self.session.data.current_page = route; 678 } 679 } 680 } 681 682 // the selected list row shows a border (the focused TextBox 683 // upgrades it to a double border itself); the rest stay borderless. 684 const lb = self.listBox(i); 685 for (lb.children.keys(), lb.children.values()) |id, *child| { 686 switch (child.widget) { 687 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 688 else => {}, 689 } 690 } 691 692 // the pane's selected child shows the selection border: the 693 // description directly, the tags via the flow's selected item. 694 // cap the list at list_max_width only while the detail pane fits 695 // beside it. the box drops the detail when the width can't hold 696 // both minimums, so when it's that narrow we lift the cap and let 697 // the list fill the whole width. 698 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 699 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 700 701 // stretch the detail pane across the rest of the width so it fills 702 // the area rather than shrinking to its content; its scroll fills 703 // the pane. 704 if (constraint.max_size.width) |w| { 705 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 706 } else { 707 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 708 } 709 } 710 711 // refresh the form inputs' entries in the session's focus-id -> input 712 // map with this frame's addresses, so the web/wasm form handling can 713 // find them by focus id 714 const form = self.newForm(); 715 const inputs_arena = self.session.arena.allocator(); 716 for (form.children.values()) |*child| switch (child.widget) { 717 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 718 else => {}, 719 }; 720 721 try self.box.build(allocator, constraint, root_focus); 722 } 723 724 fn refreshDetail(self: *View, allocator: std.mem.Allocator, index: usize) !void { 725 const sel = self.selectedIssueIndex(index) orelse return; 726 if (self.detailed_index[index]) |d| if (d == sel) return; 727 try self.populateDetail(allocator, index, sel); 728 self.detailed_index[index] = sel; 729 } 730 731 fn populateDetail(self: *View, allocator: std.mem.Allocator, index: usize, sel: usize) !void { 732 const entry = self.window(index).issues[sel]; 733 const inner = self.detailInner(index); 734 735 for (inner.children.values()) |*child| child.widget.deinit(allocator); 736 inner.children.clearAndFree(allocator); 737 inner.getFocus().child_id = null; 738 739 // the open/close button 740 { 741 const action: []const u8 = switch (entry.issue.status) { 742 .open => "close", 743 .closed => "open", 744 }; 745 const row = self.toolRow(index); 746 for (row.children.values()) |*child| child.widget.deinit(allocator); 747 row.children.clearAndFree(allocator); 748 const pa = self.session.page_arena.allocator(); 749 row.getFocus().kind = .{ .custom = if (self.data.identity.len == 0) 750 try std.fmt.allocPrint(pa, "form:/issues/{s}/{s}", .{ entry.id, action }) 751 else 752 try std.fmt.allocPrint(pa, "form:/repo/{s}/issues/{s}/{s}", .{ self.data.identity, entry.id, action }) }; 753 754 { 755 var spacer = try ui.Spacer.init(allocator); 756 errdefer spacer.deinit(allocator); 757 try row.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 758 } 759 760 { 761 var button = try wgt.TextBox(ui.Widget).init(allocator, action, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 762 errdefer button.deinit(allocator); 763 button.getFocus().focusable = true; 764 // the renderer distinguishes plain clickables from buttons that 765 // should POST to a server route by this kind. 766 button.getFocus().kind = .{ .custom = "submit" }; 767 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = action.len + 2, .height = null } }); 768 } 769 770 row.getFocus().child_id = row.children.keys()[button_in_row_index]; 771 } 772 773 // the issue's title as a focusable word-wrapped text box. 774 { 775 var tb = try wgt.TextBox(ui.Widget).init(allocator, entry.issue.title, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word }); 776 errdefer tb.deinit(allocator); 777 tb.getFocus().focusable = true; 778 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 779 } 780 781 // the issue's tags, each linking to this status's list filtered to 782 // that tag. 783 { 784 var items: std.ArrayList(ui.TagFlow.Item) = .empty; 785 defer items.deinit(allocator); 786 var tag_iter = evt.Issue.tagIterator(entry.issue.tags); 787 while (tag_iter.next()) |tag| { 788 if (tag.len == 0) continue; 789 try items.append(allocator, .{ .text = tag, .link = try tagLink(self.session.page_arena, self.data.identity, splitStatus(index), tag) }); 790 } 791 if (items.items.len > 0) { 792 var tf = try ui.TagFlow.init(allocator); 793 errdefer tf.deinit(allocator); 794 try tf.setItems(allocator, items.items); 795 try inner.children.put(allocator, tf.getFocus().id, .{ .widget = .{ .tag_flow = tf }, .rect = null, .min_size = null }); 796 } 797 } 798 799 // the description as a focusable word-wrapped text box. 800 self.description_id[index] = blk: { 801 const description = if (entry.issue.description.len == 0) "(no description)" else entry.issue.description; 802 var tb = try wgt.TextBox(ui.Widget).init(allocator, description, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word }); 803 errdefer tb.deinit(allocator); 804 tb.getFocus().focusable = true; 805 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 806 break :blk tb.getFocus().id; 807 }; 808 809 // select the title by default 810 inner.getFocus().child_id = inner.children.keys()[title_child_index]; 811 812 // reset the scroll to the top for the newly-shown issue: directly on the 813 // terminal (the wasm offset), and via a version bump on the web (so the 814 // renderer's scroll id changes and JS drops the preserved position). 815 const sc = self.detailScroll(index); 816 sc.x = 0; 817 sc.y = 0; 818 sc.getFocus().version +%= 1; 819 } 820 821 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 822 if (self.headerActive()) { 823 // down from the tabs re-enters the stack if the selected view has 824 // something to focus; other keys move the tabs. 825 if (inp.vertDirection(key) == .down) { 826 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 827 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 828 else => true, 829 } else false; 830 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 831 } else { 832 try self.header().input(allocator, key, root_focus); 833 } 834 return; 835 } 836 if (self.tagsViewActive()) { 837 self.tagsViewInput(key, root_focus); 838 return; 839 } 840 if (self.newViewActive()) { 841 try self.newViewInput(allocator, key, root_focus); 842 return; 843 } 844 const i = self.selectedSplitIndex() orelse return; 845 if (self.detailActive(i)) { 846 try self.detailInput(allocator, i, key, root_focus); 847 } else { 848 try self.listInput(i, key, root_focus); 849 } 850 } 851 852 // arrow keys move the tag selection; up from the top row crosses to the 853 // header tabs. 854 fn tagsViewInput(self: *View, key: Key, root_focus: *Focus) void { 855 const tf = self.tagsView(); 856 const cid = tf.focus.child_id orelse return; 857 const cur = tf.indexOfFocusId(cid) orelse return; 858 const count = tf.text_boxes.items.len; 859 switch (key) { 860 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 861 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 862 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 863 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 864 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 865 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 866 else => {}, 867 } 868 } 869 870 fn listInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 871 // up/down (and the scroll wheel) move the selection a row; page up/down 872 // jump a fixed amount. right/Enter cross into the detail pane. up from 873 // the top row crosses into the header tabs. 874 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 875 const lb = self.listBox(index); 876 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 877 if (delta < 0 and at_top) return self.focusHeader(root_focus); 878 ui.moveRowFocus(lb, self.listScroll(index), root_focus, delta); 879 return; 880 } 881 switch (key) { 882 .enter, .arrow_right => try self.focusDetail(index, root_focus), 883 else => {}, 884 } 885 } 886 887 fn detailInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 888 if (self.statusButtonFocused(index)) { 889 try self.statusButtonInput(allocator, index, key, root_focus); 890 } else if (self.titleFocused(index)) { 891 try self.titleInput(index, key, root_focus); 892 } else if (self.tagsFocused(index)) { 893 try self.tagsInput(index, key, root_focus); 894 } else { 895 try self.descriptionInput(index, key, root_focus); 896 } 897 } 898 899 // the open/close button: enter or a click flips the issue's status; 900 // arrows cross to the neighboring widgets. 901 fn statusButtonInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 902 switch (key) { 903 .arrow_left => try self.focusList(index, root_focus), 904 .arrow_up => self.focusHeader(root_focus), 905 .arrow_down => self.focusTitle(index, root_focus), 906 .enter => try self.toggleIssueStatus(allocator, index), 907 .mouse => |mouse| switch (mouse.action) { 908 .scroll => |dir| { 909 const sc = self.detailScroll(index); 910 sc.y += if (dir == .up) @as(isize, -1) else 1; 911 sc.clampToContent(); 912 }, 913 else => if (self.statusButton(index)) |button| { 914 if (inp.leftClickOn(root_focus, button.getFocus().id, mouse)) try self.toggleIssueStatus(allocator, index); 915 }, 916 }, 917 else => {}, 918 } 919 } 920 921 fn titleInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 922 const sc = self.detailScroll(index); 923 switch (key) { 924 .arrow_left => try self.focusList(index, root_focus), 925 .arrow_up => self.focusStatusButton(index, root_focus), 926 .arrow_down => if (self.tagFlow(index) != null) try self.focusTags(index, root_focus) else try self.focusDescription(index, root_focus), 927 .mouse => |mouse| switch (mouse.action) { 928 .scroll => |dir| { 929 sc.y += if (dir == .up) @as(isize, -1) else 1; 930 sc.clampToContent(); 931 }, 932 else => {}, 933 }, 934 else => {}, 935 } 936 } 937 938 fn descriptionInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 939 const sc = self.detailScroll(index); 940 switch (key) { 941 .arrow_left => return self.focusList(index, root_focus), 942 // once the scroll can't move further, cross into the tags (or the 943 // open/close button when the issue has none). 944 .arrow_up => { 945 const before = sc.y; 946 sc.y -= 1; 947 sc.clampToContent(); 948 if (sc.y == before) { 949 if (self.tagFlow(index) != null) try self.focusTags(index, root_focus) else self.focusTitle(index, root_focus); 950 } 951 return; 952 }, 953 .arrow_down => sc.y += 1, 954 .page_up => sc.y -= 10, 955 .page_down => sc.y += 10, 956 .home => sc.y = 0, 957 .end => sc.y = std.math.maxInt(isize), 958 .mouse => |mouse| switch (mouse.action) { 959 .scroll => |dir| sc.y += if (dir == .up) @as(isize, -1) else 1, 960 else => {}, 961 }, 962 else => return, 963 } 964 sc.clampToContent(); 965 } 966 967 // up/down (and tab/shift+tab) move between the form's fields; up from the 968 // title crosses into the header tabs. the multiline description keeps 969 // enter and any up/down that has a row to move to. 970 fn newViewInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 971 const form = self.newForm(); 972 const cid = form.getFocus().child_id orelse return; 973 const cur = form.children.getIndex(cid) orelse return; 974 975 if (cur == description_field_index) { 976 const child = &form.children.values()[cur]; 977 const description = &child.widget.text_input; 978 switch (key) { 979 .enter => return child.widget.input(allocator, key, root_focus), 980 .arrow_up => if (!try description.cursorOnFirstRow(allocator)) 981 return child.widget.input(allocator, key, root_focus), 982 .arrow_down => if (!try description.cursorOnLastRow(allocator)) 983 return child.widget.input(allocator, key, root_focus), 984 else => {}, 985 } 986 } 987 988 switch (key) { 989 .arrow_up, .back_tab => if (cur > 0) 990 root_focus.setFocus(form.children.keys()[cur - 1]) 991 else 992 self.focusHeader(root_focus), 993 .arrow_down, .tab => if (cur < submit_field_index) { 994 root_focus.setFocus(form.children.keys()[cur + 1]); 995 }, 996 .enter => if (cur == submit_field_index) try self.submitNewIssue(allocator), 997 .mouse => |mouse| if (cur == submit_field_index and inp.leftClickOn(root_focus, cid, mouse)) { 998 try self.submitNewIssue(allocator); 999 }, 1000 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 1001 } 1002 } 1003 1004 // commit the new issue to the repo's events branch and navigate to it. 1005 // this is the terminal path; the web posts the form to the issue route, 1006 // so the wasm side never runs (or compiles) the repo access below. 1007 fn submitNewIssue(self: *View, allocator: std.mem.Allocator) !void { 1008 if (comptime wasm) return; 1009 const io = self.session.io orelse return; 1010 const src = self.data.repo_source orelse return; 1011 1012 const form = self.newForm(); 1013 const title_input = &form.children.values()[title_field_index].widget.text_input; 1014 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 1015 const description_input = &form.children.values()[description_field_index].widget.text_input; 1016 1017 const title = try title_input.text(allocator); 1018 defer allocator.free(title); 1019 const tags = try tags_input.text(allocator); 1020 defer allocator.free(tags); 1021 const description = try description_input.text(allocator); 1022 defer allocator.free(description); 1023 1024 // a title is required, and a too-long tag would fail consumption 1025 // after the event is already committed 1026 if (title.len == 0) return; 1027 var tag_iter = evt.Issue.tagIterator(tags); 1028 while (tag_iter.next()) |tag| { 1029 if (tag.len > evt.Issue.tag_max_len) return; 1030 } 1031 1032 var id_bytes: [evt.event_id_size]u8 = undefined; 1033 io.random(&id_bytes); 1034 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 1035 1036 const event = evt.EventWithId{ 1037 .id = event_id_hex, 1038 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 1039 .event = .{ .issue = .{ 1040 .title = title, 1041 .description = description, 1042 .tags = tags, 1043 } }, 1044 }; 1045 1046 switch (src.repo_kind) { 1047 inline else => |repo_kind| { 1048 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1049 defer any_repo.deinit(io, allocator); 1050 switch (any_repo) { 1051 inline else => |*repo| try evt.commitAndConsume(repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 1052 } 1053 }, 1054 } 1055 1056 // wipe the form so a return visit starts fresh 1057 title_input.clear(allocator); 1058 tags_input.clear(allocator); 1059 description_input.clear(allocator); 1060 1061 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, "", &event_id_hex) orelse return; 1062 try self.session.navigate(route); 1063 } 1064 1065 // flip the shown issue's status by re-emitting its event, then reload the 1066 // page rooted at the issue so the view reflects the change. this is the 1067 // terminal path; the web posts the button's form to the status route. 1068 fn toggleIssueStatus(self: *View, allocator: std.mem.Allocator, index: usize) !void { 1069 if (comptime wasm) return; 1070 const io = self.session.io orelse return; 1071 const src = self.data.repo_source orelse return; 1072 const sel = self.detailed_index[index] orelse return; 1073 const entry = self.window(index).issues[sel]; 1074 1075 var updated = entry.issue; 1076 updated.status = switch (entry.issue.status) { 1077 .open => .closed, 1078 .closed => .open, 1079 }; 1080 1081 const event = evt.EventWithId{ 1082 .id = entry.id[0 .. evt.event_id_size * 2].*, 1083 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 1084 .event = .{ .issue = updated }, 1085 }; 1086 1087 switch (src.repo_kind) { 1088 inline else => |repo_kind| { 1089 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1090 defer any_repo.deinit(io, allocator); 1091 switch (any_repo) { 1092 inline else => |*repo| try evt.commitAndConsume(repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 1093 } 1094 }, 1095 } 1096 1097 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, updated.status, "", entry.id) orelse return; 1098 try self.session.navigate(route); 1099 } 1100 1101 // arrow keys move the tag selection; at the flow's edges focus crosses to 1102 // the neighboring widgets. 1103 fn tagsInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1104 const tf = self.tagFlow(index) orelse return; 1105 const cid = tf.focus.child_id orelse return; 1106 const cur = tf.indexOfFocusId(cid) orelse return; 1107 const count = tf.text_boxes.items.len; 1108 const sc = self.detailScroll(index); 1109 switch (key) { 1110 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 1111 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 1112 .arrow_up => if (tf.rowStep(cur, false)) |i| self.focusTag(index, tf, root_focus, i) else self.focusTitle(index, root_focus), 1113 .arrow_down => if (tf.rowStep(cur, true)) |i| self.focusTag(index, tf, root_focus, i) else try self.focusDescription(index, root_focus), 1114 .home => self.focusTag(index, tf, root_focus, 0), 1115 .end => self.focusTag(index, tf, root_focus, count - 1), 1116 .mouse => |mouse| switch (mouse.action) { 1117 .scroll => |dir| { 1118 sc.y += if (dir == .up) @as(isize, -1) else 1; 1119 sc.clampToContent(); 1120 }, 1121 else => {}, 1122 }, 1123 else => {}, 1124 } 1125 } 1126 1127 const button_in_row_index: usize = 1; 1128 const title_child_index: usize = 0; 1129 const tags_child_index: usize = 1; 1130 1131 fn tagFlow(self: *View, index: usize) ?*ui.TagFlow { 1132 const inner = self.detailInner(index); 1133 if (inner.children.count() <= tags_child_index) return null; 1134 return switch (inner.children.values()[tags_child_index].widget) { 1135 .tag_flow => |*tf| tf, 1136 else => null, 1137 }; 1138 } 1139 1140 fn tagsFocused(self: *View, index: usize) bool { 1141 const inner = self.detailInner(index); 1142 const cid = inner.getFocus().child_id orelse return false; 1143 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 1144 } 1145 1146 // the open/close button inside the detail frame's tool row. 1147 fn statusButton(self: *View, index: usize) ?*wgt.TextBox(ui.Widget) { 1148 const row = self.toolRow(index); 1149 if (row.children.count() == 0) return null; 1150 return &row.children.values()[button_in_row_index].widget.text_box; 1151 } 1152 1153 fn statusButtonFocused(self: *View, index: usize) bool { 1154 const outer = self.detailOuter(index); 1155 const cid = outer.getFocus().child_id orelse return false; 1156 return outer.children.getIndex(cid) == tool_row_index; 1157 } 1158 1159 fn focusTag(self: *View, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 1160 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 1161 // keep the tag visible on the terminal: its rect offset by the flow's 1162 // position in the pane. 1163 if (self.session.is_terminal and item < tf.rects.items.len) { 1164 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 1165 var rect = tf.rects.items[item]; 1166 rect.x += flow_rect.x; 1167 rect.y += flow_rect.y; 1168 self.detailScroll(index).scrollToRect(rect); 1169 } 1170 } 1171 } 1172 1173 fn titleFocused(self: *View, index: usize) bool { 1174 const inner = self.detailInner(index); 1175 const cid = inner.getFocus().child_id orelse return false; 1176 return inner.children.getIndex(cid) == title_child_index; 1177 } 1178 1179 // the title sits at the top of the pane, so focusing it scrolls there. 1180 fn focusTitle(self: *View, index: usize, root_focus: *Focus) void { 1181 const inner = self.detailInner(index); 1182 if (inner.children.count() == 0) return; 1183 root_focus.setFocus(inner.children.keys()[title_child_index]); 1184 self.detailScroll(index).y = 0; 1185 } 1186 1187 fn focusTags(self: *View, index: usize, root_focus: *Focus) !void { 1188 const tf = self.tagFlow(index) orelse return; 1189 if (tf.text_boxes.items.len == 0) return; 1190 const cid = tf.focus.child_id orelse tf.text_boxes.items[0].getFocus().id; 1191 const item = tf.indexOfFocusId(cid) orelse 0; 1192 self.focusTag(index, tf, root_focus, item); 1193 } 1194 1195 fn focusDescription(self: *View, index: usize, root_focus: *Focus) !void { 1196 if (self.description_id[index]) |id| root_focus.setFocus(id); 1197 } 1198 1199 fn focusStatusButton(self: *View, index: usize, root_focus: *Focus) void { 1200 if (self.statusButton(index)) |button| root_focus.setFocus(button.getFocus().id); 1201 } 1202 1203 // enter the detail pane. an empty pane (no issues) can't be entered. 1204 fn focusDetail(self: *View, index: usize, root_focus: *Focus) !void { 1205 if (self.detailInner(index).children.count() == 0) return; 1206 root_focus.setFocus(self.detailOuter(index).getFocus().id); 1207 } 1208 1209 // return to the list. 1210 fn focusList(self: *View, index: usize, root_focus: *Focus) !void { 1211 root_focus.setFocus(self.listScroll(index).getFocus().id); 1212 } 1213 1214 // cross to the header tabs above the stack. 1215 fn focusHeader(self: *View, root_focus: *Focus) void { 1216 root_focus.setFocus(self.box.children.keys()[header_index]); 1217 } 1218 1219 pub fn clearGrid(self: *View) void { 1220 self.box.clearGrid(); 1221 } 1222 1223 pub fn getGrid(self: View) ?Grid { 1224 return self.box.getGrid(); 1225 } 1226 1227 pub fn getFocus(self: *View) *Focus { 1228 return self.box.getFocus(); 1229 } 1230 1231 // for the parent's "scroll up at the top jumps to the header" check: at the 1232 // top only while the header tabs hold focus, so up from the split 1233 // crosses into the tabs first. 1234 pub fn getSelectedIndex(self: *View) ?usize { 1235 return if (self.headerActive()) 0 else 1; 1236 } 1237 }; 1238 1239 // the "a:" navigation link for `status`'s issues page filtered to the 1240 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 1241 // a rooted url ignores `status` (it derives its view from the issue's own); 1242 // with an empty `id` this is the bare list link. 1243 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 1244 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 1245 const url = try route.toUrl(page_arena); 1246 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1247 } 1248 1249 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 1250 // href is only followed with js off. 1251 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 1252 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 1253 const url = try route.toUrl(page_arena); 1254 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 1255 } 1256 1257 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 1258 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 1259 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 1260 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 1261 const url = try route.toUrl(page_arena); 1262 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1263 } 1264 1265 // tabs switching between the issues page's views. 1266 pub const Header = struct { 1267 box: wgt.Box(ui.Widget), 1268 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 1269 1270 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 1271 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1272 errdefer box.deinit(allocator); 1273 1274 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 1275 errdefer tab_ids.deinit(allocator); 1276 1277 const aa = session.page_arena.allocator(); 1278 1279 // a list tab per status, labeled with its listing's issue count 1280 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 1281 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 1282 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 1283 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 1284 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1285 errdefer text_box.deinit(allocator); 1286 text_box.getFocus().focusable = true; 1287 text_box.getFocus().kind = .{ .custom = link }; 1288 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1289 try box.children.put(allocator, text_box.getFocus().id, .{ 1290 .widget = .{ .text_box = text_box }, 1291 .rect = null, 1292 .min_size = .{ .width = label.len + 2, .height = null }, 1293 }); 1294 } 1295 1296 // tags tab, labeled with the active tag filter 1297 { 1298 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 1299 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 1300 const label = if (data.tag.len == 0) "tags" else blk: { 1301 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 1302 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 1303 }; 1304 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1305 errdefer text_box.deinit(allocator); 1306 text_box.getFocus().focusable = true; 1307 text_box.getFocus().kind = .{ .custom = tags_link }; 1308 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1309 try box.children.put(allocator, text_box.getFocus().id, .{ 1310 .widget = .{ .text_box = text_box }, 1311 .rect = null, 1312 .min_size = .{ .width = label.len + 2, .height = null }, 1313 }); 1314 } 1315 1316 // new-issue tab 1317 { 1318 const new_route = ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong; 1319 const new_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try new_route.toUrl(session.page_arena)}); 1320 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "new", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1321 errdefer text_box.deinit(allocator); 1322 text_box.getFocus().focusable = true; 1323 text_box.getFocus().kind = .{ .custom = new_link }; 1324 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1325 try box.children.put(allocator, text_box.getFocus().id, .{ 1326 .widget = .{ .text_box = text_box }, 1327 .rect = null, 1328 .min_size = .{ .width = "new".len + 2, .height = null }, 1329 }); 1330 } 1331 1332 var self = Header{ .box = box, .tab_ids = tab_ids }; 1333 // the tab matching the page's view is selected initially. 1334 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 1335 return self; 1336 } 1337 1338 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 1339 self.box.deinit(allocator); 1340 self.tab_ids.deinit(allocator); 1341 } 1342 1343 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1344 self.clearGrid(); 1345 // only the selected tab shows its border 1346 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 1347 switch (child.widget) { 1348 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 1349 else => {}, 1350 } 1351 } 1352 try self.box.build(allocator, constraint, root_focus); 1353 } 1354 1355 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1356 _ = allocator; 1357 const current_tab = self.currentTabIndex() orelse return; 1358 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 1359 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 1360 } 1361 } 1362 1363 pub fn clearGrid(self: *Header) void { 1364 self.box.clearGrid(); 1365 } 1366 1367 pub fn getGrid(self: Header) ?Grid { 1368 return self.box.getGrid(); 1369 } 1370 1371 pub fn getFocus(self: *Header) *Focus { 1372 return self.box.getFocus(); 1373 } 1374 1375 pub fn getSelectedIndex(self: Header) ?usize { 1376 return self.currentTabIndex(); 1377 } 1378 1379 fn currentTabIndex(self: Header) ?usize { 1380 const child_id = self.box.focus.child_id orelse return null; 1381 return self.tab_ids.getIndex(child_id); 1382 } 1383 }; 1384