From 2c99cd6d1930dd1a28205c9e5814a868dcfd2adf Mon Sep 17 00:00:00 2001 From: huangxiaoyao <976125628@qq.com> Date: Fri, 25 Oct 2024 11:36:03 +0800 Subject: [PATCH 1/2] =?UTF-8?q?webview=5Fflutter=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E6=A8=AA=E5=B1=8F=E6=97=8B=E8=BD=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: huangxiaoyao <976125628@qq.com> --- .../lib/src/ohos_webview.dart | 5 +++ .../lib/src/ohos_webview.g.dart | 23 +++++++++++++ .../lib/src/ohos_webview_api_impls.dart | 11 ++++++ .../lib/src/ohos_webview_controller.dart | 7 ++++ .../webview_flutter/GeneratedOhosWebView.ets | 34 +++++++++++++++++++ .../webview_flutter/OhosWebView.ets | 2 ++ .../WebSettingsHostApiImpl.ets | 10 +++++- .../webview_flutter/WebViewHostApiImpl.ets | 33 ++++++++++++++++++ 8 files changed, 124 insertions(+), 1 deletion(-) diff --git a/packages/webview_flutter/webview_flutter_ohos/lib/src/ohos_webview.dart b/packages/webview_flutter/webview_flutter_ohos/lib/src/ohos_webview.dart index 551b5b545..df31d4fc7 100644 --- a/packages/webview_flutter/webview_flutter_ohos/lib/src/ohos_webview.dart +++ b/packages/webview_flutter/webview_flutter_ohos/lib/src/ohos_webview.dart @@ -717,6 +717,11 @@ class WebSettings extends OhosObject { return api.getUserAgentStringFromInstance(this); } + /// Enables or disables full screen rotate within WebView. + Future setAllowFullScreenRotate(bool enabled) { + return api.setAllowFullScreenRotateInstance(this, enabled); + } + @override WebSettings copy() { return WebSettings.detached( diff --git a/packages/webview_flutter/webview_flutter_ohos/lib/src/ohos_webview.g.dart b/packages/webview_flutter/webview_flutter_ohos/lib/src/ohos_webview.g.dart index fbae7a0dc..9b4101645 100644 --- a/packages/webview_flutter/webview_flutter_ohos/lib/src/ohos_webview.g.dart +++ b/packages/webview_flutter/webview_flutter_ohos/lib/src/ohos_webview.g.dart @@ -1552,6 +1552,29 @@ class WebSettingsHostApi { return (replyList[0] as String?)!; } } + + Future setAllowFullScreenRotate(int arg_instanceId, bool arg_enabled) async { + final BasicMessageChannel channel = BasicMessageChannel( + 'dev.flutter.pigeon.webview_flutter_ohos.WebSettingsHostApi.setAllowFullScreenRotate', + codec, + binaryMessenger: _binaryMessenger); + final List? replyList = await channel + .send([arg_instanceId, arg_enabled]) as List?; + if (replyList == null) { + throw PlatformException( + code: 'channel-error', + message: 'Unable to establish connection on channel.', + ); + } else if (replyList.length > 1) { + throw PlatformException( + code: replyList[0]! as String, + message: replyList[1] as String?, + details: replyList[2], + ); + } else { + return; + } + } } class JavaScriptChannelHostApi { diff --git a/packages/webview_flutter/webview_flutter_ohos/lib/src/ohos_webview_api_impls.dart b/packages/webview_flutter/webview_flutter_ohos/lib/src/ohos_webview_api_impls.dart index 93b896ab7..0a0935dc4 100644 --- a/packages/webview_flutter/webview_flutter_ohos/lib/src/ohos_webview_api_impls.dart +++ b/packages/webview_flutter/webview_flutter_ohos/lib/src/ohos_webview_api_impls.dart @@ -588,6 +588,17 @@ class WebSettingsHostApiImpl extends WebSettingsHostApi { Future getUserAgentStringFromInstance(WebSettings instance) { return getUserAgentString(instanceManager.getIdentifier(instance)!); } + + /// Helper method to convert instances ids to objects. + Future setAllowFullScreenRotateInstance( + WebSettings instance, + bool enabled, + ) { + return setAllowFullScreenRotate( + instanceManager.getIdentifier(instance)!, + enabled, + ); + } } /// Host api implementation for [JavaScriptChannel]. diff --git a/packages/webview_flutter/webview_flutter_ohos/lib/src/ohos_webview_controller.dart b/packages/webview_flutter/webview_flutter_ohos/lib/src/ohos_webview_controller.dart index e33a54b47..79ddbe0a8 100644 --- a/packages/webview_flutter/webview_flutter_ohos/lib/src/ohos_webview_controller.dart +++ b/packages/webview_flutter/webview_flutter_ohos/lib/src/ohos_webview_controller.dart @@ -39,6 +39,7 @@ class OhosWebViewControllerCreationParams extends PlatformWebViewControllerCreationParams { /// Creates a new [OhosWebViewControllerCreationParams] instance. OhosWebViewControllerCreationParams({ + bool? this.isAllowFullScreenRotate = false, @visibleForTesting this.ohosWebViewProxy = const OhosWebViewProxy(), @visibleForTesting ohos_webview.WebStorage? ohosWebStorage, }) : ohosWebStorage = @@ -50,17 +51,22 @@ class OhosWebViewControllerCreationParams // Recommended placeholder to prevent being broken by platform interface. // ignore: avoid_unused_constructor_parameters PlatformWebViewControllerCreationParams params, { + bool? isAllowFullScreenRotate = false, @visibleForTesting OhosWebViewProxy ohosWebViewProxy = const OhosWebViewProxy(), @visibleForTesting ohos_webview.WebStorage? ohosWebStorage, }) { return OhosWebViewControllerCreationParams( + isAllowFullScreenRotate: isAllowFullScreenRotate, ohosWebViewProxy: ohosWebViewProxy, ohosWebStorage: ohosWebStorage ?? ohos_webview.WebStorage.instance, ); } + /// Enables or disables full screen rotate within WebView. + bool? isAllowFullScreenRotate; + /// Handles constructing objects and calling static methods for the Ohos WebView /// native library. @visibleForTesting @@ -94,6 +100,7 @@ class OhosWebViewController extends PlatformWebViewController { ? params : OhosWebViewControllerCreationParams .fromPlatformWebViewControllerCreationParams(params)) { + _webView.settings.setAllowFullScreenRotate(params is OhosWebViewControllerCreationParams ? params.isAllowFullScreenRotate ?? false : false); _webView.settings.setDomStorageEnabled(true); _webView.settings.setJavaScriptCanOpenWindowsAutomatically(true); _webView.settings.setSupportMultipleWindows(true); diff --git a/packages/webview_flutter/webview_flutter_ohos/ohos/src/main/ets/io.flutter.plugins/webview_flutter/GeneratedOhosWebView.ets b/packages/webview_flutter/webview_flutter_ohos/ohos/src/main/ets/io.flutter.plugins/webview_flutter/GeneratedOhosWebView.ets index afe1c6843..11720b2ff 100644 --- a/packages/webview_flutter/webview_flutter_ohos/ohos/src/main/ets/io.flutter.plugins/webview_flutter/GeneratedOhosWebView.ets +++ b/packages/webview_flutter/webview_flutter_ohos/ohos/src/main/ets/io.flutter.plugins/webview_flutter/GeneratedOhosWebView.ets @@ -2188,6 +2188,8 @@ export abstract class WebSettingsHostApi { abstract getUserAgentString(instanceId: number): Promise; + abstract setAllowFullScreenRotate(instanceId: number, enabled: boolean): void; + static getCodec(): MessageCodec { return new StandardMessageCodec(); } @@ -2699,6 +2701,38 @@ export abstract class WebSettingsHostApi { channel.setMessageHandler(null); } } + { + const channel: BasicMessageChannel = + new BasicMessageChannel( + binaryMessenger, + "dev.flutter.pigeon.webview_flutter_ohos.WebSettingsHostApi.setAllowFullScreenRotate", + WebSettingsHostApi.getCodec() + ); + if (api != null) { + channel.setMessageHandler( + { + onMessage: (message: ESObject, reply: Reply) => { + let wrapped: ArrayList = new ArrayList(); + const args: Array = message; + const instanceIdArg: number = args[0]; + const enabledArg: boolean = args[1]; + try { + api.setAllowFullScreenRotate( + instanceIdArg, enabledArg + ); + wrapped.insert(null, 0); + } catch (err) { + const wrappedError: ArrayList = GeneratedOhosWebView.wrapError(err); + wrapped = wrappedError; + } + reply.reply(wrapped.convertToArray()); + } + } + ); + } else { + channel.setMessageHandler(null); + } + } } } diff --git a/packages/webview_flutter/webview_flutter_ohos/ohos/src/main/ets/io.flutter.plugins/webview_flutter/OhosWebView.ets b/packages/webview_flutter/webview_flutter_ohos/ohos/src/main/ets/io.flutter.plugins/webview_flutter/OhosWebView.ets index 3f6f32211..299064999 100644 --- a/packages/webview_flutter/webview_flutter_ohos/ohos/src/main/ets/io.flutter.plugins/webview_flutter/OhosWebView.ets +++ b/packages/webview_flutter/webview_flutter_ohos/ohos/src/main/ets/io.flutter.plugins/webview_flutter/OhosWebView.ets @@ -95,6 +95,8 @@ export struct OhosWebView { } }) ) + .onFullScreenEnter(this.webView.onFullScreenEnter) + .onFullScreenExit(this.webView.onFullScreenExit) } } } diff --git a/packages/webview_flutter/webview_flutter_ohos/ohos/src/main/ets/io.flutter.plugins/webview_flutter/WebSettingsHostApiImpl.ets b/packages/webview_flutter/webview_flutter_ohos/ohos/src/main/ets/io.flutter.plugins/webview_flutter/WebSettingsHostApiImpl.ets index cc6f5912e..3719baa31 100644 --- a/packages/webview_flutter/webview_flutter_ohos/ohos/src/main/ets/io.flutter.plugins/webview_flutter/WebSettingsHostApiImpl.ets +++ b/packages/webview_flutter/webview_flutter_ohos/ohos/src/main/ets/io.flutter.plugins/webview_flutter/WebSettingsHostApiImpl.ets @@ -68,10 +68,13 @@ export interface WebSettings { getTextZoom(): number; - getUserAgentString(): Promise; onTextZoomChanged(callback: (ratio: number) => void): void; + + setAllowFullScreenRotate(enabled: boolean): void; + + getAllowFullScreenRotate(): boolean; } export class WebSettingsCreator { @@ -170,4 +173,9 @@ export class WebSettingsHostApiImpl extends WebSettingsHostApi { const webSettings:WebSettings = this.instanceManager.getInstance(instanceId); return webSettings.getUserAgentString(); } + + setAllowFullScreenRotate(instanceId: number, enabled: boolean) { + const webSettings:WebSettings = this.instanceManager.getInstance(instanceId); + webSettings.setAllowFullScreenRotate(enabled); + } } \ No newline at end of file diff --git a/packages/webview_flutter/webview_flutter_ohos/ohos/src/main/ets/io.flutter.plugins/webview_flutter/WebViewHostApiImpl.ets b/packages/webview_flutter/webview_flutter_ohos/ohos/src/main/ets/io.flutter.plugins/webview_flutter/WebViewHostApiImpl.ets index 63147881d..1177a839b 100644 --- a/packages/webview_flutter/webview_flutter_ohos/ohos/src/main/ets/io.flutter.plugins/webview_flutter/WebViewHostApiImpl.ets +++ b/packages/webview_flutter/webview_flutter_ohos/ohos/src/main/ets/io.flutter.plugins/webview_flutter/WebViewHostApiImpl.ets @@ -32,6 +32,8 @@ import { DisplayListenerProxy } from './DisplayListenerProxy' import { JavaScriptChannel } from './JavaScriptChannel' import Log from '@ohos/flutter_ohos/src/main/ets/util/Log'; import { WebViewFlutterApiImpl } from './WebViewFlutterApiImpl.ets' +import window from '@ohos.window' +import display from '@ohos.display' const TAG: string = "WebViewHostApiImpl"; @@ -299,6 +301,8 @@ export class WebViewPlatformView extends PlatformView { private onDisposeCallback?: () => void; + private rotation: number = 0; + onDownloadStart = (event: ESObject) => { if (this.downloadListener && this.downloadListener.onDownloadStart) { this.downloadListener.onDownloadStart( @@ -436,6 +440,27 @@ export class WebViewPlatformView extends PlatformView { return true; } + onFullScreenEnter = (event: ESObject) => { + let context = getContext(this) as common.UIAbilityContext; + let windowClass = context.windowStage.getMainWindowSync(); + windowClass.setWindowLayoutFullScreen(true); + windowClass.setWindowSystemBarEnable([]); + this.rotation = windowClass.getPreferredOrientation(); + let orientation: number = display.getDefaultDisplaySync().orientation; + let isAllowFullScreenRotate = this.getWebSettings()?.getAllowFullScreenRotate() + if (isAllowFullScreenRotate && (orientation == display.Orientation.PORTRAIT || orientation == display.Orientation.PORTRAIT_INVERTED)) { + windowClass.setPreferredOrientation(window.Orientation.LANDSCAPE); + } + } + + onFullScreenExit = () => { + let context = getContext(this) as common.UIAbilityContext; + let windowClass = context.windowStage.getMainWindowSync(); + windowClass.setWindowLayoutFullScreen(false); + windowClass.setWindowSystemBarEnable(['status', 'navigation']); + windowClass.setPreferredOrientation(this.rotation); + } + getType(): string { return 'web'; } @@ -552,6 +577,7 @@ class WebSettingsImpl implements WebSettings { fileAccess : boolean = true; textZoomRatio : number = 0; textZoomRatioCallback?: (ratio: number) => void; + isAllowFullScreenRotate: boolean = false; constructor(webView: WebViewPlatformView) { this.webView = webView; @@ -671,4 +697,11 @@ class WebSettingsImpl implements WebSettings { this.textZoomRatioCallback = callback; } + setAllowFullScreenRotate(enabled: boolean) { + this.isAllowFullScreenRotate = enabled; + } + + getAllowFullScreenRotate() : boolean { + return this.isAllowFullScreenRotate; + } } \ No newline at end of file -- Gitee From b22fc334dbee06ed32c94d418606393ca2e0e38d Mon Sep 17 00:00:00 2001 From: huangxiaoyao <976125628@qq.com> Date: Wed, 6 Nov 2024 09:37:53 +0800 Subject: [PATCH 2/2] =?UTF-8?q?webview=5Fflutter=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E6=A8=AA=E5=B1=8F=E6=97=8B=E8=BD=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: huangxiaoyao <976125628@qq.com> --- .../io.flutter.plugins/webview_flutter/WebViewHostApiImpl.ets | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/webview_flutter/webview_flutter_ohos/ohos/src/main/ets/io.flutter.plugins/webview_flutter/WebViewHostApiImpl.ets b/packages/webview_flutter/webview_flutter_ohos/ohos/src/main/ets/io.flutter.plugins/webview_flutter/WebViewHostApiImpl.ets index ede732229..b179e03c5 100644 --- a/packages/webview_flutter/webview_flutter_ohos/ohos/src/main/ets/io.flutter.plugins/webview_flutter/WebViewHostApiImpl.ets +++ b/packages/webview_flutter/webview_flutter_ohos/ohos/src/main/ets/io.flutter.plugins/webview_flutter/WebViewHostApiImpl.ets @@ -443,11 +443,10 @@ export class WebViewPlatformView extends PlatformView { onFullScreenEnter = (event: ESObject) => { let context = getContext(this) as common.UIAbilityContext; let windowClass = context.windowStage.getMainWindowSync(); - windowClass.setWindowLayoutFullScreen(true); windowClass.setWindowSystemBarEnable([]); this.rotation = windowClass.getPreferredOrientation(); let orientation: number = display.getDefaultDisplaySync().orientation; - let isAllowFullScreenRotate = this.getWebSettings()?.getAllowFullScreenRotate() + let isAllowFullScreenRotate = this.getWebSettings()?.getAllowFullScreenRotate(); if (isAllowFullScreenRotate && (orientation == display.Orientation.PORTRAIT || orientation == display.Orientation.PORTRAIT_INVERTED)) { windowClass.setPreferredOrientation(window.Orientation.LANDSCAPE); } @@ -456,7 +455,6 @@ export class WebViewPlatformView extends PlatformView { onFullScreenExit = () => { let context = getContext(this) as common.UIAbilityContext; let windowClass = context.windowStage.getMainWindowSync(); - windowClass.setWindowLayoutFullScreen(false); windowClass.setWindowSystemBarEnable(['status', 'navigation']); windowClass.setPreferredOrientation(this.rotation); } -- Gitee