@@ -338,7 +338,6 @@ void Instance::initialisePd(String& pdlua_version)
338338 if (inst->initialiseIntoPluginmode )
339339 return ;
340340
341- auto * pd = static_cast <PluginProcessor*>(inst);
342341 t_canvas* glist = reinterpret_cast <struct _glist *>(argv->a_w .w_gpointer );
343342
344343 if (auto const vis = atom_getfloat (argv + 1 )) {
@@ -348,45 +347,46 @@ void Instance::initialisePd(String& pdlua_version)
348347 patchFile = File (String::fromUTF8 (canvas_getdir (glist)->s_name )).getChildFile (String::fromUTF8 (glist->gl_name ->s_name )).withFileExtension (" pd" );
349348 }
350349
351- MessageManager::callAsync ([pd, inst = juce::WeakReference (inst), patchToOpen = pd::WeakReference (glist, pd), patchFile] {
352- if (!inst) return ;
353- PluginEditor* activeEditor = nullptr ;
354- for (auto * editor : pd->getEditors ()) {
355- if (editor->isActiveWindow ())
350+ MessageManager::callAsync ([inst = juce::WeakReference (inst), patchToOpen = pd::WeakReference (glist, inst), patchFile] {
351+ if (auto * pd = static_cast <PluginProcessor*>(inst.get ())) {
352+ PluginEditor* activeEditor = nullptr ;
353+ for (auto * editor : pd->getEditors ()) {
354+ if (editor->isActiveWindow ())
355+ {
356+ activeEditor = editor;
357+ break ;
358+ }
359+ }
360+ if (!activeEditor || !patchToOpen.isValid ())
361+ return ;
362+
363+ for (auto & patch : pd->patches )
356364 {
357- activeEditor = editor;
358- break ;
365+ if (patch->getRawPointer () == patchToOpen.getRaw <t_glist>())
366+ {
367+ activeEditor->getTabComponent ().openPatch (patch);
368+ return ;
369+ }
359370 }
360- }
361- if (!activeEditor || !patchToOpen.isValid ())
362- return ;
363-
364- for (auto & patch : pd->patches )
365- {
366- if (patch->getRawPointer () == patchToOpen.getRaw <t_glist>())
371+
372+ pd::Patch::Ptr subpatch = new pd::Patch (patchToOpen, pd, false );
373+ if (patchFile.exists ())
367374 {
368- activeEditor->getTabComponent ().openPatch (patch);
369- return ;
375+ subpatch->setCurrentFile (URL (patchFile));
370376 }
377+ activeEditor->getTabComponent ().openPatch (subpatch);
371378 }
372-
373- pd::Patch::Ptr subpatch = new pd::Patch (patchToOpen, pd, false );
374- if (patchFile.exists ())
375- {
376- subpatch->setCurrentFile (URL (patchFile));
377- }
378- activeEditor->getTabComponent ().openPatch (subpatch);
379-
380379 });
381380 } else {
382- MessageManager::callAsync ([pd, inst = juce::WeakReference (inst), glist] {
383- if (!inst) return ;
384- for (auto * editor : pd->getEditors ()) {
385- for (auto * canvas : editor->getCanvases ()) {
386- auto canvasPtr = canvas->patch .getPointer ();
387- if (canvasPtr && canvasPtr.get () == glist) {
388- canvas->editor ->getTabComponent ().closeTab (canvas);
389- break ;
381+ MessageManager::callAsync ([inst = juce::WeakReference (inst), glist] {
382+ if (auto * pd = static_cast <PluginProcessor*>(inst.get ())) {
383+ for (auto * editor : pd->getEditors ()) {
384+ for (auto * canvas : editor->getCanvases ()) {
385+ auto canvasPtr = canvas->patch .getPointer ();
386+ if (canvasPtr && canvasPtr.get () == glist) {
387+ canvas->editor ->getTabComponent ().closeTab (canvas);
388+ break ;
389+ }
390390 }
391391 }
392392 }
@@ -396,36 +396,38 @@ void Instance::initialisePd(String& pdlua_version)
396396 }
397397 case hash (" canvas_undo_redo" ): {
398398 auto * inst = static_cast <Instance*>(instance);
399- auto * pd = static_cast <PluginProcessor*>(inst);
400399 auto * glist = reinterpret_cast <t_canvas*>(argv->a_w .w_gpointer );
401400 auto * undoName = atom_getsymbol (argv + 1 );
402401 auto * redoName = atom_getsymbol (argv + 2 );
403- MessageManager::callAsync ([pd, glist, undoName, redoName] {
404- for (auto const & patch : pd->patches ) {
405- if (patch->ptr .getRaw <t_canvas>() == glist) {
406- patch->updateUndoRedoState (SmallString (undoName->s_name ), SmallString (redoName->s_name ));
402+ MessageManager::callAsync ([instance = juce::WeakReference (inst), glist, undoName, redoName] {
403+ if (auto * pd = static_cast <PluginProcessor*>(instance.get ())) {
404+ for (auto const & patch : pd->patches ) {
405+ if (patch->ptr .getRaw <t_canvas>() == glist) {
406+ patch->updateUndoRedoState (SmallString (undoName->s_name ), SmallString (redoName->s_name ));
407+ }
407408 }
409+ for (auto * editor : pd->getEditors ())
410+ editor->triggerAsyncUpdate ();
408411 }
409- for (auto * editor : pd->getEditors ())
410- editor->triggerAsyncUpdate ();
411412 });
412413 break ;
413414 }
414415 case hash (" canvas_title" ): {
415416 auto * inst = static_cast <Instance*>(instance);
416- auto * pd = static_cast <PluginProcessor*>(inst);
417417 auto * glist = reinterpret_cast <t_canvas*>(argv->a_w .w_gpointer );
418418 auto * title = atom_getsymbol (argv + 1 );
419419 int isDirty = atom_getfloat (argv + 2 );
420420
421- MessageManager::callAsync ([pd, glist, title, isDirty] {
422- for (auto const & patch : pd->patches ) {
423- if (patch->ptr .getRaw <t_canvas>() == glist) {
424- patch->updateTitle (SmallString (title->s_name ), isDirty);
421+ MessageManager::callAsync ([instance = juce::WeakReference (inst), glist, title, isDirty] {
422+ if (auto * pd = static_cast <PluginProcessor*>(instance.get ())) {
423+ for (auto const & patch : pd->patches ) {
424+ if (patch->ptr .getRaw <t_canvas>() == glist) {
425+ patch->updateTitle (SmallString (title->s_name ), isDirty);
426+ }
425427 }
428+ for (auto * editor : pd->getEditors ())
429+ editor->triggerAsyncUpdate ();
426430 }
427- for (auto * editor : pd->getEditors ())
428- editor->triggerAsyncUpdate ();
429431 });
430432 break ;
431433 }
0 commit comments