╭ 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 Quit = @import("./Quit.zig"); 18 19 pub const page_size = 20; // how many repos one window of the repos tab shows 20 21 pub const ForkItem = struct { 22 id: []const u8, 23 // the target repo, whose name and access the fork shares 24 target: evt.Repo, 25 title: []const u8, 26 }; 27 28 header: Header, 29 user: evt.User.Public, 30 repos: []const evt.Repo.Record, 31 repos_start: usize, // the repos window this page was built with, mirrored into the url 32 repos_next_start: ?usize, // the `start` for the "next" row, or null on the last window 33 repos_search: ?[]const u8, // the name prefix the repos are narrowed to (decoded; null = no search) 34 forks: []const ForkItem, 35 forks_start: usize, 36 forks_next_start: ?usize, 37 user_notifications: ui.UserNotifications, 38 quit: Quit, 39 40 const Self = @This(); 41 42 pub fn init( 43 arena: *std.heap.ArenaAllocator, 44 session: *ui.Session, 45 haxy_moment: evt.AdminDB.HashMap(.read_only), 46 name: ui.RoutablePage.Array(evt.User.name_max_len), 47 repos_start: usize, 48 // the url-encoded repos search ("" = none) 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 = record.event.repo_user_id[0..evt.event_id_size]; 138 const target = (try evt.readRepoById(io, gpa, arena, users_dir, owner_id, record.event.repo_id[0..evt.event_id_size], 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 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