Skip to content

Commit

Permalink
drag and drop cover image override
Browse files Browse the repository at this point in the history
  • Loading branch information
tillt committed Mar 15, 2024
1 parent 5a22856 commit 767a278
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 15 deletions.
6 changes: 6 additions & 0 deletions PlayEm.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
BBDB1B3D2B640AA700A953F7 /* build_dmg.sh in Resources */ = {isa = PBXBuildFile; fileRef = BBDB1B3C2B640AA700A953F7 /* build_dmg.sh */; };
BBDB1B402B644AD500A953F7 /* MetalWaveView.m in Sources */ = {isa = PBXBuildFile; fileRef = BBDB1B3F2B644AD500A953F7 /* MetalWaveView.m */; };
BBDB1B452B69D18A00A953F7 /* WaveTile.m in Sources */ = {isa = PBXBuildFile; fileRef = BBDB1B442B69D18A00A953F7 /* WaveTile.m */; };
BBDBBA792BA3AF9F003A80C1 /* DragImageFileView.m in Sources */ = {isa = PBXBuildFile; fileRef = BBDBBA782BA3AF9F003A80C1 /* DragImageFileView.m */; };
BBDC988A250E81D0008B19D1 /* BrowserController.m in Sources */ = {isa = PBXBuildFile; fileRef = BBDC9889250E81D0008B19D1 /* BrowserController.m */; };
BBDC988C250E8347008B19D1 /* iTunesLibrary.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BBDC988B250E8347008B19D1 /* iTunesLibrary.framework */; };
BBDEBD3D2A99417A0085E6D8 /* WaveLayerDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = BBDEBD3C2A99417A0085E6D8 /* WaveLayerDelegate.m */; };
Expand Down Expand Up @@ -200,6 +201,8 @@
BBDB1B422B65CC0500A953F7 /* VisualPair.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = VisualPair.h; sourceTree = "<group>"; };
BBDB1B432B69D18A00A953F7 /* WaveTile.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WaveTile.h; sourceTree = "<group>"; };
BBDB1B442B69D18A00A953F7 /* WaveTile.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = WaveTile.m; sourceTree = "<group>"; };
BBDBBA772BA3AF9F003A80C1 /* DragImageFileView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DragImageFileView.h; sourceTree = "<group>"; };
BBDBBA782BA3AF9F003A80C1 /* DragImageFileView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DragImageFileView.m; sourceTree = "<group>"; };
BBDC9888250E81CF008B19D1 /* BrowserController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BrowserController.h; sourceTree = "<group>"; };
BBDC9889250E81D0008B19D1 /* BrowserController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BrowserController.m; sourceTree = "<group>"; };
BBDC988B250E8347008B19D1 /* iTunesLibrary.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = iTunesLibrary.framework; path = System/Library/Frameworks/iTunesLibrary.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -356,6 +359,8 @@
BB03622825FE57E800FA0A7E /* TotalWaveView.m */,
BB5A2C212B925CD300040209 /* TextViewWithPlaceholder.h */,
BB5A2C222B925CD300040209 /* TextViewWithPlaceholder.m */,
BBDBBA772BA3AF9F003A80C1 /* DragImageFileView.h */,
BBDBBA782BA3AF9F003A80C1 /* DragImageFileView.m */,
);
path = Views;
sourceTree = "<group>";
Expand Down Expand Up @@ -607,6 +612,7 @@
BB2CB58724827A0900D47258 /* AudioController.m in Sources */,
BB2CB5652481A92600D47258 /* WaveWindowController.m in Sources */,
BB8A100728FF49E400FC6013 /* Scroller.m in Sources */,
BBDBBA792BA3AF9F003A80C1 /* DragImageFileView.m in Sources */,
BBE2846D246765BD00BE9D7F /* Shaders.metal in Sources */,
BBC2AF4B2B8974C20077EB5E /* MediaMetaData+TagLib.m in Sources */,
BB8197702B7980C50025B860 /* ConcurrentAccessDictionary.m in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion PlayEm/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
<key>CFBundleShortVersionString</key>
<string>1.2</string>
<key>CFBundleVersion</key>
<string>4729</string>
<string>4759</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.music</string>
<key>LSMinimumSystemVersion</key>
Expand Down
6 changes: 5 additions & 1 deletion PlayEm/Metadata/MediaMetaData.m
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,8 @@ - (void)updateWithKey:(NSString*)key string:(NSString*)string
NSAssert(NO, @"should never get here");
}

// The MixWheel is not entirely deterministic, we are trying
// to catch all synonymous scales here additionally to the 24 sectors.
- (NSString*)correctedKeyNotation:(NSString*)key
{
NSDictionary* mixWheel = @{
Expand Down Expand Up @@ -874,7 +876,9 @@ - (NSString*)correctedKeyNotation:(NSString*)key
patchedKey = [NSString stringWithFormat:@"%@B", key];
}
return patchedKey;
} if (t == 'm') {
}

if (t == 'm') {
patchedKey = [NSString stringWithFormat:@"%@in", key];
} else if (t != 'n') {
patchedKey = [NSString stringWithFormat:@"%@maj", key];
Expand Down
22 changes: 22 additions & 0 deletions PlayEm/Views/DragImageFileView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// DragImageFileView.h
// PlayEm
//
// Created by Till Toenshoff on 14.03.24.
// Copyright © 2024 Till Toenshoff. All rights reserved.
//

#import <Cocoa/Cocoa.h>

NS_ASSUME_NONNULL_BEGIN

@protocol DragImageFileViewDelegate <NSObject>
- (BOOL)performDragOperationWithURL:(NSURL*)url;
@end

@interface DragImageFileView : NSImageView
@property (nonatomic, weak) IBOutlet id<DragImageFileViewDelegate> delegate;

@end

NS_ASSUME_NONNULL_END
56 changes: 56 additions & 0 deletions PlayEm/Views/DragImageFileView.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// DragImageFileView.m
// PlayEm
//
// Created by Till Toenshoff on 14.03.24.
// Copyright © 2024 Till Toenshoff. All rights reserved.
//

#import "DragImageFileView.h"

@implementation DragImageFileView

- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];

// Drawing code here.
}

#pragma mark - Drag & Drop

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
NSPasteboard* pboard = [sender draggingPasteboard];
NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];

if ( [[pboard types] containsObject:NSPasteboardTypeFileURL] ) {
if (sourceDragMask & NSDragOperationGeneric) {
return NSDragOperationGeneric;
} else if (sourceDragMask & NSDragOperationLink) {
return NSDragOperationLink;
} else if (sourceDragMask & NSDragOperationCopy) {
return NSDragOperationCopy;
}
}
return NSDragOperationNone;
}

- (BOOL)prepareForDragOperation:(id<NSDraggingInfo>)sender
{
return YES;
}

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
NSPasteboard* pboard = [sender draggingPasteboard];

if (pboard.pasteboardItems.count <= 1) {
NSURL* url = [NSURL URLFromPasteboard:pboard];
if (url) {
return [_delegate performDragOperationWithURL:url];
}
}
return NO;
}

@end
4 changes: 3 additions & 1 deletion PlayEm/Window & ViewControllers/InfoPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#import <Cocoa/Cocoa.h>

#import "DragImageFileView.h"

NS_ASSUME_NONNULL_BEGIN

@class MediaMetaData;
Expand All @@ -20,7 +22,7 @@ NS_ASSUME_NONNULL_BEGIN
- (void)finalizeMetaUpdates;
@end

@interface InfoPanelController : NSViewController <NSTextFieldDelegate, NSTextViewDelegate, NSTabViewDelegate, NSComboBoxDataSource, NSComboBoxDelegate>
@interface InfoPanelController : NSViewController <NSTextFieldDelegate, NSTextViewDelegate, NSTabViewDelegate, NSComboBoxDataSource, NSComboBoxDelegate, DragImageFileViewDelegate>
@property (weak, nonatomic) id<InfoPanelControllerDelegate> delegate;
@property (assign, nonatomic) BOOL processCurrentSong;

Expand Down
57 changes: 47 additions & 10 deletions PlayEm/Window & ViewControllers/InfoPanel.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import "InfoPanel.h"
#import "MediaMetaData.h"
#import "TextViewWithPlaceholder.h"
#import "DragImageFileView.h"

typedef enum : NSUInteger {
InfoControlTypeText,
Expand All @@ -29,7 +30,7 @@ @interface InfoPanelController ()
@property (strong, nonatomic) NSProgressIndicator* progress;

@property (strong, nonatomic) NSImageView* smallCoverView;
@property (strong, nonatomic) NSImageView* largeCoverView;
@property (strong, nonatomic) DragImageFileView* largeCoverView;

@property (strong, nonatomic) TextViewWithPlaceholder* lyricsTextView;

Expand Down Expand Up @@ -332,8 +333,9 @@ - (void)loadDetailsWithView:(NSView*)view
- (void)loadArtworkWithView:(NSView*)view
{
const CGFloat imageWidth = 400.0;

_largeCoverView = [NSImageView imageViewWithImage:[NSImage imageNamed:@"UnknownSong"]];

_largeCoverView = [DragImageFileView new];
_largeCoverView.image = [NSImage imageNamed:@"UnknownSong"];
_largeCoverView.alignment = NSViewHeightSizable | NSViewWidthSizable | NSViewMinYMargin | NSViewMaxYMargin;
_largeCoverView.imageScaling = NSImageScaleProportionallyUpOrDown;
_largeCoverView.frame = CGRectMake((self.view.bounds.size.width - (imageWidth + 20.0)) / 2.0f,
Expand All @@ -345,8 +347,13 @@ - (void)loadArtworkWithView:(NSView*)view
_largeCoverView.layer.borderWidth = 1.0f;
_largeCoverView.layer.cornerRadius = 7.0f;
_largeCoverView.layer.masksToBounds = YES;

[view addSubview:_largeCoverView];

_largeCoverView.delegate = self;

NSArray *dragTypes = [NSArray arrayWithObjects:NSCreateFileContentsPboardType(@"jpeg"), NSCreateFileContentsPboardType(@"jpg"), NSCreateFileContentsPboardType(@"png"), nil];
[_largeCoverView registerForDraggedTypes:dragTypes];
_largeCoverView.allowsCutCopyPaste = YES;
}

- (void)loadLyricsWithView:(NSView*)view
Expand Down Expand Up @@ -415,7 +422,7 @@ - (void)loadView

CGFloat fontSize = 24.0f;
CGFloat x = imageWidth + 40.0;
CGFloat fieldWidth = view.frame.size.width - (imageWidth + 100.0);
CGFloat fieldWidth = view.frame.size.width - (imageWidth + 40.0);
CGFloat y = view.frame.size.height - 30.0;

NSTextField* textField = [NSTextField textFieldWithString:@""];
Expand Down Expand Up @@ -731,16 +738,28 @@ - (void)okPressed:(id)sender
NSMutableDictionary* patchedMetas = [NSMutableDictionary dictionary];

for (NSString* key in [self.mutatedKeys allKeys]) {
NSString* stringValue = [self.deltaMeta stringForKey:key];
NSLog(@"applying the meta change for %@ towards \"%@\"", key, stringValue);

NSString* stringValue = nil;
NSData* dataValue = nil;
if ([key isEqualToString:@"artwork"]) {
dataValue = self.deltaMeta.artwork;
NSLog(@"applying the meta image change");
} else {
NSString* stringValue = [self.deltaMeta stringForKey:key];
NSLog(@"applying the meta change for %@ towards \"%@\"", key, stringValue);
}
for (MediaMetaData* meta in self.metas) {
MediaMetaData* patchedMeta = [patchedMetas objectForKey:meta.location];
if (patchedMeta == nil) {
patchedMeta = [meta copy];
}
[patchedMeta updateWithKey:key string:stringValue];
patchedMetas[meta.location] = patchedMeta;
if (stringValue != nil) {
[patchedMeta updateWithKey:key string:stringValue];
patchedMetas[meta.location] = patchedMeta;
}
if (dataValue != nil) {
patchedMeta.artwork = dataValue;
patchedMetas[meta.location] = patchedMeta;
}
}
}

Expand Down Expand Up @@ -854,4 +873,22 @@ - (nullable id)comboBox:(NSComboBox *)comboBox objectValueForItemAtIndex:(NSInte
return [_delegate knownGenres][index];
}

#pragma mark - DragImageFileViewDelegate

- (BOOL)performDragOperationWithURL:(NSURL*)url
{
NSData* data = [NSData dataWithContentsOfURL:url];
if (data == nil) {
return NO;
}
_deltaMeta.artwork = data;
_mutatedKeys[@"artwork"] = @YES;

NSImage* image = [_deltaMeta imageFromArtwork];
self.largeCoverView.image = image;
self.smallCoverView.image = image;

return YES;
}

@end
4 changes: 2 additions & 2 deletions PlayEm/Window & ViewControllers/WaveWindowController.m
Original file line number Diff line number Diff line change
Expand Up @@ -968,8 +968,8 @@ - (void)windowDidLoad
CGRect contentRect = [self.window contentRectForFrameRect:rect];
_windowBarHeight = self.window.frame.size.height - contentRect.size.height;

[self.window registerForDraggedTypes:[NSArray arrayWithObjects: NSPasteboardTypeFileURL, NSPasteboardTypeSound, nil]];
self.window.delegate = self;
//[self.window registerForDraggedTypes:[NSArray arrayWithObjects: NSPasteboardTypeFileURL, NSPasteboardTypeSound, nil]];
//self.window.delegate = self;

_playlist = [[PlaylistController alloc] initWithPlaylistTable:_playlistTable delegate:self];

Expand Down

0 comments on commit 767a278

Please sign in to comment.