50 lines
1.2 KiB
TypeScript
50 lines
1.2 KiB
TypeScript
import { defineStore } from 'pinia'
|
|
import { ref, computed } from 'vue'
|
|
|
|
const INACTIVITY_MS = 3 * 60 * 1000 // 3 minutes
|
|
|
|
export const useScreensaverStore = defineStore('screensaver', () => {
|
|
const isActive = ref(false)
|
|
const activationCount = ref(0)
|
|
let inactivityTimer: ReturnType<typeof setTimeout> | null = null
|
|
|
|
/** True when the current activation is the ASCII variant (every 3rd time) */
|
|
const isAsciiMode = computed(() => activationCount.value > 0 && activationCount.value % 3 === 0)
|
|
|
|
function activate() {
|
|
activationCount.value++
|
|
isActive.value = true
|
|
clearInactivityTimer()
|
|
}
|
|
|
|
function deactivate() {
|
|
isActive.value = false
|
|
resetInactivityTimer()
|
|
}
|
|
|
|
function resetInactivityTimer() {
|
|
clearInactivityTimer()
|
|
inactivityTimer = setTimeout(() => {
|
|
inactivityTimer = null
|
|
isActive.value = true
|
|
}, INACTIVITY_MS)
|
|
}
|
|
|
|
function clearInactivityTimer() {
|
|
if (inactivityTimer) {
|
|
clearTimeout(inactivityTimer)
|
|
inactivityTimer = null
|
|
}
|
|
}
|
|
|
|
return {
|
|
isActive,
|
|
isAsciiMode,
|
|
activationCount,
|
|
activate,
|
|
deactivate,
|
|
resetInactivityTimer,
|
|
clearInactivityTimer,
|
|
}
|
|
})
|