╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
130 if (std.mem.eql(u8, files_link, current_link)) selected_tab = text_box.getFocus().id; 131 try box.children.put(allocator, text_box.getFocus().id, .{ 132 .widget = .{ .text_box = text_box }, 133 .rect = null, 134 .min_size = .{ .width = "files".len + 2, .height = null }, 135 }); 136 } 137 138 // commits tab 139 { 140 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "commits", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 141 errdefer text_box.deinit(allocator); 142 text_box.getFocus().focusable = true; 143 text_box.getFocus().kind = .{ .custom = commits_link }; 144 try tab_ids.put(allocator, text_box.getFocus().id, {}); 145 if (std.mem.eql(u8, commits_link, current_link)) selected_tab = text_box.getFocus().id; 146 try box.children.put(allocator, text_box.getFocus().id, .{ 147 .widget = .{ .text_box = text_box }, 148 .rect = null, 149 .min_size = .{ .width = "commits".len + 2, .height = null }, 150 }); 151 } 152 153 // refs tab 154 { 155 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "refs", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 156 errdefer text_box.deinit(allocator); 157 text_box.getFocus().focusable = true; 158 text_box.getFocus().kind = .{ .custom = refs_link }; 159 try tab_ids.put(allocator, text_box.getFocus().id, {}); 160 if (std.mem.eql(u8, refs_link, current_link)) selected_tab = text_box.getFocus().id; 161 try box.children.put(allocator, text_box.getFocus().id, .{ 162 .widget = .{ .text_box = text_box }, 163 .rect = null, 164 .min_size = .{ .width = "refs".len + 2, .height = null }, 165 }); 166 } 167 168 // spacer pushes settings + auth to the right 169 { 170 var spacer = try ui.Spacer.init(allocator); 171 errdefer spacer.deinit(allocator); 172 try box.children.put(allocator, spacer.getFocus().id, .{ 173 .widget = .{ .spacer = spacer }, 174 .rect = null, 175 .min_size = null, 176 }); 177 } 178 179 // settings tab 180 { 181 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "settings", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 182 errdefer text_box.deinit(allocator); 183 text_box.getFocus().focusable = true; 184 text_box.getFocus().kind = .{ .custom = settings_link }; 185 try tab_ids.put(allocator, text_box.getFocus().id, {}); 186 if (std.mem.eql(u8, settings_link, current_link)) selected_tab = text_box.getFocus().id; 187 try box.children.put(allocator, text_box.getFocus().id, .{ 188 .widget = .{ .text_box = text_box }, 189 .rect = null, 190 .min_size = .{ .width = "settings".len + 2, .height = null }, 191 }); 192 } 193 194 // auth tab (login / logout). AuthTab defaults to the global ai:/auth 195 // link; repoint its instance at this repo's auth route so it stays local. 196 { 197 var auth_tab = try AuthTab.View.init(allocator, session); 198 errdefer auth_tab.deinit(allocator); 199 auth_tab.text_box.getFocus().kind = .{ .custom = auth_link }; 200 try tab_ids.put(allocator, auth_tab.getFocus().id, {}); 201 if (std.mem.eql(u8, auth_link, current_link)) selected_tab = auth_tab.getFocus().id; 202 try box.children.put(allocator, auth_tab.getFocus().id, .{ 203 .widget = .{ .auth_tab = auth_tab }, 204 .rect = null, 205 .min_size = .{ .width = AuthTab.min_width, .height = null }, 206 }); 207 } 208 209 // quit tab 210 if (session.is_terminal) { 211 var text_box = try wgt.TextBox(ui.Widget).init(allocator, ui.Quit.tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 212 errdefer text_box.deinit(allocator); 213 text_box.getFocus().focusable = true; 214 text_box.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 215 try tab_ids.put(allocator, text_box.getFocus().id, {}); 216 try box.children.put(allocator, text_box.getFocus().id, .{ 217 .widget = .{ .text_box = text_box }, 218 .rect = null, 219 // the label is a single column; +2 for the border 220 .min_size = .{ .width = 1 + 2, .height = null }, 221 }); 222 } 223 224 var self = View{ .box = box, .data = data, .tab_ids = tab_ids }; 225 self.getFocus().child_id = selected_tab orelse self.tab_ids.keys()[0]; 226 return self; 227 } 228 229 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 230 self.box.deinit(allocator); 231 self.tab_ids.deinit(allocator); 232 } 233 234 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 235 self.clearGrid(); 236 // only the selected tab shows its border 237 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 238 const tb: ?*wgt.TextBox(ui.Widget) = switch (child.widget) { 239 .text_box => |*x| x, 240 .auth_tab => |*at| &at.text_box, 241 else => null, 242 }; 243 if (tb) |t| { 244 t.options.border_style = if (self.getFocus().child_id == id) .single else .hidden; 245 } 246 } 247 try self.box.build(allocator, constraint, root_focus); 248 } 249 250 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 251 _ = allocator; 252 const current_tab = self.currentTabIndex() orelse return; 253 var new_tab = current_tab; 254 switch (key) { 255 .arrow_left => new_tab -|= 1, 256 .arrow_right => if (new_tab + 1 < self.tab_ids.count()) { 257 new_tab += 1; 258 }, 259 else => {}, 260 } 261 if (new_tab != current_tab) { 262 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 263 } 264 } 265 266 pub fn clearGrid(self: *View) void { 267 self.box.clearGrid(); 268 } 269 270 pub fn getGrid(self: View) ?Grid { 271 return self.box.getGrid(); 272 } 273 274 pub fn getFocus(self: *View) *Focus { 275 return self.box.getFocus(); 276 } 277 278 pub fn getSelectedIndex(self: View) ?usize { 279 return self.currentTabIndex(); 280 } 281 282 fn currentTabIndex(self: View) ?usize { 283 const child_id = self.box.focus.child_id orelse return null; 284 return self.tab_ids.getIndex(child_id); 285 } 286 }; 287