From 01040a34940bc4a5c70ecd5e30bab730b3952a52 Mon Sep 17 00:00:00 2001 From: James Chen Date: Thu, 23 Jan 2025 10:49:57 +0800 Subject: [PATCH] Optimize code size for VideoPlayer and WebView. (#18231) --- cocos/video/video-player.ts | 40 +++++++++++++++-------------- cocos/web-view/web-view-impl-web.ts | 22 ++++++++-------- cocos/web-view/web-view.ts | 6 ++--- 3 files changed, 36 insertions(+), 32 deletions(-) diff --git a/cocos/video/video-player.ts b/cocos/video/video-player.ts index f34fadd9c3d..38a07b44d63 100644 --- a/cocos/video/video-player.ts +++ b/cocos/video/video-player.ts @@ -372,22 +372,24 @@ export class VideoPlayer extends Component { } protected syncSource (): void { - if (!this._impl) { return; } + const impl = this._impl; + if (!impl) { return; } + if (this._resourceType === ResourceType.REMOTE) { - this._impl.syncURL(this._remoteURL); + impl.syncURL(this._remoteURL); } else { - this._impl.syncClip(this._clip); + impl.syncClip(this._clip); } this._cachedCurrentTime = 0; - this._impl.syncLoop(this._loop); - this._impl.syncVolume(this._volume); - this._impl.syncMute(this._mute); - this._impl.seekTo(this._cachedCurrentTime); - this._impl.syncPlaybackRate(this._playbackRate); - this._impl.syncStayOnBottom(this._stayOnBottom); - this._impl.syncKeepAspectRatio(this._keepAspectRatio); - this._impl.syncFullScreenOnAwake(this._fullScreenOnAwake); + impl.syncLoop(this._loop); + impl.syncVolume(this._volume); + impl.syncMute(this._mute); + impl.seekTo(this._cachedCurrentTime); + impl.syncPlaybackRate(this._playbackRate); + impl.syncStayOnBottom(this._stayOnBottom); + impl.syncKeepAspectRatio(this._keepAspectRatio); + impl.syncFullScreenOnAwake(this._fullScreenOnAwake); } public __preload (): void { @@ -436,43 +438,43 @@ export class VideoPlayer extends Component { } } - public onMetaLoaded (): void { + private onMetaLoaded (): void { ComponentEventHandler.emitEvents(this.videoPlayerEvent, this, VideoPlayerEventType.META_LOADED); this.node.emit('meta-loaded', this); } - public onReadyToPlay (): void { + private onReadyToPlay (): void { if (this._playOnAwake && !this.isPlaying) { this.play(); } ComponentEventHandler.emitEvents(this.videoPlayerEvent, this, VideoPlayerEventType.READY_TO_PLAY); this.node.emit(VideoPlayerEventType.READY_TO_PLAY, this); } - public onPlaying (): void { + private onPlaying (): void { ComponentEventHandler.emitEvents(this.videoPlayerEvent, this, VideoPlayerEventType.PLAYING); this.node.emit(VideoPlayerEventType.PLAYING, this); } - public onPaused (): void { + private onPaused (): void { ComponentEventHandler.emitEvents(this.videoPlayerEvent, this, VideoPlayerEventType.PAUSED); this.node.emit(VideoPlayerEventType.PAUSED, this); } - public onStopped (): void { + private onStopped (): void { ComponentEventHandler.emitEvents(this.videoPlayerEvent, this, VideoPlayerEventType.STOPPED); this.node.emit(VideoPlayerEventType.STOPPED, this); } - public onCompleted (): void { + private onCompleted (): void { ComponentEventHandler.emitEvents(this.videoPlayerEvent, this, VideoPlayerEventType.COMPLETED); this.node.emit(VideoPlayerEventType.COMPLETED, this); } - public onError (): void { + private onError (): void { ComponentEventHandler.emitEvents(this.videoPlayerEvent, this, VideoPlayerEventType.ERROR); this.node.emit(VideoPlayerEventType.ERROR, this); } - public onClicked (): void { + private onClicked (): void { ComponentEventHandler.emitEvents(this.videoPlayerEvent, this, VideoPlayerEventType.CLICKED); this.node.emit(VideoPlayerEventType.CLICKED, this); } diff --git a/cocos/web-view/web-view-impl-web.ts b/cocos/web-view/web-view-impl-web.ts index b4977e2fb16..d6215ede5bf 100644 --- a/cocos/web-view/web-view-impl-web.ts +++ b/cocos/web-view/web-view-impl-web.ts @@ -72,21 +72,23 @@ export class WebViewImplWeb extends WebViewImpl { const wrapper = ccdocument.createElement('div'); this._wrapper = wrapper; wrapper.id = 'webview-wrapper'; - wrapper.style['-webkit-overflow'] = 'auto'; - wrapper.style['-webkit-overflow-scrolling'] = 'touch'; - wrapper.style.position = 'absolute'; - wrapper.style.bottom = '0px'; - wrapper.style.left = '0px'; - wrapper.style.transformOrigin = '0px 100% 0px'; - wrapper.style['-webkit-transform-origin'] = '0px 100% 0px'; + const wrapperStyle = wrapper.style; + wrapperStyle['-webkit-overflow'] = 'auto'; + wrapperStyle['-webkit-overflow-scrolling'] = 'touch'; + wrapperStyle.position = 'absolute'; + wrapperStyle.bottom = '0px'; + wrapperStyle.left = '0px'; + wrapperStyle.transformOrigin = '0px 100% 0px'; + wrapperStyle['-webkit-transform-origin'] = '0px 100% 0px'; game.container!.appendChild(wrapper); const webview = ccdocument.createElement('iframe'); this._webview = webview; + const webviewStyle = webview.style; webview.id = 'webview'; - webview.style.border = 'none'; - webview.style.width = '100%'; - webview.style.height = '100%'; + webviewStyle.border = 'none'; + webviewStyle.width = '100%'; + webviewStyle.height = '100%'; wrapper.appendChild(webview); this._bindDomEvent(); } diff --git a/cocos/web-view/web-view.ts b/cocos/web-view/web-view.ts index 667f903c97d..cfb2e00c091 100644 --- a/cocos/web-view/web-view.ts +++ b/cocos/web-view/web-view.ts @@ -178,17 +178,17 @@ export class WebView extends Component { this._impl.loadURL(this._url); } - onLoading (): void { + private onLoading (): void { ComponentEventHandler.emitEvents(this.webviewEvents, this, WebViewEventType.LOADING); this.node.emit(WebViewEventType.LOADING, this); } - onLoaded (): void { + private onLoaded (): void { ComponentEventHandler.emitEvents(this.webviewEvents, this, WebViewEventType.LOADED); this.node.emit(WebViewEventType.LOADED, this); } - onError (...args: any[any]): void { + private onError (...args: any[any]): void { ComponentEventHandler.emitEvents(this.webviewEvents, this, WebViewEventType.ERROR, args); this.node.emit(WebViewEventType.ERROR, this, args); }