KMBrowser.swift 15 KB

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