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