KMThumbnailView.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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, shouldSelectItemsAt indexPaths: Set<IndexPath>) -> Set<IndexPath> {
  196. if let lastSelectedIndexPath = collectionView.selectionIndexPaths.first {
  197. if NSApp.currentEvent?.modifierFlags.contains(.shift) == true {
  198. // Shift 键按住,进行连续多选
  199. let selectedIndexPaths = collectionView.selectionIndexPaths
  200. var allIndexPaths = Set<IndexPath>(selectedIndexPaths)
  201. // 获取两个 IndexPath 之间的所有 IndexPath
  202. let startIndex = lastSelectedIndexPath.item
  203. let endIndex = indexPaths.first?.item ?? startIndex
  204. let range = startIndex < endIndex ? startIndex...endIndex : endIndex...startIndex
  205. for index in range {
  206. let indexPath = IndexPath(item: index, section: lastSelectedIndexPath.section)
  207. allIndexPaths.insert(indexPath)
  208. }
  209. return allIndexPaths
  210. }
  211. }
  212. return indexPaths
  213. }
  214. func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {}
  215. func collectionView(_ collectionView: NSCollectionView, shouldDeselectItemsAt indexPaths: Set<IndexPath>) -> Set<IndexPath> {
  216. return indexPaths
  217. }
  218. func collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set<IndexPath>) {}
  219. func collectionView(_ collectionView: NSCollectionView, canDragItemsAt indexPaths: Set<IndexPath>, with event: NSEvent) -> Bool {
  220. return true
  221. }
  222. func collectionView(_ collectionView: NSCollectionView, writeItemsAt indexPaths: Set<IndexPath>, to pasteboard: NSPasteboard) -> Bool {
  223. let data: Data = try! NSKeyedArchiver.archivedData(withRootObject: indexPaths, requiringSecureCoding: true)
  224. pasteboard.declareTypes([self.localForDraggedTypes], owner: self)
  225. pasteboard.setData(data, forType: self.localForDraggedTypes)
  226. self.dragedIndexPaths.removeAll()
  227. for indexPath in indexPaths {
  228. self.dragedIndexPaths.append(indexPath)
  229. }
  230. return true
  231. }
  232. func collectionView(_ collectionView: NSCollectionView, draggingSession session: NSDraggingSession, willBeginAt screenPoint: NSPoint, forItemsAt indexes: IndexSet) {}
  233. func collectionView(_ collectionView: NSCollectionView, validateDrop draggingInfo: NSDraggingInfo, proposedIndexPath proposedDropIndexPath: AutoreleasingUnsafeMutablePointer<NSIndexPath>, dropOperation proposedDropOperation: UnsafeMutablePointer<NSCollectionView.DropOperation>) -> NSDragOperation {
  234. let pboard = draggingInfo.draggingPasteboard
  235. if (pboard.availableType(from: [self.localForDraggedTypes]) != nil) {
  236. return .move
  237. }
  238. return NSDragOperation(rawValue: 0)
  239. }
  240. func collectionView(_ collectionView: NSCollectionView, acceptDrop draggingInfo: NSDraggingInfo, indexPath: IndexPath, dropOperation: NSCollectionView.DropOperation) -> Bool {
  241. let pboard = draggingInfo.draggingPasteboard
  242. if (pboard.availableType(from: [self.localForDraggedTypes]) != nil) {
  243. self.delegate?.thumbnailView?(thumbanView: self, didDrag: self.dragedIndexPaths, indexpath: indexPath)
  244. self.dragedIndexPaths.removeAll()
  245. return true
  246. } else if ((pboard.availableType(from: [.fileURL])) != nil) {
  247. var array: [URL] = []
  248. for item: NSPasteboardItem in pboard.pasteboardItems! {
  249. let string: String = item.string(forType: NSPasteboard.PasteboardType.fileURL)!
  250. let url = NSURL(string: string)
  251. array.append(url! as URL)
  252. }
  253. self.delegate?.thumbnailView?(thumbanView: self, didDragAddFiles: array, indexpath: indexPath)
  254. return true
  255. }
  256. return false
  257. }
  258. }
  259. // MARK: - NSCollectionViewDelegateFlowLayout
  260. extension KMThumbnailView: NSCollectionViewDelegateFlowLayout {
  261. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  262. if let size_ = self.delegate?.thumbnailView?(thumbanView: self, sizeForItemAt: indexPath) {
  263. return size_
  264. }
  265. return self.itemSize
  266. }
  267. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  268. if let minimumLineSpacing_ = self.delegate?.thumbnailView?(thumbanView: self, minimumLineSpacingForSectionAt: section) {
  269. return minimumLineSpacing_
  270. }
  271. return self.minimumLineSpacing
  272. }
  273. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  274. if let minimumInteritemSpacing_ = self.delegate?.thumbnailView?(thumbanView: self, minimumInteritemSpacingForSectionAt: section) {
  275. return minimumInteritemSpacing_
  276. }
  277. return self.minimumInteritemSpacing
  278. }
  279. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets {
  280. if let inset = self.delegate?.thumbnailView?(thumbanView: self, insetForSectionAt: section) {
  281. return inset
  282. }
  283. return self.sectionInset
  284. }
  285. }