KMNThumnailViewController.swift 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. super.viewDidLoad()
  20. collectionView.menuClickedAction = { point in
  21. let menuStruct = self.clickMenu(point: point)
  22. if self.groupView != nil {
  23. self.groupView?.clickedAutoHide = false
  24. self.groupView?.groupDelegate = self
  25. self.groupView?.frame = CGRectMake(0, 0, 180, menuStruct.viewHeight)
  26. self.groupView?.updateGroupInfo(menuStruct.menuitems)
  27. self.groupView?.showWithPoint(CGPoint(x: point.x, y: point.y - menuStruct.viewHeight), relativeTo: self.collectionView)
  28. }
  29. return NSMenu()
  30. }
  31. }
  32. // MARK: - public
  33. public func changeDocument(document: CPDFDocument) {
  34. changeDocument = document
  35. }
  36. public func updateThumbnailSize() {
  37. collectionView.reloadData()
  38. }
  39. override func clickMenu(point:NSPoint)->ThumbnailMenuStruct {
  40. let menuStruct = super.clickMenu(point: point)
  41. var viewHeight: CGFloat = menuStruct.viewHeight
  42. var menuItemArr: [ComponentMenuitemProperty] = menuStruct.menuitems
  43. let items: [(String, String)] = [("", ""),("Page Edit", ThumbnailMenuIdentifier_PageEdit)]
  44. for (i, value) in items {
  45. if value.count == 0 {
  46. let property: ComponentMenuitemProperty = ComponentMenuitemProperty.divider()
  47. menuItemArr.append(property)
  48. viewHeight += 8
  49. } else {
  50. let properties_Menuitem: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  51. itemSelected: false,
  52. isDisabled: false,
  53. keyEquivalent: nil,
  54. text: KMLocalizedString(i),
  55. identifier: value)
  56. menuItemArr.append(properties_Menuitem)
  57. viewHeight += 36
  58. }
  59. }
  60. let newMenuStruct = ThumbnailMenuStruct(menuitems: menuItemArr, viewHeight: viewHeight)
  61. return newMenuStruct
  62. }
  63. @objc public func pageEditMenuItemAction() {
  64. thumnailViewDelegate?.enterPageEditThumnailViewController?(thumnailViewController: self)
  65. }
  66. // MARK: - NSCollectionViewDelegateFlowLayout
  67. override func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  68. let thumbnailMode: KMNThumbnail = thumbnails[indexPath.item]
  69. var orgPageHeight = thumbnailMode.pageSize.height
  70. var orgPagewidth = thumbnailMode.pageSize.width
  71. if (thumbnailMode.thumbnaiPage?.rotation ?? 0) % 180 != 0 {
  72. orgPageHeight = thumbnailMode.pageSize.width
  73. orgPagewidth = thumbnailMode.pageSize.height
  74. }
  75. let cellWidth = self.view.bounds.width - subThumleftOffser * 2 - infoThumTitleBottom * 2
  76. var cellHeight = cellWidth * (orgPageHeight / orgPagewidth)
  77. let scal = cellWidth / (suborgWidthOff - subThumleftOffser * 2 - infoThumTitleBottom * 2)
  78. if cellHeight > maxCellHeight * scal {
  79. cellHeight = maxCellHeight * scal
  80. } else if(cellHeight < minCellHeight * scal) {
  81. cellHeight = minCellHeight * scal
  82. }
  83. if(isShowPageSize) {
  84. cellHeight += infoThumTitleHeight
  85. }
  86. return CGSize(width: cellWidth + infoThumTitleBottom * 2, height: cellHeight + infoThumTitleBottom + infoThumTitleHeight + topThumOffset * 2)
  87. }
  88. override func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  89. return 20.0
  90. }
  91. override func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  92. return 40.0
  93. }
  94. public override func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets {
  95. return NSEdgeInsetsMake(8.0, 0, 8.0, 0)
  96. }
  97. }
  98. //MARK: - ComponentSelectDelegate
  99. extension KMNThumnailViewController: ComponentGroupDelegate {
  100. func componentGroupDidSelect(group: ComponentGroup?, menuItemProperty: ComponentMenuitemProperty?) {
  101. if menuItemProperty?.identifier == ThumbnailMenuIdentifier_Copy {
  102. copyMenuItemAciton()
  103. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_Cut) {
  104. cutMenuItemAciton()
  105. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_Paste) {
  106. if(menuItemProperty != nil) {
  107. // pastMenuItemAciton(menuitemProperty: menuItemProperty!)
  108. }
  109. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_Delete) {
  110. cutMenuItemAciton()
  111. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_RotateRight) {
  112. rotatePageRightAction()
  113. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_RotateLeft) {
  114. rotatePageLeftAction()
  115. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_InsertFile) {
  116. insertFromPDFAction()
  117. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_InsertBlank) {
  118. insertFromBlankAction()
  119. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_Export) {
  120. extractPDFAction()
  121. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_Share) {
  122. let indexpaths = collectionView.selectionIndexPaths
  123. let doucument = showDocument
  124. let filename : String = doucument?.documentURL.lastPathComponent ?? ""
  125. let folderPath = (NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(filename)) ?? ""
  126. try? FileManager.default.removeItem(atPath: folderPath)
  127. let pdfdocument = CPDFDocument()
  128. let ret = pdfdocument?.importPages(KMNTools.indexpathsToIndexs(indexpaths: indexpaths), from: doucument, at: 0) ?? false
  129. let url = URL(fileURLWithPath: folderPath)
  130. if ret {
  131. let _ = pdfdocument?.write(toFile: folderPath)
  132. let selects: [URL] = [url]
  133. let picker = NSSharingServicePicker.init(items: selects)
  134. if(groupView != nil) {
  135. let rect = CGRectMake(CGRectGetMaxX(groupView!.bounds), CGRectGetMinY(groupView!.bounds) + 35.0 * 2, 1, 1)
  136. picker.show(relativeTo: rect, of: groupView!, preferredEdge: NSRectEdge.maxX)
  137. }
  138. }
  139. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_PageEdit) {
  140. pageEditMenuItemAction()
  141. } else if (menuItemProperty?.identifier == ThumbnailMenuIdentifier_FileShowSize) {
  142. displayPageSizeAction()
  143. }
  144. }
  145. func componentGroupDidDismiss(group: ComponentGroup?) {
  146. }
  147. }