1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

NativeMessageBox: Return correctly mapped value when shown modally

This commit is contained in:
ed 2021-09-01 17:46:11 +01:00
parent 3b97c40c2d
commit f5eee033ba
5 changed files with 87 additions and 96 deletions

View file

@ -1400,7 +1400,8 @@ private:
//==============================================================================
static void createAndroidDialog (const MessageBoxOptions& opts,
std::unique_ptr<ModalComponentManager::Callback> callback)
ModalComponentManager::Callback* callbackIn,
AlertWindowMappings::MapFn mapFn)
{
auto* env = getEnv();
@ -1410,7 +1411,7 @@ static void createAndroidDialog (const MessageBoxOptions& opts,
builder = LocalRef<jobject> (env->CallObjectMethod (builder.get(), AndroidAlertDialogBuilder.setMessage, javaString (opts.getMessage()).get()));
builder = LocalRef<jobject> (env->CallObjectMethod (builder.get(), AndroidAlertDialogBuilder.setCancelable, true));
std::shared_ptr<ModalComponentManager::Callback> sharedCallback (std::move (callback));
std::shared_ptr<ModalComponentManager::Callback> sharedCallback (AlertWindowMappings::getWrappedCallback (callbackIn, mapFn));
builder = LocalRef<jobject> (env->CallObjectMethod (builder.get(), AndroidAlertDialogBuilder.setOnCancelListener,
CreateJavaInterface (new DialogListener (sharedCallback, 0),
@ -1455,11 +1456,11 @@ void JUCE_CALLTYPE NativeMessageBox::showMessageBoxAsync (MessageBoxIconType /*i
Component* /*associatedComponent*/,
ModalComponentManager::Callback* callback)
{
showAsync (MessageBoxOptions()
.withTitle (title)
.withMessage (message)
.withButton (TRANS("OK")),
AlertWindowMappings::getWrappedCallback (callback, AlertWindowMappings::messageBox));
createAndroidDialog (MessageBoxOptions()
.withTitle (title)
.withMessage (message)
.withButton (TRANS("OK")),
callback, AlertWindowMappings::messageBox);
}
bool JUCE_CALLTYPE NativeMessageBox::showOkCancelBox (MessageBoxIconType /*iconType*/,
@ -1467,12 +1468,12 @@ bool JUCE_CALLTYPE NativeMessageBox::showOkCancelBox (MessageBoxIconType /*iconT
Component* /*associatedComponent*/,
ModalComponentManager::Callback* callback)
{
showAsync (MessageBoxOptions()
.withTitle (title)
.withMessage (message)
.withButton (TRANS("OK"))
.withButton (TRANS("Cancel")),
AlertWindowMappings::getWrappedCallback (callback, AlertWindowMappings::okCancel));
createAndroidDialog (MessageBoxOptions()
.withTitle (title)
.withMessage (message)
.withButton (TRANS("OK"))
.withButton (TRANS("Cancel")),
callback, AlertWindowMappings::okCancel);
return false;
}
@ -1482,13 +1483,13 @@ int JUCE_CALLTYPE NativeMessageBox::showYesNoCancelBox (MessageBoxIconType /*ico
Component* /*associatedComponent*/,
ModalComponentManager::Callback* callback)
{
showAsync (MessageBoxOptions()
.withTitle (title)
.withMessage (message)
.withButton (TRANS("Yes"))
.withButton (TRANS("No"))
.withButton (TRANS("Cancel")),
AlertWindowMappings::getWrappedCallback (callback, AlertWindowMappings::yesNoCancel));
createAndroidDialog (MessageBoxOptions()
.withTitle (title)
.withMessage (message)
.withButton (TRANS("Yes"))
.withButton (TRANS("No"))
.withButton (TRANS("Cancel")),
callback, AlertWindowMappings::yesNoCancel);
return 0;
}
@ -1498,12 +1499,12 @@ int JUCE_CALLTYPE NativeMessageBox::showYesNoBox (MessageBoxIconType /*iconType*
Component* /*associatedComponent*/,
ModalComponentManager::Callback* callback)
{
showAsync (MessageBoxOptions()
.withTitle (title)
.withMessage (message)
.withButton (TRANS("Yes"))
.withButton (TRANS("No")),
AlertWindowMappings::getWrappedCallback (callback, AlertWindowMappings::okCancel));
createAndroidDialog (MessageBoxOptions()
.withTitle (title)
.withMessage (message)
.withButton (TRANS("Yes"))
.withButton (TRANS("No")),
callback, AlertWindowMappings::okCancel);
return 0;
}
@ -1511,7 +1512,7 @@ int JUCE_CALLTYPE NativeMessageBox::showYesNoBox (MessageBoxIconType /*iconType*
void JUCE_CALLTYPE NativeMessageBox::showAsync (const MessageBoxOptions& options,
ModalComponentManager::Callback* callback)
{
createAndroidDialog (options, std::unique_ptr<ModalComponentManager::Callback> (callback));
createAndroidDialog (options, callback, AlertWindowMappings::noMapping);
}
void JUCE_CALLTYPE NativeMessageBox::showAsync (const MessageBoxOptions& options,

View file

@ -507,23 +507,23 @@ private:
//==============================================================================
static int showDialog (const MessageBoxOptions& options,
std::unique_ptr<ModalComponentManager::Callback> callback,
Async async)
ModalComponentManager::Callback* callbackIn,
AlertWindowMappings::MapFn mapFn)
{
#if JUCE_MODAL_LOOPS_PERMITTED
if (async == Async::no)
if (callbackIn == nullptr)
{
JUCE_AUTORELEASEPOOL
{
iOSMessageBox messageBox (options, std::move (callback));
return messageBox.getResult();
jassert (mapFn != nullptr);
iOSMessageBox messageBox (options, nullptr);
return mapFn (messageBox.getResult());
}
}
#endif
ignoreUnused (async);
new iOSMessageBox (options, std::move (callback));
new iOSMessageBox (options, AlertWindowMappings::getWrappedCallback (callbackIn, mapFn));
return 0;
}
@ -536,12 +536,12 @@ void JUCE_CALLTYPE NativeMessageBox::showMessageBox (MessageBoxIconType /*iconTy
.withTitle (title)
.withMessage (message)
.withButton (TRANS("OK")),
nullptr, Async::no);
nullptr, AlertWindowMappings::messageBox);
}
int JUCE_CALLTYPE NativeMessageBox::show (const MessageBoxOptions& options)
{
return showDialog (options, nullptr, Async::no);
return showDialog (options, nullptr, AlertWindowMappings::noMapping);
}
#endif
@ -554,8 +554,7 @@ void JUCE_CALLTYPE NativeMessageBox::showMessageBoxAsync (MessageBoxIconType /*i
.withTitle (title)
.withMessage (message)
.withButton (TRANS("OK")),
rawToUniquePtr (AlertWindowMappings::getWrappedCallback (callback, AlertWindowMappings::messageBox)),
Async::yes);
callback, AlertWindowMappings::messageBox);
}
bool JUCE_CALLTYPE NativeMessageBox::showOkCancelBox (MessageBoxIconType /*iconType*/,
@ -568,8 +567,7 @@ bool JUCE_CALLTYPE NativeMessageBox::showOkCancelBox (MessageBoxIconType /*iconT
.withMessage (message)
.withButton (TRANS("OK"))
.withButton (TRANS("Cancel")),
rawToUniquePtr (AlertWindowMappings::getWrappedCallback (callback, AlertWindowMappings::okCancel)),
callback != nullptr ? Async::yes : Async::no) == 1;
callback, AlertWindowMappings::okCancel) != 0;
}
int JUCE_CALLTYPE NativeMessageBox::showYesNoCancelBox (MessageBoxIconType /*iconType*/,
@ -583,8 +581,7 @@ int JUCE_CALLTYPE NativeMessageBox::showYesNoCancelBox (MessageBoxIconType /*ico
.withButton (TRANS("Yes"))
.withButton (TRANS("No"))
.withButton (TRANS("Cancel")),
rawToUniquePtr (AlertWindowMappings::getWrappedCallback (callback, AlertWindowMappings::yesNoCancel)),
callback != nullptr ? Async::yes : Async::no);
callback, AlertWindowMappings::yesNoCancel);
}
int JUCE_CALLTYPE NativeMessageBox::showYesNoBox (MessageBoxIconType /*iconType*/,
@ -597,14 +594,13 @@ int JUCE_CALLTYPE NativeMessageBox::showYesNoBox (MessageBoxIconType /*iconType*
.withMessage (message)
.withButton (TRANS("Yes"))
.withButton (TRANS("No")),
rawToUniquePtr (AlertWindowMappings::getWrappedCallback (callback, AlertWindowMappings::okCancel)),
callback != nullptr ? Async::yes : Async::no);
callback, AlertWindowMappings::okCancel);
}
void JUCE_CALLTYPE NativeMessageBox::showAsync (const MessageBoxOptions& options,
ModalComponentManager::Callback* callback)
{
showDialog (options, rawToUniquePtr (callback), Async::yes);
showDialog (options, callback, AlertWindowMappings::noMapping);
}
void JUCE_CALLTYPE NativeMessageBox::showAsync (const MessageBoxOptions& options,

View file

@ -100,17 +100,21 @@ private:
};
static int showDialog (const MessageBoxOptions& options,
std::unique_ptr<ModalComponentManager::Callback> callback,
Async async)
ModalComponentManager::Callback* callbackIn,
AlertWindowMappings::MapFn mapFn)
{
auto messageBox = std::make_unique<OSXMessageBox> (options, std::move (callback));
#if JUCE_MODAL_LOOPS_PERMITTED
if (async == Async::no)
return messageBox->getResult();
if (callbackIn == nullptr)
{
jassert (mapFn != nullptr);
OSXMessageBox messageBox (options, nullptr);
return mapFn (messageBox.getResult());
}
#endif
ignoreUnused (async);
auto messageBox = std::make_unique<OSXMessageBox> (options,
AlertWindowMappings::getWrappedCallback (callbackIn, mapFn));
messageBox->triggerAsyncUpdate();
messageBox.release();
@ -128,13 +132,12 @@ void JUCE_CALLTYPE NativeMessageBox::showMessageBox (MessageBoxIconType iconType
.withTitle (title)
.withMessage (message)
.withButton (TRANS("OK")),
nullptr,
Async::no);
nullptr, AlertWindowMappings::messageBox);
}
int JUCE_CALLTYPE NativeMessageBox::show (const MessageBoxOptions& options)
{
return showDialog (options, nullptr, Async::no);
return showDialog (options, nullptr, AlertWindowMappings::noMapping);
}
#endif
@ -148,8 +151,7 @@ void JUCE_CALLTYPE NativeMessageBox::showMessageBoxAsync (MessageBoxIconType ico
.withTitle (title)
.withMessage (message)
.withButton (TRANS("OK")),
rawToUniquePtr (AlertWindowMappings::getWrappedCallback (callback, AlertWindowMappings::messageBox)),
Async::yes);
callback, AlertWindowMappings::messageBox);
}
bool JUCE_CALLTYPE NativeMessageBox::showOkCancelBox (MessageBoxIconType iconType,
@ -163,8 +165,7 @@ bool JUCE_CALLTYPE NativeMessageBox::showOkCancelBox (MessageBoxIconType iconTyp
.withMessage (message)
.withButton (TRANS("OK"))
.withButton (TRANS("Cancel")),
rawToUniquePtr (AlertWindowMappings::getWrappedCallback (callback, AlertWindowMappings::okCancel)),
callback != nullptr ? Async::yes : Async::no) != 0;
callback, AlertWindowMappings::okCancel) != 0;
}
int JUCE_CALLTYPE NativeMessageBox::showYesNoCancelBox (MessageBoxIconType iconType,
@ -179,8 +180,7 @@ int JUCE_CALLTYPE NativeMessageBox::showYesNoCancelBox (MessageBoxIconType iconT
.withButton (TRANS("Yes"))
.withButton (TRANS("No"))
.withButton (TRANS("Cancel")),
rawToUniquePtr (AlertWindowMappings::getWrappedCallback (callback, AlertWindowMappings::yesNoCancel)),
callback != nullptr ? Async::yes : Async::no);
callback, AlertWindowMappings::yesNoCancel);
}
int JUCE_CALLTYPE NativeMessageBox::showYesNoBox (MessageBoxIconType iconType,
@ -194,14 +194,13 @@ int JUCE_CALLTYPE NativeMessageBox::showYesNoBox (MessageBoxIconType iconType,
.withMessage (message)
.withButton (TRANS("Yes"))
.withButton (TRANS("No")),
rawToUniquePtr (AlertWindowMappings::getWrappedCallback (callback, AlertWindowMappings::okCancel)),
callback != nullptr ? Async::yes : Async::no);
callback, AlertWindowMappings::okCancel);
}
void JUCE_CALLTYPE NativeMessageBox::showAsync (const MessageBoxOptions& options,
ModalComponentManager::Callback* callback)
{
showDialog (options, rawToUniquePtr (callback), Async::yes);
showDialog (options, callback, AlertWindowMappings::noMapping);
}
void JUCE_CALLTYPE NativeMessageBox::showAsync (const MessageBoxOptions& options,

View file

@ -4651,17 +4651,21 @@ static std::unique_ptr<WindowsMessageBoxBase> createMessageBox (const MessageBox
}
static int showDialog (const MessageBoxOptions& options,
std::unique_ptr<ModalComponentManager::Callback> callback,
Async async)
ModalComponentManager::Callback* callbackIn,
AlertWindowMappings::MapFn mapFn)
{
auto messageBox = createMessageBox (options, std::move (callback));
#if JUCE_MODAL_LOOPS_PERMITTED
if (async == Async::no)
return messageBox->getResult();
if (callbackIn == nullptr)
{
jassert (mapFn != nullptr);
auto messageBox = createMessageBox (options, nullptr);
return mapFn (messageBox->getResult());
}
#endif
ignoreUnused (async);
auto messageBox = createMessageBox (options,
AlertWindowMappings::getWrappedCallback (callbackIn, mapFn));
messageBox->triggerAsyncUpdate();
messageBox.release();
@ -4680,13 +4684,12 @@ void JUCE_CALLTYPE NativeMessageBox::showMessageBox (MessageBoxIconType iconType
.withMessage (message)
.withButton (TRANS("OK"))
.withAssociatedComponent (associatedComponent),
nullptr,
Async::no);
nullptr, AlertWindowMappings::messageBox);
}
int JUCE_CALLTYPE NativeMessageBox::show (const MessageBoxOptions& options)
{
return showDialog (options, nullptr, Async::no);
return showDialog (options, nullptr, AlertWindowMappings::noMapping);
}
#endif
@ -4701,8 +4704,7 @@ void JUCE_CALLTYPE NativeMessageBox::showMessageBoxAsync (MessageBoxIconType ico
.withMessage (message)
.withButton (TRANS("OK"))
.withAssociatedComponent (associatedComponent),
rawToUniquePtr (AlertWindowMappings::getWrappedCallback (callback, AlertWindowMappings::messageBox)),
Async::yes);
callback, AlertWindowMappings::messageBox);
}
bool JUCE_CALLTYPE NativeMessageBox::showOkCancelBox (MessageBoxIconType iconType,
@ -4717,8 +4719,7 @@ bool JUCE_CALLTYPE NativeMessageBox::showOkCancelBox (MessageBoxIconType iconTyp
.withButton (TRANS("OK"))
.withButton (TRANS("Cancel"))
.withAssociatedComponent (associatedComponent),
rawToUniquePtr (AlertWindowMappings::getWrappedCallback (callback, AlertWindowMappings::okCancel)),
callback != nullptr ? Async::yes : Async::no) == 1;
callback, AlertWindowMappings::okCancel) != 0;
}
int JUCE_CALLTYPE NativeMessageBox::showYesNoCancelBox (MessageBoxIconType iconType,
@ -4734,8 +4735,7 @@ int JUCE_CALLTYPE NativeMessageBox::showYesNoCancelBox (MessageBoxIconType iconT
.withButton (TRANS("No"))
.withButton (TRANS("Cancel"))
.withAssociatedComponent (associatedComponent),
rawToUniquePtr (AlertWindowMappings::getWrappedCallback (callback, AlertWindowMappings::yesNoCancel)),
callback != nullptr ? Async::yes : Async::no);
callback, AlertWindowMappings::yesNoCancel);
}
int JUCE_CALLTYPE NativeMessageBox::showYesNoBox (MessageBoxIconType iconType,
@ -4750,14 +4750,13 @@ int JUCE_CALLTYPE NativeMessageBox::showYesNoBox (MessageBoxIconType iconType,
.withButton (TRANS("Yes"))
.withButton (TRANS("No"))
.withAssociatedComponent (associatedComponent),
rawToUniquePtr (AlertWindowMappings::getWrappedCallback (callback, AlertWindowMappings::okCancel)),
callback != nullptr ? Async::yes : Async::no);
callback, AlertWindowMappings::okCancel);
}
void JUCE_CALLTYPE NativeMessageBox::showAsync (const MessageBoxOptions& options,
ModalComponentManager::Callback* callback)
{
showDialog (options, rawToUniquePtr (callback), Async::yes);
showDialog (options, callback, AlertWindowMappings::noMapping);
}
void JUCE_CALLTYPE NativeMessageBox::showAsync (const MessageBoxOptions& options,

View file

@ -629,12 +629,13 @@ namespace AlertWindowMappings
{
using MapFn = int (*) (int);
static int noMapping (int buttonIndex) { return buttonIndex; }
static int messageBox (int) { return 0; }
static int okCancel (int buttonIndex) { return buttonIndex == 0 ? 1 : 0; }
static int yesNoCancel (int buttonIndex) { return buttonIndex == 2 ? 0 : buttonIndex + 1; }
static inline int noMapping (int buttonIndex) { return buttonIndex; }
static inline int messageBox (int) { return 0; }
static inline int okCancel (int buttonIndex) { return buttonIndex == 0 ? 1 : 0; }
static inline int yesNoCancel (int buttonIndex) { return buttonIndex == 2 ? 0 : buttonIndex + 1; }
static ModalComponentManager::Callback* getWrappedCallback (ModalComponentManager::Callback* callbackIn, MapFn mapFn)
static std::unique_ptr<ModalComponentManager::Callback> getWrappedCallback (ModalComponentManager::Callback* callbackIn,
MapFn mapFn)
{
jassert (mapFn != nullptr);
@ -646,7 +647,7 @@ namespace AlertWindowMappings
innerCallback->modalStateFinished (mapFn (buttonIndex));
};
return ModalCallbackFunction::create (std::move (wrappedCallback));
return rawToUniquePtr (ModalCallbackFunction::create (std::move (wrappedCallback)));
}
}
@ -711,9 +712,6 @@ void AlertWindow::showMessageBoxAsync (MessageBoxIconType iconType,
Component* associatedComponent,
ModalComponentManager::Callback* callback)
{
if (LookAndFeel::getDefaultLookAndFeel().isUsingNativeAlertWindows())
callback = AlertWindowMappings::getWrappedCallback (callback, AlertWindowMappings::messageBox);
showAsync (MessageBoxOptions()
.withIconType (iconType)
.withTitle (title)
@ -727,12 +725,10 @@ static int showMaybeAsync (const MessageBoxOptions& options,
ModalComponentManager::Callback* callbackIn,
AlertWindowMappings::MapFn mapFn)
{
jassert (mapFn != nullptr);
const auto showAsync = (callbackIn != nullptr ? Async::yes
: Async::no);
auto callback = rawToUniquePtr (AlertWindowMappings::getWrappedCallback (callbackIn, mapFn));
auto callback = AlertWindowMappings::getWrappedCallback (callbackIn, mapFn);
if (LookAndFeel::getDefaultLookAndFeel().isUsingNativeAlertWindows())
{