123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- //
- // KMPageEditThumbnailView.swift
- // PDF Master
- //
- // Created by tangchao on 2023/1/6.
- //
- import Cocoa
- typealias KMPageEditThumbnailViewSelectionDidChange = (_ selectedIndexs: Set<IndexPath>) -> ()
- class KMPageEditThumbnailView: KMPDFThumbnailView {
-
- var selectionDidChange: KMPageEditThumbnailViewSelectionDidChange!
-
- private let defaultItemSize = NSMakeSize(208, 294)
- var itemSize: NSSize = NSMakeSize(208, 294)
- private var defaultThumbnailSzie: NSSize!
-
- var selectionIndexPaths: Set<IndexPath> {
- get {
- return self.collectionView.selectionIndexPaths
- }
- set {
- var indexpaths: Set<IndexPath> = []
- for indexpath in newValue {
- if (indexpath.section >= self.collectionView.numberOfSections) {
- continue
- }
- if (indexpath.item >= self.collectionView.numberOfItems(inSection: indexpath.section)) {
- continue
- }
-
- indexpaths.insert(indexpath)
- }
-
- self.collectionView.selectionIndexPaths = indexpaths
-
- // 通知外面
- guard let callback = self.selectionDidChange else {
- return
- }
- callback(self.collectionView.selectionIndexPaths)
- }
- }
-
- override init(frame frameRect: NSRect) {
- super.init(frame: frameRect)
-
- self.initDefalutValue()
- }
-
- required init?(coder: NSCoder) {
- super.init(coder: coder)
-
- self.initDefalutValue()
- }
-
- private func initDefalutValue() {
- self.collectionView.allowsMultipleSelection = true
- self.collectionView.register(KMPageEditThumbnailItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMPageEditThumbnailItem"))
- self.collectionView.registerForDraggedTypes([NSPasteboard.PasteboardType(rawValue: "localForDraggedTypes"), .fileURL,.string,.pdf])
- self.collectionView.setDraggingSourceOperationMask([.copy, .delete], forLocal: false)
-
- self.defaultThumbnailSzie = self.thumbnailSzie
- self.isShowPageSize = true
- }
-
- func selectPages(at indexpaths: Set<IndexPath>) {
- self.selectionIndexPaths = indexpaths
- }
-
- public func refreshUI(indexs: Set<IndexPath> = []) {
- if (Thread.isMainThread) {
- if (indexs.isEmpty) {
- self.reloadData()
- } else {
- self.reloadData(indexs: indexs)
- }
- } else {
- DispatchQueue.main.async {
- if (indexs.isEmpty) {
- self.reloadData()
- } else {
- self.reloadData(indexs: indexs)
- }
- }
- }
- }
-
- public func zoomOut() {
- if (self.itemSize.width > self.defaultItemSize.width) {
- return
- }
- if (self.itemSize.width < self.defaultItemSize.width) {
- self.itemSize = self.defaultItemSize
- self.thumbnailSzie = self.defaultThumbnailSzie
- } else if (self.itemSize.width == self.defaultItemSize.width) {
- self.itemSize = NSSize(width: self.defaultItemSize.width*1.5, height: self.defaultItemSize.height*1.5)
- self.thumbnailSzie = NSSize(width: self.defaultThumbnailSzie.width*1.5, height: self.defaultThumbnailSzie.height*1.5)
- }
-
- let indexpaths = self.collectionView.selectionIndexPaths
- self.refreshUI()
- self.collectionView.selectionIndexPaths = indexpaths
- }
-
- public func zoomIn() {
- if (self.itemSize.width < self.defaultItemSize.width) {
- return
- }
- if (self.itemSize.width > self.defaultItemSize.width) {
- self.itemSize = self.defaultItemSize
- self.thumbnailSzie = self.defaultThumbnailSzie
- } else if (self.itemSize.width == self.defaultItemSize.width) {
- self.itemSize = NSSize(width: self.defaultItemSize.width*0.5, height: self.defaultItemSize.height*0.5)
- self.thumbnailSzie = NSSize(width: self.defaultThumbnailSzie.width*0.5, height: self.defaultThumbnailSzie.height*0.5)
- }
-
- let indexpaths = self.collectionView.selectionIndexPaths
- self.refreshUI()
- self.collectionView.selectionIndexPaths = indexpaths
- }
-
- // MARK: NSCollectionView 代理
-
- override func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
- super.collectionView(collectionView, itemForRepresentedObjectAt: indexPath)
- }
-
- override func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
- super.collectionView(collectionView, layout: collectionViewLayout, sizeForItemAt: indexPath)
- }
-
- override func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
- super.collectionView(collectionView, didSelectItemsAt: indexPaths)
-
- DispatchQueue.main.async {
- guard let callback = self.selectionDidChange else {
- return
- }
- callback(self.collectionView.selectionIndexPaths)
- }
- }
-
- override func collectionView(_ collectionView: NSCollectionView, shouldSelectItemsAt indexPaths: Set<IndexPath>) -> Set<IndexPath> {
- return super.collectionView(collectionView, shouldSelectItemsAt: indexPaths)
- }
- override func collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set<IndexPath>) {
- super.collectionView(collectionView, didDeselectItemsAt: indexPaths)
-
- DispatchQueue.main.async {
- guard let callback = self.selectionDidChange else {
- return
- }
-
- callback(self.collectionView.selectionIndexPaths)
- }
- }
-
- override func collectionView(_ collectionView: NSCollectionView, writeItemsAt indexPaths: Set<IndexPath>, to pasteboard: NSPasteboard) -> Bool {
- super.collectionView(collectionView, writeItemsAt: indexPaths, to: pasteboard)
- }
- override func collectionView(_ collectionView: NSCollectionView, validateDrop draggingInfo: NSDraggingInfo, proposedIndexPath proposedDropIndexPath: AutoreleasingUnsafeMutablePointer<NSIndexPath>, dropOperation proposedDropOperation: UnsafeMutablePointer<NSCollectionView.DropOperation>) -> NSDragOperation {
- return super.collectionView(collectionView, validateDrop: draggingInfo, proposedIndexPath: proposedDropIndexPath, dropOperation: proposedDropOperation)
- }
- override func collectionView(_ collectionView: NSCollectionView, acceptDrop draggingInfo: NSDraggingInfo, indexPath: IndexPath, dropOperation: NSCollectionView.DropOperation) -> Bool {
- super.collectionView(collectionView, acceptDrop: draggingInfo, indexPath: indexPath, dropOperation: dropOperation)
- }
- }
|