Repository navigation
Fix dock icon not auto-switching with system dark mode #1928
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -3684,21 +3684,49 @@ enum AppIconSettings { | |||||
| static func applyIcon(_ mode: AppIconMode) { | ||||||
| switch mode { | ||||||
| case .automatic: | ||||||
| // Let the asset catalog handle appearance-based icon selection (macOS 15+). | ||||||
| // Reset to the default bundle icon. | ||||||
| NSApplication.shared.applicationIconImage = nil | ||||||
| AppIconAppearanceObserver.shared.startObserving() | ||||||
| case .light: | ||||||
| AppIconAppearanceObserver.shared.stopObserving() | ||||||
| if let icon = NSImage(named: "AppIconLight") { | ||||||
| NSApplication.shared.applicationIconImage = icon | ||||||
| } | ||||||
| case .dark: | ||||||
| AppIconAppearanceObserver.shared.stopObserving() | ||||||
| if let icon = NSImage(named: "AppIconDark") { | ||||||
| NSApplication.shared.applicationIconImage = icon | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| final class AppIconAppearanceObserver: NSObject { | ||||||
| static let shared = AppIconAppearanceObserver() | ||||||
| private var observation: NSKeyValueObservation? | ||||||
|
|
||||||
| func startObserving() { | ||||||
| applyIconForCurrentAppearance() | ||||||
| guard observation == nil else { return } | ||||||
| observation = NSApp.observe(\.effectiveAppearance, options: [.new]) { [weak self] _, _ in | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| DispatchQueue.main.async { | ||||||
| self?.applyIconForCurrentAppearance() | ||||||
|
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
|
||||||
| } | ||||||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||||||
| } | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When the user switches from Concrete sequence:
A simple guard using a flag (or checking the current mode before applying) prevents the stale dispatch from firing: func stopObserving() {
observation?.invalidate()
observation = nil
}
private func applyIconForCurrentAppearance() {
guard observation != nil else { return } // drop stale async calls
let isDark = NSApp.effectiveAppearance.bestMatch(from: [.darkAqua, .aqua]) == .darkAqua
let imageName = isDark ? "AppIconDark" : "AppIconLight"
if let icon = NSImage(named: imageName) {
NSApplication.shared.applicationIconImage = icon
}
} |
||||||
| } | ||||||
|
|
||||||
| func stopObserving() { | ||||||
| observation?.invalidate() | ||||||
| observation = nil | ||||||
| } | ||||||
|
|
||||||
| private func applyIconForCurrentAppearance() { | ||||||
| let isDark = NSApp.effectiveAppearance.bestMatch(from: [.darkAqua, .aqua]) == .darkAqua | ||||||
| let imageName = isDark ? "AppIconDark" : "AppIconLight" | ||||||
| if let icon = NSImage(named: imageName) { | ||||||
| NSApplication.shared.applicationIconImage = icon | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| enum QuitWarningSettings { | ||||||
| static let warnBeforeQuitKey = "warnBeforeQuitShortcut" | ||||||
| static let defaultWarnBeforeQuit = true | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private init()AppIconAppearanceObserverexposesstatic let sharedbut has noprivate init(), so callers outside this file can still create independent instances (e.g.AppIconAppearanceObserver()) that each set up their own KVO observation without going through the shared lifecycle. Adding aprivate init()makes the singleton intent explicit and prevents accidental misuse.