123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- //
- // KMBrowser.swift
- // PDF Master
- //
- // Created by wanjun on 2022/12/9.
- //
- import Cocoa
- @objcMembers class KMBrowser: CTBrowser {
-
- var numberOfTabViewItem: Int = 0
- var isCloseAllTabViewItem: Bool = false
- var currentCloseDocument: KMMainDocument?
- override func createBlankTabBased(on baseContents: CTTabContents) -> CTTabContents {
- return KMMainDocument.init(baseTabContents: baseContents)!
- }
-
- func createNewBrowser() -> KMBrowser {
- let browser = KMBrowser.init() as KMBrowser
- let cls = windowController != nil ? type(of: windowController) : CTBrowserWindowController.self
- browser.windowController = cls.init(browser: browser)
- return browser
- }
-
- override func windowDidBeginToClose() {
- numberOfTabViewItem = Int(tabCount())
- isCloseAllTabViewItem = true
- closeTabViewItem()
- }
-
- func closeTabViewItem() -> Void {
- if numberOfTabViewItem < 1 {
- window.orderOut(nil)
- tabStripModel.closeAllTabs()
- isCloseAllTabViewItem = false
- } else {
- let currentDocument = tabStripModel.tabContents(at: Int32(numberOfTabViewItem-1)) as! KMMainDocument
- if (currentDocument.isHome == false) {
- currentDocument.mainViewController?.savePageNumberIfNeed()
- }
- // 清理数据 [主要清空通知]
- currentDocument.mainViewController?.clearData()
-
- let isPrompt = KMPreferenceManager.shared.closeFileIsPrompt()
- if currentDocument.isNewCreated {
- let exist = FileManager.default.fileExists(atPath: currentDocument.fileURL!.path)
- if exist {
- try? FileManager.default.removeItem(atPath: currentDocument.fileURL!.path)
- }
- currentCloseDocument = currentDocument
- currentDocument.runModalSavePanel(for: .saveAsOperation, delegate: self, didSave: #selector(saveSuccess(_:)), contextInfo: nil)
- } else {
- if currentDocument.isDocumentEdited && isPrompt {
- currentDocument.canClose(withDelegate: self, shouldClose: #selector(document(_:shouldClose:contextInfo:)), contextInfo: nil)
- } else if currentDocument.isDocumentEdited {
- currentDocument.save(nil)
- currentDocument.close()
- if self.isCloseAllTabViewItem {
- numberOfTabViewItem = numberOfTabViewItem - 1
- closeTabViewItem()
- }
- } 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)
- currentDocument.close()
- }
-
- if isCloseAllTabViewItem {
- numberOfTabViewItem = numberOfTabViewItem - 1
- closeTabViewItem()
- }
- } else {
- tabStripModel.detachTabContents(at: Int32(numberOfTabViewItem - 1))
- currentDocument.close()
- if isCloseAllTabViewItem {
- numberOfTabViewItem = numberOfTabViewItem - 1
- closeTabViewItem()
- }
- }
- } else {
- tabStripModel.detachTabContents(at: Int32(numberOfTabViewItem - 1))
- currentDocument.close()
- if isCloseAllTabViewItem {
- numberOfTabViewItem = numberOfTabViewItem - 1
- closeTabViewItem()
- }
- }
- }
- }
- }
-
- 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()
- }
-
- var canClose = false
- if currentDocument.isNewCreated {
- let exist = FileManager.default.fileExists(atPath: currentDocument.fileURL!.path)
- if exist {
- try? FileManager.default.removeItem(atPath: currentDocument.fileURL!.path)
- }
- currentCloseDocument = currentDocument
- currentDocument.runModalSavePanel(for: .saveToOperation, delegate: self, didSave: #selector(saveSuccess(_:)), contextInfo: nil)
- } else if (currentDocument.cloud) { /// 云文档
- if (currentDocument.isDocumentEdited) {
- currentCloseDocument = currentDocument
- currentDocument.save(withDelegate: self, didSave: #selector(saveSuccess(_:)), contextInfo: nil)
- } else {
- currentDocument.close()
- canClose = true
- }
- } else {
- if currentDocument.isDocumentEdited && isPrompt {
- if currentDocument.isKind(of: KMMainDocument.self) {
- currentDocument.closedByUserGestureFlag = true
- }
- currentDocument.canClose(withDelegate: self, shouldClose: #selector(document(_:shouldClose:contextInfo:)), contextInfo: nil)
- } else if currentDocument.isDocumentEdited {
- currentDocument.save(nil)
- currentDocument.close()
- canClose = true
-
- } 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 {
- currentDocument.close()
- canClose = true
- }
- } else {
- currentDocument.close()
- canClose = true
- }
- }
- if (canClose) {
- // 清理数据 [主要清空通知]
- currentDocument.mainViewController?.clearData()
- }
- return canClose
- }
- return false
- }
-
- func document(_ document: NSDocument?, shouldClose: Bool, contextInfo: UnsafeMutableRawPointer?) {
- if (document is KMMainDocument) {
- let iu: CloseTypes = CLOSE_USER_GESTURE
- if ((document as! KMMainDocument).closedByUserGestureFlag && shouldClose) {
- (document as! KMMainDocument).closedByUserGesture = uint(iu.rawValue)
- }
- (document as! KMMainDocument).closedByUserGestureFlag = false
- }
-
- if (shouldClose) {
- let currentDocument: KMMainDocument = document! as! KMMainDocument
- let index = self.tabStripModel.index(of: currentDocument)
- // 清空数据 [主要清理通知]
- currentDocument.mainViewController?.clearData()
- currentDocument.close()
- self.tabStripModel.detachTabContents(at: index)
- }
-
- if (self.isCloseAllTabViewItem) {
- self.numberOfTabViewItem = self.numberOfTabViewItem - 1
- self.closeTabViewItem()
- }
- }
-
- func saveSuccess(_ sender: Any) -> Void {
- if (currentCloseDocument != nil) {
- if (currentCloseDocument!.cloud) { /// 云文档
- currentCloseDocument!.uploadToCloud({ [weak self] finished, error in
- if (finished) {
- if (self!.currentCloseDocument == nil) {
- return
- }
-
- let index = self?.tabStripModel.index(of: self!.currentCloseDocument!)
- self?.tabStripModel.detachTabContents(at: index!)
- // self!.currentCloseDocument!.close()
- self!.currentCloseDocument = nil
-
- if (self!.isCloseAllTabViewItem) {
- self!.numberOfTabViewItem = self!.numberOfTabViewItem - 1
- self!.closeTabViewItem()
- }
- }
- })
- return
- }
-
- let index = tabStripModel.index(of: currentCloseDocument)
- tabStripModel.detachTabContents(at: index)
- // 清空数据 [主要清理通知]
- currentCloseDocument?.mainViewController?.clearData()
- currentCloseDocument?.close()
- currentCloseDocument = nil
- }
-
- if isCloseAllTabViewItem {
- numberOfTabViewItem = numberOfTabViewItem - 1
- closeTabViewItem()
- }
- }
- }
- extension KMBrowser {
- @objc public func canSelectNextTab() -> Bool {
- if (self.tabStripModel.count() < 1) {
- return false
- }
- if (self.tabStripModel.activeIndex >= (self.tabStripModel.count()-1)) {
- return false
- }
- return true
- }
-
- @objc public func canSelectPreviousTab() -> Bool {
- if (self.tabStripModel.activeIndex <= 0) {
- return false
- }
- return true
- }
- }
|