Sample CodeiOS, iPadOS, Mac Catalyst, macOSReviewed 2026-07-21View on Apple Developer

Focus Cookbook: Supporting and enhancing focus-driven interactions in your SwiftUI app

At a glance

Item Summary
Purpose Create custom focusable views with key-press handlers that accelerate keyboard input and support movement, and control focus programmatically.
App architecture A Swift sample with the source-visible chain FocusCookbookAppContentViewDataModelSwiftUI APIs.
Main patterns Binding-based state propagation
Project style 20 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.
Execution model No structured execution marker indexed; callback threading requires source review.
State/event model Source-visible mechanisms: SwiftUI state property wrapper, @Observable.
Key frameworks/packages SwiftUI, Observation; these are source dependencies, not architecture labels.

Project structure

Source bundle/
└── FocusCookbook/
    ├── FocusCookbookApp.swift
    ├── Models/
    │   ├── NavigationModel.swift
    │   ├── DataModel.swift
    │   ├── Category.swift
    │   ├── Ingredient.swift
    │   └── Recipe.swift
    ├── Grocery List/
    │   ├── GroceryListView.swift
    │   └── GroceryList.swift
    ├── Views/
    │   ├── ContentView.swift
    │   ├── RecipeNavigationView.swift
    │   └── RecipeCommands.swift
    └── Rating Picker/
        └── RatingPicker.swift

Structure observations

  • Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
  • Primary languages: Swift.
  • The verified tree contains 4 project/configuration file(s) and 27 source declaration(s).

Overall architecture

Reference code

FocusCookbook/FocusCookbookApp.swift:11 — architecture anchor

@main
struct FocusCookbookApp: App {
    // ...
        WindowGroup {
            ContentView()
        }
    // ...
}

Interpretation

The arrows summarize the source-visible entry, role-named types or folders, and framework direction; when nodes come from structural folders, the sequence is a high-level interpretation rather than proof that every adjacent node calls the next. Ownership is claimed only where the next section cites a stored property or assignment. The diagram is intentionally limited to the dominant path into SwiftUI.

Ownership and state

Ownership evidence

FocusCookbook/Models/NavigationModel.swift:12 — stored dependency or nearest verified ownership anchor

@Observable final class NavigationModel: Codable {
    var selectedCategory: Category? = nil
    // ...
}
Owner Object or state Relationship Mutation authority
NavigationModel Category (selectedCategory) stores or receives App/module collaborators
NavigationModel Array (recipePath) owns value state App/module collaborators
NavigationModel GroceryList (groceryList) creates and retains App/module collaborators
NavigationModel JSONDecoder (decoder) creates and retains Owning lexical scope

Composition arrows indicate a source-visible construction expression or locally owned value state; aggregation means the owner stores or receives a dependency without proving exclusive lifetime ownership.

Concurrency, scheduling, and thread safety

Evidence limit: actor isolation, async/await, or Task creation does not by itself prove background-thread execution; Sendable conformance alone does not prove thread-safe mutation.

No source-visible execution, scheduling, or synchronization boundary was found in the indexed source.

@MainActor/MainActor.run, DispatchQueue.main, and RunLoop.main are reported as distinct isolation, queue, and event-loop mechanisms. A plain Task is kept separate from Task.detached; neither is labeled as a background thread.

State propagation, frameworks, and dependencies

Evidence limit: an import proves a source-level compilation dependency at the cited line; it does not prove runtime use, architectural adoption, or whether a Swift package is a direct application dependency.

Category Mechanism or module Verified role Evidence
State propagation SwiftUI state property wrapper A SwiftUI property wrapper supplies or observes UI state. FocusCookbook/Grocery List/GroceryListButton.swift:11
State propagation @Observable Observation macro publishes source-visible changes. FocusCookbook/Models/DataModel.swift:11
Source import SwiftUI The cited file imports this module; runtime use and architectural role are not inferred. FocusCookbook/FocusCookbookApp.swift:9
Source import Observation The cited file imports this module; runtime use and architectural role are not inferred. FocusCookbook/Models/DataModel.swift:9

receive(on:) describes downstream delivery scheduling, while subscribe(on:) describes upstream subscription/request/cancel scheduling. An import Combine alone establishes neither behavior nor a Store, reducer, Redux, or other application architecture.

Class and protocol design

FocusCookbook/FocusCookbookApp.swift:12 — representative type boundary

@main
struct FocusCookbookApp: App {
    // ...
            ContentView()
    // ...
}
Type Responsibility Depends on or conforms to
FocusCookbookApp Application entry and top-level composition App
NavigationModel Feature data or observable state Codable
DataModel Feature data or observable state Concrete collaborators/imported frameworks
GroceryListView User-interface presentation and input forwarding View
GroceryListContentView User-interface presentation and input forwarding View
ContentView User-interface presentation and input forwarding View
RecipeNavigationView User-interface presentation and input forwarding View
EmojiView User-interface presentation and input forwarding View
RatingPicker Represents a feature value or composable behavior Concrete collaborators/imported frameworks
EmojiContainer Represents a feature value or composable behavior Concrete collaborators/imported frameworks

