1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
/*
* SPDX-FileCopyrightText: 2024 Bohdan Onofriichuk <bogdan.onofriuchuk@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#pragma once
#include "screenbrightnessdisplaymodel.h"
#include <QBindable>
#include <QCoroTask>
#include <QModelIndex>
#include <QObject>
#include <QStringList>
#include <QVariantMap>
#include <qqmlregistration.h>
class QDBusPendingCallWatcher;
class QDBusServiceWatcher;
class ScreenBrightnessControl : public QObject
{
Q_OBJECT
QML_ELEMENT
Q_PROPERTY(QAbstractListModel *displays READ displays CONSTANT)
Q_PROPERTY(bool isBrightnessAvailable READ default NOTIFY isBrightnessAvailableChanged BINDABLE bindableIsBrightnessAvailable)
Q_PROPERTY(bool isSilent MEMBER m_isSilent)
public:
enum class StepAction : uint {
Increase = 0,
Decrease = 1,
IncreaseSmall = 2,
DecreaseSmall = 3,
};
Q_ENUM(StepAction)
explicit ScreenBrightnessControl(QObject *parent = nullptr);
~ScreenBrightnessControl() override;
ScreenBrightnessDisplayModel *displays();
const ScreenBrightnessDisplayModel *displays() const;
QBindable<bool> bindableIsBrightnessAvailable();
/**
* Adjust brightness up or down for a predetermined set of displays by a given ratio.
*
* @param delta A ratio between -1.0 and +1.0 of maximum brightness.
*/
Q_INVOKABLE void adjustBrightnessRatio(double delta);
/**
* Adjust brightness up or down for a predetermined set of displays by a given step action.
*
* @param stepAction Size and direction of the brightness adjustment. Resulting brightness will be clamped.
*/
Q_INVOKABLE void adjustBrightnessStep(StepAction stepAction);
public Q_SLOTS:
void setBrightness(const QString &displayName, int value);
Q_SIGNALS:
void isBrightnessAvailableChanged(bool status);
private Q_SLOTS:
void onGlobalPropertiesChanged(const QString &ifaceName, const QVariantMap &changedProps, const QStringList &invalidatedProps);
void onBrightnessChanged(const QString &displayName, int value, const QString &sourceClientName, const QString &sourceClientContext);
void onBrightnessRangeChanged(const QString &displayName, int max, int value);
private:
QCoro::Task<void> onServiceRegistered();
void onServiceUnregistered();
QCoro::Task<bool> queryAndUpdateDisplays();
QCoro::Task<void> queryAndInsertDisplayData(const QString &displayNames);
Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(ScreenBrightnessControl, bool, m_isBrightnessAvailable, false, &ScreenBrightnessControl::isBrightnessAvailableChanged);
ScreenBrightnessDisplayModel m_displays;
QString m_alreadyChangedContext;
std::unique_ptr<QDBusPendingCallWatcher> m_brightnessChangeWatcher;
std::unique_ptr<QDBusServiceWatcher> m_serviceWatcher;
bool m_isSilent = false;
bool m_displaysUpdating = false;
bool m_shouldRecheckDisplays = false;
bool m_serviceRegistered = false;
};
|