|
| 1 | +--- |
| 2 | +name: appkit-interop |
| 3 | +description: Bridge macOS SwiftUI into AppKit narrowly. Use when implementing representables, reaching NSWindow or panels, handling menus, or using the responder chain. |
| 4 | +--- |
| 5 | + |
| 6 | +# AppKit Interop |
| 7 | + |
| 8 | +## Quick Start |
| 9 | + |
| 10 | +Use this skill when SwiftUI is close but not quite enough for native macOS behavior. |
| 11 | +Keep the bridge as small and explicit as possible. SwiftUI should usually remain |
| 12 | +the source of truth, while AppKit handles the imperative edge. |
| 13 | + |
| 14 | +## Choose The Smallest Bridge |
| 15 | + |
| 16 | +- Use pure SwiftUI when the required behavior already exists in scenes, toolbars, commands, inspectors, or standard controls. |
| 17 | +- Use `NSViewRepresentable` when you need a specific AppKit view with lightweight lifecycle needs. |
| 18 | +- Use `NSViewControllerRepresentable` when you need controller lifecycle, delegation, or presentation coordination. |
| 19 | +- Use direct AppKit window or app hooks when you need `NSWindow`, responder-chain, menu validation, panels, or app-level behavior. |
| 20 | + |
| 21 | +## Workflow |
| 22 | + |
| 23 | +1. Name the capability gap precisely. |
| 24 | + - Window behavior |
| 25 | + - Text system behavior |
| 26 | + - Menu validation |
| 27 | + - Drag and drop |
| 28 | + - File open/save panels |
| 29 | + - First responder control |
| 30 | + |
| 31 | +2. Pick the smallest boundary that solves it. |
| 32 | + - Avoid porting a whole screen to AppKit when one wrapped control or coordinator would do. |
| 33 | + |
| 34 | +3. Keep ownership explicit. |
| 35 | + - SwiftUI owns value state, selection, and observable models. |
| 36 | + - AppKit objects stay inside the representable, coordinator, or bridge object. |
| 37 | + |
| 38 | +4. Expose a narrow interface back to SwiftUI. |
| 39 | + - Bindings for editable state |
| 40 | + - Small callbacks for events |
| 41 | + - Focused bridge services only when necessary |
| 42 | + |
| 43 | +5. Validate lifecycle assumptions. |
| 44 | + - SwiftUI may recreate representables. |
| 45 | + - Coordinators exist to hold delegate and target-action glue, not as a second app architecture. |
| 46 | + |
| 47 | +## References |
| 48 | + |
| 49 | +- `references/representables.md`: choosing between view and view-controller wrappers, plus coordinator patterns. |
| 50 | +- `references/window-panels.md`: window access, utility windows, and open/save panels. |
| 51 | +- `references/responder-menus.md`: first responder, command routing, and menu validation. |
| 52 | +- `references/drag-drop-pasteboard.md`: pasteboard, file URLs, and desktop drag/drop edges. |
| 53 | + |
| 54 | +## Guardrails |
| 55 | + |
| 56 | +- Do not duplicate the source of truth between SwiftUI and AppKit. |
| 57 | +- Do not let `Coordinator` become an unstructured dumping ground. |
| 58 | +- Do not store long-lived `NSView` or `NSWindow` instances globally without a strong ownership reason. |
| 59 | +- Prefer a tiny tested bridge over rewriting the feature in raw AppKit. |
| 60 | +- If a pattern can remain entirely in `swiftui-patterns`, keep it there. |
| 61 | + |
| 62 | +## Output Expectations |
| 63 | + |
| 64 | +Provide: |
| 65 | + |
| 66 | +- the exact SwiftUI limitation being crossed |
| 67 | +- the smallest recommended bridge type |
| 68 | +- the data-flow boundary between SwiftUI and AppKit |
| 69 | +- the lifecycle or validation risks to watch |
0 commit comments