Parcourir la source

多页签 - 修复多个文档开启后,在第一个文档添加注释,不会走保存方法

wanjun il y a 1 an
Parent
commit
fc4bdae6a7

+ 60 - 46
PDF Office/PDF Master/Class/ChromiumTabs/KMBrowser.swift

@@ -38,7 +38,7 @@ import Cocoa
         } else {
             let currentDocument = tabStripModel.tabContents(at: Int32(numberOfTabViewItem-1)) as! KMMainDocument
             if (currentDocument.isHome == false) {
-                currentDocument.mainViewController.savePageNumberIfNeed()
+                currentDocument.mainViewController?.savePageNumberIfNeed()
             }
             
             let isPrompt = KMPreferenceManager.shared.closeFileIsPrompt()
@@ -59,34 +59,43 @@ import Cocoa
                         numberOfTabViewItem = numberOfTabViewItem - 1
                         closeTabViewItem()
                     }
-                } else if (currentDocument.mainViewController.isPDFDocumentEdited || currentDocument.mainViewController.needSave) {
-                    if (isPrompt) {
-                        let alert = NSAlert()
-                        let message = "Do you want to save the changes made to the document “\(currentDocument.fileURL!.lastPathComponent)”?"
-                        alert.messageText = message
-                        alert.informativeText = NSLocalizedString("Your changes will be lost if you don’t save them.", comment: "")
-                        alert.addButton(withTitle: NSLocalizedString("Save", comment: ""))
-                        alert.addButton(withTitle: NSLocalizedString("Don‘t Save", comment: ""))
-                        alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
-                        let response = alert.runModal()
-                        if (response == .alertFirstButtonReturn) { // 保存
+                } else if (currentDocument.mainViewController != nil) {
+                    if (currentDocument.mainViewController!.isPDFDocumentEdited || currentDocument.mainViewController!.needSave) {
+                        if (isPrompt) {
+                            let alert = NSAlert()
+                            let message = "Do you want to save the changes made to the document “\(currentDocument.fileURL!.lastPathComponent)”?"
+                            alert.messageText = message
+                            alert.informativeText = NSLocalizedString("Your changes will be lost if you don’t save them.", comment: "")
+                            alert.addButton(withTitle: NSLocalizedString("Save", comment: ""))
+                            alert.addButton(withTitle: NSLocalizedString("Don‘t Save", comment: ""))
+                            alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
+                            let response = alert.runModal()
+                            if (response == .alertFirstButtonReturn) { // 保存
+                                currentDocument.save(nil)
+                                tabStripModel.detachTabContents(at: Int32(numberOfTabViewItem - 1))
+                                currentDocument.close()
+                            } else if (response == .alertSecondButtonReturn) { // 不保存
+                                tabStripModel.detachTabContents(at: Int32(numberOfTabViewItem - 1))
+                                currentDocument.close()
+                            } else { // 取消
+                                self.isCloseAllTabViewItem = false
+                            }
+                        } else {
                             currentDocument.save(nil)
-                            tabStripModel.detachTabContents(at: Int32(numberOfTabViewItem - 1))
                             currentDocument.close()
-                        } else if (response == .alertSecondButtonReturn) { // 不保存
-                            tabStripModel.detachTabContents(at: Int32(numberOfTabViewItem - 1))
-                            currentDocument.close()
-                        } else { // 取消
-                            self.isCloseAllTabViewItem = false
+                        }
+                        
+                        if isCloseAllTabViewItem {
+                            numberOfTabViewItem = numberOfTabViewItem - 1
+                            closeTabViewItem()
                         }
                     } else {
-                        currentDocument.save(nil)
+                        tabStripModel.detachTabContents(at: Int32(numberOfTabViewItem - 1))
                         currentDocument.close()
-                    }
-                    
-                    if isCloseAllTabViewItem {
-                        numberOfTabViewItem = numberOfTabViewItem - 1
-                        closeTabViewItem()
+                        if isCloseAllTabViewItem {
+                            numberOfTabViewItem = numberOfTabViewItem - 1
+                            closeTabViewItem()
+                        }
                     }
                 } else {
                     tabStripModel.detachTabContents(at: Int32(numberOfTabViewItem - 1))
@@ -100,12 +109,12 @@ import Cocoa
         }
     }
     
-    func canCloseContentsAt(_ index: Int) -> Bool {
+    override func canCloseContents(at index: Int32) -> Bool {
         let isPrompt = KMPreferenceManager.shared.closeFileIsPrompt()
         if tabContents(at: Int32(index)) != nil {
             let currentDocument = tabContents(at: Int32(index)) as! KMMainDocument
             if (currentDocument.isHome == false) {
-                currentDocument.mainViewController.savePageNumberIfNeed()
+                currentDocument.mainViewController?.savePageNumberIfNeed()
             }
             
             var canClose = false
@@ -124,7 +133,7 @@ import Cocoa
                     currentDocument.close()
                     canClose = true
                 }
-            }else {
+            } else {
                 if currentDocument.isDocumentEdited && isPrompt {
                     if currentDocument.isKind(of: KMMainDocument.self) {
                         currentDocument.closedByUserGestureFlag = true
@@ -135,30 +144,35 @@ import Cocoa
                     currentDocument.close()
                     canClose = true
                     
-                } else if (currentDocument.mainViewController.isPDFDocumentEdited || currentDocument.mainViewController.needSave) {
-    //                currentCloseDocument = currentDocument
-    //                currentDocument.save(withDelegate: self, didSave: #selector(saveSuccess(_:)), contextInfo: nil)
-                    if (isPrompt) {
-                        let alert = NSAlert()
-                        let message = "Do you want to save the changes made to the document “\(currentDocument.fileURL!.lastPathComponent)”?"
-                        alert.messageText = message
-                        alert.informativeText = NSLocalizedString("Your changes will be lost if you don’t save them.", comment: "")
-                        alert.addButton(withTitle: NSLocalizedString("Save", comment: ""))
-                        alert.addButton(withTitle: NSLocalizedString("Don‘t Save", comment: ""))
-                        alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
-                        let response = alert.runModal()
-                        if (response == .alertFirstButtonReturn) { // 保存
+                } else if (currentDocument.mainViewController != nil) {
+                    if (currentDocument.mainViewController!.isPDFDocumentEdited || currentDocument.mainViewController!.needSave) {
+                        //                currentCloseDocument = currentDocument
+                        //                currentDocument.save(withDelegate: self, didSave: #selector(saveSuccess(_:)), contextInfo: nil)
+                        if (isPrompt) {
+                            let alert = NSAlert()
+                            let message = "Do you want to save the changes made to the document “\(currentDocument.fileURL!.lastPathComponent)”?"
+                            alert.messageText = message
+                            alert.informativeText = NSLocalizedString("Your changes will be lost if you don’t save them.", comment: "")
+                            alert.addButton(withTitle: NSLocalizedString("Save", comment: ""))
+                            alert.addButton(withTitle: NSLocalizedString("Don‘t Save", comment: ""))
+                            alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
+                            let response = alert.runModal()
+                            if (response == .alertFirstButtonReturn) { // 保存
+                                currentDocument.save(nil)
+                                currentDocument.close()
+                                canClose = true
+                            } else if (response == .alertSecondButtonReturn) { // 不保存
+                                currentDocument.close()
+                                canClose = true
+                            } else { // 取消
+                                canClose = false
+                            }
+                        } else {
                             currentDocument.save(nil)
                             currentDocument.close()
                             canClose = true
-                        } else if (response == .alertSecondButtonReturn) { // 不保存
-                            currentDocument.close()
-                            canClose = true
-                        } else { // 取消
-                            canClose = false
                         }
                     } else {
-                        currentDocument.save(nil)
                         currentDocument.close()
                         canClose = true
                     }

+ 4 - 4
PDF Office/PDF Master/Class/ChromiumTabs/KMBrowserWindowController.swift

@@ -242,7 +242,7 @@ import Cocoa
                                 return
                             }
 
-                            (document as! KMMainDocument).mainViewController.password = password
+                            (document as! KMMainDocument).mainViewController?.password = password
                         }
                     }
                 }
@@ -366,17 +366,17 @@ extension KMBrowserWindowController {
     
     @IBAction func importFromFile(_ sender: Any) {
         let document: KMMainDocument = self.browser.activeTabContents() as! KMMainDocument
-        document.homeViewController.importFromFile(sender)
+        document.homeViewController?.importFromFile(sender)
     }
     
     @IBAction func openBlankPage(_ sender: Any) {
         let document: KMMainDocument = self.browser.activeTabContents() as! KMMainDocument
-        document.homeViewController.openBlankPage(sender)
+        document.homeViewController?.openBlankPage(sender)
     }
     
     @IBAction func importFromScanner(_ sender: Any) {
         let document: KMMainDocument = self.browser.activeTabContents() as! KMMainDocument
-        document.homeViewController.importFromScanner(sender)
+        document.homeViewController?.importFromScanner(sender)
     }
     
     @IBAction func menuItemAction_newTab(_ sender: Any) {

+ 93 - 35
PDF Office/PDF Master/Class/Document/KMMainDocument.swift

@@ -9,9 +9,9 @@ import Cocoa
 
 typealias KMMainDocumentCloudUploadHanddler = (@escaping(Bool, String)->()) -> ()
 @objcMembers class KMMainDocument: CTTabContents {
-    var mainViewController = KMMainViewController.init()
-    var homeWindowController = KMHomeWindowController.init(windowNibName: "KMHomeWindowController")
-    var homeViewController = KMHomeViewController.init()
+    var mainViewController: KMMainViewController?
+    var homeWindowController: KMHomeWindowController?
+    var homeViewController: KMHomeViewController?
     var isNewCreated: Bool = false
     var closedByUserGestureFlag: Bool = false   // 标记 closedByUserGesture 这个状态需要延后存储(如果需要)
     var cloud: Bool = false
@@ -53,14 +53,15 @@ typealias KMMainDocumentCloudUploadHanddler = (@escaping(Bool, String)->()) -> (
             currentWindowController = browser.windowController as? KMBrowserWindowController
         }
         
-        mainViewController.myDocument = self
+        mainViewController = KMMainViewController.init()
+        mainViewController?.myDocument = self
         
         if ((self.fileURL?.path) != nil) {
             let pdfDocument = CPDFDocument.init(url: URL(fileURLWithPath: self.fileURL!.path))
-            mainViewController.document = pdfDocument
+            mainViewController?.document = pdfDocument
         }
         
-        self.view = mainViewController.view
+        self.view = mainViewController?.view
         
         if currentWindowController != nil {
             if currentWindowController?.browser != nil {
@@ -73,7 +74,7 @@ typealias KMMainDocumentCloudUploadHanddler = (@escaping(Bool, String)->()) -> (
                 let activeIndex = Int((currentWindowController?.browser.activeTabIndex())!)
                 currentWindowController?.browser.add(self, at: Int32()-1, inForeground: true)
                 self.addWindowController(currentWindowController!)
-                mainViewController.browserWindowController = currentWindowController
+                mainViewController?.browserWindowController = currentWindowController
 
                 let ishome = activeBrowser.isHome as Bool
                 let isfirstTab = (activeIndex == 0)
@@ -97,29 +98,31 @@ typealias KMMainDocumentCloudUploadHanddler = (@escaping(Bool, String)->()) -> (
         self.setDataFromTmpData()
     }
     
-//    override func save(to url: URL, ofType typeName: String, for saveOperation: NSDocument.SaveOperationType) async throws {
-//        do {
-//            try await super.save(to: url, ofType: typeName, for: saveOperation)
-//        } catch let outError {
-//            Swift.print(outError)
-//        }
-//    }
+    override func save(to url: URL, ofType typeName: String, for saveOperation: NSDocument.SaveOperationType) async throws {
+        do {
+            try await super.save(to: url, ofType: typeName, for: saveOperation)
+        } catch let outError {
+            Swift.print(outError)
+        }
+    }
     
     override func write(to url: URL, ofType typeName: String, for saveOperation: NSDocument.SaveOperationType, originalContentsURL absoluteOriginalContentsURL: URL?) throws {
         var success = true
         if !self.isHome {
-            if mainViewController.document != nil {
-                if mainViewController.document!.isEncrypted {
-                    success = mainViewController.document!.write(to: url)
-                } else {
-                    if (mainViewController.needSave) {
-//                        success = KMPasswordInputWindow.saveDocument(mainViewController.document!)
-                        success = mainViewController.document!.write(to: url)
+            if mainViewController != nil {
+                if mainViewController?.document != nil {
+                    if mainViewController!.document!.isEncrypted {
+                        success = mainViewController!.document!.write(to: url)
                     } else {
-                        success = mainViewController.document!.write(to: url)
+                        if (mainViewController!.needSave) {
+    //                        success = KMPasswordInputWindow.saveDocument(mainViewController.document!)
+                            success = mainViewController!.document!.write(to: url)
+                        } else {
+                            success = mainViewController!.document!.write(to: url)
+                        }
                     }
+                    mainViewController!.needSave = false
                 }
-                mainViewController.needSave = false
             }
         } else {
             success = false
@@ -140,8 +143,10 @@ typealias KMMainDocumentCloudUploadHanddler = (@escaping(Bool, String)->()) -> (
             self.save(nil)
         } else if (self.isDocumentEdited) {
             self.save(nil)
-        } else if (self.mainViewController.isPDFDocumentEdited || self.mainViewController.needSave) {
-            self.save(nil)
+        } else if (mainViewController != nil) {
+            if self.mainViewController!.isPDFDocumentEdited || self.mainViewController!.needSave {
+                self.save(nil)
+            }
         }
 
         super.canClose(withDelegate: delegate, shouldClose: shouldCloseSelector, contextInfo: contextInfo)
@@ -191,14 +196,18 @@ typealias KMMainDocumentCloudUploadHanddler = (@escaping(Bool, String)->()) -> (
         super.init()
         // Add your subclass-specific initialization here.
         
-        NotificationCenter.default.addObserver(self, selector: #selector(pdfChangedNotification(_:)), name: NSNotification.Name.init(rawValue: "PDFChangedNotification"), object: nil)
+        NotificationCenter.default.addObserver(self, selector: #selector(pdfChangedNotification(_:)), name: NSNotification.Name.init(rawValue: "CPDFListViewAnnotationsAttributeHasChangeNotification"), object: nil)
+//        NotificationCenter.default.addObserver(self, selector: #selector(pdfChangedNotification(_:)), name: NSNotification.Name.init(rawValue: "CPDFViewDocumentChangedNotification"), object: nil)
+//        NotificationCenter.default.addObserver(self, selector: #selector(pdfChangedNotification(_:)), name: NSNotification.Name.init(rawValue: "CPDFViewPageChangedNotification"), object: nil)
+
     }
 
     override init?(baseTabContents baseContents: CTTabContents?) {
         super.init(baseTabContents: baseContents)
         if isHome {
-            homeViewController.myDocument = self
-            self.view = homeViewController.view
+            homeViewController = KMHomeViewController.init()
+            homeViewController?.myDocument = self
+            self.view = homeViewController?.view
         }
     }
     
@@ -222,15 +231,61 @@ typealias KMMainDocumentCloudUploadHanddler = (@escaping(Bool, String)->()) -> (
     func pdfChangedNotification(_ notification: Notification) -> Void {
         if !isHome {
             let mainViewController = mainViewController
-            let document = notification.object as? CPDFDocument
-            if document == mainViewController.document {
-                updateChangeCount(.changeCleared)
-            } else {
-                updateChangeCount(.changeDone)
+//            let document = notification.object as? CPDFDocument
+            let annotation = notification.object as? CPDFAnnotation
+            let document = annotation?.page.document
+            if mainViewController != nil {
+//                if document == mainViewController!.document {
+                if document == mainViewController!.document {
+                    if notification.userInfo != nil {
+                        updateChangeCount(.changeCleared)
+                    } else {
+                        updateChangeCount(.changeDone)
+                    }
+                }
             }
         }
     }
     
+    override func updateChangeCount(_ change: NSDocument.ChangeType) {
+        let mainWindow = NSApp.mainWindow
+        var currentWindowController: KMBrowserWindowController?
+        if mainWindow != nil {
+            let windowController = mainWindow!.windowController
+            if windowController is KMBrowserWindowController {
+                currentWindowController = (windowController as! KMBrowserWindowController)
+            } else {
+                for window in NSApp.windows {
+                    let windowController = window.windowController
+                    if windowController is KMBrowserWindowController {
+                        currentWindowController = (windowController as! KMBrowserWindowController)
+                        break
+                    }
+                }
+            }
+        } else {
+            for window in NSApp.windows {
+                let windowController = window.windowController
+                if windowController is KMBrowserWindowController {
+                    currentWindowController = (windowController as! KMBrowserWindowController)
+                    break
+                }
+            }
+        }
+        if currentWindowController != nil {
+            if currentWindowController?.browser != nil {
+
+                let activeBrowser = (currentWindowController?.browser.activeTabContents())! as CTTabContents
+                let activeIndex = Int((currentWindowController?.browser.activeTabIndex())!)
+                if self == activeBrowser {
+                    super.updateChangeCount(change)
+                    return
+                }
+            }
+        }
+
+        super.updateChangeCount(.changeCleared)
+    }
 
     func uploadToCloud(_ callback: (@escaping(Bool, String)->())) {
         guard let handdler = self.cloudUploadHanddler else {
@@ -245,7 +300,10 @@ typealias KMMainDocumentCloudUploadHanddler = (@escaping(Bool, String)->()) -> (
     }
     
     func setDataFromTmpData() {
-        let document = self.mainViewController.document
+        if self.mainViewController == nil {
+            return
+        }
+        let document = self.mainViewController!.document
         if (document == nil) {
             return
         }
@@ -261,7 +319,7 @@ typealias KMMainDocumentCloudUploadHanddler = (@escaping(Bool, String)->()) -> (
             if (password != nil) {
                 self.isUnlockFromKeychain = true
 //                document.unlock(withPassword: password! as String)
-                self.mainViewController.password = password as String?
+                self.mainViewController!.password = password as String?
             }
         }
     }

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

@@ -391,7 +391,7 @@ extension KMHomeViewController {
                                     }
 
 //                                    (document as! KMMainDocument).mainViewController.listView.document.unlock(withPassword: password)
-                                    (document as! KMMainDocument).mainViewController.password = password
+                                    (document as! KMMainDocument).mainViewController?.password = password
                                 }
                             }
                         }
@@ -1197,7 +1197,7 @@ extension KMHomeViewController {
                         return
                     }
                     
-                    let toolbar = (document as! KMMainDocument).mainViewController.toolbarController
+                    let toolbar = (document as! KMMainDocument).mainViewController?.toolbarController
                     toolbar?.enterRedact()
                 }
             }
@@ -1240,7 +1240,7 @@ extension KMHomeViewController {
                     }
                     
                     let mainView = (document as! KMMainDocument).mainViewController
-                    mainView.enterPageEdit()
+                    mainView?.enterPageEdit()
                 }
             }
         }