@@ -70,15 +70,16 @@ function assingMediaSession(song) {
7070async function playSong ( song ) {
7171 const { file } = song ;
7272 currentSong = song ;
73+ saveOption ( 'currentSongId' , currentSong . id ) ;
7374 points . setUniform ( 'showMessage' , 0 ) ;
74- const audioUrl = URL . createObjectURL ( file ) ;
7575 const name = song ?. name || file . name ;
7676 const title = name ;
7777
78+ const audioUrl = URL . createObjectURL ( song . file ) ;
7879 audio && audio . pause ( ) && ( audio = null ) ;
7980 audio = points . setAudio ( 'audio' , audioUrl , options . volume , false , false ) ;
81+
8082 points . setUniform ( 'numChars' , title . length < MAXCHARS ? title . length : MAXCHARS ) ;
81- // points.setStorageMap('chars', strToCodes(title));
8283
8384 const songNameImg = strToImage ( title , atlas , size ) ;
8485 await points . setTextureImage ( 'songName' , songNameImg ) ;
@@ -92,12 +93,7 @@ async function playSong(song) {
9293 assingMediaSession ( song ) ;
9394
9495 audio . addEventListener ( 'timeupdate' , onTimeUpdate ) ;
95- audio . addEventListener ( 'ended' , async e => {
96- const id = + e . target . id ;
97- const nextSong = songs [ id + 1 ] || songs [ 0 ] ;
98- playSong ( nextSong ) ;
99-
100- } ) ;
96+ audio . addEventListener ( 'ended' , loadNextSong ) ;
10197 audio . play ( ) ;
10298}
10399
@@ -310,6 +306,10 @@ if (volume) {
310306const scheme = await getOption ( 'scheme' ) ;
311307console . log ( scheme ) ;
312308
309+ const currentSongId = await getOption ( 'currentSongId' )
310+ console . log ( currentSongId ) ;
311+
312+
313313if ( scheme ) {
314314 selectedScheme [ 'Color Scheme' ] = scheme ;
315315 points . setUniform ( 'colorScheme' , scheme )
@@ -411,8 +411,18 @@ points.canvas.addEventListener('click', _ => {
411411 if ( pauseClickTimeout ) {
412412 return ;
413413 }
414- pauseClickTimeout = setTimeout ( ( ) => {
415- audio ?. paused ? audio ?. play ( ) : audio ?. pause ( ) ;
414+ pauseClickTimeout = setTimeout ( _ => {
415+ // if the app starts first time it has no audio,
416+ // second time it has the current song saved,
417+ // no src means no song
418+ // if currentSongId then a value was saved and can be loaded
419+ const src = audio . getAttribute ( 'src' ) ;
420+ if ( ! src && currentSongId ) {
421+ currentSong = songs [ currentSongId ] ;
422+ playSong ( currentSong )
423+ } else {
424+ audio ?. paused ? audio ?. play ( ) : audio ?. pause ( ) ;
425+ }
416426 pauseClickTimeout = null ;
417427 } , 300 ) ;
418428} ) ;
@@ -442,22 +452,49 @@ function update() {
442452let isMouseMoving = false ;
443453let mouseStopTimeout ;
444454
455+ const guiElement = document . querySelector ( '.dg.main' ) ;
456+ let isMouseOnDatGui = false ;
457+
458+ function onMouseStopTimeout ( ) {
459+ clearTimeout ( mouseStopTimeout ) ;
460+ mouseStopTimeout = null ;
461+ isMouseMoving = false ;
462+ gui . close ( )
463+ document . body . style . cursor = 'none' ;
464+ }
465+
466+ guiElement . addEventListener ( 'mouseenter' , ( ) => {
467+ isMouseOnDatGui = true ;
468+ clearTimeout ( mouseStopTimeout ) ;
469+ mouseStopTimeout = null ;
470+ } ) ;
471+
472+ guiElement . addEventListener ( 'mouseleave' , ( ) => {
473+ isMouseOnDatGui = false ;
474+ if ( ! mouseStopTimeout ) {
475+ mouseStopTimeout = setTimeout ( onMouseStopTimeout , 1000 ) ;
476+ }
477+ } ) ;
478+
445479document . addEventListener ( 'mousemove' , e => {
446480 if ( ! isMouseMoving ) {
447481 isMouseMoving = true ;
482+ gui . open ( )
448483 document . body . style . cursor = 'auto' ;
449484 }
450485
451- // Clear the timeout to reset the stop detection
452486 clearTimeout ( mouseStopTimeout ) ;
487+ mouseStopTimeout = null ;
488+
489+ if ( isMouseOnDatGui ) {
490+ return ;
491+ }
453492
454- // Set a timeout to detect when the mouse stops
455- mouseStopTimeout = setTimeout ( _ => {
456- isMouseMoving = false ;
457- document . body . style . cursor = 'none' ;
458- } , 1000 ) ;
493+ mouseStopTimeout = setTimeout ( onMouseStopTimeout , 1000 ) ;
459494} ) ;
460495
496+
497+
461498/******************************/
462499async function loadSongFromURL ( ) {
463500 const url = new URL ( window . location . href ) ;
@@ -491,10 +528,14 @@ async function loadSongFromURL() {
491528
492529loadSongFromURL ( )
493530
494-
495-
496531// ----------------------------------
497532
533+ function loadNextSong ( ) {
534+ const id = + currentSong . id ;
535+ const nextSong = songs [ id + 1 ] || songs [ 0 ] ;
536+ playSong ( nextSong ) ;
537+ }
538+
498539if ( 'mediaSession' in navigator ) {
499540 navigator . mediaSession . setActionHandler ( 'play' , _ => audio ?. play ( ) ) ;
500541 navigator . mediaSession . setActionHandler ( 'pause' , _ => audio ?. pause ( ) ) ;
@@ -512,9 +553,5 @@ if ('mediaSession' in navigator) {
512553 playSong ( nextSong ) ;
513554 } ) ;
514555
515- navigator . mediaSession . setActionHandler ( 'nexttrack' , _ => {
516- const id = + currentSong . id ;
517- const nextSong = songs [ id + 1 ] || songs [ 0 ] ;
518- playSong ( nextSong ) ;
519- } ) ;
556+ navigator . mediaSession . setActionHandler ( 'nexttrack' , loadNextSong ) ;
520557}
0 commit comments