123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- //
- // KMBlankView.swift
- // PDF Reader Pro
- //
- // Created by wanjun on 2023/10/8.
- //
- import Cocoa
- @objc enum KMBlankViewMouseEventType: UInt {
- case mouseEnter
- case mouseExit
- case mouseDown
- case mouseUp
- }
- typealias DragSuccessCallBack = ([String]) -> Void
- @objcMembers
- class KMBlankView: NSView {
- @IBOutlet var titleLabel: NSTextField!
- @IBOutlet var secondTitleLabel: NSTextField!
- @IBOutlet var imageView: NSImageView!
- @IBOutlet var addButton: NSButton!
- @IBOutlet var customView: KMPDFEditAppendCustomView!
- var allowedFileTypes: [String] = ["pdf"]
- var mouseActionCallBack: ((KMBlankViewMouseEventType) -> Void)?
- var dragSuccessBlock: DragSuccessCallBack?
-
- private var _button: NSButton?
- override func awakeFromNib() {
- super.awakeFromNib()
-
- titleLabel.font = NSFont.systemFont(ofSize: 14, weight: .semibold)
- titleLabel.textColor = KMAppearance.Layout.h1Color()
- secondTitleLabel.font = NSFont.systemFont(ofSize: 14)
- secondTitleLabel.textColor = KMAppearance.Layout.h2Color()
-
- titleLabel.stringValue = ""
- secondTitleLabel.stringValue = ""
- addTrackingArea()
- self.wantsLayer = true
-
- if customView == nil {
-
- } else {
- customView.addSubview(button!)
- }
-
- // registerForDraggedTypes([.fileURL])
- }
-
- override func layout() {
- super.layout()
-
- button!.frame = imageView.frame
- }
-
- func registerDragged() {
- registerForDraggedTypes([.fileURL])
- }
-
- //MARK: Drag
-
- override func draggingExited(_ sender: NSDraggingInfo?) {
-
- }
-
- override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
- let pboard = sender.draggingPasteboard
- var isCanDrag = false
- var result: NSDragOperation = []
-
- if pboard.availableType(from: [NSPasteboard.PasteboardType.fileURL]) != nil {
- if let fileURLs = pboard.readObjects(forClasses: [NSURL.self], options: nil) as? [URL] {
- for fileURL in fileURLs {
- if allowedFileTypes.contains(fileURL.pathExtension.lowercased()) {
- isCanDrag = true
- } else {
- isCanDrag = false
- break
- }
- }
- }
- }
-
- layer?.borderColor = NSColor.clear.cgColor
-
- if isCanDrag {
- result = [.copy]
- }
-
- return result
- }
-
- override func prepareForDragOperation(_ sender: NSDraggingInfo) -> Bool {
- let pboard = sender.draggingPasteboard
- if (pboard.pasteboardItems == nil) {
- return true
- }
- var files: [String] = []
- for item in pboard.pasteboardItems! {
- let fileURL = item.string(forType: .fileURL)
- if (fileURL == nil) {
- continue
- }
- let path = URL.init(string: fileURL!)
- if (path == nil) {
- continue
- }
- let filePath: String = path!.path
- files.append(filePath)
- }
- if let successBlock = self.dragSuccessBlock {
- successBlock(files)
- }
- // if let fileURL = pboard.availableType(from: [NSPasteboard.PasteboardType.fileURL]) {
- // let fileNames: String = pboard.propertyList(forType: .fileURL) as! String
- // let path = URL.init(string: fileNames)
- // let filePath: String = path!.path
- // if let successBlock = self.dragSuccessBlock {
- // successBlock([filePath])
- // }
- // }
- return true
- }
-
- //MARK: mouse Methods
-
- func addTrackingArea() {
- let trackingArea = NSTrackingArea(rect: self.imageView.bounds, options: [.mouseEnteredAndExited, .activeAlways], owner: self, userInfo: nil)
- self.imageView.addTrackingArea(trackingArea)
- }
- override func mouseEntered(with event: NSEvent) {
- super.mouseEntered(with: event)
- if let mouseActionCallBack = self.mouseActionCallBack {
- mouseActionCallBack(.mouseEnter)
- }
- }
-
- override func mouseExited(with event: NSEvent) {
- super.mouseExited(with: event)
- if let mouseActionCallBack = self.mouseActionCallBack {
- mouseActionCallBack(.mouseExit)
- }
- }
-
- override func mouseDown(with event: NSEvent) {
- super.mouseDown(with: event)
- var point = event.locationInWindow
- point = self.convert(point, to: self.imageView)
- if let mouseActionCallBack = self.mouseActionCallBack, self.imageView.bounds.contains(point) {
- mouseActionCallBack(.mouseDown)
- }
- }
-
- override func mouseUp(with event: NSEvent) {
- super.mouseUp(with: event)
- let point = event.locationInWindow
- let pointInView = self.convert(point, to: self.imageView)
- if let mouseActionCallBack = self.mouseActionCallBack, self.imageView.bounds.contains(pointInView) {
- mouseActionCallBack(.mouseUp)
- }
- }
-
- //MARK: User Actions
-
- @objc func buttonDidClick() -> Void {
- if let mouseActionCallBack = self.mouseActionCallBack {
- mouseActionCallBack(.mouseDown)
- }
- }
-
- //MARK: Get、Set
-
- var button: NSButton? {
- get {
- if _button == nil {
- _button = NSButton()
- _button?.isBordered = false
- _button?.title = ""
- _button?.target = self
- _button?.action = #selector(buttonDidClick)
- }
- return _button
- }
- }
- }
|