╭ 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 hash_kind: hash.HashKind, 26 27 const Self = @This(); 28 29 pub const View = struct { 30 center: ui.widget.Center, 31 data: *const Self, 32 session: *ui.Session, 33 34 const access_index: usize = 2; 35 const hash_index: usize = 3; 36 37 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 38 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 39 errdefer box.deinit(allocator); 40 const route = ui.RoutablePage.repoRepoRoute(data.identity) orelse return error.RouteTooLong; 41 box.getFocus().kind = .{ .custom = try std.fmt.allocPrint(session.page_arena.allocator(), "form:{s}", .{try route.toUrl(session.page_arena)}) }; 42 43 const saved_fields = if (session.formFeedback(.repo_settings)) |saved| saved.fields else null; 44 45 { 46 var name = try wgt.TextInput.init(allocator, .{ .label = " name ", .name = "name", .visible_width = 30, .round_corners = true, .render_content = session.is_terminal }); 47 errdefer name.deinit(allocator); 48 name.getFocus().mode = .all; 49 try name.setContent(allocator, if (saved_fields) |saved| saved.name else data.name); 50 // show the start of the prefilled text 51 name.cursor = 0; 52 try box.children.put(allocator, name.getFocus().id, .{ .widget = .{ .text_input = name }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 53 box.getFocus().child_id = name.getFocus().id; 54 } 55 56 { 57 var description = try wgt.TextInput.init(allocator, .{ .label = " description ", .name = "description", .visible_width = 30, .round_corners = true, .render_content = session.is_terminal }); 58 errdefer description.deinit(allocator); 59 description.getFocus().mode = .all; 60 try description.setContent(allocator, if (saved_fields) |saved| saved.description else data.description); 61 // show the start of the prefilled text 62 description.cursor = 0; 63 try box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .text_input = description }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 64 } 65 66 { 67 const access = if (saved_fields) |saved| saved.access else data.access; 68 var access_radio = try ui.widget.Radio.init(allocator, session, "access", &.{ "private", "public" }, @tagName(access)); 69 errdefer access_radio.deinit(allocator); 70 try box.children.put(allocator, access_radio.getFocus().id, .{ .widget = .{ .radio = access_radio }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 71 } 72 73 { 74 var hash_box = try wgt.TextBox.init(allocator, @tagName(data.hash_kind), .{ .border_style = .single, .round_corners = true, .wrap_kind = .none, .label = " hash " }); 75 errdefer hash_box.deinit(allocator); 76 try box.children.put(allocator, hash_box.getFocus().id, .{ .widget = .{ .text_box = hash_box }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 77 } 78 79 { 80 var submit = try ui.widget.SubmitButton.initLabeled(allocator, "submit changes"); 81 errdefer submit.deinit(allocator); 82 try box.children.put(allocator, submit.getFocus().id, .{ .widget = .{ .submit_button = submit }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 83 } 84 85 return .{ .center = try ui.widget.Center.init(allocator, .{ .box = box }), .data = data, .session = session }; 86 } 87 88 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 89 self.center.deinit(allocator); 90 } 91 92 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 93 self.clearGrid(); 94 const failure = if (self.session.formFeedback(.repo_settings)) |saved| saved.failure else null; 95 (try formField(self.formBox(), "name")).options.label = if (failure) |value| switch (value) { 96 .required_name => " name (required) ", 97 .invalid_name => " name (invalid) ", 98 .name_taken => " name (taken) ", 99 } else " name "; 100 // the web form handling finds the inputs by focus id 101 const inputs_arena = self.session.arena.allocator(); 102 for (self.formBox().children.values()) |*child| switch (child.widget) { 103 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 104 else => {}, 105 }; 106 try self.center.build(allocator, constraint, root_focus); 107 } 108 109 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 110 const cid = self.formBox().getFocus().child_id orelse return; 111 const cur = self.formBox().children.getIndex(cid) orelse return; 112 const child = &self.formBox().children.values()[cur]; 113 const keys = self.formBox().children.keys(); 114 115 const on_submit = child.widget == .submit_button; 116 // the read-only hash row is stepped over 117 const direction = inp.vertDirection(key); 118 if (direction == .up or key == .back_tab) return if (cur > 0) root_focus.setFocus(keys[if (cur - 1 == hash_index) access_index else cur - 1]); 119 if (direction == .down or key == .tab) return if (cur + 1 < keys.len) root_focus.setFocus(keys[if (cur + 1 == hash_index) hash_index + 1 else cur + 1]); 120 switch (key) { 121 .enter => if (on_submit) return self.submitForm(allocator), 122 .mouse => |mouse| if (on_submit) { 123 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) return self.submitForm(allocator); 124 }, 125 else => {}, 126 } 127 try child.widget.input(allocator, key, root_focus); 128 } 129 130 fn formField(form: *wgt.Box(ui.Widget), name: []const u8) !*wgt.TextInput { 131 for (form.children.values()) |*child| switch (child.widget) { 132 .text_input => |*text_input| if (std.mem.eql(u8, text_input.options.name, name)) return text_input, 133 else => {}, 134 }; 135 return error.MissingFormField; 136 } 137 138 // update the repo and navigate to its new url. this is the terminal path; 139 // the web posts the form to the settings route. 140 fn submitForm(self: *View, allocator: std.mem.Allocator) !void { 141 if (comptime wasm) return; 142 const io = self.session.io orelse return; 143 const users_dir = self.session.users_dir orelse return; 144 const actor = (try self.session.authorize(self.data.identity, .owner)) orelse return; 145 146 const name = try (try formField(self.formBox(), "name")).text(allocator); 147 defer allocator.free(name); 148 const description = try (try formField(self.formBox(), "description")).text(allocator); 149 defer allocator.free(description); 150 const access_radio = &self.formBox().children.values()[access_index].widget.radio; 151 const access = std.meta.stringToEnum(evt.Repo.Access, access_radio.selected()) orelse unreachable; 152 153 const route = update(io, allocator, users_dir, actor, self.data.identity, name, description, access) catch |err| { 154 const failure = ui.Session.FormFeedback.RepoFailure.fromError(err) orelse return err; 155 const aa = self.session.arena.allocator(); 156 self.session.data.form_feedback = .{ .repo_settings = .{ .failure = failure, .fields = .{ 157 .name = try aa.dupe(u8, name), 158 .description = try aa.dupe(u8, description), 159 .access = access, 160 } } }; 161 return; 162 }; 163 try self.session.navigate(route); 164 } 165 166 fn formBox(self: *View) *wgt.Box(ui.Widget) { 167 return &self.center.child.box; 168 } 169 170 pub fn clearGrid(self: *View) void { 171 self.center.clearGrid(); 172 } 173 174 pub fn getGrid(self: View) ?Grid { 175 return self.center.getGrid(); 176 } 177 178 pub fn getFocus(self: *View) *Focus { 179 return self.center.getFocus(); 180 } 181 182 // up leaves the form from its first control 183 pub fn atTop(self: View) bool { 184 const box = &self.center.child.box; 185 return box.focus.child_id == box.children.keys()[0]; 186 } 187 }; 188 189 // change the repo `identity` names as `actor`, its owner, returning its 190 // settings route under its new name 191 pub fn update( 192 io: std.Io, 193 allocator: std.mem.Allocator, 194 users_dir: []const u8, 195 actor: ui.Actor, 196 identity: []const u8, 197 name: []const u8, 198 description: []const u8, 199 access: evt.Repo.Access, 200 ) !ui.RoutablePage { 201 const parsed = ui.RoutablePage.RepoIdentity.parse(identity) orelse return error.NotFound; 202 const owner_id = actor.repo_user_id orelse return error.NotFound; 203 try evt.updateRepo(io, allocator, users_dir, &owner_id, actor.author, parsed.name, name, description, access); 204 var buf: [ui.RoutablePage.repo_route_max_len]u8 = undefined; 205 const new_identity = std.fmt.bufPrint(&buf, "{s}:{s}", .{ parsed.owner, name }) catch return error.RouteTooLong; 206 return ui.RoutablePage.repoRepoRoute(new_identity) orelse error.RouteTooLong; 207 } 208