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

@ -15,33 +15,24 @@ Item {
implicitHeight: pill.height
visible: true
function getIcon() {
if (!NightLightService.enabled) {
return "light_mode"
}
return NightLightService.isActive ? "dark_mode" : "light_mode"
}
function getTooltipText() {
if (!NightLightService.enabled) {
return "Night Light: Disabled\nLeft click to open settings.\nRight click to enable."
}
var status = NightLightService.isActive ? "Active" : "Inactive (outside schedule)"
var warmth = Math.round(NightLightService.warmth * 10)
var schedule = NightLightService.autoSchedule ? `Schedule: ${NightLightService.startTime} - ${NightLightService.stopTime}` : "Manual mode"
return `Night Light: ${status}\nWarmth: ${warmth}/10\n${schedule}\nLeft click to open settings.\nRight click to toggle.`
}
NPill {
id: pill
icon: getIcon()
icon: NightLightService.isActive ? "bedtime" : "bedtime_off"
iconCircleColor: NightLightService.isActive ? Color.mSecondary : Color.mOnSurfaceVariant
collapsedIconColor: NightLightService.isActive ? Color.mOnSecondary : Color.mOnSurface
autoHide: false
text: NightLightService.enabled ? (NightLightService.isActive ? "ON" : "OFF") : "OFF"
tooltipText: getTooltipText()
text: NightLightService.isActive ? "On" : "Off"
tooltipText: {
if (!Settings.isLoaded || !Settings.data.nightLight.enabled) {
return "Night Light: Disabled\nLeft click to open settings.\nRight click to enable."
}
var status = NightLightService.isActive ? "Active" : "Inactive (outside schedule)"
var intensity = Math.round(Settings.data.nightLight.intensity * 100)
var schedule = Settings.data.nightLight.autoSchedule ? `Schedule: ${Settings.data.nightLight.startTime} - ${Settings.data.nightLight.stopTime}` : "Manual mode"
return `Intensity: ${intensity}%\n${schedule}\nLeft click to open settings.\nRight click to toggle.`
}
onClicked: {
// Left click - open settings
@ -52,34 +43,13 @@ Item {
onRightClicked: {
// Right click - toggle night light
NightLightService.toggle()
Settings.data.nightLight.enabled = !Settings.data.nightLight.enabled
}
}
// Update when service state changes
Connections {
target: NightLightService
function onEnabledChanged() {
pill.icon = getIcon()
pill.text = NightLightService.enabled ? (NightLightService.isActive ? "ON" : "OFF") : "OFF"
pill.tooltipText = getTooltipText()
}
function onIsActiveChanged() {
pill.icon = getIcon()
pill.text = NightLightService.enabled ? (NightLightService.isActive ? "ON" : "OFF") : "OFF"
pill.tooltipText = getTooltipText()
}
function onWarmthChanged() {
pill.tooltipText = getTooltipText()
}
function onStartTimeChanged() {
pill.tooltipText = getTooltipText()
}
function onStopTimeChanged() {
pill.tooltipText = getTooltipText()
}
function onAutoScheduleChanged() {
pill.tooltipText = getTooltipText()
}
onWheel: delta => {
var diff = delta > 0 ? 0.05 : -0.05
Settings.data.nightLight.intensity = Math.max(0, Math.min(1.0,
Settings.data.nightLight.intensity + diff))
}
}
}

View file

@ -11,17 +11,11 @@ Variants {
required property ShellScreen modelData
readonly property real scaling: ScalingService.scale(modelData)
active: NightLightService.enabled
active: NightLightService.isActive
sourceComponent: PanelWindow {
id: nightlightWindow
screen: modelData
visible: NightLightService.isActive
color: Color.transparent
mask: Region {}
anchors {
top: true
bottom: true
@ -29,6 +23,9 @@ Variants {
right: true
}
// Ensure a full click through
mask: Region {}
WlrLayershell.layer: WlrLayershell.Overlay
WlrLayershell.exclusionMode: ExclusionMode.Ignore
WlrLayershell.keyboardFocus: WlrKeyboardFocus.None
@ -37,29 +34,13 @@ Variants {
Rectangle {
anchors.fill: parent
color: NightLightService.overlayColor
}
// Safe connection that checks if the window still exists
Connections {
target: NightLightService
function onIsActiveChanged() {
if (nightlightWindow && typeof nightlightWindow.visible !== 'undefined') {
nightlightWindow.visible = NightLightService.isActive
Behavior on color {
ColorAnimation {
duration: Style.animationSlow
}
}
}
// Cleanup when component is being destroyed
Component.onDestruction: {
Logger.log("NightLight", "PanelWindow being destroyed")
}
}
// Safe state changes
onActiveChanged: {
if (!active) {
Logger.log("NightLight", "Loader deactivating for screen:", modelData.name)
}
}
}
}

View file

@ -249,7 +249,7 @@ Item {
NToggle {
label: "Enable Night Light"
description: "Apply a warm color filter to reduce blue light emission."
checked: NightLightService.enabled
checked: Settings.data.nightLight.enabled
onToggled: checked => {
Settings.data.nightLight.enabled = checked
}
@ -258,62 +258,50 @@ Item {
NToggle {
label: "Auto Schedule"
description: "Automatically enable night light based on time schedule."
checked: NightLightService.autoSchedule
enabled: NightLightService.enabled
onToggled: checked => {
NightLightService.setAutoSchedule(checked)
}
checked: Settings.data.nightLight.autoSchedule
enabled: Settings.data.nightLight.enabled
onToggled: checked => Settings.data.nightLight.autoSchedule = checked
}
// Warmth settings
NText {
text: "Warmth"
font.pointSize: Style.fontSizeM * scaling
font.weight: Style.fontWeightBold
color: Color.mOnSurface
enabled: NightLightService.enabled
}
// Intensity settings
ColumnLayout {
spacing: Style.marginXS * scaling
NText {
text: "Higher values create warmer (more orange) light, lower values create cooler (more blue) light."
font.pointSize: Style.fontSizeS * scaling
color: Color.mOnSurfaceVariant
wrapMode: Text.WordWrap
Layout.fillWidth: true
enabled: NightLightService.enabled
NText {
text: "Intensity"
font.pointSize: Style.fontSizeM * scaling
font.weight: Style.fontWeightBold
color: Color.mOnSurface
enabled: Settings.data.nightLight.enabled
}
NText {
text: "Higher values create warmer (more orange) light, lower values create cooler (more blue) light."
font.pointSize: Style.fontSizeS * scaling
color: Color.mOnSurfaceVariant
wrapMode: Text.WordWrap
Layout.fillWidth: true
enabled: Settings.data.nightLight.enabled
}
}
RowLayout {
spacing: Style.marginS * scaling
Layout.fillWidth: true
enabled: NightLightService.enabled
enabled: Settings.data.nightLight.enabled
NSlider {
id: warmthSlider
from: 0
to: 10
stepSize: 1
value: Math.round(NightLightService.warmth * 10)
onPressedChanged: {
if (!pressed) {
NightLightService.setWarmth(value / 10)
}
}
to: 1
stepSize: 0.01
value: Settings.data.nightLight.intensity
onMoved: Settings.data.nightLight.intensity = value
Layout.fillWidth: true
Layout.minimumWidth: 150 * scaling
}
Connections {
target: NightLightService
function onWarmthChanged() {
if (!warmthSlider.pressed) {
warmthSlider.value = Math.round(NightLightService.warmth * 10)
}
}
}
NText {
text: `${warmthSlider.value}`
text: `${Math.round(Settings.data.nightLight.intensity * 100)}%`
Layout.alignment: Qt.AlignVCenter
Layout.minimumWidth: 60 * scaling
horizontalAlignment: Text.AlignRight
@ -321,55 +309,55 @@ Item {
}
// Schedule settings
NText {
text: "Schedule"
font.pointSize: Style.fontSizeM * scaling
font.weight: Style.fontWeightBold
color: Color.mOnSurface
enabled: NightLightService.enabled
}
ColumnLayout {
spacing: Style.marginXS * scaling
RowLayout {
spacing: Style.marginL * scaling
Layout.fillWidth: true
enabled: NightLightService.enabled
ColumnLayout {
spacing: Style.marginXXS * scaling
Layout.fillWidth: true
NText {
text: "Start Time"
font.pointSize: Style.fontSizeS * scaling
color: Color.mOnSurfaceVariant
}
NComboBox {
model: timeOptions
currentKey: NightLightService.startTime
placeholder: "Select time"
onSelected: function (key) {
NightLightService.setSchedule(key, NightLightService.stopTime)
}
}
NText {
text: "Schedule"
font.pointSize: Style.fontSizeM * scaling
font.weight: Style.fontWeightBold
color: Color.mOnSurface
enabled: Settings.data.nightLight.enabled
}
ColumnLayout {
spacing: Style.marginXXS * scaling
RowLayout {
spacing: Style.marginL * scaling
Layout.fillWidth: true
enabled: Settings.data.nightLight.enabled
NText {
text: "Stop Time"
font.pointSize: Style.fontSizeS * scaling
color: Color.mOnSurfaceVariant
ColumnLayout {
spacing: Style.marginXXS * scaling
Layout.fillWidth: true
NText {
text: "Start Time"
font.pointSize: Style.fontSizeS * scaling
color: Color.mOnSurfaceVariant
}
NComboBox {
model: timeOptions
currentKey: Settings.data.nightLight.startTime
placeholder: "Select start time"
onSelected: key => Settings.data.nightLight.startTime = key
}
}
NComboBox {
model: timeOptions
currentKey: NightLightService.stopTime
placeholder: "Select time"
onSelected: function (key) {
NightLightService.setSchedule(NightLightService.startTime, key)
ColumnLayout {
spacing: Style.marginXXS * scaling
Layout.fillWidth: true
NText {
text: "Stop Time"
font.pointSize: Style.fontSizeS * scaling
color: Color.mOnSurfaceVariant
}
NComboBox {
model: timeOptions
currentKey: Settings.data.nightLight.stopTime
placeholder: "Select stop time"
onSelected: key => Settings.data.nightLight.stopTime = key
}
}
}