KMNThumnailViewController.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. //
  2. // KMNThumnailViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by 丁林圭 on 2024/11/2.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. @objc protocol KMNThumnailViewControllerDelegate: AnyObject {
  10. @objc optional func enterPageEditThumnailViewController(thumnailViewController:KMNThumnailViewController)
  11. }
  12. let subThumleftOffser: CGFloat = 40.0 //左右间隔
  13. let suborgWidthOff: CGFloat = 264.0
  14. class KMNThumnailViewController: KMNThumbnailBaseViewController {
  15. let ThumbnailMenuIdentifier_PageEdit = "ThumbnailMenuIdentifier_PageEdit"
  16. weak open var thumnailViewDelegate: KMNThumnailViewControllerDelegate?
  17. private var groupView: ComponentGroup? = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle())
  18. override func viewDidLoad() {
  19. flowLayout.register(KMNThumDecorationView.self, forDecorationViewOfKind: NSUserInterfaceItemIdentifier("Line").rawValue)
  20. super.viewDidLoad()
  21. collectionView.menuClickedAction = {[weak self] point in
  22. let menuStruct = self?.clickMenu(point: point)
  23. if self?.groupView != nil {
  24. self?.groupView?.clickedAutoHide = true
  25. self?.groupView?.groupDelegate = self
  26. self?.groupView?.frame = CGRectMake(0, 0, 180, menuStruct?.viewHeight ?? 0)
  27. self?.groupView?.updateGroupInfo(menuStruct?.menuitems ?? [])
  28. self?.groupView?.showWithPoint(CGPoint(x: point.x, y: point.y - (menuStruct?.viewHeight ?? 0)), relativeTo: self?.collectionView)
  29. }
  30. return NSMenu()
  31. }
  32. }
  33. override func updateUILanguage() {
  34. super.updateUILanguage()
  35. self.groupView?.reloadData()
  36. }
  37. override func updateUIThemeColor() {
  38. super.updateUIThemeColor()
  39. self.view.wantsLayer = true
  40. self.view.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/layout-middle").cgColor
  41. }
  42. // MARK: - public
  43. public func changeDocument(document: CPDFDocument) {
  44. changeDocument = document
  45. }
  46. public func updateThumbnailSize() {
  47. collectionView.reloadData()
  48. }
  49. override func clickMenu(point:NSPoint)->KMNMenuStruct {
  50. let menuStruct = super.clickMenu(point: point)
  51. var viewHeight: CGFloat = menuStruct.viewHeight
  52. var menuItemArr: [ComponentMenuitemProperty] = menuStruct.menuitems
  53. let items: [(String, String)] = [("", ""),("Page Edit", ThumbnailMenuIdentifier_PageEdit)]
  54. for (i, value) in items {
  55. if value.count == 0 {
  56. let property: ComponentMenuitemProperty = ComponentMenuitemProperty.divider()
  57. menuItemArr.append(property)
  58. viewHeight += 8
  59. } else {
  60. let properties_Menuitem: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  61. itemSelected: false,
  62. isDisabled: false,
  63. keyEquivalent: nil,
  64. text: KMLocalizedString(i),
  65. identifier: value)
  66. menuItemArr.append(properties_Menuitem)
  67. viewHeight += 36
  68. }
  69. }
  70. let newMenuStruct = KMNMenuStruct(menuitems: menuItemArr, viewHeight: viewHeight)
  71. return newMenuStruct
  72. }
  73. @objc public func pageEditMenuItemAction() {
  74. thumnailViewDelegate?.enterPageEditThumnailViewController?(thumnailViewController: self)
  75. }
  76. // MARK: - NSCollectionViewDelegateFlowLayout
  77. override func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  78. let thumbnailMode: KMNThumbnail = thumbnails[indexPath.item]
  79. var orgPageHeight = thumbnailMode.pageSize.height
  80. var orgPagewidth = thumbnailMode.pageSize.width
  81. if (thumbnailMode.thumbnaiPage?.rotation ?? 0) % 180 != 0 {
  82. orgPageHeight = thumbnailMode.pageSize.width
  83. orgPagewidth = thumbnailMode.pageSize.height
  84. }
  85. let cellWidth = self.view.bounds.width - subThumleftOffser * 2 - infoThumTitleBottom * 2
  86. var cellHeight = cellWidth * (orgPageHeight / orgPagewidth)
  87. let scal = cellWidth / (suborgWidthOff - subThumleftOffser * 2 - infoThumTitleBottom * 2)
  88. if cellHeight > maxCellHeight * scal {
  89. cellHeight = maxCellHeight * scal
  90. } else if(cellHeight < minCellHeight * scal) {
  91. cellHeight = minCellHeight * scal
  92. }
  93. if(isShowPageSize) {
  94. cellHeight += infoThumTitleHeight
  95. }
  96. return CGSize(width: cellWidth + infoThumTitleBottom * 2, height: cellHeight + infoThumTitleBottom + infoThumTitleHeight + topThumOffset * 2)
  97. }
  98. override func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  99. return 20.0
  100. }
  101. override func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  102. return 40.0
  103. }
  104. public override func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets {
  105. return NSEdgeInsetsMake(8.0, 0, 8.0, 0)
  106. }
  107. //MARK: - NSCollectionViewDelegate
  108. override func collectionView(_ collectionView: NSCollectionView, validateDrop draggingInfo: NSDraggingInfo, proposedIndexPath proposedDropIndexPath: AutoreleasingUnsafeMutablePointer<NSIndexPath>, dropOperation proposedDropOperation: UnsafeMutablePointer<NSCollectionView.DropOperation>) -> NSDragOperation {
  109. let mouseLocation = draggingInfo.draggingLocation
  110. if proposedDropOperation.pointee != .on {
  111. let dropTargetIndexPath = proposedDropIndexPath.pointee as IndexPath
  112. print(dropTargetIndexPath.item)
  113. let targetFrame = collectionView.frameForItem(at: dropTargetIndexPath.item)
  114. flowLayout.showLine(at: dropTargetIndexPath, below: targetFrame)
  115. } else {
  116. flowLayout.hideLine()
  117. }
  118. return super.collectionView(collectionView, validateDrop: draggingInfo, proposedIndexPath: proposedDropIndexPath, dropOperation: proposedDropOperation)
  119. }
  120. override func collectionView(_ collectionView: NSCollectionView,
  121. acceptDrop draggingInfo: NSDraggingInfo,
  122. indexPath: IndexPath,
  123. dropOperation: NSCollectionView.DropOperation) -> Bool {
  124. flowLayout.hideLine()
  125. return super.collectionView(collectionView, acceptDrop: draggingInfo, indexPath: indexPath, dropOperation: dropOperation)
  126. }
  127. override func collectionView(_ collectionView: NSCollectionView,
  128. draggingSession session: NSDraggingSession,
  129. endedAt screenPoint: NSPoint,
  130. dragOperation operation: NSDragOperation) {
  131. flowLayout.hideLine()
  132. super.collectionView(collectionView, draggingSession: session, endedAt: screenPoint, dragOperation: operation)
  133. }
  134. }
  135. //MARK: - ComponentSelectDelegate
  136. extension KMNThumnailViewController: ComponentGroupDelegate {
  137. func componentGroupDidSelect(group: ComponentGroup?, menuItemProperty: ComponentMenuitemProperty?) {
  138. if menuItemProperty?.identifier == ThumbnailMenuIdentifier_Copy {
  139. copyMenuItemAciton()
  140. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_Cut) {
  141. cutMenuItemAciton()
  142. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_Paste) {
  143. if(menuItemProperty != nil) {
  144. pastMenuItemAciton(menuitemProperty: menuItemProperty!)
  145. }
  146. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_Delete) {
  147. cutMenuItemAciton()
  148. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_RotateRight) {
  149. rotatePageRightAction()
  150. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_RotateLeft) {
  151. rotatePageLeftAction()
  152. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_InsertFile) {
  153. insertFromPDFAction()
  154. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_Replace) {
  155. replacePDFAction()
  156. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_InsertBlank) {
  157. insertFromBlankAction()
  158. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_Export) {
  159. extractPDFAction()
  160. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_Share) {
  161. if let point = menuItemProperty?.representedObject as? NSPoint {
  162. let rightIndex = collectionView.indexPathForItem(at: collectionView.convert(point, from: nil))
  163. if rightIndex != nil {
  164. let cellView = collectionView.item(at: rightIndex!) as? KMNThumbnailCollectionViewItem
  165. let indexpaths = collectionView.selectionIndexPaths
  166. let doucument = showDocument
  167. let filename : String = doucument?.documentURL.lastPathComponent ?? ""
  168. let folderPath = (NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(filename)) ?? ""
  169. try? FileManager.default.removeItem(atPath: folderPath)
  170. let pdfdocument = CPDFDocument()
  171. let ret = pdfdocument?.importPages(KMNTools.indexpathsToIndexs(indexpaths: indexpaths), from: doucument, at: 0) ?? false
  172. let url = URL(fileURLWithPath: folderPath)
  173. if ret && cellView?.view != nil {
  174. let _ = pdfdocument?.write(toFile: folderPath)
  175. let selects: [URL] = [url]
  176. let picker = NSSharingServicePicker.init(items: selects)
  177. let rect = CGRectMake(CGRectGetMaxX(cellView!.view.bounds), CGRectGetMinY(cellView!.view.bounds), 1, 1)
  178. picker.show(relativeTo: rect, of: cellView!.view, preferredEdge: NSRectEdge.maxX)
  179. }
  180. }
  181. }
  182. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_PageEdit) {
  183. pageEditMenuItemAction()
  184. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_FileShowSize) {
  185. displayPageSizeAction()
  186. }
  187. }
  188. func componentGroupDidDismiss(group: ComponentGroup?) {
  189. }
  190. }