╭ UserSettings.zig ─────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const builtin = @import("builtin"); 3 const evt = @import("../event.zig"); 4 const ui = @import("../ui.zig"); 5 const inp = @import("./input.zig"); 6 const xit = @import("xit"); 7 const xitui = xit.xitui; 8 const wgt = xitui.widget; 9 const layout = xitui.layout; 10 const Key = xitui.input.Key; 11 const Grid = xitui.grid.Grid; 12 const Focus = xitui.focus.Focus; 13 14 const wasm = builtin.target.cpu.arch == .wasm32; 15 16 // the logged-in user's settings, or the login form when logged out 17 pub fn initView(allocator: std.mem.Allocator, session: *ui.Session) !ui.Widget { 18 if (session.data.user_id != null) return .{ .user_settings = try View.init(allocator, session) }; 19 return .{ .user_login = try ui.UserLogin.View.init(allocator, session) }; 20 } 21 22 // the user tab's label: the logged-in user's name, or "login" 23 pub fn tabLabel(session: *const ui.Session) []const u8 { 24 if (session.data.user_id == null) return "login"; 25 return session.data.user_name orelse unreachable; 26 } 27 28 const logout_label = "logout"; 29 30 pub const View = struct { 31 // a vertical box: the logout sub-header above a centered form, scrolled 32 // when it outgrows the window 33 box: wgt.Box(ui.Widget), 34 session: *ui.Session, 35 logout_id: usize, 36 // the form's controls in order: email, the passwords, then submit 37 control_ids: [5]usize, 38 39 const header_index = 0; 40 const scroll_index = 1; 41 // a row taller than the button, leaving a blank line above the form 42 const header_height = 4; 43 // the form's children: the email box, the bordered password box, submit 44 const passwords_index = 1; 45 const submit_index = 2; 46 47 pub fn init(allocator: std.mem.Allocator, session: *ui.Session) !View { 48 var outer = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert }); 49 errdefer outer.deinit(allocator); 50 51 var logout_id: usize = undefined; 52 { 53 // the form kind makes the web renderer post the button to /logout 54 var header = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 55 errdefer header.deinit(allocator); 56 header.getFocus().kind = .{ .custom = "form:logout" }; 57 // the spacer pushes the logout button to the right edge 58 { 59 var spacer = try ui.widget.Spacer.init(allocator); 60 errdefer spacer.deinit(allocator); 61 try header.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 62 } 63 { 64 var button = try wgt.TextBox.init(allocator, logout_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 65 errdefer button.deinit(allocator); 66 button.getFocus().mode = .all; 67 // the renderer distinguishes plain clickables from buttons that 68 // should POST to a server route by this kind. 69 button.getFocus().kind = .{ .custom = "submit" }; 70 logout_id = button.getFocus().id; 71 header.getFocus().child_id = logout_id; 72 try header.children.put(allocator, logout_id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = logout_label.len + 2, .height = 3 } }); 73 } 74 try outer.children.put(allocator, header.getFocus().id, .{ .widget = .{ .box = header }, .rect = null, .min_size = .{ .width = null, .height = header_height } }); 75 } 76 77 var control_ids: [5]usize = undefined; 78 { 79 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert }); 80 errdefer box.deinit(allocator); 81 // the form posts to the page's own user tab 82 box.getFocus().kind = .{ .custom = "form:user" }; 83 84 const saved_fields = if (session.formFeedback(.user_settings)) |saved| saved.fields else null; 85 86 { 87 var email = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " email " }, .name = "email", .visible_width = 30, .round_corners = true, .render_content = session.is_terminal }); 88 errdefer email.deinit(allocator); 89 email.getFocus().mode = .all; 90 try email.setContent(allocator, if (saved_fields) |saved| saved.email else session.data.user_email orelse unreachable); 91 // show the start of the prefilled text 92 email.cursor = 0; 93 control_ids[0] = email.getFocus().id; 94 box.getFocus().child_id = email.getFocus().id; 95 try box.children.put(allocator, email.getFocus().id, .{ .widget = .{ .text_input = email }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 96 } 97 98 // the password changes only when all three are filled in 99 { 100 var passwords = try wgt.Box(ui.Widget).init(allocator, .{ .border = .single, .round_corners = true, .direction = .vert, .top_label = .{ .text = " password " } }); 101 errdefer passwords.deinit(allocator); 102 const fields = [_]struct { label: []const u8, name: []const u8 }{ 103 .{ .label = " old ", .name = "current_password" }, 104 .{ .label = " new ", .name = "new_password" }, 105 .{ .label = " new again ", .name = "new_password_again" }, 106 }; 107 for (fields, 1..) |field, index| { 108 // narrower by the border, so the box lines up with the email box 109 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 }); 110 errdefer password.deinit(allocator); 111 password.getFocus().mode = .all; 112 control_ids[index] = password.getFocus().id; 113 try passwords.children.put(allocator, password.getFocus().id, .{ .widget = .{ .text_input = password }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 114 } 115 passwords.getFocus().child_id = control_ids[1]; 116 try box.children.put(allocator, passwords.getFocus().id, .{ .widget = .{ .box = passwords }, .rect = null, .min_size = null }); 117 } 118 119 { 120 var submit = try ui.widget.SubmitButton.initLabeled(allocator, "submit changes"); 121 errdefer submit.deinit(allocator); 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