Skip to content

Commit

Permalink
cocoaui: refresh medialib album artwork on settings changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksiy-Yakovenko committed Dec 5, 2023
1 parent 14acfe0 commit 391bdda
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
4 changes: 0 additions & 4 deletions plugins/cocoaui/MediaLibrary/MediaLibraryItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
#include <deadbeef/deadbeef.h>
#include "../../medialib/medialib.h"

NS_ASSUME_NONNULL_BEGIN

@interface MediaLibraryItem : NSObject

+ (instancetype)new NS_UNAVAILABLE;
Expand All @@ -33,5 +31,3 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic,readonly) const ddb_medialib_item_t *medialibItem;

@end

NS_ASSUME_NONNULL_END
47 changes: 40 additions & 7 deletions plugins/cocoaui/MediaLibrary/MediaLibraryOutlineViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ @interface MediaLibraryOutlineViewController() <NSOutlineViewDataSource,MediaLib

@property (nonatomic) NSArray *topLevelItems;

@property (nonatomic) int listenerId;
@property (nonatomic) int artworkListenerId;
@property (nonatomic) int medialibListenerId;

@property (nonatomic) NSOutlineView *outlineView;
@property (nonatomic) NSSearchField *searchField;
Expand Down Expand Up @@ -101,7 +102,11 @@ - (instancetype)initWithOutlineView:(NSOutlineView *)outlineView searchField:(NS

self.medialibPlugin = (DB_mediasource_t *)deadbeef->plug_get_for_id ("medialib");
self.artworkPlugin = (ddb_artwork_plugin_t *)deadbeef->plug_get_for_id ("artwork2");
self.listenerId = self.medialibPlugin->add_listener (self.medialibSource, _medialib_listener, (__bridge void *)self);

if (self->_artworkPlugin != NULL) {
self.artworkPlugin->add_listener(_artwork_listener, (__bridge void *)self);
}
self.medialibListenerId = self.medialibPlugin->add_listener (self.medialibSource, _medialib_listener, (__bridge void *)self);

self.trackContextMenu = [[TrackContextMenu alloc] initWithView:self.outlineView];
self.outlineView.menu = self.trackContextMenu;
Expand Down Expand Up @@ -149,9 +154,9 @@ - (void)disconnect {
if (self.medialibPlugin == NULL) {
return;
}
if (self.listenerId != -1) {
self.medialibPlugin->remove_listener (self.medialibSource, self.listenerId);
self.listenerId = -1;
if (self.medialibListenerId != -1) {
self.medialibPlugin->remove_listener (self.medialibSource, self.medialibListenerId);
self.medialibListenerId = -1;
}
self.medialibPlugin = NULL;
self.artworkPlugin = NULL;
Expand All @@ -163,11 +168,23 @@ - (void)applicationWillQuit:(NSNotification *)notification {
}

- (void)dealloc {
if (self.artworkPlugin != NULL) {
self.artworkPlugin->remove_listener(_artwork_listener, (__bridge void *)self);
}
[self.mediaLibraryManager removeObserver:self forKeyPath:@"preset" context:kPresetCtx];
[self disconnect];
}

static void _medialib_listener (ddb_mediasource_event_type_t event, void *user_data) {
static void
_artwork_listener (ddb_artwork_listener_event_t event, void *user_data, int64_t p1, int64_t p2) {
MediaLibraryOutlineViewController *ctl = (__bridge MediaLibraryOutlineViewController *)user_data;
dispatch_async(dispatch_get_main_queue(), ^{
[ctl artworkEvent:event];
});
}

static void
_medialib_listener (ddb_mediasource_event_type_t event, void *user_data) {
MediaLibraryOutlineViewController *ctl = (__bridge MediaLibraryOutlineViewController *)user_data;
dispatch_async(dispatch_get_main_queue(), ^{
[ctl medialibEvent:event];
Expand Down Expand Up @@ -287,7 +304,7 @@ - (void)updateMedialibStatusForView:(NSTableCellView *)view {
case DDB_MEDIASOURCE_STATE_SAVING:
view.textField.stringValue = @"Saving...";
break;
}
}
}

- (void)updateMedialibStatus {
Expand All @@ -301,6 +318,22 @@ - (void)updateMedialibStatus {
[self updateMedialibStatusForView:view];
}

- (void)resetArtworkForItem:(MediaLibraryItem *)item {
item.coverImage = nil;
item.coverObtained = NO;
for (MediaLibraryItem *child in item.children) {
[self resetArtworkForItem:child];
}
}

- (void)artworkEvent:(ddb_artwork_listener_event_t)event {
if (event == DDB_ARTWORK_SETTINGS_DID_CHANGE) {
[self resetArtworkForItem:self.medialibRootItem];
[self.albumArtCache removeAllObjects];
[self.outlineView reloadData];
}
}

- (void)medialibEvent:(ddb_mediasource_event_type_t)event {
if (self.medialibPlugin == NULL) {
return;
Expand Down

0 comments on commit 391bdda

Please sign in to comment.