KGuiAddons

kcolorschemewatcher_mac.mm
1/*
2 * SPDX-FileCopyrightText: 2022 Jyrki Gadinger <nilsding@nilsding.org>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7#include "kcolorschemewatcher_mac.h"
8
9#import <AppKit/AppKit.h>
10
11#include <QTimer>
12
13KColorSchemeWatcherMac::KColorSchemeWatcherMac()
14{
15 // subscribe to the distributed notification centre
16 id notificationCenter = NSDistributedNotificationCenter.defaultCenter;
17 m_observer = [notificationCenter addObserverForName:@"AppleInterfaceThemeChangedNotification"
18 object:nil
19 queue:nil
20 usingBlock:^(NSNotification *) {
21 // "fun" workaround to not emit the signal immediately after receiving the notification.
22 // for some reason NSAppearance.currentDrawingAppearance is still set to the old value here, after a short
23 // delay it is updated correctly
24 QTimer::singleShot(0, [this]() {
25 Q_EMIT systemPreferenceChanged();
26 });
27 }];
28}
29
30KColorSchemeWatcherMac::~KColorSchemeWatcherMac()
31{
32 [NSDistributedNotificationCenter.defaultCenter removeObserver:static_cast<id>(m_observer)];
33}
34
35KColorSchemeWatcher::ColorPreference KColorSchemeWatcherMac::systemPreference() const
36{
37 NSAppearance *appearance = nullptr;
38
39 if (@available(macOS 11.0, *)) {
40 appearance = NSAppearance.currentDrawingAppearance;
41 } else if (@available(macOS 10.14, *)) {
42 appearance = NSAppearance.currentAppearance;
43 } else {
44 // macOS < 10.14 (Mojave) does not support a light/dark mode switch, always prefer light mode
46 }
47
48 return appearance.name == NSAppearanceNameDarkAqua ? KColorSchemeWatcher::PreferDark : KColorSchemeWatcher::PreferLight;
49}
50
51#include "moc_kcolorschemewatcher_mac.cpp"
@ PreferLight
The user prefers a dark color scheme.
@ PreferDark
No preference available.
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:09:48 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.