Browse Source

【综合】内存优化

tangchao 10 months ago
parent
commit
5282242c89

+ 5 - 4
PDF Office/PDF Master/Class/Home/ViewController/KMHomeViewController+Action.swift

@@ -1985,16 +1985,17 @@ extension KMHomeViewController {
 
 extension KMHomeViewController {    
     @IBAction func menuItemAction_showForwardTagPage(_ sender: Any) {
-        (self.myDocument as! KMMainDocument).browser.selectPreviousTab()
+        (self.myDocument as? KMMainDocument)?.browser.selectPreviousTab()
     }
     
     @IBAction func menuItemAction_showNextTagPage(_ sender: Any) {
-        (self.myDocument as! KMMainDocument).browser.selectNextTab()
+        (self.myDocument as? KMMainDocument)?.browser.selectNextTab()
     }
     
     @IBAction func menuItemAction_newTagPageToNewWindow(_ sender: Any) {
-        let browser = (self.myDocument as! KMMainDocument).browser
-        ((browser as! KMBrowser).windowController as? KMBrowserWindowController)?.openNewWindow(sender)
+        if let browser = (self.myDocument as? KMMainDocument)?.browser {
+            ((browser as! KMBrowser).windowController as? KMBrowserWindowController)?.openNewWindow(sender)
+        }
     }
     
     @IBAction func menuItemAction_mergeAllWindow(_ sender: Any) {

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

@@ -76,7 +76,7 @@ import Cocoa
     var homeWindowController : MainWindowController!
     
     var isShowQuickTour: Bool = false
-    var myDocument: NSDocument?
+    weak var myDocument: NSDocument?
     var currentWindowController: NSWindowController?
     var homeState: KMHomeToolState = .Home
     

+ 4 - 3
PDF Office/PDF Master/Class/PDFWindowController/ViewController/KMMainViewController+Action.swift

@@ -2479,9 +2479,10 @@ extension KMMainViewController {
         if sender is NSNotification {
             let tabController = (sender as! NSNotification).object as? CTTabController
             if tabController?.title == self.document?.documentURL.lastPathComponent {
-                let file: URL = (self.myDocument?.fileURL)!
-                if FileManager.default.fileExists(atPath: file.path) {
-                    NSWorkspace.shared.activateFileViewerSelecting([file])
+                if let file = self.myDocument?.fileURL {
+                    if FileManager.default.fileExists(atPath: file.path) {
+                        NSWorkspace.shared.activateFileViewerSelecting([file])
+                    }
                 }
             }
         } else {

+ 1 - 1
PDF Office/PDF Master/Class/PDFWindowController/ViewController/KMMainViewController+MenuAction.swift

@@ -1316,7 +1316,7 @@ extension KMMainViewController {
 extension KMMainViewController {
     override func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
         if (menuItem.action == #selector(menuItemAction_currentWindowName)) {
-            menuItem.title = (self.myDocument?.fileURL!.lastPathComponent)!
+            menuItem.title = (self.myDocument?.fileURL?.lastPathComponent) ?? ""
             return true
         }
         

+ 2 - 2
PDF Office/PDF Master/Class/PDFWindowController/ViewController/KMMainViewController+UI.swift

@@ -171,8 +171,8 @@ extension KMMainViewController {
                 let annotation = removeAnnotations[i]
                 
                 // this is only to make sure markup annotations generate the lineRects, for thread safety
-                pdfView!.addAnnotation(with: newAnnotation, to: page)
-                pdfView!.remove(annotation)
+                pdfView?.addAnnotation(with: newAnnotation, to: page)
+                pdfView?.remove(annotation)
                 if newAnnotation.contents != nil {
                     if newAnnotation.contents.count == 0 {
                         newAnnotation.autoUpdateString()

+ 3 - 1
PDF Office/PDF Master/Class/PDFWindowController/ViewController/KMMainViewController.swift

@@ -1870,6 +1870,7 @@ import Cocoa
         }
         self.removeAutoSaveInfo()
         KMAdsManager.defaultManager.dismissSheetModal(for: self.readContentView)
+        self.myDocument = nil
     }
     
     public func clearSecureOptions() {
@@ -1961,7 +1962,8 @@ import Cocoa
                 self.hiddenSecureLimitTip()
             }
             
-            if ((self.myDocument as! KMMainDocument).isUnlockFromKeychain || self.model.isSaveKeyChain == false) {
+            let isUnlockFromKeychain = (self.myDocument as? KMMainDocument)?.isUnlockFromKeychain ?? false
+            if (isUnlockFromKeychain || self.model.isSaveKeyChain == false) {
                 return
             }