1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-02-06 04:00:08 +00:00

ScopedMessageBox: Replace old AlertWindow uses with new API

This commit is contained in:
reuk 2023-02-22 20:54:45 +00:00
parent 79ed81c24a
commit 39a731de46
No known key found for this signature in database
GPG key ID: FCB43929F012EE5C
55 changed files with 893 additions and 677 deletions

View file

@ -26,10 +26,59 @@
namespace juce
{
template <ScopedMessageBox::Pimpl::MapFn mapFn = nullptr>
static int showNativeBoxUnmanaged (const MessageBoxOptions& opts, ModalComponentManager::Callback* cb)
enum class ResultCodeMappingMode
{
return ScopedMessageBox::Pimpl::showUnmanaged<mapFn> (ScopedMessageBoxInterface::create (opts), cb);
plainIndex, // The result code is equal to the index of the selected button.
// This is used for NativeMessageBox::show, showAsync, and showMessageBox.
alertWindow, // The result code is mapped in the same way as AlertWindow, i.e. if there
// are N buttons then button X will return ((X + 1) % N).
};
static std::unique_ptr<ScopedMessageBoxInterface> makeNativeMessageBoxWithMappedResult (const MessageBoxOptions& opts,
ResultCodeMappingMode mode)
{
class Adapter : public ScopedMessageBoxInterface
{
public:
explicit Adapter (const MessageBoxOptions& options)
: inner (ScopedMessageBoxInterface::create (options)),
numButtons (options.getNumButtons()) {}
void runAsync (std::function<void (int)> fn) override
{
inner->runAsync ([fn, n = numButtons] (int result)
{
fn (map (result, n));
});
}
int runSync() override
{
return map (inner->runSync(), numButtons);
}
void close() override
{
inner->close();
}
private:
static int map (int button, int numButtons) { return (button + 1) % numButtons; }
std::unique_ptr<ScopedMessageBoxInterface> inner;
int numButtons = 0;
};
return mode == ResultCodeMappingMode::plainIndex ? ScopedMessageBoxInterface::create (opts)
: std::make_unique<Adapter> (opts);
}
static int showNativeBoxUnmanaged (const MessageBoxOptions& opts,
ModalComponentManager::Callback* cb,
ResultCodeMappingMode mode)
{
auto implementation = makeNativeMessageBoxWithMappedResult (opts, mode);
return ScopedMessageBox::Pimpl::showUnmanaged (std::move (implementation), cb);
}
#if JUCE_MODAL_LOOPS_PERMITTED
@ -37,17 +86,18 @@ void JUCE_CALLTYPE NativeMessageBox::showMessageBox (MessageBoxIconType iconType
const String& title, const String& message,
Component* associatedComponent)
{
showNativeBoxUnmanaged<ScopedMessageBox::Pimpl::messageBox> (MessageBoxOptions().withIconType (iconType)
.withTitle (title)
.withMessage (message)
.withButton (TRANS("OK"))
.withAssociatedComponent (associatedComponent),
nullptr);
showNativeBoxUnmanaged (MessageBoxOptions().withIconType (iconType)
.withTitle (title)
.withMessage (message)
.withButton (TRANS("OK"))
.withAssociatedComponent (associatedComponent),
nullptr,
ResultCodeMappingMode::plainIndex);
}
int JUCE_CALLTYPE NativeMessageBox::show (const MessageBoxOptions& options)
{
return showNativeBoxUnmanaged<> (options, nullptr);
return showNativeBoxUnmanaged (options, nullptr, ResultCodeMappingMode::plainIndex);
}
#endif
@ -56,12 +106,8 @@ void JUCE_CALLTYPE NativeMessageBox::showMessageBoxAsync (MessageBoxIconType ico
Component* associatedComponent,
ModalComponentManager::Callback* callback)
{
showNativeBoxUnmanaged<ScopedMessageBox::Pimpl::messageBox> (MessageBoxOptions().withIconType (iconType)
.withTitle (title)
.withMessage (message)
.withButton (TRANS("OK"))
.withAssociatedComponent (associatedComponent),
callback);
auto options = MessageBoxOptions::makeOptionsOk (iconType, title, message, {}, associatedComponent);
showNativeBoxUnmanaged (options, callback, ResultCodeMappingMode::alertWindow);
}
bool JUCE_CALLTYPE NativeMessageBox::showOkCancelBox (MessageBoxIconType iconType,
@ -69,13 +115,8 @@ bool JUCE_CALLTYPE NativeMessageBox::showOkCancelBox (MessageBoxIconType iconTyp
Component* associatedComponent,
ModalComponentManager::Callback* callback)
{
return showNativeBoxUnmanaged<ScopedMessageBox::Pimpl::okCancel> (MessageBoxOptions().withIconType (iconType)
.withTitle (title)
.withMessage (message)
.withButton (TRANS("OK"))
.withButton (TRANS("Cancel"))
.withAssociatedComponent (associatedComponent),
callback) != 0;
auto options = MessageBoxOptions::makeOptionsOkCancel (iconType, title, message, {}, {}, associatedComponent);
return showNativeBoxUnmanaged (options, callback, ResultCodeMappingMode::alertWindow) != 0;
}
int JUCE_CALLTYPE NativeMessageBox::showYesNoCancelBox (MessageBoxIconType iconType,
@ -83,14 +124,8 @@ int JUCE_CALLTYPE NativeMessageBox::showYesNoCancelBox (MessageBoxIconType iconT
Component* associatedComponent,
ModalComponentManager::Callback* callback)
{
return showNativeBoxUnmanaged<ScopedMessageBox::Pimpl::yesNoCancel> (MessageBoxOptions().withIconType (iconType)
.withTitle (title)
.withMessage (message)
.withButton (TRANS("Yes"))
.withButton (TRANS("No"))
.withButton (TRANS("Cancel"))
.withAssociatedComponent (associatedComponent),
callback);
auto options = MessageBoxOptions::makeOptionsYesNoCancel (iconType, title, message, {}, {}, {}, associatedComponent);
return showNativeBoxUnmanaged (options, callback, ResultCodeMappingMode::alertWindow);
}
int JUCE_CALLTYPE NativeMessageBox::showYesNoBox (MessageBoxIconType iconType,
@ -98,19 +133,14 @@ int JUCE_CALLTYPE NativeMessageBox::showYesNoBox (MessageBoxIconType iconType,
Component* associatedComponent,
ModalComponentManager::Callback* callback)
{
return showNativeBoxUnmanaged<ScopedMessageBox::Pimpl::okCancel> (MessageBoxOptions().withIconType (iconType)
.withTitle (title)
.withMessage (message)
.withButton (TRANS("Yes"))
.withButton (TRANS("No"))
.withAssociatedComponent (associatedComponent),
callback);
auto options = MessageBoxOptions::makeOptionsYesNo (iconType, title, message, {}, {}, associatedComponent);
return showNativeBoxUnmanaged (options, callback, ResultCodeMappingMode::alertWindow);
}
void JUCE_CALLTYPE NativeMessageBox::showAsync (const MessageBoxOptions& options,
ModalComponentManager::Callback* callback)
{
showNativeBoxUnmanaged<> (options, callback);
showNativeBoxUnmanaged (options, callback, ResultCodeMappingMode::plainIndex);
}
void JUCE_CALLTYPE NativeMessageBox::showAsync (const MessageBoxOptions& options,
@ -121,7 +151,8 @@ void JUCE_CALLTYPE NativeMessageBox::showAsync (const MessageBoxOptions& options
ScopedMessageBox NativeMessageBox::showScopedAsync (const MessageBoxOptions& options, std::function<void (int)> callback)
{
return ScopedMessageBox::Pimpl::show (ScopedMessageBoxInterface::create (options), std::move (callback));
auto implementation = makeNativeMessageBoxWithMappedResult (options, ResultCodeMappingMode::alertWindow);
return ScopedMessageBox::Pimpl::show (std::move (implementation), std::move (callback));
}
} // namespace juce