|
@@ -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?
|
|
|
}
|
|
|
}
|
|
|
}
|