From d5d9167b1e4edbdd3da0b1e3091eb299bedad138 Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Mon, 26 Feb 2024 23:54:54 +0800 Subject: [PATCH] CocoaImpl // Fix isDarkMode(). --- .../CocoaExtension/CocoaExtension_Misc.swift | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/Packages/vChewing_CocoaExtension/Sources/CocoaExtension/CocoaExtension_Misc.swift b/Packages/vChewing_CocoaExtension/Sources/CocoaExtension/CocoaExtension_Misc.swift index 4e8bfba4..858e2d17 100644 --- a/Packages/vChewing_CocoaExtension/Sources/CocoaExtension/CocoaExtension_Misc.swift +++ b/Packages/vChewing_CocoaExtension/Sources/CocoaExtension/CocoaExtension_Misc.swift @@ -128,15 +128,14 @@ public extension NSApplication { // MARK: - System Dark Mode Status Detector. static var isDarkMode: Bool { - if #unavailable(macOS 10.14) { return false } - if #available(macOS 10.15, *) { - let appearanceDescription = NSApp.effectiveAppearance.debugDescription - .lowercased() - return appearanceDescription.contains("dark") - } else if let appleInterfaceStyle = UserDefaults.current.string(forKey: "AppleInterfaceStyle") { - return appleInterfaceStyle.lowercased().contains("dark") + // "NSApp" can be nil during SPM unit tests. + // Therefore, the method dedicated for macOS 10.15 and later is not considered stable anymore. + // Fortunately, the method for macOS 10.14 works well on later macOS releases. + if #available(macOS 10.14, *), let strAIS = UserDefaults.current.string(forKey: "AppleInterfaceStyle") { + return strAIS.lowercased().contains("dark") + } else { + return false } - return false } // MARK: - Tell whether this IME is running with Root privileges.