╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Fork.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
127 .fork_commits => |c| if (c.oid.len == 0) null else .object, 128 else => null, 129 }; 130 const requested_value: []const u8 = switch (route) { 131 .fork_files => |*f| f.oid.slice(), 132 .fork_commits => |*c| c.oid.slice(), 133 else => "", 134 }; 135 const files_path: []const u8 = switch (route) { 136 .fork_files => |*f| f.path.slice(), 137 else => "", 138 }; 139 const files_line: usize = switch (route) { 140 .fork_files => |f| f.line, 141 else => 0, 142 }; 143 const files_find: []const u8 = switch (route) { 144 .fork_files => |*f| f.find.slice(), 145 else => "", 146 }; 147 const commits_message = route == .fork_commits and route.fork_commits.message; 148 const files = try Files.init(.xit, repo_opts, arena, fork_repo, io, arena.child_allocator, handle, requested_ref, requested_value, files_path, files_line, files_find); 149 var commits = try Commits.init(.xit, repo_opts, arena, fork_repo, io, arena.child_allocator, haxy_moment, handle, requested_ref, requested_value, commits_message, &commits_base_oid, "", ""); 150 commits.commit_count = if (newest_revision) |revision| revision.record.commit_count else 0; 151 const diff_start: usize = switch (route) { 152 .fork_diff => |d| d.start, 153 else => 0, 154 }; 155 const diff_path = switch (route) { 156 .fork_diff => |*d| try aa.dupe(u8, d.path.slice()), 157 else => "", 158 }; 159 // a named commit's diff is against the route's base (all zeros for 160 // none), else the diff is the whole patch's 161 const diff_oid: []const u8, const diff_base_oid: []const u8 = switch (route) { 162 .fork_diff => |*d| .{ try aa.dupe(u8, d.oid.slice()), try aa.dupe(u8, d.base_oid.slice()) }, 163 else => .{ "", "" }, 164 }; 165 const diff_base: ?@TypeOf(fork_oid), const diff_head: @TypeOf(fork_oid) = if (diff_oid.len == 0) .{ commits_base_oid, fork_oid } else named: { 166 evt.PatchRev.validateOid(repo_opts.hash, diff_oid) catch return error.NotFound; 167 evt.PatchRev.validateOid(repo_opts.hash, diff_base_oid) catch return error.NotFound; 168 break :named .{ if (std.mem.allEqual(u8, diff_base_oid, '0')) null else diff_base_oid[0..fork_oid.len].*, diff_oid[0..fork_oid.len].* }; 169 }; 170 171 return .{ 172 .header = try Header.init(arena, try aa.dupe(u8, identity.name), identity.owner, &id_hex, requested_value), 173 .files = files, 174 .commits = commits, 175 .patch = patch_data, 176 .diff = .{ 177 .route = .{ .fork = .{ .identity = try aa.dupe(u8, identity.identity), .id = try aa.dupe(u8, &id_hex), .oid = diff_oid, .base_oid = diff_base_oid, .patch_base_oid = try aa.dupe(u8, &commits_base_oid) } }, 178 .path = diff_path, 179 .window = try Diff.render(.xit, repo_opts, io, arena.child_allocator, aa, fork_repo, if (diff_base) |*base| base else null, diff_head, diff_start, diff_path), 180 }, 181 .delete = if (forker) .{ .target = .{ .fork = .{ .identity = try aa.dupe(u8, identity.identity), .id = id_hex } } } else null, 182 .user_notifications = try ui.UserNotifications.init(arena, session, route.userTab()), 183 .quit = Quit.init(), 184 }; 185 }, 186 } 187 } 188 189 pub const View = struct { 190 box: wgt.Box(ui.Widget), 191 192 const header_index: usize = 0; 193 const stack_index: usize = 1; 194 195 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 196 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert }); 197 errdefer box.deinit(allocator); 198 { 199 var header = try Header.View.init(allocator, &data.header, data.commits.commit_count, data.delete != null, session); 200 errdefer header.deinit(allocator); 201 try box.children.put(allocator, header.getFocus().id, .{ .widget = .{ .fork_header = header }, .rect = null, .min_size = null }); 202 } 203 { 204 var stack = try wgt.Stack(ui.Widget).init(allocator); 205 errdefer stack.deinit(allocator); 206 const selected = data.patch.selectedThread() orelse return error.NotFound; 207 var patch_detail = try Patches.Detail.init(allocator, &data.patch, session, selected.*, .{ .actions = data.patch.repo_source != null }); 208 errdefer patch_detail.deinit(allocator); 209 try stack.children.put(allocator, patch_detail.getFocus().id, .{ .repo_patch_detail = patch_detail }); 210 { 211 var diff = try Diff.View.init(allocator, &data.diff, session); 212 errdefer diff.deinit(allocator); 213 try stack.children.put(allocator, diff.getFocus().id, .{ .diff_view = diff }); 214 } 215 { 216 var files = try Files.View.init(allocator, &data.files, session); 217 errdefer files.deinit(allocator); 218 try stack.children.put(allocator, files.getFocus().id, .{ .repo_files = files }); 219 } 220 { 221 var commits = try Commits.View.init(allocator, &data.commits, session); 222 errdefer commits.deinit(allocator); 223 try stack.children.put(allocator, commits.getFocus().id, .{ .repo_commits = commits }); 224 } 225 const identity = try session.page_arena.allocator().print("{s}:{s}", .{ data.header.forker_name, data.header.name }); 226 // right after commits, as in the header 227 if (data.delete) |delete| { 228 var view = try ui.Delete.View.init(allocator, delete, session, ui.RoutablePage.forkDeleteRoute(identity, data.header.id) orelse return error.RouteTooLong); 229 errdefer view.deinit(allocator); 230 try stack.children.put(allocator, view.getFocus().id, .{ .delete_form = view }); 231 } 232 if (session.data.user_id != null) { 233 const route = ui.RoutablePage.forkRepoNewRoute(identity, data.header.id) orelse return error.RouteTooLong; 234 var new_repo = try ui.NewRepo.View.init(allocator, session, route); 235 errdefer new_repo.deinit(allocator); 236 try stack.children.put(allocator, new_repo.getFocus().id, .{ .new_repo = new_repo }); 237 } else if (session.data.host_kind == .server) { 238 const route = ui.RoutablePage.forkUserNewRoute(identity, data.header.id) orelse return error.RouteTooLong; 239 var new_user = try ui.NewUser.View.init(allocator, session, route); 240 errdefer new_user.deinit(allocator); 241 try stack.children.put(allocator, new_user.getFocus().id, .{ .new_user = new_user }); 242 } 243 if (session.data.host_kind == .server) { 244 const route = ui.RoutablePage.forkUserRoute(identity, data.header.id) orelse return error.RouteTooLong; 245 var user_view = try ui.UserSettings.initView(allocator, session, &data.user_notifications, route); 246 errdefer user_view.deinit(allocator); 247 try stack.children.put(allocator, user_view.getFocus().id, user_view); 248 } 249 if (session.is_terminal) { 250 var quit = try Quit.View.init(allocator, session); 251 errdefer quit.deinit(allocator); 252 try stack.children.put(allocator, quit.getFocus().id, .{ .quit = quit }); 253 } 254 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 255 } 256 // a page opens on its tabs, except that search results open in the 257 // files tab's search box. 258 box.getFocus().child_id = box.children.keys()[if (data.files.find != null) stack_index else header_index]; 259 return .{ .box = box }; 260 } 261 262 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 263 self.box.deinit(allocator); 264 } 265 266 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 267 self.clearGrid(); 268 const header = &self.box.children.values()[header_index].widget.fork_header; 269 const stack = &self.box.children.values()[stack_index].widget.stack; 270 if (header.getSelectedIndex()) |index| stack.getFocus().child_id = stack.children.keys()[index]; 271 try self.box.build(allocator, constraint, root_focus); 272 } 273 274 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 275 const stack = &self.box.children.values()[stack_index].widget.stack; 276 const current_id = self.box.getFocus().child_id orelse return; 277 const current_index = self.box.children.getIndex(current_id) orelse return; 278 const child = &self.box.children.values()[current_index].widget; 279 var next_index = current_index; 280 switch (inp.vertDirection(key)) { 281 .up => switch (child.*) { 282 .fork_header => try child.input(allocator, key, root_focus), 283 .stack => if (stack.getSelected()) |selected| { 284 if (selected.atTop(root_focus)) next_index = header_index else try child.input(allocator, key, root_focus); 285 }, 286 else => {}, 287 }, 288 .down => switch (child.*) { 289 .fork_header => { 290 if (stack.getSelected()) |selected| switch (selected.*) { 291 .repo_patch_detail => |*view| if (view.focusFirst(root_focus)) return, 292 .repo_files => |*view| if (view.focusHeader(root_focus)) return, 293 .repo_commits => |*view| if (view.focusHeader(root_focus)) return, 294 .user_notifications => |*view| if (view.focusHeader(root_focus)) return, 295 else => {}, 296 }; 297 next_index = stack_index; 298 }, 299 .stack => try child.input(allocator, key, root_focus), 300 else => {}, 301 }, 302 .none => try child.input(allocator, key, root_focus), 303 } 304 if (next_index != current_index) root_focus.setFocus(self.box.children.keys()[next_index]); 305 } 306 307 pub fn clearGrid(self: *View) void { 308 self.box.clearGrid(); 309 } 310 311 pub fn getGrid(self: View) ?Grid { 312 return self.box.getGrid(); 313 } 314 315 pub fn getFocus(self: *View) *Focus { 316 return self.box.getFocus(); 317 } 318 }; 319