diff --git a/modules/juce_core/memory/juce_WeakReference.h b/modules/juce_core/memory/juce_WeakReference.h index ca3dd1fb27..7f18c18acb 100644 --- a/modules/juce_core/memory/juce_WeakReference.h +++ b/modules/juce_core/memory/juce_WeakReference.h @@ -193,6 +193,15 @@ public: sharedPointer->clearPointer(); } + /** Returns a raw pointer to the underlying object. */ + ObjectType* get() const noexcept + { + if (sharedPointer != nullptr) + return sharedPointer->get(); + + return nullptr; + } + private: SharedRef sharedPointer; diff --git a/modules/juce_gui_basics/components/juce_Component.cpp b/modules/juce_gui_basics/components/juce_Component.cpp index 3f13fbbe9e..f1911a867b 100644 --- a/modules/juce_gui_basics/components/juce_Component.cpp +++ b/modules/juce_gui_basics/components/juce_Component.cpp @@ -710,10 +710,13 @@ void Component::removeFromDesktop() ComponentPeer* const peer = ComponentPeer::getPeerFor (this); jassert (peer != nullptr); - flags.hasHeavyweightPeerFlag = false; - delete peer; + ScopedPointer oldPeerToDelete (peer); + flags.hasHeavyweightPeerFlag = false; Desktop::getInstance().removeDesktopComponent (this); + + if (masterReference.get() != nullptr) + internalHierarchyChanged(); } } diff --git a/modules/juce_gui_basics/native/juce_mac_NSViewComponentPeer.mm b/modules/juce_gui_basics/native/juce_mac_NSViewComponentPeer.mm index 341eb1006f..87afb9f6b3 100644 --- a/modules/juce_gui_basics/native/juce_mac_NSViewComponentPeer.mm +++ b/modules/juce_gui_basics/native/juce_mac_NSViewComponentPeer.mm @@ -672,6 +672,12 @@ public: void redirectPaste (NSObject*) { handleKeyPress (KeyPress ('v', ModifierKeys (ModifierKeys::commandModifier), 'v')); } void redirectCut (NSObject*) { handleKeyPress (KeyPress ('x', ModifierKeys (ModifierKeys::commandModifier), 'x')); } + void redirectWillMoveToWindow (NSWindow* newWindow) + { + if ([view window] == window && newWindow == nullptr) + getComponent().removeFromDesktop(); + } + void sendMouseEvent (NSEvent* ev) { updateModifiers (ev); @@ -1536,6 +1542,8 @@ struct JuceNSViewClass : public ObjCClass addMethod (@selector (copy:), copy, "v@:@"); addMethod (@selector (cut:), cut, "v@:@"); + addMethod (@selector (viewWillMoveToWindow:), willMoveToWindow, "v@:@"); + addProtocol (@protocol (NSTextInput)); registerClass(); @@ -1585,6 +1593,11 @@ private: static void paste (id self, SEL, NSObject* s) { if (NSViewComponentPeer* p = getOwner (self)) p->redirectPaste (s); } static void cut (id self, SEL, NSObject* s) { if (NSViewComponentPeer* p = getOwner (self)) p->redirectCut (s); } + static void willMoveToWindow (id self, SEL, NSWindow* window) + { + if (NSViewComponentPeer* p = getOwner (self)) p->redirectWillMoveToWindow (window); + } + static BOOL acceptsFirstMouse (id, SEL, NSEvent*) { return YES; } static BOOL wantsDefaultClipping (id, SEL) { return YES; } // (this is the default, but may want to customise it in future) static BOOL worksWhenModal (id self, SEL) { if (NSViewComponentPeer* p = getOwner (self)) return p->worksWhenModal(); return NO; };