Last active
July 9, 2022 17:37
-
-
Save GroovinChip/06697ecf39daa8eacd3582d8d2b938e3 to your computer and use it in GitHub Desktop.
Attempt at a Swift class that listens to changes to the system accent color. Doesn't seem to work at the moment.
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
extension NSColor { | |
var asFlutterHexString: String { | |
let red = Int(round(self.redComponent * 0xFF)) | |
let green = Int(round(self.greenComponent * 0xFF)) | |
let blue = Int(round(self.blueComponent * 0xFF)) | |
let hexString = NSString(format: "#%02X%02X%02X", red, green, blue) | |
return hexString.replacingOccurrences(of: "#", with: "0xFF") as String | |
} | |
} |
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
// | |
// SystemAccentColorProvider.swift | |
// macos_ui | |
// | |
// Created by Reuben Turner on 7/9/22. | |
// | |
import FlutterMacOS | |
import AppKit | |
class SystemAccentColorProvider: NSObject, FlutterStreamHandler { | |
var eventSink: FlutterEventSink? | |
var accentColor: NSColor? | |
func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? { | |
eventSink = events | |
return nil | |
} | |
@available(macOS 10.13, *) | |
@objc public func startStream() { | |
NotificationCenter.default.addObserver( | |
self, | |
selector: #selector(accentColorChanged), | |
name: NSNotification.Name("AppleColorPreferencesChangedNotification"), | |
object: nil) | |
let hexColor = accentColor!.usingColorSpace(NSColorSpace.deviceRGB) ?? accentColor | |
eventSink?(hexColor?.asFlutterHexString) | |
} | |
func onCancel(withArguments arguments: Any?) -> FlutterError? { | |
eventSink = nil | |
return nil | |
} | |
@available(macOS 10.13, *) | |
@objc private func accentColorChanged(notification: NSNotification) -> NSColor { | |
print(notification.userInfo!) | |
let MacAccentColor = (NSColor.value(forKey: "controlAccentColor") as? NSColor ?? NSColor(named: "AccentColor")!) | |
accentColor = MacAccentColor | |
return MacAccentColor | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If anyone has any thoughts on why this doesn't work, please comment below.