╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Members.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
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()[0].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 fn aboutBox(self: *View) *wgt.Box(ui.Widget) { 277 return &self.contentBox().children.values()[about_index].widget.box; 278 } 279 280 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 281 self.clearGrid(); 282 if (self.data.confirm == null) { 283 try self.labelAddInput(root_focus); 284 ui.widget.markAboutFocused(self.aboutBox(), root_focus); 285 } 286 // the window's height as a minimum centers rows that fit 287 try self.scroll.build(allocator, .{ 288 .min_size = .{ .width = constraint.min_size.width, .height = constraint.max_size.height orelse constraint.min_size.height }, 289 .max_size = constraint.max_size, 290 }, root_focus); 291 } 292 293 // label the add input for this frame and register it for the web form 294 // handling 295 fn labelAddInput(self: *View, root_focus: *Focus) !void { 296 const name = self.nameInput(); 297 const failure = if (self.session.formFeedback(.repo_members)) |saved| saved.failure else null; 298 name.options.top_label.text = if (failure) |value| switch (value) { 299 .not_found => " add a member (not found) ", 300 .already_added => " add a member (already added) ", 301 } else " add a member "; 302 name.options.bottom_label.text = if (root_focus.grandchild_id == name.getFocus().id) " press enter " else ""; 303 try self.session.text_inputs.put(self.session.arena.allocator(), name.getFocus().id, name); 304 } 305 306 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 307 if (self.data.confirm) |confirm| { 308 const button_id = self.contentBox().getFocus().child_id orelse return; 309 if (inp.activated(root_focus, button_id, key)) try self.act(allocator, confirm.action, confirm.name); 310 return; 311 } 312 // the add row keeps every key but the vertical ones, which move between rows 313 if (self.addActive() and inp.vertDirection(key) == .none) { 314 if (key == .enter) { 315 const name = try self.nameInput().text(allocator); 316 defer allocator.free(name); 317 try self.act(allocator, .grant, name); 318 } else try self.nameInput().input(allocator, key, root_focus); 319 return; 320 } 321 322 const rows = self.contentBox(); 323 const current = rows.children.getIndex(rows.getFocus().child_id orelse return) orelse return; 324 if (inp.rowDelta(key, @intCast(rows.children.count()))) |delta| { 325 ui.widget.moveRowFocus(rows, &self.scroll, root_focus, delta); 326 return; 327 } 328 switch (key) { 329 .arrow_left, .arrow_right => _ = ui.widget.moveInSelectedRow(rows, root_focus, key == .arrow_right), 330 else => { 331 // the text, the add row and the previous link, when there is one, sit above the rows 332 const offset: usize = add_index + 1 + @as(usize, @intFromBool(self.data.start > 0)); 333 if (current < offset or current - offset >= self.data.rows.len) return; 334 const row = self.data.rows[current - offset]; 335 const row_box = &rows.children.values()[current].widget.box; 336 // remove and demote are links to their confirmations, which the host follows 337 if (row.role == .owner) return; 338 const button_id = row_box.getFocus().child_id orelse return; 339 if (row_box.children.getIndex(button_id) != 1 or !inp.activated(root_focus, button_id, key)) return; 340 try self.act(allocator, .promote, row.name); 341 }, 342 } 343 } 344 345 // the terminal performs the action itself; the web posts the form instead 346 fn act(self: *View, allocator: std.mem.Allocator, action: Action, name: []const u8) !void { 347 if (comptime wasm) return; 348 const io = self.session.io orelse return; 349 const users_dir = self.session.users_dir orelse return; 350 const admin_repo = self.session.admin_repo orelse return; 351 const actor = (try self.session.authorize(self.data.identity, .owner, null)) orelse return; 352 const moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 353 const failure = perform(io, allocator, moment, users_dir, actor, self.data.identity, action, name) catch |err| switch (err) { 354 // a stale row; the refresh below shows the list as it is 355 error.EventNotFound => null, 356 else => return err, 357 }; 358 if (failure) |value| { 359 self.session.data.form_feedback = .{ .repo_members = .{ .failure = value, .name = try self.session.arena.allocator().dupe(u8, name) } }; 360 return; 361 } 362 try self.session.navigate(landingRoute(self.data.identity, self.data.start, actor, action, name) orelse return error.RouteTooLong); 363 } 364 365 pub fn clearGrid(self: *View) void { 366 self.scroll.clearGrid(); 367 } 368 369 pub fn getGrid(self: View) ?Grid { 370 return self.scroll.getGrid(); 371 } 372 373 pub fn getFocus(self: *View) *Focus { 374 return self.scroll.getFocus(); 375 } 376 377 // up leaves the view from the explanatory text or the confirmation 378 pub fn atTop(self: *View) bool { 379 return self.data.confirm != null or self.contentBox().getFocus().child_id == self.aboutBox().getFocus().id; 380 } 381 }; 382