diff options
author | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2019-05-29 16:36:09 +0200 |
---|---|---|
committer | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2019-05-29 17:11:40 +0200 |
commit | b53994de5f1b641ca2f6c79048339ff34fe14b55 (patch) | |
tree | 4c6c1fe9cc8ff91a8ac563d06cfbb1edcd5218e7 /src | |
parent | 15d73a9f479e88af85944119e79d88ed26d4f346 (diff) |
macOS: Use QMacNotificationObserver over manual notification handling
This also fixes a bug where we were implicitly capturing this inside
the block, which meant that we would crash if the theme was recreated.
The capture is now tied to the lifetime of QCocoaTheme.
Change-Id: I37df8e6c0b33bf41e76d66be3cf29576041a7546
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/plugins/platforms/cocoa/qcocoatheme.h | 3 | ||||
-rw-r--r-- | src/plugins/platforms/cocoa/qcocoatheme.mm | 6 | ||||
-rw-r--r-- | src/plugins/styles/mac/qmacstyle_mac.mm | 28 |
3 files changed, 17 insertions, 20 deletions
diff --git a/src/plugins/platforms/cocoa/qcocoatheme.h b/src/plugins/platforms/cocoa/qcocoatheme.h index c42fa7d2e8..63227ed6c1 100644 --- a/src/plugins/platforms/cocoa/qcocoatheme.h +++ b/src/plugins/platforms/cocoa/qcocoatheme.h @@ -45,6 +45,8 @@ Q_FORWARD_DECLARE_OBJC_CLASS(QT_MANGLE_NAMESPACE(QCocoaThemeAppAppearanceObserver)); +#include <QtCore/private/qcore_mac_p.h> + QT_BEGIN_NAMESPACE class QPalette; @@ -82,6 +84,7 @@ public: private: mutable QPalette *m_systemPalette; + QMacNotificationObserver m_systemColorObserver; mutable QHash<QPlatformTheme::Palette, QPalette*> m_palettes; mutable QHash<QPlatformTheme::Font, QFont*> m_fonts; QT_MANGLE_NAMESPACE(QCocoaThemeAppAppearanceObserver) *m_appearanceObserver; diff --git a/src/plugins/platforms/cocoa/qcocoatheme.mm b/src/plugins/platforms/cocoa/qcocoatheme.mm index efe670abed..ba93560689 100644 --- a/src/plugins/platforms/cocoa/qcocoatheme.mm +++ b/src/plugins/platforms/cocoa/qcocoatheme.mm @@ -133,10 +133,10 @@ QCocoaTheme::QCocoaTheme() m_appearanceObserver = [[QCocoaThemeAppAppearanceObserver alloc] initWithTheme:this]; #endif - [[NSNotificationCenter defaultCenter] addObserverForName:NSSystemColorsDidChangeNotification - object:nil queue:nil usingBlock:^(NSNotification *) { + m_systemColorObserver = QMacNotificationObserver(nil, + NSSystemColorsDidChangeNotification, [this] { handleSystemThemeChange(); - }]; + }); } QCocoaTheme::~QCocoaTheme() diff --git a/src/plugins/styles/mac/qmacstyle_mac.mm b/src/plugins/styles/mac/qmacstyle_mac.mm index ee5556ec9e..f88e3203bc 100644 --- a/src/plugins/styles/mac/qmacstyle_mac.mm +++ b/src/plugins/styles/mac/qmacstyle_mac.mm @@ -161,18 +161,6 @@ QT_NAMESPACE_ALIAS_OBJC_CLASS(NotificationReceiver); return self; } -- (void)scrollBarStyleDidChange:(NSNotification *)notification -{ - Q_UNUSED(notification); - - // purge destroyed scroll bars: - QMacStylePrivate::scrollBars.removeAll(QPointer<QObject>()); - - QEvent event(QEvent::StyleChange); - for (const auto &o : QMacStylePrivate::scrollBars) - QCoreApplication::sendEvent(o, &event); -} - - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey, id> *)change context:(void *)context { @@ -2095,11 +2083,18 @@ QMacStyle::QMacStyle() Q_D(QMacStyle); QMacAutoReleasePool pool; + static QMacNotificationObserver scrollbarStyleObserver(nil, + NSPreferredScrollerStyleDidChangeNotification, []() { + // Purge destroyed scroll bars + QMacStylePrivate::scrollBars.removeAll(QPointer<QObject>()); + + QEvent event(QEvent::StyleChange); + for (const auto &o : QMacStylePrivate::scrollBars) + QCoreApplication::sendEvent(o, &event); + }); + d->receiver = [[NotificationReceiver alloc] initWithPrivateStyle:d]; - [[NSNotificationCenter defaultCenter] addObserver:d->receiver - selector:@selector(scrollBarStyleDidChange:) - name:NSPreferredScrollerStyleDidChangeNotification - object:nil]; + #if QT_MACOS_PLATFORM_SDK_EQUAL_OR_ABOVE(__MAC_10_14) if (QOperatingSystemVersion::current() >= QOperatingSystemVersion::MacOSMojave) { [NSApplication.sharedApplication addObserver:d->receiver forKeyPath:@"effectiveAppearance" @@ -2113,7 +2108,6 @@ QMacStyle::~QMacStyle() Q_D(QMacStyle); QMacAutoReleasePool pool; - [[NSNotificationCenter defaultCenter] removeObserver:d->receiver]; #if QT_MACOS_PLATFORM_SDK_EQUAL_OR_ABOVE(__MAC_10_14) if (QOperatingSystemVersion::current() >= QOperatingSystemVersion::MacOSMojave) [NSApplication.sharedApplication removeObserver:d->receiver forKeyPath:@"effectiveAppearance"]; |