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