╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ User.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
177 .quit = Quit.init(), 178 }; 179 } 180 181 pub const View = struct { 182 box: wgt.Box(ui.Widget), 183 184 const header_index: usize = 0; 185 const stack_index: usize = 1; 186 187 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 188 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .round_corners = true, .direction = .vert }); 189 errdefer box.deinit(allocator); 190 191 // build the header first so we can grab the repos-tab id for the user 192 // view (it focuses there after login). 193 { 194 var header_view = try Header.View.init(allocator, &data.header, session); 195 errdefer header_view.deinit(allocator); 196 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .user_header = header_view }, .rect = null, .min_size = null }); 197 } 198 199 { 200 var stack = try wgt.Stack(ui.Widget).init(allocator); 201 errdefer stack.deinit(allocator); 202 203 // repos list — the default tab 204 { 205 var repos_view = try ReposView.init(allocator, data, session); 206 errdefer repos_view.deinit(allocator); 207 try stack.children.put(allocator, repos_view.getFocus().id, .{ .user_repos = repos_view }); 208 } 209 210 // forks list 211 { 212 var list = try ui.widget.FlowBox.Scroll.init(allocator, .{}, !session.is_terminal); 213 errdefer list.deinit(allocator); 214 215 var item_arena = std.heap.ArenaAllocator.init(allocator); 216 defer item_arena.deinit(); 217 const aa = item_arena.allocator(); 218 var items: std.ArrayList(ui.widget.FlowBox.Item) = .empty; 219 if (data.forks_start > 0) 220 try items.append(aa, .{ .text = "← previous", .link = try aa.print("a:/{s}/forks/start:{d}", .{ data.user.name, data.forks_start -| page_size }) }); 221 for (data.forks) |fork_item| { 222 // the fork route names the forker 223 const identity = try aa.print("{s}:{s}", .{ data.user.name, fork_item.target.name }); 224 const route = ui.RoutablePage.forkPatchRoute(identity, fork_item.id) orelse return error.RouteTooLong; 225 try items.append(aa, .{ 226 .text = try aa.print("{s} - {s}", .{ fork_item.target.name, fork_item.title }), 227 .link = try aa.print("a:{s}", .{try route.toUrl(session.page_arena)}), 228 .bottom_label = if (fork_item.target.read_access == .private) "(private)" else "", 229 }); 230 } 231 if (data.forks_next_start) |next_start| 232 try items.append(aa, .{ .text = "next →", .link = try aa.print("a:/{s}/forks/start:{d}", .{ data.user.name, next_start }) }); 233 try list.setItems(allocator, items.items); 234 try stack.children.put(allocator, list.getFocus().id, .{ .flow_box_scroll = list }); 235 } 236 237 // the header shows new repo with a login and new user without 238 // one, so keep the stack's children 1:1 with the tabs 239 if (session.data.user_id != null) { 240 const route = ui.RoutablePage{ .user_repo_new = ui.RoutablePage.Array(evt.User.name_max_len).from(data.user.name) orelse return error.RouteTooLong }; 241 var new_repo_view = try ui.NewRepo.View.init(allocator, session, route); 242 errdefer new_repo_view.deinit(allocator); 243 try stack.children.put(allocator, new_repo_view.getFocus().id, .{ .new_repo = new_repo_view }); 244 } else { 245 const route = ui.RoutablePage{ .user_user_new = ui.RoutablePage.Array(evt.User.name_max_len).from(data.user.name) orelse return error.RouteTooLong }; 246 var new_user_view = try ui.NewUser.View.init(allocator, session, route); 247 errdefer new_user_view.deinit(allocator); 248 try stack.children.put(allocator, new_user_view.getFocus().id, .{ .new_user = new_user_view }); 249 } 250 251 { 252 const route = ui.RoutablePage{ .user_user = .{ .name = ui.RoutablePage.Array(evt.User.name_max_len).from(data.user.name) orelse return error.RouteTooLong } }; 253 var user_view = try ui.UserSettings.initView(allocator, session, &data.user_notifications, route); 254 errdefer user_view.deinit(allocator); 255 try stack.children.put(allocator, user_view.getFocus().id, user_view); 256 } 257 258 if (session.is_terminal) { 259 var quit_view = try Quit.View.init(allocator, session); 260 errdefer quit_view.deinit(allocator); 261 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 262 } 263 264 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 265 } 266 267 var self = View{ .box = box }; 268 // search results open in the tab's own search box 269 self.getFocus().child_id = box.children.keys()[if (data.repos_search != null) stack_index else header_index]; 270 return self; 271 } 272 273 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 274 self.box.deinit(allocator); 275 } 276 277 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 278 self.clearGrid(); 279 const header = &self.box.children.values()[header_index].widget.user_header; 280 const stack = &self.box.children.values()[stack_index].widget.stack; 281 282 // each header tab maps 1:1 to a stack child by position 283 if (header.getSelectedIndex()) |index| 284 stack.getFocus().child_id = stack.children.keys()[index]; 285 try self.box.build(allocator, constraint, root_focus); 286 } 287 288 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 289 const stack = &self.box.children.values()[stack_index].widget.stack; 290 if (self.getFocus().child_id) |child_id| { 291 if (self.box.children.getIndex(child_id)) |current_index| { 292 const child = &self.box.children.values()[current_index].widget; 293 var index = current_index; 294 295 const direction = inp.vertDirection(key); 296 297 switch (direction) { 298 .up => { 299 switch (child.*) { 300 .user_header => { 301 try child.input(allocator, key, root_focus); 302 }, 303 .stack => { 304 if (child.stack.getSelected()) |selected_widget| { 305 if (selected_widget.atTop(root_focus)) { 306 index = header_index; 307 } else { 308 try child.input(allocator, key, root_focus); 309 } 310 } 311 }, 312 else => {}, 313 } 314 }, 315 .down => { 316 switch (child.*) { 317 .user_header => { 318 if (stack.getSelected()) |selected_widget| switch (selected_widget.*) { 319 .user_repos => |*v| return v.focusHeader(root_focus), 320 // an empty list has nothing to focus 321 .flow_box_scroll => |*v| if (v.isEmpty()) return, 322 .user_notifications => |*v| if (v.focusHeader(root_focus)) return, 323 else => {}, 324 }; 325 index = stack_index; 326 }, 327 .stack => { 328 try child.input(allocator, key, root_focus); 329 }, 330 else => {}, 331 } 332 }, 333 .none => { 334 try child.input(allocator, key, root_focus); 335 }, 336 } 337 338 if (index != current_index) { 339 root_focus.setFocus(self.box.children.keys()[index]); 340 } 341 } 342 } 343 } 344 345 pub fn clearGrid(self: *View) void { 346 self.box.clearGrid(); 347 } 348 349 pub fn getGrid(self: View) ?Grid { 350 return self.box.getGrid(); 351 } 352 353 pub fn getFocus(self: *View) *Focus { 354 return self.box.getFocus(); 355 } 356 }; 357 358 pub const ReposView = struct { 359 // a vertical box: the search sub-header above the scrolling list. focus 360 // points at the header's box or the list's selected row. 361 box: wgt.Box(ui.Widget), 362 data: *const Self, 363 session: *ui.Session, 364 365 const header_index = 0; 366 const list_index = 1; 367 368 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !ReposView { 369 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert }); 370 errdefer box.deinit(allocator); 371 372 { 373 var header = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 374 errdefer header.deinit(allocator); 375 { 376 var search_box = try ui.widget.SearchBox.init(allocator, session, " search ", "search", data.repos_search); 377 errdefer search_box.deinit(allocator); 378 header.getFocus().child_id = search_box.getFocus().id; 379 try header.children.put(allocator, search_box.getFocus().id, .{ .widget = .{ .search_box = search_box }, .rect = null, .min_size = ui.widget.SearchBox.min_size }); 380 } 381 // the spacer keeps the box at its own width 382 { 383 var spacer = try ui.widget.Spacer.init(allocator); 384 errdefer spacer.deinit(allocator); 385 try header.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 386 } 387 try box.children.put(allocator, header.getFocus().id, .{ .widget = .{ .box = header }, .rect = null, .min_size = .{ .width = null, .height = ui.widget.SearchBox.min_size.height } }); 388 } 389 390 { 391 var list = try ui.widget.FlowBox.Scroll.init(allocator, .{}, !session.is_terminal); 392 errdefer list.deinit(allocator); 393 394 var arena = std.heap.ArenaAllocator.init(allocator); 395 defer arena.deinit(); 396 const aa = arena.allocator(); 397 398 const route = (reposRoute(data.user.name) orelse return error.RouteTooLong).withSearch(data.repos_search orelse "") orelse return error.RouteTooLong; 399 400 // a leading "previous" row off the first window, one row per repo, 401 // then a trailing "next" row when more remain. each window row 402 // navigates to the adjacent window of this user's repos. 403 var items: std.ArrayList(ui.widget.FlowBox.Item) = .empty; 404 if (data.repos_start > 0) { 405 var prev = route; 406 prev.user_repos.start = data.repos_start -| page_size; 407 try items.append(aa, .{ .text = "← previous", .link = try aa.print("a:{s}", .{try prev.toUrl(&arena)}) }); 408 } 409 for (data.repos) |repo| 410 // clicking a repo opens its page; the "a:" prefix makes the web 411 // renderer emit an <a href="/alice:foo"> anchor. 412 try items.append(aa, .{ 413 .text = if (repo.event.description.len == 0) repo.event.name else try aa.print("{s} - {s}", .{ repo.event.name, repo.event.description }), 414 .link = try aa.print("a:/{s}:{s}", .{ data.user.name, repo.event.name }), 415 .bottom_label = if (repo.event.read_access == .private) "(private)" else "", 416 }); 417 if (data.repos_next_start) |next_start| { 418 var next = route; 419 next.user_repos.start = next_start; 420 try items.append(aa, .{ .text = "next →", .link = try aa.print("a:{s}", .{try next.toUrl(&arena)}) }); 421 } 422 try list.setItems(allocator, items.items); 423 424 try box.children.put(allocator, list.getFocus().id, .{ .widget = .{ .flow_box_scroll = list }, .rect = null, .min_size = null }); 425 } 426 427 // search results start in the box so the term can be refined right away 428 box.getFocus().child_id = box.children.keys()[if (data.repos_search != null) header_index else list_index]; 429 return .{ .box = box, .data = data, .session = session }; 430 } 431 432 pub fn deinit(self: *ReposView, allocator: std.mem.Allocator) void { 433 self.box.deinit(allocator); 434 } 435 436 pub fn build(self: *ReposView, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 437 self.clearGrid(); 438 // clear the incoming min height so the list scrolls within what's left 439 try self.box.build(allocator, .{ 440 .min_size = .{ .width = null, .height = null }, 441 .max_size = constraint.max_size, 442 }, root_focus); 443 } 444 445 pub fn input(self: *ReposView, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 446 const direction = inp.vertDirection(key); 447 if (self.headerActive()) { 448 const search_box = self.searchBox(); 449 if (direction == .down) return root_focus.setFocus(self.listScroll().getFocus().id); 450 if (key == .enter) { 451 const text = try search_box.text(allocator); 452 defer allocator.free(text); 453 return self.submit(text); 454 } 455 return search_box.input(allocator, key, root_focus); 456 } 457 // up from the first row reaches the search box 458 if (direction == .up and self.listScroll().atTop()) return self.focusHeader(root_focus); 459 try self.listScroll().input(allocator, key, root_focus); 460 } 461 462 // the repos narrowed to `text` from the first window. an empty box on an 463 // unsearched page has nothing to clear. 464 fn submit(self: *ReposView, text: []const u8) !void { 465 if (text.len == 0 and self.data.repos_search == null) return; 466 const route = reposRoute(self.data.user.name) orelse return; 467 try self.session.navigate(route.withSearch(text) orelse return); 468 } 469 470 fn searchBox(self: *ReposView) *ui.widget.SearchBox { 471 return &self.box.children.values()[header_index].widget.box.children.values()[0].widget.search_box; 472 } 473 474 fn listScroll(self: *ReposView) *ui.widget.FlowBox.Scroll { 475 return &self.box.children.values()[list_index].widget.flow_box_scroll; 476 } 477 478 fn headerActive(self: *ReposView) bool { 479 return self.box.getFocus().child_id == self.box.children.keys()[header_index]; 480 } 481 482 pub fn clearGrid(self: *ReposView) void { 483 self.box.clearGrid(); 484 } 485 486 pub fn getGrid(self: ReposView) ?Grid { 487 return self.box.getGrid(); 488 } 489 490 pub fn getFocus(self: *ReposView) *Focus { 491 return self.box.getFocus(); 492 } 493 494 // up leaves the tab from the search box 495 pub fn atTop(self: *ReposView) bool { 496 return self.headerActive(); 497 } 498 499 pub fn focusHeader(self: *ReposView, root_focus: *Focus) void { 500 root_focus.setFocus(self.searchBox().getFocus().id); 501 } 502 }; 503 504 // the first window of `name`'s repos 505 fn reposRoute(name: []const u8) ?ui.RoutablePage { 506 return .{ .user_repos = .{ .name = ui.RoutablePage.Array(evt.User.name_max_len).from(name) orelse return null } }; 507 } 508