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