123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- //
- // KMPDFThumbnailView.swift
- // PDF Master
- //
- // Created by lxy on 2022/12/15.
- //
- class KMPDFThumbnailView: KMThumbnailView {
- var document: CPDFDocument?
- var isShowPageSize = false
- var thumbnailSzie = NSZeroSize
-
- //标记线
- var isNeedMarkerLine: Bool = false
- var markerLineView: NSView = NSView()
- var markBeginIndexes: IndexSet = IndexSet()
- //注释状态
- var annotationShowState: KMAnnotationViewShowType = .none {
- didSet {
- self.collectionView.reloadData()
- }
- }
- //悬浮
- var hoverIndex: Int = -1
-
- override func initDefaultValue() {
- super.initDefaultValue()
-
- self.wantsLayer = true
- self.layer?.backgroundColor = NSColor(hex: "#F7F8FA").cgColor
-
- self.thumbnailSzie = CGSize(width: 120, height: 155)
- self.minimumLineSpacing = 8
- self.register(KMPDFThumbnailItem.self)
- }
-
- override func initSubViews() {
- super.initSubViews()
-
- self.markerLineView.wantsLayer = true
- self.markerLineView.layer?.backgroundColor = NSColor(hex: "#1770F4").cgColor
- self.markerLineView.frame = CGRectMake(0, 0, 100, 2)
- self.collectionView.addSubview(markerLineView)
- self.markerLineView.isHidden = true
- }
- }
- extension KMPDFThumbnailView {
- override func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
- if let size_ = self.delegate?.thumbnailView?(thumbanView: self, sizeForItemAt: indexPath) {
- return size_
- }
-
- let page = self.document?.page(at: UInt(indexPath.item))
- let height = KMPDFThumbnailItem.sizeToFit(size: self.thumbnailSzie, page: page!, isShow: self.isShowPageSize)
-
- return NSMakeSize(collectionView.frame.size.width - 20, CGFloat(height))
- }
-
- override func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
- if let count = self.document?.pageCount {
- return Int(count)
- }
- return 0
- }
-
- override func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
- if let item = self.delegate?.thumbnailView?(thumbanView: self, itemForRepresentedObjectAt: indexPath) {
- return item
- }
- let cellView: KMPDFThumbnailItem = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: NSStringFromClass(KMPDFThumbnailItem.self)), for: indexPath) as! KMPDFThumbnailItem
- cellView.thumbnailView = self
- if let page_ = self.document?.page(at: UInt(indexPath.item)) {
- cellView.page = page_
- }
- cellView.annotationShowState = self.annotationShowState
- if indexPath.item == hoverIndex {
- cellView.hover = true
- } else {
- cellView.hover = false
- }
-
- cellView.mouseDownAction = { [unowned self] (view, event) in
- self.delegate?.thumbnailView?(thumbanView: self, didSelectItemAt: indexPath, object: event)
- }
-
- cellView.rightMouseDownAction = { [unowned self] (view, event) in
- self.delegate?.thumbnailView?(thumbanView: self, rightMouseDidClick: indexPath, item: view, object: event)
- }
-
- cellView.hoverCallBack = { [unowned self] view, mouseEntered in
- if let _ = self.collectionView.item(at: hoverIndex)?.view {
- let tempCell = self.collectionView.item(at: hoverIndex) as? KMPDFThumbnailItem
- tempCell!.hover = false
- }
-
- if mouseEntered {
- hoverIndex = indexPath.item
- if let _ = self.collectionView.item(at: hoverIndex)?.view {
- let tempCell = self.collectionView.item(at: hoverIndex) as? KMPDFThumbnailItem
- tempCell!.hover = true
- }
- } else {
- hoverIndex = -1
- }
- }
-
- return cellView
- }
-
- override func collectionView(_ collectionView: NSCollectionView, draggingSession session: NSDraggingSession, willBeginAt screenPoint: NSPoint, forItemsAt indexes: IndexSet) {
- if self.isNeedMarkerLine {
- self.markBeginIndexes = collectionView.selectionIndexes
- }
- return super.collectionView(collectionView, draggingSession: session, willBeginAt: screenPoint, forItemsAt: indexes)
- }
-
- override func collectionView(_ collectionView: NSCollectionView, validateDrop draggingInfo: NSDraggingInfo, proposedIndexPath proposedDropIndexPath: AutoreleasingUnsafeMutablePointer<NSIndexPath>, dropOperation proposedDropOperation: UnsafeMutablePointer<NSCollectionView.DropOperation>) -> NSDragOperation {
- if self.isNeedMarkerLine {
- if self.markBeginIndexes.count != 0 {
- if !self.markBeginIndexes.contains(proposedDropIndexPath.pointee.item) {
- //标记线
- var rect = self.collectionView.frameForItem(at: proposedDropIndexPath.pointee.item)
- rect.size.height = 2
- self.markerLineView.frame = rect
- self.markerLineView.isHidden = false
- }
- }
- }
-
- return super.collectionView(collectionView, validateDrop: draggingInfo, proposedIndexPath: proposedDropIndexPath, dropOperation: proposedDropOperation)
- }
-
- override func collectionView(_ collectionView: NSCollectionView, acceptDrop draggingInfo: NSDraggingInfo, indexPath: IndexPath, dropOperation: NSCollectionView.DropOperation) -> Bool {
- self.markerLineView.isHidden = true
- self.markBeginIndexes = IndexSet()
-
- let pboard = draggingInfo.draggingPasteboard
- if (pboard.availableType(from: [self.localForDraggedTypes]) != nil) {
- let dragIndexPath = self.dragedIndexPaths.first
- if (dragIndexPath == nil) {
- return false
- }
- let dragIndex = dragIndexPath!.item
- let toIndex = max(0, indexPath.item)
- if dragIndex < 0 || dragIndex > self.document!.pageCount {
- return false
- }
- if (toIndex >= self.document!.pageCount) {
- return false
- }
- if dragIndex == toIndex {
- return false
- }
- return super.collectionView(collectionView, acceptDrop: draggingInfo, indexPath: indexPath, dropOperation: dropOperation)
- } else if ((pboard.availableType(from: [.fileURL])) != nil) {
- //获取url
- var array: [URL] = []
- for item: NSPasteboardItem in pboard.pasteboardItems! {
- let string: String = item.string(forType: NSPasteboard.PasteboardType.fileURL)!
- let url = NSURL(string: string)
- /// MARK: 暂时支持PDF格式
- if (url?.pathExtension?.lowercased() == "pdf") {
- array.append(url! as URL)
- }
- }
-
- self.delegate?.thumbnailView?(thumbanView: self, didDragAddFiles: array, indexpath: indexPath)
- return true
- }
-
- return false
- }
- }
|