╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ UserKeys.zig ─────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
157 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 158 self.clearGrid(); 159 if (self.data.confirm == null) try self.labelInputs(root_focus); 160 // the window's height as a minimum centers rows that fit 161 try self.scroll.build(allocator, .{ 162 .min_size = .{ .width = constraint.min_size.width, .height = constraint.max_size.height orelse constraint.min_size.height }, 163 .max_size = constraint.max_size, 164 }, root_focus); 165 } 166 167 // label the add input for this frame and register every input for the 168 // web form handling 169 fn labelInputs(self: *View, root_focus: *Focus) !void { 170 const add = self.addInput(); 171 const failure = if (self.session.formFeedback(.user_keys)) |saved| saved.failure else null; 172 add.options.top_label.text = if (failure) |value| switch (value) { 173 .invalid => " add ssh key (invalid) ", 174 .unsupported => " add ssh key (unsupported type) ", 175 .already_added => " add ssh key (already added) ", 176 .too_many => " add ssh key (too many keys) ", 177 } else add_label; 178 add.options.bottom_label.text = if (root_focus.grandchild_id == add.getFocus().id) " press enter " else ""; 179 // the web form handling finds the inputs by focus id 180 const inputs_arena = self.session.arena.allocator(); 181 for (self.contentBox().children.values()) |*child| { 182 const text_input = &child.widget.box.children.values()[0].widget.text_input; 183 try self.session.text_inputs.put(inputs_arena, text_input.getFocus().id, text_input); 184 } 185 } 186 187 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 188 if (self.data.confirm) |confirmed| { 189 const button_id = self.contentBox().getFocus().child_id orelse return; 190 if (inp.activated(root_focus, button_id, key)) try self.removeKey(allocator, confirmed); 191 return; 192 } 193 // the add row keeps every key but the vertical ones, which move between rows 194 if (self.addActive() and inp.vertDirection(key) == .none) { 195 if (key == .enter) { 196 const text = try self.addInput().text(allocator); 197 defer allocator.free(text); 198 try self.addKey(allocator, text); 199 } else try self.addInput().input(allocator, key, root_focus); 200 return; 201 } 202 203 const rows = self.contentBox(); 204 if (inp.rowDelta(key, @intCast(rows.children.count()))) |delta| { 205 ui.widget.moveRowFocus(rows, &self.scroll, root_focus, delta); 206 return; 207 } 208 // a row's ✕ is a link the host follows 209 if (key == .arrow_left or key == .arrow_right) _ = ui.widget.moveInSelectedRow(rows, root_focus, key == .arrow_right); 210 } 211 212 // the terminal changes the keys itself; the web posts the form instead 213 fn addKey(self: *View, allocator: std.mem.Allocator, key: []const u8) !void { 214 if (comptime wasm) return; 215 const io = self.session.io orelse return; 216 const users_dir = self.session.users_dir orelse return; 217 const admin_repo = self.session.admin_repo orelse return; 218 const user_id = self.session.userId() orelse return; 219 evt.addUserKey(io, allocator, users_dir, admin_repo, &user_id, key) catch |err| switch (err) { 220 // the account was removed under them 221 error.NotFound => return ui.UserNotifications.logOut(self.session), 222 else => { 223 const failure = ui.Session.FormFeedback.KeyFailure.fromError(err) orelse return err; 224 self.session.data.form_feedback = .{ .user_keys = .{ .failure = failure, .key = try self.session.arena.allocator().dupe(u8, key) } }; 225 return; 226 }, 227 }; 228 // reload the page with the keys as they are now 229 self.session.haxy_moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 230 try self.session.navigate(self.session.data.current_page); 231 } 232 233 // the terminal removes the key itself; the web posts the form instead 234 fn removeKey(self: *View, allocator: std.mem.Allocator, key: []const u8) !void { 235 if (comptime wasm) return; 236 const io = self.session.io orelse return; 237 const users_dir = self.session.users_dir orelse return; 238 const admin_repo = self.session.admin_repo orelse return; 239 const user_id = self.session.userId() orelse return; 240 const fingerprint = evt.User.fingerprintOfKey(key) orelse unreachable; 241 evt.removeUserKey(io, allocator, users_dir, admin_repo, &user_id, &fingerprint) catch |err| switch (err) { 242 // the account was removed under them 243 error.NotFound => return ui.UserNotifications.logOut(self.session), 244 else => return err, 245 }; 246 self.session.haxy_moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 247 try self.session.navigate(self.session.data.current_page.withUserTab(.{ .view = .keys })); 248 } 249 250 pub fn clearGrid(self: *View) void { 251 self.scroll.clearGrid(); 252 } 253 254 pub fn getGrid(self: View) ?Grid { 255 return self.scroll.getGrid(); 256 } 257 258 pub fn getFocus(self: *View) *Focus { 259 return self.scroll.getFocus(); 260 } 261 262 // up leaves the view from the add row or the confirmation 263 pub fn atTop(self: *View) bool { 264 return self.data.confirm != null or self.addActive(); 265 } 266 }; 267