diff --git a/modules/juce_core/native/juce_ObjCHelpers_mac.h b/modules/juce_core/native/juce_ObjCHelpers_mac.h index 7f2c9c5700..aa4e45d218 100644 --- a/modules/juce_core/native/juce_ObjCHelpers_mac.h +++ b/modules/juce_core/native/juce_ObjCHelpers_mac.h @@ -585,6 +585,63 @@ private: Class klass = nullptr; }; +//============================================================================== +/* + Forwards NSNotificationCenter callbacks to a std::function. +*/ +class FunctionNotificationCenterObserver +{ +public: + FunctionNotificationCenterObserver (NSNotificationName notificationName, + id objectToObserve, + std::function callback) + : onNotification (std::move (callback)), + observer (observerObject.get(), getSelector(), notificationName, objectToObserve) + {} + +private: + static SEL getSelector() + { + JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wundeclared-selector") + return @selector (notificationFired:); + JUCE_END_IGNORE_WARNINGS_GCC_LIKE + } + + std::function onNotification; + + NSUniquePtr observerObject + { + [this] + { + static auto observerClass = std::invoke ([] + { + ObjCClass k { "JUCEObserverClass_" }; + + k.addIvar ("owner"); + + k.addMethod (getSelector(), [] (id self, SEL, NSNotification*) + { + getIvar (self, "owner")->onNotification(); + }); + + k.registerClass(); + + return k; + }); + auto* result = observerClass.createInstance(); + object_setInstanceVariable (result, "owner", this); + return result; + }() + }; + + ScopedNotificationCenterObserver observer; + + // Instances can't be copied or moved, because 'this' is stored as a member of the ObserverClass + // object. + JUCE_DECLARE_NON_COPYABLE (FunctionNotificationCenterObserver) + JUCE_DECLARE_NON_MOVEABLE (FunctionNotificationCenterObserver) +}; + #if JUCE_IOS // Defines a function that will check the requested version both at diff --git a/modules/juce_gui_basics/native/juce_PerScreenDisplayLinks_mac.h b/modules/juce_gui_basics/native/juce_PerScreenDisplayLinks_mac.h index dcea884cf9..983a772317 100644 --- a/modules/juce_gui_basics/native/juce_PerScreenDisplayLinks_mac.h +++ b/modules/juce_gui_basics/native/juce_PerScreenDisplayLinks_mac.h @@ -35,69 +35,6 @@ namespace juce { -//============================================================================== -/* - Forwards NSNotificationCenter callbacks to a std::function. -*/ -class FunctionNotificationCenterObserver -{ -public: - FunctionNotificationCenterObserver (NSNotificationName notificationName, - id objectToObserve, - std::function callback) - : onNotification (std::move (callback)), - observer (observerObject.get(), getSelector(), notificationName, objectToObserve) - {} - -private: - struct ObserverClass - { - ObserverClass() - { - klass.addIvar ("owner"); - - klass.addMethod (getSelector(), [] (id self, SEL, NSNotification*) - { - getIvar (self, "owner")->onNotification(); - }); - - klass.registerClass(); - } - - NSObject* createInstance() const { return klass.createInstance(); } - - private: - ObjCClass klass { "JUCEObserverClass_" }; - }; - - static SEL getSelector() - { - JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wundeclared-selector") - return @selector (notificationFired:); - JUCE_END_IGNORE_WARNINGS_GCC_LIKE - } - - std::function onNotification; - - NSUniquePtr observerObject - { - [this] - { - static ObserverClass observerClass; - auto* result = observerClass.createInstance(); - object_setInstanceVariable (result, "owner", this); - return result; - }() - }; - - ScopedNotificationCenterObserver observer; - - // Instances can't be copied or moved, because 'this' is stored as a member of the ObserverClass - // object. - JUCE_DECLARE_NON_COPYABLE (FunctionNotificationCenterObserver) - JUCE_DECLARE_NON_MOVEABLE (FunctionNotificationCenterObserver) -}; - //============================================================================== /* Manages the lifetime of a CVDisplayLinkRef for a single display, and automatically starts and