Created
December 19, 2018 11:25
-
-
Save DivineDominion/b648ccfd1021bb1acdc93a52089e70d7 to your computer and use it in GitHub Desktop.
`NSAppearance` change notification when you cannot use `NSApp.effectiveAppearance` which is available for macOS 10.14+ only
This file contains hidden or 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
import Cocoa | |
import RxSwift | |
import RxCocoa | |
@NSApplicationMain | |
class AppDelegate: NSObject, NSApplicationDelegate { | |
@IBOutlet weak var window: NSWindow! | |
let disposeBag = DisposeBag() | |
func applicationDidFinishLaunching(_ aNotification: Notification) { | |
setUpAppearanceChangeNotifications() | |
consumeAppearanceChangeNotificiations() | |
} | |
fileprivate func setUpAppearanceChangeNotifications() { | |
window!.contentView!.rx | |
.isDarkMode | |
.startWith(NSAppearance.current.isDarkMode) | |
.subscribe { isDarkMode in | |
NotificationCenter.default.post( | |
name: NSView.darkModeDidChange, | |
object: nil, | |
userInfo: [NSAppearance.isDarkModeKey : isDarkMode]) | |
}.disposed(by: disposeBag) | |
} | |
fileprivate func consumeAppearanceChangeNotificiations() { | |
let darkModeChange = NotificationCenter.default.rx | |
.notification(NSView.darkModeDidChange) | |
.map { $0.userInfo?[NSAppearance.isDarkModeKey] as? Bool } | |
.filter { $0 != nil }.map { $0! } | |
darkModeChange.debug("Dark Mode Change Signal").subscribe().disposed(by: disposeBag) | |
} | |
} |
This file contains hidden or 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
import Cocoa | |
extension NSView { | |
static var darkModeDidChange: Notification.Name { return .init(rawValue: "NSAppearance did change") } | |
} | |
extension NSAppearance { | |
static var isDarkModeKey: String { return "isDarkMode" } | |
} |
This file contains hidden or 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
import Cocoa | |
// Adapted from https://indiestack.com/2018/10/supporting-dark-mode-checking-appearances/ | |
extension NSAppearance { | |
public var isDarkMode: Bool { | |
if #available(macOS 10.14, *) { | |
if self.bestMatch(from: [.darkAqua, .aqua]) == .darkAqua { | |
return true | |
} else { | |
return false | |
} | |
} else { | |
return false | |
} | |
} | |
} |
This file contains hidden or 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
import Cocoa | |
import RxSwift | |
extension Reactive where Base: NSView { | |
var effectiveAppearance: ControlEvent<NSAppearance> { | |
let source = base.rx | |
.observe(NSAppearance.self, "effectiveAppearance", options: [.new]) | |
.map { $0 ?? NSAppearance.current } | |
return ControlEvent(event: source) | |
} | |
var isDarkMode: ControlEvent<Bool> { | |
return base.rx.effectiveAppearance | |
.map { $0.isDarkMode } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment