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