╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Members.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
228 } 229 row_box.getFocus().child_id = row_box.children.keys()[0]; 230 try box.children.put(allocator, row_box.getFocus().id, .{ .widget = .{ .box = row_box }, .rect = null, .min_size = null }); 231 } 232 if (data.next_start) |next_start| { 233 var next = try Comment.linkBox(allocator, session, "next →", ui.RoutablePage.repoMembersRoute(data.identity, next_start) orelse return error.RouteTooLong); 234 errdefer next.deinit(allocator); 235 try box.children.put(allocator, next.getFocus().id, .{ .widget = .{ .text_box = next }, .rect = null, .min_size = null }); 236 } 237 238 box.getFocus().child_id = box.children.keys()[add_index]; 239 var center = try ui.widget.Center.init(allocator, .{ .box = box }); 240 errdefer center.deinit(allocator); 241 return .{ 242 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .center = center }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }), 243 .data = data, 244 .session = session, 245 }; 246 } 247 248 fn addButton(allocator: std.mem.Allocator, row: *wgt.Box(ui.Widget), text: []const u8, kind: []const u8) !void { 249 var button = try wgt.TextBox.init(allocator, text, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 250 errdefer button.deinit(allocator); 251 button.getFocus().mode = .all; 252 button.getFocus().kind = .{ .custom = kind }; 253 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 254 } 255 256 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 257 self.scroll.deinit(allocator); 258 } 259 260 fn contentBox(self: *View) *wgt.Box(ui.Widget) { 261 return &self.scroll.child.center.child.box; 262 } 263 264 fn addBox(self: *View) *wgt.Box(ui.Widget) { 265 return &self.contentBox().children.values()[add_index].widget.box; 266 } 267 268 fn nameInput(self: *View) *wgt.TextInput { 269 return &self.addBox().children.values()[1].widget.text_input; 270 } 271 272 fn addActive(self: *View) bool { 273 return self.contentBox().getFocus().child_id == self.addBox().getFocus().id; 274 } 275 276 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 277 self.clearGrid(); 278 if (self.data.confirm == null) try self.labelAddInput(root_focus); 279 // the window's height as a minimum centers rows that fit 280 try self.scroll.build(allocator, .{ 281 .min_size = .{ .width = constraint.min_size.width, .height = constraint.max_size.height orelse constraint.min_size.height }, 282 .max_size = constraint.max_size, 283 }, root_focus); 284 } 285 286 // label the add input for this frame and register it for the web form 287 // handling 288 fn labelAddInput(self: *View, root_focus: *Focus) !void { 289 const name = self.nameInput(); 290 const failure = if (self.session.formFeedback(.repo_members)) |saved| saved.failure else null; 291 name.options.top_label.text = if (failure) |value| switch (value) { 292 .not_found => " add a member (not found) ", 293 .already_added => " add a member (already added) ", 294 } else " add a member "; 295 name.options.bottom_label.text = if (root_focus.grandchild_id == name.getFocus().id) " press enter " else ""; 296 try self.session.text_inputs.put(self.session.arena.allocator(), name.getFocus().id, name); 297 } 298 299 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 300 if (self.data.confirm) |confirm| { 301 const button_id = self.contentBox().getFocus().child_id orelse return; 302 if (inp.activated(root_focus, button_id, key)) try self.act(allocator, confirm.action, confirm.name); 303 return; 304 } 305 // the add row keeps every key but the vertical ones, which move between rows 306 if (self.addActive() and inp.vertDirection(key) == .none) { 307 if (key == .enter) { 308 const name = try self.nameInput().text(allocator); 309 defer allocator.free(name); 310 try self.act(allocator, .grant, name); 311 } else try self.nameInput().input(allocator, key, root_focus); 312 return; 313 } 314 315 const rows = self.contentBox(); 316 const current = rows.children.getIndex(rows.getFocus().child_id orelse return) orelse return; 317 if (inp.rowDelta(key, @intCast(rows.children.count()))) |delta| { 318 ui.widget.moveRowFocus(rows, &self.scroll, root_focus, delta); 319 return; 320 } 321 switch (key) { 322 .arrow_left, .arrow_right => _ = ui.widget.moveInSelectedRow(rows, root_focus, key == .arrow_right), 323 else => { 324 // the add row and the previous link, when there is one, sit above the rows 325 const offset: usize = add_index + 1 + @intFromBool(self.data.start > 0); 326 if (current < offset or current - offset >= self.data.rows.len) return; 327 const row = self.data.rows[current - offset]; 328 const row_box = &rows.children.values()[current].widget.box; 329 // remove and demote are links to their confirmations, which the host follows 330 if (row.role == .owner) return; 331 const button_id = row_box.getFocus().child_id orelse return; 332 if (row_box.children.getIndex(button_id) != 1 or !inp.activated(root_focus, button_id, key)) return; 333 try self.act(allocator, .promote, row.name); 334 }, 335 } 336 } 337 338 // the terminal performs the action itself; the web posts the form instead 339 fn act(self: *View, allocator: std.mem.Allocator, action: Action, name: []const u8) !void { 340 if (comptime wasm) return; 341 const io = self.session.io orelse return; 342 const users_dir = self.session.users_dir orelse return; 343 const admin_repo = self.session.admin_repo orelse return; 344 const actor = (try self.session.authorize(self.data.identity, .owner, null)) orelse return; 345 const moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 346 const failure = perform(io, allocator, moment, users_dir, actor, self.data.identity, action, name) catch |err| switch (err) { 347 // a stale row; the refresh below shows the list as it is 348 error.EventNotFound => null, 349 else => return err, 350 }; 351 if (failure) |value| { 352 self.session.data.form_feedback = .{ .repo_members = .{ .failure = value, .name = try self.session.arena.allocator().dupe(u8, name) } }; 353 return; 354 } 355 try self.session.navigate(landingRoute(self.data.identity, self.data.start, actor, action, name) orelse return error.RouteTooLong); 356 } 357 358 pub fn clearGrid(self: *View) void { 359 self.scroll.clearGrid(); 360 } 361 362 pub fn getGrid(self: View) ?Grid { 363 return self.scroll.getGrid(); 364 } 365 366 pub fn getFocus(self: *View) *Focus { 367 return self.scroll.getFocus(); 368 } 369 370 // up leaves the view from the add row or the confirmation 371 pub fn atTop(self: *View) bool { 372 return self.data.confirm != null or self.addActive(); 373 } 374 }; 375