╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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 = tabs_box.getFocus().child_id; 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 else => null, 253 }; 254 if (tb) |t| ui.widget.markSelected(t, selected_tab == id); 255 tabs_width += if (child.min_size) |min_size| min_size.width orelse 0 else 0; 256 } 257 258 // the outer box's hidden border occupies two columns. keep the title 259 // and tabs together if they fit; otherwise tab strip on its own row. 260 const viewport_width = constraint.max_size.width orelse constraint.min_size.width; 261 const content_width = if (viewport_width) |width| width -| 2 else null; 262 const back_width: usize = if (back_visible) ui.widget.back_button_width else 0; 263 const wrap = if (content_width) |width| self.first_group_width + back_width + tabs_width > width else false; 264 box.options.direction = if (wrap) .vert else .horiz; 265 tabs_child.min_size = if (wrap) .{ .width = content_width, .height = null } else null; 266 (box.children.getPtr(self.gap_id) orelse unreachable).hidden = !wrap; 267 268 var scroll_constraint = constraint; 269 scroll_constraint.min_size.width = viewport_width; 270 try self.scroll.build(allocator, scroll_constraint, root_focus); 271 } 272 273 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 274 _ = allocator; 275 const current_tab = self.currentTabIndex() orelse return; 276 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 277 const tab_id = self.tab_ids.keys()[new_tab]; 278 root_focus.setFocus(tab_id); 279 const tabs_child = self.tabsChild(); 280 const tabs_rect = tabs_child.rect orelse return; 281 const tab = tabs_child.widget.box.children.get(tab_id) orelse return; 282 var rect = tab.rect orelse return; 283 rect.x += tabs_rect.x; 284 rect.y += tabs_rect.y; 285 self.scroll.scrollToRect(rect); 286 } 287 } 288 289 pub fn clearGrid(self: *View) void { 290 self.scroll.clearGrid(); 291 } 292 293 pub fn getGrid(self: View) ?Grid { 294 return self.scroll.getGrid(); 295 } 296 297 pub fn getFocus(self: *View) *Focus { 298 return self.scroll.getFocus(); 299 } 300 301 pub fn getSelectedIndex(self: View) ?usize { 302 return self.currentTabIndex(); 303 } 304 305 fn currentTabIndex(self: View) ?usize { 306 if (self.scroll.child.box.focus.child_id != self.tabs_id) return null; 307 const child_id = self.tabsChild().widget.box.focus.child_id orelse return null; 308 return self.tab_ids.getIndex(child_id); 309 } 310 311 fn tabsChild(self: *const View) *wgt.Box(ui.Widget).Child { 312 return self.scroll.child.box.children.getPtr(self.tabs_id) orelse unreachable; 313 } 314 }; 315