diff --git a/modules/juce_gui_extra/juce_gui_extra.cpp b/modules/juce_gui_extra/juce_gui_extra.cpp index b156237314..d78ca23377 100644 --- a/modules/juce_gui_extra/juce_gui_extra.cpp +++ b/modules/juce_gui_extra/juce_gui_extra.cpp @@ -143,6 +143,7 @@ #if JUCE_MAC || JUCE_IOS #if JUCE_MAC + #include "native/juce_mac_NSViewFrameWatcher.h" #include "native/juce_mac_NSViewComponent.mm" #include "native/juce_mac_AppleRemote.mm" #include "native/juce_mac_SystemTrayIcon.cpp" diff --git a/modules/juce_gui_extra/native/juce_mac_NSViewComponent.mm b/modules/juce_gui_extra/native/juce_mac_NSViewComponent.mm index 270f3528e3..0a5ae11fd4 100644 --- a/modules/juce_gui_extra/native/juce_mac_NSViewComponent.mm +++ b/modules/juce_gui_extra/native/juce_mac_NSViewComponent.mm @@ -26,85 +26,6 @@ namespace juce { -JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wundeclared-selector") -static const auto nsViewFrameChangedSelector = @selector (frameChanged:); -JUCE_END_IGNORE_WARNINGS_GCC_LIKE - -struct NSViewCallbackInterface -{ - virtual ~NSViewCallbackInterface() = default; - virtual void frameChanged() = 0; -}; - -//============================================================================== -struct NSViewFrameChangeCallbackClass : public ObjCClass -{ - NSViewFrameChangeCallbackClass() - : ObjCClass ("JUCE_NSViewCallback_") - { - addIvar ("target"); - - addMethod (nsViewFrameChangedSelector, frameChanged); - - registerClass(); - } - - static void setTarget (id self, NSViewCallbackInterface* c) - { - object_setInstanceVariable (self, "target", c); - } - -private: - static void frameChanged (id self, SEL, NSNotification*) - { - if (auto* target = getIvar (self, "target")) - target->frameChanged(); - } - - JUCE_DECLARE_NON_COPYABLE (NSViewFrameChangeCallbackClass) -}; - -//============================================================================== -class NSViewFrameWatcher : private NSViewCallbackInterface -{ -public: - NSViewFrameWatcher (NSView* viewToWatch, std::function viewResizedIn) - : viewResized (std::move (viewResizedIn)), callback (makeCallbackForView (viewToWatch)) - { - } - - ~NSViewFrameWatcher() override - { - [[NSNotificationCenter defaultCenter] removeObserver: callback]; - [callback release]; - callback = nil; - } - - JUCE_DECLARE_NON_COPYABLE (NSViewFrameWatcher) - JUCE_DECLARE_NON_MOVEABLE (NSViewFrameWatcher) - -private: - id makeCallbackForView (NSView* view) - { - static NSViewFrameChangeCallbackClass cls; - auto* result = [cls.createInstance() init]; - NSViewFrameChangeCallbackClass::setTarget (result, this); - - [[NSNotificationCenter defaultCenter] addObserver: result - selector: nsViewFrameChangedSelector - name: NSViewFrameDidChangeNotification - object: view]; - - return result; - } - - void frameChanged() override { viewResized(); } - - std::function viewResized; - id callback; -}; - -//============================================================================== class NSViewAttachment : public ReferenceCountedObject, public ComponentMovementWatcher { diff --git a/modules/juce_gui_extra/native/juce_mac_NSViewFrameWatcher.h b/modules/juce_gui_extra/native/juce_mac_NSViewFrameWatcher.h new file mode 100644 index 0000000000..62f69a5597 --- /dev/null +++ b/modules/juce_gui_extra/native/juce_mac_NSViewFrameWatcher.h @@ -0,0 +1,111 @@ +/* + ============================================================================== + + This file is part of the JUCE library. + Copyright (c) 2020 - Raw Material Software Limited + + JUCE is an open source library subject to commercial or open-source + licensing. + + By using JUCE, you agree to the terms of both the JUCE 6 End-User License + Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020). + + End User License Agreement: www.juce.com/juce-6-licence + Privacy Policy: www.juce.com/juce-privacy-policy + + Or: You may also use this code under the terms of the GPL v3 (see + www.gnu.org/licenses). + + JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER + EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE + DISCLAIMED. + + ============================================================================== +*/ + +#if JUCE_MAC + +namespace juce +{ + +JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wundeclared-selector") +const auto nsViewFrameChangedSelector = @selector (frameChanged:); +JUCE_END_IGNORE_WARNINGS_GCC_LIKE + +struct NSViewCallbackInterface +{ + virtual ~NSViewCallbackInterface() = default; + virtual void frameChanged() = 0; +}; + +//============================================================================== +struct NSViewFrameChangeCallbackClass : public ObjCClass +{ + NSViewFrameChangeCallbackClass() + : ObjCClass ("JUCE_NSViewCallback_") + { + addIvar ("target"); + + addMethod (nsViewFrameChangedSelector, frameChanged); + + registerClass(); + } + + static void setTarget (id self, NSViewCallbackInterface* c) + { + object_setInstanceVariable (self, "target", c); + } + +private: + static void frameChanged (id self, SEL, NSNotification*) + { + if (auto* target = getIvar (self, "target")) + target->frameChanged(); + } + + JUCE_DECLARE_NON_COPYABLE (NSViewFrameChangeCallbackClass) +}; + +//============================================================================== +class NSViewFrameWatcher : private NSViewCallbackInterface +{ +public: + NSViewFrameWatcher (NSView* viewToWatch, std::function viewResizedIn) + : viewResized (std::move (viewResizedIn)), callback (makeCallbackForView (viewToWatch)) + { + } + + ~NSViewFrameWatcher() override + { + [[NSNotificationCenter defaultCenter] removeObserver: callback]; + [callback release]; + callback = nil; + } + + JUCE_DECLARE_NON_COPYABLE (NSViewFrameWatcher) + JUCE_DECLARE_NON_MOVEABLE (NSViewFrameWatcher) + +private: + id makeCallbackForView (NSView* view) + { + static NSViewFrameChangeCallbackClass cls; + auto* result = [cls.createInstance() init]; + NSViewFrameChangeCallbackClass::setTarget (result, this); + + [[NSNotificationCenter defaultCenter] addObserver: result + selector: nsViewFrameChangedSelector + name: NSViewFrameDidChangeNotification + object: view]; + + return result; + } + + void frameChanged() override { viewResized(); } + + std::function viewResized; + id callback; +}; + +} // namespace juce + +#endif