KMNThumbnailBaseViewController.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. @objc optional func insertPDFThumbnailViewControlle(pageEditVC:KMNThumbnailBaseViewController?,pdfDocment:CPDFDocument?)
  11. @objc optional func changeIndexPathsThumbnailViewControlle(pageEditVC:KMNThumbnailBaseViewController?,selectionIndexPaths: Set<IndexPath>,selectionStrings:String )
  12. }
  13. enum KMNThumbnailChoosePageStyle: Int {
  14. case odd
  15. case even
  16. case horizontal
  17. case vertical
  18. case allPage
  19. case custom
  20. }
  21. internal let kmnThumLocalForDraggedTypes = NSPasteboard.PasteboardType(rawValue: "kmnThumLocalForDraggedTypes")
  22. class KMNThumbnailBaseViewController: KMNBaseViewController,NSCollectionViewDelegate, NSCollectionViewDataSource,NSCollectionViewDelegateFlowLayout {
  23. weak open var thumbnailBaseViewDelegate: KMNThumbnailBaseViewDelegate?
  24. @IBOutlet var backViewBox: NSBox!
  25. @IBOutlet var collectionView: KMNThumbnailCollectionView!
  26. let subTitleHeight: CGFloat = 20.0
  27. let maxCellHeight: CGFloat = 280.0
  28. private var currentDocument:CPDFDocument?
  29. public var currentUndoManager:UndoManager?
  30. public var showDocument: CPDFDocument? {
  31. return currentDocument
  32. }
  33. private var thumbnails:[KMNThumbnail] = []
  34. private var isChangeIndexPaths = false
  35. public var thumbnailChoosePageStyle:KMNThumbnailChoosePageStyle = .custom {
  36. didSet {
  37. var tSelectionIndexPaths: Set<IndexPath> = []
  38. let pageCount = currentDocument?.pageCount ?? 0
  39. switch thumbnailChoosePageStyle {
  40. case .even:
  41. for i in 0 ..< pageCount {
  42. if(i % 2 == 0) {
  43. tSelectionIndexPaths.insert(IndexPath(item: Int(i), section: 0))
  44. }
  45. }
  46. case .odd:
  47. for i in 0 ..< pageCount {
  48. if(i % 2 != 0) {
  49. tSelectionIndexPaths.insert(IndexPath(item: Int(i), section: 0))
  50. }
  51. }
  52. case .allPage:
  53. for i in 0 ..< pageCount {
  54. tSelectionIndexPaths.insert(IndexPath(item: Int(i), section: 0))
  55. }
  56. case .vertical:
  57. for i in 0 ..< pageCount {
  58. let page = showDocument?.page(at: i)
  59. if(page != nil) {
  60. if(page!.rotation % 180 != 0) {
  61. tSelectionIndexPaths.insert(IndexPath(item: Int(i), section: 0))
  62. }
  63. }
  64. }
  65. case .horizontal:
  66. for i in 0 ..< pageCount {
  67. let page = showDocument?.page(at: i)
  68. if(page != nil) {
  69. if(page!.rotation % 180 == 0) {
  70. tSelectionIndexPaths.insert(IndexPath(item: Int(i), section: 0))
  71. }
  72. }
  73. }
  74. default: break
  75. }
  76. isChangeIndexPaths = true
  77. collectionView.selectionIndexPaths = tSelectionIndexPaths
  78. isChangeIndexPaths = false
  79. }
  80. }
  81. var selectionIndexPaths: Set<IndexPath> = [] {
  82. didSet {
  83. var indexpaths: Set<IndexPath> = []
  84. for indexpath in selectionIndexPaths {
  85. if (indexpath.section >= collectionView.numberOfSections) {
  86. continue
  87. }
  88. if indexpath.section < 0 {
  89. continue
  90. }
  91. if (indexpath.item >= collectionView.numberOfItems(inSection: indexpath.section)) {
  92. continue
  93. }
  94. if indexpath.item < 0 {
  95. continue
  96. }
  97. indexpaths.insert(indexpath)
  98. }
  99. collectionView.selectionIndexPaths = indexpaths
  100. collectionView.scrollToItems(at: indexpaths, scrollPosition: .top)
  101. }
  102. }
  103. public var clickPageIndex: Int{
  104. let minIndexPath = selectionIndexPaths.min(by: { $0.item < $1.item })
  105. return minIndexPath?.item ?? 0
  106. }
  107. public var isAdjustWidth:Bool = false
  108. public var isShowPageSize:Bool = false {
  109. didSet {
  110. if oldValue != isShowPageSize {
  111. var pageSize = pageThumbnailSize
  112. if(isShowPageSize) {
  113. pageSize.height += subTitleHeight
  114. } else {
  115. pageSize.height -= subTitleHeight
  116. }
  117. pageThumbnailSize = pageSize
  118. collectionView.reloadData()
  119. }
  120. }
  121. }
  122. public var pageThumbnailSize:CGSize = CGSizeMake(185.0, 260) {
  123. didSet {
  124. collectionView.reloadData()
  125. }
  126. }
  127. public let defaultItemSize = NSMakeSize(185.0, 260)
  128. var dragLocalityPages: [CPDFPage] = []
  129. deinit {
  130. thumbnailBaseViewDelegate = nil
  131. KMPrint("KMNThumbnailBaseViewController deinit.")
  132. }
  133. init(_ document: CPDFDocument?) {
  134. super.init(nibName: "KMNThumbnailBaseViewController", bundle: nil)
  135. currentDocument = document
  136. }
  137. init(_ filePath: String,password:String?) {
  138. super.init(nibName: "KMNThumbnailBaseViewController", bundle: nil)
  139. let document = CPDFDocument.init(url: URL(fileURLWithPath: filePath))
  140. if password != nil {
  141. document?.unlock(withPassword: password as String?)
  142. }
  143. if document?.allowsCopying == false || document?.allowsPrinting == false {
  144. self.exitCurrentView()
  145. } else {
  146. currentDocument = document
  147. }
  148. }
  149. required init?(coder: NSCoder) {
  150. fatalError("init(coder:) has not been implemented")
  151. }
  152. override func viewDidLoad() {
  153. super.viewDidLoad()
  154. collectionView.delegate = self
  155. collectionView.dataSource = self
  156. collectionView.isSelectable = true //支持拖拽需设置未True
  157. collectionView.allowsMultipleSelection = true
  158. collectionView.register(KMNThumbnailCollectionViewItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "thumbnailCollectionViewItem"))
  159. collectionView.registerForDraggedTypes([.fileURL])
  160. collectionView.setDraggingSourceOperationMask(.every, forLocal: false)
  161. collectionView.setDraggingSourceOperationMask(.every, forLocal: true)
  162. refreshDatas()
  163. }
  164. public func exitCurrentView() {
  165. thumbnailBaseViewDelegate?.clickThumbnailViewControlle?(pageEditVC: self, currentIndex: clickPageIndex)
  166. }
  167. public func supportDragFileTypes()->[String] {
  168. let supportFiles = KMNConvertTool.pdfExtensions + KMConvertPDFManager.supportFileType()
  169. return supportFiles
  170. }
  171. public func refreshDatas() {
  172. thumbnails = []
  173. if currentDocument != nil {
  174. for i in 0 ... currentDocument!.pageCount {
  175. let thumbnail = KMNThumbnail.init(document: currentDocument!, currentPageIndex: Int(i))
  176. thumbnails.append(thumbnail)
  177. }
  178. }
  179. }
  180. // MARK: - NSCollectionViewDataSource
  181. func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  182. return thumbnails.count
  183. }
  184. func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  185. let item: KMNThumbnailCollectionViewItem = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "thumbnailCollectionViewItem"), for: indexPath) as! KMNThumbnailCollectionViewItem
  186. item.isShowFileSize = isShowPageSize
  187. item.doubleClickBack = { [weak self] in
  188. self?.thumbnailBaseViewDelegate?.clickThumbnailViewControlle?(pageEditVC: self, currentIndex: indexPath.item)
  189. }
  190. item.thumbnailMode = thumbnails[indexPath.item]
  191. return item
  192. }
  193. func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
  194. if isChangeIndexPaths == false {
  195. let indexpathsz = self.collectionView.selectionIndexPaths
  196. let dex:IndexSet = indexpathsToIndexs(indexpaths: indexpathsz)
  197. let selectedIndexPathsString = KMNTools.parseIndexSet(indexSet: dex)
  198. thumbnailBaseViewDelegate?.changeIndexPathsThumbnailViewControlle?(pageEditVC: self, selectionIndexPaths: indexpathsz, selectionStrings: selectedIndexPathsString)
  199. }
  200. }
  201. func collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set<IndexPath>) {
  202. if isChangeIndexPaths == false {
  203. let indexpathsz = self.collectionView.selectionIndexPaths
  204. let dex:IndexSet = indexpathsToIndexs(indexpaths: indexpathsz)
  205. let selectedIndexPathsString = KMNTools.parseIndexSet(indexSet: dex)
  206. thumbnailBaseViewDelegate?.changeIndexPathsThumbnailViewControlle?(pageEditVC: self, selectionIndexPaths: indexpathsz, selectionStrings: selectedIndexPathsString)
  207. }
  208. }
  209. // MARK: - NSCollectionViewDelegateFlowLayout
  210. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  211. if isAdjustWidth == false {
  212. return pageThumbnailSize
  213. } else {
  214. let thumbnailMode: KMNThumbnail = thumbnails[indexPath.item]
  215. let pageSize = thumbnailMode.pageSize
  216. var cellHeight = pageSize.width / ((pageThumbnailSize.width - 32) * pageThumbnailSize.height)
  217. var cellWidth = pageThumbnailSize.width
  218. if cellHeight > maxCellHeight {
  219. cellHeight = 280.0
  220. cellWidth = cellHeight/pageSize.height * pageSize.width
  221. }
  222. return CGSize(width: cellWidth, height: cellHeight)
  223. }
  224. }
  225. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  226. return 16.0
  227. }
  228. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  229. return 24.0
  230. }
  231. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets {
  232. return NSEdgeInsetsMake(24.0, 24.0, 24.0, 24.0)
  233. }
  234. //MARK: - NSCollectionViewDelegate
  235. func collectionView(_ collectionView: NSCollectionView,
  236. writeItemsAt indexPaths: Set<IndexPath>,
  237. to pasteboard: NSPasteboard) -> Bool {
  238. if IAPProductsManager.default().isAvailableAllFunction() == false {
  239. return false
  240. }
  241. var docmentName = currentDocument?.documentURL.lastPathComponent.deletingPathExtension ?? ""
  242. let pagesName = indexPaths.count > 1 ? " pages" : " page"
  243. var tFileName = pagesName + KMNTools.parseIndexPathsSet(indexSets: collectionView.selectionIndexPaths)
  244. if tFileName.count > 50 {
  245. tFileName = String(tFileName.prefix(50))
  246. }
  247. pasteboard.declareTypes([.fileURL], owner: self)
  248. let writePDFDocument = CPDFDocument()
  249. for indexPath in indexPaths {
  250. let row = indexPath.item
  251. if let copyPage = currentDocument?.page(at: UInt(row)) as? CPDFPage {
  252. writePDFDocument?.insertPageObject(copyPage, at: writePDFDocument?.pageCount ?? 0)
  253. }
  254. }
  255. var cachesDir = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!
  256. cachesDir = cachesDir.appendingPathComponent("PageEdit_Pasteboard")
  257. let fileManager = FileManager.default
  258. if !fileManager.fileExists(atPath: cachesDir.path) {
  259. try? FileManager.default.createDirectory(atPath: cachesDir.path, withIntermediateDirectories: true, attributes: nil)
  260. }
  261. docmentName = "\(docmentName)\(tFileName)"
  262. if docmentName.count > 50 {
  263. docmentName = String(docmentName.prefix(50))
  264. }
  265. let filePathURL = cachesDir.appendingPathComponent(docmentName).appendingPathExtension("pdf")
  266. let success = writePDFDocument?.write(to: filePathURL, isSaveFontSubset:false)
  267. if success == true {
  268. pasteboard.setPropertyList([filePathURL.path], forType: .fileURL)
  269. return true
  270. } else {
  271. return false
  272. }
  273. }
  274. func collectionView(_ collectionView: NSCollectionView,
  275. draggingSession session: NSDraggingSession,
  276. willBeginAt screenPoint: NSPoint,
  277. forItemsAt indexPaths: Set<IndexPath>) {
  278. let sortedIndexPaths = indexPaths.sorted { (ip1, ip2) -> Bool in
  279. if ip1.section == ip2.section {
  280. return ip1.item < ip2.item
  281. }
  282. return ip1.section < ip2.section
  283. }
  284. dragLocalityPages = []
  285. for fromIndexPath in sortedIndexPaths {
  286. let page = thumbnails[fromIndexPath.item].thumbnaiPage
  287. if(page != nil) {
  288. dragLocalityPages.append(page!)
  289. }
  290. }
  291. }
  292. func collectionView(_ collectionView: NSCollectionView, validateDrop draggingInfo: NSDraggingInfo, proposedIndexPath proposedDropIndexPath: AutoreleasingUnsafeMutablePointer<NSIndexPath>, dropOperation proposedDropOperation: UnsafeMutablePointer<NSCollectionView.DropOperation>) -> NSDragOperation {
  293. let pboard = draggingInfo.draggingPasteboard
  294. if dragLocalityPages.count != 0 {
  295. if proposedDropOperation.pointee == .on {
  296. proposedDropOperation.pointee = .before
  297. }
  298. return .move
  299. } else if let availableType = pboard.availableType(from: [.fileURL]), availableType == .fileURL {
  300. if let fileNames = pboard.propertyList(forType: .fileURL) as? [String], fileNames.count >= 1 {
  301. let path = fileNames.first!
  302. let pathExtension = URL(fileURLWithPath: path).pathExtension.lowercased()
  303. if pathExtension == "pdf" || supportDragFileTypes().contains(pathExtension) {
  304. return .copy
  305. }
  306. }
  307. }
  308. return []
  309. }
  310. func collectionView(_ collectionView: NSCollectionView,
  311. acceptDrop draggingInfo: NSDraggingInfo,
  312. indexPath: IndexPath,
  313. dropOperation: NSCollectionView.DropOperation) -> Bool {
  314. let pboard = draggingInfo.draggingPasteboard
  315. if dragLocalityPages.count != 0 {
  316. movePages(pages: dragLocalityPages, destinationDex: indexPath.item)
  317. return true
  318. } else if let availableType = pboard.availableType(from: [.fileURL]), availableType == .fileURL {
  319. let index = indexPath.item
  320. if let fileNames = pboard.propertyList(forType: .fileURL) as? [String] {
  321. insertFromFilePath(fileNames: fileNames, formDex: 0, indexDex: UInt(index), selectIndexs: []) { zSelectIndexs in
  322. self.refreshDatas()
  323. self.selectPages(with: zSelectIndexs)
  324. }
  325. return true
  326. }
  327. }
  328. return false
  329. }
  330. func collectionView(_ collectionView: NSCollectionView,
  331. draggingSession session: NSDraggingSession,
  332. endedAt screenPoint: NSPoint,
  333. dragOperation operation: NSDragOperation) {
  334. dragLocalityPages = []
  335. }
  336. }