╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const ui = @import("../../ui.zig"); 3 const xit = @import("xit"); 4 const xitui = xit.xitui; 5 const wgt = xitui.widget; 6 const layout = xitui.layout; 7 const inp = xitui.input; 8 const Grid = xitui.grid.Grid; 9 const Focus = xitui.focus.Focus; 10 11 pub const AuthTab = @import("./../AuthTab.zig"); 12 13 name: []const u8, 14 title: ui.Title, 15 auth_tab: AuthTab, 16 17 const Self = @This(); 18 19 pub fn init(arena: *std.heap.ArenaAllocator, name: []const u8) !Self { 20 return .{ 21 .name = name, 22 .title = try ui.Title.init(arena, name), 23 .auth_tab = AuthTab.init(), 24 }; 25 } 26 27 pub const View = struct { 28 box: wgt.Box(ui.Widget), 29 data: *const Self, 30 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 31 32 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 33 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .hidden, .rounded_corners = true, .direction = .horiz }); 34 errdefer box.deinit(allocator); 35 36 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 37 errdefer tab_ids.deinit(allocator); 38 39 // title sits to the left of the tabs 40 { 41 var title_view = try ui.Title.View.init(allocator, &data.title); 42 errdefer title_view.deinit(allocator); 43 title_view.getFocus().focusable = true; 44 title_view.getFocus().kind = .{ .custom = "a:/" }; 45 // shrink the title when there is not enough space 46 try box.children.put(allocator, title_view.getFocus().id, .{ 47 .widget = .{ .title = title_view }, 48 .rect = null, 49 .min_size = null, 50 .shrink = true, 51 }); 52 } 53 54 // spacer 55 { 56 var text = try wgt.Text(ui.Widget).init(allocator, " "); 57 errdefer text.deinit(allocator); 58 try box.children.put(allocator, text.getFocus().id, .{ 59 .widget = .{ .text = text }, 60 .rect = null, 61 .min_size = .{ .width = 1, .height = null }, 62 }); 63 } 64 65 // every tab link is user-scoped so selecting one stays on this page — 66 // switching its stack and updating the url — instead of navigating to 67 // the global settings or auth pages. allocated in the page arena so the 68 // focus kinds that borrow them live as long as this page's widget tree. 69 const aa = session.page_arena.allocator(); 70 const repos_link = try std.fmt.allocPrint(aa, "a:/user/{s}", .{data.name}); 71 const settings_link = try std.fmt.allocPrint(aa, "a:/user/{s}/settings", .{data.name}); 72 const auth_link = try std.fmt.allocPrint(aa, "a:/user/{s}/auth", .{data.name}); 73 74 // the tab matching the current page is focused initially; matching by 75 // link (rather than position) keeps this robust to tab changes. 76 const current_link: []const u8 = switch (session.data.current_page) { 77 .user_settings => settings_link, 78 .user_auth => auth_link, 79 else => repos_link, 80 }; 81 var selected_tab: ?usize = null; 82 83 // repos tab 84 { 85 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "repos", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 86 errdefer text_box.deinit(allocator); 87 text_box.getFocus().focusable = true; 88 text_box.getFocus().kind = .{ .custom = repos_link }; 89 try tab_ids.put(allocator, text_box.getFocus().id, {}); 90 if (std.mem.eql(u8, repos_link, current_link)) selected_tab = text_box.getFocus().id; 91 try box.children.put(allocator, text_box.getFocus().id, .{ 92 .widget = .{ .text_box = text_box }, 93 .rect = null, 94 .min_size = .{ .width = "repos".len + 2, .height = null }, 95 }); 96 } 97 98 // spacer pushes settings + auth to the right 99 { 100 var spacer = try ui.Spacer.init(allocator); 101 errdefer spacer.deinit(allocator); 102 try box.children.put(allocator, spacer.getFocus().id, .{ 103 .widget = .{ .spacer = spacer }, 104 .rect = null, 105 .min_size = null, 106 }); 107 } 108 109 // settings tab 110 { 111 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "settings", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 112 errdefer text_box.deinit(allocator); 113 text_box.getFocus().focusable = true; 114 text_box.getFocus().kind = .{ .custom = settings_link }; 115 try tab_ids.put(allocator, text_box.getFocus().id, {}); 116 if (std.mem.eql(u8, settings_link, current_link)) selected_tab = text_box.getFocus().id; 117 try box.children.put(allocator, text_box.getFocus().id, .{ 118 .widget = .{ .text_box = text_box }, 119 .rect = null, 120 .min_size = .{ .width = "settings".len + 2, .height = null }, 121 }); 122 } 123 124 // auth tab (login / logout). AuthTab defaults to the global a:/auth link; 125 // repoint its instance at this user's auth route so it stays local too. 126 { 127 var auth_tab = try AuthTab.View.init(allocator, &data.auth_tab, session); 128 errdefer auth_tab.deinit(allocator); 129 auth_tab.text_box.getFocus().kind = .{ .custom = auth_link }; 130 try tab_ids.put(allocator, auth_tab.getFocus().id, {}); 131 if (std.mem.eql(u8, auth_link, current_link)) selected_tab = auth_tab.getFocus().id; 132 try box.children.put(allocator, auth_tab.getFocus().id, .{ 133 .widget = .{ .auth_tab = auth_tab }, 134 .rect = null, 135 .min_size = .{ .width = AuthTab.min_width, .height = null }, 136 }); 137 } 138 139 // quit tab 140 if (session.is_terminal) { 141 var text_box = try wgt.TextBox(ui.Widget).init(allocator, ui.Quit.tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 142 errdefer text_box.deinit(allocator); 143 text_box.getFocus().focusable = true; 144 text_box.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 145 try tab_ids.put(allocator, text_box.getFocus().id, {}); 146 try box.children.put(allocator, text_box.getFocus().id, .{ 147 .widget = .{ .text_box = text_box }, 148 .rect = null, 149 // the label is a single column; +2 for the border 150 .min_size = .{ .width = 1 + 2, .height = null }, 151 }); 152 } 153 154 var self = View{ .box = box, .data = data, .tab_ids = tab_ids }; 155 self.getFocus().child_id = selected_tab orelse self.tab_ids.keys()[0]; 156 return self; 157 } 158 159 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 160 self.box.deinit(allocator); 161 self.tab_ids.deinit(allocator); 162 } 163 164 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 165 self.clearGrid(); 166 // only the selected tab shows its border 167 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 168 const tb: ?*wgt.TextBox(ui.Widget) = switch (child.widget) { 169 .text_box => |*x| x, 170 .auth_tab => |*at| &at.text_box, 171 else => null, 172 }; 173 if (tb) |t| { 174 t.options.border_style = if (self.getFocus().child_id == id) .single else .hidden; 175 } 176 } 177 try self.box.build(allocator, constraint, root_focus); 178 } 179 180 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 181 _ = allocator; 182 const current_tab = self.currentTabIndex() orelse return; 183 var new_tab = current_tab; 184 switch (key) { 185 .arrow_left => new_tab -|= 1, 186 .arrow_right => if (new_tab + 1 < self.tab_ids.count()) { 187 new_tab += 1; 188 }, 189 else => {}, 190 } 191 if (new_tab != current_tab) { 192 try root_focus.setFocus(self.tab_ids.keys()[new_tab]); 193 } 194 } 195 196 pub fn clearGrid(self: *View) void { 197 self.box.clearGrid(); 198 } 199 200 pub fn getGrid(self: View) ?Grid { 201 return self.box.getGrid(); 202 } 203 204 pub fn getFocus(self: *View) *Focus { 205 return self.box.getFocus(); 206 } 207 208 pub fn getSelectedIndex(self: View) ?usize { 209 return self.currentTabIndex(); 210 } 211 212 fn currentTabIndex(self: View) ?usize { 213 const child_id = self.box.focus.child_id orelse return null; 214 return self.tab_ids.getIndex(child_id); 215 } 216 }; 217