KMBrowser.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. // currentCloseDocument = currentDocument
  170. // currentDocument.save(withDelegate: self, didSave: #selector(saveSuccess(_:)), contextInfo: nil)
  171. if (isPrompt) {
  172. let alert = NSAlert()
  173. let message = "Do you want to save the changes made to the document “\(currentDocument.fileURL!.lastPathComponent)”?"
  174. alert.messageText = message
  175. alert.informativeText = NSLocalizedString("Your changes will be lost if you don’t save them.", comment: "")
  176. alert.addButton(withTitle: NSLocalizedString("Save", comment: ""))
  177. alert.addButton(withTitle: NSLocalizedString("Don‘t Save", comment: ""))
  178. alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
  179. let response = alert.runModal()
  180. if (response == .alertFirstButtonReturn) { // 保存
  181. if let home = self.tabContents(at: 0) as? KMMainDocument, home.isHome { // 标记 home 最近列表需要强制刷新
  182. home.homeViewController?.historyFileViewController.isForceReload = true
  183. }
  184. currentDocument.save(nil)
  185. currentDocument.close()
  186. canClose = true
  187. } else if (response == .alertSecondButtonReturn) { // 不保存
  188. currentDocument.close()
  189. canClose = true
  190. } else { // 取消
  191. canClose = false
  192. }
  193. } else {
  194. currentDocument.save(nil)
  195. currentDocument.close()
  196. canClose = true
  197. }
  198. } else {
  199. currentDocument.close()
  200. canClose = true
  201. }
  202. } else {
  203. currentDocument.close()
  204. canClose = true
  205. }
  206. }
  207. if (canClose) {
  208. // 清理数据 [主要清空通知]
  209. currentDocument.mainViewController?.clearData()
  210. self._trackCloseDocumentEvent()
  211. }
  212. return canClose
  213. }
  214. return false
  215. }
  216. func document(_ document: NSDocument?, shouldClose: Bool, contextInfo: UnsafeMutableRawPointer?) {
  217. if (document is KMMainDocument) {
  218. let iu: CloseTypes = CLOSE_USER_GESTURE
  219. if ((document as! KMMainDocument).closedByUserGestureFlag && shouldClose) {
  220. (document as! KMMainDocument).closedByUserGesture = uint(iu.rawValue)
  221. }
  222. (document as! KMMainDocument).closedByUserGestureFlag = false
  223. }
  224. if (shouldClose) {
  225. let currentDocument: KMMainDocument = document! as! KMMainDocument
  226. let index = self.tabStripModel.index(of: currentDocument)
  227. // 清空数据 [主要清理通知]
  228. currentDocument.mainViewController?.clearData()
  229. self._trackCloseDocumentEvent()
  230. if index != kNoTab {
  231. self.tabStripModel.detachTabContents(at: index)
  232. }
  233. currentDocument.close()
  234. }
  235. // if (self.isCloseAllTabViewItem) {
  236. // self.numberOfTabViewItem = self.numberOfTabViewItem - 1
  237. // self.closeTabViewItem()
  238. // }
  239. }
  240. func saveSuccess(_ sender: Any) -> Void {
  241. if (currentCloseDocument != nil) {
  242. if (currentCloseDocument!.cloud) { /// 云文档
  243. currentCloseDocument!.uploadToCloud({ [weak self] finished, error in
  244. if (finished) {
  245. if (self!.currentCloseDocument == nil) {
  246. return
  247. }
  248. let index = self?.tabStripModel.index(of: self!.currentCloseDocument!)
  249. self?.tabStripModel.detachTabContents(at: index!)
  250. // self!.currentCloseDocument!.close()
  251. self!.currentCloseDocument = nil
  252. if (self!.isCloseAllTabViewItem) {
  253. self!.numberOfTabViewItem = self!.numberOfTabViewItem - 1
  254. self!.closeTabViewItem()
  255. }
  256. }
  257. })
  258. return
  259. }
  260. if let isNewCreated = currentCloseDocument?.isNewCreated, isNewCreated {
  261. if let _url = currentCloseDocument?.saveToURL { // 将新创建的空白文档将入最近列表
  262. NSDocumentController.shared.noteNewRecentDocumentURL(_url)
  263. }
  264. }
  265. let index = tabStripModel.index(of: currentCloseDocument)
  266. tabStripModel.detachTabContents(at: index)
  267. // 清空数据 [主要清理通知]
  268. currentCloseDocument?.mainViewController?.clearData()
  269. currentCloseDocument?.close()
  270. currentCloseDocument = nil
  271. self._trackCloseDocumentEvent()
  272. }
  273. if isCloseAllTabViewItem {
  274. numberOfTabViewItem = numberOfTabViewItem - 1
  275. closeTabViewItem()
  276. }
  277. }
  278. func addNewTabContents() {
  279. if (self.tabCount() <= 0) {
  280. KMPrint("New Tab can‘t add first.")
  281. return
  282. }
  283. let contents = self.createBlankTabBased(on: nil)
  284. contents.isHome = true
  285. contents.isNewTab = true
  286. self.add(contents, at: -1, inForeground: true)
  287. }
  288. // MARK: - Private Methods
  289. private func _trackCloseDocumentEvent() {
  290. FMTrackEventManager.defaultManager.trackEvent(event: "Crash", withProperties: ["Crash_Trig" : "Crash_Trig_CloseDocument"])
  291. }
  292. }
  293. extension KMBrowser {
  294. @objc public func canSelectNextTab() -> Bool {
  295. if (self.tabStripModel.count() < 1) {
  296. return false
  297. }
  298. if (self.tabStripModel.activeIndex >= (self.tabStripModel.count()-1)) {
  299. return false
  300. }
  301. return true
  302. }
  303. @objc public func canSelectPreviousTab() -> Bool {
  304. if (self.tabStripModel.activeIndex <= 0) {
  305. return false
  306. }
  307. return true
  308. }
  309. }