KMBrowser.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. var currentDocument: KMMainDocument?
  13. override func createBlankTabBased(on baseContents: CTTabContents?) -> CTTabContents {
  14. return KMMainDocument.init(baseTabContents: baseContents)!
  15. }
  16. func createNewBrowser() -> KMBrowser {
  17. let browser = KMBrowser.init() as KMBrowser
  18. let cls = windowController != nil ? type(of: windowController) : CTBrowserWindowController.self
  19. browser.windowController = cls.init(browser: browser)
  20. return browser
  21. }
  22. override func windowDidBeginToClose() {
  23. self.currentDocument = NSDocumentController.shared.currentDocument as? KMMainDocument
  24. numberOfTabViewItem = Int(tabCount())
  25. isCloseAllTabViewItem = true
  26. closeTabViewItem()
  27. }
  28. func closeTabViewItem() -> Void {
  29. if numberOfTabViewItem < 1 {
  30. window.orderOut(nil)
  31. tabStripModel.closeAllTabs()
  32. isCloseAllTabViewItem = false
  33. } else {
  34. let currentDocument = tabStripModel.tabContents(at: Int32(numberOfTabViewItem-1)) as! KMMainDocument
  35. if (currentDocument.isHome == false) {
  36. currentDocument.mainViewController?.savePageNumberIfNeed()
  37. if let _isEdited = currentDocument.mainViewController?.listView.isEdited(), _isEdited, !currentDocument.isDocumentEdited {
  38. currentDocument.updateChangeCount(.changeDone)
  39. }
  40. }
  41. // 清理数据 [主要清空通知]
  42. currentDocument.mainViewController?.clearData()
  43. if (currentDocument.isEqual(to: self.currentDocument)) {
  44. // 当前文档 不用提醒,在关闭窗口时已提醒过了
  45. tabStripModel.detachTabContents(at: Int32(self.numberOfTabViewItem - 1))
  46. currentDocument.close()
  47. if (self.isCloseAllTabViewItem) {
  48. self.numberOfTabViewItem = self.numberOfTabViewItem - 1
  49. self.closeTabViewItem()
  50. }
  51. return
  52. }
  53. let isPrompt = KMPreferenceManager.shared.closeFileIsPrompt()
  54. if currentDocument.isNewCreated {
  55. let exist = FileManager.default.fileExists(atPath: currentDocument.fileURL!.path)
  56. if exist {
  57. try? FileManager.default.removeItem(atPath: currentDocument.fileURL!.path)
  58. }
  59. currentCloseDocument = currentDocument
  60. currentDocument.runModalSavePanel(for: .saveAsOperation, delegate: self, didSave: #selector(saveSuccess(_:)), contextInfo: nil)
  61. } else {
  62. if currentDocument.isDocumentEdited && isPrompt {
  63. currentDocument.watermarkSaveDelegate = self
  64. currentDocument.canClose(withDelegate: self, shouldClose: #selector(document(_:shouldClose:contextInfo:)), contextInfo: nil)
  65. } else if currentDocument.isDocumentEdited {
  66. currentDocument.save(nil)
  67. currentDocument.close()
  68. if self.isCloseAllTabViewItem {
  69. numberOfTabViewItem = numberOfTabViewItem - 1
  70. closeTabViewItem()
  71. }
  72. } else if (currentDocument.mainViewController != nil) {
  73. if (currentDocument.mainViewController!.isPDFDocumentEdited || currentDocument.mainViewController!.needSave) {
  74. if (isPrompt) {
  75. let alert = NSAlert()
  76. let message = "Do you want to save the changes made to the document “\(currentDocument.fileURL!.lastPathComponent)”?"
  77. alert.messageText = message
  78. alert.informativeText = NSLocalizedString("Your changes will be lost if you don’t save them.", comment: "")
  79. alert.addButton(withTitle: NSLocalizedString("Save", comment: ""))
  80. alert.addButton(withTitle: NSLocalizedString("Don‘t Save", comment: ""))
  81. alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
  82. let response = alert.runModal()
  83. if (response == .alertFirstButtonReturn) { // 保存
  84. currentDocument.save(nil)
  85. tabStripModel.detachTabContents(at: Int32(numberOfTabViewItem - 1))
  86. currentDocument.close()
  87. } else if (response == .alertSecondButtonReturn) { // 不保存
  88. tabStripModel.detachTabContents(at: Int32(numberOfTabViewItem - 1))
  89. currentDocument.close()
  90. } else { // 取消
  91. self.isCloseAllTabViewItem = false
  92. }
  93. } else {
  94. currentDocument.save(nil)
  95. currentDocument.close()
  96. }
  97. if isCloseAllTabViewItem {
  98. numberOfTabViewItem = numberOfTabViewItem - 1
  99. closeTabViewItem()
  100. }
  101. } else {
  102. tabStripModel.detachTabContents(at: Int32(numberOfTabViewItem - 1))
  103. currentDocument.close()
  104. if isCloseAllTabViewItem {
  105. numberOfTabViewItem = numberOfTabViewItem - 1
  106. closeTabViewItem()
  107. }
  108. }
  109. } else {
  110. tabStripModel.detachTabContents(at: Int32(numberOfTabViewItem - 1))
  111. currentDocument.close()
  112. if isCloseAllTabViewItem {
  113. numberOfTabViewItem = numberOfTabViewItem - 1
  114. closeTabViewItem()
  115. }
  116. }
  117. }
  118. }
  119. }
  120. override func canCloseContents(at index: Int32) -> Bool {
  121. let isPrompt = KMPreferenceManager.shared.closeFileIsPrompt()
  122. if tabContents(at: Int32(index)) != nil {
  123. let currentDocument = tabContents(at: Int32(index)) as! KMMainDocument
  124. if (currentDocument.isHome == false) {
  125. currentDocument.mainViewController?.savePageNumberIfNeed()
  126. if let _isEdited = currentDocument.mainViewController?.listView.isEdited(), _isEdited, !currentDocument.isDocumentEdited {
  127. currentDocument.updateChangeCount(.changeDone)
  128. }
  129. }
  130. var canClose = false
  131. if currentDocument.isNewCreated {
  132. let exist = FileManager.default.fileExists(atPath: currentDocument.fileURL!.path)
  133. if exist {
  134. try? FileManager.default.removeItem(atPath: currentDocument.fileURL!.path)
  135. }
  136. currentCloseDocument = currentDocument
  137. currentDocument.runModalSavePanel(for: .saveToOperation, delegate: self, didSave: #selector(saveSuccess(_:)), contextInfo: nil)
  138. } else if (currentDocument.cloud) { /// 云文档
  139. if (currentDocument.isDocumentEdited) {
  140. currentCloseDocument = currentDocument
  141. currentDocument.save(withDelegate: self, didSave: #selector(saveSuccess(_:)), contextInfo: nil)
  142. } else {
  143. currentDocument.close()
  144. canClose = true
  145. }
  146. } else {
  147. if currentDocument.isDocumentEdited && isPrompt {
  148. if currentDocument.isKind(of: KMMainDocument.self) {
  149. currentDocument.closedByUserGestureFlag = true
  150. }
  151. currentDocument.watermarkSaveDelegate = self
  152. currentDocument.canClose(withDelegate: self, shouldClose: #selector(document(_:shouldClose:contextInfo:)), contextInfo: nil)
  153. } else if currentDocument.isDocumentEdited {
  154. if (!currentDocument.needSaveWatermark()) {
  155. currentDocument.save(nil)
  156. currentDocument.close()
  157. canClose = true
  158. } else {
  159. currentDocument.watermarkSaveDelegate = self
  160. currentDocument.saveForWatermark()
  161. canClose = false
  162. }
  163. } else if (currentDocument.mainViewController != nil) {
  164. if (currentDocument.mainViewController!.isPDFDocumentEdited || currentDocument.mainViewController!.needSave) {
  165. // currentCloseDocument = currentDocument
  166. // currentDocument.save(withDelegate: self, didSave: #selector(saveSuccess(_:)), contextInfo: nil)
  167. if (isPrompt) {
  168. let alert = NSAlert()
  169. let message = "Do you want to save the changes made to the document “\(currentDocument.fileURL!.lastPathComponent)”?"
  170. alert.messageText = message
  171. alert.informativeText = NSLocalizedString("Your changes will be lost if you don’t save them.", comment: "")
  172. alert.addButton(withTitle: NSLocalizedString("Save", comment: ""))
  173. alert.addButton(withTitle: NSLocalizedString("Don‘t Save", comment: ""))
  174. alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
  175. let response = alert.runModal()
  176. if (response == .alertFirstButtonReturn) { // 保存
  177. if let home = self.tabContents(at: 0) as? KMMainDocument, home.isHome { // 标记 home 最近列表需要强制刷新
  178. home.homeViewController?.historyFileViewController.isForceReload = true
  179. }
  180. currentDocument.save(nil)
  181. currentDocument.close()
  182. canClose = true
  183. } else if (response == .alertSecondButtonReturn) { // 不保存
  184. currentDocument.close()
  185. canClose = true
  186. } else { // 取消
  187. canClose = false
  188. }
  189. } else {
  190. currentDocument.save(nil)
  191. currentDocument.close()
  192. canClose = true
  193. }
  194. } else {
  195. currentDocument.close()
  196. canClose = true
  197. }
  198. } else {
  199. currentDocument.close()
  200. canClose = true
  201. }
  202. }
  203. if (canClose) {
  204. // 清理数据 [主要清空通知]
  205. currentDocument.mainViewController?.clearData()
  206. }
  207. return canClose
  208. }
  209. return false
  210. }
  211. func document(_ document: NSDocument?, shouldClose: Bool, contextInfo: UnsafeMutableRawPointer?) {
  212. if (document is KMMainDocument) {
  213. let iu: CloseTypes = CLOSE_USER_GESTURE
  214. if ((document as! KMMainDocument).closedByUserGestureFlag && shouldClose) {
  215. (document as! KMMainDocument).closedByUserGesture = uint(iu.rawValue)
  216. }
  217. (document as! KMMainDocument).closedByUserGestureFlag = false
  218. }
  219. if (shouldClose) {
  220. let currentDocument: KMMainDocument = document! as! KMMainDocument
  221. let index = self.tabStripModel.index(of: currentDocument)
  222. // 清空数据 [主要清理通知]
  223. currentDocument.mainViewController?.clearData()
  224. currentDocument.close()
  225. self.tabStripModel.detachTabContents(at: index)
  226. }
  227. if (self.isCloseAllTabViewItem) {
  228. self.numberOfTabViewItem = self.numberOfTabViewItem - 1
  229. self.closeTabViewItem()
  230. }
  231. }
  232. func saveSuccess(_ sender: Any) -> Void {
  233. if (currentCloseDocument != nil) {
  234. if (currentCloseDocument!.cloud) { /// 云文档
  235. currentCloseDocument!.uploadToCloud({ [weak self] finished, error in
  236. if (finished) {
  237. if (self!.currentCloseDocument == nil) {
  238. return
  239. }
  240. let index = self?.tabStripModel.index(of: self!.currentCloseDocument!)
  241. self?.tabStripModel.detachTabContents(at: index!)
  242. // self!.currentCloseDocument!.close()
  243. self!.currentCloseDocument = nil
  244. if (self!.isCloseAllTabViewItem) {
  245. self!.numberOfTabViewItem = self!.numberOfTabViewItem - 1
  246. self!.closeTabViewItem()
  247. }
  248. }
  249. })
  250. return
  251. }
  252. if let isNewCreated = currentCloseDocument?.isNewCreated, isNewCreated {
  253. if let _url = currentCloseDocument?.saveToURL { // 将新创建的空白文档将入最近列表
  254. NSDocumentController.shared.noteNewRecentDocumentURL(_url)
  255. }
  256. }
  257. let index = tabStripModel.index(of: currentCloseDocument)
  258. tabStripModel.detachTabContents(at: index)
  259. // 清空数据 [主要清理通知]
  260. currentCloseDocument?.mainViewController?.clearData()
  261. currentCloseDocument?.close()
  262. currentCloseDocument = nil
  263. }
  264. if isCloseAllTabViewItem {
  265. numberOfTabViewItem = numberOfTabViewItem - 1
  266. closeTabViewItem()
  267. }
  268. }
  269. func addNewTabContents() {
  270. if (self.tabCount() <= 0) {
  271. KMPrint("New Tab can‘t add first.")
  272. return
  273. }
  274. let contents = self.createBlankTabBased(on: nil)
  275. contents.isHome = true
  276. contents.isNewTab = true
  277. self.add(contents, at: -1, inForeground: true)
  278. }
  279. }
  280. extension KMBrowser {
  281. @objc public func canSelectNextTab() -> Bool {
  282. if (self.tabStripModel.count() < 1) {
  283. return false
  284. }
  285. if (self.tabStripModel.activeIndex >= (self.tabStripModel.count()-1)) {
  286. return false
  287. }
  288. return true
  289. }
  290. @objc public func canSelectPreviousTab() -> Bool {
  291. if (self.tabStripModel.activeIndex <= 0) {
  292. return false
  293. }
  294. return true
  295. }
  296. }