From cd02c548f4ee06196cdfb5aef7441f56e44045e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20Slav=C3=ADk?= Date: Tue, 12 Jun 2018 20:27:50 +0200 Subject: [PATCH] Implement wxEVT_SYS_COLOUR_CHANGED in wxOSX (#832) Starting with macOS 10.14 Mojave, system colors can change dynamically when the user switches between light and dark modes. Detect this by observing the effectiveAppearance property and emit wxSysColourChangedEvent accordingly. See #18146. --- src/osx/cocoa/nonownedwnd.mm | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/osx/cocoa/nonownedwnd.mm b/src/osx/cocoa/nonownedwnd.mm index a5eed3e9bb..9a7bb661e6 100644 --- a/src/osx/cocoa/nonownedwnd.mm +++ b/src/osx/cocoa/nonownedwnd.mm @@ -305,6 +305,8 @@ static NSResponder* s_formerFirstResponder = NULL; // controller // +static void *EffectiveAppearanceContext = &EffectiveAppearanceContext; + @interface wxNonOwnedWindowController : NSObject { } @@ -610,6 +612,32 @@ extern int wxOSXGetIdFromSelector(SEL action ); [view setFrameSize: expectedframerect.size]; } } + +- (void)addObservers:(NSWindow*)win +{ + [win addObserver:self forKeyPath:@"effectiveAppearance" + options:0 context:EffectiveAppearanceContext]; +} + +- (void)removeObservers:(NSWindow*)win +{ + [win removeObserver:self forKeyPath:@"effectiveAppearance" context:EffectiveAppearanceContext]; +} + +- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context +{ + if (context == EffectiveAppearanceContext) + { + wxNonOwnedWindowCocoaImpl* windowimpl = [(NSWindow*)object WX_implementation]; + wxNonOwnedWindow* wxpeer = windowimpl ? windowimpl->GetWXPeer() : NULL; + if (wxpeer) + { + wxSysColourChangedEvent event; + event.SetEventObject(wxpeer); + wxpeer->HandleWindowEvent(event); + } + } +} @end @@ -632,6 +660,7 @@ wxNonOwnedWindowCocoaImpl::~wxNonOwnedWindowCocoaImpl() { if ( !m_wxPeer->IsNativeWindowWrapper() ) { + [(wxNonOwnedWindowController*)[m_macWindow delegate] removeObservers:m_macWindow]; [m_macWindow setDelegate:nil]; // make sure we remove this first, otherwise the ref count will not lead to the @@ -648,6 +677,7 @@ void wxNonOwnedWindowCocoaImpl::WillBeDestroyed() { if ( !m_wxPeer->IsNativeWindowWrapper() ) { + [(wxNonOwnedWindowController*)[m_macWindow delegate] removeObservers:m_macWindow]; [m_macWindow setDelegate:nil]; } } @@ -738,6 +768,7 @@ long style, long extraStyle, const wxString& WXUNUSED(name) ) [m_macWindow setLevel:m_macWindowLevel]; [m_macWindow setDelegate:controller]; + [controller addObservers:m_macWindow]; if ( ( style & wxFRAME_SHAPED) ) {