╭ 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 Roles = @import("./Roles.zig"); 16 17 const wasm = builtin.target.cpu.arch == .wasm32; 18 19 pub const tab_label = "☼"; 20 21 // the repo's settings, which only its owner sees. the hash kind is fixed at 22 // creation, so it only shows. 23 identity: []const u8, 24 name: []const u8, 25 description: []const u8, 26 access: evt.Repo.Access, 27 discuss_role: ?evt.Repo.Role, 28 issue_role: ?evt.Repo.Role, 29 patch_role: ?evt.Repo.Role, 30 hash_kind: hash.HashKind, 31 // the sub tab the route names 32 view: enum { settings, roles }, 33 roles: Roles, 34 35 const Self = @This(); 36 37 // the tab role choices, in the order the form shows them 38 const role_choices = [_]?evt.Repo.Role{ null, .owner, .write, .read }; 39 40 const role_labels = blk: { 41 var labels: [role_choices.len][]const u8 = undefined; 42 for (&labels, role_choices) |*label, role| label.* = roleLabel(role); 43 break :blk labels; 44 }; 45 46 pub fn roleLabel(role_maybe: ?evt.Repo.Role) []const u8 { 47 const role = role_maybe orelse return "nobody"; 48 return switch (role) { 49 .owner => "owners", 50 .write => "collaborators", 51 .read => "anybody", 52 }; 53 } 54 55 // the tab role a label names 56 pub fn labelRole(label: []const u8) error{InvalidRole}!?evt.Repo.Role { 57 for (role_choices) |role| { 58 if (std.mem.eql(u8, label, roleLabel(role))) return role; 59 } 60 return error.InvalidRole; 61 } 62 63 // the repo settings and user roles sub tabs over their content 64 pub const View = struct { 65 box: wgt.Box(ui.Widget), 66 67 const header_index = 0; 68 const content_index = 1; 69 70 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 71 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert }); 72 errdefer box.deinit(allocator); 73 74 { 75 var header = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 76 errdefer header.deinit(allocator); 77 const on_tab = switch (session.data.current_page) { 78 .repo_settings, .repo_roles => true, 79 else => false, 80 }; 81 const routes = [_]?ui.RoutablePage{ ui.RoutablePage.repoSettingsRoute(data.identity), ui.RoutablePage.repoRolesRoute(data.identity, 0) }; 82 for ([_]@FieldType(Self, "view"){ .settings, .roles }, [_][]const u8{ "repo settings", "user roles" }, routes) |view, label, route| { 83 var tab = try wgt.TextBox.init(allocator, label, .{ .border = .hidden, .round_corners = true, .wrap_kind = .none }); 84 errdefer tab.deinit(allocator); 85 tab.getFocus().mode = .all; 86 tab.getFocus().kind = .{ .custom = try ui.inPageTabLink(session, route orelse return error.RouteTooLong, on_tab and data.view == view) }; 87 try header.children.put(allocator, tab.getFocus().id, .{ .widget = .{ .text_box = tab }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 88 } 89 header.getFocus().child_id = header.children.keys()[@backingInt(data.view)]; 90 // a row taller than the header, leaving a blank line beneath it 91 try box.children.put(allocator, header.getFocus().id, .{ .widget = .{ .box = header }, .rect = null, .min_size = .{ .width = null, .height = 4 } }); 92 } 93 94 { 95 var stack = try wgt.Stack(ui.Widget).init(allocator); 96 errdefer stack.deinit(allocator); 97 { 98 var form = try Form.init(allocator, data, session); 99 errdefer form.deinit(allocator); 100 try stack.children.put(allocator, form.getFocus().id, .{ .repo_settings_form = form }); 101 } 102 { 103 var roles = try Roles.View.init(allocator, &data.roles, session); 104 errdefer roles.deinit(allocator); 105 try stack.children.put(allocator, roles.getFocus().id, .{ .repo_roles = roles }); 106 } 107 stack.getFocus().child_id = stack.children.keys()[@backingInt(data.view)]; 108 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 109 } 110 box.getFocus().child_id = box.children.keys()[content_index]; 111 return .{ .box = box }; 112 } 113 114 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 115 self.box.deinit(allocator); 116 } 117 118 fn headerBox(self: *View) *wgt.Box(ui.Widget) { 119 return &self.box.children.values()[header_index].widget.box; 120 } 121 122 fn contentStack(self: *View) *wgt.Stack(ui.Widget) { 123 return &self.box.children.values()[content_index].widget.stack; 124 } 125 126 fn headerActive(self: *View) bool { 127 return self.box.getFocus().child_id == self.headerBox().getFocus().id; 128 } 129 130 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 131 self.clearGrid(); 132 const header = self.headerBox(); 133 if (header.getFocus().child_id) |selected_id| { 134 for (header.children.keys(), header.children.values(), 0..) |id, *child, index| { 135 ui.widget.markSelected(&child.widget.text_box, id == selected_id); 136 if (id == selected_id) self.contentStack().getFocus().child_id = self.contentStack().children.keys()[index]; 137 } 138 } 139 try self.box.build(allocator, constraint, root_focus); 140 } 141 142 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 143 const direction = inp.vertDirection(key); 144 if (self.headerActive()) { 145 const header = self.headerBox(); 146 if (direction == .down) { 147 root_focus.setFocus(self.contentStack().getFocus().id); 148 } else if (header.children.getIndex(header.getFocus().child_id orelse return)) |current| { 149 if (inp.moveTab(key, current, header.children.count())) |next| root_focus.setFocus(header.children.keys()[next]); 150 } 151 return; 152 } 153 if (direction == .up) if (self.contentStack().getSelected()) |selected| if (selected.atTop(root_focus)) { 154 root_focus.setFocus(self.headerBox().getFocus().id); 155 return; 156 }; 157 try self.contentStack().input(allocator, key, root_focus); 158 } 159 160 // moving up from the sub header returns to the page's tabs 161 pub fn atTop(self: *View) bool { 162 return self.headerActive(); 163 } 164 165 // arriving from the page's tabs lands on the sub header, not the content 166 pub fn focusHeader(self: *View, root_focus: *Focus) bool { 167 root_focus.setFocus(self.headerBox().getFocus().id); 168 return true; 169 } 170 171 pub fn clearGrid(self: *View) void { 172 self.box.clearGrid(); 173 } 174 175 pub fn getGrid(self: View) ?Grid { 176 return self.box.getGrid(); 177 } 178 179 pub fn getFocus(self: *View) *Focus { 180 return self.box.getFocus(); 181 } 182 }; 183 184 pub const Form = struct { 185 // a centered form, scrolled when it outgrows the window 186 scroll: wgt.Scroll(ui.Widget), 187 data: *const Self, 188 session: *ui.Session, 189 190 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !Form { 191 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert }); 192 errdefer box.deinit(allocator); 193 const route = ui.RoutablePage.repoSettingsRoute(data.identity) orelse return error.RouteTooLong; 194 box.getFocus().kind = .{ .custom = try session.page_arena.allocator().print("form:{s}", .{try route.toUrl(session.page_arena)}) }; 195 196 const saved_fields = if (session.formFeedback(.repo_settings)) |saved| saved.fields else null; 197 198 { 199 var name = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " name " }, .name = "name", .visible_width = 30, .round_corners = true, .render_content = session.is_terminal }); 200 errdefer name.deinit(allocator); 201 name.getFocus().mode = .all; 202 try name.setContent(allocator, if (saved_fields) |saved| saved.name else data.name); 203 // show the start of the prefilled text 204 name.cursor = 0; 205 try box.children.put(allocator, name.getFocus().id, .{ .widget = .{ .text_input = name }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 206 box.getFocus().child_id = name.getFocus().id; 207 } 208 209 { 210 var description = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " description " }, .name = "description", .visible_width = 30, .round_corners = true, .render_content = session.is_terminal }); 211 errdefer description.deinit(allocator); 212 description.getFocus().mode = .all; 213 try description.setContent(allocator, if (saved_fields) |saved| saved.description else data.description); 214 // show the start of the prefilled text 215 description.cursor = 0; 216 try box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .text_input = description }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 217 } 218 219 { 220 const access = if (saved_fields) |saved| saved.access else data.access; 221 var access_radio = try ui.widget.Radio.init(allocator, session, "access", &.{ "private", "public" }, @tagName(access), null); 222 errdefer access_radio.deinit(allocator); 223 try box.children.put(allocator, access_radio.getFocus().id, .{ .widget = .{ .radio = access_radio }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 224 } 225 226 { 227 var hash_box = try wgt.TextBox.init(allocator, @tagName(data.hash_kind), .{ .border = .single, .round_corners = true, .wrap_kind = .none, .top_label = .{ .text = " hash " } }); 228 errdefer hash_box.deinit(allocator); 229 try box.children.put(allocator, hash_box.getFocus().id, .{ .widget = .{ .text_box = hash_box }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 230 } 231 232 const roles = [_]struct { name: []const u8, label: []const u8, role: ?evt.Repo.Role }{ 233 .{ .name = "discuss_role", .label = " new discussions can be made by ", .role = if (saved_fields) |saved| saved.discuss_role else data.discuss_role }, 234 .{ .name = "issue_role", .label = " new issues can be made by ", .role = if (saved_fields) |saved| saved.issue_role else data.issue_role }, 235 .{ .name = "patch_role", .label = " new patches can be made by ", .role = if (saved_fields) |saved| saved.patch_role else data.patch_role }, 236 }; 237 for (roles) |role| { 238 var role_radio = try ui.widget.Radio.init(allocator, session, role.name, &role_labels, roleLabel(role.role), role.label); 239 errdefer role_radio.deinit(allocator); 240 try box.children.put(allocator, role_radio.getFocus().id, .{ .widget = .{ .radio = role_radio }, .rect = null, .min_size = null }); 241 } 242 243 { 244 var submit = try ui.widget.SubmitButton.initLabeled(allocator, "submit changes"); 245 errdefer submit.deinit(allocator); 246 try box.children.put(allocator, submit.getFocus().id, .{ .widget = .{ .submit_button = submit }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 247 } 248 249 var center = try ui.widget.Center.init(allocator, .{ .box = box }); 250 errdefer center.deinit(allocator); 251 return .{ 252 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .center = center }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }), 253 .data = data, 254 .session = session, 255 }; 256 } 257 258 pub fn deinit(self: *Form, allocator: std.mem.Allocator) void { 259 self.scroll.deinit(allocator); 260 } 261 262 pub fn build(self: *Form, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 263 self.clearGrid(); 264 const failure = if (self.session.formFeedback(.repo_settings)) |saved| saved.failure else null; 265 (try formField(self.formBox(), "name")).options.top_label.text = if (failure) |value| switch (value) { 266 .required_name => " name (required) ", 267 .invalid_name => " name (invalid) ", 268 .name_taken => " name (taken) ", 269 } else " name "; 270 // the web form handling finds the inputs by focus id 271 const inputs_arena = self.session.arena.allocator(); 272 for (self.formBox().children.values()) |*child| switch (child.widget) { 273 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 274 else => {}, 275 }; 276 // the window's height as a minimum centers a form that fits 277 try self.scroll.build(allocator, .{ 278 .min_size = .{ .width = constraint.min_size.width, .height = constraint.max_size.height orelse constraint.min_size.height }, 279 .max_size = constraint.max_size, 280 }, root_focus); 281 } 282 283 pub fn input(self: *Form, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 284 const cid = self.formBox().getFocus().child_id orelse return; 285 const cur = self.formBox().children.getIndex(cid) orelse return; 286 const child = &self.formBox().children.values()[cur]; 287 const keys = self.formBox().children.keys(); 288 289 const on_submit = child.widget == .submit_button; 290 const direction = inp.vertDirection(key); 291 const up = direction == .up or key == .back_tab; 292 if (up or direction == .down or key == .tab) { 293 var next = cur; 294 while (true) { 295 if (up) { 296 if (next == 0) return; 297 next -= 1; 298 } else { 299 next += 1; 300 if (next == keys.len) return; 301 } 302 // the read-only hash row is stepped over 303 if (self.formBox().children.values()[next].widget != .text_box) break; 304 } 305 root_focus.setFocus(keys[next]); 306 if (self.session.is_terminal) if (self.formBox().children.values()[next].rect) |rect| ui.widget.scrollToCenteredRect(&self.scroll, rect); 307 return; 308 } 309 switch (key) { 310 .enter => if (on_submit) return self.submitForm(allocator), 311 .mouse => |mouse| if (on_submit) { 312 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) return self.submitForm(allocator); 313 }, 314 else => {}, 315 } 316 try child.widget.input(allocator, key, root_focus); 317 } 318 319 fn formRadio(form: *wgt.Box(ui.Widget), name: []const u8) !*ui.widget.Radio { 320 for (form.children.values()) |*child| switch (child.widget) { 321 .radio => |*radio| if (std.mem.eql(u8, radio.name, name)) return radio, 322 else => {}, 323 }; 324 return error.MissingFormField; 325 } 326 327 fn formField(form: *wgt.Box(ui.Widget), name: []const u8) !*wgt.TextInput { 328 for (form.children.values()) |*child| switch (child.widget) { 329 .text_input => |*text_input| if (std.mem.eql(u8, text_input.options.name, name)) return text_input, 330 else => {}, 331 }; 332 return error.MissingFormField; 333 } 334 335 // update the repo and navigate to its new url. this is the terminal path; 336 // the web posts the form to the settings route. 337 fn submitForm(self: *Form, allocator: std.mem.Allocator) !void { 338 if (comptime wasm) return; 339 const io = self.session.io orelse return; 340 const users_dir = self.session.users_dir orelse return; 341 const actor = (try self.session.authorize(self.data.identity, .owner, null)) orelse return; 342 343 const name = try (try formField(self.formBox(), "name")).text(allocator); 344 defer allocator.free(name); 345 const description = try (try formField(self.formBox(), "description")).text(allocator); 346 defer allocator.free(description); 347 const access = std.meta.stringToEnum(evt.Repo.Access, (try formRadio(self.formBox(), "access")).selected()) orelse unreachable; 348 const discuss_role = labelRole((try formRadio(self.formBox(), "discuss_role")).selected()) catch unreachable; 349 const issue_role = labelRole((try formRadio(self.formBox(), "issue_role")).selected()) catch unreachable; 350 const patch_role = labelRole((try formRadio(self.formBox(), "patch_role")).selected()) catch unreachable; 351 352 const route = update(io, allocator, users_dir, actor, self.data.identity, name, description, access, discuss_role, issue_role, patch_role) catch |err| { 353 const failure = ui.Session.FormFeedback.RepoFailure.fromError(err) orelse return err; 354 const aa = self.session.arena.allocator(); 355 self.session.data.form_feedback = .{ .repo_settings = .{ .failure = failure, .fields = .{ 356 .name = try aa.dupe(u8, name), 357 .description = try aa.dupe(u8, description), 358 .access = access, 359 .discuss_role = discuss_role, 360 .issue_role = issue_role, 361 .patch_role = patch_role, 362 } } }; 363 return; 364 }; 365 try self.session.navigate(route); 366 } 367 368 fn formBox(self: *Form) *wgt.Box(ui.Widget) { 369 return &self.scroll.child.center.child.box; 370 } 371 372 pub fn clearGrid(self: *Form) void { 373 self.scroll.clearGrid(); 374 } 375 376 pub fn getGrid(self: Form) ?Grid { 377 return self.scroll.getGrid(); 378 } 379 380 pub fn getFocus(self: *Form) *Focus { 381 return self.scroll.getFocus(); 382 } 383 384 // up leaves the form from its first control 385 pub fn atTop(self: Form) bool { 386 const box = &self.scroll.child.center.child.box; 387 return box.focus.child_id == box.children.keys()[0]; 388 } 389 }; 390 391 // change the repo `identity` names as `actor`, its owner, returning its 392 // settings route under its new name 393 pub fn update( 394 io: std.Io, 395 allocator: std.mem.Allocator, 396 users_dir: []const u8, 397 actor: ui.Actor, 398 identity: []const u8, 399 name: []const u8, 400 description: []const u8, 401 access: evt.Repo.Access, 402 discuss_role: ?evt.Repo.Role, 403 issue_role: ?evt.Repo.Role, 404 patch_role: ?evt.Repo.Role, 405 ) !ui.RoutablePage { 406 const parsed = ui.RoutablePage.RepoIdentity.parse(identity) orelse return error.NotFound; 407 const owner_id = actor.repo_user_id orelse return error.NotFound; 408 try evt.updateRepo(io, allocator, users_dir, &owner_id, actor.author, parsed.name, name, description, access, discuss_role, issue_role, patch_role); 409 var buf: [ui.RoutablePage.repo_route_max_len]u8 = undefined; 410 const new_identity = std.fmt.bufPrint(&buf, "{s}:{s}", .{ parsed.owner, name }) catch return error.RouteTooLong; 411 return ui.RoutablePage.repoSettingsRoute(new_identity) orelse error.RouteTooLong; 412 } 413