Commit 34e88dc7 authored by Felix Paul Kühne's avatar Felix Paul Kühne

macosx/coredialogs: refactor to always run on the main thread regardless of the caller

parent 0cb29ee3
...@@ -31,22 +31,15 @@ ...@@ -31,22 +31,15 @@
@interface VLCCoreDialogProvider () @interface VLCCoreDialogProvider ()
- (void)displayLoginDialogWithID:(vlc_dialog_id *)p_id - (void)displayError:(NSArray *)dialogData;
title:(const char *)psz_title
description:(const char *)psz_text - (void)displayLoginDialog:(NSArray *)dialogData;
defaultUserName:(const char *)psz_default_username
askToStore:(bool )b_ask_store; - (void)displayQuestion:(NSArray *)dialogData;
- (void)displayProgressDialogWithID:(vlc_dialog_id *)p_id - (void)displayProgressDialog:(NSArray *)dialogData;
title:(const char *)psz_title
description:(const char *)psz_text - (void)updateDisplayedProgressDialog:(NSArray *)dialogData;
isIndeterminate:(bool)b_indeterminate
position:(float)f_position
cancelTitle:(const char *)psz_cancel;
- (void)updateDisplayedProgressDialogWithID:(vlc_dialog_id *)p_id
value:(float)f_value
description:(const char *)psz_text;
@end @end
...@@ -56,13 +49,11 @@ static void displayErrorCallback(const char *psz_title, ...@@ -56,13 +49,11 @@ static void displayErrorCallback(const char *psz_title,
void *p_data) void *p_data)
{ {
@autoreleasepool { @autoreleasepool {
NSAlert *alert = [NSAlert alertWithMessageText:toNSStr(psz_title) VLCCoreDialogProvider *dialogProvider = (__bridge VLCCoreDialogProvider *)p_data;
defaultButton:_NS("OK") [dialogProvider performSelectorOnMainThread:@selector(displayError:)
alternateButton:nil withObject:@[toNSStr(psz_title),
otherButton:nil toNSStr(psz_text)]
informativeTextWithFormat:@"%@", toNSStr(psz_text)]; waitUntilDone:NO];
[alert setAlertStyle:NSCriticalAlertStyle];
[alert runModal];
} }
} }
...@@ -75,11 +66,13 @@ static void displayLoginCallback(vlc_dialog_id *p_id, ...@@ -75,11 +66,13 @@ static void displayLoginCallback(vlc_dialog_id *p_id,
{ {
@autoreleasepool { @autoreleasepool {
VLCCoreDialogProvider *dialogProvider = (__bridge VLCCoreDialogProvider *)p_data; VLCCoreDialogProvider *dialogProvider = (__bridge VLCCoreDialogProvider *)p_data;
[dialogProvider displayLoginDialogWithID:p_id [dialogProvider performSelectorOnMainThread:@selector(displayLoginDialog:)
title:psz_title withObject:@[[NSValue valueWithPointer:p_id],
description:psz_text toNSStr(psz_title),
defaultUserName:psz_default_username toNSStr(psz_text),
askToStore:b_ask_store]; toNSStr(psz_default_username),
@(b_ask_store)]
waitUntilDone:NO];
} }
} }
...@@ -93,38 +86,16 @@ static void displayQuestionCallback(vlc_dialog_id *p_id, ...@@ -93,38 +86,16 @@ static void displayQuestionCallback(vlc_dialog_id *p_id,
void *p_data) void *p_data)
{ {
@autoreleasepool { @autoreleasepool {
NSAlert *alert = [NSAlert alertWithMessageText:toNSStr(psz_title) VLCCoreDialogProvider *dialogProvider = (__bridge VLCCoreDialogProvider *)p_data;
defaultButton:toNSStr(psz_action1) [dialogProvider performSelectorOnMainThread:@selector(displayQuestion:)
alternateButton:toNSStr(psz_action2) withObject:@[[NSValue valueWithPointer:p_id],
otherButton:toNSStr(psz_cancel) toNSStr(psz_title),
informativeTextWithFormat:@"%@", toNSStr(psz_text)]; toNSStr(psz_text),
@(i_type),
switch (i_type) { toNSStr(psz_cancel),
case VLC_DIALOG_QUESTION_WARNING: toNSStr(psz_action1),
[alert setAlertStyle:NSWarningAlertStyle]; toNSStr(psz_action2)]
break; waitUntilDone:NO];
case VLC_DIALOG_QUESTION_CRITICAL:
[alert setAlertStyle:NSCriticalAlertStyle];
break;
default:
[alert setAlertStyle:NSInformationalAlertStyle];
break;
}
NSInteger returnValue = [alert runModal];
switch (returnValue) {
case NSAlertAlternateReturn:
vlc_dialog_id_post_action(p_id, 2);
break;
case NSAlertOtherReturn:
vlc_dialog_id_post_action(p_id, 3);
break;
default:
vlc_dialog_id_post_action(p_id, 1);
break;
}
} }
} }
...@@ -138,12 +109,14 @@ static void displayProgressCallback(vlc_dialog_id *p_id, ...@@ -138,12 +109,14 @@ static void displayProgressCallback(vlc_dialog_id *p_id,
{ {
@autoreleasepool { @autoreleasepool {
VLCCoreDialogProvider *dialogProvider = (__bridge VLCCoreDialogProvider *)p_data; VLCCoreDialogProvider *dialogProvider = (__bridge VLCCoreDialogProvider *)p_data;
[dialogProvider displayProgressDialogWithID:p_id [dialogProvider performSelectorOnMainThread:@selector(displayProgressDialog:)
title:psz_title withObject:@[[NSValue valueWithPointer:p_id],
description:psz_text toNSStr(psz_title),
isIndeterminate:b_indeterminate toNSStr(psz_text),
position:f_position @(b_indeterminate),
cancelTitle:psz_cancel]; @(f_position),
toNSStr(psz_cancel)]
waitUntilDone:NO];
} }
} }
...@@ -162,9 +135,11 @@ static void updateProgressCallback(vlc_dialog_id *p_id, ...@@ -162,9 +135,11 @@ static void updateProgressCallback(vlc_dialog_id *p_id,
{ {
@autoreleasepool { @autoreleasepool {
VLCCoreDialogProvider *dialogProvider = (__bridge VLCCoreDialogProvider *)p_data; VLCCoreDialogProvider *dialogProvider = (__bridge VLCCoreDialogProvider *)p_data;
[dialogProvider updateDisplayedProgressDialogWithID:p_id [dialogProvider performSelectorOnMainThread:@selector(updateDisplayedProgressDialog:)
value:f_value withObject:@[[NSValue valueWithPointer:p_id],
description:psz_text]; @(f_value),
toNSStr(psz_text)]
waitUntilDone:NO];
} }
} }
...@@ -217,20 +192,27 @@ static void updateProgressCallback(vlc_dialog_id *p_id, ...@@ -217,20 +192,27 @@ static void updateProgressCallback(vlc_dialog_id *p_id,
[progressIndicator setUsesThreadedAnimation: YES]; [progressIndicator setUsesThreadedAnimation: YES];
} }
- (void)displayLoginDialogWithID:(vlc_dialog_id *)p_id - (void)displayError:(NSArray *)dialogData
title:(const char *)psz_title
description:(const char *)psz_text
defaultUserName:(const char *)psz_default_username
askToStore:(bool )b_ask_store
{ {
[authenticationTitleLabel setStringValue:toNSStr(psz_title)]; NSAlert *alert = [NSAlert alertWithMessageText:dialogData[0]
authenticationWindow.title = authenticationTitleLabel.stringValue; defaultButton:_NS("OK")
[authenticationDescriptionLabel setStringValue:toNSStr(psz_text)]; alternateButton:nil
otherButton:nil
informativeTextWithFormat:@"%@", dialogData[1]];
[alert setAlertStyle:NSCriticalAlertStyle];
[alert runModal];
}
- (void)displayLoginDialog:(NSArray *)dialogData
{
[authenticationTitleLabel setStringValue:dialogData[1]];
authenticationWindow.title = dialogData[1];
[authenticationDescriptionLabel setStringValue:dialogData[2]];
[authenticationLoginTextField setStringValue:toNSStr(psz_default_username)]; [authenticationLoginTextField setStringValue:dialogData[3]];
[authenticationPasswordTextField setStringValue:@""]; [authenticationPasswordTextField setStringValue:@""];
authenticationStorePasswordCheckbox.hidden = !b_ask_store; authenticationStorePasswordCheckbox.hidden = ![dialogData[4] boolValue];
authenticationStorePasswordCheckbox.state = NSOffState; authenticationStorePasswordCheckbox.state = NSOffState;
[authenticationWindow center]; [authenticationWindow center];
...@@ -240,7 +222,7 @@ static void updateProgressCallback(vlc_dialog_id *p_id, ...@@ -240,7 +222,7 @@ static void updateProgressCallback(vlc_dialog_id *p_id,
NSString *username = authenticationLoginTextField.stringValue; NSString *username = authenticationLoginTextField.stringValue;
NSString *password = authenticationPasswordTextField.stringValue; NSString *password = authenticationPasswordTextField.stringValue;
vlc_dialog_id_post_login(p_id, vlc_dialog_id_post_login([dialogData[0] pointerValue],
username ? [username UTF8String] : NULL, username ? [username UTF8String] : NULL,
password ? [password UTF8String] : NULL, password ? [password UTF8String] : NULL,
authenticationStorePasswordCheckbox.state == NSOnState); authenticationStorePasswordCheckbox.state == NSOnState);
...@@ -254,23 +236,55 @@ static void updateProgressCallback(vlc_dialog_id *p_id, ...@@ -254,23 +236,55 @@ static void updateProgressCallback(vlc_dialog_id *p_id,
[NSApp stopModalWithCode: 0]; [NSApp stopModalWithCode: 0];
} }
- (void)displayProgressDialogWithID:(vlc_dialog_id *)p_id - (void)displayQuestion:(NSArray *)dialogData
title:(const char *)psz_title {
description:(const char *)psz_text NSAlert *alert = [NSAlert alertWithMessageText:dialogData[1]
isIndeterminate:(bool)b_indeterminate defaultButton:dialogData[5]
position:(float)f_position alternateButton:dialogData[6]
cancelTitle:(const char *)psz_cancel otherButton:dialogData[4]
informativeTextWithFormat:@"%@", dialogData[2]];
switch ([dialogData[3] intValue]) {
case VLC_DIALOG_QUESTION_WARNING:
[alert setAlertStyle:NSWarningAlertStyle];
break;
case VLC_DIALOG_QUESTION_CRITICAL:
[alert setAlertStyle:NSCriticalAlertStyle];
break;
default:
[alert setAlertStyle:NSInformationalAlertStyle];
break;
}
NSInteger returnValue = [alert runModal];
switch (returnValue) {
case NSAlertAlternateReturn:
vlc_dialog_id_post_action([dialogData[0] pointerValue], 2);
break;
case NSAlertOtherReturn:
vlc_dialog_id_post_action([dialogData[0] pointerValue], 3);
break;
default:
vlc_dialog_id_post_action([dialogData[0] pointerValue], 1);
break;
}
}
- (void)displayProgressDialog:(NSArray *)dialogData
{ {
progressTitleLabel.stringValue = toNSStr(psz_title); progressTitleLabel.stringValue = dialogData[1];
progressWindow.title = progressTitleLabel.stringValue; progressWindow.title = dialogData[1];
progressDescriptionLabel.stringValue = toNSStr(psz_text); progressDescriptionLabel.stringValue = dialogData[2];
progressIndicator.indeterminate = b_indeterminate; progressIndicator.indeterminate = [dialogData[3] boolValue];
progressIndicator.doubleValue = f_position; progressIndicator.doubleValue = [dialogData[4] doubleValue];
if (psz_cancel) { if ([dialogData[5] length] > 0) {
progressCancelButton.title = toNSStr(psz_cancel); progressCancelButton.title = dialogData[5];
} else { } else {
progressCancelButton.title = _NS("Cancel"); progressCancelButton.title = _NS("Cancel");
} }
...@@ -278,28 +292,27 @@ static void updateProgressCallback(vlc_dialog_id *p_id, ...@@ -278,28 +292,27 @@ static void updateProgressCallback(vlc_dialog_id *p_id,
[progressIndicator startAnimation:self]; [progressIndicator startAnimation:self];
[progressWindow center]; [progressWindow center];
[NSApp runModalForWindow:progressWindow]; NSInteger returnValue = [NSApp runModalForWindow:progressWindow];
[progressWindow close]; [progressWindow close];
if (p_id != NULL) { [progressIndicator stopAnimation:self];
vlc_dialog_id_dismiss(p_id);
} if (returnValue == -1)
p_id = NULL; vlc_dialog_id_dismiss([dialogData[0] pointerValue]);
} }
- (void)updateDisplayedProgressDialogWithID:(vlc_dialog_id *)p_id - (void)updateDisplayedProgressDialog:(NSArray *)dialogData
value:(float)f_value
description:(const char *)psz_text
{ {
if (!progressIndicator.indeterminate) { if (!progressIndicator.indeterminate) {
progressIndicator.doubleValue = f_value; progressIndicator.doubleValue = [dialogData[1] doubleValue];
progressDescriptionLabel.stringValue = toNSStr(psz_text); progressDescriptionLabel.stringValue = dialogData[2];
} }
} }
- (IBAction)progressDialogAction:(id)sender - (IBAction)progressDialogAction:(id)sender
{ {
[NSApp stopModalWithCode: 0]; [NSApp stopModalWithCode: -1];
} }
@end @end
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