╭ 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 title: ui.Title, 14 auth_tab: AuthTab, 15 16 const Self = @This(); 17 18 pub fn init(arena: *std.heap.ArenaAllocator) !Self { 19 return .{ 20 .title = try ui.Title.init(arena, "haxy"), 21 .auth_tab = AuthTab.init(), 22 }; 23 } 24 25 pub const View = struct { 26 box: wgt.Box(ui.Widget), 27 data: *const Self, 28 tab_ids: [3]usize, 29 30 const auth_tab_index: usize = 2; 31 // the auth slot must fit either "login" or "logout" 32 const auth_tab_min_width: usize = @as(usize, "logout".len) + 2; 33 34 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 35 var box = wgt.Box(ui.Widget).init(.{ .border_style = null, .rounded_corners = true, .direction = .horiz }); 36 errdefer box.deinit(allocator); 37 38 var tab_ids: [3]usize = undefined; 39 40 // spacer 41 { 42 var text = wgt.Text(ui.Widget).init(" "); 43 errdefer text.deinit(allocator); 44 try box.children.put(allocator, text.getFocus().id, .{ 45 .widget = .{ .text = text }, 46 .rect = null, 47 .min_size = .{ .width = 1, .height = null }, 48 }); 49 } 50 51 // title sits to the left of the tabs 52 { 53 var title_view = try ui.Title.View.init(allocator, &data.title); 54 errdefer title_view.deinit(allocator); 55 try box.children.put(allocator, title_view.getFocus().id, .{ 56 .widget = .{ .title = title_view }, 57 .rect = null, 58 .min_size = .{ .width = data.title.width, .height = null }, 59 }); 60 } 61 62 // spacer 63 { 64 var text = wgt.Text(ui.Widget).init(" "); 65 errdefer text.deinit(allocator); 66 try box.children.put(allocator, text.getFocus().id, .{ 67 .widget = .{ .text = text }, 68 .rect = null, 69 .min_size = .{ .width = 1, .height = null }, 70 }); 71 } 72 73 // fixed tabs: users, repos 74 for ( 75 [_][]const u8{ "users", "repos" }, 76 [_][]const u8{ "a:/users", "a:/repos" }, 77 0.., 78 ) |name, focus_name, i| { 79 var text_box = try wgt.TextBox(ui.Widget).init(allocator, name, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 80 errdefer text_box.deinit(allocator); 81 text_box.getFocus().focusable = true; 82 text_box.getFocus().kind = .{ .custom = focus_name }; 83 tab_ids[i] = text_box.getFocus().id; 84 try box.children.put(allocator, text_box.getFocus().id, .{ 85 .widget = .{ .text_box = text_box }, 86 .rect = null, 87 .min_size = .{ .width = name.len + 2, .height = null }, 88 }); 89 } 90 91 // spacer pushes the auth tab to the right 92 { 93 var spacer = ui.Spacer.init(); 94 errdefer spacer.deinit(allocator); 95 try box.children.put(allocator, spacer.getFocus().id, .{ 96 .widget = .{ .spacer = spacer }, 97 .rect = null, 98 .min_size = .{ .width = 1, .height = null }, 99 }); 100 } 101 102 // auth tab — knows how to swap its own label based on session 103 { 104 var auth_tab = try AuthTab.View.init(allocator, &data.auth_tab, session); 105 errdefer auth_tab.deinit(allocator); 106 tab_ids[auth_tab_index] = auth_tab.getFocus().id; 107 try box.children.put(allocator, auth_tab.getFocus().id, .{ 108 .widget = .{ .home_auth_tab = auth_tab }, 109 .rect = null, 110 .min_size = .{ .width = auth_tab_min_width, .height = null }, 111 }); 112 } 113 114 var self = View{ .box = box, .data = data, .tab_ids = tab_ids }; 115 // initial selected tab follows session.data.current_page — the web 116 // layer decides this from the request URL, the TTY uses whatever 117 // the session arrives with — so we don't bake a default here. 118 self.getFocus().child_id = tab_ids[ 119 switch (session.data.current_page) { 120 .home_users => 0, 121 .home_repos => 1, 122 .home_auth => 2, 123 } 124 ]; 125 return self; 126 } 127 128 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 129 self.box.deinit(allocator); 130 } 131 132 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 133 self.clearGrid(); 134 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 135 const tb: ?*wgt.TextBox(ui.Widget) = switch (child.widget) { 136 .text_box => |*x| x, 137 .home_auth_tab => |*at| &at.text_box, 138 else => null, 139 }; 140 if (tb) |t| { 141 t.options.border_style = if (self.getFocus().child_id == id) .single else .hidden; 142 } 143 } 144 try self.box.build(allocator, constraint, root_focus); 145 } 146 147 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 148 _ = allocator; 149 const current_tab = self.currentTabIndex() orelse return; 150 var new_tab = current_tab; 151 switch (key) { 152 .arrow_left => new_tab -|= 1, 153 .arrow_right => if (new_tab + 1 < self.tab_ids.len) { 154 new_tab += 1; 155 }, 156 else => {}, 157 } 158 if (new_tab != current_tab) { 159 try root_focus.setFocus(self.tab_ids[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 for (self.tab_ids, 0..) |id, i| { 182 if (id == child_id) return i; 183 } 184 return null; 185 } 186 }; 187