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.

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 {
    // ...
}

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

    var selectedCategory: Category? = nil
    var recipePath: [Recipe.ID] = []
    var groceryList: GroceryList = GroceryList()

    private var decoder = JSONDecoder()
    private var encoder = JSONEncoder()
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.

Class and protocol design

FocusCookbook/FocusCookbookApp.swift:12 — representative type boundary

struct FocusCookbookApp: App {
    // ...
}
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

    @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 FocusCookbookApp
FocusCookbook/Models/NavigationModel.swift NavigationModel, CodingKeys
FocusCookbook/Models/DataModel.swift DataModel
FocusCookbook/Grocery List/GroceryListView.swift GroceryListView, GroceryListContentView
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