╭ Settings.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 hash = xit.hash; 8 const xitui = xit.xitui; 9 const wgt = xitui.widget; 10 const layout = xitui.layout; 11 const Key = xitui.input.Key; 12 const Grid = xitui.grid.Grid; 13 const Focus = xitui.focus.Focus; 14 15 const wasm = builtin.target.cpu.arch == .wasm32; 16 17 pub const tab_label = "☼"; 18 19 // the repo's settings, which only its owner sees. the hash kind is fixed at 20 // creation, so it only shows. 21 identity: []const u8, 22 name: []const u8, 23 description: []const u8, 24 access: evt.Repo.Access, 25 discuss_role: ?evt.Repo.Role, 26 issue_role: ?evt.Repo.Role, 27 patch_role: ?evt.Repo.Role, 28 hash_kind: hash.HashKind, 29 30 const Self = @This(); 31 32 // the tab role choices, in the order the form shows them 33 const role_choices = [_]?evt.Repo.Role{ null, .owner, .write, .read }; 34 35 const role_labels = blk: { 36 var labels: [role_choices.len][]const u8 = undefined; 37 for (&labels, role_choices) |*label, role| label.* = roleLabel(role); 38 break :blk labels; 39 }; 40 41 pub fn roleLabel(role_maybe: ?evt.Repo.Role) []const u8 { 42 const role = role_maybe orelse return "nobody"; 43 return switch (role) { 44 .owner => "owners", 45 .write => "collaborators", 46 .read => "anybody", 47 }; 48 } 49 50 // the tab role a label names 51 pub fn labelRole(label: []const u8) error{InvalidRole}!?evt.Repo.Role { 52 for (role_choices) |role| { 53 if (std.mem.eql(u8, label, roleLabel(role))) return role; 54 } 55 return error.InvalidRole; 56 } 57 58 pub const View = struct { 59 // a centered form, scrolled when it outgrows the window 60 scroll: wgt.Scroll(ui.Widget), 61 data: *const Self, 62 session: *ui.Session, 63 64 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 65 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert }); 66 errdefer box.deinit(allocator); 67 const route = ui.RoutablePage.repoRepoRoute(data.identity) orelse return error.RouteTooLong; 68 box.getFocus().kind = .{ .custom = try session.page_arena.allocator().print("form:{s}", .{try route.toUrl(session.page_arena)}) }; 69 70 const saved_fields = if (session.formFeedback(.repo_settings)) |saved| saved.fields else null; 71 72 { 73 var name = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " name " }, .name = "name", .visible_width = 30, .round_corners = true, .render_content = session.is_terminal }); 74 errdefer name.deinit(allocator); 75 name.getFocus().mode = .all; 76 try name.setContent(allocator, if (saved_fields) |saved| saved.name else data.name); 77 // show the start of the prefilled text 78 name.cursor = 0; 79 try box.children.put(allocator, name.getFocus().id, .{ .widget = .{ .text_input = name }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 80 box.getFocus().child_id = name.getFocus().id; 81 } 82 83 { 84 var description = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " description " }, .name = "description", .visible_width = 30, .round_corners = true, .render_content = session.is_terminal }); 85 errdefer description.deinit(allocator); 86 description.getFocus().mode = .all; 87 try description.setContent(allocator, if (saved_fields) |saved| saved.description else data.description); 88 // show the start of the prefilled text 89 description.cursor = 0; 90 try box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .text_input = description }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 91 } 92 93 { 94 const access = if (saved_fields) |saved| saved.access else data.access; 95 var access_radio = try ui.widget.Radio.init(allocator, session, "access", &.{ "private", "public" }, @tagName(access), null); 96 errdefer access_radio.deinit(allocator); 97 try box.children.put(allocator, access_radio.getFocus().id, .{ .widget = .{ .radio = access_radio }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 98 } 99 100 { 101 var hash_box = try wgt.TextBox.init(allocator, @tagName(data.hash_kind), .{ .border = .single, .round_corners = true, .wrap_kind = .none, .top_label = .{ .text = " hash " } }); 102 errdefer hash_box.deinit(allocator); 103 try box.children.put(allocator, hash_box.getFocus().id, .{ .widget = .{ .text_box = hash_box }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 104 } 105 106 const roles = [_]struct { name: []const u8, label: []const u8, role: ?evt.Repo.Role }{ 107 .{ .name = "discuss_role", .label = " who can create discussions ", .role = if (saved_fields) |saved| saved.discuss_role else data.discuss_role }, 108 .{ .name = "issue_role", .label = " who can create issues ", .role = if (saved_fields) |saved| saved.issue_role else data.issue_role }, 109 .{ .name = "patch_role", .label = " who can create patches ", .role = if (saved_fields) |saved| saved.patch_role else data.patch_role }, 110 }; 111 for (roles) |role| { 112 var role_radio = try ui.widget.Radio.init(allocator, session, role.name, &role_labels, roleLabel(role.role), role.label); 113 errdefer role_radio.deinit(allocator); 114 try box.children.put(allocator, role_radio.getFocus().id, .{ .widget = .{ .radio = role_radio }, .rect = null, .min_size = null }); 115 } 116 117 { 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