NightLight: refactored the code to make simpler

- using intensity instead of warmth
- animated color transition
- removed unecessary bindings and double properties
- using better icons to avoid confusion with brightness
- polished settings UI
This commit is contained in:
LemmyCook 2025-08-26 18:48:10 -04:00
parent 76f0368a64
commit 4cd94f0426
6 changed files with 113 additions and 216 deletions

View file

@ -9,78 +9,40 @@ Singleton {
id: root
// Night Light properties - directly bound to settings
property bool enabled: Settings.data.nightLight?.enabled || false
property real warmth: (Settings.data.nightLight
&& Settings.data.nightLight.warmth !== undefined) ? Settings.data.nightLight.warmth : 0.6
property real intensity: (Settings.data.nightLight
&& Settings.data.nightLight.intensity !== undefined) ? Settings.data.nightLight.intensity : 0.8
property string startTime: Settings.data.nightLight?.startTime || "20:00"
property string stopTime: Settings.data.nightLight?.stopTime || "07:00"
property bool autoSchedule: Settings.data.nightLight?.autoSchedule !== false
readonly property var params: Settings.data.nightLight
// Computed properties
property color overlayColor: enabled ? calculateOverlayColor() : "transparent"
property bool isActive: enabled && (autoSchedule ? isWithinSchedule() : true)
readonly property color overlayColor: params.enabled ? calculateOverlayColor() : "transparent"
property bool isActive: params.enabled && (params.autoSchedule ? isWithinSchedule() : true)
Component.onCompleted: {
Logger.log("NightLight", "Service started")
}
function toggle() {
Settings.data.nightLight.enabled = !Settings.data.nightLight.enabled
Logger.log("NightLight", "Toggled:", Settings.data.nightLight.enabled)
}
function setWarmth(value) {
Settings.data.nightLight.warmth = Math.max(0.0, Math.min(1.0, value))
Logger.log("NightLight", "Warmth set to:", Settings.data.nightLight.warmth)
}
function setIntensity(value) {
Settings.data.nightLight.intensity = Math.max(0.0, Math.min(1.0, value))
Logger.log("NightLight", "Intensity set to:", Settings.data.nightLight.intensity)
}
function setSchedule(start, stop) {
Settings.data.nightLight.startTime = start
Settings.data.nightLight.stopTime = stop
Logger.log("NightLight", "Schedule set to:", Settings.data.nightLight.startTime, "-",
Settings.data.nightLight.stopTime)
}
function setAutoSchedule(auto) {
Settings.data.nightLight.autoSchedule = auto
Logger.log("NightLight", "Auto schedule set to:", Settings.data.nightLight.autoSchedule, "enabled:", enabled,
"isActive:", isActive, "withinSchedule:", isWithinSchedule())
}
function calculateOverlayColor() {
if (!isActive)
if (!isActive) {
return "transparent"
}
// More vibrant color formula - stronger effect at high warmth
var red = 1.0
var green = 0.85 - warmth * 0.4 // More green reduction for stronger effect
var blue = 0.5 - warmth * 0.45 // More blue reduction for warmer feel
var alpha = 0.1 + warmth * 0.25 // Higher alpha for more noticeable effect
// Apply intensity
red = red * intensity
green = green * intensity
blue = blue * intensity
var green = 1.0 - (0.43 * params.intensity)
var blue = 1.0 - (0.84 * params.intensity)
var alpha = (params.intensity * 0.25) // Higher alpha for more noticeable effect
return Qt.rgba(red, green, blue, alpha)
}
function isWithinSchedule() {
if (!autoSchedule)
if (!params.autoSchedule) {
return true
}
var now = new Date()
var currentTime = now.getHours() * 60 + now.getMinutes()
var startParts = startTime.split(":")
var stopParts = stopTime.split(":")
var startParts = params.startTime.split(":")
var stopParts = params.stopTime.split(":")
var startMinutes = parseInt(startParts[0]) * 60 + parseInt(startParts[1])
var stopMinutes = parseInt(stopParts[0]) * 60 + parseInt(stopParts[1])
@ -95,14 +57,10 @@ Singleton {
// Timer to check schedule changes
Timer {
interval: 60000 // Check every minute
running: true
running: params.enabled && params.autoSchedule
repeat: true
onTriggered: {
if (autoSchedule && enabled) {
// Force overlay update when schedule changes
Logger.log("NightLight", "Schedule check - enabled:", enabled, "autoSchedule:", autoSchedule, "isActive:",
isActive, "withinSchedule:", isWithinSchedule())
}
isActive = isWithinSchedule()
}
}
}