diff --git a/src/core/utils_mac.mm b/src/core/utils_mac.mm index 6eb213b..2649140 100644 --- a/src/core/utils_mac.mm +++ b/src/core/utils_mac.mm @@ -24,6 +24,7 @@ #include "utils.h" #include "framelessmanager.h" +#include "framelessmanager_p.h" #include "framelessconfig_p.h" #include #include @@ -43,6 +44,25 @@ QT_BEGIN_NAMESPACE [[nodiscard]] Q_GUI_EXPORT QColor qt_mac_toQColor(const NSColor *color); // Since 5.8 QT_END_NAMESPACE +FRAMELESSHELPER_BEGIN_NAMESPACE +using Callback = std::function; +FRAMELESSHELPER_END_NAMESPACE + +@interface MyKeyValueObserver : NSObject +@end + +@implementation MyKeyValueObserver +- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object + change:(NSDictionary *)change context:(void *)context +{ + Q_UNUSED(keyPath); + Q_UNUSED(object); + Q_UNUSED(change); + + (*reinterpret_cast(context))(); +} +@end + FRAMELESSHELPER_BEGIN_NAMESPACE Q_LOGGING_CATEGORY(lcUtilsMac, "wangwenx190.framelesshelper.core.utils.mac") @@ -53,8 +73,6 @@ Q_LOGGING_CATEGORY(lcUtilsMac, "wangwenx190.framelesshelper.core.utils.mac") using namespace Global; -using Callback = std::function; - class MacOSNotificationObserver { Q_DISABLE_COPY_MOVE(MacOSNotificationObserver) @@ -73,6 +91,8 @@ public: ]; } + explicit MacOSNotificationObserver() = default; + ~MacOSNotificationObserver() { remove(); @@ -91,21 +111,6 @@ private: NSObject *observer = nil; }; -@interface KeyValueObserver : NSObject -@end - -@implementation KeyValueObserver -- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object - change:(NSDictionary *)change context:(void *)context -{ - Q_UNUSED(keyPath); - Q_UNUSED(object); - Q_UNUSED(change); - - (*reinterpret_cast(context))(); -} -@end - class MacOSKeyValueObserver { Q_DISABLE_COPY_MOVE(MacOSKeyValueObserver) @@ -127,6 +132,8 @@ public: addObserver(options); } + explicit MacOSKeyValueObserver() = default; + ~MacOSKeyValueObserver() { removeObserver(); @@ -152,7 +159,7 @@ private: NSString *keyPath = nil; QScopedPointer callback; - static inline KeyValueObserver *observer = [[KeyValueObserver alloc] init]; + static inline MyKeyValueObserver *observer = [[MyKeyValueObserver alloc] init]; }; class MacOSThemeObserver @@ -172,13 +179,16 @@ public: static const bool isMojave = (QSysInfo::macVersion() > QSysInfo::MV_SIERRA); #endif if (isMojave) { - m_appearanceObserver = MacOSKeyValueObserver(NSApp, @"effectiveAppearance", [](){ - NSAppearance.currentAppearance = NSApp.effectiveAppearance; + m_appearanceObserver.reset(new MacOSKeyValueObserver(NSApp, @"effectiveAppearance", [](){ + QT_WARNING_PUSH + QT_WARNING_DISABLE_DEPRECATED + NSAppearance.currentAppearance = NSApp.effectiveAppearance; // FIXME: use latest API. + QT_WARNING_POP MacOSThemeObserver::notifySystemThemeChange(); - }); + })); } - m_systemColorObserver = MacOSNotificationObserver(nil, NSSystemColorsDidChangeNotification, - [](){ MacOSThemeObserver::notifySystemThemeChange(); }); + m_systemColorObserver.reset(new MacOSNotificationObserver(nil, NSSystemColorsDidChangeNotification, + [](){ MacOSThemeObserver::notifySystemThemeChange(); })); } ~MacOSThemeObserver() = default; @@ -194,9 +204,9 @@ public: } private: - MacOSNotificationObserver m_systemColorObserver; - MacOSKeyValueObserver m_appearanceObserver; -} + QScopedPointer m_systemColorObserver; + QScopedPointer m_appearanceObserver; +}; class NSWindowProxy : public QObject {