KMNThumbnailBaseViewController.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. //
  2. // KMNThumbnailBaseViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by 丁林圭 on 2024/10/21.
  6. //
  7. import Cocoa
  8. @objc protocol KMNThumbnailBaseViewDelegate: AnyObject {
  9. @objc optional func clickThumbnailViewControlle(pageEditVC:KMNThumbnailBaseViewController?,currentIndex:Int)
  10. }
  11. internal let kmnThumLocalForDraggedTypes = NSPasteboard.PasteboardType(rawValue: "kmnThumLocalForDraggedTypes")
  12. class KMNThumbnailBaseViewController: NSViewController,NSCollectionViewDelegate, NSCollectionViewDataSource,NSCollectionViewDelegateFlowLayout {
  13. weak open var thumbnailBaseViewDelegate: KMNThumbnailBaseViewDelegate?
  14. @IBOutlet var backViewBox: NSBox!
  15. @IBOutlet var collectionView: NSCollectionView!
  16. let subTitleHeight: CGFloat = 20.0
  17. let maxCellHeight: CGFloat = 280.0
  18. private var currentDocument:CPDFDocument?
  19. public var showDocument: CPDFDocument? {
  20. return currentDocument
  21. }
  22. private var thumbnails:[KMNThumbnail] = []
  23. var selectionIndexPaths: Set<IndexPath> = [] {
  24. didSet {
  25. var indexpaths: Set<IndexPath> = []
  26. for indexpath in selectionIndexPaths {
  27. if (indexpath.section >= collectionView.numberOfSections) {
  28. continue
  29. }
  30. if indexpath.section < 0 {
  31. continue
  32. }
  33. if (indexpath.item >= collectionView.numberOfItems(inSection: indexpath.section)) {
  34. continue
  35. }
  36. if indexpath.item < 0 {
  37. continue
  38. }
  39. indexpaths.insert(indexpath)
  40. }
  41. collectionView.selectionIndexPaths = indexpaths
  42. collectionView.scrollToItems(at: indexpaths, scrollPosition: .top)
  43. }
  44. }
  45. public var clickPageIndex: Int{
  46. let minIndexPath = selectionIndexPaths.min(by: { $0.item < $1.item })
  47. return minIndexPath?.item ?? 0
  48. }
  49. public var isAdjustWidth:Bool = false
  50. public var isShowPageSize:Bool = false {
  51. didSet {
  52. if oldValue != isShowPageSize {
  53. var pageSize = pageThumbnailSize
  54. if(isShowPageSize) {
  55. pageSize.height -= subTitleHeight
  56. } else {
  57. pageSize.height += subTitleHeight
  58. }
  59. pageThumbnailSize = pageSize
  60. }
  61. }
  62. }
  63. public var pageThumbnailSize:CGSize = CGSizeMake(185.0, 260) {
  64. didSet {
  65. collectionView.reloadData()
  66. }
  67. }
  68. var dragLocalityIndexPaths: Set<IndexPath> = []
  69. deinit {
  70. thumbnailBaseViewDelegate = nil
  71. KMPrint("KMNThumbnailBaseViewController deinit.")
  72. }
  73. init(_ document: CPDFDocument?) {
  74. super.init(nibName: "KMNThumbnailBaseViewController", bundle: nil)
  75. currentDocument = document
  76. }
  77. init(_ filePath: String,password:String?) {
  78. super.init(nibName: "KMNThumbnailBaseViewController", bundle: nil)
  79. let document = CPDFDocument.init(url: URL(fileURLWithPath: filePath))
  80. if password != nil {
  81. document?.unlock(withPassword: password as String?)
  82. }
  83. if document?.allowsCopying == false || document?.allowsPrinting == false {
  84. self.exitCurrentView()
  85. } else {
  86. currentDocument = document
  87. }
  88. }
  89. required init?(coder: NSCoder) {
  90. fatalError("init(coder:) has not been implemented")
  91. }
  92. override func viewDidLoad() {
  93. super.viewDidLoad()
  94. thumbnails = []
  95. if currentDocument != nil {
  96. for i in 0 ... currentDocument!.pageCount {
  97. let thumbnail = KMNThumbnail.init(document: currentDocument!, currentPageIndex: Int(i))
  98. thumbnails.append(thumbnail)
  99. }
  100. }
  101. collectionView.delegate = self
  102. collectionView.dataSource = self
  103. collectionView.isSelectable = true //支持拖拽需设置未True
  104. collectionView.allowsMultipleSelection = true
  105. collectionView.register(KMNThumbnailCollectionViewItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "thumbnailCollectionViewItem"))
  106. collectionView.registerForDraggedTypes(NSFilePromiseReceiver.readableDraggedTypes.map { NSPasteboard.PasteboardType($0) })
  107. collectionView.registerForDraggedTypes([kmnThumLocalForDraggedTypes, .fileURL,.string,.pdf])
  108. }
  109. public func exitCurrentView() {
  110. thumbnailBaseViewDelegate?.clickThumbnailViewControlle?(pageEditVC: self, currentIndex: clickPageIndex)
  111. }
  112. private func supportDragFileTypes()->[String] {
  113. let supportFiles = KMTools.pdfExtensions + KMConvertPDFManager.supportFileType()
  114. return supportFiles
  115. }
  116. // MARK: - NSCollectionViewDataSource
  117. func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  118. return thumbnails.count
  119. }
  120. func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  121. let item: KMNThumbnailCollectionViewItem = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "thumbnailCollectionViewItem"), for: indexPath) as! KMNThumbnailCollectionViewItem
  122. item.isShowFileSize = isShowPageSize
  123. item.doubleClickBack = { [weak self] in
  124. self?.thumbnailBaseViewDelegate?.clickThumbnailViewControlle?(pageEditVC: self, currentIndex: indexPath.item)
  125. }
  126. item.thumbnailMode = thumbnails[indexPath.item]
  127. return item
  128. }
  129. // MARK: - NSCollectionViewDelegateFlowLayout
  130. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  131. if isAdjustWidth == false {
  132. return pageThumbnailSize
  133. } else {
  134. let thumbnailMode: KMNThumbnail = thumbnails[indexPath.item]
  135. let pageSize = thumbnailMode.pageSize
  136. var cellHeight = pageSize.width / ((pageThumbnailSize.width - 32) * pageThumbnailSize.height)
  137. var cellWidth = pageThumbnailSize.width
  138. if cellHeight > maxCellHeight {
  139. cellHeight = 280.0
  140. cellWidth = cellHeight/pageSize.height * pageSize.width
  141. }
  142. return CGSize(width: cellWidth, height: cellHeight)
  143. }
  144. }
  145. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  146. return 16.0
  147. }
  148. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  149. return 24.0
  150. }
  151. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets {
  152. return NSEdgeInsetsMake(24.0, 24.0, 24.0, 24.0)
  153. }
  154. //MARK: - NSCollectionViewDelegate
  155. func collectionView(_ collectionView: NSCollectionView, canDragItemsAt indexPaths: Set<IndexPath>, with event: NSEvent) -> Bool {
  156. return IAPProductsManager.default().isAvailableAllFunction()
  157. }
  158. func collectionView(_ collectionView: NSCollectionView, writeItemsAt indexPaths: Set<IndexPath>, to pasteboard: NSPasteboard) -> Bool {
  159. let data: Data = try! NSKeyedArchiver.archivedData(withRootObject: indexPaths, requiringSecureCoding: true)
  160. pasteboard.declareTypes([kmnThumLocalForDraggedTypes], owner: self)
  161. pasteboard.setData(data, forType: kmnThumLocalForDraggedTypes)
  162. dragLocalityIndexPaths.removeAll()
  163. dragLocalityIndexPaths = indexPaths
  164. return true
  165. }
  166. func collectionView(_ collectionView: NSCollectionView, pasteboardWriterForItemAt indexPath: IndexPath) -> NSPasteboardWriting? {
  167. return String(indexPath.item) as NSPasteboardWriting
  168. }
  169. func collectionView(_ collectionView: NSCollectionView, draggingSession session: NSDraggingSession, willBeginAt screenPoint: NSPoint, forItemsAt indexPaths: Set<IndexPath>) {
  170. dragLocalityIndexPaths = indexPaths
  171. }
  172. func collectionView(_ collectionView: NSCollectionView, validateDrop draggingInfo: NSDraggingInfo, proposedIndexPath proposedDropIndexPath: AutoreleasingUnsafeMutablePointer<NSIndexPath>, dropOperation proposedDropOperation: UnsafeMutablePointer<NSCollectionView.DropOperation>) -> NSDragOperation {
  173. let pboard = draggingInfo.draggingPasteboard
  174. if (pboard.availableType(from: [kmnThumLocalForDraggedTypes]) != nil) {
  175. return .move
  176. } else if (pboard.availableType(from: [.localDraggedTypes]) != nil) {
  177. return .move
  178. } else if ((pboard.availableType(from: [.fileURL])) != nil) {
  179. guard let pbItems = pboard.pasteboardItems else {
  180. return NSDragOperation(rawValue: 0)
  181. }
  182. let allowedFileTypes = supportDragFileTypes()
  183. var hasValidFile = false
  184. for item in pbItems {
  185. guard let data = item.string(forType: .fileURL), let fileUrl = URL(string: data) else {
  186. continue
  187. }
  188. let type = fileUrl.pathExtension.lowercased()
  189. if (allowedFileTypes.contains(type)) {
  190. hasValidFile = true
  191. break
  192. }
  193. }
  194. if (!hasValidFile) {
  195. return NSDragOperation(rawValue: 0)
  196. }
  197. }
  198. return .generic
  199. }
  200. func collectionView(_ collectionView: NSCollectionView, acceptDrop draggingInfo: NSDraggingInfo, indexPath: IndexPath, dropOperation: NSCollectionView.DropOperation) -> Bool {
  201. let result = false
  202. return true
  203. }
  204. func collectionView(_ collectionView: NSCollectionView, draggingSession session: NSDraggingSession, endedAt screenPoint: NSPoint, dragOperation operation: NSDragOperation) {
  205. dragLocalityIndexPaths = []
  206. }
  207. }