@@ -14,153 +14,31 @@ See the License for the specific language governing permissions and
1414limitations under the License.
1515*/
1616
17- import React , { createRef } from "react" ;
18- import "context-filter-polyfill" ;
19-
20- import UIStore from "../../stores/UIStore" ;
17+ import React , { CSSProperties } from "react" ;
2118
2219interface IProps {
23- backgroundImage ?: CanvasImageSource ;
24- }
25-
26- interface IState {
27- // Left Panel image
28- lpImage ?: string ;
29- // Left-left panel image
30- llpImage ?: string ;
20+ backgroundImage ?: string ;
21+ blurMultiplier ?: number ;
3122}
3223
33- export default class BackdropPanel extends React . PureComponent < IProps , IState > {
34- private leftLeftPanelRef = createRef < HTMLCanvasElement > ( ) ;
35- private leftPanelRef = createRef < HTMLCanvasElement > ( ) ;
36-
37- private sizes = {
38- leftLeftPanelWidth : 0 ,
39- leftPanelWidth : 0 ,
40- height : 0 ,
41- } ;
42- private style = getComputedStyle ( document . documentElement ) ;
43-
44- public state : IState = { } ;
45-
46- public componentDidMount ( ) {
47- UIStore . instance . on ( "SpacePanel" , this . onResize ) ;
48- UIStore . instance . on ( "GroupFilterPanelContainer" , this . onResize ) ;
49- this . onResize ( ) ;
50- }
51-
52- public componentWillUnmount ( ) {
53- UIStore . instance . off ( "SpacePanel" , this . onResize ) ;
54- UIStore . instance . on ( "GroupFilterPanelContainer" , this . onResize ) ;
55- }
56-
57- public componentDidUpdate ( prevProps : IProps ) {
58- if ( prevProps . backgroundImage !== this . props . backgroundImage ) {
59- this . setState ( { } ) ;
60- this . onResize ( ) ;
24+ export const BackdropPanel : React . FC < IProps > = ( { backgroundImage, blurMultiplier } ) => {
25+ if ( ! backgroundImage ) return null ;
26+
27+ const styles : CSSProperties = { } ;
28+ if ( blurMultiplier ) {
29+ const rootStyle = getComputedStyle ( document . documentElement ) ;
30+ const blurValue = rootStyle . getPropertyValue ( '--lp-background-blur' ) ;
31+ const pixelsValue = blurValue . replace ( 'px' , '' ) ;
32+ const parsed = parseInt ( pixelsValue , 10 ) ;
33+ if ( ! isNaN ( parsed ) ) {
34+ styles . filter = `blur(${ parsed * blurMultiplier } px)` ;
6135 }
6236 }
63-
64- private onResize = ( ) => {
65- if ( this . props . backgroundImage ) {
66- const groupFilterPanelDimensions = UIStore . instance . getElementDimensions ( "GroupFilterPanelContainer" ) ;
67- const spacePanelDimensions = UIStore . instance . getElementDimensions ( "SpacePanel" ) ;
68- const roomListDimensions = UIStore . instance . getElementDimensions ( "LeftPanel" ) ;
69- this . sizes = {
70- leftLeftPanelWidth : spacePanelDimensions ?. width ?? groupFilterPanelDimensions ?. width ?? 0 ,
71- leftPanelWidth : roomListDimensions ?. width ?? 0 ,
72- height : UIStore . instance . windowHeight ,
73- } ;
74- this . refreshBackdropImage ( ) ;
75- }
76- } ;
77-
78- private refreshBackdropImage = ( ) : void => {
79- const leftLeftPanelContext = this . leftLeftPanelRef . current . getContext ( "2d" ) ;
80- const leftPanelContext = this . leftPanelRef . current . getContext ( "2d" ) ;
81- const { leftLeftPanelWidth, leftPanelWidth, height } = this . sizes ;
82- const width = leftLeftPanelWidth + leftPanelWidth ;
83- const { backgroundImage } = this . props ;
84-
85- const imageWidth = ( backgroundImage as ImageBitmap ) . width ;
86- const imageHeight = ( backgroundImage as ImageBitmap ) . height ;
87-
88- const contentRatio = imageWidth / imageHeight ;
89- const containerRatio = width / height ;
90- let resultHeight ;
91- let resultWidth ;
92- if ( contentRatio > containerRatio ) {
93- resultHeight = height ;
94- resultWidth = height * contentRatio ;
95- } else {
96- resultWidth = width ;
97- resultHeight = width / contentRatio ;
98- }
99-
100- // This value has been chosen to be as close with rendering as the css-only
101- // backdrop-filter: blur effect was, mostly takes effect for vertical pictures.
102- const x = width * 0.1 ;
103- const y = ( height - resultHeight ) / 2 ;
104-
105- this . leftLeftPanelRef . current . width = leftLeftPanelWidth ;
106- this . leftLeftPanelRef . current . height = height ;
107- this . leftPanelRef . current . width = ( window . screen . width * 0.5 ) ;
108- this . leftPanelRef . current . height = height ;
109-
110- const spacesBlur = this . style . getPropertyValue ( '--llp-background-blur' ) ;
111- const roomListBlur = this . style . getPropertyValue ( '--lp-background-blur' ) ;
112-
113- leftLeftPanelContext . filter = `blur(${ spacesBlur } )` ;
114- leftPanelContext . filter = `blur(${ roomListBlur } )` ;
115- leftLeftPanelContext . drawImage (
116- backgroundImage ,
117- 0 , 0 ,
118- imageWidth , imageHeight ,
119- x ,
120- y ,
121- resultWidth ,
122- resultHeight ,
123- ) ;
124- leftPanelContext . drawImage (
125- backgroundImage ,
126- 0 , 0 ,
127- imageWidth , imageHeight ,
128- x - leftLeftPanelWidth ,
129- y ,
130- resultWidth ,
131- resultHeight ,
132- ) ;
133- this . setState ( {
134- lpImage : this . leftPanelRef . current . toDataURL ( 'image/jpeg' , 1 ) ,
135- llpImage : this . leftLeftPanelRef . current . toDataURL ( 'image/jpeg' , 1 ) ,
136-
137- } ) ;
138- } ;
139-
140- public render ( ) {
141- if ( ! this . props . backgroundImage ) return null ;
142- return < div className = "mx_BackdropPanel" >
143- { this . state ?. llpImage !== 'data:,' && < img
144- className = "mx_BackdropPanel--canvas"
145- src = { this . state . llpImage } /> }
146-
147- { this . state ?. lpImage !== 'data:,' && < img
148- className = "mx_BackdropPanel--canvas"
149- src = { this . state . lpImage } /> }
150- < canvas
151- ref = { this . leftLeftPanelRef }
152- className = "mx_BackdropPanel--canvas"
153- style = { {
154- display : this . state . lpImage ? 'none' : 'block' ,
155- } }
156- />
157- < canvas
158- style = { {
159- display : this . state . lpImage ? 'none' : 'block' ,
160- } }
161- ref = { this . leftPanelRef }
162- className = "mx_BackdropPanel--canvas"
163- />
164- </ div > ;
165- }
166- }
37+ return < div className = "mx_BackdropPanel" >
38+ < img
39+ style = { styles }
40+ className = "mx_BackdropPanel--image"
41+ src = { backgroundImage } />
42+ </ div > ;
43+ } ;
44+ export default BackdropPanel ;
0 commit comments