╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
195 }; 196 try box.children.put(allocator, tabs_id, .{ .widget = .{ .box = tabs_box }, .rect = null, .min_size = null }); 197 tabs_box_owned = true; 198 box.getFocus().child_id = tabs_id; 199 200 return .{ 201 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = box }, .{ .direction = .horiz, .show_bar = false, .web_native = !session.is_terminal }), 202 .tab_ids = tab_ids, 203 .tabs_id = tabs_id, 204 .gap_id = gap_id, 205 .first_group_width = first_group_width, 206 .session = session, 207 }; 208 } 209 210 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 211 self.scroll.deinit(allocator); 212 self.tab_ids.deinit(allocator); 213 } 214 215 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 216 self.clearGrid(); 217 const box = &self.scroll.child.box; 218 const back_visible = self.session.back == .available; 219 ui.widget.setBackButtonVisible(box, back_visible); 220 const tabs_child = self.tabsChild(); 221 const tabs_box = &tabs_child.widget.box; 222 223 const selected_tab = if (box.getFocus().child_id == self.tabs_id) tabs_box.getFocus().child_id else null; 224 var tabs_width: usize = 0; 225 for (tabs_box.children.keys(), tabs_box.children.values()) |id, *child| { 226 const text_box: ?*wgt.TextBox = switch (child.widget) { 227 .text_box => |*value| value, 228 .auth_tab => |*auth_tab| blk: { 229 child.min_size = .{ .width = auth_tab.minWidth(), .height = null }; 230 break :blk &auth_tab.text_box; 231 }, 232 else => null, 233 }; 234 if (text_box) |tab| ui.widget.markSelected(tab, selected_tab == id); 235 tabs_width += if (child.min_size) |min_size| min_size.width orelse 0 else 0; 236 } 237 238 const viewport_width = constraint.max_size.width orelse constraint.min_size.width; 239 const content_width = if (viewport_width) |width| width -| 2 else null; 240 const back_width: usize = if (back_visible) ui.widget.back_button_width else 0; 241 const wrap = if (content_width) |width| self.first_group_width + back_width + tabs_width > width else false; 242 box.options.direction = if (wrap) .vert else .horiz; 243 tabs_child.min_size = if (wrap) .{ .width = content_width, .height = null } else null; 244 (box.children.getPtr(self.gap_id) orelse unreachable).hidden = !wrap; 245 246 var scroll_constraint = constraint; 247 scroll_constraint.min_size.width = viewport_width; 248 try self.scroll.build(allocator, scroll_constraint, root_focus); 249 } 250 251 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 252 _ = allocator; 253 const current_tab = self.currentTabIndex() orelse return; 254 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 255 const tab_id = self.tab_ids.keys()[new_tab]; 256 root_focus.setFocus(tab_id); 257 const tabs_child = self.tabsChild(); 258 const tabs_rect = tabs_child.rect orelse return; 259 const tab = tabs_child.widget.box.children.get(tab_id) orelse return; 260 var rect = tab.rect orelse return; 261 rect.x += tabs_rect.x; 262 rect.y += tabs_rect.y; 263 self.scroll.scrollToRect(rect); 264 } 265 } 266 267 pub fn clearGrid(self: *View) void { 268 self.scroll.clearGrid(); 269 } 270 271 pub fn getGrid(self: View) ?Grid { 272 return self.scroll.getGrid(); 273 } 274 275 pub fn getFocus(self: *View) *Focus { 276 return self.scroll.getFocus(); 277 } 278 279 pub fn getSelectedIndex(self: View) ?usize { 280 return self.currentTabIndex(); 281 } 282 283 fn currentTabIndex(self: View) ?usize { 284 if (self.scroll.child.box.focus.child_id != self.tabs_id) return null; 285 const child_id = self.tabsChild().widget.box.focus.child_id orelse return null; 286 return self.tab_ids.getIndex(child_id); 287 } 288 289 fn tabsChild(self: *const View) *wgt.Box(ui.Widget).Child { 290 return self.scroll.child.box.children.getPtr(self.tabs_id) orelse unreachable; 291 } 292 }; 293