Commit 0b2f47e8 authored by David Fuhrmann's avatar David Fuhrmann

macosx: popup panel: switch from delegate to completion block

Also fixes behaviour for eq profile list
parent 2609901e
......@@ -38,7 +38,6 @@
@interface VLCAudioEffects ()
{
BOOL b_genericAudioProfileInInteraction;
NSInteger i_old_profile_index;
}
- (void)resetProfileSelector;
......@@ -420,10 +419,31 @@
[_popupPanel setOKButtonLabel:_NS("Remove")];
[_popupPanel setCancelButtonLabel:_NS("Cancel")];
[_popupPanel setPopupButtonContent:[[NSUserDefaults standardUserDefaults] objectForKey:@"AudioEffectProfileNames"]];
[_popupPanel setTarget:self];
b_genericAudioProfileInInteraction = YES;
[_popupPanel runModalForWindow:self.window];
__weak typeof(self) _self = self;
[_popupPanel runModalForWindow:self.window completionHandler:^(NSInteger returnCode, NSInteger selectedIndex) {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (returnCode != NSOKButton) {
[_profilePopup selectItemAtIndex:[defaults integerForKey:@"AudioEffectSelectedProfile"]];
return;
}
/* remove selected profile from settings */
NSMutableArray *workArray = [[NSMutableArray alloc] initWithArray:[defaults objectForKey:@"AudioEffectProfiles"]];
[workArray removeObjectAtIndex:selectedIndex];
[defaults setObject:[NSArray arrayWithArray:workArray] forKey:@"AudioEffectProfiles"];
workArray = [[NSMutableArray alloc] initWithArray:[defaults objectForKey:@"AudioEffectProfileNames"]];
[workArray removeObjectAtIndex:selectedIndex];
[defaults setObject:[NSArray arrayWithArray:workArray] forKey:@"AudioEffectProfileNames"];
if (i_old_profile_index >= selectedIndex)
[defaults setInteger:i_old_profile_index - 1 forKey:@"AudioEffectSelectedProfile"];
/* save defaults */
[defaults synchronize];
[_self resetProfileSelector];
}];
}
#pragma mark -
......@@ -684,64 +704,37 @@ static bool GetEqualizerStatus(intf_thread_t *p_custom_intf,
- (IBAction)deletePresetAction:(id)sender
{
VLCPopupPanelController *panel = [[VLCPopupPanelController alloc] init];
[panel setTitle:_NS("Remove a preset")];
[panel setSubTitle:_NS("Select the preset you would like to remove:")];
[panel setOKButtonLabel:_NS("Remove")];
[panel setCancelButtonLabel:_NS("Cancel")];
[panel setPopupButtonContent:[[NSUserDefaults standardUserDefaults] objectForKey:@"EQTitles"]];
[panel setTarget:self];
b_genericAudioProfileInInteraction = NO;
[panel runModalForWindow:self.window];
}
- (void)panel:(VLCPopupPanelController *)panel returnValue:(NSUInteger)value item:(NSUInteger)item
{
if (!b_genericAudioProfileInInteraction) {
if (value == NSOKButton) {
/* remove requested profile from the arrays */
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *workArray = [[NSMutableArray alloc] initWithArray:[defaults objectForKey:@"EQValues"]];
[workArray removeObjectAtIndex:item];
[defaults setObject:[NSArray arrayWithArray:workArray] forKey:@"EQValues"];
workArray = [[NSMutableArray alloc] initWithArray:[defaults objectForKey:@"EQTitles"]];
[workArray removeObjectAtIndex:item];
[defaults setObject:[NSArray arrayWithArray:workArray] forKey:@"EQTitles"];
workArray = [[NSMutableArray alloc] initWithArray:[defaults objectForKey:@"EQPreampValues"]];
[workArray removeObjectAtIndex:item];
[defaults setObject:[NSArray arrayWithArray:workArray] forKey:@"EQPreampValues"];
workArray = [[NSMutableArray alloc] initWithArray:[defaults objectForKey:@"EQNames"]];
[workArray removeObjectAtIndex:item];
[defaults setObject:[NSArray arrayWithArray:workArray] forKey:@"EQNames"];
[defaults synchronize];
}
[_popupPanel setTitle:_NS("Remove a preset")];
[_popupPanel setSubTitle:_NS("Select the preset you would like to remove:")];
[_popupPanel setOKButtonLabel:_NS("Remove")];
[_popupPanel setCancelButtonLabel:_NS("Cancel")];
[_popupPanel setPopupButtonContent:[[NSUserDefaults standardUserDefaults] objectForKey:@"EQTitles"]];
/* update UI */
[self updatePresetSelector];
} else {
__weak typeof(self) _self = self;
[_popupPanel runModalForWindow:self.window completionHandler:^(NSInteger returnCode, NSInteger selectedIndex) {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (value != NSOKButton) {
[_profilePopup selectItemAtIndex:[defaults integerForKey:@"AudioEffectSelectedProfile"]];
if (returnCode != NSOKButton)
return;
}
/* remove selected profile from settings */
NSMutableArray *workArray = [[NSMutableArray alloc] initWithArray:[defaults objectForKey:@"AudioEffectProfiles"]];
[workArray removeObjectAtIndex:item];
[defaults setObject:[NSArray arrayWithArray:workArray] forKey:@"AudioEffectProfiles"];
workArray = [[NSMutableArray alloc] initWithArray:[defaults objectForKey:@"AudioEffectProfileNames"]];
[workArray removeObjectAtIndex:item];
[defaults setObject:[NSArray arrayWithArray:workArray] forKey:@"AudioEffectProfileNames"];
if (i_old_profile_index >= item)
[defaults setInteger:i_old_profile_index - 1 forKey:@"AudioEffectSelectedProfile"];
/* save defaults */
/* remove requested profile from the arrays */
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *workArray = [[NSMutableArray alloc] initWithArray:[defaults objectForKey:@"EQValues"]];
[workArray removeObjectAtIndex:selectedIndex];
[defaults setObject:[NSArray arrayWithArray:workArray] forKey:@"EQValues"];
workArray = [[NSMutableArray alloc] initWithArray:[defaults objectForKey:@"EQTitles"]];
[workArray removeObjectAtIndex:selectedIndex];
[defaults setObject:[NSArray arrayWithArray:workArray] forKey:@"EQTitles"];
workArray = [[NSMutableArray alloc] initWithArray:[defaults objectForKey:@"EQPreampValues"]];
[workArray removeObjectAtIndex:selectedIndex];
[defaults setObject:[NSArray arrayWithArray:workArray] forKey:@"EQPreampValues"];
workArray = [[NSMutableArray alloc] initWithArray:[defaults objectForKey:@"EQNames"]];
[workArray removeObjectAtIndex:selectedIndex];
[defaults setObject:[NSArray arrayWithArray:workArray] forKey:@"EQNames"];
[defaults synchronize];
[self resetProfileSelector];
}
/* update UI */
[_self updatePresetSelector];
}];
}
#pragma mark -
......
......@@ -166,6 +166,4 @@
- (IBAction)streamAnnouncementToggle:(id)sender;
- (IBAction)sdpFileLocationSelector:(id)sender;
- (void)panel:(VLCPopupPanelController *)panel returnValue:(NSUInteger)value item:(NSUInteger)item;
@end
......@@ -326,7 +326,6 @@
[self resetCustomizationSheetBasedOnProfile:[self.profileValueList objectAtIndex:index]];
}
- (IBAction)deleteProfileAction:(id)sender
{
/* show panel */
......@@ -335,9 +334,28 @@
[_popupPanel setOKButtonLabel:_NS("Remove")];
[_popupPanel setCancelButtonLabel:_NS("Cancel")];
[_popupPanel setPopupButtonContent:self.profileNames];
[_popupPanel setTarget:self];
[_popupPanel runModalForWindow:self.window];
__weak typeof(self) _self = self;
[_popupPanel runModalForWindow:self.window completionHandler:^(NSInteger returnCode, NSInteger selectedIndex) {
if (returnCode != NSOKButton)
return;
/* remove requested profile from the arrays */
NSMutableArray * workArray = [[NSMutableArray alloc] initWithArray:_self.profileNames];
[workArray removeObjectAtIndex:selectedIndex];
[_self setProfileNames:[[NSArray alloc] initWithArray:workArray]];
workArray = [[NSMutableArray alloc] initWithArray:_self.profileValueList];
[workArray removeObjectAtIndex:selectedIndex];
[_self setProfileValueList:[[NSArray alloc] initWithArray:workArray]];
/* update UI */
[_self recreateProfilePopup];
/* update internals */
[_self switchProfile:_self];
[_self storeProfilesOnDisk];
}];
}
- (IBAction)iWantAFile:(id)sender
......@@ -628,26 +646,6 @@
return NO;
}
- (void)panel:(VLCPopupPanelController *)panel returnValue:(NSUInteger)value item:(NSUInteger)item
{
if (value == NSOKButton) {
/* remove requested profile from the arrays */
NSMutableArray * workArray = [[NSMutableArray alloc] initWithArray:self.profileNames];
[workArray removeObjectAtIndex:item];
[self setProfileNames:[[NSArray alloc] initWithArray:workArray]];
workArray = [[NSMutableArray alloc] initWithArray:self.profileValueList];
[workArray removeObjectAtIndex:item];
[self setProfileValueList:[[NSArray alloc] initWithArray:workArray]];
/* update UI */
[self recreateProfilePopup];
/* update internals */
[self switchProfile:self];
[self storeProfilesOnDisk];
}
}
# pragma mark -
# pragma mark Private Functionality
......
......@@ -68,16 +68,21 @@ typedef void(^TextfieldPanelCompletionBlock)(NSInteger returnCode, NSString *res
@property (readwrite, assign) NSString *OKButtonLabel;
@property (readwrite, assign) NSString *CancelButtonLabel;
@property (readwrite, assign) NSArray *popupButtonContent;
@property (readwrite, assign) id target;
@property (readonly) NSUInteger currentItem;
- (void)runModalForWindow:(NSWindow *)window;
/**
* Completion handler for popup panel
* \param returnCode Result from panel. Can be NSOKButton or NSCancelButton.
* \param selectedIndex Selected index of the popup in panel.
*/
typedef void(^PopupPanelCompletionBlock)(NSInteger returnCode, NSInteger selectedIndex);
- (IBAction)windowElementAction:(id)sender;
/**
* Shows the panel as a modal dialog with window as its owner.
* \param window Parent window for the dialog.
* \param handler Completion block.
*/
- (void)runModalForWindow:(NSWindow *)window completionHandler:(PopupPanelCompletionBlock)handler;
@end
- (IBAction)windowElementAction:(id)sender;
@protocol VLCPopupPanelController <NSObject>
@optional
- (void)panel:(VLCPopupPanelController *)panel returnValue:(NSUInteger)value item:(NSUInteger)item;
@end
......@@ -66,6 +66,12 @@
@end
@interface VLCPopupPanelController()
{
PopupPanelCompletionBlock _completionBlock;
}
@end
@implementation VLCPopupPanelController
- (id)init
......@@ -80,17 +86,11 @@
[self.window orderOut:sender];
[NSApp endSheet: self.window];
if (self.target) {
if ([self.target respondsToSelector:@selector(panel:returnValue:item:)]) {
if (sender == _cancelButton)
[self.target panel:self returnValue:NSCancelButton item:0];
else
[self.target panel:self returnValue:NSOKButton item:self.currentItem];
}
}
if (_completionBlock)
_completionBlock(sender == _okButton ? NSOKButton : NSCancelButton, [_popupButton indexOfSelectedItem]);
}
- (void)runModalForWindow:(NSWindow *)window
- (void)runModalForWindow:(NSWindow *)window completionHandler:(PopupPanelCompletionBlock)handler;
{
[self window];
......@@ -102,12 +102,9 @@
for (NSString *value in self.popupButtonContent)
[[_popupButton menu] addItemWithTitle:value action:nil keyEquivalent:@""];
[NSApp beginSheet:self.window modalForWindow:window modalDelegate:self didEndSelector:NULL contextInfo:nil];
}
_completionBlock = [handler copy];
- (NSUInteger)currentItem
{
return [_popupButton indexOfSelectedItem];
[NSApp beginSheet:self.window modalForWindow:window modalDelegate:self didEndSelector:NULL contextInfo:nil];
}
@end
......@@ -732,39 +732,37 @@
[_popupPanel setOKButtonLabel:_NS("Remove")];
[_popupPanel setCancelButtonLabel:_NS("Cancel")];
[_popupPanel setPopupButtonContent:[[NSUserDefaults standardUserDefaults] objectForKey:@"VideoEffectProfileNames"]];
[_popupPanel setTarget:self];
[_popupPanel runModalForWindow:self.window];
}
__weak typeof(self) _self = self;
[_popupPanel runModalForWindow:self.window completionHandler:^(NSInteger returnCode, NSInteger selectedIndex) {
- (void)panel:(VLCPopupPanelController *)panel returnValue:(NSUInteger)value item:(NSUInteger)item
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (value != NSOKButton) {
[_profilePopup selectItemAtIndex:[defaults integerForKey:@"VideoEffectSelectedProfile"]];
return;
}
if (returnCode != NSOKButton) {
[_profilePopup selectItemAtIndex:[defaults integerForKey:@"VideoEffectSelectedProfile"]];
return;
}
/* remove selected profile from settings */
NSMutableArray *workArray = [[NSMutableArray alloc] initWithArray: [defaults objectForKey:@"VideoEffectProfiles"]];
[workArray removeObjectAtIndex:item];
[defaults setObject:[NSArray arrayWithArray:workArray] forKey:@"VideoEffectProfiles"];
/* remove selected profile from settings */
NSMutableArray *workArray = [[NSMutableArray alloc] initWithArray: [defaults objectForKey:@"VideoEffectProfiles"]];
[workArray removeObjectAtIndex:selectedIndex];
[defaults setObject:[NSArray arrayWithArray:workArray] forKey:@"VideoEffectProfiles"];
workArray = [[NSMutableArray alloc] initWithArray: [defaults objectForKey:@"VideoEffectProfileNames"]];
[workArray removeObjectAtIndex:item];
[defaults setObject:[NSArray arrayWithArray:workArray] forKey:@"VideoEffectProfileNames"];
workArray = [[NSMutableArray alloc] initWithArray: [defaults objectForKey:@"VideoEffectProfileNames"]];
[workArray removeObjectAtIndex:selectedIndex];
[defaults setObject:[NSArray arrayWithArray:workArray] forKey:@"VideoEffectProfileNames"];
if (i_old_profile_index >= item)
[defaults setInteger:i_old_profile_index - 1 forKey:@"VideoEffectSelectedProfile"];
if (i_old_profile_index >= selectedIndex)
[defaults setInteger:i_old_profile_index - 1 forKey:@"VideoEffectSelectedProfile"];
/* save defaults */
[defaults synchronize];
/* save defaults */
[defaults synchronize];
/* do not save deleted profile */
i_old_profile_index = -1;
/* refresh UI */
[self resetProfileSelector];
/* do not save deleted profile */
i_old_profile_index = -1;
/* refresh UI */
[_self resetProfileSelector];
}];
}
#pragma mark -
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment