Last active
April 16, 2021 22:26
-
-
Save dagronf/8c3e1dcc0f8175365f3055bacd9c99fa to your computer and use it in GitHub Desktop.
macOS detecting contrast changes using notifications (NSWorkspaceAccessibilityDisplayOptionsDidChangeNotification, NSWorkspace.accessibilityDisplayOptionsDidChangeNotification)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// NSWorkspace.accessibilityDisplayOptionsDidChangeNotification fires when option changes. | |
// The only thing is, it fires in NSWorkspace's notification center. Not in the default one. | |
///// Using a selector | |
NSWorkspace.shared.notificationCenter.addObserver( | |
self, | |
selector: #selector(accessibilityDisplayOptionsDidChange(_:)), | |
name: NSWorkspace.accessibilityDisplayOptionsDidChangeNotification, | |
object: NSWorkspace.shared) | |
// ... | |
@objc func accessibilityDisplayOptionsDidChange(_ notification: Notification) { | |
// Do whatever you need when the contrast settings change | |
} | |
///// Using a block | |
NSWorkspace.shared.notificationCenter.addObserver( | |
forName: NSWorkspace.accessibilityDisplayOptionsDidChangeNotification, | |
object: NSWorkspace.shared, | |
queue: nil) { (_) in | |
// Do whatever you need when the contrast settings change | |
} | |
// Then you have to use NSWorkspace.shared.accessibilityDisplayShouldIncreaseContrast property | |
// in your code to alter the appearance of your UI when necessary. | |
// https://stackoverflow.com/a/49530299/146361 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment