╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Settings.zig ─────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
118 var submit = try ui.widget.SubmitButton.initLabeled(allocator, "submit changes"); 119 errdefer submit.deinit(allocator); 120 try box.children.put(allocator, submit.getFocus().id, .{ .widget = .{ .submit_button = submit }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 121 } 122 123 var center = try ui.widget.Center.init(allocator, .{ .box = box }); 124 errdefer center.deinit(allocator); 125 return .{ 126 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .center = center }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }), 127 .data = data, 128 .session = session, 129 }; 130 } 131 132 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 133 self.scroll.deinit(allocator); 134 } 135 136 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 137 self.clearGrid(); 138 const failure = if (self.session.formFeedback(.repo_settings)) |saved| saved.failure else null; 139 (try formField(self.formBox(), "name")).options.top_label.text = if (failure) |value| switch (value) { 140 .required_name => " name (required) ", 141 .invalid_name => " name (invalid) ", 142 .name_taken => " name (taken) ", 143 } else " name "; 144 // the web form handling finds the inputs by focus id 145 const inputs_arena = self.session.arena.allocator(); 146 for (self.formBox().children.values()) |*child| switch (child.widget) { 147 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 148 else => {}, 149 }; 150 // the window's height as a minimum centers a form that fits 151 try self.scroll.build(allocator, .{ 152 .min_size = .{ .width = constraint.min_size.width, .height = constraint.max_size.height orelse constraint.min_size.height }, 153 .max_size = constraint.max_size, 154 }, root_focus); 155 } 156 157 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 158 const cid = self.formBox().getFocus().child_id orelse return; 159 const cur = self.formBox().children.getIndex(cid) orelse return; 160 const child = &self.formBox().children.values()[cur]; 161 const keys = self.formBox().children.keys(); 162 163 const on_submit = child.widget == .submit_button; 164 const direction = inp.vertDirection(key); 165 const up = direction == .up or key == .back_tab; 166 if (up or direction == .down or key == .tab) { 167 var next = cur; 168 while (true) { 169 if (up) { 170 if (next == 0) return; 171 next -= 1; 172 } else { 173 next += 1; 174 if (next == keys.len) return; 175 } 176 // the read-only hash row is stepped over 177 if (self.formBox().children.values()[next].widget != .text_box) break; 178 } 179 root_focus.setFocus(keys[next]); 180 if (self.session.is_terminal) if (self.formBox().children.values()[next].rect) |rect| ui.widget.scrollToCenteredRect(&self.scroll, rect); 181 return; 182 } 183 switch (key) { 184 .enter => if (on_submit) return self.submitForm(allocator), 185 .mouse => |mouse| if (on_submit) { 186 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) return self.submitForm(allocator); 187 }, 188 else => {}, 189 } 190 try child.widget.input(allocator, key, root_focus); 191 } 192 193 fn formRadio(form: *wgt.Box(ui.Widget), name: []const u8) !*ui.widget.Radio { 194 for (form.children.values()) |*child| switch (child.widget) { 195 .radio => |*radio| if (std.mem.eql(u8, radio.name, name)) return radio, 196 else => {}, 197 }; 198 return error.MissingFormField; 199 } 200 201 fn formField(form: *wgt.Box(ui.Widget), name: []const u8) !*wgt.TextInput { 202 for (form.children.values()) |*child| switch (child.widget) { 203 .text_input => |*text_input| if (std.mem.eql(u8, text_input.options.name, name)) return text_input, 204 else => {}, 205 }; 206 return error.MissingFormField; 207 } 208 209 // update the repo and navigate to its new url. this is the terminal path; 210 // the web posts the form to the settings route. 211 fn submitForm(self: *View, allocator: std.mem.Allocator) !void { 212 if (comptime wasm) return; 213 const io = self.session.io orelse return; 214 const users_dir = self.session.users_dir orelse return; 215 const actor = (try self.session.authorize(self.data.identity, .owner, null)) orelse return; 216 217 const name = try (try formField(self.formBox(), "name")).text(allocator); 218 defer allocator.free(name); 219 const description = try (try formField(self.formBox(), "description")).text(allocator); 220 defer allocator.free(description); 221 const access = std.meta.stringToEnum(evt.Repo.Access, (try formRadio(self.formBox(), "access")).selected()) orelse unreachable; 222 const discuss_role = labelRole((try formRadio(self.formBox(), "discuss_role")).selected()) catch unreachable; 223 const issue_role = labelRole((try formRadio(self.formBox(), "issue_role")).selected()) catch unreachable; 224 const patch_role = labelRole((try formRadio(self.formBox(), "patch_role")).selected()) catch unreachable; 225 226 const route = update(io, allocator, users_dir, actor, self.data.identity, name, description, access, discuss_role, issue_role, patch_role) catch |err| { 227 const failure = ui.Session.FormFeedback.RepoFailure.fromError(err) orelse return err; 228 const aa = self.session.arena.allocator(); 229 self.session.data.form_feedback = .{ .repo_settings = .{ .failure = failure, .fields = .{ 230 .name = try aa.dupe(u8, name), 231 .description = try aa.dupe(u8, description), 232 .access = access, 233 .discuss_role = discuss_role, 234 .issue_role = issue_role, 235 .patch_role = patch_role, 236 } } }; 237 return; 238 }; 239 try self.session.navigate(route); 240 } 241 242 fn formBox(self: *View) *wgt.Box(ui.Widget) { 243 return &self.scroll.child.center.child.box; 244 } 245 246 pub fn clearGrid(self: *View) void { 247 self.scroll.clearGrid(); 248 } 249 250 pub fn getGrid(self: View) ?Grid { 251 return self.scroll.getGrid(); 252 } 253 254 pub fn getFocus(self: *View) *Focus { 255 return self.scroll.getFocus(); 256 } 257 258 // up leaves the form from its first control 259 pub fn atTop(self: View) bool { 260 const box = &self.scroll.child.center.child.box; 261 return box.focus.child_id == box.children.keys()[0]; 262 } 263 }; 264 265 // change the repo `identity` names as `actor`, its owner, returning its 266 // settings route under its new name 267 pub fn update( 268 io: std.Io, 269 allocator: std.mem.Allocator, 270 users_dir: []const u8, 271 actor: ui.Actor, 272 identity: []const u8, 273 name: []const u8, 274 description: []const u8, 275 access: evt.Repo.Access, 276 discuss_role: ?evt.Repo.Role, 277 issue_role: ?evt.Repo.Role, 278 patch_role: ?evt.Repo.Role, 279 ) !ui.RoutablePage { 280 const parsed = ui.RoutablePage.RepoIdentity.parse(identity) orelse return error.NotFound; 281 const owner_id = actor.repo_user_id orelse return error.NotFound; 282 try evt.updateRepo(io, allocator, users_dir, &owner_id, actor.author, parsed.name, name, description, access, discuss_role, issue_role, patch_role); 283 var buf: [ui.RoutablePage.repo_route_max_len]u8 = undefined; 284 const new_identity = std.fmt.bufPrint(&buf, "{s}:{s}", .{ parsed.owner, name }) catch return error.RouteTooLong; 285 return ui.RoutablePage.repoRepoRoute(new_identity) orelse error.RouteTooLong; 286 } 287