Swiftui patterns

From fanfan-de/anybox · View interactive version

Swiftui patterns is an AI agent skill from the GitHub repository fanfan-de/anybox. Build macOS SwiftUI scenes and components with desktop patterns. Use when shaping windows, commands, toolbars, settings, split views, or inspectors.

How to use this skill

Skill file

SwiftUI Patterns

Quick Start

Choose a track based on your goal:

Existing project

New app scaffolding

New App File Structure

For any non-trivial macOS app, start with this shape instead of putting the app,

all views, models, stores, services, and helpers in one Swift file:

Keep files small and named after the primary type they contain. If a file starts

collecting unrelated views, models, stores, networking clients, and helper

extensions, split it before adding more behavior.

Pre-Edit Checklist For New App Scaffolds

Before writing the full UI:

  1. Choose the scene model.
  2. Choose state ownership: app-wide, scene-scoped, window-scoped, or view-local.
  3. Sketch file and module boundaries.
  4. Create the folder structure before filling in the UI.
  5. Keep script/build_and_run.sh and .codex/environments/environment.toml separate from app source.

General Rules To Follow

For concrete sidebar row and split-view background examples, read

references/split-inspectors.md.

State Ownership Summary

Use the narrowest state tool that matches the ownership model:

| Scenario | Preferred pattern |

| --- | --- |

| Local view or control state | @State |

| Child mutates parent-owned value state | @Binding |

| Root-owned reference model on macOS 14+ | @State with an @Observable type |

| Child reads or mutates an injected @Observable model | Pass it explicitly as a stored property |

| Window-scoped ephemeral selection or expansion state | @SceneStorage when practical, otherwise scene-owned @State |

| Shared user preference | @AppStorage |

| Shared app service or configuration | @Environment(Type.self) |

| Legacy reference model on older targets | @StateObject at the owner and @ObservedObject when injected |

Choose the ownership location first, then the wrapper. Do not turn simple desktop state into a view model by reflex.

Cross-Cutting References

Anti-Patterns

Workflow For A New macOS Scene Or View

  1. Define the scene type and ownership model before writing child views.
  2. Decide which actions live in content, toolbars, commands, inspectors, or settings.
  3. Sketch the selection model and layout: sidebar-detail, editor-inspector, document window, or utility window.
  4. Create the file/folder structure for app entrypoint, root layout, feature views, models, stores, services, and support helpers.
  5. Build with small, focused subviews and explicit inputs rather than giant computed fragments.
  6. Add keyboard shortcuts and menu or toolbar exposure for actions that matter on desktop.
  7. Validate the flow with a build and a quick usability pass: multiwindow assumptions, settings entry points, and selection stability.

Component References

Use references/components-index.md as the entry point. Each component reference should include:

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.