╭ 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: [3]usize, 31 32 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 33 var box = wgt.Box(ui.Widget).init(.{ .border_style = .hidden, .rounded_corners = true, .direction = .horiz }); 34 errdefer box.deinit(allocator); 35 36 var tab_ids: [3]usize = undefined; 37 38 // title sits to the left of the tabs 39 { 40 var title_view = try ui.Title.View.init(allocator, &data.title); 41 errdefer title_view.deinit(allocator); 42 title_view.getFocus().focusable = true; 43 title_view.getFocus().kind = .{ .custom = "a:/" }; 44 try box.children.put(allocator, title_view.getFocus().id, .{ 45 .widget = .{ .title = title_view }, 46 .rect = null, 47 .min_size = .{ .width = data.title.width, .height = null }, 48 }); 49 } 50 51 // spacer 52 { 53 var text = wgt.Text(ui.Widget).init(" "); 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 // every tab link is user-scoped so selecting one stays on this page — 63 // switching its stack and updating the url — instead of navigating to 64 // the global settings or auth pages. allocated in the page arena so the 65 // focus kinds that borrow them live as long as this page's widget tree. 66 const aa = session.page_arena.allocator(); 67 const repos_link = try std.fmt.allocPrint(aa, "a:/user/{s}", .{data.name}); 68 const settings_link = try std.fmt.allocPrint(aa, "a:/user/{s}/settings", .{data.name}); 69 const auth_link = try std.fmt.allocPrint(aa, "a:/user/{s}/auth", .{data.name}); 70 71 // repos tab 72 { 73 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "repos", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 74 errdefer text_box.deinit(allocator); 75 text_box.getFocus().focusable = true; 76 text_box.getFocus().kind = .{ .custom = repos_link }; 77 tab_ids[0] = text_box.getFocus().id; 78 try box.children.put(allocator, text_box.getFocus().id, .{ 79 .widget = .{ .text_box = text_box }, 80 .rect = null, 81 .min_size = .{ .width = "repos".len + 2, .height = null }, 82 }); 83 } 84 85 // spacer pushes settings + auth to the right 86 { 87 var spacer = ui.Spacer.init(); 88 errdefer spacer.deinit(allocator); 89 try box.children.put(allocator, spacer.getFocus().id, .{ 90 .widget = .{ .spacer = spacer }, 91 .rect = null, 92 .min_size = .{ .width = 1, .height = null }, 93 }); 94 } 95 96 // settings tab 97 { 98 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "settings", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 99 errdefer text_box.deinit(allocator); 100 text_box.getFocus().focusable = true; 101 text_box.getFocus().kind = .{ .custom = settings_link }; 102 tab_ids[1] = text_box.getFocus().id; 103 try box.children.put(allocator, text_box.getFocus().id, .{ 104 .widget = .{ .text_box = text_box }, 105 .rect = null, 106 .min_size = .{ .width = "settings".len + 2, .height = null }, 107 }); 108 } 109 110 // auth tab (login / logout). AuthTab defaults to the global a:/auth link; 111 // repoint its instance at this user's auth route so it stays local too. 112 { 113 var auth_tab = try AuthTab.View.init(allocator, &data.auth_tab, session); 114 errdefer auth_tab.deinit(allocator); 115 auth_tab.text_box.getFocus().kind = .{ .custom = auth_link }; 116 tab_ids[2] = auth_tab.getFocus().id; 117 try box.children.put(allocator, auth_tab.getFocus().id, .{ 118 .widget = .{ .auth_tab = auth_tab }, 119 .rect = null, 120 .min_size = .{ .width = AuthTab.min_width, .height = null }, 121 }); 122 } 123 124 var self = View{ .box = box, .data = data, .tab_ids = tab_ids }; 125 // open on the tab named by the current route 126 self.getFocus().child_id = tab_ids[ 127 switch (session.data.current_page) { 128 .user_settings => 1, 129 .user_auth => 2, 130 else => 0, 131 } 132 ]; 133 return self; 134 } 135 136 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 137 self.box.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 // only the selected tab shows its border 143 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 144 const tb: ?*wgt.TextBox(ui.Widget) = switch (child.widget) { 145 .text_box => |*x| x, 146 .auth_tab => |*at| &at.text_box, 147 else => null, 148 }; 149 if (tb) |t| { 150 t.options.border_style = if (self.getFocus().child_id == id) .single else .hidden; 151 } 152 } 153 try self.box.build(allocator, constraint, root_focus); 154 } 155 156 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 157 _ = allocator; 158 const current_tab = self.currentTabIndex() orelse return; 159 var new_tab = current_tab; 160 switch (key) { 161 .arrow_left => new_tab -|= 1, 162 .arrow_right => if (new_tab + 1 < self.tab_ids.len) { 163 new_tab += 1; 164 }, 165 else => {}, 166 } 167 if (new_tab != current_tab) { 168 try root_focus.setFocus(self.tab_ids[new_tab]); 169 } 170 } 171 172 pub fn clearGrid(self: *View) void { 173 self.box.clearGrid(); 174 } 175 176 pub fn getGrid(self: View) ?Grid { 177 return self.box.getGrid(); 178 } 179 180 pub fn getFocus(self: *View) *Focus { 181 return self.box.getFocus(); 182 } 183 184 pub fn getSelectedIndex(self: View) ?usize { 185 return self.currentTabIndex(); 186 } 187 188 fn currentTabIndex(self: View) ?usize { 189 const child_id = self.box.focus.child_id orelse return null; 190 for (self.tab_ids, 0..) |id, i| { 191 if (id == child_id) return i; 192 } 193 return null; 194 } 195 }; 196