|
@@ -5,40 +5,10 @@
|
|
|
// Created by lxy on 2022/12/15.
|
|
|
//
|
|
|
|
|
|
-import Cocoa
|
|
|
-
|
|
|
-@objc protocol KMPDFThumbnailViewDelegate: NSObjectProtocol {
|
|
|
- @objc optional func thumbnailView(thumbanView: KMPDFThumbnailView, didSelectPageAtIndex index: UInt, event: NSEvent)
|
|
|
- @objc optional func thumbnailView(thumbanView: KMPDFThumbnailView, item: KMPDFThumbnailItem, rightMouseDidSelect index: UInt, event: NSEvent)
|
|
|
- @objc optional func thumbnailView(thumbanView: KMPDFThumbnailView, didDragAddFiles files: [URL], indexpath: IndexPath)
|
|
|
- @objc optional func thumbnailView(thumbanView: KMPDFThumbnailView, didDragPages pages: [Int], indexpath: IndexPath)
|
|
|
-
|
|
|
- @objc optional func thumbnailView(thumbanView: KMPDFThumbnailView, minimumLineSpacingForSectionAt section: Int) -> CGFloat
|
|
|
- @objc optional func thumbnailView(thumbanView: KMPDFThumbnailView, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat
|
|
|
- @objc optional func thumbnailView(thumbanView: KMPDFThumbnailView, insetForSectionAt section: Int) -> NSEdgeInsets
|
|
|
-
|
|
|
- @objc optional func thumbnailView(thumbanView: KMPDFThumbnailView, sizeForItemAt indexpath: IndexPath) -> NSSize
|
|
|
- @objc optional func thumbnailView(thumbanView: KMPDFThumbnailView, itemForRepresentedObjectAt indexpath: IndexPath) -> NSCollectionViewItem
|
|
|
-}
|
|
|
-
|
|
|
-class KMPDFThumbnailView: NSView {
|
|
|
- open weak var delegate: KMPDFThumbnailViewDelegate?
|
|
|
-
|
|
|
+class KMPDFThumbnailView: KMThumbnailView {
|
|
|
var document: CPDFDocument?
|
|
|
var isShowPageSize = false
|
|
|
var thumbnailSzie = NSZeroSize
|
|
|
- var scrollView: NSScrollView = NSScrollView()
|
|
|
-
|
|
|
- private var collectionView_: NSCollectionView?
|
|
|
- var collectionView: NSCollectionView? {
|
|
|
- get {
|
|
|
- return self.collectionView_
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private let thumbnailDraggedTypes = NSPasteboard.PasteboardType(rawValue: "localForDraggedTypes")
|
|
|
-
|
|
|
- private var dragedIndexPaths: [IndexPath] = []
|
|
|
|
|
|
//标记线
|
|
|
var isNeedMarkerLine: Bool = false
|
|
@@ -47,121 +17,60 @@ class KMPDFThumbnailView: NSView {
|
|
|
//注释状态
|
|
|
var annotationShowState: KMAnnotationViewShowType = .none {
|
|
|
didSet {
|
|
|
- self.collectionView?.reloadData()
|
|
|
+ self.collectionView.reloadData()
|
|
|
}
|
|
|
}
|
|
|
//悬浮
|
|
|
var hoverIndex: Int = -1
|
|
|
|
|
|
- required init?(coder: NSCoder) {
|
|
|
- super.init(coder: coder)
|
|
|
- self.initUI()
|
|
|
- }
|
|
|
-
|
|
|
- override init(frame frameRect: NSRect) {
|
|
|
- super.init(frame: frameRect)
|
|
|
- self.initUI()
|
|
|
- }
|
|
|
-
|
|
|
- private func initUI() {
|
|
|
+ override func initDefaultValue() {
|
|
|
+ super.initDefaultValue()
|
|
|
+
|
|
|
self.wantsLayer = true
|
|
|
self.layer?.backgroundColor = NSColor(hex: "#F7F8FA").cgColor
|
|
|
|
|
|
self.thumbnailSzie = CGSize(width: 120, height: 155)
|
|
|
-
|
|
|
- let layout = NSCollectionViewFlowLayout()
|
|
|
- layout.sectionInset = NSEdgeInsetsMake(8, 15, 8, 15)
|
|
|
- layout.minimumLineSpacing = 0
|
|
|
- layout.minimumInteritemSpacing = 0
|
|
|
-
|
|
|
- self.collectionView_ = NSCollectionView()
|
|
|
- self.collectionView_?.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
|
|
|
- self.collectionView_?.collectionViewLayout = layout
|
|
|
- self.collectionView_?.dataSource = self
|
|
|
- self.collectionView_?.delegate = self
|
|
|
- self.collectionView_?.register(KMPDFThumbnailItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMPDFThumbnailItem"))
|
|
|
- self.collectionView_?.registerForDraggedTypes([thumbnailDraggedTypes])
|
|
|
- self.collectionView_?.isSelectable = true
|
|
|
-
|
|
|
- self.collectionView_?.wantsLayer = true
|
|
|
- self.collectionView_?.layer?.backgroundColor = NSColor(hex: "#F7F8FA").cgColor
|
|
|
-
|
|
|
- self.scrollView.hasHorizontalScroller = true
|
|
|
- self.scrollView.hasVerticalScroller = true
|
|
|
- self.scrollView.autohidesScrollers = true
|
|
|
- self.scrollView.minMagnification = 1.0
|
|
|
- self.scrollView.scrollerStyle = NSScroller.Style.overlay
|
|
|
- self.scrollView.documentView = self.collectionView
|
|
|
-
|
|
|
- self.scrollView.wantsLayer = true
|
|
|
- self.scrollView.layer?.backgroundColor = NSColor.clear.cgColor
|
|
|
-
|
|
|
- self.scrollView.contentView.layer?.backgroundColor = NSColor(hex: "#FFFFFF").cgColor
|
|
|
- self.scrollView.contentView.wantsLayer = true
|
|
|
-
|
|
|
- self.addSubview(self.scrollView)
|
|
|
-
|
|
|
-
|
|
|
- markerLineView.wantsLayer = true
|
|
|
- markerLineView.layer?.backgroundColor = NSColor(hex: "#1770F4").cgColor
|
|
|
- markerLineView.frame = CGRectMake(0, 0, 100, 2)
|
|
|
- self.collectionView?.addSubview(markerLineView)
|
|
|
- markerLineView.isHidden = true
|
|
|
- }
|
|
|
-
|
|
|
- override func setFrameSize(_ newSize: NSSize) {
|
|
|
- super.setFrameSize(newSize)
|
|
|
- self.scrollView.frame = self.bounds
|
|
|
+ self.minimumLineSpacing = 8
|
|
|
}
|
|
|
|
|
|
- public func reloadData(indexs: Set<IndexPath> = NSSet() as! Set<IndexPath>) {
|
|
|
- if indexs.count == 0 {
|
|
|
- self.collectionView?.reloadData()
|
|
|
- } else {
|
|
|
- var indexpaths: Set<IndexPath> = []
|
|
|
- for index in indexs {
|
|
|
- if (index.section >= self.collectionView!.numberOfSections) {
|
|
|
- continue
|
|
|
- }
|
|
|
- if (index.item >= self.collectionView!.numberOfItems(inSection: index.section)) {
|
|
|
- continue
|
|
|
- }
|
|
|
-
|
|
|
- indexpaths.insert(index)
|
|
|
- }
|
|
|
- if (indexpaths.count == 0) {
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- if (Thread.isMainThread) {
|
|
|
- self.collectionView?.reloadItems(at: indexpaths)
|
|
|
- } else {
|
|
|
- DispatchQueue.main.async {
|
|
|
- self.collectionView?.reloadItems(at: indexpaths)
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ override func initSubViews() {
|
|
|
+ super.initSubViews()
|
|
|
+
|
|
|
+ self.register(KMPDFThumbnailItem.self)
|
|
|
+
|
|
|
+ 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 : NSCollectionViewDataSource {
|
|
|
- func numberOfSections(in collectionView: NSCollectionView) -> Int {
|
|
|
- return 1
|
|
|
+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))
|
|
|
}
|
|
|
|
|
|
- func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
|
|
|
+ override func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
|
|
|
if let count = self.document?.pageCount {
|
|
|
return Int(count)
|
|
|
}
|
|
|
return 0
|
|
|
}
|
|
|
-
|
|
|
- func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
|
|
|
- if let view = self.delegate?.thumbnailView?(thumbanView: self, itemForRepresentedObjectAt: indexPath) {
|
|
|
- return view
|
|
|
+
|
|
|
+ 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: "KMPDFThumbnailItem"), for: indexPath) as! KMPDFThumbnailItem
|
|
|
+
|
|
|
+ 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_
|
|
@@ -174,23 +83,23 @@ extension KMPDFThumbnailView : NSCollectionViewDataSource {
|
|
|
}
|
|
|
|
|
|
cellView.mouseDownAction = { [unowned self] (view, event) in
|
|
|
- self.delegate?.thumbnailView?(thumbanView: self, didSelectPageAtIndex: UInt(indexPath.item), event: event)
|
|
|
+ self.delegate?.thumbnailView?(thumbanView: self, didSelectItemAt: indexPath, object: event)
|
|
|
}
|
|
|
|
|
|
cellView.rightMouseDownAction = { [unowned self] (view, event) in
|
|
|
- self.delegate?.thumbnailView?(thumbanView: self, item: view, rightMouseDidSelect: UInt(indexPath.item), event: event)
|
|
|
+ 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
|
|
|
+ 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
|
|
|
+ if let _ = self.collectionView.item(at: hoverIndex)?.view {
|
|
|
+ let tempCell = self.collectionView.item(at: hoverIndex) as? KMPDFThumbnailItem
|
|
|
tempCell!.hover = true
|
|
|
}
|
|
|
} else {
|
|
@@ -200,100 +109,20 @@ extension KMPDFThumbnailView : NSCollectionViewDataSource {
|
|
|
|
|
|
return cellView
|
|
|
}
|
|
|
-}
|
|
|
-
|
|
|
-extension KMPDFThumbnailView: NSCollectionViewDelegateFlowLayout {
|
|
|
- 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))
|
|
|
- }
|
|
|
-
|
|
|
- func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
|
|
|
- if (self.delegate != nil) {
|
|
|
- let minimumLineSpacing = self.delegate!.thumbnailView?(thumbanView: self, minimumLineSpacingForSectionAt: section)
|
|
|
- if (minimumLineSpacing != nil) {
|
|
|
- return minimumLineSpacing!
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return 8
|
|
|
- }
|
|
|
-
|
|
|
- func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
|
|
|
- if (self.delegate != nil) {
|
|
|
- let minimumInteritemSpacing = self.delegate!.thumbnailView?(thumbanView: self, minimumInteritemSpacingForSectionAt: section)
|
|
|
- if (minimumInteritemSpacing != nil) {
|
|
|
- return minimumInteritemSpacing!
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return 0.01
|
|
|
- }
|
|
|
-
|
|
|
- func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets {
|
|
|
- let inset = self.delegate?.thumbnailView?(thumbanView: self, insetForSectionAt: section)
|
|
|
- if (inset != nil) {
|
|
|
- return inset!
|
|
|
- }
|
|
|
- return NSEdgeInsetsZero
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-extension KMPDFThumbnailView: NSCollectionViewDelegate {
|
|
|
- func collectionView(_ collectionView: NSCollectionView, shouldSelectItemsAt indexPaths: Set<IndexPath>) -> Set<IndexPath> {
|
|
|
- return indexPaths
|
|
|
- }
|
|
|
-
|
|
|
- func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
|
|
|
-// let index: IndexPath = indexPaths.first!
|
|
|
-// self.delegate?.thumbnailView?(thumbanView: self, didSelectPageAtIndex: UInt(index.item))
|
|
|
- }
|
|
|
-
|
|
|
- func collectionView(_ collectionView: NSCollectionView, shouldDeselectItemsAt indexPaths: Set<IndexPath>) -> Set<IndexPath> {
|
|
|
- return indexPaths
|
|
|
- }
|
|
|
|
|
|
- func collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set<IndexPath>) {
|
|
|
- }
|
|
|
-
|
|
|
- func collectionView(_ collectionView: NSCollectionView, canDragItemsAt indexPaths: Set<IndexPath>, with event: NSEvent) -> Bool {
|
|
|
-
|
|
|
- return true
|
|
|
- }
|
|
|
-
|
|
|
- func collectionView(_ collectionView: NSCollectionView, writeItemsAt indexPaths: Set<IndexPath>, to pasteboard: NSPasteboard) -> Bool {
|
|
|
- let data: Data = try! NSKeyedArchiver.archivedData(withRootObject: indexPaths, requiringSecureCoding: true)
|
|
|
- pasteboard.declareTypes([self.thumbnailDraggedTypes], owner: self)
|
|
|
- pasteboard.setData(data, forType: self.thumbnailDraggedTypes)
|
|
|
-
|
|
|
- self.dragedIndexPaths.removeAll()
|
|
|
- for indexPath in indexPaths {
|
|
|
- self.dragedIndexPaths.append(indexPath)
|
|
|
- }
|
|
|
- return true
|
|
|
- }
|
|
|
-
|
|
|
- func collectionView(_ collectionView: NSCollectionView, draggingSession session: NSDraggingSession, willBeginAt screenPoint: NSPoint, forItemsAt indexes: IndexSet) {
|
|
|
- guard let collectionView_ = self.collectionView_ else {
|
|
|
- return
|
|
|
- }
|
|
|
+ override func collectionView(_ collectionView: NSCollectionView, draggingSession session: NSDraggingSession, willBeginAt screenPoint: NSPoint, forItemsAt indexes: IndexSet) {
|
|
|
if self.isNeedMarkerLine {
|
|
|
- self.markBeginIndexes = collectionView_.selectionIndexes
|
|
|
+ self.markBeginIndexes = collectionView.selectionIndexes
|
|
|
}
|
|
|
+ return super.collectionView(collectionView, draggingSession: session, willBeginAt: screenPoint, forItemsAt: indexes)
|
|
|
}
|
|
|
|
|
|
- func collectionView(_ collectionView: NSCollectionView, validateDrop draggingInfo: NSDraggingInfo, proposedIndexPath proposedDropIndexPath: AutoreleasingUnsafeMutablePointer<NSIndexPath>, dropOperation proposedDropOperation: UnsafeMutablePointer<NSCollectionView.DropOperation>) -> NSDragOperation {
|
|
|
+ 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)
|
|
|
+ var rect = self.collectionView.frameForItem(at: proposedDropIndexPath.pointee.item)
|
|
|
rect.size.height = 2
|
|
|
self.markerLineView.frame = rect
|
|
|
self.markerLineView.isHidden = false
|
|
@@ -301,20 +130,15 @@ extension KMPDFThumbnailView: NSCollectionViewDelegate {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- let pboard = draggingInfo.draggingPasteboard
|
|
|
- if (pboard.availableType(from: [thumbnailDraggedTypes]) != nil) {
|
|
|
- return .move
|
|
|
- }
|
|
|
-
|
|
|
- return NSDragOperation(rawValue: 0)
|
|
|
+ return super.collectionView(collectionView, validateDrop: draggingInfo, proposedIndexPath: proposedDropIndexPath, dropOperation: proposedDropOperation)
|
|
|
}
|
|
|
|
|
|
- func collectionView(_ collectionView: NSCollectionView, acceptDrop draggingInfo: NSDraggingInfo, indexPath: IndexPath, dropOperation: NSCollectionView.DropOperation) -> Bool {
|
|
|
- markerLineView.isHidden = true
|
|
|
+ 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: [NSPasteboard.PasteboardType(rawValue: "localForDraggedTypes")]) != nil) {
|
|
|
+ if (pboard.availableType(from: [self.localForDraggedTypes]) != nil) {
|
|
|
let dragIndexPath = self.dragedIndexPaths.first
|
|
|
if (dragIndexPath == nil) {
|
|
|
return false
|
|
@@ -327,19 +151,12 @@ extension KMPDFThumbnailView: NSCollectionViewDelegate {
|
|
|
if (toIndex >= self.document!.pageCount) {
|
|
|
return false
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
if dragIndex == toIndex {
|
|
|
return false
|
|
|
}
|
|
|
-
|
|
|
- var dragPages: [Int] = []
|
|
|
- for indexpath in dragedIndexPaths {
|
|
|
- dragPages.append(indexpath.item)
|
|
|
- }
|
|
|
- self.dragedIndexPaths.removeAll()
|
|
|
-
|
|
|
- self.delegate?.thumbnailView?(thumbanView: self, didDragPages: dragPages, indexpath: indexPath)
|
|
|
- return true
|
|
|
+
|
|
|
+ return super.collectionView(collectionView, acceptDrop: draggingInfo, indexPath: indexPath, dropOperation: dropOperation)
|
|
|
} else if ((pboard.availableType(from: [.fileURL])) != nil) {
|
|
|
//获取url
|
|
|
var array: [URL] = []
|
|
@@ -353,8 +170,9 @@ extension KMPDFThumbnailView: NSCollectionViewDelegate {
|
|
|
}
|
|
|
|
|
|
self.delegate?.thumbnailView?(thumbanView: self, didDragAddFiles: array, indexpath: indexPath)
|
|
|
+ return true
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
return false
|
|
|
}
|
|
|
}
|