No local protocol conformance is claimed as protocol-oriented design; external framework conformances are listed only as dependencies.

Access control

Symbol Access Verified effect Likely rationale
dismiss (FocusCookbook/Grocery List/GroceryListView.swift:11) private Use is restricted to the lexical declaration and same-file extensions allowed by Swift. Inference: keep state mutation or dependency lifetime inside the owning implementation.
currentItemID (FocusCookbook/Grocery List/GroceryListView.swift:13) private Use is restricted to the lexical declaration and same-file extensions allowed by Swift. Inference: keep state mutation or dependency lifetime inside the owning implementation.
addEmptyItem (FocusCookbook/Grocery List/GroceryListView.swift:37) private Use is restricted to the lexical declaration and same-file extensions allowed by Swift. Inference: hide an implementation step that is not part of the collaboration surface.
newItemButton (FocusCookbook/Grocery List/GroceryListView.swift:42) private Use is restricted to the lexical declaration and same-file extensions allowed by Swift. Inference: keep state mutation or dependency lifetime inside the owning implementation.

Reference code

FocusCookbook/Grocery List/GroceryListView.swift:11 — representative boundary

struct GroceryListView: View {
    @Environment(\.dismiss) private var dismiss
    // ...
}

Swift declarations without a modifier are internal; explicit private, fileprivate, private(set), public, or open entries above are interpreted by language semantics. Objective-C/C samples instead rely on header and implementation boundaries, which are not equivalent to Swift lexical privacy.

Logic ownership and placement

Logic Owning type or file Placement rationale
Application entry and top-level composition FocusCookbookApp The source’s App suffix makes this role explicit.
Feature data or observable state DataModel, NavigationModel The source’s Model suffix makes this role explicit.
User-interface presentation and input forwarding ContentView, EmojiView, GroceryListContentView, GroceryListView The source’s View suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
Binding-based state propagation FocusCookbook/Grocery List/GroceryListButton.swift:11 A binding exposes controlled read/write access while the upstream owner remains authoritative.

Naming conventions

  • Types: App: FocusCookbookApp; Model: DataModel, NavigationModel; View: ContentView, EmojiView, GroceryListContentView, GroceryListView, RecipeNavigationView.
  • Protocols: no local protocol declaration in the scanned source.
  • Methods: encode, recipes, addEmptyItem, selectRating.
  • Files: FocusCookbook/FocusCookbookApp.swift, FocusCookbook/Models/NavigationModel.swift, FocusCookbook/Models/DataModel.swift, FocusCookbook/Grocery List/GroceryListView.swift, FocusCookbook/Views/ContentView.swift, FocusCookbook/Views/RecipeNavigationView.swift.

Architecture takeaways

  • FocusCookbookApp is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches SwiftUI through a deliberately small high-level chain; the detailed API graph remains inside the cited implementation files.
  • Stored-property evidence identifies lifecycle collaboration; it does not by itself prove exclusive object ownership.
  • Access-control conclusions separate verified language visibility from the likely design rationale.
  • The source does not justify labeling the design protocol-oriented.

Source map

Source file Relevant symbols
FocusCookbook/FocusCookbookApp.swift Cited implementation, FocusCookbookApp, SwiftUI
FocusCookbook/Models/NavigationModel.swift Cited implementation, NavigationModel, CodingKeys
FocusCookbook/Grocery List/GroceryListView.swift Cited implementation, GroceryListView, GroceryListContentView
FocusCookbook/Grocery List/GroceryListButton.swift Cited implementation, SwiftUI state property wrapper, GroceryListButton
FocusCookbook/Models/DataModel.swift @Observable, Observation, DataModel
FocusCookbook/Views/ContentView.swift ContentView
FocusCookbook/Views/RecipeNavigationView.swift RecipeNavigationView
FocusCookbook/Rating Picker/RatingPicker.swift RatingPicker, EmojiContainer, EmojiView
FocusCookbook/Models/Category.swift Category
FocusCookbook/Models/Ingredient.swift Ingredient
FocusCookbook/Models/Recipe.swift Recipe
FocusCookbook/Grocery List/GroceryList.swift GroceryList, Item
FocusCookbook/Views/RecipeCommands.swift SelectedRecipeKey, RecipeCommands
FocusCookbook/Views/RecipeDetail.swift RecipeDetail, Content
FocusCookbook/Views/RelatedRecipeLink.swift RelatedRecipeButtonStyle, RelatedRecipeLink
FocusCookbook/Grocery List/ChecklistToggleStyle.swift ChecklistToggleStyle