// // KMPageEditThumbnailView.swift // PDF Master // // Created by tangchao on 2023/1/6. // import Cocoa typealias KMPageEditThumbnailViewSelectionDidChange = (_ selectedIndexs: Set) -> () class KMPageEditThumbnailView: KMPDFThumbnailView { var selectionDidChange: KMPageEditThumbnailViewSelectionDidChange? private let defaultItemSize = NSMakeSize(208, 294) private var defaultThumbnailSzie: NSSize? override var selectionIndexPaths: Set { get { return super.selectionIndexPaths } set { super.selectionIndexPaths = newValue // 通知外面 guard let callback = self.selectionDidChange else { return } callback(self.collectionView.selectionIndexPaths) } } override func initDefaultValue() { super.initDefaultValue() self.itemSize = NSMakeSize(208, 294) self.defaultThumbnailSzie = self.thumbnailSzie self.isShowPageSize = true self.register(KMPageEditThumbnailItem.self) self.collectionView.allowsMultipleSelection = true } func selectPages(at indexpaths: Set) { self.selectionIndexPaths = indexpaths } public func canZoomOut() -> Bool { return self.itemSize.width <= self.defaultItemSize.width } public func zoomOut() { if (self.canZoomOut() == false) { return } let indexpaths = self.collectionView.selectionIndexPaths 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) } self.reloadData() self.collectionView.selectionIndexPaths = indexpaths } public func canZoomIn() -> Bool { return self.itemSize.width >= self.defaultItemSize.width } public func zoomIn() { if (self.canZoomIn() == false) { return } let indexpaths = self.collectionView.selectionIndexPaths 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) } self.reloadData() self.collectionView.selectionIndexPaths = indexpaths } } extension KMPageEditThumbnailView { override func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set) { super.collectionView(collectionView, didSelectItemsAt: indexPaths) DispatchQueue.main.async { guard let callback = self.selectionDidChange else { return } callback(collectionView.selectionIndexPaths) } } override func collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set) { super.collectionView(collectionView, didDeselectItemsAt: indexPaths) DispatchQueue.main.async { guard let callback = self.selectionDidChange else { return } callback(collectionView.selectionIndexPaths) } } }