Skip to content

Instantly share code, notes, and snippets.

@avaidyam
Last active February 11, 2022 14:20
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avaidyam/6d0e3605cf85b10f4d0f9d654518e984 to your computer and use it in GitHub Desktop.
Save avaidyam/6d0e3605cf85b10f4d0f9d654518e984 to your computer and use it in GitHub Desktop.
Animated dark mode transition
// swiftc this file & mv to /usr/local/bin
import Cocoa
typealias NSGlobalPreferenceTransitionBlock = @convention(block) () -> Void
@objc protocol NSGlobalPreferenceTransitionProtocol: NSObjectProtocol {
static func transition() -> AnyObject
func waitForTransitionWithCompletionHandler(_ arg1: @escaping NSGlobalPreferenceTransitionBlock)
func postChangeNotification(_ arg1: UInt64, completionHandler arg2: @escaping NSGlobalPreferenceTransitionBlock)
}
let NSGlobalPreferenceTransition: AnyClass = NSClassFromString("NSGlobalPreferenceTransition")!
let libSkyLight = dlopen("/System/Library/PrivateFrameworks/SkyLight.framework/Skylight", RTLD_LAZY)
let SLSGetAppearanceThemeLegacy = unsafeBitCast(dlsym(
libSkyLight,
"SLSGetAppearanceThemeLegacy"
), to: (@convention (c) () -> Bool).self)
let SLSSetAppearanceThemeLegacy = unsafeBitCast(dlsym(
libSkyLight,
"SLSSetAppearanceThemeLegacy"
), to: (@convention (c) (Bool) -> Void).self)
let SLSSetAppearanceThemeNotifying = unsafeBitCast(dlsym(
libSkyLight,
"SLSSetAppearanceThemeNotifying"
), to: (@convention (c) (Bool, Bool) -> Void).self)
DispatchQueue.main.async {
SLSSetAppearanceThemeNotifying(!SLSGetAppearanceThemeLegacy(), false)
let transition: AnyObject? = NSGlobalPreferenceTransition.transition()
transition?.postChangeNotification(0, completionHandler: {})
transition?.waitForTransitionWithCompletionHandler { exit(0) }
}
NSApplication.shared.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment