File indexing completed on 2024-04-28 04:00:33

0001 /*
0002     SPDX-FileCopyrightText: 2023 Fushan Wen <qydwhotmail@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "animationspeedprovider.h"
0008 
0009 #ifdef Q_OS_UNIX
0010 #include <KConfigGroup>
0011 #include <KSharedConfig>
0012 #endif
0013 
0014 #ifdef Q_OS_WIN
0015 #include <QCoreApplication>
0016 #include <Windows.h>
0017 #endif
0018 
0019 using namespace Qt::StringLiterals;
0020 
0021 AnimationSpeedProvider::AnimationSpeedProvider()
0022 {
0023 }
0024 
0025 AnimationSpeedProvider::~AnimationSpeedProvider()
0026 {
0027 }
0028 
0029 QBindable<double> AnimationSpeedProvider::animationSpeedModifier() const
0030 {
0031     return &m_animationSpeedModifier;
0032 }
0033 
0034 #ifdef Q_OS_UNIX
0035 KConfigAnimationSpeedProvider::KConfigAnimationSpeedProvider(QObject *parent)
0036     : QObject(parent)
0037     , AnimationSpeedProvider()
0038     , m_animationSpeedWatcher(KConfigWatcher::create(KSharedConfig::openConfig()))
0039 {
0040     connect(m_animationSpeedWatcher.data(), &KConfigWatcher::configChanged, this, [this](const KConfigGroup &group, const QByteArrayList &names) {
0041         if (group.name() == "KDE"_L1 && names.contains(QByteArrayLiteral("AnimationDurationFactor"))) {
0042             m_animationSpeedModifier = std::max<double>(0.0, group.readEntry(u"AnimationDurationFactor"_s, 1.0));
0043         }
0044     });
0045 
0046     KConfigGroup generalCfg = KConfigGroup(KSharedConfig::openConfig(), u"KDE"_s);
0047     m_animationSpeedModifier = std::max<double>(0.0, generalCfg.readEntry(u"AnimationDurationFactor"_s, 1.0));
0048 }
0049 
0050 KConfigAnimationSpeedProvider::~KConfigAnimationSpeedProvider()
0051 {
0052 }
0053 #endif
0054 
0055 #ifdef Q_OS_WIN
0056 WindowsAnimationSpeedProvider::WindowsAnimationSpeedProvider()
0057     : QAbstractNativeEventFilter()
0058     , AnimationSpeedProvider()
0059 {
0060     update();
0061     QCoreApplication::instance()->installNativeEventFilter(this);
0062 }
0063 
0064 WindowsAnimationSpeedProvider::~WindowsAnimationSpeedProvider()
0065 {
0066 }
0067 
0068 void WindowsAnimationSpeedProvider::update()
0069 {
0070     bool isAnimated = true;
0071     if (SystemParametersInfoW(SPI_GETCLIENTAREAANIMATION, 0, &isAnimated, 0)) {
0072         m_animationSpeedModifier = isAnimated ? 1.0 : 0.0;
0073     }
0074 }
0075 
0076 bool WindowsAnimationSpeedProvider::nativeEventFilter(const QByteArray &eventType, void *message, qintptr *)
0077 {
0078     if (eventType != "windows_generic_MSG") {
0079         return false;
0080     }
0081 
0082     MSG *msg = static_cast<MSG *>(message);
0083     if (msg->message != WM_SETTINGCHANGE || msg->wParam != SPI_SETCLIENTAREAANIMATION) {
0084         return false;
0085     }
0086 
0087     update();
0088     return false;
0089 }
0090 #endif