Skip to content

Commit c4389da

Browse files
committed
Added option to disable theme transition animation
1 parent fc6d14f commit c4389da

File tree

2 files changed

+52
-44
lines changed

2 files changed

+52
-44
lines changed

Demo/Demo/AppDelegate.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
4949
// 2.4 Set default theme (default: macOS theme `ThemeManager.systemTheme`)
5050
ThemeManager.defaultTheme = ThemeManager.lightTheme
5151

52-
// 2.5 Apply last applied theme, or the default one
52+
// 2.5 Animate theme transitions? (default = true)
53+
//ThemeManager.shared.animateThemeTransitions = false
54+
55+
// 2.6 Apply last applied theme, or the default one
5356
ThemeManager.shared.applyLastOrDefaultTheme()
5457

5558
}

Sources/ThemeManager.swift

Lines changed: 48 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,9 @@ public class ThemeManager: NSObject {
420420
// MARK: -
421421
// MARK: Theme Switching
422422

423+
/// Animate theme transitions?
424+
public var animateThemeTransitions: Bool = true
425+
423426
/// Keypath for string `values.ThemeKitTheme`.
424427
private var themeChangeKVOKeyPath: String = "values.\(ThemeManager.userDefaultsThemeKey)"
425428

@@ -487,57 +490,59 @@ public class ThemeManager: NSObject {
487490
applyAndPropagate(newTheme)
488491
}
489492

490-
// Animate theme transition
491-
Thread.onMain {
492-
// Find windows to animate
493-
let windows = NSWindow.windowsCompliantWithWindowThemePolicy()
494-
guard windows.count > 0 else {
495-
// Change theme without animation
496-
makeThemeEffective(newTheme)
497-
return
498-
}
499-
500-
// Create transition windows off-screen
501-
var transitionWindows = [Int: NSWindow]()
502-
for window in windows {
503-
let windowNumber = window.windowNumber
504-
/* Make sure the window has a number, and that it's not one of our
505-
* existing transition windows */
506-
if windowNumber > 0 && !self.themeTransitionWindows.contains(window) {
507-
let transitionWindow = window.makeScreenshotWindow()
508-
transitionWindows[windowNumber] = transitionWindow
509-
self.themeTransitionWindows.insert(transitionWindow)
493+
Thread.onMain { [unowned self] in
494+
// Animate theme transition
495+
if self.animateThemeTransitions {
496+
// Find windows to animate
497+
let windows = NSWindow.windowsCompliantWithWindowThemePolicy()
498+
guard windows.count > 0 else {
499+
// Change theme without animation
500+
makeThemeEffective(newTheme)
501+
return
510502
}
511-
}
512503

513-
// Show (if we have at least one window to animate)
514-
if transitionWindows.count > 0 {
515-
// Show them all (hidden)
516-
for (windowNumber, transitionWindow) in transitionWindows {
517-
transitionWindow.alphaValue = 0.0
518-
let parentWindow = NSApp.window(withWindowNumber: windowNumber)
519-
parentWindow?.addChildWindow(transitionWindow, ordered: .above)
504+
// Create transition windows off-screen
505+
var transitionWindows = [Int: NSWindow]()
506+
for window in windows {
507+
let windowNumber = window.windowNumber
508+
/* Make sure the window has a number, and that it's not one of our
509+
* existing transition windows */
510+
if windowNumber > 0 && !self.themeTransitionWindows.contains(window) {
511+
let transitionWindow = window.makeScreenshotWindow()
512+
transitionWindows[windowNumber] = transitionWindow
513+
self.themeTransitionWindows.insert(transitionWindow)
514+
}
520515
}
521516

522-
// Setup animation
523-
NSAnimationContext.beginGrouping()
524-
let ctx = NSAnimationContext.current
525-
ctx.duration = 0.3
526-
ctx.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
527-
ctx.completionHandler = {() -> Void in
517+
// Show (if we have at least one window to animate)
518+
if transitionWindows.count > 0 {
519+
// Show them all (hidden)
520+
for (windowNumber, transitionWindow) in transitionWindows {
521+
transitionWindow.alphaValue = 0.0
522+
let parentWindow = NSApp.window(withWindowNumber: windowNumber)
523+
parentWindow?.addChildWindow(transitionWindow, ordered: .above)
524+
}
525+
526+
// Setup animation
527+
NSAnimationContext.beginGrouping()
528+
let ctx = NSAnimationContext.current
529+
ctx.duration = 0.3
530+
ctx.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
531+
ctx.completionHandler = {() -> Void in
532+
for transitionWindow in transitionWindows.values {
533+
transitionWindow.orderOut(self)
534+
self.themeTransitionWindows.remove(transitionWindow)
535+
}
536+
}
537+
538+
// Show them all and fade out
528539
for transitionWindow in transitionWindows.values {
529-
transitionWindow.orderOut(self)
530-
self.themeTransitionWindows.remove(transitionWindow)
540+
transitionWindow.alphaValue = 1.0
541+
transitionWindow.animator().alphaValue = 0.0
531542
}
532-
}
543+
NSAnimationContext.endGrouping()
533544

534-
// Show them all and fade out
535-
for transitionWindow in transitionWindows.values {
536-
transitionWindow.alphaValue = 1.0
537-
transitionWindow.animator().alphaValue = 0.0
538545
}
539-
NSAnimationContext.endGrouping()
540-
541546
}
542547

543548
// Actually change theme

0 commit comments

Comments
 (0)