╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
81 .header = try Header.init(arena, repo.name, owner.name), 82 .repo = repo, 83 // the repo's event id is the on-disk repo directory name 84 .files = try Files.init(arena, session, &found.event_id, rf.identity, files_dir), 85 .commits = try Commits.init(arena, session, &found.event_id, rf.identity, commits_start, commits_after), 86 .settings = Settings.init(), 87 .auth = Auth.init(), 88 .quit = Quit.init(), 89 .route_name = route_name, 90 .commits_route_name = commits_route_name, 91 .identity = Array.from(rf.identity) orelse return error.NotFound, 92 }; 93 } 94 95 pub const View = struct { 96 box: wgt.Box(ui.Widget), 97 data: *const Self, 98 session: *ui.Session, 99 100 const header_index: usize = 0; 101 const stack_index: usize = 1; 102 103 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 104 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .rounded_corners = true, .direction = .vert }); 105 errdefer box.deinit(allocator); 106 107 // build the header first so we can grab the files-tab id for the auth 108 // view (it focuses there after login). 109 var files_tab_id: usize = undefined; 110 { 111 var header_view = try Header.View.init(allocator, &data.header, session); 112 errdefer header_view.deinit(allocator); 113 files_tab_id = header_view.tab_ids.keys()[0]; 114 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .repo_header = header_view }, .rect = null, .min_size = null }); 115 } 116 117 { 118 var stack = try wgt.Stack(ui.Widget).init(allocator); 119 errdefer stack.deinit(allocator); 120 121 // files — the default tab: the current directory's listing. 122 { 123 var files_view = try Files.View.init(allocator, &data.files, session); 124 errdefer files_view.deinit(allocator); 125 try stack.children.put(allocator, files_view.getFocus().id, .{ .repo_files = files_view }); 126 } 127 128 // commits — the current page of the commit log. 129 { 130 var commits_view = try Commits.View.init(allocator, &data.commits, session); 131 errdefer commits_view.deinit(allocator); 132 try stack.children.put(allocator, commits_view.getFocus().id, .{ .repo_commits = commits_view }); 133 } 134 135 { 136 var settings_view = try Settings.View.init(allocator, &data.settings, session); 137 errdefer settings_view.deinit(allocator); 138 try stack.children.put(allocator, settings_view.getFocus().id, .{ .home_settings = settings_view }); 139 } 140 141 { 142 var auth_view = try Auth.View.init(allocator, &data.auth, session, files_tab_id); 143 errdefer auth_view.deinit(allocator); 144 try stack.children.put(allocator, auth_view.getFocus().id, .{ .home_auth = auth_view }); 145 } 146 147 if (session.is_terminal) { 148 var quit_view = try Quit.View.init(allocator, &data.quit, session); 149 errdefer quit_view.deinit(allocator); 150 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 151 } 152 153 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 154 } 155 156 var self = View{ .box = box, .data = data, .session = session }; 157 self.getFocus().child_id = box.children.keys()[header_index]; 158 return self; 159 } 160 161 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 162 self.box.deinit(allocator); 163 } 164 165 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 166 self.clearGrid(); 167 const header = &self.box.children.values()[header_index].widget.repo_header; 168 const stack = &self.box.children.values()[stack_index].widget.stack; 169 170 // each header tab maps 1:1 to a stack child by position. mirror the 171 // selection into current_page so the host can push the matching url; 172 // all repo tabs share the .repo parent, so this stays on the page 173 // rather than navigating. 174 if (header.getSelectedIndex()) |index| { 175 stack.getFocus().child_id = stack.children.keys()[index]; 176 switch (index) { 177 // files/commits carry this page's content route (directory / log 178 // page); settings/auth carry only "owner/name". 179 1 => self.session.data.current_page = .{ .repo_commits = .{ .name = self.data.commits_route_name } }, 180 2 => self.session.data.current_page = .{ .repo_settings = self.data.identity }, 181 3 => self.session.data.current_page = .{ .repo_auth = self.data.identity }, 182 // the quit tab is tty-only and not a route, so leave current_page 183 // alone (nothing to mirror into the url). 184 4 => {}, 185 else => self.session.data.current_page = .{ .repo = self.data.route_name }, 186 } 187 } 188 try self.box.build(allocator, constraint, root_focus); 189 } 190 191 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 192 if (self.getFocus().child_id) |child_id| { 193 if (self.box.children.getIndex(child_id)) |current_index| { 194 const child = &self.box.children.values()[current_index].widget; 195 var index = current_index; 196 197 const Direction = enum { up, down, none }; 198 const direction: Direction = switch (key) { 199 .arrow_up => .up, 200 .arrow_down => .down, 201 .mouse => |mouse| if (mouse.action == .scroll) 202 (if (mouse.action.scroll == .up) .up else .down) 203 else 204 .none, 205 else => .none, 206 }; 207 208 switch (direction) { 209 .up => { 210 switch (child.*) { 211 .repo_header => { 212 try child.input(allocator, key, root_focus); 213 }, 214 .stack => { 215 if (child.stack.getSelected()) |selected_widget| { 216 const at_top = switch (selected_widget.*) { 217 .repo_files => |*v| v.getSelectedIndex() == 0, 218 .repo_commits => |*v| v.getSelectedIndex() == 0, 219 .home_settings => |*v| v.getSelectedIndex() == 0, 220 .home_auth => |*v| v.getSelectedIndex() == 0, 221 .quit => |*v| v.getSelectedIndex() == 0, 222 else => false, 223 }; 224 if (at_top) { 225 index = header_index; 226 } else { 227 try child.input(allocator, key, root_focus); 228 } 229 } 230 }, 231 else => {}, 232 } 233 }, 234 .down => { 235 switch (child.*) { 236 .repo_header => { 237 index = stack_index; 238 }, 239 .stack => { 240 try child.input(allocator, key, root_focus); 241 }, 242 else => {}, 243 } 244 }, 245 .none => { 246 try child.input(allocator, key, root_focus); 247 }, 248 } 249 250 if (index != current_index) { 251 try root_focus.setFocus(self.box.children.keys()[index]); 252 } 253 } 254 } 255 } 256 257 pub fn clearGrid(self: *View) void { 258 self.box.clearGrid(); 259 } 260 261 pub fn getGrid(self: View) ?Grid { 262 return self.box.getGrid(); 263 } 264 265 pub fn getFocus(self: *View) *Focus { 266 return self.box.getFocus(); 267 } 268 }; 269