// // KMPageEditThumbnailView.swift // Cisdem PDFMaster // // 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? var area: NSTrackingArea? 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.collectionView.km.register(cell: 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 } override func updateTrackingAreas() { super.updateTrackingAreas() if let _area = self.area, _area.rect.isEmpty == false { if (_area.rect.equalTo(self.bounds)) { return } } if (self.area != nil) { self.removeTrackingArea(self.area!) self.area = nil } self.area = NSTrackingArea(rect: self.bounds, options: [.mouseEnteredAndExited, .mouseMoved, .activeAlways], owner: self) self.addTrackingArea(self.area!) // self.addTrackingRect(self.bounds, owner: self, userData: nil, assumeInside: false) } override func mouseMoved(with event: NSEvent) { super.mouseMoved(with: event) self.window?.mouseMoved(with: event) } } 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) } } }