╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ User.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
49 repos_search: []const u8, 50 forks_start: usize, 51 user_tab: ui.RoutablePage.UserTab, 52 ) !Self { 53 // admin and user repos share one db type 54 const DB = evt.AdminDB; 55 const hash_kind = evt.admin_repo_opts.hash; 56 57 // a route identifies a user by name, which everything below keys off of 58 const user_id = (try evt.User.readIdByName(DB, hash_kind, haxy_moment, name.slice())) orelse return error.NotFound; 59 const user = (try evt.User.readById(DB, hash_kind, haxy_moment, arena, &user_id)) orelse return error.NotFound; 60 61 const io = session.io orelse return error.NoMoment; 62 const users_dir = session.users_dir orelse return error.NoMoment; 63 const gpa = arena.child_allocator; 64 65 // a user with no user repo or no events has empty lists 66 var user_repo_maybe = try evt.openUserRepo(io, gpa, users_dir, &user_id); 67 defer if (user_repo_maybe) |*user_repo| user_repo.deinit(io, gpa); 68 const user_moment = if (user_repo_maybe) |*user_repo| try evt.userMoment(user_repo) else null; 69 70 const search: ?[]const u8 = if (repos_search.len == 0) null else std.Uri.percentDecodeInPlace(try arena.allocator().dupe(u8, repos_search)); 71 72 var repos: std.ArrayList(evt.Repo.Record) = .empty; 73 var repos_next_start: ?usize = null; 74 if (user_moment) |moment| { 75 if (search) |prefix| { 76 if (try moment.getCursor(hash.hashInt(hash_kind, evt.Repo.name_index_key))) |index_cursor| { 77 const index = try DB.SortedMap(.read_only).init(index_cursor); 78 79 // the names with the prefix are contiguous from its rank, so the 80 // window is one seek then a walk that stops at the first name without it 81 var taken: usize = 0; 82 var repos_iter = try index.iteratorFromIndex(try index.rank(prefix) +| repos_start); 83 while (try repos_iter.next()) |cursor| { 84 const pair = try cursor.readKeyValuePair(); 85 const repo_name = try pair.key_cursor.readBytesAlloc(arena.allocator(), null); 86 if (!std.mem.startsWith(u8, repo_name, prefix)) break; 87 if (taken == page_size) { 88 repos_next_start = repos_start + page_size; 89 break; 90 } 91 taken += 1; 92 var event_id: [evt.event_id_size]u8 = undefined; 93 _ = try pair.value_cursor.readBytes(&event_id); 94 const repo_event = (try evt.Repo.readById(DB, hash_kind, moment, arena, &event_id)) orelse continue; 95 // unreadable repos leave their window short rather than shifting the others 96 if (try evt.Repo.roleOf(DB, hash_kind, moment, arena, repo_event, &event_id, session.userId()) == null) continue; 97 try repos.append(arena.allocator(), repo_event); 98 } 99 } 100 } else { 101 // unsearched, the newest activity comes first. its index sits beside the events. 102 const user_repo = if (user_repo_maybe) |*user_repo| user_repo else unreachable; 103 if (try (try user_repo.core.latestMoment()).getCursor(hash.hashInt(hash_kind, evt.recent_repo_id_set_key))) |recent_cursor| { 104 const recent = try DB.SortedSet(.read_only).init(recent_cursor); 105 const count = try recent.count(); 106 const end = @min(repos_start +| page_size, count); 107 var iter = try recent.iteratorFromIndex(repos_start); 108 var i = repos_start; 109 while (i < end) : (i += 1) { 110 const kv_cursor = (try iter.next()) orelse break; 111 const repo_id = try evt.readOrderKeyId(DB, kv_cursor); 112 const repo_event = (try evt.Repo.readById(DB, hash_kind, moment, arena, &repo_id)) orelse continue; 113 if (try evt.Repo.roleOf(DB, hash_kind, moment, arena, repo_event, &repo_id, session.userId()) == null) continue; 114 try repos.append(arena.allocator(), repo_event); 115 } 116 repos_next_start = if (end < count) end else null; 117 } 118 } 119 } 120 121 var forks: std.ArrayList(ForkItem) = .empty; 122 var forks_next_start: ?usize = null; 123 if (user_moment) |moment| { 124 if (try moment.getCursor(hash.hashInt(hash_kind, evt.Fork.active_id_set_key))) |user_forks_cursor| { 125 const user_forks = try DB.SortedSet(.read_only).init(user_forks_cursor); 126 const count = try user_forks.count(); 127 const end = @min(forks_start +| page_size, count); 128 var iter = try user_forks.iteratorFromIndex(forks_start); 129 var i = forks_start; 130 while (i < end) : (i += 1) { 131 const kv_cursor = (try iter.next()) orelse break; 132 const fork_id = try evt.readOrderKeyId(DB, kv_cursor); 133 const fork_id_hex = std.fmt.bytesToHex(fork_id, .lower); 134 const record = (try evt.Fork.readById(DB, hash_kind, moment, arena, &fork_id)) orelse continue; 135 136 // the target lives in its owner's user repo 137 const owner_id = try evt.parseEventId(&record.event.repo_user_id); 138 const target = (try evt.readRepoById(io, gpa, arena, users_dir, &owner_id, &try evt.parseEventId(&record.event.repo_id), session.userId())) orelse continue; 139 if (target.role == null) continue; 140 // patches that are off take their drafts with them 141 if (target.repo.event.patch_role == null) continue; 142 const target_repo = target.repo; 143 144 var title: []const u8 = "(unavailable)"; 145 const path = try fork.forkPath(arena.allocator(), users_dir, &user_id, &fork_id); 146 if (rp.AnyRepo(.xit, .{}).open(io, gpa, .{ .path = path, .require_repo_root = true })) |opened| { 147 var any_fork = opened; 148 defer any_fork.deinit(io, gpa); 149 switch (any_fork) { 150 inline else => |*fork_repo| if (evt.currentMoment(fork_repo.self_repo_opts, fork_repo)) |fork_moment| { 151 const kind = fork_repo.self_repo_opts.hash; 152 if (try evt.Patch.readById(evt.EventDB(kind), kind, fork_moment, arena, &fork_id)) |patch| title = patch.event.title; 153 } else |_| {}, 154 } 155 } else |_| {} 156 try forks.append(arena.allocator(), .{ 157 .id = try arena.allocator().dupe(u8, &fork_id_hex), 158 .target = target_repo.event, 159 .title = title, 160 }); 161 } 162 forks_next_start = if (end < count) end else null; 163 } 164 } 165 166 return .{ 167 .header = try Header.init(arena, user.event.name), 168 .user = evt.project(evt.User.Public, user.event), 169 .repos = repos.items, 170 .repos_start = repos_start, 171 .repos_next_start = repos_next_start, 172 .repos_search = search, 173 .forks = forks.items, 174 .forks_start = forks_start, 175 .forks_next_start = forks_next_start, 176 .user_notifications = try ui.UserNotifications.init(arena, session, user_tab), 177 .quit = Quit.init(), 178 }; 179 } 180 181 // a first repos window without repos, which shows the empty message 182 fn reposEmpty(self: *const Self) bool { 183 return self.repos.len == 0 and self.repos_start == 0; 184 } 185 186 // a first forks window without forks, which shows the empty message 187 fn forksEmpty(self: *const Self) bool { 188 return self.forks.len == 0 and self.forks_start == 0; 189 } 190 191 pub const View = struct { 192 box: wgt.Box(ui.Widget), 193 data: *const Self, 194 195 const header_index: usize = 0; 196 const stack_index: usize = 1; 197 // the forks tab's position in the stack 198 const forks_index: usize = 1; 199 200 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 201 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .round_corners = true, .direction = .vert }); 202 errdefer box.deinit(allocator); 203 204 // build the header first so we can grab the repos-tab id for the user 205 // view (it focuses there after login). 206 { 207 var header_view = try Header.View.init(allocator, &data.header, session); 208 errdefer header_view.deinit(allocator); 209 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .user_header = header_view }, .rect = null, .min_size = null }); 210 } 211 212 { 213 var stack = try wgt.Stack(ui.Widget).init(allocator); 214 errdefer stack.deinit(allocator); 215 216 // repos list — the default tab 217 { 218 var repos_view = try ReposView.init(allocator, data, session); 219 errdefer repos_view.deinit(allocator); 220 try stack.children.put(allocator, repos_view.getFocus().id, .{ .user_repos = repos_view }); 221 } 222 223 // forks list, or the empty message for an empty first window 224 if (data.forksEmpty()) { 225 var center = try ui.widget.initEmpty(allocator); 226 errdefer center.deinit(allocator); 227 try stack.children.put(allocator, center.getFocus().id, .{ .center = center }); 228 } else { 229 var list = try ui.widget.FlowBox.Scroll.init(allocator, .{}, !session.is_terminal); 230 errdefer list.deinit(allocator); 231 232 var item_arena = std.heap.ArenaAllocator.init(allocator); 233 defer item_arena.deinit(); 234 const aa = item_arena.allocator(); 235 var items: std.ArrayList(ui.widget.FlowBox.Item) = .empty; 236 if (data.forks_start > 0) 237 try items.append(aa, .{ .text = "← previous", .link = try aa.print("a:/{s}/forks/start:{d}", .{ data.user.name, data.forks_start -| page_size }) }); 238 for (data.forks) |fork_item| { 239 // the fork route names the forker 240 const identity = try aa.print("{s}:{s}", .{ data.user.name, fork_item.target.name }); 241 const route = ui.RoutablePage.forkPatchRoute(identity, fork_item.id) orelse return error.RouteTooLong; 242 try items.append(aa, .{ 243 .text = try aa.print("{s} - {s}", .{ fork_item.target.name, fork_item.title }), 244 .link = try aa.print("a:{s}", .{try route.toUrl(session.page_arena)}), 245 .bottom_label = if (fork_item.target.read_access == .private) "(private)" else "", 246 }); 247 } 248 if (data.forks_next_start) |next_start| 249 try items.append(aa, .{ .text = "next →", .link = try aa.print("a:/{s}/forks/start:{d}", .{ data.user.name, next_start }) }); 250 try list.setItems(allocator, items.items); 251 try stack.children.put(allocator, list.getFocus().id, .{ .flow_box_scroll = list }); 252 } 253 254 // the header shows new repo with a login and new user without 255 // one, so keep the stack's children 1:1 with the tabs 256 if (session.data.user_id != null) { 257 const route = ui.RoutablePage{ .user_repo_new = ui.RoutablePage.Array(evt.User.name_max_len).from(data.user.name) orelse return error.RouteTooLong }; 258 var new_repo_view = try ui.NewRepo.View.init(allocator, session, route); 259 errdefer new_repo_view.deinit(allocator); 260 try stack.children.put(allocator, new_repo_view.getFocus().id, .{ .new_repo = new_repo_view }); 261 } else { 262 const route = ui.RoutablePage{ .user_user_new = ui.RoutablePage.Array(evt.User.name_max_len).from(data.user.name) orelse return error.RouteTooLong }; 263 var new_user_view = try ui.NewUser.View.init(allocator, session, route); 264 errdefer new_user_view.deinit(allocator); 265 try stack.children.put(allocator, new_user_view.getFocus().id, .{ .new_user = new_user_view }); 266 } 267 268 { 269 const route = ui.RoutablePage{ .user_user = .{ .name = ui.RoutablePage.Array(evt.User.name_max_len).from(data.user.name) orelse return error.RouteTooLong } }; 270 var user_view = try ui.UserSettings.initView(allocator, session, &data.user_notifications, route); 271 errdefer user_view.deinit(allocator); 272 try stack.children.put(allocator, user_view.getFocus().id, user_view); 273 } 274 275 if (session.is_terminal) { 276 var quit_view = try Quit.View.init(allocator, session); 277 errdefer quit_view.deinit(allocator); 278 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 279 } 280 281 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 282 } 283 284 var self = View{ .box = box, .data = data }; 285 // search results open in the tab's own search box 286 self.getFocus().child_id = box.children.keys()[if (data.repos_search != null) stack_index else header_index]; 287 return self; 288 } 289 290 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 291 self.box.deinit(allocator); 292 } 293 294 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 295 self.clearGrid(); 296 const header = &self.box.children.values()[header_index].widget.user_header; 297 const stack = &self.box.children.values()[stack_index].widget.stack; 298 299 // each header tab maps 1:1 to a stack child by position 300 if (header.getSelectedIndex()) |index| 301 stack.getFocus().child_id = stack.children.keys()[index]; 302 try self.box.build(allocator, constraint, root_focus); 303 } 304 305 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 306 const stack = &self.box.children.values()[stack_index].widget.stack; 307 if (self.getFocus().child_id) |child_id| { 308 if (self.box.children.getIndex(child_id)) |current_index| { 309 const child = &self.box.children.values()[current_index].widget; 310 var index = current_index; 311 312 const direction = inp.vertDirection(key); 313 314 switch (direction) { 315 .up => { 316 switch (child.*) { 317 .user_header => { 318 try child.input(allocator, key, root_focus); 319 }, 320 .stack => { 321 if (child.stack.getSelected()) |selected_widget| { 322 if (selected_widget.atTop(root_focus)) { 323 index = header_index; 324 } else { 325 try child.input(allocator, key, root_focus); 326 } 327 } 328 }, 329 else => {}, 330 } 331 }, 332 .down => { 333 switch (child.*) { 334 .user_header => { 335 // the empty message has nothing to focus 336 if (self.data.forksEmpty() and stack.getFocus().child_id == stack.children.keys()[forks_index]) return; 337 if (stack.getSelected()) |selected_widget| switch (selected_widget.*) { 338 .user_repos => |*v| return v.focusHeader(root_focus), 339 .user_notifications => |*v| if (v.focusHeader(root_focus)) return, 340 else => {}, 341 }; 342 index = stack_index; 343 }, 344 .stack => { 345 try child.input(allocator, key, root_focus); 346 }, 347 else => {}, 348 } 349 }, 350 .none => { 351 try child.input(allocator, key, root_focus); 352 }, 353 } 354 355 if (index != current_index) { 356 root_focus.setFocus(self.box.children.keys()[index]); 357 } 358 } 359 } 360 } 361 362 pub fn clearGrid(self: *View) void { 363 self.box.clearGrid(); 364 } 365 366 pub fn getGrid(self: View) ?Grid { 367 return self.box.getGrid(); 368 } 369 370 pub fn getFocus(self: *View) *Focus { 371 return self.box.getFocus(); 372 } 373 }; 374 375 pub const ReposView = struct { 376 // a vertical box: the search sub-header above the scrolling list. focus 377 // points at the header's box or the list's selected row. 378 box: wgt.Box(ui.Widget), 379 data: *const Self, 380 session: *ui.Session, 381 382 const header_index = 0; 383 const list_index = 1; 384 385 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !ReposView { 386 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert }); 387 errdefer box.deinit(allocator); 388 389 { 390 var header = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 391 errdefer header.deinit(allocator); 392 { 393 var search_box = try ui.widget.SearchBox.init(allocator, session, " search ", "search", data.repos_search); 394 errdefer search_box.deinit(allocator); 395 header.getFocus().child_id = search_box.getFocus().id; 396 try header.children.put(allocator, search_box.getFocus().id, .{ .widget = .{ .search_box = search_box }, .rect = null, .min_size = ui.widget.SearchBox.min_size }); 397 } 398 // the spacer keeps the box at its own width 399 { 400 var spacer = try ui.widget.Spacer.init(allocator); 401 errdefer spacer.deinit(allocator); 402 try header.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 403 } 404 try box.children.put(allocator, header.getFocus().id, .{ .widget = .{ .box = header }, .rect = null, .min_size = .{ .width = null, .height = ui.widget.SearchBox.min_size.height } }); 405 } 406 407 // an empty first window has no rows, not even paging ones 408 if (data.reposEmpty()) { 409 var center = try ui.widget.initEmpty(allocator); 410 errdefer center.deinit(allocator); 411 try box.children.put(allocator, center.getFocus().id, .{ .widget = .{ .center = center }, .rect = null, .min_size = null }); 412 } else { 413 var list = try ui.widget.FlowBox.Scroll.init(allocator, .{}, !session.is_terminal); 414 errdefer list.deinit(allocator); 415 416 var arena = std.heap.ArenaAllocator.init(allocator); 417 defer arena.deinit(); 418 const aa = arena.allocator(); 419 420 const route = (reposRoute(data.user.name) orelse return error.RouteTooLong).withSearch(data.repos_search orelse "") orelse return error.RouteTooLong; 421 422 // a leading "previous" row off the first window, one row per repo, 423 // then a trailing "next" row when more remain. each window row 424 // navigates to the adjacent window of this user's repos. 425 var items: std.ArrayList(ui.widget.FlowBox.Item) = .empty; 426 if (data.repos_start > 0) { 427 var prev = route; 428 prev.user_repos.start = data.repos_start -| page_size; 429 try items.append(aa, .{ .text = "← previous", .link = try aa.print("a:{s}", .{try prev.toUrl(&arena)}) }); 430 } 431 for (data.repos) |repo| 432 // clicking a repo opens its page; the "a:" prefix makes the web 433 // renderer emit an <a href="/alice:foo"> anchor. 434 try items.append(aa, .{ 435 .text = if (repo.event.description.len == 0) repo.event.name else try aa.print("{s} - {s}", .{ repo.event.name, repo.event.description }), 436 .link = try aa.print("a:/{s}:{s}", .{ data.user.name, repo.event.name }), 437 .bottom_label = if (repo.event.read_access == .private) "(private)" else "", 438 }); 439 if (data.repos_next_start) |next_start| { 440 var next = route; 441 next.user_repos.start = next_start; 442 try items.append(aa, .{ .text = "next →", .link = try aa.print("a:{s}", .{try next.toUrl(&arena)}) }); 443 } 444 try list.setItems(allocator, items.items); 445 446 try box.children.put(allocator, list.getFocus().id, .{ .widget = .{ .flow_box_scroll = list }, .rect = null, .min_size = null }); 447 } 448 449 // search results start in the box so the term can be refined right away 450 box.getFocus().child_id = box.children.keys()[if (data.repos_search != null) header_index else list_index]; 451 return .{ .box = box, .data = data, .session = session }; 452 } 453 454 pub fn deinit(self: *ReposView, allocator: std.mem.Allocator) void { 455 self.box.deinit(allocator); 456 } 457 458 pub fn build(self: *ReposView, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 459 self.clearGrid(); 460 // clear the incoming min height so the list scrolls within what's left 461 try self.box.build(allocator, .{ 462 .min_size = .{ .width = null, .height = null }, 463 .max_size = constraint.max_size, 464 }, root_focus); 465 } 466 467 pub fn input(self: *ReposView, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 468 const direction = inp.vertDirection(key); 469 if (self.headerActive()) { 470 const search_box = self.searchBox(); 471 if (direction == .down) { 472 // the empty message has nothing to focus 473 if (!self.data.reposEmpty()) root_focus.setFocus(self.listScroll().getFocus().id); 474 return; 475 } 476 if (key == .enter) { 477 const text = try search_box.text(allocator); 478 defer allocator.free(text); 479 return self.submit(text); 480 } 481 return search_box.input(allocator, key, root_focus); 482 } 483 // up from the first row reaches the search box 484 if (direction == .up and self.listScroll().atTop()) return self.focusHeader(root_focus); 485 try self.listScroll().input(allocator, key, root_focus); 486 } 487 488 // the repos narrowed to `text` from the first window. an empty box on an 489 // unsearched page has nothing to clear. 490 fn submit(self: *ReposView, text: []const u8) !void { 491 if (text.len == 0 and self.data.repos_search == null) return; 492 const route = reposRoute(self.data.user.name) orelse return; 493 try self.session.navigate(route.withSearch(text) orelse return); 494 } 495 496 fn searchBox(self: *ReposView) *ui.widget.SearchBox { 497 return &self.box.children.values()[header_index].widget.box.children.values()[0].widget.search_box; 498 } 499 500 fn listScroll(self: *ReposView) *ui.widget.FlowBox.Scroll { 501 return &self.box.children.values()[list_index].widget.flow_box_scroll; 502 } 503 504 fn headerActive(self: *ReposView) bool { 505 return self.box.getFocus().child_id == self.box.children.keys()[header_index]; 506 } 507 508 pub fn clearGrid(self: *ReposView) void { 509 self.box.clearGrid(); 510 } 511 512 pub fn getGrid(self: ReposView) ?Grid { 513 return self.box.getGrid(); 514 } 515 516 pub fn getFocus(self: *ReposView) *Focus { 517 return self.box.getFocus(); 518 } 519 520 // up leaves the tab from the search box 521 pub fn atTop(self: *ReposView) bool { 522 return self.headerActive(); 523 } 524 525 pub fn focusHeader(self: *ReposView, root_focus: *Focus) void { 526 root_focus.setFocus(self.searchBox().getFocus().id); 527 } 528 }; 529 530 // the first window of `name`'s repos 531 fn reposRoute(name: []const u8) ?ui.RoutablePage { 532 return .{ .user_repos = .{ .name = ui.RoutablePage.Array(evt.User.name_max_len).from(name) orelse return null } }; 533 } 534