KMThumbnailView.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. //
  2. // KMThumbnailView.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2023/5/4.
  6. //
  7. import Cocoa
  8. @objc protocol KMThumbnailViewDelegate : NSObjectProtocol {
  9. // layout
  10. @objc optional func thumbnailView(thumbanView: KMThumbnailView, minimumLineSpacingForSectionAt section: Int) -> CGFloat
  11. @objc optional func thumbnailView(thumbanView: KMThumbnailView, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat
  12. @objc optional func thumbnailView(thumbanView: KMThumbnailView, insetForSectionAt section: Int) -> NSEdgeInsets
  13. @objc optional func thumbnailView(thumbanView: KMThumbnailView, sizeForItemAt indexpath: IndexPath) -> NSSize
  14. @objc optional func thumbnailView(thumbanView: KMThumbnailView, numberOfItemsInSection section: Int) -> Int
  15. @objc optional func thumbnailView(thumbanView: KMThumbnailView, itemForRepresentedObjectAt indexpath: IndexPath) -> NSCollectionViewItem
  16. // Drag & Drop
  17. // 本地拖拽
  18. @objc optional func thumbnailView(thumbanView: KMThumbnailView, didDrag dragedIndexPaths: [IndexPath], indexpath: IndexPath)
  19. // 外部拖拽
  20. @objc optional func thumbnailView(thumbanView: KMThumbnailView, didDragAddFiles files: [URL], indexpath: IndexPath)
  21. @objc optional func thumbnailView(thumbanView: KMThumbnailView, didSelectItemAt indexpath: IndexPath, object: AnyObject?)
  22. @objc optional func thumbnailView(thumbanView: KMThumbnailView, rightMouseDidClick indexpath: IndexPath, item: NSCollectionViewItem?, object: AnyObject?)
  23. }
  24. @objc class KMThumbnailView: NSView {
  25. open weak var delegate: KMThumbnailViewDelegate?
  26. internal let localForDraggedTypes = kKMLocalForDraggedTypes
  27. internal var dragedIndexPaths: [IndexPath] = []
  28. override init(frame frameRect: NSRect) {
  29. super.init(frame: frameRect)
  30. self.initDefaultValue()
  31. self.initSubViews()
  32. }
  33. required public init?(coder: NSCoder) {
  34. super.init(coder: coder)
  35. self.initDefaultValue()
  36. self.initSubViews()
  37. }
  38. open var minimumLineSpacing: CGFloat = 0.0 {
  39. didSet {
  40. self.collectionView.reloadData()
  41. }
  42. }
  43. open var minimumInteritemSpacing: CGFloat = 0.0 {
  44. didSet {
  45. self.collectionView.reloadData()
  46. }
  47. }
  48. open var itemSize: NSSize = NSMakeSize(60, 80) {
  49. didSet {
  50. self.collectionView.reloadData()
  51. }
  52. }
  53. open var sectionInset: NSEdgeInsets = NSEdgeInsetsZero {
  54. didSet {
  55. self.collectionView.reloadData()
  56. }
  57. }
  58. open var numberOfSections: Int = 0 {
  59. didSet {
  60. self.collectionView.reloadData()
  61. }
  62. }
  63. var selectionIndexPaths: Set<IndexPath> {
  64. get {
  65. return self.collectionView.selectionIndexPaths
  66. }
  67. set {
  68. var indexpaths: Set<IndexPath> = []
  69. for indexpath in newValue {
  70. if (indexpath.section >= self.collectionView.numberOfSections) {
  71. continue
  72. }
  73. if (indexpath.item >= self.collectionView.numberOfItems(inSection: indexpath.section)) {
  74. continue
  75. }
  76. indexpaths.insert(indexpath)
  77. }
  78. self.collectionView.selectionIndexPaths = indexpaths
  79. }
  80. }
  81. // MARK: - Publick Methods
  82. public func initDefaultValue() {
  83. self.collectionView.registerForDraggedTypes([self.localForDraggedTypes, .fileURL,.string,.pdf])
  84. }
  85. public func initSubViews() {
  86. self.addSubview(self.scrollView)
  87. self.scrollView.frame = self.bounds
  88. self.scrollView.autoresizingMask = [.width, .height]
  89. self.scrollView.documentView = self.collectionView
  90. }
  91. // MARK: - register ItemClass
  92. open func register(_ itemClass: AnyClass?) {
  93. guard let itemClass_ = itemClass else {
  94. return
  95. }
  96. self.register(itemClass_, forItemWithIdentifier: NSStringFromClass(itemClass_))
  97. }
  98. open func register(_ itemClass: AnyClass?, forItemWithIdentifier identifier: String) {
  99. self.collectionView.register(itemClass, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: identifier))
  100. }
  101. // MARK: - 刷新UI
  102. public func reloadData(indexs: Set<IndexPath> = []) {
  103. if (indexs.count == 0) {
  104. if (Thread.isMainThread) {
  105. self.collectionView.reloadData()
  106. } else {
  107. DispatchQueue.main.async {
  108. self.collectionView.reloadData()
  109. }
  110. }
  111. } else {
  112. var indexpaths: Set<IndexPath> = []
  113. for index in indexs {
  114. if (index.section >= self.collectionView.numberOfSections) {
  115. continue
  116. }
  117. if (index.item >= self.collectionView.numberOfItems(inSection: index.section)) {
  118. continue
  119. }
  120. indexpaths.insert(index)
  121. }
  122. if (indexpaths.count == 0) {
  123. return
  124. }
  125. if (Thread.isMainThread) {
  126. self.collectionView.reloadItems(at: indexpaths)
  127. } else {
  128. DispatchQueue.main.async {
  129. self.collectionView.reloadItems(at: indexpaths)
  130. }
  131. }
  132. }
  133. }
  134. // MARK: - 属性 【懒加载】
  135. internal lazy var scrollView_: NSScrollView = {
  136. let view = NSScrollView()
  137. view.hasHorizontalScroller = true
  138. view.hasVerticalScroller = true
  139. view.autohidesScrollers = true
  140. view.minMagnification = 1.0
  141. view.scrollerStyle = .overlay
  142. view.wantsLayer = true
  143. view.layer?.backgroundColor = NSColor.clear.cgColor
  144. view.wantsLayer = true
  145. view.contentView.layer?.backgroundColor = .white
  146. return view
  147. }()
  148. var scrollView: NSScrollView {
  149. get {
  150. return self.scrollView_
  151. }
  152. }
  153. internal lazy var collectionView_: NSCollectionView = {
  154. let view = NSCollectionView()
  155. view.autoresizingMask = [.width, .height]
  156. let layout = NSCollectionViewFlowLayout()
  157. layout.sectionInset = NSEdgeInsetsMake(8, 15, 8, 15)
  158. layout.minimumLineSpacing = 0
  159. layout.minimumInteritemSpacing = 0
  160. view.collectionViewLayout = layout
  161. view.delegate = self
  162. view.dataSource = self
  163. view.register(NSCollectionViewItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMPDFThumbnailItem"))
  164. view.isSelectable = true
  165. view.wantsLayer = true
  166. view.layer?.backgroundColor = NSColor(hex: "#F7F8FA").cgColor
  167. return view
  168. }()
  169. var collectionView: NSCollectionView {
  170. get {
  171. return self.collectionView_
  172. }
  173. }
  174. }
  175. // MARK: - NSCollectionViewDataSource, NSCollectionViewDelegate
  176. extension KMThumbnailView: NSCollectionViewDataSource {
  177. public func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  178. if let items = self.delegate?.thumbnailView?(thumbanView: self, numberOfItemsInSection: section) {
  179. return items
  180. }
  181. return self.numberOfSections
  182. }
  183. func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  184. if let item = self.delegate?.thumbnailView?(thumbanView: self, itemForRepresentedObjectAt: indexPath) {
  185. return item
  186. }
  187. return NSCollectionViewItem()
  188. }
  189. }
  190. // MARK: - NSCollectionViewDelegate
  191. extension KMThumbnailView: NSCollectionViewDelegate {
  192. func collectionView(_ collectionView: NSCollectionView, shouldSelectItemsAt indexPaths: Set<IndexPath>) -> Set<IndexPath> {
  193. return indexPaths
  194. }
  195. func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {}
  196. func collectionView(_ collectionView: NSCollectionView, shouldDeselectItemsAt indexPaths: Set<IndexPath>) -> Set<IndexPath> {
  197. return indexPaths
  198. }
  199. func collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set<IndexPath>) {}
  200. func collectionView(_ collectionView: NSCollectionView, canDragItemsAt indexPaths: Set<IndexPath>, with event: NSEvent) -> Bool {
  201. return true
  202. }
  203. func collectionView(_ collectionView: NSCollectionView, writeItemsAt indexPaths: Set<IndexPath>, to pasteboard: NSPasteboard) -> Bool {
  204. let data: Data = try! NSKeyedArchiver.archivedData(withRootObject: indexPaths, requiringSecureCoding: true)
  205. pasteboard.declareTypes([self.localForDraggedTypes], owner: self)
  206. pasteboard.setData(data, forType: self.localForDraggedTypes)
  207. self.dragedIndexPaths.removeAll()
  208. for indexPath in indexPaths {
  209. self.dragedIndexPaths.append(indexPath)
  210. }
  211. return true
  212. }
  213. func collectionView(_ collectionView: NSCollectionView, draggingSession session: NSDraggingSession, willBeginAt screenPoint: NSPoint, forItemsAt indexes: IndexSet) {}
  214. func collectionView(_ collectionView: NSCollectionView, validateDrop draggingInfo: NSDraggingInfo, proposedIndexPath proposedDropIndexPath: AutoreleasingUnsafeMutablePointer<NSIndexPath>, dropOperation proposedDropOperation: UnsafeMutablePointer<NSCollectionView.DropOperation>) -> NSDragOperation {
  215. let pboard = draggingInfo.draggingPasteboard
  216. if (pboard.availableType(from: [self.localForDraggedTypes]) != nil) {
  217. return .move
  218. }
  219. return NSDragOperation(rawValue: 0)
  220. }
  221. func collectionView(_ collectionView: NSCollectionView, acceptDrop draggingInfo: NSDraggingInfo, indexPath: IndexPath, dropOperation: NSCollectionView.DropOperation) -> Bool {
  222. let pboard = draggingInfo.draggingPasteboard
  223. if (pboard.availableType(from: [self.localForDraggedTypes]) != nil) {
  224. self.delegate?.thumbnailView?(thumbanView: self, didDrag: self.dragedIndexPaths, indexpath: indexPath)
  225. self.dragedIndexPaths.removeAll()
  226. return true
  227. } else if ((pboard.availableType(from: [.fileURL])) != nil) {
  228. var array: [URL] = []
  229. for item: NSPasteboardItem in pboard.pasteboardItems! {
  230. let string: String = item.string(forType: NSPasteboard.PasteboardType.fileURL)!
  231. let url = NSURL(string: string)
  232. array.append(url! as URL)
  233. }
  234. self.delegate?.thumbnailView?(thumbanView: self, didDragAddFiles: array, indexpath: indexPath)
  235. return true
  236. }
  237. return false
  238. }
  239. }
  240. // MARK: - NSCollectionViewDelegateFlowLayout
  241. extension KMThumbnailView: NSCollectionViewDelegateFlowLayout {
  242. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  243. if let size_ = self.delegate?.thumbnailView?(thumbanView: self, sizeForItemAt: indexPath) {
  244. return size_
  245. }
  246. return self.itemSize
  247. }
  248. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  249. if let minimumLineSpacing_ = self.delegate?.thumbnailView?(thumbanView: self, minimumLineSpacingForSectionAt: section) {
  250. return minimumLineSpacing_
  251. }
  252. return self.minimumLineSpacing
  253. }
  254. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  255. if let minimumInteritemSpacing_ = self.delegate?.thumbnailView?(thumbanView: self, minimumInteritemSpacingForSectionAt: section) {
  256. return minimumInteritemSpacing_
  257. }
  258. return self.minimumInteritemSpacing
  259. }
  260. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets {
  261. if let inset = self.delegate?.thumbnailView?(thumbanView: self, insetForSectionAt: section) {
  262. return inset
  263. }
  264. return self.sectionInset
  265. }
  266. }