Implement NSPasteboardWriting and NSPasteboardReading in MP42Image

This commit is contained in:
Damiano Galassi committed 2024-06-30 12:14:17 +02:00
1 parent 23ac1a3256
commit 5555a95ee6
2 files changed
+46 -1

No files matched your search

+4 -1
View File
@@ -7,11 +7,14 @@
//
#import <Foundation/Foundation.h>
#import <AppKit/NSPasteboard.h>
NS_ASSUME_NONNULL_BEGIN
@class NSImage;
extern NSPasteboardType const MP42PasteboardTypeArtwork;
typedef enum MP42TagArtworkType_e {
MP42_ART_UNDEFINED = 0,
MP42_ART_BMP = 1,
@@ -20,7 +23,7 @@ typedef enum MP42TagArtworkType_e {
MP42_ART_PNG = 4
} MP42TagArtworkType;
@interface MP42Image : NSObject <NSSecureCoding, NSCopying>
@interface MP42Image : NSObject <NSSecureCoding, NSCopying, NSPasteboardWriting, NSPasteboardReading>
- (instancetype)initWithURL:(NSURL *)url type:(MP42TagArtworkType)type;
- (instancetype)initWithImage:(NSImage *)image;
+42
View File
@@ -10,6 +10,8 @@
#import <Quartz/Quartz.h>
#import "MP42Utilities.h"
NSPasteboardType const MP42PasteboardTypeArtwork = @"org.subler.artworkdata";
@interface MP42Image ()
@property (atomic, readonly) NSString *uuid;
@@ -167,4 +169,44 @@ MP42_OBJC_DIRECT_MEMBERS
return self;
}
- (instancetype)initWithPasteboardPropertyList:(id)propertyList
ofType:(NSPasteboardType)type
{
if ([type isEqualToString:MP42PasteboardTypeArtwork]) {
MP42Image *unarchivedImage = [NSKeyedUnarchiver unarchiveObjectWithData:propertyList];
if ([unarchivedImage isKindOfClass:[MP42Image class]]) {
return unarchivedImage;
}
}
return nil;
}
- (nullable id)pasteboardPropertyListForType:(nonnull NSPasteboardType)type
{
if ([type isEqualToString:NSPasteboardTypeTIFF]) {
NSArray<NSImageRep *> *representations = self.image.representations;
if (representations) {
return [NSBitmapImageRep representationOfImageRepsInArray:representations
usingType:NSBitmapImageFileTypeTIFF
properties:@{}];
}
}
else if ([type isEqualToString:MP42PasteboardTypeArtwork]) {
return [NSKeyedArchiver archivedDataWithRootObject:self];
}
return nil;
}
- (nonnull NSArray<NSPasteboardType> *)writableTypesForPasteboard:(nonnull NSPasteboard *)pasteboard
{
return @[MP42PasteboardTypeArtwork, NSPasteboardTypeTIFF];
}
+ (nonnull NSArray<NSPasteboardType> *)readableTypesForPasteboard:(nonnull NSPasteboard *)pasteboard
{
return @[MP42PasteboardTypeArtwork];
}
@end