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