KMLeftSideViewController+Snapshot.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. //
  2. // KMLeftSideViewController+Snapshot.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2024/1/12.
  6. //
  7. import Foundation
  8. extension KMLeftSideViewController.Key {
  9. static let snapshotSizeScaling = "KMSnapshotSizeScalingKey"
  10. }
  11. // MARK: - Actions
  12. extension KMLeftSideViewController {
  13. @objc func menuItemClick_ExportPNG(_ sender: AnyObject?) {
  14. guard let snapshot = (sender as? NSMenuItem)?.representedObject as? KMSnapshotWindowController else {
  15. return
  16. }
  17. let image = snapshot.thumbnailWithSize(0)
  18. NSPanel.savePanel_data_success(self.view.window!, imageData: image?.pngData(), allowedTypes: ["png"]) { url in
  19. NSWorkspace.shared.selectFile(url.path, inFileViewerRootedAtPath: "")
  20. }
  21. }
  22. @objc func menuItemClick_ExportJPG(_ sender: AnyObject?) {
  23. guard let snapshot = (sender as? NSMenuItem)?.representedObject as? KMSnapshotWindowController else {
  24. return
  25. }
  26. let image = snapshot.thumbnailWithSize(0)
  27. NSPanel.savePanel_data_success(self.view.window!, imageData: image?.jpgData(), allowedTypes: ["jpg"]) { url in
  28. NSWorkspace.shared.selectFile(url.path, inFileViewerRootedAtPath: "")
  29. }
  30. }
  31. @objc func menuItemClick_ExportPDF(_ sender: AnyObject?) {
  32. guard let snapshot = (sender as? NSMenuItem)?.representedObject as? KMSnapshotWindowController else {
  33. return
  34. }
  35. if let image = snapshot.thumbnailWithSize(0) {
  36. let document = CPDFDocument()
  37. _ = document?.km_insert(image: image, at: 0)
  38. NSPanel.savePanel_pdf_success(self.view.window!, document: document) { url in
  39. NSWorkspace.shared.selectFile(url.path, inFileViewerRootedAtPath: "")
  40. }
  41. }
  42. }
  43. @objc func menuItemClick_Print(_ sender: AnyObject?) {
  44. guard let snapshot = (sender as? NSMenuItem)?.representedObject as? KMSnapshotWindowController else {
  45. return
  46. }
  47. if let image = snapshot.thumbnailWithSize(0) {
  48. self.delegate?.controller?(controller: self, itemClick: nil, itemKey: .print, params: image)
  49. }
  50. }
  51. @objc func menuItemClick_SelectAll(_ sender: AnyObject?) {
  52. let selected = self.snapshotListIsAllSelected()
  53. for model in self.snapshots {
  54. model.isSelected = !selected
  55. }
  56. Task { @MainActor in
  57. self.snapshotTableView.reloadData()
  58. }
  59. }
  60. @objc func deleteAllSnapshot(_ sender: AnyObject?) {
  61. for model in self.snapshots {
  62. model.windowC?.close()
  63. }
  64. }
  65. @objc func hideSnapshot(_ sender: AnyObject?) {
  66. guard let snapshot = (sender as? NSMenuItem)?.representedObject as? KMSnapshotWindowController else {
  67. return
  68. }
  69. if let data = snapshot.window?.isVisible, data {
  70. snapshot.miniaturize()
  71. }
  72. }
  73. @objc func showSnapshot(_ sender: AnyObject?) {
  74. guard let snapshot = (sender as? NSMenuItem)?.representedObject as? KMSnapshotWindowController else {
  75. return
  76. }
  77. if let data = snapshot.window?.isVisible, data {
  78. snapshot.window?.orderFront(nil)
  79. } else {
  80. snapshot.deminiaturize()
  81. }
  82. }
  83. @objc func deleteSnapshot(_ sender: AnyObject?) {
  84. guard let snapshot = (sender as? NSMenuItem)?.representedObject as? KMSnapshotWindowController else {
  85. return
  86. }
  87. snapshot.close()
  88. }
  89. @objc func menuItemClick_Copy(_ sender: AnyObject?) {
  90. guard let snapshot = (sender as? NSMenuItem)?.representedObject as? KMSnapshotWindowController else {
  91. return
  92. }
  93. let image = snapshot.thumbnailWithSize(0)
  94. if let tiffData = image?.tiffRepresentation {
  95. let pasteboardItem = NSPasteboardItem()
  96. pasteboardItem.setData(tiffData, forType: .tiff)
  97. let pboard = NSPasteboard.general
  98. pboard.clearContents()
  99. pboard.writeObjects([pasteboardItem])
  100. }
  101. }
  102. func snapshot_initSubViews() {
  103. self.snapshotController.snapshotNormalZoomInButton.target = self
  104. self.snapshotController.snapshotNormalZoomInButton.tag = 2
  105. self.snapshotController.snapshotNormalZoomInButton.action = #selector(thumbnailSizeScaling)
  106. self.snapshotController.snapshotNormalZoomOutButton.target = self
  107. self.snapshotController.snapshotNormalZoomOutButton.tag = 3
  108. self.snapshotController.snapshotNormalZoomOutButton.action = #selector(thumbnailSizeScaling)
  109. self.snapshotController.snapshotNormalMoreButton.target = self
  110. self.snapshotController.snapshotNormalMoreButton.tag = 300
  111. self.snapshotController.snapshotNormalMoreButton.action = #selector(leftSideViewMoreButtonAction)
  112. self.snapshotController.snapshotDoneButton.target = self
  113. self.snapshotController.snapshotDoneButton.tag = 312
  114. self.snapshotController.snapshotDoneButton.action = #selector(leftSideViewDoneButtonAction)
  115. self.snapshotTableView.delegate = self
  116. self.snapshotTableView.dataSource = self
  117. self.snapshotTableView.menu = NSMenu()
  118. self.snapshotTableView.menu?.delegate = self
  119. self.snapshotTableView.doubleAction = #selector(toggleSelectedSnapshots)
  120. self.snapshotTableView.target = self
  121. self.snapshotTableView.botaDelegate = self
  122. self.snapshotTableView.setDraggingSourceOperationMask(.every, forLocal: false)
  123. self.snapshotController.snapshotSearchField.changeCallBack = { [weak self] changeContent in
  124. var editContent = ""
  125. if changeContent != nil {
  126. editContent = changeContent!
  127. }
  128. self?.searchFieldChangeAction(editContent)
  129. }
  130. }
  131. func snapshot_initDefalutValue() {
  132. }
  133. func updataLeftSideSnapView() {
  134. // if (snapshots.count < 1) {
  135. // self.snapshotController.snapshotNormalSearchButton.isEnabled = false
  136. // self.snapshotController.snapshotNormalZoomOutButton.isEnabled = false
  137. // self.snapshotController.snapshotNormalZoomInButton.isEnabled = false
  138. // self.snapshotController.snapshotNormalMoreButton.isEnabled = false
  139. //
  140. // self.leftSideEmptyVC.deleteSnapBtn.isEnabled = false
  141. // self.leftSideEmptyVC.exportSnapBtn.isEnabled = false
  142. // self.leftSideEmptyVC.printSnapBtn.isEnabled = false
  143. // self.snapshotTableView.usesAlternatingRowBackgroundColors = false
  144. //
  145. // self.snapshotController.emptySnapView.isHidden = false
  146. //
  147. // } else {
  148. // self.snapshotController.snapshotNormalSearchButton.isEnabled = true
  149. // self.snapshotController.snapshotNormalZoomOutButton.isEnabled = true
  150. // self.snapshotController.snapshotNormalZoomInButton.isEnabled = true
  151. // self.snapshotController.snapshotNormalMoreButton.isEnabled = true
  152. //
  153. // self.snapshotController.emptySnapView.isHidden = true
  154. //
  155. // self.leftSideEmptyVC.deleteSnapBtn.isEnabled = true
  156. // self.leftSideEmptyVC.exportSnapBtn.isEnabled = true
  157. // self.leftSideEmptyVC.printSnapBtn.isEnabled = true
  158. // }
  159. }
  160. @IBAction func snapshotNormalSearchButtonAction(_ sender: NSButton) {
  161. // self.snapshotController.snapshotSearchField.isHidden = false
  162. // self.snapshotController.snapshotNormalSearchButton.isHidden = true
  163. // self.snapshotController.snapshotDoneButton.isHidden = false
  164. // self.snapshotController.snapshotLabel.isHidden = true
  165. // self.snapshotController.snapshotNormalZoomOutButton.isHidden = true
  166. // self.snapshotController.snapshotNormalZoomInButton.isHidden = true
  167. //
  168. // self.snapshotController.snapshotSearchField.becomeFirstResponder()
  169. }
  170. }
  171. extension KMLeftSideViewController {
  172. public func refreshUIOfSnapshotIfNeed(preference: Bool = false) {
  173. if self.type.methodType != .snapshot {
  174. return
  175. }
  176. if preference {
  177. self.reloadSnapshotSize()
  178. }
  179. Task { @MainActor in
  180. self.snapshotTableView.reloadData()
  181. }
  182. }
  183. public func reloadSnapshotDataIfNeed() {
  184. if self.type.methodType != .snapshot {
  185. return
  186. }
  187. Task { @MainActor in
  188. self.snapshotTableView.reloadData()
  189. }
  190. }
  191. public func reloadSnapshotSize() {
  192. let defaultSize = roundf(KMPreference.shared.thumbSnapshotSize)
  193. var thumbnailSize = Self.kTinySize
  194. if defaultSize < Self.kTinySize + Self.kFudgeSize {
  195. } else {
  196. if defaultSize < Self.kSmallSize + Self.kFudgeSize {
  197. thumbnailSize = Self.kSmallSize
  198. } else {
  199. if defaultSize < Self.kLargeSize + Self.kFudgeSize {
  200. thumbnailSize = Self.kLargeSize
  201. } else {
  202. thumbnailSize = Self.kHugeSize
  203. }
  204. }
  205. }
  206. if (abs(thumbnailSize - Float(self.snapshotCacheSize)) > Self.kFudgeSize) {
  207. self.snapshotCacheSize = thumbnailSize.cgFloat
  208. }
  209. Task { @MainActor in
  210. self.snapshotTableView.reloadData()
  211. }
  212. }
  213. func updateSnapshotsIfNeed(for page: CPDFPage?) {
  214. if self.type.methodType != .snapshot {
  215. return
  216. }
  217. if page != nil {
  218. for model in self.snapshots {
  219. if let wc = model.windowC, wc.isPageVisible(page) {
  220. self.snapshotNeedsUpdate(wc)
  221. wc.redisplay()
  222. }
  223. }
  224. } else {
  225. for model in self.snapshots {
  226. model.windowC?.redisplay()
  227. }
  228. self.allSnapshotsNeedUpdate()
  229. }
  230. }
  231. func snapshotNeedsUpdate(_ dirtySnapshot: KMSnapshotWindowController) {
  232. if self.dirtySnapshots.contains(dirtySnapshot) == false {
  233. self.dirtySnapshots.append(dirtySnapshot)
  234. self.updateSnapshotsIfNeeded()
  235. }
  236. }
  237. func updateSnapshotsIfNeeded() {
  238. // if ([rightSideController.snapshotTableView window] != nil && [dirtySnapshots count] > 0 && snapshotTimer == nil)
  239. // snapshotTimer = [[NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(updateSnapshot:) userInfo:NULL repeats:YES] retain];
  240. }
  241. func allSnapshotsNeedUpdate() {
  242. self.dirtySnapshots.removeAll()
  243. for model in self.snapshots {
  244. if let winC = model.windowC {
  245. self.dirtySnapshots.append(winC)
  246. }
  247. }
  248. self.updateSnapshotsIfNeeded()
  249. }
  250. }
  251. extension KMLeftSideViewController: KMSnapshotWindowControllerDelegate {
  252. func snapshotControllerWillClose(_ controller: KMSnapshotWindowController) {
  253. var flag: KMSnapshotModel?
  254. for model in self.snapshots {
  255. if controller.isEqual(model.windowC) {
  256. flag = model
  257. }
  258. }
  259. if flag != nil {
  260. self.snapshots.removeObject(flag!)
  261. Task { @MainActor in
  262. self.snapshotTableView.reloadData()
  263. self.updataLeftSideSnapView()
  264. }
  265. }
  266. }
  267. func snapshotControllerDidFinishSetup(_ controller: KMSnapshotWindowController) {
  268. let image = controller.thumbnailWithSize(self.snapshotCacheSize)
  269. controller.thumbnail = image
  270. let model = KMSnapshotModel()
  271. model.windowC = controller
  272. self.snapshots.append(model)
  273. Task { @MainActor in
  274. self.snapshotTableView.ks_reloadData()
  275. }
  276. }
  277. func snapshotController(_ controller: KMSnapshotWindowController, miniaturizedRect isMiniaturize: Bool) -> NSRect {
  278. var row = NSNotFound
  279. for (i,sp) in self.snapshots.enumerated() {
  280. if controller.isEqual(sp.windowC) {
  281. row = i
  282. }
  283. }
  284. var rect: NSRect = .zero
  285. if (row != NSNotFound) {
  286. rect = self.snapshotTableView.frameOfCell(atColumn: 0, row: row)
  287. } else {
  288. rect.origin = KMBottomLeftPoint(self.snapshotTableView.visibleRect)
  289. rect.size.width = 1.0
  290. rect.size.height = 1.0
  291. }
  292. rect = self.snapshotTableView.convertRectToScreen(rect)
  293. return rect
  294. }
  295. }