From 32b4423ca8ba31ee6e597d9d9209e7ddb899e3c8 Mon Sep 17 00:00:00 2001 From: Anthony Nicholls Date: Thu, 2 Oct 2025 17:50:40 +0100 Subject: [PATCH] AUv3: Ensure an editor is always available if possible --- .../juce_audio_plugin_client_AUv3.mm | 73 +++++++++---------- 1 file changed, 34 insertions(+), 39 deletions(-) diff --git a/modules/juce_audio_plugin_client/juce_audio_plugin_client_AUv3.mm b/modules/juce_audio_plugin_client/juce_audio_plugin_client_AUv3.mm index 9217a78f80..ca5ebcab55 100644 --- a/modules/juce_audio_plugin_client/juce_audio_plugin_client_AUv3.mm +++ b/modules/juce_audio_plugin_client/juce_audio_plugin_client_AUv3.mm @@ -910,33 +910,47 @@ private: auto supportedViewIndices = [[NSMutableIndexSet alloc] init]; auto n = [configs count]; - if (auto* editor = _this (self)->getAudioProcessor().createEditorIfNeeded()) + const auto getEditor = [&] { - // If you hit this assertion then your plug-in's editor is reporting that it doesn't support - // any host MIDI controller configurations! - jassert (editor->supportsHostMIDIControllerPresence (true) || editor->supportsHostMIDIControllerPresence (false)); + if (auto* editor = _this (self)->getAudioProcessor().getActiveEditor()) + return editor; - for (auto i = 0u; i < n; ++i) + return _this (self)->getAudioProcessor().createEditorIfNeeded(); + }; + + MessageManager::callSync ([&] + { + if (auto* editor = getEditor()) { - if (auto viewConfiguration = [configs objectAtIndex: i]) + // If you hit this assertion then your plug-in's editor is reporting that it doesn't support + // any host MIDI controller configurations! + jassert ( editor->supportsHostMIDIControllerPresence (true) + || editor->supportsHostMIDIControllerPresence (false)); + + for (auto i = 0u; i < n; ++i) { - if (editor->supportsHostMIDIControllerPresence ([viewConfiguration hostHasController] == YES)) + if (auto viewConfiguration = [configs objectAtIndex: i]) { - auto* constrainer = editor->getConstrainer(); - auto height = (int) [viewConfiguration height]; - auto width = (int) [viewConfiguration width]; + if (editor->supportsHostMIDIControllerPresence ([viewConfiguration hostHasController] == YES)) + { + auto* constrainer = editor->getConstrainer(); + auto height = (int) [viewConfiguration height]; + auto width = (int) [viewConfiguration width]; - const auto maxLimits = std::numeric_limits::max() / 2; - const Rectangle requestedBounds { width, height }; - auto modifiedBounds = requestedBounds; - constrainer->checkBounds (modifiedBounds, editor->getBounds().withZeroOrigin(), { maxLimits, maxLimits }, false, false, false, false); + const auto maxLimits = std::numeric_limits::max() / 2; + const Rectangle requestedBounds { width, height }; + auto modifiedBounds = requestedBounds; + constrainer->checkBounds (modifiedBounds, + editor->getBounds().withZeroOrigin(), + { maxLimits, maxLimits }, false, false, false, false); - if (modifiedBounds == requestedBounds) - [supportedViewIndices addIndex: i]; + if (modifiedBounds == requestedBounds) + [supportedViewIndices addIndex: i]; + } } } } - } + }); return [supportedViewIndices autorelease]; }); @@ -1972,14 +1986,14 @@ public: //============================================================================== AUAudioUnit* createAudioUnit (const AudioComponentDescription& descr, NSError** error) { - const auto holder = [&] + const auto holder = std::invoke ([&] { if (auto initialisedHolder = processorHolder.get()) return initialisedHolder; - waitForExecutionOnMainThread ([this] { [myself view]; }); + MessageManager::callSync ([this] { [myself view]; }); return processorHolder.get(); - }(); + }); if (holder == nullptr) return nullptr; @@ -1988,25 +2002,6 @@ public: } private: - template - static void waitForExecutionOnMainThread (Callback&& callback) - { - if (MessageManager::getInstance()->isThisTheMessageThread()) - { - callback(); - return; - } - - std::promise promise; - - MessageManager::callAsync ([&] - { - callback(); - promise.set_value(); - }); - - promise.get_future().get(); - } // There's a chance that createAudioUnit will be called from a background // thread while the processorHolder is being updated on the main thread.