Rewrite the manager responsible for handling automatic hyprsunset temperature activation as a qml plugin #117
+11
-59
@@ -2,12 +2,13 @@ pragma Singleton
|
||||
|
||||
import Quickshell
|
||||
import QtQuick
|
||||
import ZShell.Services
|
||||
import qs.Config
|
||||
|
||||
Singleton {
|
||||
id: root
|
||||
|
||||
property bool enabled
|
||||
readonly property bool enabled: service.enabled
|
||||
readonly property int end: Config.general.color.scheduleHyprsunsetEnd
|
||||
property bool manualToggle: false
|
||||
readonly property int start: Config.general.color.scheduleHyprsunsetStart
|
||||
@@ -17,69 +18,20 @@ Singleton {
|
||||
if (!Config.general.color.scheduleHyprsunset)
|
||||
return;
|
||||
|
||||
var now = new Date();
|
||||
if (now.getHours() >= root.start || now.getHours() < root.end) {
|
||||
root.startNightLight(root.temp);
|
||||
} else {
|
||||
root.stopNightLight();
|
||||
}
|
||||
}
|
||||
|
||||
function startNightLight(temp: int): void {
|
||||
Quickshell.execDetached(["hyprctl", "hyprsunset", "temperature", `${temp}`]);
|
||||
root.enabled = true;
|
||||
}
|
||||
|
||||
function stopNightLight(): void {
|
||||
Quickshell.execDetached(["hyprctl", "hyprsunset", "identity"]);
|
||||
root.enabled = false;
|
||||
service.apply();
|
||||
}
|
||||
|
||||
function toggleNightLight(): void {
|
||||
if (enabled)
|
||||
stopNightLight();
|
||||
else
|
||||
startNightLight(temp);
|
||||
service.manualToggle = true;
|
||||
service.toggle();
|
||||
}
|
||||
|
||||
onManualToggleChanged: {
|
||||
if (root.manualToggle)
|
||||
manualTimer.start();
|
||||
}
|
||||
HyprsunsetManager {
|
||||
id: service
|
||||
|
||||
Timer {
|
||||
id: manualTimer
|
||||
|
||||
interval: 60000 * 60
|
||||
repeat: false
|
||||
running: false
|
||||
|
||||
onTriggered: {
|
||||
root.manualToggle = false;
|
||||
}
|
||||
}
|
||||
|
||||
Timer {
|
||||
interval: 5000
|
||||
repeat: true
|
||||
running: true
|
||||
triggeredOnStart: true
|
||||
|
||||
onTriggered: {
|
||||
if (!Config.general.color.scheduleHyprsunset)
|
||||
return;
|
||||
|
||||
if (root.manualToggle)
|
||||
return;
|
||||
|
||||
var now = new Date();
|
||||
if (now.getHours() >= root.start || now.getHours() < root.end) {
|
||||
if (!root.enabled)
|
||||
root.startNightLight(root.temp);
|
||||
} else {
|
||||
if (root.enabled)
|
||||
root.stopNightLight();
|
||||
}
|
||||
}
|
||||
activeAuto: Config.general.color.scheduleHyprsunset
|
||||
endTime: root.end
|
||||
startTime: root.start
|
||||
temp: root.temp
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ qml_module(ZShell-services
|
||||
cavaprovider.hpp cavaprovider.cpp
|
||||
desktopmodel.hpp desktopmodel.cpp
|
||||
desktopstatemanager.hpp desktopstatemanager.cpp
|
||||
hyprsunsetmanager.hpp hyprsunsetmanager.cpp
|
||||
LIBRARIES
|
||||
Qt6::Core
|
||||
Qt6::Qml
|
||||
|
||||
@@ -0,0 +1,145 @@
|
||||
#include "hyprsunsetmanager.hpp"
|
||||
#include <qlogging.h>
|
||||
#include <qobject.h>
|
||||
#include <qprocess.h>
|
||||
#include <qtimer.h>
|
||||
|
||||
namespace ZShell::services {
|
||||
|
||||
HyprsunsetManager::HyprsunsetManager(QObject* parent) : QObject(parent) {
|
||||
connect(&m_timer, &QTimer::timeout, this, &HyprsunsetManager::apply);
|
||||
connect(&m_manualTimer, &QTimer::timeout, this, [this] {
|
||||
m_manualToggle = false;
|
||||
emit manualToggleChanged();
|
||||
apply();
|
||||
});
|
||||
connect(&m_startCooldown, &QTimer::timeout, this, [this] {
|
||||
m_startAllowed = true;
|
||||
apply();
|
||||
});
|
||||
|
||||
m_startCooldown.start(2000);
|
||||
m_manualTimer.setSingleShot(true);
|
||||
m_timer.start(60000);
|
||||
|
||||
m_process.setStandardInputFile(QProcess::nullDevice());
|
||||
m_process.setStandardOutputFile(QProcess::nullDevice());
|
||||
|
||||
apply();
|
||||
}
|
||||
|
||||
int HyprsunsetManager::startTime() const {
|
||||
return m_startTime;
|
||||
}
|
||||
|
||||
int HyprsunsetManager::endTime() const {
|
||||
return m_endTime;
|
||||
}
|
||||
|
||||
bool HyprsunsetManager::manualToggle() const {
|
||||
return m_manualToggle;
|
||||
}
|
||||
|
||||
bool HyprsunsetManager::enabled() const {
|
||||
return m_enabled;
|
||||
}
|
||||
|
||||
bool HyprsunsetManager::activeAuto() const {
|
||||
return m_activeAuto;
|
||||
}
|
||||
|
||||
int HyprsunsetManager::temp() const {
|
||||
return m_temp;
|
||||
}
|
||||
|
||||
void HyprsunsetManager::setActiveAuto(bool activeAuto) {
|
||||
if (activeAuto == m_activeAuto)
|
||||
return;
|
||||
|
||||
m_activeAuto = activeAuto;
|
||||
emit activeAutoChanged();
|
||||
}
|
||||
|
||||
void HyprsunsetManager::setManualToggle(bool toggle) {
|
||||
if (toggle == m_manualToggle)
|
||||
return;
|
||||
|
||||
m_manualToggle = toggle;
|
||||
emit manualToggleChanged();
|
||||
|
||||
m_manualTimer.start(60 * 60 * 1000);
|
||||
}
|
||||
|
||||
void HyprsunsetManager::setEndTime(const int& time) {
|
||||
if (time == m_endTime)
|
||||
return;
|
||||
|
||||
m_endTime = time;
|
||||
emit endTimeChanged();
|
||||
apply();
|
||||
}
|
||||
|
||||
void HyprsunsetManager::setStartTime(const int& time) {
|
||||
if (time == m_startTime)
|
||||
return;
|
||||
|
||||
m_startTime = time;
|
||||
emit startTimeChanged();
|
||||
apply();
|
||||
}
|
||||
|
||||
void HyprsunsetManager::setTemp(const int& temp) {
|
||||
if (temp == m_temp)
|
||||
return;
|
||||
|
||||
m_temp = temp;
|
||||
emit tempChanged();
|
||||
apply();
|
||||
}
|
||||
|
||||
void HyprsunsetManager::toggle() {
|
||||
if (m_enabled) {
|
||||
end();
|
||||
} else {
|
||||
start();
|
||||
}
|
||||
}
|
||||
|
||||
void HyprsunsetManager::start() {
|
||||
if (m_enabled)
|
||||
return;
|
||||
|
||||
m_enabled = true;
|
||||
emit enabledChanged();
|
||||
|
||||
m_process.setProgram("hyprctl");
|
||||
m_process.setArguments({"hyprsunset", "temperature", QString::number(m_temp)});
|
||||
m_process.startDetached();
|
||||
}
|
||||
|
||||
void HyprsunsetManager::end() {
|
||||
if (!m_enabled)
|
||||
return;
|
||||
|
||||
m_enabled = false;
|
||||
emit enabledChanged();
|
||||
|
||||
m_process.setProgram("hyprctl");
|
||||
m_process.setArguments({"hyprsunset", "identity"});
|
||||
m_process.startDetached();
|
||||
}
|
||||
|
||||
void HyprsunsetManager::apply() {
|
||||
if (m_manualToggle || !m_activeAuto || !m_startAllowed)
|
||||
return;
|
||||
|
||||
const auto current = QTime::currentTime().hour();
|
||||
|
||||
if (current >= m_startTime || current < m_endTime) {
|
||||
start();
|
||||
} else {
|
||||
end();
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
@@ -0,0 +1,64 @@
|
||||
#pragma once
|
||||
|
||||
#include <QObject>
|
||||
#include <QTime>
|
||||
#include <QProcess>
|
||||
#include <QTimer>
|
||||
#include <qqmlintegration.h>
|
||||
#include <qtmetamacros.h>
|
||||
|
||||
namespace ZShell::services {
|
||||
|
||||
class HyprsunsetManager : public QObject {
|
||||
Q_OBJECT
|
||||
QML_ELEMENT
|
||||
Q_PROPERTY(bool enabled READ enabled NOTIFY enabledChanged)
|
||||
Q_PROPERTY(int startTime READ startTime WRITE setStartTime NOTIFY startTimeChanged)
|
||||
Q_PROPERTY(int endTime READ endTime WRITE setEndTime NOTIFY endTimeChanged)
|
||||
Q_PROPERTY(int temp READ temp WRITE setTemp NOTIFY tempChanged)
|
||||
Q_PROPERTY(bool activeAuto READ activeAuto WRITE setActiveAuto NOTIFY activeAutoChanged)
|
||||
Q_PROPERTY(bool manualToggle READ manualToggle WRITE setManualToggle NOTIFY manualToggleChanged)
|
||||
|
||||
public:
|
||||
explicit HyprsunsetManager(QObject* parent = nullptr);
|
||||
|
||||
int startTime() const;
|
||||
int endTime() const;
|
||||
bool enabled() const;
|
||||
int temp() const;
|
||||
bool activeAuto() const;
|
||||
bool manualToggle() const;
|
||||
Q_INVOKABLE void toggle();
|
||||
Q_INVOKABLE void apply();
|
||||
|
||||
void setStartTime(const int& time);
|
||||
void setEndTime(const int& time);
|
||||
void setTemp(const int& temp);
|
||||
void setActiveAuto(bool activeAuto);
|
||||
void setManualToggle(bool toggle);
|
||||
|
||||
signals:
|
||||
void enabledChanged();
|
||||
void startTimeChanged();
|
||||
void activeAutoChanged();
|
||||
void endTimeChanged();
|
||||
void tempChanged();
|
||||
void manualToggleChanged();
|
||||
|
||||
private:
|
||||
int m_startTime;
|
||||
int m_endTime;
|
||||
bool m_enabled;
|
||||
bool m_manualToggle = false;
|
||||
bool m_activeAuto;
|
||||
bool m_startAllowed;
|
||||
QTimer m_startCooldown;
|
||||
int m_temp;
|
||||
QProcess m_process;
|
||||
QTimer m_timer;
|
||||
QTimer m_manualTimer;
|
||||
void start();
|
||||
void end();
|
||||
};
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user