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