╭ Auth.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 layout = xitui.layout; 6 const Key = xitui.input.Key; 7 const Grid = xitui.grid.Grid; 8 const Focus = xitui.focus.Focus; 9 10 pub const Login = @import("./Auth/Login.zig"); 11 pub const Logout = @import("./Auth/Logout.zig"); 12 13 login: Login, 14 logout: Logout, 15 16 const Self = @This(); 17 18 pub fn init() Self { 19 return .{ 20 .login = Login.init(), 21 .logout = Logout.init(), 22 }; 23 } 24 25 pub const View = struct { 26 // own container focus so callers (e.g. wgt.Stack) see a stable id. the 27 // inner views' focus ids change as login/logout swap; if we exposed those 28 // directly, anything keyed off our id at one moment would mismatch the 29 // focus tree the next frame. 30 focus: *Focus, 31 login: Login.View, 32 logout: Logout.View, 33 session: *ui.Session, 34 35 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session, users_tab_id: usize) !View { 36 var login_view = try Login.View.init(allocator, &data.login, session, users_tab_id); 37 errdefer login_view.deinit(allocator); 38 var logout_view = try Logout.View.init(allocator, &data.logout, session, users_tab_id); 39 errdefer logout_view.deinit(allocator); 40 return .{ 41 .focus = try Focus.create(allocator, .container), 42 .login = login_view, 43 .logout = logout_view, 44 .session = session, 45 }; 46 } 47 48 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 49 self.focus.destroy(allocator); 50 self.login.deinit(allocator); 51 self.logout.deinit(allocator); 52 } 53 54 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 55 self.focus.clear(); 56 if (self.session.data.user_id != null) { 57 try self.logout.build(allocator, constraint, root_focus); 58 if (self.logout.getGrid()) |inner_grid| { 59 try self.focus.addChild(allocator, self.logout.getFocus(), inner_grid.size, 0, 0); 60 } 61 self.focus.child_id = self.logout.getFocus().id; 62 } else { 63 try self.login.build(allocator, constraint, root_focus); 64 if (self.login.getGrid()) |inner_grid| { 65 try self.focus.addChild(allocator, self.login.getFocus(), inner_grid.size, 0, 0); 66 } 67 self.focus.child_id = self.login.getFocus().id; 68 } 69 } 70 71 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 72 if (self.session.data.user_id != null) { 73 try self.logout.input(allocator, key, root_focus); 74 } else { 75 try self.login.input(allocator, key, root_focus); 76 } 77 } 78 79 pub fn clearGrid(self: *View) void { 80 self.login.clearGrid(); 81 self.logout.clearGrid(); 82 } 83 84 pub fn getGrid(self: View) ?Grid { 85 if (self.session.data.user_id != null) return self.logout.getGrid(); 86 return self.login.getGrid(); 87 } 88 89 pub fn getFocus(self: *View) *Focus { 90 return self.focus; 91 } 92 93 pub fn getSelectedIndex(self: View) ?usize { 94 if (self.session.data.user_id != null) return self.logout.getSelectedIndex(); 95 return self.login.getSelectedIndex(); 96 } 97 }; 98