AlertWindow::showOkCancelBox always returning False

Hey there, I’ve currently got an issue in a preset manager I’m making. Below is the implementation of the deletePreset function that should delete the selected preset.

The alert window doesnt return true no matter what button I click.

It worked fine before adding the showOkCancelBox logic (ie it immediately deleted the file), but now that I want to add the confirmation box the code never runs since the Alert Window never returns true.

For some context (if it even helps), my PresetManager is defined in the Plugin Processor as follows

// in the PluginProcessor constructor
presetManager = std::make_unique<Service::PresetManager>(apvts);

And below is the deletePreset implementation

void PresetManager::deletePreset(const String& presetName)
	{
		if (presetName.isEmpty())
			return;

		const auto presetFile = defaultDirectory.getChildFile(presetName + "." + extension);
		
		if (!presetFile.existsAsFile())
		{
			DBG("Preset file " + presetFile.getFullPathName() + " does not exist");
			jassertfalse;
			return;
		}
		
		// Show confirmation before deleting
		bool actuallyDelete = AlertWindow::showOkCancelBox(
			AlertWindow::WarningIcon, 
			"Delete Preset", 
			"Are you sure you want to delete the preset \"" + presetName + "\"?", 
			"Delete", 
			"Cancel",
			nullptr,
			nullptr
		);

		DBG(presetFile.getFullPathName());
		DBG( (actuallyDelete ? "Deleting" : "Not deleting") );

		// we do actually want to delete the file
		if (actuallyDelete)
		{
			presetFile.setReadOnly(false);
			if (!presetFile.deleteFile())
			{
				DBG("Preset file " + presetFile.getFullPathName() + " could not be deleted");
				jassertfalse;
				return;
			} else {
				DBG("Preset file " + presetFile.getFullPathName() + " deleted");
			}
			
			currentPreset.setValue("");
		}
	}

I’m not sure if this is the reason, but if this is part of a plugin you should not be using AlertWindow modally. Per the documentation:

if JUCE_MODAL_LOOPS_PERMITTED is defined and the callback parameter is null, the box is shown modally, and the method will block until the user has clicked the button (or pressed the escape or return keys). This mode of operation can cause problems, especially in plugins, so it is not recommended.

Supplying a callback function to handle the results of the dialog is what you want to do in a plugin (and I just use it this way all the time, since I don’t write modal code anymore). I didn’t compile this code, but it should be close to what you want.

AlertWindow::showOkCancelBox(
    AlertWindow::WarningIcon, 
    "Delete Preset", 
    "Are you sure you want to delete the preset \"" + presetName + "\"?", 
    "Delete", 
    "Cancel",
    nullptr,
    juce::ModalCallbackFunction::create ([this, presetName] (int option)
    {
        if (option == 0) // no
            return;
        DBG(presetFile.getFullPathName());

        presetFile.setReadOnly(false);
        if (!presetFile.deleteFile())
        {
            DBG("Preset file " + presetFile.getFullPathName() + " could not be deleted");
            jassertfalse;
            return;
        } else {
            DBG("Preset file " + presetFile.getFullPathName() + " deleted");
        }
        
        currentPreset.setValue("");
    }));
2 Likes

Just implemented this now and it functions perfectly, Thanks a bunch for the help!

1 Like