╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("../event.zig"); 3 const ui = @import("../ui.zig"); 4 const xit = @import("xit"); 5 const xitui = xit.xitui; 6 const wgt = xitui.widget; 7 const layout = xitui.layout; 8 const inp = xitui.input; 9 const Grid = xitui.grid.Grid; 10 const Focus = xitui.focus.Focus; 11 12 pub const Header = @import("./Repo/Header.zig"); 13 pub const Files = @import("./Repo/Files.zig"); 14 pub const Commits = @import("./Repo/Commits.zig"); 15 pub const Settings = @import("./Settings.zig"); 16 pub const Auth = @import("./Auth.zig"); 17 pub const Quit = @import("./Quit.zig"); 18 19 const Array = ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len); 20 21 header: Header, 22 repo: evt.Repo, 23 files: Files, 24 commits: Commits, 25 settings: Settings, 26 auth: Auth, 27 quit: Quit, 28 // the full files route this page renders ("owner/name" or "owner/name/files/<dir>"), 29 // mirrored into current_page when the files tab is selected. 30 route_name: Array, 31 // the commits route this page renders ("owner/name/commits[/<oid>]"), mirrored 32 // when the commits tab is selected. 33 commits_route_name: Array, 34 // "owner/name" alone, mirrored when the settings/auth tab is selected. 35 identity: Array, 36 37 const Self = @This(); 38 39 pub fn init( 40 arena: *std.heap.ArenaAllocator, 41 session: *ui.Session, 42 route: ui.RoutablePage, 43 ) !Self { 44 const DB = evt.AdminDB; 45 const hash_kind = evt.admin_repo_opts.hash; 46 const haxy_moment = session.haxy_moment orelse return error.NoMoment; 47 48 // every repo route's stored string starts with "owner/name"; the files 49 // route additionally encodes a directory, the commits route a start oid. 50 const name = switch (route) { 51 .repo, .repo_commits, .repo_settings, .repo_auth => |n| n, 52 else => return error.UnexpectedRoute, 53 }; 54 const rf = ui.RoutablePage.RepoFiles.parse(name.slice()) orelse return error.NotFound; 55 const tag = std.meta.activeTag(route); 56 57 // page state lives in the route only for the tab it targets; the other tabs 58 // open at their root/first page. 59 const files_dir = if (tag == .repo) rf.dir else ""; 60 const commits_start = if (tag == .repo_commits) ui.RoutablePage.repoCommitsStart(name.slice()) else ""; 61 62 const found = (try evt.Repo.readByOwnerAndName(DB, hash_kind, haxy_moment, arena, rf.owner, rf.name)) orelse return error.NotFound; 63 const repo = found.repo; 64 65 // resolve the creating user so the header can show their name to the left 66 // of the repo title. 67 const owner = (try evt.User.readById(DB, hash_kind, haxy_moment, arena, repo.user_id)) orelse return error.NotFound; 68 69 // each tab mirror carries this page's route for that tab; tabs not targeted 70 // by the incoming route fall back to their root/first-page route. 71 const route_name = if (tag == .repo) name else (ui.RoutablePage.repoFilesRoute(rf.identity, "") orelse return error.NotFound).repo; 72 const commits_route_name = if (tag == .repo_commits) name else (ui.RoutablePage.repoCommitsRoute(rf.identity, "") orelse return error.NotFound).repo_commits; 73 74 return .{ 75 .header = try Header.init(arena, repo.name, owner.name), 76 .repo = repo, 77 // the repo's event id is the on-disk repo directory name 78 .files = try Files.init(arena, session, &found.event_id, rf.identity, files_dir), 79 .commits = try Commits.init(arena, session, &found.event_id, rf.identity, commits_start), 80 .settings = Settings.init(), 81 .auth = Auth.init(), 82 .quit = Quit.init(), 83 .route_name = route_name, 84 .commits_route_name = commits_route_name, 85 .identity = Array.from(rf.identity) orelse return error.NotFound, 86 }; 87 } 88 89 pub const View = struct { 90 box: wgt.Box(ui.Widget), 91 data: *const Self, 92 session: *ui.Session, 93 94 const header_index: usize = 0; 95 const stack_index: usize = 1; 96 97 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 98 var box = wgt.Box(ui.Widget).init(.{ .border_style = null, .rounded_corners = true, .direction = .vert }); 99 errdefer box.deinit(allocator); 100 101 // build the header first so we can grab the files-tab id for the auth 102 // view (it focuses there after login). 103 var files_tab_id: usize = undefined; 104 { 105 var header_view = try Header.View.init(allocator, &data.header, session); 106 errdefer header_view.deinit(allocator); 107 files_tab_id = header_view.tab_ids.keys()[0]; 108 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .repo_header = header_view }, .rect = null, .min_size = null }); 109 } 110 111 { 112 var stack = wgt.Stack(ui.Widget).init(); 113 errdefer stack.deinit(allocator); 114 115 // files — the default tab: the current directory's listing. 116 { 117 var files_view = try Files.View.init(allocator, &data.files, session); 118 errdefer files_view.deinit(allocator); 119 try stack.children.put(allocator, files_view.getFocus().id, .{ .repo_files = files_view }); 120 } 121 122 // commits — the current page of the commit log. 123 { 124 var commits_view = try Commits.View.init(allocator, &data.commits, session); 125 errdefer commits_view.deinit(allocator); 126 try stack.children.put(allocator, commits_view.getFocus().id, .{ .repo_commits = commits_view }); 127 } 128 129 { 130 var settings_view = try Settings.View.init(allocator, &data.settings, session); 131 errdefer settings_view.deinit(allocator); 132 try stack.children.put(allocator, settings_view.getFocus().id, .{ .home_settings = settings_view }); 133 } 134 135 { 136 var auth_view = try Auth.View.init(allocator, &data.auth, session, files_tab_id); 137 errdefer auth_view.deinit(allocator); 138 try stack.children.put(allocator, auth_view.getFocus().id, .{ .home_auth = auth_view }); 139 } 140 141 if (session.is_terminal) { 142 var quit_view = try Quit.View.init(allocator, &data.quit, session); 143 errdefer quit_view.deinit(allocator); 144 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 145 } 146 147 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 148 } 149 150 var self = View{ .box = box, .data = data, .session = session }; 151 self.getFocus().child_id = box.children.keys()[header_index]; 152 return self; 153 } 154 155 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 156 self.box.deinit(allocator); 157 } 158 159 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 160 self.clearGrid(); 161 const header = &self.box.children.values()[header_index].widget.repo_header; 162 const stack = &self.box.children.values()[stack_index].widget.stack; 163 164 // each header tab maps 1:1 to a stack child by position. mirror the 165 // selection into current_page so the host can push the matching url; 166 // all repo tabs share the .repo parent, so this stays on the page 167 // rather than navigating. 168 if (header.getSelectedIndex()) |index| { 169 stack.getFocus().child_id = stack.children.keys()[index]; 170 switch (index) { 171 // files/commits carry this page's content route (directory / log 172 // page); settings/auth carry only "owner/name". 173 1 => self.session.data.current_page = .{ .repo_commits = self.data.commits_route_name }, 174 2 => self.session.data.current_page = .{ .repo_settings = self.data.identity }, 175 3 => self.session.data.current_page = .{ .repo_auth = self.data.identity }, 176 // the quit tab is tty-only and not a route, so leave current_page 177 // alone (nothing to mirror into the url). 178 4 => {}, 179 else => self.session.data.current_page = .{ .repo = self.data.route_name }, 180 } 181 } 182 try self.box.build(allocator, constraint, root_focus); 183 } 184 185 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 186 if (self.getFocus().child_id) |child_id| { 187 if (self.box.children.getIndex(child_id)) |current_index| { 188 const child = &self.box.children.values()[current_index].widget; 189 var index = current_index; 190 191 const Direction = enum { up, down, none }; 192 const direction: Direction = switch (key) { 193 .arrow_up => .up, 194 .arrow_down => .down, 195 .mouse => |mouse| if (mouse.action == .scroll) 196 (if (mouse.action.scroll == .up) .up else .down) 197 else 198 .none, 199 else => .none, 200 }; 201 202 switch (direction) { 203 .up => { 204 switch (child.*) { 205 .repo_header => { 206 try child.input(allocator, key, root_focus); 207 }, 208 .stack => { 209 if (child.stack.getSelected()) |selected_widget| { 210 const at_top = switch (selected_widget.*) { 211 .repo_files => |*v| v.getSelectedIndex() == 0, 212 .repo_commits => |*v| v.getSelectedIndex() == 0, 213 .home_settings => |*v| v.getSelectedIndex() == 0, 214 .home_auth => |*v| v.getSelectedIndex() == 0, 215 .quit => |*v| v.getSelectedIndex() == 0, 216 else => false, 217 }; 218 if (at_top) { 219 index = header_index; 220 } else { 221 try child.input(allocator, key, root_focus); 222 } 223 } 224 }, 225 else => {}, 226 } 227 }, 228 .down => { 229 switch (child.*) { 230 .repo_header => { 231 index = stack_index; 232 }, 233 .stack => { 234 try child.input(allocator, key, root_focus); 235 }, 236 else => {}, 237 } 238 }, 239 .none => { 240 try child.input(allocator, key, root_focus); 241 }, 242 } 243 244 if (index != current_index) { 245 try root_focus.setFocus(self.box.children.keys()[index]); 246 } 247 } 248 } 249 } 250 251 pub fn clearGrid(self: *View) void { 252 self.box.clearGrid(); 253 } 254 255 pub fn getGrid(self: View) ?Grid { 256 return self.box.getGrid(); 257 } 258 259 pub fn getFocus(self: *View) *Focus { 260 return self.box.getFocus(); 261 } 262 }; 263