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