-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[webview_flutter_android] Adds support for use of old hybrid composition #6063
Changes from 4 commits
fd4f93d
35ba65c
8df3aca
b33b740
c445c8e
c39b717
850b3ed
854a311
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// 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 'package:flutter/foundation.dart'; | ||
import 'package:flutter/gestures.dart'; | ||
import 'package:flutter/rendering.dart'; | ||
import 'package:flutter/services.dart'; | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:webview_flutter_platform_interface/webview_flutter_platform_interface.dart'; | ||
|
||
import 'src/android_webview.dart'; | ||
import 'src/instance_manager.dart'; | ||
import 'webview_android.dart'; | ||
import 'webview_android_widget.dart'; | ||
|
||
/// 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 | ||
/// 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 | ||
/// information. | ||
class ExpensiveSurfaceAndroidWebView extends AndroidWebView { | ||
@override | ||
Widget build({ | ||
required BuildContext context, | ||
required CreationParams creationParams, | ||
required JavascriptChannelRegistry javascriptChannelRegistry, | ||
WebViewPlatformCreatedCallback? onWebViewPlatformCreated, | ||
Set<Factory<OneSequenceGestureRecognizer>>? gestureRecognizers, | ||
required WebViewPlatformCallbacksHandler webViewPlatformCallbacksHandler, | ||
}) { | ||
return WebViewAndroidWidget( | ||
useHybridComposition: true, | ||
creationParams: creationParams, | ||
callbacksHandler: webViewPlatformCallbacksHandler, | ||
javascriptChannelRegistry: javascriptChannelRegistry, | ||
onBuildWidget: (WebViewAndroidPlatformController controller) { | ||
return PlatformViewLink( | ||
viewType: 'plugins.flutter.io/webview', | ||
surfaceFactory: ( | ||
BuildContext context, | ||
PlatformViewController controller, | ||
) { | ||
return AndroidViewSurface( | ||
controller: controller as AndroidViewController, | ||
gestureRecognizers: gestureRecognizers ?? | ||
const <Factory<OneSequenceGestureRecognizer>>{}, | ||
hitTestBehavior: PlatformViewHitTestBehavior.opaque, | ||
); | ||
}, | ||
onCreatePlatformView: (PlatformViewCreationParams params) { | ||
return PlatformViewsService.initExpensiveAndroidView( | ||
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: | ||
Directionality.maybeOf(context) ?? TextDirection.ltr, | ||
creationParams: | ||
InstanceManager.instance.getInstanceId(controller.webView), | ||
creationParamsCodec: const StandardMessageCodec(), | ||
) | ||
..addOnPlatformViewCreatedListener(params.onPlatformViewCreated) | ||
..addOnPlatformViewCreatedListener((int id) { | ||
if (onWebViewPlatformCreated != null) { | ||
onWebViewPlatformCreated(controller); | ||
} | ||
}) | ||
..create(); | ||
}, | ||
); | ||
}, | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,12 +17,6 @@ import 'webview_android_widget.dart'; | |
/// 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 | ||
/// 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 | ||
/// information. | ||
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 is no longer true for this 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. 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). |
||
class SurfaceAndroidWebView extends AndroidWebView { | ||
@override | ||
Widget build({ | ||
|
@@ -59,7 +53,8 @@ class SurfaceAndroidWebView extends AndroidWebView { | |
// 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, | ||
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 |
||
layoutDirection: | ||
Directionality.maybeOf(context) ?? TextDirection.ltr, | ||
creationParams: | ||
InstanceManager.instance.getInstanceId(controller.webView), | ||
creationParamsCodec: const StandardMessageCodec(), | ||
|
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" | ||
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: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
// 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/services.dart'; | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:webview_flutter_android/webview_expensive_surface_android.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('ExpensiveSurfaceAndroidWebView', () { | ||
late List<MethodCall> log; | ||
|
||
setUpAll(() { | ||
SystemChannels.platform_views.setMockMethodCallHandler( | ||
(MethodCall call) async { | ||
log.add(call); | ||
}, | ||
); | ||
}); | ||
|
||
tearDownAll(() { | ||
SystemChannels.platform_views.setMockMethodCallHandler(null); | ||
}); | ||
|
||
setUp(() { | ||
log = <MethodCall>[]; | ||
}); | ||
|
||
testWidgets('uses hybrid composition', (WidgetTester tester) async { | ||
await tester.pumpWidget(Builder(builder: (BuildContext context) { | ||
return ExpensiveSurfaceAndroidWebView().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('hybrid', true)); | ||
}); | ||
|
||
testWidgets('default text direction is ltr', (WidgetTester tester) async { | ||
await tester.pumpWidget(Builder(builder: (BuildContext context) { | ||
return ExpensiveSurfaceAndroidWebView().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, | ||
), | ||
); | ||
}); | ||
}); | ||
|
||
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('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) {} | ||
} |
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.
Now that we have three options, we need to be clear in each one what we are comparing against. E.g., my understanding is that the new mode (I'm just going to call it Mode3 until we pick a name 🙂 ) doesn't have accessibility issues, and that that's specific to the old VirtualDisplay implementation.
The advantage of this one vs.
SurfaceAndroidWebView
AFAIK is transparency support on older versions of Android. Are there other know issue with Mode3 relative to HC?