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