╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Branches.zig ─────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
155 156 fn nameInput(self: *View) *wgt.TextInput { 157 return &self.addBox().children.values()[1].widget.text_input; 158 } 159 160 fn addActive(self: *View) bool { 161 return self.contentBox().getFocus().child_id == self.addBox().getFocus().id; 162 } 163 164 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 165 self.clearGrid(); 166 try self.labelAddInput(root_focus); 167 // the window's height as a minimum centers rows that fit 168 try self.scroll.build(allocator, .{ 169 .min_size = .{ .width = constraint.min_size.width, .height = constraint.max_size.height orelse constraint.min_size.height }, 170 .max_size = constraint.max_size, 171 }, root_focus); 172 } 173 174 // label the add input for this frame and register it for the web form 175 // handling 176 fn labelAddInput(self: *View, root_focus: *Focus) !void { 177 const name = self.nameInput(); 178 const failure = if (self.session.formFeedback(.repo_branches)) |saved| saved.failure else null; 179 name.options.top_label.text = if (failure) |value| switch (value) { 180 .not_found => " add a protected branch (not found) ", 181 .already_added => " add a protected branch (already added) ", 182 .too_many => " add a protected branch (too many branches) ", 183 .too_long => " add a protected branch (name too long) ", 184 } else " add a protected branch "; 185 name.options.bottom_label.text = if (root_focus.grandchild_id == name.getFocus().id) " press enter " else ""; 186 try self.session.text_inputs.put(self.session.arena.allocator(), name.getFocus().id, name); 187 } 188 189 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 190 // the add row keeps every key but the vertical ones, which move between rows 191 if (self.addActive() and inp.vertDirection(key) == .none) { 192 if (key == .enter) { 193 const name = try self.nameInput().text(allocator); 194 defer allocator.free(name); 195 try self.act(allocator, .protect, name); 196 } else try self.nameInput().input(allocator, key, root_focus); 197 return; 198 } 199 200 const rows = self.contentBox(); 201 const current = rows.children.getIndex(rows.getFocus().child_id orelse return) orelse return; 202 if (inp.rowDelta(key, @intCast(rows.children.count()))) |delta| { 203 ui.widget.moveRowFocus(rows, &self.scroll, root_focus, delta); 204 return; 205 } 206 switch (key) { 207 .arrow_left, .arrow_right => _ = ui.widget.moveInSelectedRow(rows, root_focus, key == .arrow_right), 208 else => { 209 // the add row sits above the rows, and a row's name is a link the host follows 210 if (current <= add_index) return; 211 const row_box = &rows.children.values()[current].widget.box; 212 const button_id = row_box.getFocus().child_id orelse return; 213 if (row_box.children.getIndex(button_id) != 1 or !inp.activated(root_focus, button_id, key)) return; 214 try self.act(allocator, .unprotect, self.data.names[current - add_index - 1]); 215 }, 216 } 217 } 218 219 // the terminal performs the action itself; the web posts the form instead 220 fn act(self: *View, allocator: std.mem.Allocator, action: Action, branch: []const u8) !void { 221 if (comptime wasm) return; 222 const io = self.session.io orelse return; 223 const users_dir = self.session.users_dir orelse return; 224 const actor = (try self.session.authorize(self.data.identity, .owner, null)) orelse return; 225 if (try perform(io, allocator, users_dir, actor, self.data.identity, action, branch)) |failure| { 226 self.session.data.form_feedback = .{ .repo_branches = .{ .failure = failure, .name = try self.session.arena.allocator().dupe(u8, branch) } }; 227 return; 228 } 229 try self.session.navigate(ui.RoutablePage.repoBranchesRoute(self.data.identity) orelse return error.RouteTooLong); 230 } 231 232 pub fn clearGrid(self: *View) void { 233 self.scroll.clearGrid(); 234 } 235 236 pub fn getGrid(self: View) ?Grid { 237 return self.scroll.getGrid(); 238 } 239 240 pub fn getFocus(self: *View) *Focus { 241 return self.scroll.getFocus(); 242 } 243 244 // up leaves the view from the add row 245 pub fn atTop(self: *View) bool { 246 return self.addActive(); 247 } 248 }; 249