╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
132 } 133 134 // forks tab 135 { 136 var text_box = try wgt.TextBox.init(allocator, forks_tab_label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 137 errdefer text_box.deinit(allocator); 138 text_box.getFocus().mode = .all; 139 text_box.getFocus().kind = .{ .custom = forks_link }; 140 try tab_ids.put(allocator, text_box.getFocus().id, {}); 141 if (std.mem.eql(u8, forks_link, selected_link)) selected_tab = text_box.getFocus().id; 142 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 143 .widget = .{ .text_box = text_box }, 144 .rect = null, 145 .min_size = .{ .width = forks_tab_label.len + 2, .height = null }, 146 }); 147 } 148 149 // spacer pushes new repo and auth to the right 150 { 151 var spacer = try ui.widget.Spacer.init(allocator); 152 errdefer spacer.deinit(allocator); 153 try tabs_box.children.put(allocator, spacer.getFocus().id, .{ 154 .widget = .{ .spacer = spacer }, 155 .rect = null, 156 .min_size = null, 157 .flex = .grow, 158 }); 159 } 160 161 // new repo tab. repos are created for the logged-in user. 162 if (session.data.user_id != null) { 163 var text_box = try wgt.TextBox.init(allocator, new_repo_tab_label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = new_repo_tab_bottom_label }); 164 errdefer text_box.deinit(allocator); 165 text_box.getFocus().mode = .all; 166 text_box.getFocus().kind = .{ .custom = new_repo_link }; 167 try tab_ids.put(allocator, text_box.getFocus().id, {}); 168 if (std.mem.eql(u8, new_repo_link, selected_link)) selected_tab = text_box.getFocus().id; 169 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 170 .widget = .{ .text_box = text_box }, 171 .rect = null, 172 .min_size = .{ .width = @max(new_repo_tab_label.len, new_repo_tab_bottom_label.len) + 2, .height = null }, 173 }); 174 } 175 176 // auth tab (login / logout). AuthTab defaults to the global ai:/auth 177 // link; repoint its instance at this user's auth route so it stays local. 178 { 179 var auth_tab = try AuthTab.View.init(allocator, session); 180 errdefer auth_tab.deinit(allocator); 181 auth_tab.text_box.getFocus().kind = .{ .custom = auth_link }; 182 try tab_ids.put(allocator, auth_tab.getFocus().id, {}); 183 if (std.mem.eql(u8, auth_link, selected_link)) selected_tab = auth_tab.getFocus().id; 184 try tabs_box.children.put(allocator, auth_tab.getFocus().id, .{ 185 .widget = .{ .auth_tab = auth_tab }, 186 .rect = null, 187 .min_size = .{ .width = auth_tab.minWidth(), .height = null }, 188 }); 189 } 190 191 // quit tab 192 if (session.is_terminal) { 193 var text_box = try wgt.TextBox.init(allocator, ui.Quit.tab_label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 194 errdefer text_box.deinit(allocator); 195 text_box.getFocus().mode = .all; 196 text_box.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 197 try tab_ids.put(allocator, text_box.getFocus().id, {}); 198 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 199 .widget = .{ .text_box = text_box }, 200 .rect = null, 201 // the label is a single column; +2 for the border 202 .min_size = .{ .width = 1 + 2, .height = null }, 203 }); 204 } 205 206 tabs_box.getFocus().child_id = selected_tab orelse tab_ids.keys()[0]; 207 const title_id = title_box.getFocus().id; 208 const tabs_id = tabs_box.getFocus().id; 209 try box.children.put(allocator, title_id, .{ .widget = .{ .box = title_box }, .rect = null, .min_size = null }); 210 title_box_owned = true; 211 // a blank row between the title and the tabs, shown only when they wrap 212 const gap_id = blk: { 213 var gap = try wgt.Text.init(allocator, " "); 214 errdefer gap.deinit(allocator); 215 const id = gap.getFocus().id; 216 try box.children.put(allocator, id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null, .hidden = true }); 217 break :blk id; 218 }; 219 try box.children.put(allocator, tabs_id, .{ .widget = .{ .box = tabs_box }, .rect = null, .min_size = null }); 220 tabs_box_owned = true; 221 box.getFocus().child_id = tabs_id; 222 223 return .{ 224 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = box }, .{ .direction = .horiz, .show_bar = false, .web_native = !session.is_terminal }), 225 .tab_ids = tab_ids, 226 .tabs_id = tabs_id, 227 .gap_id = gap_id, 228 .first_group_width = first_group_width, 229 .session = session, 230 }; 231 } 232 233 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 234 self.scroll.deinit(allocator); 235 self.tab_ids.deinit(allocator); 236 } 237 238 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 239 self.clearGrid(); 240 const box = &self.scroll.child.box; 241 const back_visible = self.session.back == .available; 242 ui.widget.setBackButtonVisible(box, back_visible); 243 const tabs_child = self.tabsChild(); 244 const tabs_box = &tabs_child.widget.box; 245 246 // only the selected tab shows its border 247 const selected_tab = if (box.getFocus().child_id == self.tabs_id) tabs_box.getFocus().child_id else null; 248 var tabs_width: usize = 0; 249 for (tabs_box.children.keys(), tabs_box.children.values()) |id, *child| { 250 const tb: ?*wgt.TextBox = switch (child.widget) { 251 .text_box => |*x| x, 252 .auth_tab => |*at| blk: { 253 // the label tracks login state per frame, so the width must too 254 child.min_size = .{ .width = at.minWidth(), .height = null }; 255 break :blk &at.text_box; 256 }, 257 else => null, 258 }; 259 if (tb) |t| ui.widget.markSelected(t, selected_tab == id); 260 tabs_width += if (child.min_size) |min_size| min_size.width orelse 0 else 0; 261 } 262 263 // the outer box's hidden border occupies two columns. keep the title 264 // and tabs together if they fit; otherwise tab strip on its own row. 265 const viewport_width = constraint.max_size.width orelse constraint.min_size.width; 266 const content_width = if (viewport_width) |width| width -| 2 else null; 267 const back_width: usize = if (back_visible) ui.widget.back_button_width else 0; 268 const wrap = if (content_width) |width| self.first_group_width + back_width + tabs_width > width else false; 269 box.options.direction = if (wrap) .vert else .horiz; 270 tabs_child.min_size = if (wrap) .{ .width = content_width, .height = null } else null; 271 (box.children.getPtr(self.gap_id) orelse unreachable).hidden = !wrap; 272 273 var scroll_constraint = constraint; 274 scroll_constraint.min_size.width = viewport_width; 275 try self.scroll.build(allocator, scroll_constraint, root_focus); 276 } 277 278 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 279 _ = allocator; 280 const current_tab = self.currentTabIndex() orelse return; 281 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 282 const tab_id = self.tab_ids.keys()[new_tab]; 283 root_focus.setFocus(tab_id); 284 const tabs_child = self.tabsChild(); 285 const tabs_rect = tabs_child.rect orelse return; 286 const tab = tabs_child.widget.box.children.get(tab_id) orelse return; 287 var rect = tab.rect orelse return; 288 rect.x += tabs_rect.x; 289 rect.y += tabs_rect.y; 290 self.scroll.scrollToRect(rect); 291 } 292 } 293 294 pub fn clearGrid(self: *View) void { 295 self.scroll.clearGrid(); 296 } 297 298 pub fn getGrid(self: View) ?Grid { 299 return self.scroll.getGrid(); 300 } 301 302 pub fn getFocus(self: *View) *Focus { 303 return self.scroll.getFocus(); 304 } 305 306 pub fn getSelectedIndex(self: View) ?usize { 307 return self.currentTabIndex(); 308 } 309 310 fn currentTabIndex(self: View) ?usize { 311 if (self.scroll.child.box.focus.child_id != self.tabs_id) return null; 312 const child_id = self.tabsChild().widget.box.focus.child_id orelse return null; 313 return self.tab_ids.getIndex(child_id); 314 } 315 316 fn tabsChild(self: *const View) *wgt.Box(ui.Widget).Child { 317 return self.scroll.child.box.children.getPtr(self.tabs_id) orelse unreachable; 318 } 319 }; 320