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