╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
51 // spacer 52 { 53 var text = try wgt.Text(ui.Widget).init(allocator, " "); 54 errdefer text.deinit(allocator); 55 try box.children.put(allocator, text.getFocus().id, .{ 56 .widget = .{ .text = text }, 57 .rect = null, 58 .min_size = .{ .width = 1, .height = null }, 59 }); 60 } 61 62 // the tab matching the current page is focused initially; matching by 63 // link (rather than position) keeps this robust to tab changes. 64 const current_link: []const u8 = switch (session.data.current_page) { 65 .home_repos => "ai:/repos", 66 .home_settings => "ai:/settings", 67 .home_auth => "ai:/auth", 68 else => "ai:/users", 69 }; 70 var selected_tab: ?usize = null; 71 72 // tabs 73 for ( 74 [_][]const u8{ "users", "repos", "", "settings", "" }, 75 [_][]const u8{ "ai:/users", "ai:/repos", "", "ai:/settings", "ai:/auth" }, 76 ) |name, focus_name| { 77 // spacer 78 if (focus_name.len == 0) { 79 var spacer = try ui.Spacer.init(allocator); 80 errdefer spacer.deinit(allocator); 81 try box.children.put(allocator, spacer.getFocus().id, .{ 82 .widget = .{ .spacer = spacer }, 83 .rect = null, 84 .min_size = null, 85 }); 86 } 87 // auth 88 else if (std.mem.eql(u8, "ai:/auth", focus_name)) { 89 var auth_tab = try AuthTab.View.init(allocator, session); 90 errdefer auth_tab.deinit(allocator); 91 try tab_ids.put(allocator, auth_tab.getFocus().id, {}); 92 if (std.mem.eql(u8, focus_name, current_link)) selected_tab = auth_tab.getFocus().id; 93 try box.children.put(allocator, auth_tab.getFocus().id, .{ 94 .widget = .{ .auth_tab = auth_tab }, 95 .rect = null, 96 .min_size = .{ .width = AuthTab.min_width, .height = null }, 97 }); 98 } 99 // other tabs 100 else { 101 var text_box = try wgt.TextBox(ui.Widget).init(allocator, name, .{ .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 = focus_name }; 105 try tab_ids.put(allocator, text_box.getFocus().id, {}); 106 if (std.mem.eql(u8, focus_name, current_link)) selected_tab = text_box.getFocus().id; 107 try box.children.put(allocator, text_box.getFocus().id, .{ 108 .widget = .{ .text_box = text_box }, 109 .rect = null, 110 .min_size = .{ .width = name.len + 2, .height = null }, 111 }); 112 } 113 } 114 115 // quit tab 116 if (session.is_terminal) { 117 var text_box = try wgt.TextBox(ui.Widget).init(allocator, ui.Quit.tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 118 errdefer text_box.deinit(allocator); 119 text_box.getFocus().focusable = true; 120 text_box.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 121 try tab_ids.put(allocator, text_box.getFocus().id, {}); 122 try box.children.put(allocator, text_box.getFocus().id, .{ 123 .widget = .{ .text_box = text_box }, 124 .rect = null, 125 // the label is a single column; +2 for the border 126 .min_size = .{ .width = 1 + 2, .height = null }, 127 }); 128 } 129 130 var self = View{ .box = box, .data = data, .tab_ids = tab_ids }; 131 self.getFocus().child_id = selected_tab orelse self.tab_ids.keys()[0]; 132 return self; 133 } 134 135 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 136 self.box.deinit(allocator); 137 self.tab_ids.deinit(allocator); 138 } 139 140 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 141 self.clearGrid(); 142 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 143 const tb: ?*wgt.TextBox(ui.Widget) = switch (child.widget) { 144 .text_box => |*x| x, 145 .auth_tab => |*at| &at.text_box, 146 else => null, 147 }; 148 if (tb) |t| { 149 t.options.border_style = if (self.getFocus().child_id == id) .single else .hidden; 150 } 151 } 152 try self.box.build(allocator, constraint, root_focus); 153 } 154 155 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 156 _ = allocator; 157 const current_tab = self.currentTabIndex() orelse return; 158 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 159 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 160 } 161 } 162 163 pub fn clearGrid(self: *View) void { 164 self.box.clearGrid(); 165 } 166 167 pub fn getGrid(self: View) ?Grid { 168 return self.box.getGrid(); 169 } 170 171 pub fn getFocus(self: *View) *Focus { 172 return self.box.getFocus(); 173 } 174 175 pub fn getSelectedIndex(self: View) ?usize { 176 return self.currentTabIndex(); 177 } 178 179 fn currentTabIndex(self: View) ?usize { 180 const child_id = self.box.focus.child_id orelse return null; 181 return self.tab_ids.getIndex(child_id); 182 } 183 }; 184