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