{"id":18031751,"url":"https://github.com/dagronf/dsfappearancemanager","last_synced_at":"2025-03-27T05:30:57.230Z","repository":{"id":63907288,"uuid":"481432030","full_name":"dagronf/DSFAppearanceManager","owner":"dagronf","description":"Theme and Appearance handling for macOS Appkit (Swift/Objective-C).","archived":false,"fork":false,"pushed_at":"2025-02-23T04:08:46.000Z","size":1707,"stargazers_count":16,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-23T04:31:39.610Z","etag":null,"topics":["accessibility","appearance","appkit","dark-mode","dark-theme","light-mode","macos","objective-c","swift","theme"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dagronf.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-04-14T01:51:58.000Z","updated_at":"2025-02-24T11:19:29.000Z","dependencies_parsed_at":"2024-10-30T10:21:06.638Z","dependency_job_id":null,"html_url":"https://github.com/dagronf/DSFAppearanceManager","commit_stats":{"total_commits":29,"total_committers":1,"mean_commits":29.0,"dds":0.0,"last_synced_commit":"676cfd4e6aaa3cf6422990734c22544373db5c1a"},"previous_names":[],"tags_count":32,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dagronf%2FDSFAppearanceManager","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dagronf%2FDSFAppearanceManager/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dagronf%2FDSFAppearanceManager/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dagronf%2FDSFAppearanceManager/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dagronf","download_url":"https://codeload.github.com/dagronf/DSFAppearanceManager/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245791335,"owners_count":20672665,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["accessibility","appearance","appkit","dark-mode","dark-theme","light-mode","macos","objective-c","swift","theme"],"created_at":"2024-10-30T10:10:39.607Z","updated_at":"2025-03-27T05:30:57.220Z","avatar_url":"https://github.com/dagronf.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DSFAppearanceManager\n\n![](https://img.shields.io/github/v/tag/dagronf/DSFAppearanceManager)\n![](https://img.shields.io/badge/macOS-10.11+-red) \n![](https://img.shields.io/badge/Swift-5.3+-orange.svg)\n![](https://img.shields.io/badge/ObjectiveC-2.0-purple.svg)\n\n![](https://img.shields.io/badge/License-MIT-lightgrey) \n[![](https://img.shields.io/badge/spm-compatible-brightgreen.svg?style=flat)](https://swift.org/package-manager)\n\nA class for simplifying macOS appearance values and detecting setting changes (Swift/Objective-C).\n\nSupported back to macOS 10.11 with sensible fallbacks on older systems to reduce the `#available/@available` dance in your code.\n\n## Why?\n\nIf you're performing custom drawing within your macOS app, it's important to obey the user's display and accessibility settings when performing your drawing so you can adapt accordingly.\n\n1. On different macOS systems, the method for retrieving these values can differ (and on earlier systems are quite difficult to extract reliably). This library wraps away all these inconsistencies so your code can remain clean(er).\n2. When the user changes their settings (eg. when the system changes automatically light/dark modes) I wanted my app to be notified of the change so I can update the drawing to match the new setting(s).\n\n## Appearance\n\n`DSFAppearanceManager` has a number of properties to simplify macOS appearance settings\n\n### Available properties\n\nThese are the static properties available on the `DSFAppearanceManager`\n\n| Properties                       | Description                                                               |\n|:---------------------------------|:--------------------------------------------------------------------------|\n| `IsDark`                         | Is the UI currently being displayed as dark                               |\n| `IsDarkMenu`                     | Are the menu and dock currently being displayed as dark                   |\n| `AccentColor`                    | The current accent color                                                  |\n| `HighlightColor`                 | The current highlight color                                               |\n| `AquaVariant`                    | The current aqua variant                                                  |\n| `IncreaseContrast`               | The user's 'Increase Contrast' accessibility setting                      |\n| `DifferentiateWithoutColor`      | The user's 'Differentiate without color' accessibility setting            |\n| `ReduceTransparency`             | The user's 'Reduce transparency' accessibility setting                    |\n| `InvertColors`                   | The user's 'Invert colors' accessibility setting                          |\n| `ReduceMotion`                   | The user's 'Reduce motion' accessibility setting                          |\n| `AutoplayAnimatedImages`         | The user's 'Auto-play animated images' accessibility setting (macOS 14+)  |\n| `SimulatedHardwareColor`         | The color representing the user's hardware color (iMac 2021-)             |\n| `IsUsingSimulatedHardwareColor`  | Is the user using the hardware color as the accent color (iMac 2021-)     |\n\nSo, for example, to get the current macOS highlight color, call `DSFAppearanceManager.HighlightColor`.\n\n## Change detection\n\nYou can ask to be notified when appearance settings changes. macOS calls some methods automatically when\nthe appearance changes :-\n\n### NSView\n\n* `updateLayer`\n* `drawRect(dirtyRect: NSRect)`\n* `layout`\n* `updateConstraints`\n\n### NSViewController\n\n* `updateViewConstraints`\n* `viewWillLayout`\n* `viewDidLayout`\n\nbut there are times where you need to manage this yourself. This is where the `ChangeDetector` class is used.\n\nDeclare a variable of type `DSFAppearanceManager.ChangeDetector()`\n\n```swift\nprivate let appearanceChangeDetector = DSFAppearanceManager.ChangeDetector()\n```\n\n... and set the callback block. Note that this callback is guaranteed to be called on the main thread.\n\n```swift\nappearanceChangeDetector.appearanceChangeCallback = { [weak self] change in\n   // Handle the change here.\n   // `change` contains the _types_ of change(s) that occurred. This might be theme, accent, contrastOrAccessibility etc\n   let currentHighlightColor = DSFAppearanceManager.HighlightColor\n   ...\n}\n```\n\n### Change detection types\n\nThe change object indicates the type of change that occurred.\n\n| Change type                | Description                                               |\n|:---------------------------|:----------------------------------------------------------|\n| `theme`                    | The system appearance (eg. dark/light) changed            |\n| `accent`                   | The user changed the accent color(s) eg. accent/highlight |\n| `aquaVariant`              | For older macOS versions, the variant (blue, graphite)    |\n| `systemColors`             | The user changed the system colors                        |\n| `finderLabelColorsChanged` | The user changed finder label color(s)                    |\n| `accessibility`            | The accessibility display settings changed                |\n| `autoplayAnimatedImages`   | The auto-play animated images changed                     |\n\nNote that the change detection class debounces changes to reduce the number of callbacks when a change occurs.\nThe `change` object passed in the callback block contains a set of the changes that occurred.\n\n## Objective-C support\n\n```objc\n@interface ViewController ()\n@property(nonatomic, strong) DSFAppearanceManagerChangeDetector* detector;\n@end\n\n@implementation ViewController\n- (void)viewDidAppear {\n   [super viewDidAppear];\n   [self setDetector: [[DSFAppearanceManagerChangeDetector alloc] init]];\n   [[self detector] setAppearanceChangeCallback:^(DSFAppearanceManagerChange * _Nonnull change) {\n      // Change detected! Do something to update display\n   }];\n}\n@end\n```\n\n## Centralized notifications (DSFAppearanceCache)\n\nIf you have lots and lots of little classes that need to be updated, it may be more efficient to centralize the change notifications in a common location.\n\nThe library provides a default global (lazy) `DSFAppearanceCache.shared` object instance you can use,\nor you can create and manage one yourself.\n\nThe appearance cache provides two mechanisms for receiving appearance update notifications.\n\n### Register for updates directly with the cache \n\nYou can register an object to receive appearance updates by conforming your object to the `DSFAppearanceCacheNotifiable` protocol.  The object is held weakly within the cache object.\n\n#### Example\n\n```swift\nclass LevelGauge: CustomLayer, DSFAppearanceCacheNotifiable {\n   init() {\n      DSFAppearanceCache.shared.register(self)\n   }\n\n   deinit {\n      DSFAppearanceCache.shared.deregister(self)\n   }\n\n   func appearanceDidChange() {\n      // Update the object\n   }\n}\n```\n\n### Register for updates via NotificationCenter\n\nThe change center object `DSFAppearanceCache` generates notifications on `NotificationCenter.default`.\n\n**Notification name:** `DSFAppearanceCache.ChangeNotificationName`\n\nYou can register for notifications using the standard `addObserver` mechanisms.\n\n#### Example\n\n```swift\nself.observer = NotificationCenter.default.addObserver(\n   forName: DSFAppearanceCache.ChangeNotificationName,\n   object: DSFAppearanceCache.shared,\n   queue: OperationQueue.main) { _ in\n      // Do something with the change\n}\n```\n\nMake sure to keep the code in your 'appearanceDidChange' fast!\n\n## Additional support\n\n### `NSView` appearance drawing\n\n`DSFAppearanceManager` provides extensions to `NSView` as a convenience for automatically handling the view's effective drawing appearance.\n\n```swift\nfunc drawRect(_ dirtyRect: CGRect) {\n   ...\n   self.usingEffectiveAppearance {\n      // Requests for dynamic colors etc. within the block will automatically use the correct appearance for the view.\n   }\n}\n```\n\n## Rolling your own dynamic `NSColor`\n\nIf you can't use the `Assets.xcassets` to store your dynamic `NSColor`s (or you want to move your app's configuration into code) you'll find that the default `NSColor` doesn't have much support for automatically handling light/dark mode changes.\n \n[Dusk](https://github.com/ChimeHQ/Dusk) is a small swift framework to aid in supporting Dark Mode on macOS. It provides an `NSColor` subclass (`DynamicColor`) that automatically provides light/dark mode variants when required.\n\n```swift\nlazy var c1 = DynamicColor(name: \"uniqueColorName\") { (appearance) in \n    // return the color to use for this appearance\n}\n\nlet c1 = DynamicColor(name: \"uniqueColorName\", lightColor: NSColor.white, darkColor: NSColor.black)\n```\n\nAnd because `DynamicColor` inherits from `NSColor`, it can be used wherever `NSColor` can be used.\n\n## Thanks!\n\n[`ChimeHQ`](https://github.com/ChimeHQ) for developing the awesome [dynamic NSColor subclass](https://github.com/ChimeHQ/Dusk).\n\n# License\n\n```\nMIT License\n\nCopyright (c) 2025 Darren Ford\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdagronf%2Fdsfappearancemanager","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdagronf%2Fdsfappearancemanager","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdagronf%2Fdsfappearancemanager/lists"}