╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ User.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
131 errdefer list.deinit(allocator); 132 133 var arena = std.heap.ArenaAllocator.init(allocator); 134 defer arena.deinit(); 135 const aa = arena.allocator(); 136 137 // a leading "previous" row off the first window, one row per repo, 138 // then a trailing "next" row when more remain. each window row 139 // navigates to the adjacent window of this user's repos. 140 var items: std.ArrayList(ui.FlowBox.Item) = .empty; 141 if (data.repos_after > 0) 142 try items.append(aa, .{ .text = "← previous", .link = try std.fmt.allocPrint(aa, "a:/user/{s}?after={d}", .{ data.user.name, data.repos_after -| page_size }) }); 143 for (data.repos) |repo| 144 // clicking a repo opens its page; the "a:" prefix makes the web 145 // renderer emit an <a href="/repo/alice/foo"> anchor. 146 try items.append(aa, .{ 147 .text = try std.fmt.allocPrint(aa, "{s} - {s}", .{ repo.name, repo.description }), 148 .link = try std.fmt.allocPrint(aa, "a:/repo/{s}/{s}", .{ data.user.name, repo.name }), 149 }); 150 if (data.repos_next_after) |next_after| 151 try items.append(aa, .{ .text = "next →", .link = try std.fmt.allocPrint(aa, "a:/user/{s}?after={d}", .{ data.user.name, next_after }) }); 152 try list.setItems(allocator, items.items); 153 154 try stack.children.put(allocator, list.getFocus().id, .{ .flow_box_scroll = list }); 155 } 156 157 { 158 var settings_view = try Settings.View.init(allocator, &data.settings, session); 159 errdefer settings_view.deinit(allocator); 160 try stack.children.put(allocator, settings_view.getFocus().id, .{ .home_settings = settings_view }); 161 } 162 163 { 164 var auth_view = try Auth.View.init(allocator, &data.auth, session, repos_tab_id); 165 errdefer auth_view.deinit(allocator); 166 try stack.children.put(allocator, auth_view.getFocus().id, .{ .home_auth = auth_view }); 167 } 168 169 if (session.is_terminal) { 170 var quit_view = try Quit.View.init(allocator, &data.quit, session); 171 errdefer quit_view.deinit(allocator); 172 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 173 } 174 175 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 176 } 177 178 var self = View{ .box = box, .data = data, .session = session }; 179 self.getFocus().child_id = box.children.keys()[header_index]; 180 return self; 181 } 182 183 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 184 self.box.deinit(allocator); 185 } 186 187 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 188 self.clearGrid(); 189 const header = &self.box.children.values()[header_index].widget.user_header; 190 const stack = &self.box.children.values()[stack_index].widget.stack; 191 192 // each header tab maps 1:1 to a stack child by position. mirror the 193 // selection into current_page so the host can push the matching url; 194 // all user tabs share the .user parent, so this stays on the page 195 // rather than navigating. 196 if (header.getSelectedIndex()) |index| { 197 stack.getFocus().child_id = stack.children.keys()[index]; 198 const name = self.data.route_name; 199 switch (std.meta.activeTag(stack.children.values()[index])) { 200 .home_settings => self.session.data.current_page = .{ .user_settings = name }, 201 .home_auth => self.session.data.current_page = .{ .user_auth = name }, 202 // the quit tab is tty-only and not a route, so leave current_page 203 // alone (nothing to mirror into the url). 204 .quit => {}, 205 // the repos list (the default tab) 206 else => self.session.data.current_page = .{ .user = .{ .name = name, .after = self.data.repos_after } }, 207 } 208 } 209 try self.box.build(allocator, constraint, root_focus); 210 } 211 212 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 213 if (self.getFocus().child_id) |child_id| { 214 if (self.box.children.getIndex(child_id)) |current_index| { 215 const child = &self.box.children.values()[current_index].widget; 216 var index = current_index; 217 218 const Direction = enum { up, down, none }; 219 const direction: Direction = switch (key) { 220 .arrow_up => .up, 221 .arrow_down => .down, 222 .mouse => |mouse| if (mouse.action == .scroll) 223 (if (mouse.action.scroll == .up) .up else .down) 224 else 225 .none, 226 else => .none, 227 }; 228 229 switch (direction) { 230 .up => { 231 switch (child.*) { 232 .user_header => { 233 try child.input(allocator, key, root_focus); 234 }, 235 .stack => { 236 if (child.stack.getSelected()) |selected_widget| { 237 const at_top = switch (selected_widget.*) { 238 .flow_box_scroll => |*v| v.getSelectedIndex() == 0, 239 .home_settings => |*v| v.getSelectedIndex() == 0, 240 .home_auth => |*v| v.getSelectedIndex() == 0, 241 .quit => |*v| v.getSelectedIndex() == 0, 242 else => false, 243 }; 244 if (at_top) { 245 index = header_index; 246 } else { 247 try child.input(allocator, key, root_focus); 248 } 249 } 250 }, 251 else => {}, 252 } 253 }, 254 .down => { 255 switch (child.*) { 256 .user_header => { 257 index = stack_index; 258 }, 259 .stack => { 260 try child.input(allocator, key, root_focus); 261 }, 262 else => {}, 263 } 264 }, 265 .none => { 266 try child.input(allocator, key, root_focus); 267 }, 268 } 269 270 if (index != current_index) { 271 root_focus.setFocus(self.box.children.keys()[index]); 272 } 273 } 274 } 275 } 276 277 pub fn clearGrid(self: *View) void { 278 self.box.clearGrid(); 279 } 280 281 pub fn getGrid(self: View) ?Grid { 282 return self.box.getGrid(); 283 } 284 285 pub fn getFocus(self: *View) *Focus { 286 return self.box.getFocus(); 287 } 288 }; 289