diff --git a/cli.sh b/cli.sh index 97505a76..a759106e 100755 --- a/cli.sh +++ b/cli.sh @@ -283,14 +283,30 @@ brightness) bash "${SCRIPT_DIR}/scripts/brightness_list.sh" >"${BRIGHTNESS_SAVE_FILE}.tmp" 2>/dev/null || { echo "Warning: Could not query current brightness" } + # Skip saving if brightness is 0 (screen off state) and we already have a save if [ -f "${BRIGHTNESS_SAVE_FILE}.tmp" ]; then - while IFS=: read -r name bright method; do - if [ -n "$name" ] && [ -n "$bright" ]; then - echo "${name}:${bright}" + # Check if all values are 0 or very low (screen off state) + ALL_ZERO=true + while IFS=: read -r name bright; do + if [ -n "$bright" ] && [ "$bright" -gt 5 ]; then + ALL_ZERO=false + break fi - done <"${BRIGHTNESS_SAVE_FILE}.tmp" >"$BRIGHTNESS_SAVE_FILE" - rm -f "${BRIGHTNESS_SAVE_FILE}.tmp" - echo "Saved current brightness for all monitors" + done <"${BRIGHTNESS_SAVE_FILE}.tmp" + + # Only save if not all zero, OR if no previous save exists + if [ "$ALL_ZERO" = true ] && [ -f "$BRIGHTNESS_SAVE_FILE" ]; then + echo "Skipped saving - brightness at 0 (keeping previous save)" + rm -f "${BRIGHTNESS_SAVE_FILE}.tmp" + else + while IFS=: read -r name bright; do + if [ -n "$name" ] && [ -n "$bright" ]; then + echo "${name}:${bright}" + fi + done <"${BRIGHTNESS_SAVE_FILE}.tmp" >"$BRIGHTNESS_SAVE_FILE" + rm -f "${BRIGHTNESS_SAVE_FILE}.tmp" + echo "Saved current brightness for all monitors" + fi fi else # Save specific monitor @@ -352,14 +368,30 @@ brightness) echo "Warning: Could not query current brightness" } # Convert format from name:brightness:method to name:brightness + # Skip saving if brightness is 0 (screen off state) and we already have a save if [ -f "${BRIGHTNESS_SAVE_FILE}.tmp" ]; then - while IFS=: read -r name bright method; do - if [ -n "$name" ] && [ -n "$bright" ]; then - echo "${name}:${bright}" + # Check if all values are 0 or very low (screen off state) + ALL_ZERO=true + while IFS=: read -r name bright; do + if [ -n "$bright" ] && [ "$bright" -gt 5 ]; then + ALL_ZERO=false + break fi - done <"${BRIGHTNESS_SAVE_FILE}.tmp" >"$BRIGHTNESS_SAVE_FILE" - rm -f "${BRIGHTNESS_SAVE_FILE}.tmp" - echo "Saved current brightness for all monitors" + done <"${BRIGHTNESS_SAVE_FILE}.tmp" + + # Only save if not all zero, OR if no previous save exists + if [ "$ALL_ZERO" = true ] && [ -f "$BRIGHTNESS_SAVE_FILE" ]; then + echo "Skipped saving - brightness at 0 (keeping previous save)" + rm -f "${BRIGHTNESS_SAVE_FILE}.tmp" + else + while IFS=: read -r name bright; do + if [ -n "$name" ] && [ -n "$bright" ]; then + echo "${name}:${bright}" + fi + done <"${BRIGHTNESS_SAVE_FILE}.tmp" >"$BRIGHTNESS_SAVE_FILE" + rm -f "${BRIGHTNESS_SAVE_FILE}.tmp" + echo "Saved current brightness for all monitors" + fi fi else # Save specific monitor diff --git a/modules/services/IdleService.qml b/modules/services/IdleService.qml index 5bacb5c5..81d9140c 100644 --- a/modules/services/IdleService.qml +++ b/modules/services/IdleService.qml @@ -6,6 +6,7 @@ import Quickshell import Quickshell.Io import Quickshell.Wayland import qs.config +import qs.modules.services Singleton { id: root @@ -60,6 +61,8 @@ Singleton { // Master Idle Logic property int elapsedIdleTime: 0 property var triggeredListeners: [] // Keeps track of indices that have fired + property bool resumeCooldown: false // Prevent immediate resume after listener triggers + property bool pendingReset: false // Track if reset was requested during cooldown // Master Monitor: Detects "absence of activity" almost immediately IdleMonitor { @@ -72,6 +75,26 @@ Singleton { idleTimer.start(); } else { idleTimer.stop(); + // If in cooldown, defer the reset + if (root.resumeCooldown) { + root.pendingReset = true; + } else { + root.resetIdleState(); + } + } + } + } + + // Cooldown timer - prevents resume for a short period after listener triggers + Timer { + id: cooldownTimer + interval: 1500 // 1.5 seconds - enough for lockscreen to fully load + repeat: false + onTriggered: { + root.resumeCooldown = false; + // Execute pending reset if activity happened during cooldown and user is still active + if (root.pendingReset && !masterMonitor.isIdle) { + root.pendingReset = false; root.resetIdleState(); } } @@ -88,6 +111,12 @@ Singleton { } function checkListeners() { + // Skip idle actions if media is playing (YouTube, Spotify, etc.) + if (MprisController.isPlaying) { + root.elapsedIdleTime = 0; // Reset timer while media plays + return; + } + let listeners = Config.system.idle.listeners; for (let i = 0; i < listeners.length; i++) { let listener = listeners[i]; @@ -99,6 +128,10 @@ Singleton { console.log("Idle timer " + tVal + "s reached: " + listener.onTimeout); executionProc.command = ["sh", "-c", listener.onTimeout]; executionProc.running = true; + + // Activate cooldown to prevent immediate resume from listener-caused activity + root.resumeCooldown = true; + cooldownTimer.restart(); } root.triggeredListeners.push(i); }