╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
205 } 206 207 tabs_box.getFocus().child_id = selected_tab orelse tab_ids.keys()[0]; 208 const title_id = title_box.getFocus().id; 209 const tabs_id = tabs_box.getFocus().id; 210 try box.children.put(allocator, title_id, .{ .widget = .{ .box = title_box }, .rect = null, .min_size = null }); 211 title_box_owned = true; 212 // a blank row between the title and the tabs, shown only when they wrap 213 const gap_id = blk: { 214 var gap = try wgt.Text.init(allocator, " "); 215 errdefer gap.deinit(allocator); 216 const id = gap.getFocus().id; 217 try box.children.put(allocator, id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null, .hidden = true }); 218 break :blk id; 219 }; 220 try box.children.put(allocator, tabs_id, .{ .widget = .{ .box = tabs_box }, .rect = null, .min_size = null }); 221 tabs_box_owned = true; 222 box.getFocus().child_id = tabs_id; 223 224 return .{ 225 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = box }, .{ .direction = .horiz, .show_bar = false, .web_native = !session.is_terminal }), 226 .tab_ids = tab_ids, 227 .tabs_id = tabs_id, 228 .gap_id = gap_id, 229 .first_group_width = first_group_width, 230 .session = session, 231 }; 232 } 233 234 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 235 self.scroll.deinit(allocator); 236 self.tab_ids.deinit(allocator); 237 } 238 239 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 240 self.clearGrid(); 241 const box = &self.scroll.child.box; 242 const back_visible = self.session.back == .available; 243 ui.widget.setBackButtonVisible(box, back_visible); 244 const tabs_child = self.tabsChild(); 245 const tabs_box = &tabs_child.widget.box; 246 247 // only the selected tab shows its border 248 const selected_tab = tabs_box.getFocus().child_id; 249 var tabs_width: usize = 0; 250 for (tabs_box.children.keys(), tabs_box.children.values()) |id, *child| { 251 const tb: ?*wgt.TextBox = switch (child.widget) { 252 .text_box => |*x| x, 253 else => null, 254 }; 255 if (tb) |t| ui.widget.markSelected(t, selected_tab == id); 256 tabs_width += if (child.min_size) |min_size| min_size.width orelse 0 else 0; 257 } 258 259 // the outer box's hidden border occupies two columns. keep the title 260 // and tabs together if they fit; otherwise tab strip on its own row. 261 const viewport_width = constraint.max_size.width orelse constraint.min_size.width; 262 const content_width = if (viewport_width) |width| width -| 2 else null; 263 const back_width: usize = if (back_visible) ui.widget.back_button_width else 0; 264 const wrap = if (content_width) |width| self.first_group_width + back_width + tabs_width > width else false; 265 box.options.direction = if (wrap) .vert else .horiz; 266 tabs_child.min_size = if (wrap) .{ .width = content_width, .height = null } else null; 267 (box.children.getPtr(self.gap_id) orelse unreachable).hidden = !wrap; 268 269 var scroll_constraint = constraint; 270 scroll_constraint.min_size.width = viewport_width; 271 try self.scroll.build(allocator, scroll_constraint, root_focus); 272 } 273 274 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 275 _ = allocator; 276 const current_tab = self.currentTabIndex() orelse return; 277 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 278 const tab_id = self.tab_ids.keys()[new_tab]; 279 root_focus.setFocus(tab_id); 280 const tabs_child = self.tabsChild(); 281 const tabs_rect = tabs_child.rect orelse return; 282 const tab = tabs_child.widget.box.children.get(tab_id) orelse return; 283 var rect = tab.rect orelse return; 284 rect.x += tabs_rect.x; 285 rect.y += tabs_rect.y; 286 self.scroll.scrollToRect(rect); 287 } 288 } 289 290 pub fn clearGrid(self: *View) void { 291 self.scroll.clearGrid(); 292 } 293 294 pub fn getGrid(self: View) ?Grid { 295 return self.scroll.getGrid(); 296 } 297 298 pub fn getFocus(self: *View) *Focus { 299 return self.scroll.getFocus(); 300 } 301 302 pub fn getSelectedIndex(self: View) ?usize { 303 return self.currentTabIndex(); 304 } 305 306 fn currentTabIndex(self: View) ?usize { 307 if (self.scroll.child.box.focus.child_id != self.tabs_id) return null; 308 const child_id = self.tabsChild().widget.box.focus.child_id orelse return null; 309 return self.tab_ids.getIndex(child_id); 310 } 311 312 fn tabsChild(self: *const View) *wgt.Box(ui.Widget).Child { 313 return self.scroll.child.box.children.getPtr(self.tabs_id) orelse unreachable; 314 } 315 }; 316