wallitor/wallitor-gui/video/playback.ts

116 lines
2.9 KiB
TypeScript

import { invoke } from '@tauri-apps/api/core'
import { listen } from '@tauri-apps/api/event'
import { getCurrentWindow } from '@tauri-apps/api/window'
interface Params {
url?: string
mute?: string
auto_pause?: string
}
function getSearchParamsAsObject() {
const params = new URLSearchParams(window.location.search)
const paramsObject = {}
params.forEach((value, key) => {
paramsObject[key] = value
})
return paramsObject
}
listen('video_pause', () => {
const player = document.getElementById('player') as HTMLVideoElement
if (player) player.pause()
})
class player {
playing: boolean
params: Params
interval: number
constructor(params: Params) {
this.params = params
this.playing = false
this.interval = 0
}
init_player(videoUrl: string) {
const player = document.getElementById('player') as HTMLVideoElement
if (player) {
player.src = videoUrl
player.play()
this.playing = true
if (this.params.mute) {
player.muted = JSON.parse(this.params.mute)
}
player.loop = true
setTimeout(() => {
player.style.opacity = '1'
}, 0)
}
if (this.params.auto_pause && this.params.auto_pause === 'true') {
this.detect_zoom()
}
}
detect_zoom() {
this.clear_detection()
this.interval = window.setInterval(() => {
invoke('any_zoomed').then((res) => {
if (res as boolean) {
if (this.playing) {
const player = document.getElementById('player') as HTMLVideoElement
if (player) {
player.pause()
this.playing = false
}
}
} else {
if (!this.playing) {
const player = document.getElementById('player') as HTMLVideoElement
if (player) {
player.play()
this.playing = true
}
}
}
})
}, 1000)
}
clear_detection() {
if (this.interval) {
clearInterval(this.interval)
this.interval = 0
}
}
}
let player_instance: player | null = null
window.onload = () => {
invoke('set_wallpaper', {
title: 'wallitor_video_playback'
}).then((res) => {
if (!res) {
console.error('Unable to set wallpaper.')
const win = getCurrentWindow()
win.destroy()
return
}
const params: Params = getSearchParamsAsObject()
if (params.url) {
invoke('get_file', {
path: params.url
}).then((res) => {
const binary_data_arr = new Uint8Array(res as number[])
const blob = new Blob([binary_data_arr], { type: 'video/mp4' })
const videoUrl = URL.createObjectURL(blob)
player_instance = new player(params)
player_instance.init_player(videoUrl)
})
}
})
}
listen('stop_auto_pause', () => {
if (player_instance) player_instance.clear_detection()
})
listen('start_auto_pause', () => {
if (player_instance) player_instance.detect_zoom()
})