mac: fix build

This commit is contained in:
Yuhang Zhao 2022-09-10 17:56:39 +08:00
parent f2affbca85
commit 9cf8ba959d
1 changed files with 36 additions and 26 deletions

View File

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