NativeMessageBox::showAsync() how to call?

I have some problems switching to the new async methods for the NativeMessageBox. I tried this static method call but I don’t see any title and no text:

            juce::MessageBoxOptions options;
            options.withTitle("A Title");
            options.withMessage("A message");
            options.withButton("NO");
            options.withButton("YES");
            NativeMessageBox::showAsync(options, [this, file, &invalidSampleMappings](int result)
            {
                if (result == 0)
                {
                    return;
                }
                ...
            });

I guess I’m having a problem with the object lifetimes here. Does someone know what the right way is to show a native message box?

Without seeing the full context, it is hard to point exactly to the issue.
But from your lambda capture it seems that you are capturing invalidSampleMappings by reference. Remember that NativeMessageBox::showAsync returns immediately. So you have to guarantee that invalidSampleMappings will still be a valid object after NativeMessageBox::showAsync returns (and most likely after its caller returns).

Thanks for pointing that out. This is a problem when the callback will be called. I removed the callback code but still have the same problem. The options do not work. I still have an empty dialog.

I guess i see now the problem. All option parameter texts are passed as reference. So they may not exist anymore when the dialog is visible… ?

Any ideas what a good way is to implement this? Do i really need to create a new class here that holds the strings?

Please do it like this:

juce::NativeMessageBox::showAsync (juce::MessageBoxOptions()
                                  .withTitle ("A Title")
                                  .withMessage ("A message.")
                                  .withButton ("OK")
                                  .withButton ("Cancel"), [this](int result) {};

or:

juce::MessageBoxOptions options;
options = options.withTitle ("A Title")
                 .withMessage ("A message")
                 .withButton ("NO")
                 .withButton ("YES");
        
juce::NativeMessageBox::showAsync (options, [this](int result) {});

The problem was that writing for example options.withMessage ("A message") doesn’t change options. You should use MessageBoxOptions returned by options.withMessage ("A message")

4 Likes

A cool. A fluent synthax :smile: thanks for your help.

1 Like