╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
149 try box.children.put(allocator, text_box.getFocus().id, .{ 150 .widget = .{ .text_box = text_box }, 151 .rect = null, 152 // the label is a single column; +2 for the border 153 .min_size = .{ .width = 1 + 2, .height = null }, 154 }); 155 } 156 157 var self = View{ .box = box, .data = data, .tab_ids = tab_ids }; 158 self.getFocus().child_id = selected_tab orelse self.tab_ids.keys()[0]; 159 return self; 160 } 161 162 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 163 self.box.deinit(allocator); 164 self.tab_ids.deinit(allocator); 165 } 166 167 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 168 self.clearGrid(); 169 // only the selected tab shows its border 170 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 171 const tb: ?*wgt.TextBox(ui.Widget) = switch (child.widget) { 172 .text_box => |*x| x, 173 .auth_tab => |*at| &at.text_box, 174 else => null, 175 }; 176 if (tb) |t| { 177 t.options.border_style = if (self.getFocus().child_id == id) .single else .hidden; 178 } 179 } 180 try self.box.build(allocator, constraint, root_focus); 181 } 182 183 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 184 _ = allocator; 185 const current_tab = self.currentTabIndex() orelse return; 186 var new_tab = current_tab; 187 switch (key) { 188 .arrow_left => new_tab -|= 1, 189 .arrow_right => if (new_tab + 1 < self.tab_ids.count()) { 190 new_tab += 1; 191 }, 192 else => {}, 193 } 194 if (new_tab != current_tab) { 195 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 196 } 197 } 198 199 pub fn clearGrid(self: *View) void { 200 self.box.clearGrid(); 201 } 202 203 pub fn getGrid(self: View) ?Grid { 204 return self.box.getGrid(); 205 } 206 207 pub fn getFocus(self: *View) *Focus { 208 return self.box.getFocus(); 209 } 210 211 pub fn getSelectedIndex(self: View) ?usize { 212 return self.currentTabIndex(); 213 } 214 215 fn currentTabIndex(self: View) ?usize { 216 const child_id = self.box.focus.child_id orelse return null; 217 return self.tab_ids.getIndex(child_id); 218 } 219 }; 220