╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ UserSettings.zig ─────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
122 control_ids[4] = submit.buttonId(); 123 try box.children.put(allocator, submit.getFocus().id, .{ .widget = .{ .submit_button = submit }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 124 } 125 126 var center = try ui.widget.Center.init(allocator, .{ .box = box }); 127 errdefer center.deinit(allocator); 128 var scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .center = center }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 129 errdefer scroll.deinit(allocator); 130 try outer.children.put(allocator, scroll.getFocus().id, .{ .widget = .{ .scroll = scroll }, .rect = null, .min_size = null }); 131 } 132 133 outer.getFocus().child_id = outer.children.keys()[header_index]; 134 return .{ .box = outer, .session = session, .logout_id = logout_id, .control_ids = control_ids }; 135 } 136 137 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 138 self.box.deinit(allocator); 139 } 140 141 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 142 self.clearGrid(); 143 const failure = if (self.session.formFeedback(.user_settings)) |saved| saved.failure else null; 144 (try self.formField("email")).options.top_label.text = if (failure) |value| switch (value) { 145 .required_email => " email (required) ", 146 .email_taken => " email (taken) ", 147 else => " email ", 148 } else " email "; 149 (try self.formField("current_password")).options.top_label.text = if (failure == .wrong_password) " old (wrong) " else " old "; 150 (try self.formField("new_password")).options.top_label.text = if (failure == .required_password) " new (required) " else " new "; 151 (try self.formField("new_password_again")).options.top_label.text = if (failure == .password_mismatch) " new again (doesn't match) " else " new again "; 152 // the web form handling finds the inputs by focus id 153 const inputs_arena = self.session.arena.allocator(); 154 for (self.formBoxes()) |box| for (box.children.values()) |*child| switch (child.widget) { 155 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 156 else => {}, 157 }; 158 // the window's height below the sub-header as a minimum centers a form that fits 159 const viewport_height = constraint.max_size.height orelse constraint.min_size.height; 160 self.box.children.values()[scroll_index].min_size = .{ .width = null, .height = if (viewport_height) |height| height -| header_height else null }; 161 try self.box.build(allocator, .{ 162 .min_size = .{ .width = constraint.min_size.width, .height = null }, 163 .max_size = constraint.max_size, 164 }, root_focus); 165 } 166 167 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 168 if (key == .mouse and inp.leftClickOn(root_focus, self.logout_id, key.mouse)) return self.logOut(); 169 if (self.headerActive()) { 170 if (key == .tab or inp.vertDirection(key) == .down) return root_focus.setFocus(self.control_ids[0]); 171 if (key == .enter) try self.logOut(); 172 return; 173 } 174 175 const focused = root_focus.grandchild_id orelse return; 176 const cur = std.mem.indexOfScalar(usize, &self.control_ids, focused) orelse return; 177 const submit_id = self.control_ids[self.control_ids.len - 1]; 178 const on_submit = focused == submit_id; 179 const direction = inp.vertDirection(key); 180 const up = direction == .up or key == .back_tab; 181 if (up or direction == .down or key == .tab) { 182 // up leaves the form from its first control for the logout button 183 if (up and cur == 0) return root_focus.setFocus(self.logout_id); 184 const next = if (up) cur - 1 else cur + 1; 185 if (next == self.control_ids.len) return; 186 root_focus.setFocus(self.control_ids[next]); 187 if (self.session.is_terminal) if (self.controlRect(next)) |rect| ui.widget.scrollToCenteredRect(self.formScroll(), rect); 188 return; 189 } 190 switch (key) { 191 .enter => if (on_submit) return self.submitForm(allocator), 192 .mouse => |mouse| if (inp.leftClickOn(root_focus, submit_id, mouse)) return self.submitForm(allocator), 193 else => {}, 194 } 195 if (!on_submit) try (try self.formInput(focused)).input(allocator, key, root_focus); 196 } 197 198 fn logOut(self: *View) !void { 199 self.session.logOut(); 200 // leave the user tab for the page it belongs to, where the web's 201 // /logout redirect also lands 202 try self.session.navigate(self.session.data.current_page.pageRoot()); 203 } 204 205 // the boxes holding the form's inputs: the form itself and the password box 206 fn formBoxes(self: *View) [2]*wgt.Box(ui.Widget) { 207 const form = self.formBox(); 208 return .{ form, &form.children.values()[passwords_index].widget.box }; 209 } 210 211 fn formField(self: *View, name: []const u8) !*wgt.TextInput { 212 for (self.formBoxes()) |box| for (box.children.values()) |*child| switch (child.widget) { 213 .text_input => |*text_input| if (std.mem.eql(u8, text_input.options.name, name)) return text_input, 214 else => {}, 215 }; 216 return error.MissingFormField; 217 } 218 219 fn formInput(self: *View, id: usize) !*wgt.TextInput { 220 for (self.formBoxes()) |box| if (box.children.getPtr(id)) |child| return &child.widget.text_input; 221 return error.MissingFormField; 222 } 223 224 // the rect of the control at `index` in the form's space; a password's is 225 // offset by its box 226 fn controlRect(self: *View, index: usize) ?layout.IRect { 227 const form, const passwords = self.formBoxes(); 228 return switch (index) { 229 0 => form.children.values()[0].rect, 230 self.control_ids.len - 1 => form.children.values()[submit_index].rect, 231 else => { 232 const outer = form.children.values()[passwords_index].rect orelse return null; 233 const inner = passwords.children.values()[index - 1].rect orelse return null; 234 return .{ .x = outer.x + inner.x, .y = outer.y + inner.y, .size = inner.size }; 235 }, 236 }; 237 } 238 239 // update the user. this is the terminal path; the web posts the form to 240 // the user route. 241 fn submitForm(self: *View, allocator: std.mem.Allocator) !void { 242 if (comptime wasm) return; 243 const io = self.session.io orelse return; 244 const users_dir = self.session.users_dir orelse return; 245 const admin_repo = self.session.admin_repo orelse return; 246 const user_id = self.session.userId() orelse return; 247 248 const email = try (try self.formField("email")).text(allocator); 249 defer allocator.free(email); 250 const current_password = try (try self.formField("current_password")).text(allocator); 251 defer allocator.free(current_password); 252 const new_password = try (try self.formField("new_password")).text(allocator); 253 defer allocator.free(new_password); 254 const new_password_again = try (try self.formField("new_password_again")).text(allocator); 255 defer allocator.free(new_password_again); 256 257 evt.updateUser(io, allocator, users_dir, admin_repo, &user_id, email, current_password, new_password, new_password_again) catch |err| switch (err) { 258 // the account was removed under them 259 error.NotFound => return self.logOut(), 260 else => { 261 const failure = ui.Session.FormFeedback.UserFailure.fromError(err) orelse return err; 262 const aa = self.session.arena.allocator(); 263 self.session.data.form_feedback = .{ .user_settings = .{ .failure = failure, .fields = .{ .email = try aa.dupe(u8, email) } } }; 264 return; 265 }, 266 }; 267 268 // reload the page with the saved email and the password fields cleared 269 self.session.haxy_moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 270 try self.session.loadUser(); 271 try self.session.navigate(self.session.data.current_page); 272 } 273 274 fn headerActive(self: *View) bool { 275 return self.box.getFocus().child_id == self.box.children.keys()[header_index]; 276 } 277 278 fn formScroll(self: *View) *wgt.Scroll(ui.Widget) { 279 return &self.box.children.values()[scroll_index].widget.scroll; 280 } 281 282 fn formBox(self: *View) *wgt.Box(ui.Widget) { 283 return &self.formScroll().child.center.child.box; 284 } 285 286 pub fn clearGrid(self: *View) void { 287 self.box.clearGrid(); 288 } 289 290 pub fn getGrid(self: View) ?Grid { 291 return self.box.getGrid(); 292 } 293 294 pub fn getFocus(self: *View) *Focus { 295 return self.box.getFocus(); 296 } 297 298 // up leaves the view from the logout button 299 pub fn atTop(self: *View) bool { 300 return self.headerActive(); 301 } 302 }; 303