iosWebViewFix/flutter_inappwebview_web/lib/web/web_platform.dart

121 lines
4.2 KiB
Dart
Raw Normal View History

2022-04-21 21:14:51 +00:00
import 'dart:async';
import '../src/inappwebview_platform.dart';
import 'headless_inappwebview_manager.dart';
2022-04-21 21:14:51 +00:00
import 'web_platform_manager.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'in_app_web_view_web_element.dart';
import 'platform_util.dart';
2022-04-22 00:24:50 +00:00
import 'package:js/js.dart';
import 'shims/platform_view_registry.dart' show platformViewRegistry;
2022-04-21 21:14:51 +00:00
/// Builds an iframe based WebView.
///
2023-11-24 16:56:57 +00:00
/// This is used as the default implementation for `WebView` on web.
class InAppWebViewFlutterPlugin {
/// Constructs a new instance of [InAppWebViewFlutterPlugin].
InAppWebViewFlutterPlugin(Registrar registrar) {
platformViewRegistry.registerViewFactory(
2022-04-25 21:21:26 +00:00
'com.pichillilorenzo/flutter_inappwebview', (int viewId) {
var webView =
InAppWebViewWebElement(viewId: viewId, messenger: registrar);
WebPlatformManager.webViews.putIfAbsent(viewId, () => webView);
return webView.iframeContainer;
2022-04-25 21:21:26 +00:00
});
2022-04-21 21:14:51 +00:00
}
static void registerWith(Registrar registrar) {
WebPlatformInAppWebViewPlatform.registerWith();
// ignore: unused_local_variable
final pluginInstance = InAppWebViewFlutterPlugin(registrar);
// ignore: unused_local_variable
final platformUtil = PlatformUtil(messenger: registrar);
// ignore: unused_local_variable
final headlessManager = HeadlessInAppWebViewManager(messenger: registrar);
2022-04-22 00:24:50 +00:00
_nativeCommunication = allowInterop(_dartNativeCommunication);
2022-04-21 21:14:51 +00:00
}
2022-04-22 00:24:50 +00:00
}
/// Allows assigning a function to be callable from `window.flutter_inappwebview.nativeCommunication()`
@JS('flutter_inappwebview.nativeCommunication')
2022-04-25 21:21:26 +00:00
external set _nativeCommunication(
Future<dynamic> Function(String method, dynamic viewId, [List? args]) f);
2022-04-22 00:24:50 +00:00
/// Allows calling the assigned function from Dart as well.
@JS()
2022-04-25 21:21:26 +00:00
external Future<dynamic> nativeCommunication(String method, dynamic viewId,
[List? args]);
2022-04-22 00:24:50 +00:00
2022-04-25 21:21:26 +00:00
Future<dynamic> _dartNativeCommunication(String method, dynamic viewId,
[List? args]) async {
2022-04-22 00:24:50 +00:00
if (WebPlatformManager.webViews.containsKey(viewId)) {
2022-04-25 21:21:26 +00:00
var webViewHtmlElement =
WebPlatformManager.webViews[viewId] as InAppWebViewWebElement;
2022-04-22 00:24:50 +00:00
switch (method) {
2022-04-22 11:39:21 +00:00
case 'onLoadStart':
2022-04-27 14:59:49 +00:00
String url = args![0];
2022-04-22 11:39:21 +00:00
webViewHtmlElement.onLoadStart(url);
break;
case 'onLoadStop':
2022-04-27 14:59:49 +00:00
String url = args![0];
2022-04-22 11:39:21 +00:00
webViewHtmlElement.onLoadStop(url);
break;
case 'onUpdateVisitedHistory':
2022-04-27 14:59:49 +00:00
String url = args![0];
2022-04-22 11:39:21 +00:00
webViewHtmlElement.onUpdateVisitedHistory(url);
2022-04-22 00:24:50 +00:00
break;
case 'onScrollChanged':
2022-04-27 14:59:49 +00:00
int x = (args![0] as double).toInt();
int y = (args[1] as double).toInt();
webViewHtmlElement.onScrollChanged(x, y);
break;
case 'onConsoleMessage':
2022-04-27 14:59:49 +00:00
String type = args![0];
String? message = args[1];
webViewHtmlElement.onConsoleMessage(type, message);
break;
2022-04-23 20:10:02 +00:00
case 'onCreateWindow':
2022-04-27 14:59:49 +00:00
int windowId = args![0];
String url = args[1] ?? 'about:blank';
String? target = args[2];
String? windowFeatures = args[3];
2022-04-25 21:21:26 +00:00
return await webViewHtmlElement.onCreateWindow(
windowId, url, target, windowFeatures);
2022-04-23 20:10:02 +00:00
case 'onWindowFocus':
webViewHtmlElement.onWindowFocus();
break;
case 'onWindowBlur':
webViewHtmlElement.onWindowBlur();
break;
case 'onPrintRequest':
2022-04-27 14:59:49 +00:00
String? url = args![0];
webViewHtmlElement.onPrintRequest(url);
2022-04-23 20:10:02 +00:00
break;
case 'onEnterFullscreen':
webViewHtmlElement.onEnterFullscreen();
break;
case 'onExitFullscreen':
webViewHtmlElement.onExitFullscreen();
break;
case 'onTitleChanged':
2022-04-27 14:59:49 +00:00
String? title = args![0];
2022-04-23 20:10:02 +00:00
webViewHtmlElement.onTitleChanged(title);
break;
case 'onZoomScaleChanged':
2022-04-27 14:59:49 +00:00
double oldScale = args![0];
double newScale = args[1];
2022-04-23 20:10:02 +00:00
webViewHtmlElement.onZoomScaleChanged(oldScale, newScale);
break;
2022-04-27 14:59:49 +00:00
case 'onInjectedScriptLoaded':
String id = args![0];
webViewHtmlElement.onInjectedScriptLoaded(id);
break;
case 'onInjectedScriptError':
String id = args![0];
webViewHtmlElement.onInjectedScriptError(id);
break;
2022-04-22 00:24:50 +00:00
}
}
2022-04-25 21:21:26 +00:00
}