diff options
author | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2019-05-29 17:00:40 +0200 |
---|---|---|
committer | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2019-06-07 11:44:57 +0200 |
commit | 2dfe0f8f6854f518e8ffb23e17cc99400355cc3e (patch) | |
tree | 0c536f7838e57f4e11d626ff9fe5c004d479414a /src/plugins/styles/mac | |
parent | 99636127d10f96d8313bc26030cabf9841381914 (diff) |
macOS: Introduce QMacKeyValueObserver and use in theme and style
Change-Id: I2d21c883628933543ae5a66b694ff7503119bc4a
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
Diffstat (limited to 'src/plugins/styles/mac')
-rw-r--r-- | src/plugins/styles/mac/qmacstyle_mac.mm | 55 | ||||
-rw-r--r-- | src/plugins/styles/mac/qmacstyle_mac_p_p.h | 4 |
2 files changed, 9 insertions, 50 deletions
diff --git a/src/plugins/styles/mac/qmacstyle_mac.mm b/src/plugins/styles/mac/qmacstyle_mac.mm index f88e3203bc..c1f88149bb 100644 --- a/src/plugins/styles/mac/qmacstyle_mac.mm +++ b/src/plugins/styles/mac/qmacstyle_mac.mm @@ -144,41 +144,6 @@ static QWindow *qt_getWindow(const QWidget *widget) return widget ? widget->window()->windowHandle() : 0; } -@interface QT_MANGLE_NAMESPACE(NotificationReceiver) : NSObject -@end - -QT_NAMESPACE_ALIAS_OBJC_CLASS(NotificationReceiver); - -@implementation NotificationReceiver -{ - QMacStylePrivate *privateStyle; -} - -- (instancetype)initWithPrivateStyle:(QMacStylePrivate *)style -{ - if (self = [super init]) - privateStyle = style; - return self; -} - -- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object - change:(NSDictionary<NSKeyValueChangeKey, id> *)change context:(void *)context -{ - Q_UNUSED(keyPath); - Q_UNUSED(object); - Q_UNUSED(change); - Q_UNUSED(context); - - Q_ASSERT([keyPath isEqualToString:@"effectiveAppearance"]); - Q_ASSERT(object == NSApp); - - for (NSView *b : privateStyle->cocoaControls) - [b release]; - privateStyle->cocoaControls.clear(); -} - -@end - @interface QT_MANGLE_NAMESPACE(QIndeterminateProgressIndicator) : NSProgressIndicator @property (readonly, nonatomic) NSInteger animators; @@ -2080,7 +2045,6 @@ void QMacStylePrivate::resolveCurrentNSView(QWindow *window) const QMacStyle::QMacStyle() : QCommonStyle(*new QMacStylePrivate) { - Q_D(QMacStyle); QMacAutoReleasePool pool; static QMacNotificationObserver scrollbarStyleObserver(nil, @@ -2093,26 +2057,21 @@ QMacStyle::QMacStyle() QCoreApplication::sendEvent(o, &event); }); - d->receiver = [[NotificationReceiver alloc] initWithPrivateStyle:d]; - #if QT_MACOS_PLATFORM_SDK_EQUAL_OR_ABOVE(__MAC_10_14) + Q_D(QMacStyle); + // FIXME: Tie this logic into theme change, or even polish/unpolish if (QOperatingSystemVersion::current() >= QOperatingSystemVersion::MacOSMojave) { - [NSApplication.sharedApplication addObserver:d->receiver forKeyPath:@"effectiveAppearance" - options:NSKeyValueObservingOptionNew context:nullptr]; + d->appearanceObserver = QMacKeyValueObserver(NSApp, @"effectiveAppearance", [&d] { + for (NSView *b : d->cocoaControls) + [b release]; + d->cocoaControls.clear(); + }); } #endif } QMacStyle::~QMacStyle() { - Q_D(QMacStyle); - QMacAutoReleasePool pool; - -#if QT_MACOS_PLATFORM_SDK_EQUAL_OR_ABOVE(__MAC_10_14) - if (QOperatingSystemVersion::current() >= QOperatingSystemVersion::MacOSMojave) - [NSApplication.sharedApplication removeObserver:d->receiver forKeyPath:@"effectiveAppearance"]; -#endif - [d->receiver release]; } void QMacStyle::polish(QPalette &) diff --git a/src/plugins/styles/mac/qmacstyle_mac_p_p.h b/src/plugins/styles/mac/qmacstyle_mac_p_p.h index dd99cf4bb5..6b3f525adc 100644 --- a/src/plugins/styles/mac/qmacstyle_mac_p_p.h +++ b/src/plugins/styles/mac/qmacstyle_mac_p_p.h @@ -149,7 +149,6 @@ Q_FORWARD_DECLARE_MUTABLE_CG_TYPE(CGContext); Q_FORWARD_DECLARE_OBJC_CLASS(NSView); Q_FORWARD_DECLARE_OBJC_CLASS(NSCell); -Q_FORWARD_DECLARE_OBJC_CLASS(QT_MANGLE_NAMESPACE(NotificationReceiver)); QT_BEGIN_NAMESPACE @@ -288,13 +287,14 @@ public: static QVector<QPointer<QObject> > scrollBars; mutable QPointer<QFocusFrame> focusWidget; - QT_MANGLE_NAMESPACE(NotificationReceiver) *receiver; mutable NSView *backingStoreNSView; mutable QHash<CocoaControl, NSView *> cocoaControls; mutable QHash<CocoaControl, NSCell *> cocoaCells; QFont smallSystemFont; QFont miniSystemFont; + + QMacKeyValueObserver appearanceObserver; }; QT_END_NAMESPACE |