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