Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions Sources/cmuxApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Comment on lines +3702 to +3703

ghost Mar 22, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Singleton missing private init()

AppIconAppearanceObserver exposes static let shared but has no private 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 a private init() makes the singleton intent explicit and prevents accidental misuse.

Suggested change
final class AppIconAppearanceObserver: NSObject {
static let shared = AppIconAppearanceObserver()
final class AppIconAppearanceObserver: NSObject {
static let shared = AppIconAppearanceObserver()
private var observation: NSKeyValueObservation?
private override init() { super.init() }

private var observation: NSKeyValueObservation?

func startObserving() {
applyIconForCurrentAppearance()
guard observation == nil else { return }
observation = NSApp.observe(\.effectiveAppearance, options: [.new]) { [weak self] _, _ in

ghost Mar 22, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 .new option is unused overhead

options: [.new] asks KVO to allocate and populate the NSKeyValueObservedChange with the new value, but the closure's second parameter is _ — it's never read. Using options: [] eliminates the unnecessary allocation on every appearance change.

Suggested change
observation = NSApp.observe(\.effectiveAppearance, options: [.new]) { [weak self] _, _ in
observation = NSApp.observe(\.effectiveAppearance, options: []) { [weak self] _, _ in

DispatchQueue.main.async {
self?.applyIconForCurrentAppearance()
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}

ghost Mar 22, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Stale async block can override explicit icon selection

When the user switches from .automatic to .light or .dark, stopObserving() invalidates the KVO token, but any DispatchQueue.main.async block that was already enqueued from a prior KVO callback will still run after the explicit icon has been set.

Concrete sequence:

  1. System appearance changes → KVO fires synchronously on main thread → DispatchQueue.main.async { applyIconForCurrentAppearance() } is enqueued
  2. Before the queued block runs, user picks "Light" → stopObserving() → applicationIconImage = lightIcon
  3. The enqueued block now runs → overwrites with the appearance-based icon, ignoring the user's explicit choice

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
Expand Down