╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ UserSettings.zig ─────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
80 var passwords = try wgt.Box(ui.Widget).init(allocator, .{ .border = .single, .round_corners = true, .direction = .vert, .top_label = .{ .text = " password " } }); 81 errdefer passwords.deinit(allocator); 82 const fields = [_]struct { label: []const u8, name: []const u8 }{ 83 .{ .label = " old ", .name = "current_password" }, 84 .{ .label = " new ", .name = "new_password" }, 85 .{ .label = " new again ", .name = "new_password_again" }, 86 }; 87 for (fields, 1..) |field, index| { 88 // narrower by the border, so the box lines up with the email box 89 var password = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = field.label }, .password = true, .name = field.name, .visible_width = 28, .round_corners = true, .render_content = session.is_terminal }); 90 errdefer password.deinit(allocator); 91 password.getFocus().mode = .all; 92 control_ids[index] = password.getFocus().id; 93 try passwords.children.put(allocator, password.getFocus().id, .{ .widget = .{ .text_input = password }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 94 } 95 passwords.getFocus().child_id = control_ids[1]; 96 try box.children.put(allocator, passwords.getFocus().id, .{ .widget = .{ .box = passwords }, .rect = null, .min_size = null }); 97 } 98 99 { 100 var submit = try ui.widget.SubmitButton.initLabeled(allocator, "submit changes"); 101 errdefer submit.deinit(allocator); 102 control_ids[4] = submit.buttonId(); 103 try box.children.put(allocator, submit.getFocus().id, .{ .widget = .{ .submit_button = submit }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 104 } 105 106 var center = try ui.widget.Center.init(allocator, .{ .box = box }); 107 errdefer center.deinit(allocator); 108 var scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .center = center }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 109 errdefer scroll.deinit(allocator); 110 try outer.children.put(allocator, scroll.getFocus().id, .{ .widget = .{ .scroll = scroll }, .rect = null, .min_size = null }); 111 } 112 113 outer.getFocus().child_id = outer.children.keys()[scroll_index]; 114 return .{ .box = outer, .session = session, .control_ids = control_ids }; 115 } 116 117 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 118 self.box.deinit(allocator); 119 } 120 121 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 122 self.clearGrid(); 123 const failure = if (self.session.formFeedback(.user_settings)) |saved| saved.failure else null; 124 (try self.formField("email")).options.top_label.text = if (failure) |value| switch (value) { 125 .required_email => " email (required) ", 126 .email_taken => " email (taken) ", 127 else => " email ", 128 } else " email "; 129 (try self.formField("current_password")).options.top_label.text = if (failure == .wrong_password) " old (wrong) " else " old "; 130 (try self.formField("new_password")).options.top_label.text = if (failure == .required_password) " new (required) " else " new "; 131 (try self.formField("new_password_again")).options.top_label.text = if (failure == .password_mismatch) " new again (doesn't match) " else " new again "; 132 // the web form handling finds the inputs by focus id 133 const inputs_arena = self.session.arena.allocator(); 134 for (self.formBoxes()) |box| for (box.children.values()) |*child| switch (child.widget) { 135 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 136 else => {}, 137 }; 138 // the window's height as a minimum centers a form that fits 139 self.box.children.values()[scroll_index].min_size = .{ .width = null, .height = constraint.max_size.height orelse constraint.min_size.height }; 140 try self.box.build(allocator, .{ 141 .min_size = .{ .width = constraint.min_size.width, .height = null }, 142 .max_size = constraint.max_size, 143 }, root_focus); 144 } 145 146 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 147 const focused = root_focus.grandchild_id orelse return; 148 const cur = std.mem.indexOfScalar(usize, &self.control_ids, focused) orelse return; 149 const submit_id = self.control_ids[self.control_ids.len - 1]; 150 const on_submit = focused == submit_id; 151 const direction = inp.vertDirection(key); 152 const up = direction == .up or key == .back_tab; 153 if (up or direction == .down or key == .tab) { 154 // up from the first control is the sub header's, through `atTop` 155 if (up and cur == 0) return; 156 const next = if (up) cur - 1 else cur + 1; 157 if (next == self.control_ids.len) return; 158 root_focus.setFocus(self.control_ids[next]); 159 if (self.session.is_terminal) if (self.controlRect(next)) |rect| ui.widget.scrollToCenteredRect(self.formScroll(), rect); 160 return; 161 } 162 switch (key) { 163 .enter => if (on_submit) return self.submitForm(allocator), 164 .mouse => |mouse| if (inp.leftClickOn(root_focus, submit_id, mouse)) return self.submitForm(allocator), 165 else => {}, 166 } 167 if (!on_submit) try (try self.formInput(focused)).input(allocator, key, root_focus); 168 } 169 170 // the boxes holding the form's inputs: the form itself and the password box 171 fn formBoxes(self: *View) [2]*wgt.Box(ui.Widget) { 172 const form = self.formBox(); 173 return .{ form, &form.children.values()[passwords_index].widget.box }; 174 } 175 176 fn formField(self: *View, name: []const u8) !*wgt.TextInput { 177 for (self.formBoxes()) |box| for (box.children.values()) |*child| switch (child.widget) { 178 .text_input => |*text_input| if (std.mem.eql(u8, text_input.options.name, name)) return text_input, 179 else => {}, 180 }; 181 return error.MissingFormField; 182 } 183 184 fn formInput(self: *View, id: usize) !*wgt.TextInput { 185 for (self.formBoxes()) |box| if (box.children.getPtr(id)) |child| return &child.widget.text_input; 186 return error.MissingFormField; 187 } 188 189 // the rect of the control at `index` in the form's space; a password's is 190 // offset by its box 191 fn controlRect(self: *View, index: usize) ?layout.IRect { 192 const form, const passwords = self.formBoxes(); 193 return switch (index) { 194 0 => form.children.values()[0].rect, 195 self.control_ids.len - 1 => form.children.values()[submit_index].rect, 196 else => { 197 const outer = form.children.values()[passwords_index].rect orelse return null; 198 const inner = passwords.children.values()[index - 1].rect orelse return null; 199 return .{ .x = outer.x + inner.x, .y = outer.y + inner.y, .size = inner.size }; 200 }, 201 }; 202 } 203 204 // update the user. this is the terminal path; the web posts the form to 205 // the user route. 206 fn submitForm(self: *View, allocator: std.mem.Allocator) !void { 207 if (comptime wasm) return; 208 const io = self.session.io orelse return; 209 const users_dir = self.session.users_dir orelse return; 210 const admin_repo = self.session.admin_repo orelse return; 211 const user_id = self.session.userId() orelse return; 212 213 const email = try (try self.formField("email")).text(allocator); 214 defer allocator.free(email); 215 const current_password = try (try self.formField("current_password")).text(allocator); 216 defer allocator.free(current_password); 217 const new_password = try (try self.formField("new_password")).text(allocator); 218 defer allocator.free(new_password); 219 const new_password_again = try (try self.formField("new_password_again")).text(allocator); 220 defer allocator.free(new_password_again); 221 222 evt.updateUser(io, allocator, users_dir, admin_repo, &user_id, email, current_password, new_password, new_password_again) catch |err| switch (err) { 223 // the account was removed under them 224 error.NotFound => return ui.UserNotifications.logOut(self.session), 225 else => { 226 const failure = ui.Session.FormFeedback.UserFailure.fromError(err) orelse return err; 227 const aa = self.session.arena.allocator(); 228 self.session.data.form_feedback = .{ .user_settings = .{ .failure = failure, .fields = .{ .email = try aa.dupe(u8, email) } } }; 229 return; 230 }, 231 }; 232 233 // reload the page with the saved email and the password fields cleared 234 self.session.haxy_moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 235 try self.session.loadUser(); 236 try self.session.navigate(self.session.data.current_page); 237 } 238 239 fn formScroll(self: *View) *wgt.Scroll(ui.Widget) { 240 return &self.box.children.values()[scroll_index].widget.scroll; 241 } 242 243 fn formBox(self: *View) *wgt.Box(ui.Widget) { 244 return &self.formScroll().child.center.child.box; 245 } 246 247 pub fn clearGrid(self: *View) void { 248 self.box.clearGrid(); 249 } 250 251 pub fn getGrid(self: View) ?Grid { 252 return self.box.getGrid(); 253 } 254 255 pub fn getFocus(self: *View) *Focus { 256 return self.box.getFocus(); 257 } 258 259 // up leaves the view from the first control 260 pub fn atTop(self: *View, root_focus: *Focus) bool { 261 return root_focus.grandchild_id == self.control_ids[0]; 262 } 263 }; 264