1+ import { ipcRenderer } from 'electron' ;
2+
13import { ConfigType } from '../../config/dynamic' ;
24
3- export default ( _ : ConfigType < 'ambient-mode' > ) => {
4- const interpolationTime = 3000 ; // interpolation time (ms)
5- const framerate = 30 ; // frame
6- const qualityRatio = 50 ; // width size (pixel)
5+ export default ( config : ConfigType < 'ambient-mode' > ) => {
6+ let interpolationTime = config . interpolationTime ; // interpolation time (ms)
7+ let buffer = config . buffer ; // frame
8+ let qualityRatio = config . quality ; // width size (pixel)
9+ let sizeRatio = config . size / 100 ; // size ratio (percent)
10+ let blur = config . blur ; // blur (pixel)
11+ let opacity = config . opacity ; // opacity (percent)
12+ let isFullscreen = config . fullscreen ; // fullscreen (boolean)
713
814 let unregister : ( ( ) => void ) | null = null ;
915
@@ -37,7 +43,7 @@ export default (_: ConfigType<'ambient-mode'>) => {
3743
3844 context . globalAlpha = 1 ;
3945 if ( lastImageData ) {
40- const frameOffset = ( 1 / framerate ) * ( 1000 / interpolationTime ) ;
46+ const frameOffset = ( 1 / buffer ) * ( 1000 / interpolationTime ) ;
4147 context . globalAlpha = 1 - ( frameOffset * 2 ) ; // because of alpha value must be < 1
4248 context . putImageData ( lastImageData , 0 , 0 ) ;
4349 context . globalAlpha = frameOffset ;
@@ -61,8 +67,18 @@ export default (_: ConfigType<'ambient-mode'>) => {
6167
6268 blurCanvas . width = qualityRatio ;
6369 blurCanvas . height = Math . floor ( newHeight / newWidth * qualityRatio ) ;
64- blurCanvas . style . width = `${ newWidth } px` ;
65- blurCanvas . style . height = `${ newHeight } px` ;
70+ blurCanvas . style . width = `${ newWidth * sizeRatio } px` ;
71+ blurCanvas . style . height = `${ newHeight * sizeRatio } px` ;
72+
73+ if ( isFullscreen ) blurCanvas . classList . add ( 'fullscreen' ) ;
74+ else blurCanvas . classList . remove ( 'fullscreen' ) ;
75+
76+ const leftOffset = newWidth * ( sizeRatio - 1 ) / 2 ;
77+ const topOffset = newHeight * ( sizeRatio - 1 ) / 2 ;
78+ blurCanvas . style . setProperty ( '--left' , `${ - 1 * leftOffset } px` ) ;
79+ blurCanvas . style . setProperty ( '--top' , `${ - 1 * topOffset } px` ) ;
80+ blurCanvas . style . setProperty ( '--blur' , `${ blur } px` ) ;
81+ blurCanvas . style . setProperty ( '--opacity' , `${ opacity } ` ) ;
6682 } ;
6783
6884 const observer = new MutationObserver ( ( mutations ) => {
@@ -75,10 +91,22 @@ export default (_: ConfigType<'ambient-mode'>) => {
7591 const resizeObserver = new ResizeObserver ( ( ) => {
7692 applyVideoAttributes ( ) ;
7793 } ) ;
94+ const onConfigSync = ( _ : Electron . IpcRendererEvent , newConfig : ConfigType < 'ambient-mode' > ) => {
95+ if ( typeof newConfig . interpolationTime === 'number' ) interpolationTime = newConfig . interpolationTime ;
96+ if ( typeof newConfig . buffer === 'number' ) buffer = newConfig . buffer ;
97+ if ( typeof newConfig . quality === 'number' ) qualityRatio = newConfig . quality ;
98+ if ( typeof newConfig . size === 'number' ) sizeRatio = newConfig . size / 100 ;
99+ if ( typeof newConfig . blur === 'number' ) blur = newConfig . blur ;
100+ if ( typeof newConfig . opacity === 'number' ) opacity = newConfig . opacity ;
101+ if ( typeof newConfig . fullscreen === 'boolean' ) isFullscreen = newConfig . fullscreen ;
78102
103+ applyVideoAttributes ( ) ;
104+ } ;
105+ ipcRenderer . on ( 'ambient-mode:config-change' , onConfigSync ) ;
106+
79107 /* hooking */
80108 let canvasInterval : NodeJS . Timeout | null = null ;
81- canvasInterval = setInterval ( onSync , Math . max ( 1 , Math . ceil ( 1000 / framerate ) ) ) ;
109+ canvasInterval = setInterval ( onSync , Math . max ( 1 , Math . ceil ( 1000 / buffer ) ) ) ;
82110 applyVideoAttributes ( ) ;
83111 observer . observe ( songVideo , { attributes : true } ) ;
84112 resizeObserver . observe ( songVideo ) ;
@@ -90,7 +118,7 @@ export default (_: ConfigType<'ambient-mode'>) => {
90118 } ;
91119 const onPlay = ( ) => {
92120 if ( canvasInterval ) clearInterval ( canvasInterval ) ;
93- canvasInterval = setInterval ( onSync , Math . max ( 1 , Math . ceil ( 1000 / framerate ) ) ) ;
121+ canvasInterval = setInterval ( onSync , Math . max ( 1 , Math . ceil ( 1000 / buffer ) ) ) ;
94122 } ;
95123 songVideo . addEventListener ( 'pause' , onPause ) ;
96124 songVideo . addEventListener ( 'play' , onPlay ) ;
@@ -107,6 +135,7 @@ export default (_: ConfigType<'ambient-mode'>) => {
107135
108136 observer . disconnect ( ) ;
109137 resizeObserver . disconnect ( ) ;
138+ ipcRenderer . off ( 'ambient-mode:config-change' , onConfigSync ) ;
110139 window . removeEventListener ( 'resize' , applyVideoAttributes ) ;
111140
112141 wrapper . removeChild ( blurCanvas ) ;
0 commit comments