Skip to content

Commit

Permalink
cocoaui: use scriptableModel to observe selected preset instead of me…
Browse files Browse the repository at this point in the history
…dialib manager KVO
  • Loading branch information
Oleksiy-Yakovenko committed Nov 19, 2023
1 parent a438c6c commit 2dd8d3e
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 64 deletions.
16 changes: 1 addition & 15 deletions plugins/cocoaui/MainWindowController.m
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,7 @@ - (void)windowDidLoad {
[_tfQueryContainer addSubview:self.tfQuerySelectViewController.view];
self.tfQuerySelectViewController.errorViewer = ScriptableErrorViewer.sharedInstance;
self.tfQuerySelectViewController.dataSource = self.mlQueriesDataSource;

if (self.mlQueriesDataSource.scriptable != NULL) {
NSString *preset = self.mediaLibraryManager.preset;
scriptableItem_t *currentPreset = scriptableItemSubItemForName(self.mlQueriesDataSource.scriptable, preset.UTF8String);
if (currentPreset != NULL) {
[self.tfQuerySelectViewController selectItem:currentPreset];
}
}
self.tfQuerySelectViewController.scriptableModel = self.mediaLibraryManager.model;

id<WidgetProtocol> rootWidget = DesignModeState.sharedInstance.rootWidget;
NSView *view = rootWidget.view;
Expand Down Expand Up @@ -544,8 +537,6 @@ - (void)deletePlaylistDone:(DeletePlaylistConfirmationController *)controller {
#pragma mark - ScriptableSelectDelegate

- (void)scriptableSelectItemSelected:(scriptableItem_t *)item {
const char *name = scriptableItemPropertyValueForKey(item, "name");
self.mediaLibraryManager.preset = @(name);
}

#pragma mark - ScriptableItemDelegate
Expand All @@ -560,11 +551,6 @@ - (void)scriptableItemDidChange:(scriptableItem_t *)scriptable change:(Scriptabl
if (tfQueryRoot != NULL) {
scriptableItemSave(tfQueryRoot);
}

// refresh the tree
NSInteger index = self.tfQuerySelectViewController.indexOfSelectedItem;
scriptableItem_t *item = scriptableItemChildAtIndex(tfQueryRoot, (unsigned int)index);
[self scriptableSelectItemSelected:item];
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions plugins/cocoaui/MediaLibrary/MediaLibraryManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#import <Foundation/Foundation.h>
#include <deadbeef/deadbeef.h>
#include "scriptable/scriptable_model.h"
#include "medialib.h"

NS_ASSUME_NONNULL_BEGIN
Expand All @@ -16,8 +17,7 @@ NS_ASSUME_NONNULL_BEGIN

@property (nonatomic,readonly) DB_mediasource_t *medialibPlugin;
@property (nonatomic,readonly) ddb_mediasource_source_t *source;

@property (nullable, nonatomic) NSString *preset;
@property (nonatomic,readonly) scriptableModel_t *model;

@end

Expand Down
17 changes: 4 additions & 13 deletions plugins/cocoaui/MediaLibrary/MediaLibraryManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ @interface MediaLibraryManager()

@property (nonatomic,readwrite) DB_mediasource_t *medialibPlugin;
@property (nonatomic,readwrite) ddb_mediasource_source_t *source;
@property (nonatomic,readwrite) scriptableModel_t *model;

@end

Expand All @@ -33,29 +34,19 @@ - (instancetype)init
_source = self.medialibPlugin->create_source ("deadbeef");
self.medialibPlugin->refresh(_source);

self.model = scriptableModelInit(scriptableModelAlloc(), deadbeef, "medialib.preset");

return self;
}

- (void)dealloc
{
scriptableModelFree(_model);
if (_source) {
_medialibPlugin->free_source(_source);
_source = NULL;
}
_medialibPlugin = NULL;
}

- (void)setPreset:(NSString *)preset {
[self willChangeValueForKey:@"preset"];
deadbeef->conf_set_str("medialib.preset", preset.UTF8String);
deadbeef->sendmessage(DB_EV_CONFIGCHANGED, 0, 0, 0);
[self didChangeValueForKey:@"preset"];
}

- (NSString *)preset {
char buffer[100];
deadbeef->conf_get_str("medialib.preset", "", buffer, sizeof(buffer));
return @(buffer);
}

@end
27 changes: 20 additions & 7 deletions plugins/cocoaui/MediaLibrary/MediaLibraryOutlineViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
static void *kPresetCtx = &kPresetCtx;

@interface MediaLibraryOutlineViewController() <NSOutlineViewDataSource,MediaLibraryOutlineViewDelegate,TrackContextMenuDelegate,TrackPropertiesWindowControllerDelegate> {
int64_t _modelListenerId;
}

@property (nonatomic) MediaLibraryItem *medialibRootItem;
Expand All @@ -50,8 +51,6 @@ @interface MediaLibraryOutlineViewController() <NSOutlineViewDataSource,MediaLib

@property (nonatomic) NSMutableDictionary<NSString *,NSImage *> *albumArtCache;

@property (nonatomic) NSString *currentPreset;

@property (nonatomic) NSImage *folderImage;

@end
Expand All @@ -71,6 +70,16 @@ - (instancetype)init {
return [self initWithOutlineView:[NSOutlineView new] searchField:[NSSearchField new]];
}

static void
_model_listener (struct scriptableModel_t *model, void *user_data) {
MediaLibraryOutlineViewController *self = (__bridge MediaLibraryOutlineViewController *)user_data;
[self modelListener];
}

- (void)modelListener {
[self initializeTreeView];
}

- (instancetype)initWithOutlineView:(NSOutlineView *)outlineView searchField:(NSSearchField *)searchField {
self = [super init];
if (!self) {
Expand All @@ -79,9 +88,6 @@ - (instancetype)initWithOutlineView:(NSOutlineView *)outlineView searchField:(NS

[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(applicationWillQuit:) name:@"ApplicationWillQuit" object:nil];



self.currentPreset = self.mediaLibraryManager.preset;
[self.mediaLibraryManager addObserver:self forKeyPath:@"preset" options:0 context:kPresetCtx];

self.outlineView = outlineView;
Expand All @@ -103,6 +109,9 @@ - (instancetype)initWithOutlineView:(NSOutlineView *)outlineView searchField:(NS

[self initializeTreeView];

scriptableModel_t *model = self.mediaLibraryManager.model;
_modelListenerId = scriptableModelGetAPI(model)->add_listener (model, _model_listener, (__bridge void *)self);

[self.outlineView expandItem:self.medialibRootItem];

[self updateMedialibStatus];
Expand All @@ -125,7 +134,6 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
dispatch_async(dispatch_get_main_queue(), ^{
// NOTE: don't add a check for whether user changed to another preset.
// This would break a refresh if the current preset changes settings.
self.currentPreset = self.mediaLibraryManager.preset;
[self filterChanged];
});
} else {
Expand All @@ -134,6 +142,10 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
}

- (void)disconnect {
scriptableModel_t *model = self.mediaLibraryManager.model;
scriptableModelGetAPI(model)->remove_listener(model, _modelListenerId);
_modelListenerId = 0;

if (self.medialibPlugin == NULL) {
return;
}
Expand Down Expand Up @@ -170,7 +182,8 @@ - (void)initializeTreeView {

scriptableItem_t *tfQueryRoot = self.medialibPlugin->get_queries_scriptable(self.medialibSource);

NSString *presetName = self.mediaLibraryManager.preset;
scriptableModel_t *model = self.mediaLibraryManager.model;
NSString *presetName = @(scriptableModelGetAPI(model)->get_active_name(model));
scriptableItem_t *preset = NULL;
if (presetName != nil) {
preset = scriptableItemSubItemForName(tfQueryRoot, presetName.UTF8String);
Expand Down
2 changes: 2 additions & 0 deletions plugins/cocoaui/Scriptable/ScriptableSelectViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#import <Cocoa/Cocoa.h>
#include "scriptable/scriptable.h"
#include "scriptable/scriptable_model.h"
#import "ScriptableTableDataSource.h"
#import "ScriptableProtocols.h"

Expand All @@ -25,6 +26,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (weak) NSObject<ScriptableItemDelegate> *scriptableItemDelegate;
@property (weak) NSObject<ScriptableSelectDelegate> *scriptableSelectDelegate;
@property (weak) NSObject<ScriptableErrorViewer> *errorViewer;
@property scriptableModel_t *scriptableModel;

@property (nonatomic,readonly) NSInteger indexOfSelectedItem;

Expand Down
82 changes: 80 additions & 2 deletions plugins/cocoaui/Scriptable/ScriptableSelectViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
#import "ScriptableSelectViewController.h"
#import "ScriptableNodeEditorWindowController.h"

@interface ScriptableSelectViewController ()
@interface ScriptableSelectViewController () <ScriptableItemDelegate> {
scriptableModelAPI_t *_modelAPI;
int64_t _modelListenerId;
BOOL _updatingModel;
}

@property (weak) IBOutlet NSPopUpButton *nameList;
@property (weak) IBOutlet NSButton *browseButton;
Expand All @@ -19,20 +23,23 @@ @interface ScriptableSelectViewController ()

@implementation ScriptableSelectViewController

@synthesize scriptableModel = _scriptableModel;

- (void)setDataSource:(ScriptableTableDataSource *)dataSource {
_dataSource = dataSource;
[self reloadData];
}

- (IBAction)nameSelectedAction:(NSPopUpButton *)sender {
NSUInteger index = sender.indexOfSelectedItem;
[self updateModelFromCurrent];
[self.scriptableSelectDelegate scriptableSelectItemSelected:scriptableItemChildAtIndex(self.dataSource.scriptable, (unsigned int)index)];
}

- (void)initNodeEditorWindowController {
self.nodeEditorWindowController = [[ScriptableNodeEditorWindowController alloc] initWithWindowNibName:@"ScriptableNodeEditorWindow"];
self.nodeEditorWindowController.dataSource = self.dataSource;
self.nodeEditorWindowController.delegate = self.scriptableItemDelegate;
self.nodeEditorWindowController.delegate = self;
self.nodeEditorWindowController.errorViewer = self.errorViewer;
}

Expand Down Expand Up @@ -73,4 +80,75 @@ - (void)selectItem:(scriptableItem_t *)item {
}
}

- (void)updateCurrentFromModel {
if (_scriptableModel == NULL) {
return;
}
char *preset = _modelAPI->get_active_name (_scriptableModel);
scriptableItem_t *currentPreset = scriptableItemSubItemForName (self.dataSource.scriptable, preset);
if (currentPreset != NULL) {
[self selectItem:currentPreset];
}
free (preset);
}

- (void)updateModelFromCurrent {
if (_scriptableModel == NULL) {
return;
}

NSUInteger index = self.nameList.indexOfSelectedItem;
scriptableItem_t *item = scriptableItemChildAtIndex(self.dataSource.scriptable, (unsigned int)index);

const char *name = "";
if (item != NULL) {
name = scriptableItemPropertyValueForKey (item, "name");
}
_updatingModel = YES;
_modelAPI->set_active_name (_scriptableModel, name);
_updatingModel = NO;
}

static void
_model_listener (struct scriptableModel_t *model, void *user_data) {
ScriptableSelectViewController *self = (__bridge ScriptableSelectViewController *)user_data;
[self modelListener];
}

- (void)modelListener {
if (_updatingModel) {
return;
}

[self updateCurrentFromModel];
}

- (scriptableModel_t *)scriptableModel {
return _scriptableModel;
}

- (void)setScriptableModel:(scriptableModel_t *)scriptableModel {
if (_scriptableModel != NULL) {
_modelAPI->remove_listener (_scriptableModel, self->_modelListenerId);
_modelListenerId = 0;
_modelAPI = NULL;
}

_scriptableModel = scriptableModel;

if (scriptableModel != NULL) {
_modelAPI = scriptableModelGetAPI (scriptableModel);
_modelListenerId = _modelAPI->add_listener (scriptableModel, _model_listener, (__bridge void *)self);

[self updateCurrentFromModel];
}
}

#pragma mark - ScriptableItemDelegate

- (void)scriptableItemDidChange:(scriptableItem_t *)scriptable change:(ScriptableItemChange)change {
[self updateModelFromCurrent];
[self.scriptableItemDelegate scriptableItemDidChange:scriptable change:change];
}

@end
10 changes: 0 additions & 10 deletions plugins/gtkui/medialib/medialibmanager.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,3 @@ scriptableModel_t *
gtkui_medialib_get_model (void) {
return _model;
}

void
gtkui_medialib_preset_set (const char *preset) {
scriptableModelGetAPI (_model)->set_active_name (_model, preset);
}

char *
gtkui_medialib_preset_get (void) {
return scriptableModelGetAPI (_model)->get_active_name (_model);
}
6 changes: 0 additions & 6 deletions plugins/gtkui/medialib/medialibmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ gtkui_medialib_get_source (void);
void
gtkui_medialib_free (void);

void
gtkui_medialib_preset_set (const char *preset);

char *
gtkui_medialib_preset_get (void);

scriptableModel_t *
gtkui_medialib_get_model (void);

Expand Down
4 changes: 3 additions & 1 deletion plugins/gtkui/medialib/medialibwidget.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ _reload_content (w_medialib_viewer_t *mlv) {

scriptableItem_t *presets = plugin->get_queries_scriptable (mlv->source);
if (presets) {
char *curr_preset = gtkui_medialib_preset_get ();
scriptableModel_t *model = gtkui_medialib_get_model ();

char *curr_preset = scriptableModelGetAPI (model)->get_active_name (model);
if (curr_preset) {
preset = scriptableItemSubItemForName (presets, curr_preset);
}
Expand Down
20 changes: 12 additions & 8 deletions plugins/gtkui/scriptable/gtkScriptableSelectViewController.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ gtkScriptableSelectViewControllerSetDelegate (

static void
_update_current_from_model (gtkScriptableSelectViewController_t *self) {
if (self->model == NULL) {
return;
}
char *preset = self->model_api->get_active_name (self->model);
scriptableItem_t *currentPreset = scriptableItemSubItemForName (self->scriptable, preset);
if (currentPreset != NULL) {
Expand Down Expand Up @@ -197,15 +200,16 @@ gtkScriptableSelectViewControllerIndexOfSelectedItem (gtkScriptableSelectViewCon

static void
_apply_active_selection_to_model (scriptableItem_t *item, gtkScriptableSelectViewController_t *self) {
if (self->model != NULL) {
const char *name = "";
if (item != NULL) {
name = scriptableItemPropertyValueForKey (item, "name");
}
self->updating_model = 1;
self->model_api->set_active_name (self->model, name);
self->updating_model = 0;
if (self->model == NULL) {
return;
}
const char *name = "";
if (item != NULL) {
name = scriptableItemPropertyValueForKey (item, "name");
}
self->updating_model = 1;
self->model_api->set_active_name (self->model, name);
self->updating_model = 0;
}

static scriptableItem_t *
Expand Down

0 comments on commit 2dd8d3e

Please sign in to comment.