╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ NewRepo.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
26 errdefer box.deinit(allocator); 27 box.getFocus().kind = .{ .custom = try session.page_arena.allocator().print("form:{s}", .{try route.toUrl(session.page_arena)}) }; 28 29 const saved_fields = if (session.formFeedback(.repo)) |saved| saved.fields else null; 30 31 { 32 var name = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " name " }, .name = "name", .visible_width = 30, .round_corners = true, .render_content = session.is_terminal }); 33 errdefer name.deinit(allocator); 34 name.getFocus().mode = .all; 35 if (saved_fields) |saved| try name.setContent(allocator, saved.name); 36 try box.children.put(allocator, name.getFocus().id, .{ .widget = .{ .text_input = name }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 37 box.getFocus().child_id = name.getFocus().id; 38 } 39 40 { 41 var description = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " description " }, .name = "description", .visible_width = 30, .round_corners = true, .render_content = session.is_terminal }); 42 errdefer description.deinit(allocator); 43 description.getFocus().mode = .all; 44 if (saved_fields) |saved| try description.setContent(allocator, saved.description); 45 try box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .text_input = description }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 46 } 47 48 { 49 var access_radio = try ui.widget.Radio.init(allocator, session, "access", &.{ "private", "public" }, if (saved_fields) |saved| @tagName(saved.access) else "private", null); 50 errdefer access_radio.deinit(allocator); 51 try box.children.put(allocator, access_radio.getFocus().id, .{ .widget = .{ .radio = access_radio }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 52 } 53 54 { 55 var hash_radio = try ui.widget.Radio.init(allocator, session, "hash", &.{ "sha1", "sha256" }, if (saved_fields) |saved| @tagName(saved.hash) else "sha1", null); 56 errdefer hash_radio.deinit(allocator); 57 try box.children.put(allocator, hash_radio.getFocus().id, .{ .widget = .{ .radio = hash_radio }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 58 } 59 60 { 61 var submit = try ui.widget.SubmitButton.initLabeled(allocator, "submit"); 62 errdefer submit.deinit(allocator); 63 try box.children.put(allocator, submit.getFocus().id, .{ .widget = .{ .submit_button = submit }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 64 } 65 66 return .{ .center = try ui.widget.Center.init(allocator, .{ .box = box }), .session = session }; 67 } 68 69 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 70 self.center.deinit(allocator); 71 } 72 73 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 74 self.clearGrid(); 75 const failure = if (self.session.formFeedback(.repo)) |saved| saved.failure else null; 76 (try formField(self.formBox(), "name")).options.top_label.text = if (failure) |value| switch (value) { 77 .required_name => " name (required) ", 78 .invalid_name => " name (invalid) ", 79 .name_taken => " name (taken) ", 80 } else " name "; 81 // the web form handling finds the inputs by focus id 82 const inputs_arena = self.session.arena.allocator(); 83 for (self.formBox().children.values()) |*child| switch (child.widget) { 84 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 85 else => {}, 86 }; 87 try self.center.build(allocator, constraint, root_focus); 88 } 89 90 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 91 const cid = self.formBox().getFocus().child_id orelse return; 92 const cur = self.formBox().children.getIndex(cid) orelse return; 93 const child = &self.formBox().children.values()[cur]; 94 95 const on_submit = child.widget == .submit_button; 96 const direction = inp.vertDirection(key); 97 if (direction == .up or key == .back_tab) return if (cur > 0) root_focus.setFocus(self.formBox().children.keys()[cur - 1]); 98 if (direction == .down or key == .tab) return if (cur + 1 < self.formBox().children.count()) root_focus.setFocus(self.formBox().children.keys()[cur + 1]); 99 switch (key) { 100 .enter => if (on_submit) return self.submitForm(allocator), 101 .mouse => |mouse| if (on_submit) { 102 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) return self.submitForm(allocator); 103 }, 104 else => {}, 105 } 106 try child.widget.input(allocator, key, root_focus); 107 } 108 109 fn formRadio(form: *wgt.Box(ui.Widget), name: []const u8) !*ui.widget.Radio { 110 for (form.children.values()) |*child| switch (child.widget) { 111 .radio => |*radio| if (std.mem.eql(u8, radio.name, name)) return radio, 112 else => {}, 113 }; 114 return error.MissingFormField; 115 } 116 117 fn formField(form: *wgt.Box(ui.Widget), name: []const u8) !*wgt.TextInput { 118 for (form.children.values()) |*child| switch (child.widget) { 119 .text_input => |*text_input| if (std.mem.eql(u8, text_input.options.name, name)) return text_input, 120 else => {}, 121 }; 122 return error.MissingFormField; 123 } 124 125 // create the repo and navigate to it. this is the terminal path; the web 126 // posts the form to the new-repo route. 127 fn submitForm(self: *View, allocator: std.mem.Allocator) !void { 128 if (comptime wasm) return; 129 const io = self.session.io orelse return; 130 const users_dir = self.session.users_dir orelse return; 131 const admin_repo = self.session.admin_repo orelse return; 132 const user_id = self.session.userId() orelse return; 133 const moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 134 const user = (try ui.activeUser(moment, self.session.page_arena, user_id)) orelse return; 135 136 const name_input = try formField(self.formBox(), "name"); 137 const description_input = try formField(self.formBox(), "description"); 138 const name = try name_input.text(allocator); 139 defer allocator.free(name); 140 const description = try description_input.text(allocator); 141 defer allocator.free(description); 142 const hash_kind = std.meta.stringToEnum(hash.HashKind, (try formRadio(self.formBox(), "hash")).selected()) orelse unreachable; 143 const access = std.meta.stringToEnum(evt.Repo.Access, (try formRadio(self.formBox(), "access")).selected()) orelse unreachable; 144 145 _ = evt.createRepo(io, allocator, users_dir, &user_id, .{ .name = user.event.name, .email = user.event.email }, name, description, access, hash_kind) catch |err| { 146 const failure = ui.Session.FormFeedback.RepoFailure.fromError(err) orelse return err; 147 const aa = self.session.arena.allocator(); 148 self.session.data.form_feedback = .{ .repo = .{ .failure = failure, .fields = .{ 149 .name = try aa.dupe(u8, name), 150 .description = try aa.dupe(u8, description), 151 .hash = hash_kind, 152 .access = access, 153 } } }; 154 return; 155 }; 156 157 // wipe the form so a return visit starts fresh 158 name_input.clear(allocator); 159 description_input.clear(allocator); 160 161 const identity = try self.session.page_arena.allocator().print("{s}:{s}", .{ user.event.name, name }); 162 try self.session.navigate(ui.RoutablePage.repoFilesRoute(identity, null, "", "", 0) orelse return); 163 } 164 165 fn formBox(self: *View) *wgt.Box(ui.Widget) { 166 return &self.center.child.box; 167 } 168 169 pub fn clearGrid(self: *View) void { 170 self.center.clearGrid(); 171 } 172 173 pub fn getGrid(self: View) ?Grid { 174 return self.center.getGrid(); 175 } 176 177 pub fn getFocus(self: *View) *Focus { 178 return self.center.getFocus(); 179 } 180 181 // up leaves the form from its first control 182 pub fn atTop(self: View) bool { 183 const box = &self.center.child.box; 184 return box.focus.child_id == box.children.keys()[0]; 185 } 186 }; 187