local brightness = require("hs.brightness") local caffeinate = require("hs.caffeinate") local timer = require("hs.timer")
-- 配置参数 local delayBeforeDim = 1 * 60-- 进入屏保后等待 1 分钟再开始降亮度(秒) local dimDuration = 5-- 从当前亮度降到 0 的总时长(秒) local steps = 2-- 分多少步降完(越大越平滑) local originalBrightness = nil local dimTimer = nil local delayTimer = nil
-- 停止所有计时器并恢复亮度 localfunctionrestoreBrightness() if delayTimer then delayTimer:stop() delayTimer = nil end if dimTimer then dimTimer:stop() dimTimer = nil end if originalBrightness then brightness.set(originalBrightness) originalBrightness = nil end end
-- 开始渐暗 localfunctionstartDimming() -- 如果已经在渐暗,就不重复 if dimTimer ~= nilthenreturnend
originalBrightness = brightness.get() ifnot originalBrightness or originalBrightness <= 0then return end
local currentStep = 0 local stepInterval = dimDuration / steps local stepDelta = originalBrightness / steps