Warning, /frameworks/kguiaddons/src/colors/kcolorschemewatcher_mac.mm is written in an unsupported language. File is not indexed.

0001 /*
0002  * SPDX-FileCopyrightText: 2022 Georg Gadinger <nilsding@nilsding.org>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #include "kcolorschemewatcher_mac.h"
0008 
0009 #import <AppKit/AppKit.h>
0010 
0011 #include <QTimer>
0012 
0013 KColorSchemeWatcherMac::KColorSchemeWatcherMac()
0014 {
0015     // subscribe to the distributed notification centre
0016     id notificationCenter = NSDistributedNotificationCenter.defaultCenter;
0017     m_observer = [notificationCenter addObserverForName:@"AppleInterfaceThemeChangedNotification"
0018                                                  object:nil
0019                                                   queue:nil
0020                                              usingBlock:^(NSNotification *) {
0021                                                  // "fun" workaround to not emit the signal immediately after receiving the notification.
0022                                                  // for some reason NSAppearance.currentDrawingAppearance is still set to the old value here, after a short
0023                                                  // delay it is updated correctly
0024                                                  QTimer::singleShot(0, [this]() {
0025                                                      Q_EMIT systemPreferenceChanged();
0026                                                  });
0027                                              }];
0028 }
0029 
0030 KColorSchemeWatcherMac::~KColorSchemeWatcherMac()
0031 {
0032     [NSDistributedNotificationCenter.defaultCenter removeObserver:static_cast<id>(m_observer)];
0033 }
0034 
0035 KColorSchemeWatcher::ColorPreference KColorSchemeWatcherMac::systemPreference() const
0036 {
0037     NSAppearance *appearance = nullptr;
0038 
0039     if (@available(macOS 11.0, *)) {
0040         appearance = NSAppearance.currentDrawingAppearance;
0041     } else if (@available(macOS 10.14, *)) {
0042         appearance = NSAppearance.currentAppearance;
0043     } else {
0044         // macOS < 10.14 (Mojave) does not support a light/dark mode switch, always prefer light mode
0045         return KColorSchemeWatcher::PreferLight;
0046     }
0047 
0048     return appearance.name == NSAppearanceNameDarkAqua ? KColorSchemeWatcher::PreferDark : KColorSchemeWatcher::PreferLight;
0049 }
0050 
0051 #include "moc_kcolorschemewatcher_mac.cpp"