This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[webview_flutter_android] Adds support for use of old hybrid composition #6063
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fd4f93d
add webview expensive surface android
bparrishMines 35ba65c
version bump
bparrishMines 8df3aca
docs and stuff
bparrishMines b33b740
license and flutter version bump
bparrishMines c445c8e
spelling
bparrishMines c39b717
update to automatically switch
bparrishMines 850b3ed
fix docs
bparrishMines 854a311
helper method
bparrishMines File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,14 +14,20 @@ import 'src/instance_manager.dart'; | |
| import 'webview_android.dart'; | ||
| import 'webview_android_widget.dart'; | ||
|
|
||
| /// Android [WebViewPlatform] that uses [AndroidViewSurface] to build the [WebView] widget. | ||
| /// Android [WebViewPlatform] that uses [AndroidViewSurface] to build the | ||
| /// [WebView] widget. | ||
| /// | ||
| /// To use this, set [WebView.platform] to an instance of this class. | ||
| /// | ||
| /// This implementation uses hybrid composition to render the [WebView] on | ||
| /// This implementation uses [AndroidViewSurface] to render the [WebView] on | ||
| /// Android. It solves multiple issues related to accessibility and interaction | ||
| /// with the [WebView] at the cost of some performance on Android versions below | ||
| /// 10. See https://github.com/flutter/flutter/wiki/Hybrid-Composition for more | ||
| /// 10. | ||
| /// | ||
| /// To support transparent backgrounds on all Android devices, this | ||
| /// implementation uses hybrid composition when the opacity of | ||
| /// `CreationParams.backgroundColor` is less than 1.0. See | ||
| /// https://github.com/flutter/flutter/wiki/Hybrid-Composition for more | ||
| /// information. | ||
| class SurfaceAndroidWebView extends AndroidWebView { | ||
| @override | ||
|
|
@@ -53,16 +59,23 @@ class SurfaceAndroidWebView extends AndroidWebView { | |
| ); | ||
| }, | ||
| onCreatePlatformView: (PlatformViewCreationParams params) { | ||
| return PlatformViewsService.initSurfaceAndroidView( | ||
| final Color? backgroundColor = creationParams.backgroundColor; | ||
| return _createViewController( | ||
| // On some Android devices, transparent backgrounds can cause | ||
| // rendering issues on the non hybrid composition | ||
| // AndroidViewSurface. This switches the WebView to Hybrid | ||
| // Composition when the background color is not 100% opaque. | ||
| hybridComposition: | ||
| backgroundColor != null && backgroundColor.opacity < 1.0, | ||
| id: params.id, | ||
| viewType: 'plugins.flutter.io/webview', | ||
| // WebView content is not affected by the Android view's layout direction, | ||
| // we explicitly set it here so that the widget doesn't require an ambient | ||
| // directionality. | ||
| layoutDirection: TextDirection.rtl, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This shouldn't be a breaking change for large majority of people. This value is ignored unless support for this is explicitly added to the |
||
| creationParams: | ||
| InstanceManager.instance.getInstanceId(controller.webView), | ||
| creationParamsCodec: const StandardMessageCodec(), | ||
| layoutDirection: | ||
| Directionality.maybeOf(context) ?? TextDirection.ltr, | ||
| webViewIdentifier: | ||
| InstanceManager.instance.getInstanceId(controller.webView)!, | ||
| ) | ||
| ..addOnPlatformViewCreatedListener(params.onPlatformViewCreated) | ||
| ..addOnPlatformViewCreatedListener((int id) { | ||
|
|
@@ -76,4 +89,29 @@ class SurfaceAndroidWebView extends AndroidWebView { | |
| }, | ||
| ); | ||
| } | ||
|
|
||
| AndroidViewController _createViewController({ | ||
| required bool hybridComposition, | ||
| required int id, | ||
| required String viewType, | ||
| required TextDirection layoutDirection, | ||
| required int webViewIdentifier, | ||
| }) { | ||
| if (hybridComposition) { | ||
| return PlatformViewsService.initExpensiveAndroidView( | ||
| id: id, | ||
| viewType: viewType, | ||
| layoutDirection: layoutDirection, | ||
| creationParams: webViewIdentifier, | ||
| creationParamsCodec: const StandardMessageCodec(), | ||
| ); | ||
| } | ||
| return PlatformViewsService.initSurfaceAndroidView( | ||
| id: id, | ||
| viewType: viewType, | ||
| layoutDirection: layoutDirection, | ||
| creationParams: webViewIdentifier, | ||
| creationParamsCodec: const StandardMessageCodec(), | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,11 +2,11 @@ name: webview_flutter_android | |
| description: A Flutter plugin that provides a WebView widget on Android. | ||
| repository: https://github.com/flutter/plugins/tree/main/packages/webview_flutter/webview_flutter_android | ||
| issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview%22 | ||
| version: 2.8.14 | ||
| version: 2.9.0 | ||
|
|
||
| environment: | ||
| sdk: ">=2.14.0 <3.0.0" | ||
| flutter: ">=2.8.0" | ||
| flutter: ">=3.0.0" | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Required for |
||
|
|
||
| flutter: | ||
| plugin: | ||
|
|
||
115 changes: 115 additions & 0 deletions
115
packages/webview_flutter/webview_flutter_android/test/surface_android_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'dart:async'; | ||
|
|
||
| import 'package:flutter/material.dart'; | ||
| import 'package:flutter/services.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:webview_flutter_android/webview_surface_android.dart'; | ||
| import 'package:webview_flutter_platform_interface/webview_flutter_platform_interface.dart'; | ||
|
|
||
| void main() { | ||
| TestWidgetsFlutterBinding.ensureInitialized(); | ||
|
|
||
| group('SurfaceAndroidWebView', () { | ||
| late List<MethodCall> log; | ||
|
|
||
| setUpAll(() { | ||
| SystemChannels.platform_views.setMockMethodCallHandler( | ||
| (MethodCall call) async { | ||
| log.add(call); | ||
| if (call.method == 'resize') { | ||
| return <String, Object?>{ | ||
| 'width': call.arguments['width'], | ||
| 'height': call.arguments['height'], | ||
| }; | ||
| } | ||
| }, | ||
| ); | ||
| }); | ||
|
|
||
| tearDownAll(() { | ||
| SystemChannels.platform_views.setMockMethodCallHandler(null); | ||
| }); | ||
|
|
||
| setUp(() { | ||
| log = <MethodCall>[]; | ||
| }); | ||
|
|
||
| testWidgets( | ||
| 'uses hybrid composition when background color is not 100% opaque', | ||
| (WidgetTester tester) async { | ||
| await tester.pumpWidget(Builder(builder: (BuildContext context) { | ||
| return SurfaceAndroidWebView().build( | ||
| context: context, | ||
| creationParams: CreationParams( | ||
| backgroundColor: Colors.transparent, | ||
| webSettings: WebSettings( | ||
| userAgent: const WebSetting<String?>.absent(), | ||
| hasNavigationDelegate: false, | ||
| )), | ||
| javascriptChannelRegistry: JavascriptChannelRegistry(null), | ||
| webViewPlatformCallbacksHandler: | ||
| TestWebViewPlatformCallbacksHandler(), | ||
| ); | ||
| })); | ||
| await tester.pumpAndSettle(); | ||
|
|
||
| final MethodCall createMethodCall = log[0]; | ||
| expect(createMethodCall.method, 'create'); | ||
| expect(createMethodCall.arguments, containsPair('hybrid', true)); | ||
| }); | ||
|
|
||
| testWidgets('default text direction is ltr', (WidgetTester tester) async { | ||
| await tester.pumpWidget(Builder(builder: (BuildContext context) { | ||
| return SurfaceAndroidWebView().build( | ||
| context: context, | ||
| creationParams: CreationParams( | ||
| webSettings: WebSettings( | ||
| userAgent: const WebSetting<String?>.absent(), | ||
| hasNavigationDelegate: false, | ||
| )), | ||
| javascriptChannelRegistry: JavascriptChannelRegistry(null), | ||
| webViewPlatformCallbacksHandler: | ||
| TestWebViewPlatformCallbacksHandler(), | ||
| ); | ||
| })); | ||
| await tester.pumpAndSettle(); | ||
|
|
||
| final MethodCall createMethodCall = log[0]; | ||
| expect(createMethodCall.method, 'create'); | ||
| expect( | ||
| createMethodCall.arguments, | ||
| containsPair( | ||
| 'direction', | ||
| AndroidViewController.kAndroidLayoutDirectionLtr, | ||
| ), | ||
| ); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| class TestWebViewPlatformCallbacksHandler | ||
| implements WebViewPlatformCallbacksHandler { | ||
| @override | ||
| FutureOr<bool> onNavigationRequest({ | ||
| required String url, | ||
| required bool isForMainFrame, | ||
| }) { | ||
| throw UnimplementedError(); | ||
| } | ||
|
|
||
| @override | ||
| void onPageFinished(String url) {} | ||
|
|
||
| @override | ||
| void onPageStarted(String url) {} | ||
|
|
||
| @override | ||
| void onProgress(int progress) {} | ||
|
|
||
| @override | ||
| void onWebResourceError(WebResourceError error) {} | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is no longer true for this
AndroidView. I moved it to the new one.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe the parts about accessibility and interaction are still true, relative to VD?
We should document the known tradeoffs of this relative to the other two here. We should also add that to the README for this package, and/or some Flutter docs page somewhere (and then later we can link to that from the main README).