Enhancing your custom text engine with Writing Tools
At a glance
| Item | Summary |
|---|---|
| Purpose | Add Writing Tools support to your custom text engine to enhance the text editing experience. |
| App architecture | A Swift sample with the source-visible chain WritingToolsSampleApp → DocumentViewController → DocumentViewModel → Cocoa APIs. |
| Main patterns | Model-View-ViewModel, View-controller organization, Delegate or data-source callbacks |
| Project style | 14 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
| Execution model | Source-visible boundaries: @MainActor, Task closure isolated to MainActor, Task; none alone proves a background thread. |
| State/event model | Source-visible mechanisms: NotificationCenter. |
| Key frameworks/packages | Cocoa; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
└── WritingToolsSampleApp/
├── WritingToolsSampleApp.swift
├── ViewModels/
│ ├── DocumentViewModel.swift
│ ├── DocumentViewModel+Formatting.swift
│ ├── DocumentViewModel+Input.swift
│ ├── DocumentViewModel+Pasteboard.swift
│ └── DocumentViewModel+Selection.swift
└── Views/
├── DocumentView/
│ ├── DocumentView.swift
│ └── DocumentView+WritingToolsCoordinator.swift
├── DocumentViewController.swift
├── Misc/
│ ├── TextLayoutFragmentView.swift
│ └── TextSelectionView.swift
└── WindowController.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 8 source declaration(s).
Overall architecture
flowchart LR
N1["WritingToolsSampleApp"]
N2["DocumentViewController"]
N3["DocumentViewModel"]
N4["Cocoa APIs"]
N1 --> N2
N2 --> N3
N3 --> N4
Reference code
WritingToolsSampleApp/WritingToolsSampleApp.swift:10 — architecture anchor
@main
class AppDelegate: NSObject, NSApplicationDelegate {
var windowController: NSWindowController?
func applicationDidFinishLaunching(_ aNotification: Notification) {
let storyboard = NSStoryboard(name: "Main", bundle: nil)
windowController = storyboard.instantiateController(withIdentifier: "MainWindowController") as? NSWindowController
windowController?.showWindow(self)
windowController?.window?.makeKeyAndOrderFront(self)
}
func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {
return true
}
}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 AppKit.
Ownership and state
classDiagram
AppDelegate o-- NSWindowController : windowController
DocumentViewModel *-- NSTextContentStorage : textContentStorage
DocumentViewModel *-- NSTextLayoutManager : textLayoutManager
DocumentViewModel o-- ViewModelDelegate : viewModelDelegate
Ownership evidence
WritingToolsSampleApp/WritingToolsSampleApp.swift:12 — stored dependency or nearest verified ownership anchor
@main
class AppDelegate: NSObject, NSApplicationDelegate {
var windowController: NSWindowController?
// ...
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
AppDelegate |
NSWindowController (windowController) |
stores or receives | App/module collaborators |
DocumentViewModel |
NSTextContentStorage (textContentStorage) |
creates and retains | Initialized by the owner; the binding is immutable |
DocumentViewModel |
NSTextLayoutManager (textLayoutManager) |
creates and retains | Initialized by the owner; the binding is immutable |
DocumentViewModel |
ViewModelDelegate (viewModelDelegate) |
holds a non-owning reference | The referenced object’s lifecycle is owned elsewhere |
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.
| Concern | Source mechanism | Verified placement or handoff | Evidence |
|---|---|---|---|
| Main isolation | @MainActor |
The cited annotation marks its attached declaration or closure as main-actor isolated. | WritingToolsSampleApp/Views/DocumentView/DocumentView.swift:269 |
| Main isolation | Task closure isolated to MainActor |
The cited operation explicitly enters a main-actor-isolated region. | WritingToolsSampleApp/Views/DocumentView/DocumentView.swift:269 |
| Task creation | Task |
The source creates an unstructured task; surrounding context determines inherited actor isolation. | WritingToolsSampleApp/Views/DocumentView/DocumentView.swift:269 |
@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.
Reference code
WritingToolsSampleApp/Views/DocumentView/DocumentView.swift:269 — representative execution boundary
Task { @MainActor in
self?.needsLayout = true
}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 | NotificationCenter |
NotificationCenter distributes named process-local events. | WritingToolsSampleApp/Views/DocumentView/DocumentView.swift:255 |
| Source import | Cocoa |
The cited file imports this module; runtime use and architectural role are not inferred. | WritingToolsSampleApp/ViewModels/DocumentViewModel+Formatting.swift:8 |
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
WritingToolsSampleApp/ViewModels/DocumentViewModel.swift:70 — representative type boundary
protocol ViewModelDelegate: AnyObject {
func textDidChange()
func selectionDidChange()
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
AppDelegate |
Receives callback-driven events | NSObject, NSApplicationDelegate |
ViewModelDelegate |
Defines a capability or collaboration contract | AnyObject |
DocumentViewModel |
UI-facing state and feature coordination | NSObject |
DocumentView |
User-interface presentation and input forwarding | NSView, @preconcurrency NSTextViewportLayoutControllerDelegate |
DocumentViewController |
View lifecycle, callbacks, and feature coordination | NSViewController |
TextLayoutFragmentView |
User-interface presentation and input forwarding | NSView |
TextSelectionView |
User-interface presentation and input forwarding | NSView |
WindowController |
View lifecycle, callbacks, and feature coordination | NSWindowController |
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 |
|---|---|---|---|
performDelete (WritingToolsSampleApp/Views/DocumentView/DocumentView+Editing.swift:113) |
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. |
boundsDidChangeObserver (WritingToolsSampleApp/Views/DocumentView/DocumentView.swift:28) |
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. |
selectionView (WritingToolsSampleApp/Views/DocumentView/DocumentView.swift:30) |
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. |
lastFragmentViews (WritingToolsSampleApp/Views/DocumentView/DocumentView.swift:31) |
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
WritingToolsSampleApp/Views/DocumentView/DocumentView+Editing.swift:113 — representative boundary
private func performDelete(forward: Bool) {
// ...
return
// ...
}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 |
|---|---|---|
| View lifecycle, callbacks, and feature coordination | DocumentViewController, WindowController |
The source’s Controller suffix makes this role explicit. |
| Receives callback-driven events | AppDelegate, ViewModelDelegate |
The source’s Delegate suffix makes this role explicit. |
| User-interface presentation and input forwarding | DocumentView, TextLayoutFragmentView, TextSelectionView |
The source’s View suffix makes this role explicit. |
| UI-facing state and feature coordination | DocumentViewModel |
The source’s ViewModel suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| Model-View-ViewModel | WritingToolsSampleApp/ViewModels/DocumentViewModel.swift:10 |
Role-named view models keep UI-facing state or coordination outside view declarations. |
| View-controller organization | WritingToolsSampleApp/Views/DocumentViewController.swift:10 |
A controller is the verified coordination boundary; this is MVC-style only where a separate model is present. |
| Delegate or data-source callbacks | WritingToolsSampleApp/Views/DocumentView/DocumentView.swift:10 |
Callback protocols invert event delivery back into the sample’s owner. |
Naming conventions
- Types: Controller: DocumentViewController, WindowController; Delegate: AppDelegate, ViewModelDelegate; View: DocumentView, TextLayoutFragmentView, TextSelectionView; ViewModel: DocumentViewModel.
- Protocols:
ViewModelDelegate. - Methods:
applicationDidFinishLaunching,applicationSupportsSecureRestorableState,textDidChange,selectionDidChange,replaceText,clearText,configureWritingTools,layout. - Files:
WritingToolsSampleApp/ViewModels/DocumentViewModel.swift,WritingToolsSampleApp/Views/DocumentView/DocumentView.swift,WritingToolsSampleApp/Views/DocumentViewController.swift,WritingToolsSampleApp/Views/Misc/TextLayoutFragmentView.swift,WritingToolsSampleApp/Views/Misc/TextSelectionView.swift,WritingToolsSampleApp/Views/WindowController.swift.
Architecture takeaways
WritingToolsSampleAppis the main source-visible entry or composition anchor for this sample.- Framework work reaches Cocoa 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 |
|---|---|
WritingToolsSampleApp/WritingToolsSampleApp.swift |
Cited implementation, AppDelegate |
WritingToolsSampleApp/ViewModels/DocumentViewModel.swift |
ViewModelDelegate, DocumentViewModel |
WritingToolsSampleApp/Views/DocumentView/DocumentView+Editing.swift |
Cited implementation, Feature implementation |
WritingToolsSampleApp/Views/DocumentView/DocumentView.swift |
Cited implementation, @MainActor, Task closure isolated to MainActor, Task, NotificationCenter, DocumentView |
WritingToolsSampleApp/Views/DocumentViewController.swift |
DocumentViewController |
WritingToolsSampleApp/ViewModels/DocumentViewModel+Formatting.swift |
Cocoa, Feature implementation |
WritingToolsSampleApp/Views/Misc/TextLayoutFragmentView.swift |
TextLayoutFragmentView |
WritingToolsSampleApp/Views/Misc/TextSelectionView.swift |
TextSelectionView |
WritingToolsSampleApp/Views/WindowController.swift |
WindowController |
WritingToolsSampleApp/Views/DocumentView/DocumentView+WritingToolsCoordinator.swift |
Feature implementation |
WritingToolsSampleApp/ViewModels/DocumentViewModel+Input.swift |
Feature implementation |
WritingToolsSampleApp/ViewModels/DocumentViewModel+Pasteboard.swift |
Feature implementation |
WritingToolsSampleApp/ViewModels/DocumentViewModel+Selection.swift |
Feature implementation |
WritingToolsSampleApp/Views/DocumentView/DocumentView+NSServicesMenuRequestor.swift |
Feature implementation |