╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ User.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
165 pub const View = struct { 166 box: wgt.Box(ui.Widget), 167 168 const header_index: usize = 0; 169 const stack_index: usize = 1; 170 171 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 172 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .rounded_corners = true, .direction = .vert }); 173 errdefer box.deinit(allocator); 174 175 // build the header first so we can grab the repos-tab id for the auth 176 // view (it focuses there after login). 177 { 178 var header_view = try Header.View.init(allocator, &data.header, session); 179 errdefer header_view.deinit(allocator); 180 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .user_header = header_view }, .rect = null, .min_size = null }); 181 } 182 183 { 184 var stack = try wgt.Stack(ui.Widget).init(allocator); 185 errdefer stack.deinit(allocator); 186 187 // repos list — the default tab 188 { 189 var list = try ui.widget.FlowBox.Scroll.init(allocator, .{}, !session.is_terminal); 190 errdefer list.deinit(allocator); 191 192 var arena = std.heap.ArenaAllocator.init(allocator); 193 defer arena.deinit(); 194 const aa = arena.allocator(); 195 196 // a leading "previous" row off the first window, one row per repo, 197 // then a trailing "next" row when more remain. each window row 198 // navigates to the adjacent window of this user's repos. 199 var items: std.ArrayList(ui.widget.FlowBox.Item) = .empty; 200 if (data.repos_start > 0) 201 try items.append(aa, .{ .text = "← previous", .link = try std.fmt.allocPrint(aa, "a:/user/{s}/repos/start:{d}", .{ data.user.name, data.repos_start -| page_size }) }); 202 for (data.repos) |repo| 203 // clicking a repo opens its page; the "a:" prefix makes the web 204 // renderer emit an <a href="/repo/alice/foo"> anchor. 205 try items.append(aa, .{ 206 .text = try std.fmt.allocPrint(aa, "{s} - {s}", .{ repo.event.name, repo.event.description }), 207 .link = try std.fmt.allocPrint(aa, "a:/repo/{s}/{s}", .{ data.user.name, repo.event.name }), 208 }); 209 if (data.repos_next_start) |next_start| 210 try items.append(aa, .{ .text = "next →", .link = try std.fmt.allocPrint(aa, "a:/user/{s}/repos/start:{d}", .{ data.user.name, next_start }) }); 211 try list.setItems(allocator, items.items); 212 213 try stack.children.put(allocator, list.getFocus().id, .{ .flow_box_scroll = list }); 214 } 215 216 // forks list 217 { 218 var list = try ui.widget.FlowBox.Scroll.init(allocator, .{}, !session.is_terminal); 219 errdefer list.deinit(allocator); 220 221 var item_arena = std.heap.ArenaAllocator.init(allocator); 222 defer item_arena.deinit(); 223 const aa = item_arena.allocator(); 224 var items: std.ArrayList(ui.widget.FlowBox.Item) = .empty; 225 if (data.forks_start > 0) 226 try items.append(aa, .{ .text = "← previous", .link = try std.fmt.allocPrint(aa, "a:/user/{s}/forks/start:{d}", .{ data.user.name, data.forks_start -| page_size }) }); 227 for (data.forks) |fork_item| { 228 const route = ui.RoutablePage.forkPatchRoute(fork_item.target, fork_item.id) orelse return error.RouteTooLong; 229 try items.append(aa, .{ 230 .text = try std.fmt.allocPrint(aa, "{s}\n{s}", .{ fork_item.target, fork_item.title }), 231 .link = try std.fmt.allocPrint(aa, "a:{s}", .{try route.toUrl(session.page_arena)}), 232 }); 233 } 234 if (data.forks_next_start) |next_start| 235 try items.append(aa, .{ .text = "next →", .link = try std.fmt.allocPrint(aa, "a:/user/{s}/forks/start:{d}", .{ data.user.name, next_start }) }); 236 try list.setItems(allocator, items.items); 237 try stack.children.put(allocator, list.getFocus().id, .{ .flow_box_scroll = list }); 238 } 239 240 // the header has no settings tab without a login, so keep the 241 // stack's children 1:1 with the tabs by skipping the view too 242 if (session.data.user_id != null) { 243 var settings_view = try Settings.View.init(allocator, session); 244 errdefer settings_view.deinit(allocator); 245 try stack.children.put(allocator, settings_view.getFocus().id, .{ .home_settings = settings_view }); 246 } 247 248 { 249 var auth_view = try Auth.View.init(allocator, &data.auth, session); 250 errdefer auth_view.deinit(allocator); 251 try stack.children.put(allocator, auth_view.getFocus().id, .{ .home_auth = auth_view }); 252 } 253 254 if (session.is_terminal) { 255 var quit_view = try Quit.View.init(allocator, session); 256 errdefer quit_view.deinit(allocator); 257 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 258 } 259 260 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 261 } 262 263 var self = View{ .box = box }; 264 self.getFocus().child_id = box.children.keys()[header_index]; 265 return self; 266 } 267 268 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 269 self.box.deinit(allocator); 270 } 271 272 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 273 self.clearGrid(); 274 const header = &self.box.children.values()[header_index].widget.user_header; 275 const stack = &self.box.children.values()[stack_index].widget.stack; 276 277 // each header tab maps 1:1 to a stack child by position 278 if (header.getSelectedIndex()) |index| 279 stack.getFocus().child_id = stack.children.keys()[index]; 280 try self.box.build(allocator, constraint, root_focus); 281 } 282 283 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 284 if (self.getFocus().child_id) |child_id| { 285 if (self.box.children.getIndex(child_id)) |current_index| { 286 const child = &self.box.children.values()[current_index].widget; 287 var index = current_index; 288 289 const direction = inp.vertDirection(key); 290 291 switch (direction) { 292 .up => { 293 switch (child.*) { 294 .user_header => { 295 try child.input(allocator, key, root_focus); 296 }, 297 .stack => { 298 if (child.stack.getSelected()) |selected_widget| { 299 const at_top = switch (selected_widget.*) { 300 .flow_box_scroll => |*v| v.getSelectedIndex() == 0, 301 .home_settings => |*v| v.getSelectedIndex() == 0, 302 .home_auth => |*v| v.getSelectedIndex() == 0, 303 .quit => |*v| v.getSelectedIndex() == 0, 304 else => false, 305 }; 306 if (at_top) { 307 index = header_index; 308 } else { 309 try child.input(allocator, key, root_focus); 310 } 311 } 312 }, 313 else => {}, 314 } 315 }, 316 .down => { 317 switch (child.*) { 318 .user_header => { 319 index = stack_index; 320 }, 321 .stack => { 322 try child.input(allocator, key, root_focus); 323 }, 324 else => {}, 325 } 326 }, 327 .none => { 328 try child.input(allocator, key, root_focus); 329 }, 330 } 331 332 if (index != current_index) { 333 root_focus.setFocus(self.box.children.keys()[index]); 334 } 335 } 336 } 337 } 338 339 pub fn clearGrid(self: *View) void { 340 self.box.clearGrid(); 341 } 342 343 pub fn getGrid(self: View) ?Grid { 344 return self.box.getGrid(); 345 } 346 347 pub fn getFocus(self: *View) *Focus { 348 return self.box.getFocus(); 349 } 350 }; 351