KMNThumnailViewController.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 = false
  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("colorBorder/popUp").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)->ThumbnailMenuStruct {
  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 = ThumbnailMenuStruct(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 dropTargetIndexPath = proposedDropIndexPath.pointee as IndexPath
  110. let targetFrame = collectionView.frameForItem(at: dropTargetIndexPath.item)
  111. flowLayout.showLine(at: dropTargetIndexPath, below: targetFrame)
  112. return super.collectionView(collectionView, validateDrop: draggingInfo, proposedIndexPath: proposedDropIndexPath, dropOperation: proposedDropOperation)
  113. }
  114. override func collectionView(_ collectionView: NSCollectionView,
  115. acceptDrop draggingInfo: NSDraggingInfo,
  116. indexPath: IndexPath,
  117. dropOperation: NSCollectionView.DropOperation) -> Bool {
  118. flowLayout.hideLine()
  119. return super.collectionView(collectionView, acceptDrop: draggingInfo, indexPath: indexPath, dropOperation: dropOperation)
  120. }
  121. override func collectionView(_ collectionView: NSCollectionView,
  122. draggingSession session: NSDraggingSession,
  123. endedAt screenPoint: NSPoint,
  124. dragOperation operation: NSDragOperation) {
  125. flowLayout.hideLine()
  126. super.collectionView(collectionView, draggingSession: session, endedAt: screenPoint, dragOperation: operation)
  127. }
  128. }
  129. //MARK: - ComponentSelectDelegate
  130. extension KMNThumnailViewController: ComponentGroupDelegate {
  131. func componentGroupDidSelect(group: ComponentGroup?, menuItemProperty: ComponentMenuitemProperty?) {
  132. if menuItemProperty?.identifier == ThumbnailMenuIdentifier_Copy {
  133. copyMenuItemAciton()
  134. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_Cut) {
  135. cutMenuItemAciton()
  136. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_Paste) {
  137. if(menuItemProperty != nil) {
  138. pastMenuItemAciton(menuitemProperty: menuItemProperty!)
  139. }
  140. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_Delete) {
  141. cutMenuItemAciton()
  142. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_RotateRight) {
  143. rotatePageRightAction()
  144. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_RotateLeft) {
  145. rotatePageLeftAction()
  146. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_InsertFile) {
  147. insertFromPDFAction()
  148. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_InsertBlank) {
  149. insertFromBlankAction()
  150. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_Export) {
  151. extractPDFAction()
  152. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_Share) {
  153. let indexpaths = collectionView.selectionIndexPaths
  154. let doucument = showDocument
  155. let filename : String = doucument?.documentURL.lastPathComponent ?? ""
  156. let folderPath = (NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(filename)) ?? ""
  157. try? FileManager.default.removeItem(atPath: folderPath)
  158. let pdfdocument = CPDFDocument()
  159. let ret = pdfdocument?.importPages(KMNTools.indexpathsToIndexs(indexpaths: indexpaths), from: doucument, at: 0) ?? false
  160. let url = URL(fileURLWithPath: folderPath)
  161. if ret {
  162. let _ = pdfdocument?.write(toFile: folderPath)
  163. let selects: [URL] = [url]
  164. let picker = NSSharingServicePicker.init(items: selects)
  165. if(groupView != nil) {
  166. let rect = CGRectMake(CGRectGetMaxX(groupView!.bounds), CGRectGetMinY(groupView!.bounds) + 35.0 * 2, 1, 1)
  167. picker.show(relativeTo: rect, of: groupView!, preferredEdge: NSRectEdge.maxX)
  168. }
  169. }
  170. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_PageEdit) {
  171. pageEditMenuItemAction()
  172. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_FileShowSize) {
  173. displayPageSizeAction()
  174. }
  175. }
  176. func componentGroupDidDismiss(group: ComponentGroup?) {
  177. }
  178. }