Creating and Customizing the Touch Bar
At a glance
| Item | Summary |
|---|---|
| Purpose | Adopt Touch Bar support by displaying interactive content and controls for your macOS apps. |
| App architecture | A C/Objective-C header, Objective-C, Swift sample bundle with entry-bearing project variants Objective-C, Swift, each leading to Cocoa / Contacts APIs. |
| Main patterns | View-controller organization, Protocol-oriented abstraction, Delegate or data-source callbacks |
| Project style | 111 scanned source file(s) across C/Objective-C header, Objective-C, Swift, organized around ranked entry, type, and file boundaries. |
| Execution model | Source-visible boundaries: DispatchQueue.main.async, DispatchQueue.global.async; none alone proves a background thread. |
| State/event model | Source-visible mechanisms: NotificationCenter. |
| Key frameworks/packages | Cocoa, Contacts, Foundation; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
├── Objective-C/
│ └── NSTouchBar Catalog/
│ ├── main.m
│ ├── TestViewControllers/
│ │ ├── VisbilityViewController.m
│ │ └── ButtonViewController.m
│ ├── AppDelegate.h
│ ├── AppDelegate.m
│ ├── BackgroundWindow/
│ │ ├── PhotoManager.h
│ │ ├── BackgroundImagesViewController.m
│ │ └── BackgroundViewController.m
│ └── PrimaryViewController.m
└── Swift/
└── NSTouchBar Catalog/
├── ScrubberViewController.swift
├── AppDelegate.swift
└── VisibilityViewController.swift
Structure observations
- Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
- Primary languages: C/Objective-C header, Objective-C, Swift.
- The verified tree contains 56 project/configuration file(s) and 95 source declaration(s).
Overall architecture
flowchart LR
Bundle["Sample bundle"]
V1["Objective-C"]
V2["Swift"]
Boundary["Cocoa / Contacts APIs"]
Bundle --> V1
V1 --> Boundary
Bundle --> V2
V2 --> Boundary
Reference code
Objective-C/NSTouchBar Catalog/main.m:10 — architecture anchor
int main(int argc, const char * argv[])
{
return NSApplicationMain(argc, argv);
}Interpretation
The branches represent separate entry-bearing project variants in the downloaded bundle, not runtime calls between those variants. Each branch is intentionally collapsed at the documented framework boundary; the detailed target-local flow remains in the cited files. Ownership is claimed only where the next section cites a stored property or assignment.
Ownership and state
classDiagram
VisbilityViewController o-- NSTouchBarItem : button1
PhotoManager o-- NSMutableArray : photos
PhotoManager o-- BOOL : loadComplete
PhotoManager o-- id : delegate
Ownership evidence
Objective-C/NSTouchBar Catalog/TestViewControllers/VisbilityViewController.m:23 — stored dependency or nearest verified ownership anchor
@interface VisibilityViewController () <NSTextFieldDelegate>
// ...
@property (weak) IBOutlet NSTouchBarItem *button1;
// ...
@end| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
VisbilityViewController |
NSTouchBarItem (button1) |
holds a non-owning reference | The referenced object’s lifecycle is owned elsewhere |
PhotoManager |
NSMutableArray (photos) |
retains or copies an assigned value | Header-visible collaborators |
PhotoManager |
BOOL (loadComplete) |
holds a non-owning reference | The referenced object’s lifecycle is owned elsewhere |
PhotoManager |
id (delegate) |
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 |
|---|---|---|---|
| Queue scheduling | DispatchQueue.main.async |
The source addresses the main dispatch queue. | Swift/NSTouchBar Catalog/CandidateListViewController.swift:103 |
| Queue scheduling | DispatchQueue.global.async |
The source addresses a global dispatch queue; no stable thread identity is implied. | Swift/NSTouchBar Catalog/PhotoManager.swift:24 |
@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
Swift/NSTouchBar Catalog/CandidateListViewController.swift:103 — representative execution boundary
DispatchQueue.main.async {
candidates = self.candidates(matching: textField.stringValue)
}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. | Swift/NSTouchBar Catalog/BackgroundImagesViewController.swift:28 |
| Source import | Cocoa |
The cited file imports this module; runtime use and architectural role are not inferred. | Objective-C/NSTouchBar Catalog/AppDelegate.h:8 |
| Source import | Contacts |
The cited file imports this module; runtime use and architectural role are not inferred. | Swift/NSTouchBar Catalog/CandidateListViewController.swift:9 |
| Source import | Foundation |
The cited file imports this module; runtime use and architectural role are not inferred. | Swift/NSTouchBar Catalog/PhotoManager.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
Objective-C/NSTouchBar Catalog/BackgroundWindow/PhotoManager.h:11 — representative type boundary
@protocol PhotoManagerDelegate;| Type | Responsibility | Depends on or conforms to |
|---|---|---|
AppDelegate |
Receives callback-driven events | NSObject, NSApplicationDelegate |
PhotoManagerDelegate |
Defines a capability or collaboration contract | Concrete collaborators/imported frameworks |
PhotoManagerDelegate |
Defines a capability or collaboration contract | NSObject |
PhotoManagerDelegate |
Defines a capability or collaboration contract | AnyObject |
ScrubberViewController |
View lifecycle, callbacks, and feature coordination | NSViewController |
AppDelegate |
Receives callback-driven events | NSObject, NSApplicationDelegate |
PhotoManager |
Long-lived feature or framework coordination | NSObject |
VisibilityViewController |
View lifecycle, callbacks, and feature coordination | NSViewController |
IconTextItemView |
User-interface presentation and input forwarding | NSScrubberItemView |
ColorPickerViewController |
View lifecycle, callbacks, and feature coordination | NSViewController |
The source explicitly defines local protocol relationships: BackgroundImagesViewController → PhotoManagerDelegate, ImageScrubberBarItemSample → PhotoManagerDelegate.
Access control
| Symbol | Access | Verified effect | Likely rationale |
|---|---|---|---|
scrollView (Objective-C/NSTouchBar Catalog/BackgroundWindow/BackgroundImagesViewController.m:26) |
implementation |
Visibility follows header/implementation and language linkage rules. | Inference: keep the declaration in the Objective-C implementation boundary. |
tableView (Objective-C/NSTouchBar Catalog/BackgroundWindow/BackgroundImagesViewController.m:27) |
implementation |
Visibility follows header/implementation and language linkage rules. | Inference: keep the declaration in the Objective-C implementation boundary. |
progressIndicator (Objective-C/NSTouchBar Catalog/BackgroundWindow/BackgroundImagesViewController.m:28) |
implementation |
Visibility follows header/implementation and language linkage rules. | Inference: keep the declaration in the Objective-C implementation boundary. |
imageView (Objective-C/NSTouchBar Catalog/BackgroundWindow/BackgroundViewController.h:12) |
header-visible |
The declaration is exposed to translation units that import the header. | Inference: declare a contract needed by other Objective-C/C translation units. |
Reference code
Objective-C/NSTouchBar Catalog/BackgroundWindow/BackgroundImagesViewController.m:26 — representative boundary
@interface BackgroundImagesViewController () < NSTouchBarDelegate,
// ...
@property (nonatomic, weak) IBOutlet NSScrollView *scrollView;
// ...
@endSwift 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 | BackgroundImagesViewController, BackgroundViewController, ButtonViewController, CandidateListViewController |
The source’s Controller suffix makes this role explicit. |
| Receives callback-driven events | AppDelegate, PhotoManagerDelegate |
The source’s Delegate suffix makes this role explicit. |
| Long-lived feature or framework coordination | PhotoManager |
The source’s Manager suffix makes this role explicit. |
| User-interface presentation and input forwarding | CanvasView, CustomBackgroundView, CustomSelectionBackgroundView, CustomSelectionOverlayView |
The source’s View suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| View-controller organization | Objective-C/NSTouchBar Catalog/BackgroundWindow/BackgroundImagesViewController.h:10 |
A controller is the verified coordination boundary; this is MVC-style only where a separate model is present. |
| Protocol-oriented abstraction | Swift/NSTouchBar Catalog/BackgroundImagesViewController.swift:165 |
A local protocol and concrete conformance create an explicit capability boundary. |
| Delegate or data-source callbacks | Objective-C/NSTouchBar Catalog/AppDelegate.h:10 |
Callback protocols invert event delivery back into the sample’s owner. |
Main application flow
sequenceDiagram
participant CandidateListViewController
participant CNContactStore
participant contactStore
participant NSAlert
participant alert
participant candidateListItem
CandidateListViewController->>CNContactStore: authorizationStatusForEntityType()
CandidateListViewController->>CNContactStore: alloc()
CandidateListViewController->>contactStore: completionHandler
CandidateListViewController->>CandidateListViewController: searchForCandidatesWithString()
CandidateListViewController->>CNContactStore: authorizationStatusForEntityType()
CandidateListViewController->>NSAlert: alloc()
CandidateListViewController->>alert: runModal()
CandidateListViewController->>candidateListItem: forSelectedRange()
Reference code
Objective-C/NSTouchBar Catalog/TestViewControllers/CandidateListViewController.m:140 — controlTextDidChange()
@implementation CandidateListViewController
// ...
[contactStore requestAccessForEntityType:entityType completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted)
{
dispatch_async(dispatch_get_main_queue(), ^{
[self searchForCandidatesWithString:textField.stringValue];
});
}
}];
// ...
@endNaming conventions
- Types: Controller: BackgroundImagesViewController, BackgroundViewController, ButtonViewController, CandidateListViewController, ColorPickerViewController; Delegate: AppDelegate, PhotoManagerDelegate; Manager: PhotoManager; View: CanvasView, CustomBackgroundView, CustomSelectionBackgroundView, CustomSelectionOverlayView, CustomView.
- Protocols:
PhotoManagerDelegate,PhotoManagerDelegate,PhotoManagerDelegate. - Methods:
customizeAction,useBackgroundColorAction,kindAction,modeAction,selectionAction,overlayAction,flowAction,spacingSliderAction. - Files:
Swift/NSTouchBar Catalog/ScrubberViewController.swift,Objective-C/NSTouchBar Catalog/AppDelegate.h,Objective-C/NSTouchBar Catalog/AppDelegate.m,Swift/NSTouchBar Catalog/AppDelegate.swift,Objective-C/NSTouchBar Catalog/BackgroundWindow/PhotoManager.h,Swift/NSTouchBar Catalog/VisibilityViewController.swift.
Architecture takeaways
mainis the main source-visible entry or composition anchor for this sample.- Framework work reaches Cocoa, Contacts 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.
- Local protocol relationships provide an explicit substitution boundary.
Source map
| Source file | Relevant symbols |
|---|---|
Objective-C/NSTouchBar Catalog/main.m |
Cited implementation, Feature implementation |
Objective-C/NSTouchBar Catalog/TestViewControllers/VisbilityViewController.m |
Cited implementation, VisibilityViewController, PriorityValueFormatter |
Objective-C/NSTouchBar Catalog/BackgroundWindow/PhotoManager.h |
PhotoManagerDelegate, PhotoManager |
Objective-C/NSTouchBar Catalog/BackgroundWindow/BackgroundImagesViewController.m |
scrollView, tableView, progressIndicator, BackgroundImagesViewController |
Objective-C/NSTouchBar Catalog/BackgroundWindow/BackgroundViewController.h |
imageView, BackgroundViewController |
Objective-C/NSTouchBar Catalog/BackgroundWindow/BackgroundImagesViewController.h |
BackgroundImagesViewController |
Swift/NSTouchBar Catalog/BackgroundImagesViewController.swift |
Cited implementation, NotificationCenter, BackgroundImagesViewController |
Objective-C/NSTouchBar Catalog/AppDelegate.h |
Cited implementation, Cocoa, AppDelegate |
Swift/NSTouchBar Catalog/CandidateListViewController.swift |
DispatchQueue.main.async, Contacts |
Swift/NSTouchBar Catalog/PhotoManager.swift |
DispatchQueue.global.async, Foundation, PhotoManager, PhotoManagerDelegate |
Swift/NSTouchBar Catalog/ScrubberViewController.swift |
KindButtonTag, ModeButtonTag, SelectionBackgroundStyleButtonTag, SelectionOverlayStyleButtonTag, LayoutTypeButtonTag, ScrubberViewController |
Objective-C/NSTouchBar Catalog/AppDelegate.m |
AppDelegate |
Swift/NSTouchBar Catalog/AppDelegate.swift |
AppDelegate |
Swift/NSTouchBar Catalog/VisibilityViewController.swift |
VisibilityViewController, TextFieldTag, PriorityValueFormatter |
Objective-C/NSTouchBar Catalog/BackgroundWindow/BackgroundViewController.m |
BackgroundViewController |
Objective-C/NSTouchBar Catalog/PrimaryViewController.m |
PrimaryViewController |
Objective-C/NSTouchBar Catalog/TestViewControllers/CandidateListViewController.m |
controlTextDidChange |