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