| name | macos-patterns |
| description | macOS platform patterns: window management, menu bar, keyboard shortcuts, Settings, drag-and-drop, App Sandbox. Use when building macOS-specific features, handling desktop input, or implementing Mac app patterns. Triggers: macOS, Mac, desktop, menu bar, keyboard shortcut, Settings scene, window management. |
macOS Platform Patterns
Window Management
Three scene types for Mac apps:
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
.defaultSize(width: 900, height: 600)
Window("Inspector", id: "inspector") {
InspectorView()
}
.defaultSize(width: 300, height: 400)
DocumentGroup(newDocument: MyDocument()) { file in
DocumentEditor(document: file.$document)
}
}
}
Menu Bar Customization (CommandMenu + FocusedValue)
CommandMenu closures run at the App scene level — outside the view hierarchy. They CANNOT access view @State directly. Use @FocusedValue to bridge view state to menu actions.
extension FocusedValues {
@Entry var activeDocument: DocumentViewModel?
}
@Observable @MainActor
class DocumentViewModel {
var content = ""
func save() { }
func togglePreview() { }
}
struct DocumentView: View {
@State var viewModel = DocumentViewModel()
var body: some View {
EditorContent(viewModel: viewModel)
.focusedValue(\.activeDocument, viewModel)
}
}
@main
struct MyApp: App {
@FocusedValue(\.activeDocument) private var document
var body: some Scene {
WindowGroup { DocumentView() }
CommandMenu("Document") {
Button("Save") { document?.save() }
.keyboardShortcut("s", modifiers: .command)
.disabled(document == nil)
Button("Toggle Preview") { document?.togglePreview() }
.keyboardShortcut("p", modifiers: [.command, .option])
.disabled(document == nil)
}
CommandGroup(replacing: .newItem) {
Button("New Document") { document?.createNew() }
.keyboardShortcut("n", modifiers: .command)
.disabled(document == nil)
}
}
}
Rules:
- ALWAYS define FocusedValues key using
@Entry macro
- ALWAYS use
.focusedValue(\.key, value) on the active view to publish state
- ALWAYS consume via
@FocusedValue(\.key) private var name in the App struct
- ALWAYS
.disabled(object == nil) on every menu item — menus are active even when no view is focused
- Using empty closures
{} on CommandMenu buttons is unacceptable — every action must call through to the FocusedValue
- CommandMenu closures run outside the view hierarchy — they cannot call view methods directly
Keyboard Shortcuts
Every menu item and primary action needs .keyboardShortcut():
Button("Save") { save() }
.keyboardShortcut("s", modifiers: .command)
Button("Undo") { undo() }
.keyboardShortcut("z", modifiers: .command)
Button("Find") { showSearch() }
.keyboardShortcut("f", modifiers: .command)
Button("Toggle Sidebar") { toggleSidebar() }
.keyboardShortcut("s", modifiers: [.command, .control])
Settings / Preferences
@main
struct MyApp: App {
var body: some Scene {
WindowGroup { ContentView() }
Settings {
SettingsView()
}
}
}
struct SettingsView: View {
var body: some View {
TabView {
GeneralSettingsView()
.tabItem { Label("General", systemImage: "gear") }
AppearanceSettingsView()
.tabItem { Label("Appearance", systemImage: "paintbrush") }
}
.frame(width: 450, height: 300)
}
}
Menu Bar Apps
@main
struct StatusApp: App {
var body: some Scene {
MenuBarExtra("Status", systemImage: "circle.fill") {
StatusMenuView()
}
.menuBarExtraStyle(.window)
}
}
Drag and Drop
ItemRow(item: item)
.draggable(item)
FolderView(folder: folder)
.dropDestination(for: Item.self) { items, location in
moveItems(items, to: folder)
return true
}
App Sandbox
macOS apps need entitlements for system access:
- File access:
com.apple.security.files.user-selected.read-write
- Network:
com.apple.security.network.client
- Camera:
com.apple.security.device.camera
- Microphone:
com.apple.security.device.audio-input
Platform Conditionals
Use for platform-specific code in shared modules:
#if os(macOS)
import AppKit
#elseif os(iOS)
import UIKit
#endif
Liquid Glass (macOS 26)
VStack { content }
.glassEffect()
Button("Action") { }
.buttonStyle(.glass)
Button("Primary") { }
.buttonStyle(.glassProminent)
File Handling
.fileImporter(
isPresented: $showOpen,
allowedContentTypes: [.json]
) { result in
}
.fileExporter(
isPresented: $showSave,
document: doc,
contentType: .json
) { result in
}
Deprecated API Alternatives
- Use
.foregroundStyle() not .foregroundColor()
- Use
.clipShape(.rect(cornerRadius:)) not .cornerRadius()
- Use
@Observable not ObservableObject — never combine both on the same class
- Touch Bar is deprecated — do not implement
NOT Available on macOS
- No UIKit — macOS uses AppKit; SwiftUI apps should never import UIKit
- No
UIColor / UIImage — use SwiftUI Color / Image instead
- No HealthKit
- No haptic feedback (CoreHaptics)
- No rear camera, LiDAR, or portrait mode (FaceTime camera only)
- No App Clips
- No Live Activities
- No Safari extensions (different extension model)
Rules
- Use
WindowGroup, Window, or DocumentGroup for scene types
- Use
CommandMenu / CommandGroup for menu bar customization
- Every primary action MUST have
.keyboardShortcut()
- Use
Settings { } scene for preferences (auto-wires Cmd+,)
- Use
MenuBarExtra for menu bar apps with .menuBarExtraStyle(.window)
- Use
.draggable() / .dropDestination() for drag-and-drop
- Use
#if os(macOS) for platform-specific code in shared modules
- Never import UIKit — use SwiftUI
Color/Image not NSColor/NSImage
- Use
.foregroundStyle() not .foregroundColor()
- Use
.clipShape(.rect(cornerRadius:)) not .cornerRadius()