From 5555a95ee60a250fdd165d834169b6580541c761 Mon Sep 17 00:00:00 2001 From: Damiano Galassi Date: Sun, 30 Jun 2024 12:14:17 +0200 Subject: [PATCH] Implement NSPasteboardWriting and NSPasteboardReading in MP42Image --- MP42/MP42Image.h | 5 ++++- MP42/MP42Image.m | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/MP42/MP42Image.h b/MP42/MP42Image.h index bc64de3..3a053bf 100644 --- a/MP42/MP42Image.h +++ b/MP42/MP42Image.h @@ -7,11 +7,14 @@ // #import +#import 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 +@interface MP42Image : NSObject - (instancetype)initWithURL:(NSURL *)url type:(MP42TagArtworkType)type; - (instancetype)initWithImage:(NSImage *)image; diff --git a/MP42/MP42Image.m b/MP42/MP42Image.m index 88661b4..600f7e0 100644 --- a/MP42/MP42Image.m +++ b/MP42/MP42Image.m @@ -10,6 +10,8 @@ #import #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 *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 *)writableTypesForPasteboard:(nonnull NSPasteboard *)pasteboard +{ + return @[MP42PasteboardTypeArtwork, NSPasteboardTypeTIFF]; +} + ++ (nonnull NSArray *)readableTypesForPasteboard:(nonnull NSPasteboard *)pasteboard +{ + return @[MP42PasteboardTypeArtwork]; +} + @end