Browse Source

【主页】黑暗模式兼容

lizhe 1 year ago
parent
commit
11406279a8

+ 0 - 7
PDF Office/PDF Master.xcodeproj/xcshareddata/xcschemes/PDF Master.xcscheme

@@ -50,13 +50,6 @@
             ReferencedContainer = "container:PDF Master.xcodeproj">
          </BuildableReference>
       </BuildableProductRunnable>
-      <AdditionalOptions>
-         <AdditionalOption
-            key = "NSZombieEnabled"
-            value = "YES"
-            isEnabled = "YES">
-         </AdditionalOption>
-      </AdditionalOptions>
    </LaunchAction>
    <ProfileAction
       buildConfiguration = "Release"

+ 1 - 0
PDF Office/PDF Master/AppDelegate.swift

@@ -240,6 +240,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
         } else {
             NSApp.appearance = .init(named: .darkAqua)
         }
+        NotificationCenter.default.post(name: Notification.Name(rawValue: "kEffectiveAppearance"), object: nil)
     }
     
     // MARK: Menu

+ 0 - 24
PDF Office/PDF Master/Class/Home/ViewController/KMHomeViewController.swift

@@ -740,27 +740,3 @@ extension KMHomeViewController {
 //        }
     }
 }
-
-extension KMHomeViewController {
-    // 监听系统外观模式变化的方法
-    @objc func appearanceChanged() {
-        // 根据当前的外观模式更新应用程序的外观
-//        print("appearanceChanged\(String(describing: self.view.appearance))")
-        if KMAdvertisementConfig.isDarkModel() && self.view.appearance == NSAppearance(named: .aqua) ||
-            (!KMAdvertisementConfig.isDarkModel() && self.view.appearance == NSAppearance(named: .darkAqua))  {
-            setAppearance(isDarkMode: KMAdvertisementConfig.isDarkModel())
-        }
-    }
-    
-    // 根据系统外观模式设置应用程序的外观
-    func setAppearance(isDarkMode: Bool) {
-        if isDarkMode {
-            self.view.appearance = NSAppearance(named: .darkAqua)
-//            NSApp.appearance = NSAppearance(named: .darkAqua)
-        } else {
-            self.view.appearance = NSAppearance(named: .aqua)
-//            NSApp.appearance = NSAppearance(named: .aqua)
-        }
-//        self.homeContentView?.qucikToolsView.updateUI()
-    }
-}

+ 12 - 27
PDF Office/PDF Master/Class/PDFTools/Tools/KMBaseXibView/KMBaseXibView.swift

@@ -10,8 +10,7 @@ import Cocoa
 class KMBaseXibView: NSView {
 
     @IBOutlet var contentView: NSView!
-
-    var isObservingAppearance = false
+    
     deinit {
         self.removeNotification()
     }
@@ -24,6 +23,7 @@ class KMBaseXibView: NSView {
         self.updateLanguage()
         self.reloadData()
         self.addNotification()
+        self.changeEffectiveAppearance()
     }
     
     override init(frame frameRect: NSRect) {
@@ -34,6 +34,7 @@ class KMBaseXibView: NSView {
         self.updateLanguage()
         self.reloadData()
         self.addNotification()
+        self.changeEffectiveAppearance()
     }
     
     private func initContentView() {
@@ -90,36 +91,20 @@ class KMBaseXibView: NSView {
     }
     
     func addNotification() {
-        
-        self.addObserver(self, forKeyPath: "effectiveAppearance", options: .new, context: nil)
-        isObservingAppearance = true
+        NotificationCenter.default.addObserver(self, selector: #selector(changeEffectiveAppearance), name: NSNotification.Name(rawValue: "kEffectiveAppearance"), object: nil)
     }
     
     func removeNotification() {
-        if isObservingAppearance {
-            self.removeObserver(self, forKeyPath: "effectiveAppearance")
-            NotificationCenter.default.removeObserver(self)
-        }
-        isObservingAppearance = false
+        NotificationCenter.default.removeObserver(self)
     }
     
-    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
-        if keyPath == "effectiveAppearance" {
-            let isDarkModel = KMAdvertisementConfig.isDarkModel()
-            if isDarkModel {
-                self.appearance = NSAppearance(named: .darkAqua)
-            } else {
-                self.appearance = NSAppearance(named: .aqua)
-            }
-            
-//            if isDarkModel {
-//                NSApp.appearance = NSAppearance(named: .darkAqua)
-//            } else {
-//                NSApp.appearance = NSAppearance(named: .aqua)
-//            }
-            
-            self.updateUI()
-            print("黑暗模式触发")
+    @objc func changeEffectiveAppearance() {
+        let isDarkModel = KMAdvertisementConfig.isDarkModel()
+        if isDarkModel {
+            self.appearance = NSAppearance(named: .darkAqua)
+        } else {
+            self.appearance = NSAppearance(named: .aqua)
         }
+        self.updateUI()
     }
 }