KMBrowser.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. //
  2. // KMBrowser.swift
  3. // PDF Master
  4. //
  5. // Created by wanjun on 2022/12/9.
  6. //
  7. import Cocoa
  8. @objcMembers class KMBrowser: CTBrowser {
  9. var numberOfTabViewItem: Int = 0
  10. var isCloseAllTabViewItem: Bool = false
  11. var currentCloseDocument: KMMainDocument?
  12. override func createBlankTabBased(on baseContents: CTTabContents) -> CTTabContents {
  13. return KMMainDocument.init(baseTabContents: baseContents)!
  14. }
  15. func createNewBrowser() -> KMBrowser {
  16. let browser = KMBrowser.init() as KMBrowser
  17. let cls = windowController != nil ? type(of: windowController) : CTBrowserWindowController.self
  18. browser.windowController = cls.init(browser: browser)
  19. return browser
  20. }
  21. override func windowDidBeginToClose() {
  22. numberOfTabViewItem = Int(tabCount())
  23. isCloseAllTabViewItem = true
  24. closeTabViewItem()
  25. }
  26. func closeTabViewItem() -> Void {
  27. if numberOfTabViewItem < 1 {
  28. window.orderOut(nil)
  29. tabStripModel.closeAllTabs()
  30. isCloseAllTabViewItem = false
  31. } else {
  32. let currentDocument = tabStripModel.tabContents(at: Int32(numberOfTabViewItem-1)) as! KMMainDocument
  33. if (currentDocument.isHome == false) {
  34. currentDocument.mainViewController?.savePageNumberIfNeed()
  35. }
  36. // 清理数据 [主要清空通知]
  37. currentDocument.mainViewController?.clearData()
  38. let isPrompt = KMPreferenceManager.shared.closeFileIsPrompt()
  39. if currentDocument.isNewCreated {
  40. let exist = FileManager.default.fileExists(atPath: currentDocument.fileURL!.path)
  41. if exist {
  42. try? FileManager.default.removeItem(atPath: currentDocument.fileURL!.path)
  43. }
  44. currentCloseDocument = currentDocument
  45. currentDocument.runModalSavePanel(for: .saveAsOperation, delegate: self, didSave: #selector(saveSuccess(_:)), contextInfo: nil)
  46. } else {
  47. if currentDocument.isDocumentEdited && isPrompt {
  48. currentDocument.canClose(withDelegate: self, shouldClose: #selector(document(_:shouldClose:contextInfo:)), contextInfo: nil)
  49. } else if currentDocument.isDocumentEdited {
  50. currentDocument.save(nil)
  51. currentDocument.close()
  52. if self.isCloseAllTabViewItem {
  53. numberOfTabViewItem = numberOfTabViewItem - 1
  54. closeTabViewItem()
  55. }
  56. } else if (currentDocument.mainViewController != nil) {
  57. if (currentDocument.mainViewController!.isPDFDocumentEdited || currentDocument.mainViewController!.needSave) {
  58. if (isPrompt) {
  59. let alert = NSAlert()
  60. let message = "Do you want to save the changes made to the document “\(currentDocument.fileURL!.lastPathComponent)”?"
  61. alert.messageText = message
  62. alert.informativeText = NSLocalizedString("Your changes will be lost if you don’t save them.", comment: "")
  63. alert.addButton(withTitle: NSLocalizedString("Save", comment: ""))
  64. alert.addButton(withTitle: NSLocalizedString("Don‘t Save", comment: ""))
  65. alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
  66. let response = alert.runModal()
  67. if (response == .alertFirstButtonReturn) { // 保存
  68. currentDocument.save(nil)
  69. tabStripModel.detachTabContents(at: Int32(numberOfTabViewItem - 1))
  70. currentDocument.close()
  71. } else if (response == .alertSecondButtonReturn) { // 不保存
  72. tabStripModel.detachTabContents(at: Int32(numberOfTabViewItem - 1))
  73. currentDocument.close()
  74. } else { // 取消
  75. self.isCloseAllTabViewItem = false
  76. }
  77. } else {
  78. currentDocument.save(nil)
  79. currentDocument.close()
  80. }
  81. if isCloseAllTabViewItem {
  82. numberOfTabViewItem = numberOfTabViewItem - 1
  83. closeTabViewItem()
  84. }
  85. } else {
  86. tabStripModel.detachTabContents(at: Int32(numberOfTabViewItem - 1))
  87. currentDocument.close()
  88. if isCloseAllTabViewItem {
  89. numberOfTabViewItem = numberOfTabViewItem - 1
  90. closeTabViewItem()
  91. }
  92. }
  93. } else {
  94. tabStripModel.detachTabContents(at: Int32(numberOfTabViewItem - 1))
  95. currentDocument.close()
  96. if isCloseAllTabViewItem {
  97. numberOfTabViewItem = numberOfTabViewItem - 1
  98. closeTabViewItem()
  99. }
  100. }
  101. }
  102. }
  103. }
  104. override func canCloseContents(at index: Int32) -> Bool {
  105. let isPrompt = KMPreferenceManager.shared.closeFileIsPrompt()
  106. if tabContents(at: Int32(index)) != nil {
  107. let currentDocument = tabContents(at: Int32(index)) as! KMMainDocument
  108. if (currentDocument.isHome == false) {
  109. currentDocument.mainViewController?.savePageNumberIfNeed()
  110. }
  111. var canClose = false
  112. if currentDocument.isNewCreated {
  113. let exist = FileManager.default.fileExists(atPath: currentDocument.fileURL!.path)
  114. if exist {
  115. try? FileManager.default.removeItem(atPath: currentDocument.fileURL!.path)
  116. }
  117. currentCloseDocument = currentDocument
  118. currentDocument.runModalSavePanel(for: .saveToOperation, delegate: self, didSave: #selector(saveSuccess(_:)), contextInfo: nil)
  119. } else if (currentDocument.cloud) { /// 云文档
  120. if (currentDocument.isDocumentEdited) {
  121. currentCloseDocument = currentDocument
  122. currentDocument.save(withDelegate: self, didSave: #selector(saveSuccess(_:)), contextInfo: nil)
  123. } else {
  124. currentDocument.close()
  125. canClose = true
  126. }
  127. } else {
  128. if currentDocument.isDocumentEdited && isPrompt {
  129. if currentDocument.isKind(of: KMMainDocument.self) {
  130. currentDocument.closedByUserGestureFlag = true
  131. }
  132. currentDocument.canClose(withDelegate: self, shouldClose: #selector(document(_:shouldClose:contextInfo:)), contextInfo: nil)
  133. } else if currentDocument.isDocumentEdited {
  134. currentDocument.save(nil)
  135. currentDocument.close()
  136. canClose = true
  137. } else if (currentDocument.mainViewController != nil) {
  138. if (currentDocument.mainViewController!.isPDFDocumentEdited || currentDocument.mainViewController!.needSave) {
  139. // currentCloseDocument = currentDocument
  140. // currentDocument.save(withDelegate: self, didSave: #selector(saveSuccess(_:)), contextInfo: nil)
  141. if (isPrompt) {
  142. let alert = NSAlert()
  143. let message = "Do you want to save the changes made to the document “\(currentDocument.fileURL!.lastPathComponent)”?"
  144. alert.messageText = message
  145. alert.informativeText = NSLocalizedString("Your changes will be lost if you don’t save them.", comment: "")
  146. alert.addButton(withTitle: NSLocalizedString("Save", comment: ""))
  147. alert.addButton(withTitle: NSLocalizedString("Don‘t Save", comment: ""))
  148. alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
  149. let response = alert.runModal()
  150. if (response == .alertFirstButtonReturn) { // 保存
  151. currentDocument.save(nil)
  152. currentDocument.close()
  153. canClose = true
  154. } else if (response == .alertSecondButtonReturn) { // 不保存
  155. currentDocument.close()
  156. canClose = true
  157. } else { // 取消
  158. canClose = false
  159. }
  160. } else {
  161. currentDocument.save(nil)
  162. currentDocument.close()
  163. canClose = true
  164. }
  165. } else {
  166. currentDocument.close()
  167. canClose = true
  168. }
  169. } else {
  170. currentDocument.close()
  171. canClose = true
  172. }
  173. }
  174. if (canClose) {
  175. // 清理数据 [主要清空通知]
  176. currentDocument.mainViewController?.clearData()
  177. }
  178. return canClose
  179. }
  180. return false
  181. }
  182. func document(_ document: NSDocument?, shouldClose: Bool, contextInfo: UnsafeMutableRawPointer?) {
  183. if (document is KMMainDocument) {
  184. let iu: CloseTypes = CLOSE_USER_GESTURE
  185. if ((document as! KMMainDocument).closedByUserGestureFlag && shouldClose) {
  186. (document as! KMMainDocument).closedByUserGesture = uint(iu.rawValue)
  187. }
  188. (document as! KMMainDocument).closedByUserGestureFlag = false
  189. }
  190. if (shouldClose) {
  191. let currentDocument: KMMainDocument = document! as! KMMainDocument
  192. let index = self.tabStripModel.index(of: currentDocument)
  193. // 清空数据 [主要清理通知]
  194. currentDocument.mainViewController?.clearData()
  195. currentDocument.close()
  196. self.tabStripModel.detachTabContents(at: index)
  197. }
  198. if (self.isCloseAllTabViewItem) {
  199. self.numberOfTabViewItem = self.numberOfTabViewItem - 1
  200. self.closeTabViewItem()
  201. }
  202. }
  203. func saveSuccess(_ sender: Any) -> Void {
  204. if (currentCloseDocument != nil) {
  205. if (currentCloseDocument!.cloud) { /// 云文档
  206. currentCloseDocument!.uploadToCloud({ [weak self] finished, error in
  207. if (finished) {
  208. if (self!.currentCloseDocument == nil) {
  209. return
  210. }
  211. let index = self?.tabStripModel.index(of: self!.currentCloseDocument!)
  212. self?.tabStripModel.detachTabContents(at: index!)
  213. // self!.currentCloseDocument!.close()
  214. self!.currentCloseDocument = nil
  215. if (self!.isCloseAllTabViewItem) {
  216. self!.numberOfTabViewItem = self!.numberOfTabViewItem - 1
  217. self!.closeTabViewItem()
  218. }
  219. }
  220. })
  221. return
  222. }
  223. let index = tabStripModel.index(of: currentCloseDocument)
  224. tabStripModel.detachTabContents(at: index)
  225. // 清空数据 [主要清理通知]
  226. currentCloseDocument?.mainViewController?.clearData()
  227. currentCloseDocument?.close()
  228. currentCloseDocument = nil
  229. }
  230. if isCloseAllTabViewItem {
  231. numberOfTabViewItem = numberOfTabViewItem - 1
  232. closeTabViewItem()
  233. }
  234. }
  235. }
  236. extension KMBrowser {
  237. @objc public func canSelectNextTab() -> Bool {
  238. if (self.tabStripModel.count() < 1) {
  239. return false
  240. }
  241. if (self.tabStripModel.activeIndex >= (self.tabStripModel.count()-1)) {
  242. return false
  243. }
  244. return true
  245. }
  246. @objc public func canSelectPreviousTab() -> Bool {
  247. if (self.tabStripModel.activeIndex <= 0) {
  248. return false
  249. }
  250. return true
  251. }
  252. }