Skip to content

Commit

Permalink
controller: Enable joystick forwarding only when the document (window…
Browse files Browse the repository at this point in the history
…/tab) is visible
  • Loading branch information
rafaellehmkuhl committed Feb 1, 2024
1 parent cab6791 commit 8528c72
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/stores/controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useStorage } from '@vueuse/core'
import { useDocumentVisibility, useStorage } from '@vueuse/core'
import { saveAs } from 'file-saver'
import { defineStore } from 'pinia'
import Swal from 'sweetalert2'
Expand Down Expand Up @@ -96,6 +96,18 @@ export const useControllerStore = defineStore('controller', () => {
}
}

// Disable joystick forwarding if the window/tab is not visible (using VueUse)
const windowVisibility = useDocumentVisibility()
watch(windowVisibility, (value) => {
if (value === 'hidden') {
console.warn('Window/tab hidden. Disabling joystick forwarding.')
enableForwarding.value = false
} else {
console.info('Window/tab visible. Enabling joystick forwarding.')
enableForwarding.value = true
}
})

const processJoystickStateEvent = (event: JoystickEvent): void => {
const joystick = joysticks.value.get(event.detail.index)
if (joystick === undefined || (event.type !== EventType.Axis && event.type !== EventType.Button)) return
Expand Down

0 comments on commit 8528c72

Please sign in to comment.