╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
79 const current_link: []const u8 = switch (session.data.current_page) { 80 .user_settings => settings_link, 81 .user_auth => auth_link, 82 else => repos_link, 83 }; 84 var selected_tab: ?usize = null; 85 86 // repos tab 87 { 88 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "repos", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 89 errdefer text_box.deinit(allocator); 90 text_box.getFocus().focusable = true; 91 text_box.getFocus().kind = .{ .custom = repos_link }; 92 try tab_ids.put(allocator, text_box.getFocus().id, {}); 93 if (std.mem.eql(u8, repos_link, current_link)) selected_tab = text_box.getFocus().id; 94 try box.children.put(allocator, text_box.getFocus().id, .{ 95 .widget = .{ .text_box = text_box }, 96 .rect = null, 97 .min_size = .{ .width = "repos".len + 2, .height = null }, 98 }); 99 } 100 101 // spacer pushes settings + auth to the right 102 { 103 var spacer = try ui.Spacer.init(allocator); 104 errdefer spacer.deinit(allocator); 105 try box.children.put(allocator, spacer.getFocus().id, .{ 106 .widget = .{ .spacer = spacer }, 107 .rect = null, 108 .min_size = null, 109 }); 110 } 111 112 // settings tab 113 { 114 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "settings", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 115 errdefer text_box.deinit(allocator); 116 text_box.getFocus().focusable = true; 117 text_box.getFocus().kind = .{ .custom = settings_link }; 118 try tab_ids.put(allocator, text_box.getFocus().id, {}); 119 if (std.mem.eql(u8, settings_link, current_link)) selected_tab = text_box.getFocus().id; 120 try box.children.put(allocator, text_box.getFocus().id, .{ 121 .widget = .{ .text_box = text_box }, 122 .rect = null, 123 .min_size = .{ .width = "settings".len + 2, .height = null }, 124 }); 125 } 126 127 // auth tab (login / logout). AuthTab defaults to the global ai:/auth 128 // link; repoint its instance at this user's auth route so it stays local. 129 { 130 var auth_tab = try AuthTab.View.init(allocator, &data.auth_tab, session); 131 errdefer auth_tab.deinit(allocator); 132 auth_tab.text_box.getFocus().kind = .{ .custom = auth_link }; 133 try tab_ids.put(allocator, auth_tab.getFocus().id, {}); 134 if (std.mem.eql(u8, auth_link, current_link)) selected_tab = auth_tab.getFocus().id; 135 try box.children.put(allocator, auth_tab.getFocus().id, .{ 136 .widget = .{ .auth_tab = auth_tab }, 137 .rect = null, 138 .min_size = .{ .width = AuthTab.min_width, .height = null }, 139 }); 140 } 141 142 // quit tab 143 if (session.is_terminal) { 144 var text_box = try wgt.TextBox(ui.Widget).init(allocator, ui.Quit.tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 145 errdefer text_box.deinit(allocator); 146 text_box.getFocus().focusable = true; 147 text_box.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 148 try tab_ids.put(allocator, text_box.getFocus().id, {}); 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