swiftui-macos-patterns

Implement @Observable state and focused value menu validation for macOS SwiftUI apps.

1|Updated Jun 5, 2026
One-click install
npx skills add https://github.com/LaughingJackalope/macos-skills --skill swiftui-macos-patterns
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: swiftui-macos-patterns
Source: https://github.com/LaughingJackalope/macos-skills/tree/main/swiftui-macos-patterns
Command: npx skills add https://github.com/LaughingJackalope/macos-skills --skill swiftui-macos-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill solves the frequent bugs, crashes, and poor user experience caused by inconsistent state management, broken menu command validation, and incorrect environment injection in macOS SwiftUI applications, which are common pain points for developers building native macOS software with SwiftUI.

Core Features & Use Cases

  • Modern State Management Patterns: Covers @Observable, @State, @Binding, @Environment, @AppStorage, and @SceneStorage for all macOS SwiftUI state ownership and persistence scenarios.
  • Menu Command Validation: Implements the focused values system (@FocusedValue, @FocusedBinding, @FocusedObject) to automatically enable/disable menu items based on the state of the currently focused view, replacing legacy NSMenuItemValidation patterns.
  • macOS-Specific Architecture Wiring: Provides production-ready patterns for scene-scoped environment injection, Settings scene state sharing, focus system implementation, and App Groups-backed UserDefaults integration for multi-window and document-based apps.
  • Use Case Example: A document-based macOS app can use these patterns to implement per-window document state, validate Save/Export menu items based on the active editor, and share user preferences between the main window and Settings scene without runtime crashes.

Quick Start

Use the swiftui-macos-patterns skill to implement @Observable view models with @FocusedValue menu validation for your macOS document editor app, so Save and Export menu items automatically enable when a document is active and disable when no editor is focused.

Frequently Asked Questions about swiftui-macos-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I validate menu commands in SwiftUI macOS based on the focused window?

Menu command validation in SwiftUI macOS uses the focused values system, specifically @FocusedValue and @FocusedBinding, to automatically enable or disable menu items based on the currently focused view's state. This replaces legacy NSMenuItemValidation patterns by making the responder chain aware of your active document editor's state.

How does @Observable work for state management in macOS SwiftUI apps?

The @Observable pattern in macOS SwiftUI provides modern state ownership by replacing older observation models, allowing view models to track changes without environment injection crashes. It integrates with @State, @Binding, and @Environment to manage per-window document state and scene-scoped dependencies seamlessly.

Why do my SwiftUI environment injections crash in multi-window macOS apps?

Environment injection crashes in macOS SwiftUI typically occur due to inconsistent state management across multiple windows or scenes. Using scene-scoped environment injection and proper @Observable patterns ensures that dependencies are correctly wired and shared between the main window and Settings scene without runtime errors.

What is the best way to share user preferences between a main window and a Settings scene in macOS SwiftUI?

Sharing preferences between a main window and Settings scene in macOS SwiftUI requires App Groups-backed @AppStorage combined with proper environment wiring. This configuration allows UserDefaults integration to synchronize user preferences across all scenes and multi-window instances without runtime crashes.

Can I use @SceneStorage to restore window state in a document-based macOS SwiftUI app?

Yes, @SceneStorage restores window state in document-based macOS SwiftUI apps by automatically persisting and recovering per-window document state. Combined with @FocusedValue for menu validation and @Observable view models, it ensures robust window restoration for native macOS document editors.

Does this macOS SwiftUI architecture require Swift 6.3 and macOS 26+?

Yes, these macOS SwiftUI state and architecture patterns target macOS 26+ and Swift 6.3 to satisfy modern @Observable observation requirements and focused value system implementations. This ensures compatibility with the latest scene-scoped dependency injection and Settings scene state sharing features.