Move some files to Classes
This commit is contained in:
3 files changed
+8
-8
No files matched your search
@@ -0,0 +1,133 @@
|
||||
//
|
||||
// CollectionView.swift
|
||||
// Subler
|
||||
//
|
||||
// Created by Damiano Galassi on 29/06/24.
|
||||
//
|
||||
|
||||
import Cocoa
|
||||
|
||||
@objc protocol CollectionViewDelegate: NSCollectionViewDelegate {
|
||||
@MainActor @objc optional func collectionViewDelete(in collectionView: NSCollectionView)
|
||||
@MainActor @objc optional func collectionViewCopy(in collectionView: NSCollectionView)
|
||||
@MainActor @objc optional func collectionViewCut(in collectionView: NSCollectionView)
|
||||
@MainActor @objc optional func collectionViewPaste(to collectionView: NSCollectionView)
|
||||
}
|
||||
|
||||
class CollectionView: NSCollectionView, NSMenuItemValidation {
|
||||
|
||||
var pasteboardTypes: [NSPasteboard.PasteboardType]
|
||||
var pasteboardHasSupportedType: Bool {
|
||||
get {
|
||||
// Has the pasteboard got a type we support?
|
||||
let pb = NSPasteboard.general
|
||||
let bestType = pb.availableType(from: pasteboardTypes)
|
||||
return bestType != nil
|
||||
}
|
||||
}
|
||||
|
||||
private var expandedDelegate: CollectionViewDelegate? {
|
||||
get {
|
||||
return delegate as? CollectionViewDelegate
|
||||
}
|
||||
}
|
||||
|
||||
override init(frame frameRect: NSRect) {
|
||||
pasteboardTypes = Array()
|
||||
super.init(frame: frameRect)
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
pasteboardTypes = Array()
|
||||
super.init(coder: coder)
|
||||
}
|
||||
|
||||
private var isSelected: Bool {
|
||||
get {
|
||||
selectionIndexPaths.isEmpty == false
|
||||
}
|
||||
}
|
||||
|
||||
private var isSelectionEmpty: Bool {
|
||||
get {
|
||||
selectionIndexPaths.isEmpty == true
|
||||
}
|
||||
}
|
||||
|
||||
override func keyDown(with event: NSEvent) {
|
||||
guard let key = event.charactersIgnoringModifiers?.utf16.first else { super.keyDown(with: event); return }
|
||||
|
||||
if (key == NSDeleteCharacter || key == NSDeleteFunctionKey) && implements(selector: #selector(CollectionViewDelegate.collectionViewDelete(in:))) {
|
||||
if isSelectionEmpty {
|
||||
__NSBeep()
|
||||
} else {
|
||||
expandedDelegate?.collectionViewDelete!(in: self)
|
||||
}
|
||||
}
|
||||
else if key == 27 && isSelected {
|
||||
deselectAll(self)
|
||||
}
|
||||
else {
|
||||
super.keyDown(with: event)
|
||||
}
|
||||
}
|
||||
|
||||
private func implements(selector: Selector) -> Bool {
|
||||
if let implemented = expandedDelegate?.responds(to: selector),
|
||||
implemented == true {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@IBAction func delete(_ sender: Any?) {
|
||||
if isSelected && implements(selector: #selector(CollectionViewDelegate.collectionViewDelete(in:))) {
|
||||
expandedDelegate?.collectionViewDelete!(in: self)
|
||||
}
|
||||
}
|
||||
|
||||
@IBAction func copy(_ sender: Any?) {
|
||||
if isSelected && implements(selector: #selector(CollectionViewDelegate.collectionViewCopy(in:))) {
|
||||
expandedDelegate?.collectionViewCopy!(in: self)
|
||||
}
|
||||
}
|
||||
|
||||
@IBAction func cut(_ sender: Any?) {
|
||||
if isSelected && implements(selector: #selector(CollectionViewDelegate.collectionViewCut(in:))) {
|
||||
expandedDelegate?.collectionViewCut!(in: self)
|
||||
}
|
||||
}
|
||||
|
||||
@IBAction func paste(_ sender: Any?) {
|
||||
if implements(selector: #selector(CollectionViewDelegate.collectionViewPaste(to:))) {
|
||||
expandedDelegate?.collectionViewPaste!(to: self)
|
||||
}
|
||||
}
|
||||
|
||||
func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
|
||||
if let action = menuItem.action {
|
||||
switch action {
|
||||
case #selector(delete(_:)):
|
||||
if isSelectionEmpty || implements(selector: #selector(CollectionViewDelegate.collectionViewDelete(in:))) == false {
|
||||
return false
|
||||
}
|
||||
case #selector(copy(_:)):
|
||||
if isSelectionEmpty || implements(selector: #selector(CollectionViewDelegate.collectionViewCopy(in:))) == false {
|
||||
return false
|
||||
}
|
||||
case #selector(cut(_:)):
|
||||
if isSelectionEmpty || implements(selector: #selector(CollectionViewDelegate.collectionViewCut(in:))) == false {
|
||||
return false
|
||||
}
|
||||
case #selector(paste(_:)):
|
||||
if pasteboardHasSupportedType == false || implements(selector: #selector(CollectionViewDelegate.collectionViewPaste(to:))) == false {
|
||||
return false
|
||||
}
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
//
|
||||
// FilePromiseProvider.swift
|
||||
// Subler
|
||||
//
|
||||
// Created by Damiano Galassi on 28/06/24.
|
||||
//
|
||||
|
||||
import Cocoa
|
||||
import UniformTypeIdentifiers
|
||||
|
||||
class FilePromiseProvider: NSFilePromiseProvider {
|
||||
|
||||
struct UserInfoKeys {
|
||||
static let indexPathKey = "indexPath"
|
||||
static let extensionKey = "extension"
|
||||
static let urlKey = "url"
|
||||
}
|
||||
|
||||
/** Required:
|
||||
Return an array of UTI strings of data types the receiver can write to the pasteboard.
|
||||
By default, data for the first returned type is put onto the pasteboard immediately, with the remaining types being promised.
|
||||
To change the default behavior, implement -writingOptionsForType:pasteboard: and return
|
||||
NSPasteboardWritingPromised to lazily provided data for types, return no option to provide the data for that type immediately.
|
||||
|
||||
Use the pasteboard argument to provide different types based on the pasteboard name, if desired.
|
||||
Do not perform other pasteboard operations in the function implementation.
|
||||
*/
|
||||
override func writableTypes(for pasteboard: NSPasteboard) -> [NSPasteboard.PasteboardType] {
|
||||
var types = super.writableTypes(for: pasteboard)
|
||||
types.append(.artworkDragType) // Add our own internal drag type (row drag and drop reordering).
|
||||
var type: NSPasteboard.PasteboardType
|
||||
if #available(macOS 11.0, *) {
|
||||
type = NSPasteboard.PasteboardType(UTType.image.identifier)
|
||||
} else {
|
||||
type = (kUTTypeImage as NSPasteboard.PasteboardType)
|
||||
}
|
||||
types.append(type)
|
||||
return types
|
||||
}
|
||||
|
||||
/** Required:
|
||||
Return the appropriate property list object for the provided type.
|
||||
This will commonly be the NSData for that data type. However, if this function returns either a string, or any other property-list type,
|
||||
the pasteboard will automatically convert these items to the correct NSData format required for the pasteboard.
|
||||
*/
|
||||
override func pasteboardPropertyList(forType type: NSPasteboard.PasteboardType) -> Any? {
|
||||
guard let userInfoDict = userInfo as? [String: Any] else { return nil }
|
||||
switch type {
|
||||
case .fileURL:
|
||||
// Incoming type is "public.file-url", return (from our userInfo) the item's URL.
|
||||
if let url = userInfoDict[FilePromiseProvider.UserInfoKeys.urlKey] as? NSURL {
|
||||
return url.pasteboardPropertyList(forType: type)
|
||||
}
|
||||
case .artworkDragType:
|
||||
// Incoming type is "com.mycompany.mydragdrop", return (from our userInfo) the item's indexPath.
|
||||
let indexPathData = userInfoDict[FilePromiseProvider.UserInfoKeys.indexPathKey]
|
||||
return indexPathData
|
||||
default:
|
||||
break
|
||||
}
|
||||
return super.pasteboardPropertyList(forType: type)
|
||||
}
|
||||
|
||||
/** Optional:
|
||||
Returns options for writing data of a type to a pasteboard.
|
||||
Use the pasteboard argument to provide different options based on the pasteboard name, if desired.
|
||||
Do not perform other pasteboard operations in the function implementation.
|
||||
*/
|
||||
public override func writingOptions(forType type: NSPasteboard.PasteboardType, pasteboard: NSPasteboard)
|
||||
-> NSPasteboard.WritingOptions {
|
||||
return super.writingOptions(forType: type, pasteboard: pasteboard)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user