123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- //
- // KMPageEditThumbnailView.swift
- // Cisdem PDFMaster
- //
- // 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)
- private var defaultThumbnailSzie: NSSize?
-
- var area: NSTrackingArea?
-
- override var selectionIndexPaths: Set<IndexPath> {
- 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<IndexPath>) {
- 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<IndexPath>) {
- 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<IndexPath>) {
- super.collectionView(collectionView, didDeselectItemsAt: indexPaths)
-
- DispatchQueue.main.async {
- guard let callback = self.selectionDidChange else {
- return
- }
- callback(collectionView.selectionIndexPaths)
- }
- }
- }
|