╭ changed ──────╮ │src/ui/Auth.zig│ ╰───────────────╯ ╭───────────╮ │← all files│ ╰───────────╯ ╭───────────────────────────────────╮ │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 Key = xitui.input.Key; │ │8 const Grid = xitui.grid.Grid; │ ╰───────────────────────────────────╯ ╭────────────────────────────────────────────────────────────────────────────────────────────────────╮ │24 } │ │25 │ │26 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, │ │27 + stack: wgt.Stack(ui.Widget), │ │28 session: *ui.Session, │ │29 │ │30 + const login_index = 0; │ │31 + const logout_index = 1; │ │32 + │ │33 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { │ │36 - var login_view = try Login.View.init(allocator, &data.login, session); │ │37 - errdefer login_view.deinit(allocator); │ │38 - var logout_view = try Logout.View.init(allocator, &data.logout, session); │ │39 - errdefer logout_view.deinit(allocator); │ │34 + var stack = try wgt.Stack(ui.Widget).init(allocator); │ │35 + errdefer stack.deinit(allocator); │ │36 + { │ │37 + var login = try Login.View.init(allocator, &data.login, session); │ │38 + errdefer login.deinit(allocator); │ │39 + try stack.children.put(allocator, login.getFocus().id, .{ .auth_login = login }); │ │40 + } │ │41 + { │ │42 + var logout = try Logout.View.init(allocator, &data.logout, session); │ │43 + errdefer logout.deinit(allocator); │ │44 + try stack.children.put(allocator, logout.getFocus().id, .{ .auth_logout = logout });│ │45 + } │ │46 return .{ │ │41 - .focus = try Focus.create(allocator, .container), │ │42 - .login = login_view, │ │43 - .logout = logout_view, │ │47 + .stack = stack, │ │48 .session = session, │ │49 }; │ │50 } │ ╰────────────────────────────────────────────────────────────────────────────────────────────────────╯ ╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │51 │ │52 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); │ │53 + self.stack.deinit(allocator); │ │54 } │ │55 │ │56 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 - } │ │57 + const index: usize = if (self.session.data.user_id != null) logout_index else login_index;│ │58 + self.stack.getFocus().child_id = self.stack.children.keys()[index]; │ │59 + try self.stack.build(allocator, constraint, root_focus); │ │60 } │ │61 │ │62 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 - } │ │63 + try self.stack.input(allocator, key, root_focus); │ │64 } │ │65 │ │66 pub fn clearGrid(self: *View) void { │ ╰──────────────────────────────────────────────────────────────────╯ ╭──────────────────────────────────────────╮ │80 - self.login.clearGrid(); │ │81 - self.logout.clearGrid(); │ │67 + self.stack.clearGrid(); │ │68 } │ │69 │ │70 pub fn getGrid(self: View) ?Grid {│ ╰──────────────────────────────────────────╯ ╭────────────────────────────────────────────────────────────────────────────────╮ │85 - if (self.session.data.user_id != null) return self.logout.getGrid();│ │86 - return self.login.getGrid(); │ │71 + return self.stack.getGrid(); │ │72 } │ │73 │ │74 pub fn getFocus(self: *View) *Focus { │ ╰────────────────────────────────────────────────────────────────────────────────╯ ╭──────────────────────────────────────────────────────────────────────────────╮ │90 - return self.focus; │ │75 + return self.stack.getFocus(); │ │76 } │ │77 │ │93 - pub fn atTop(self: View) bool { │ │94 - if (self.session.data.user_id != null) return self.logout.atTop();│ │95 - return self.login.atTop(); │ │78 + pub fn atTop(self: View, root_focus: *Focus) bool { │ │79 + const selected = self.stack.getSelected() orelse return false; │ │80 + return selected.atTop(root_focus); │ │81 } │ │82 }; │ │83 │ ╰──────────────────────────────────────────────────────────────────────────────╯