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