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