KMBrowser.swift 15 KB

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