From 2d9531b2a63d0c791fae60204841b13e104c492e Mon Sep 17 00:00:00 2001 From: wyu <97944796+wyu71@users.noreply.github.com> Date: Tue, 14 Jan 2025 18:50:38 +0800 Subject: [PATCH] chore: add mini mode special handling configuration (#559) - Add org_deepin_movie_minimode class for mini mode handling - Add miniModeSpecialHandling property and related signals - Implement DConfig integration for configuration persistence Log: add mini mode special handling configuration --- .../json/org_deepin_movie_minimode.hpp | 174 ++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 src/configs/json/org_deepin_movie_minimode.hpp diff --git a/src/configs/json/org_deepin_movie_minimode.hpp b/src/configs/json/org_deepin_movie_minimode.hpp new file mode 100644 index 00000000..ceadba25 --- /dev/null +++ b/src/configs/json/org_deepin_movie_minimode.hpp @@ -0,0 +1,174 @@ +// SPDX-FileCopyrightText: 2024 - 2025 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef ORG_DEEPIN_MOVIE_MINIMODE_H +#define ORG_DEEPIN_MOVIE_MINIMODE_H + +#include +#include +#include +#include +#include +#include + +class org_deepin_movie_minimode : public QObject { + Q_OBJECT + + Q_PROPERTY(double miniModeSpecialHandling READ miniModeSpecialHandling WRITE setMiniModeSpecialHandling NOTIFY miniModeSpecialHandlingChanged) +public: + explicit org_deepin_movie_minimode(QThread *thread, const QString &appId, const QString &name, const QString &subpath, QObject *parent = nullptr) + : QObject(parent) { + + if (!thread->isRunning()) { + qWarning() << QStringLiteral("Warning: The provided thread is not running."); + } + Q_ASSERT(QThread::currentThread() != thread); + auto worker = new QObject(); + worker->moveToThread(thread); + QMetaObject::invokeMethod(worker, [=]() { + auto config = DTK_CORE_NAMESPACE::DConfig::create(appId, name, subpath, nullptr); + if (!config) { + qWarning() << QStringLiteral("Failed to create DConfig instance."); + worker->deleteLater(); + return; + } + config->moveToThread(QThread::currentThread()); + initialize(config); + worker->deleteLater(); + }); + } + explicit org_deepin_movie_minimode(QThread *thread, DTK_CORE_NAMESPACE::DConfigBackend *backend, const QString &appId, const QString &name, const QString &subpath, QObject *parent = nullptr) + : QObject(parent) { + + if (!thread->isRunning()) { + qWarning() << QStringLiteral("Warning: The provided thread is not running."); + } + Q_ASSERT(QThread::currentThread() != thread); + auto worker = new QObject(); + worker->moveToThread(thread); + QMetaObject::invokeMethod(worker, [=]() { + auto config = DTK_CORE_NAMESPACE::DConfig::create(backend, appId, name, subpath, nullptr); + if (!config) { + qWarning() << QStringLiteral("Failed to create DConfig instance."); + worker->deleteLater(); + return; + } + config->moveToThread(QThread::currentThread()); + initialize(config); + worker->deleteLater(); + }); + } + explicit org_deepin_movie_minimode(QThread *thread, const QString &name, const QString &subpath, QObject *parent = nullptr) + : QObject(parent) { + + if (!thread->isRunning()) { + qWarning() << QStringLiteral("Warning: The provided thread is not running."); + } + Q_ASSERT(QThread::currentThread() != thread); + auto worker = new QObject(); + worker->moveToThread(thread); + QMetaObject::invokeMethod(worker, [=]() { + auto config = DTK_CORE_NAMESPACE::DConfig::create(name, subpath, nullptr); + if (!config) { + qWarning() << QStringLiteral("Failed to create DConfig instance."); + worker->deleteLater(); + return; + } + config->moveToThread(QThread::currentThread()); + initialize(config); + worker->deleteLater(); + }); + } + explicit org_deepin_movie_minimode(QThread *thread, DTK_CORE_NAMESPACE::DConfigBackend *backend, const QString &name, const QString &subpath, QObject *parent = nullptr) + : QObject(parent) { + + if (!thread->isRunning()) { + qWarning() << QStringLiteral("Warning: The provided thread is not running."); + } + Q_ASSERT(QThread::currentThread() != thread); + auto worker = new QObject(); + worker->moveToThread(thread); + QMetaObject::invokeMethod(worker, [=]() { + auto config = DTK_CORE_NAMESPACE::DConfig::create(backend, name, subpath, nullptr); + if (!config) { + qWarning() << QStringLiteral("Failed to create DConfig instance."); + worker->deleteLater(); + return; + } + config->moveToThread(QThread::currentThread()); + initialize(config); + worker->deleteLater(); + }); + } + ~org_deepin_movie_minimode() { + if (m_config.loadRelaxed()) { + m_config.loadRelaxed()->deleteLater(); + } + } + + double miniModeSpecialHandling() const { + return p_miniModeSpecialHandling; + } + void setMiniModeSpecialHandling(const double &value) { + auto oldValue = p_miniModeSpecialHandling; + p_miniModeSpecialHandling = value; + markPropertySet(0); + if (auto config = m_config.loadRelaxed()) { + QMetaObject::invokeMethod(config, [this, value]() { + m_config.loadRelaxed()->setValue(QStringLiteral("miniModeSpecialHandling"), value); + }); + } + if (p_miniModeSpecialHandling != oldValue) { + Q_EMIT miniModeSpecialHandlingChanged(); + } + } +Q_SIGNALS: + void miniModeSpecialHandlingChanged(); +private: + void initialize(DTK_CORE_NAMESPACE::DConfig *config) { + Q_ASSERT(!m_config.loadRelaxed()); + m_config.storeRelaxed(config); + if (testPropertySet(0)) { + config->setValue(QStringLiteral("miniModeSpecialHandling"), QVariant::fromValue(p_miniModeSpecialHandling)); + } else { + updateValue(QStringLiteral("miniModeSpecialHandling"), QVariant::fromValue(p_miniModeSpecialHandling)); + } + + connect(config, &DTK_CORE_NAMESPACE::DConfig::valueChanged, this, [this](const QString &key) { + updateValue(key); + }, Qt::DirectConnection); + } + void updateValue(const QString &key, const QVariant &fallback = QVariant()) { + Q_ASSERT(QThread::currentThread() == m_config.loadRelaxed()->thread()); + const QVariant &value = m_config.loadRelaxed()->value(key, fallback); + if (key == QStringLiteral("miniModeSpecialHandling")) { + auto newValue = qvariant_cast(value); + QMetaObject::invokeMethod(this, [this, newValue]() { + if (p_miniModeSpecialHandling != newValue) { + p_miniModeSpecialHandling = newValue; + Q_EMIT miniModeSpecialHandlingChanged(); + } + }); + return; + } + } + inline void markPropertySet(const int index) { + if (index < 32) { + m_propertySetStatus0.fetchAndOrOrdered(1 << (index - 0)); + return; + } + Q_UNREACHABLE(); + } + inline bool testPropertySet(const int index) const { + if (index < 32) { + return (m_propertySetStatus0.loadRelaxed() & (1 << (index - 0))); + } + Q_UNREACHABLE(); + } + QAtomicPointer m_config = nullptr; + double p_miniModeSpecialHandling { -1 }; + QAtomicInteger m_propertySetStatus0 = 0; +}; + +#endif // ORG_DEEPIN_MOVIE_MINIMODE_H