╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Roles.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
198 errdefer button.deinit(allocator); 199 button.getFocus().mode = .all; 200 button.getFocus().kind = .{ .custom = kind }; 201 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 202 } 203 204 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 205 self.scroll.deinit(allocator); 206 } 207 208 fn contentBox(self: *View) *wgt.Box(ui.Widget) { 209 return &self.scroll.child.center.child.box; 210 } 211 212 fn addBox(self: *View) *wgt.Box(ui.Widget) { 213 return &self.contentBox().children.values()[add_index].widget.box; 214 } 215 216 fn nameInput(self: *View) *wgt.TextInput { 217 return &self.addBox().children.values()[0].widget.text_input; 218 } 219 220 fn addActive(self: *View) bool { 221 return self.contentBox().getFocus().child_id == self.addBox().getFocus().id; 222 } 223 224 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 225 self.clearGrid(); 226 const name = self.nameInput(); 227 const failure = if (self.session.formFeedback(.repo_roles)) |saved| saved.failure else null; 228 name.options.top_label.text = if (failure) |value| switch (value) { 229 .not_found => " add a collaborator (not found) ", 230 .already_added => " add a collaborator (already added) ", 231 } else " add a collaborator "; 232 name.options.bottom_label.text = if (root_focus.grandchild_id == name.getFocus().id) " press enter " else ""; 233 // the web form handling finds the input by focus id 234 try self.session.text_inputs.put(self.session.arena.allocator(), name.getFocus().id, name); 235 // the window's height as a minimum centers rows that fit 236 try self.scroll.build(allocator, .{ 237 .min_size = .{ .width = constraint.min_size.width, .height = constraint.max_size.height orelse constraint.min_size.height }, 238 .max_size = constraint.max_size, 239 }, root_focus); 240 } 241 242 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 243 // the add row keeps every key but the vertical ones, which move between rows 244 if (self.addActive() and inp.vertDirection(key) == .none) { 245 if (key == .enter) { 246 const name = try self.nameInput().text(allocator); 247 defer allocator.free(name); 248 try self.act(allocator, .grant, name); 249 } else try self.nameInput().input(allocator, key, root_focus); 250 return; 251 } 252 253 const rows = self.contentBox(); 254 const current = rows.children.getIndex(rows.getFocus().child_id orelse return) orelse return; 255 if (inp.rowDelta(key, @intCast(rows.children.count()))) |delta| { 256 ui.widget.moveRowFocus(rows, &self.scroll, root_focus, delta); 257 return; 258 } 259 switch (key) { 260 .arrow_left, .arrow_right => _ = ui.widget.moveInSelectedRow(rows, root_focus, key == .arrow_right), 261 else => { 262 // the add row and the previous link, when there is one, sit above the rows 263 const offset: usize = add_index + 1 + @intFromBool(self.data.start > 0); 264 if (current < offset or current - offset >= self.data.rows.len) return; 265 const row = self.data.rows[current - offset]; 266 const row_box = &rows.children.values()[current].widget.box; 267 const button_id = row_box.getFocus().child_id orelse return; 268 if (!inp.activated(root_focus, button_id, key)) return; 269 const action: Action = switch (row_box.children.getIndex(button_id) orelse return) { 270 1 => .revoke, 271 2 => if (row.role == .owner) .demote else .promote, 272 else => return, 273 }; 274 try self.act(allocator, action, row.name); 275 }, 276 } 277 } 278 279 // the terminal performs the action itself; the web posts the form instead 280 fn act(self: *View, allocator: std.mem.Allocator, action: Action, name: []const u8) !void { 281 if (comptime wasm) return; 282 const io = self.session.io orelse return; 283 const users_dir = self.session.users_dir orelse return; 284 const admin_repo = self.session.admin_repo orelse return; 285 const actor = (try self.session.authorize(self.data.identity, .owner, null)) orelse return; 286 const moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 287 const failure = perform(io, allocator, moment, users_dir, actor, self.data.identity, action, name) catch |err| switch (err) { 288 // a stale row; the refresh below shows the list as it is 289 error.EventNotFound => null, 290 else => return err, 291 }; 292 if (failure) |value| { 293 self.session.data.form_feedback = .{ .repo_roles = .{ .failure = value, .name = try self.session.arena.allocator().dupe(u8, name) } }; 294 return; 295 } 296 try self.session.navigate(landingRoute(self.data.identity, self.data.start, actor, action, name) orelse return error.RouteTooLong); 297 } 298 299 pub fn clearGrid(self: *View) void { 300 self.scroll.clearGrid(); 301 } 302 303 pub fn getGrid(self: View) ?Grid { 304 return self.scroll.getGrid(); 305 } 306 307 pub fn getFocus(self: *View) *Focus { 308 return self.scroll.getFocus(); 309 } 310 311 // up leaves the view from the add row 312 pub fn atTop(self: *View) bool { 313 return self.addActive(); 314 } 315 }; 316