╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
127 try tab_ids.put(allocator, text_box.getFocus().id, {}); 128 if (std.mem.eql(u8, forks_link, selected_link)) selected_tab = text_box.getFocus().id; 129 try box.children.put(allocator, text_box.getFocus().id, .{ 130 .widget = .{ .text_box = text_box }, 131 .rect = null, 132 .min_size = .{ .width = forks_tab_label.len + 2, .height = null }, 133 }); 134 } 135 136 // spacer pushes settings + auth to the right 137 { 138 var spacer = try ui.widget.Spacer.init(allocator); 139 errdefer spacer.deinit(allocator); 140 try box.children.put(allocator, spacer.getFocus().id, .{ 141 .widget = .{ .spacer = spacer }, 142 .rect = null, 143 .min_size = null, 144 .flex = .grow, 145 }); 146 } 147 148 // settings tab. settings are account preferences, so it needs a login. 149 if (session.data.user_id != null) { 150 var text_box = try wgt.TextBox.init(allocator, settings_tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 151 errdefer text_box.deinit(allocator); 152 text_box.getFocus().mode = .all; 153 text_box.getFocus().kind = .{ .custom = settings_link }; 154 try tab_ids.put(allocator, text_box.getFocus().id, {}); 155 if (std.mem.eql(u8, settings_link, selected_link)) selected_tab = text_box.getFocus().id; 156 try box.children.put(allocator, text_box.getFocus().id, .{ 157 .widget = .{ .text_box = text_box }, 158 .rect = null, 159 .min_size = .{ .width = settings_tab_label.len + 2, .height = null }, 160 }); 161 } 162 163 // auth tab (login / logout). AuthTab defaults to the global ai:/auth 164 // link; repoint its instance at this user's auth route so it stays local. 165 { 166 var auth_tab = try AuthTab.View.init(allocator, session); 167 errdefer auth_tab.deinit(allocator); 168 auth_tab.text_box.getFocus().kind = .{ .custom = auth_link }; 169 try tab_ids.put(allocator, auth_tab.getFocus().id, {}); 170 if (std.mem.eql(u8, auth_link, selected_link)) selected_tab = auth_tab.getFocus().id; 171 try box.children.put(allocator, auth_tab.getFocus().id, .{ 172 .widget = .{ .auth_tab = auth_tab }, 173 .rect = null, 174 .min_size = .{ .width = auth_tab.minWidth(), .height = null }, 175 }); 176 } 177 178 // quit tab 179 if (session.is_terminal) { 180 var text_box = try wgt.TextBox.init(allocator, ui.Quit.tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 181 errdefer text_box.deinit(allocator); 182 text_box.getFocus().mode = .all; 183 text_box.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 184 try tab_ids.put(allocator, text_box.getFocus().id, {}); 185 try box.children.put(allocator, text_box.getFocus().id, .{ 186 .widget = .{ .text_box = text_box }, 187 .rect = null, 188 // the label is a single column; +2 for the border 189 .min_size = .{ .width = 1 + 2, .height = null }, 190 }); 191 } 192 193 var self = View{ 194 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = box }, .{ .direction = .horiz, .show_bar = false, .web_native = !session.is_terminal }), 195 .tab_ids = tab_ids, 196 .session = session, 197 }; 198 self.getFocus().child_id = selected_tab orelse self.tab_ids.keys()[0]; 199 return self; 200 } 201 202 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 203 self.scroll.deinit(allocator); 204 self.tab_ids.deinit(allocator); 205 } 206 207 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 208 self.clearGrid(); 209 // only the selected tab shows its border 210 const box = &self.scroll.child.box; 211 ui.widget.setBackButtonVisible(box, self.session.back == .available); 212 for (box.children.keys(), box.children.values()) |id, *child| { 213 const tb: ?*wgt.TextBox = switch (child.widget) { 214 .text_box => |*x| x, 215 .auth_tab => |*at| blk: { 216 // the label tracks login state per frame, so the width must too 217 child.min_size = .{ .width = at.minWidth(), .height = null }; 218 break :blk &at.text_box; 219 }, 220 else => null, 221 }; 222 if (tb) |t| { 223 t.options.border_style = if (self.getFocus().child_id == id) .single else .hidden; 224 } 225 } 226 var scroll_constraint = constraint; 227 scroll_constraint.min_size.width = constraint.max_size.width orelse constraint.min_size.width; 228 try self.scroll.build(allocator, scroll_constraint, root_focus); 229 } 230 231 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 232 _ = allocator; 233 const current_tab = self.currentTabIndex() orelse return; 234 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 235 const tab_id = self.tab_ids.keys()[new_tab]; 236 root_focus.setFocus(tab_id); 237 const tab = self.scroll.child.box.children.get(tab_id) orelse return; 238 if (tab.rect) |rect| self.scroll.scrollToRect(rect); 239 } 240 } 241 242 pub fn clearGrid(self: *View) void { 243 self.scroll.clearGrid(); 244 } 245 246 pub fn getGrid(self: View) ?Grid { 247 return self.scroll.getGrid(); 248 } 249 250 pub fn getFocus(self: *View) *Focus { 251 return self.scroll.getFocus(); 252 } 253 254 pub fn getSelectedIndex(self: View) ?usize { 255 return self.currentTabIndex(); 256 } 257 258 fn currentTabIndex(self: View) ?usize { 259 const child_id = self.scroll.child.box.focus.child_id orelse return null; 260 return self.tab_ids.getIndex(child_id); 261 } 262 }; 263