From aa09320613c8509448541f46f7299e05b9d1fe20 Mon Sep 17 00:00:00 2001 From: Eilon Lipton Date: Tue, 12 Mar 2024 11:04:24 -0700 Subject: [PATCH] Expose platform webview for initialization to enable customization (#53) Fixes #45 --- HybridWebView/HybridWebView.cs | 19 +++++++- .../HybridWebViewInitializedEventArgs.cs | 43 +++++++++++++++++++ .../Android/HybridWebView.Android.cs | 7 +-- MauiCSharpInteropWebView/MainPage.xaml.cs | 13 +++++- 4 files changed, 74 insertions(+), 8 deletions(-) create mode 100644 HybridWebView/HybridWebViewInitializedEventArgs.cs diff --git a/HybridWebView/HybridWebView.cs b/HybridWebView/HybridWebView.cs index e655111..5f7d001 100644 --- a/HybridWebView/HybridWebView.cs +++ b/HybridWebView/HybridWebView.cs @@ -35,14 +35,22 @@ public partial class HybridWebView : WebView /// public bool EnableWebDevTools { get; set; } + /// + /// Raised when a raw message is received from the web view. Raw messages are strings that have no additional processing. + /// public event EventHandler? RawMessageReceived; /// - /// Async event handler that is called when a proxy request is received from the webview. + /// Async event handler that is called when a proxy request is received from the web view. /// - public event Func? ProxyRequestReceived; + /// + /// Raised after the web view is initialized but before any content has been loaded into the web view. The event arguments provide the instance of the platform-specific web view control. + /// + public event EventHandler? HybridWebViewInitialized; + + public void Navigate(string url) { NavigateCore(url); @@ -54,6 +62,13 @@ protected override async void OnHandlerChanged() await InitializeHybridWebView(); + HybridWebViewInitialized?.Invoke(this, new HybridWebViewInitializedEventArgs() + { +#if ANDROID || IOS || MACCATALYST || WINDOWS + WebView = PlatformWebView, +#endif + }); + Navigate(StartPath); } diff --git a/HybridWebView/HybridWebViewInitializedEventArgs.cs b/HybridWebView/HybridWebViewInitializedEventArgs.cs new file mode 100644 index 0000000..f6ab18f --- /dev/null +++ b/HybridWebView/HybridWebViewInitializedEventArgs.cs @@ -0,0 +1,43 @@ +#if WINDOWS +using Microsoft.Web.WebView2.Core; +using WebView2Control = Microsoft.UI.Xaml.Controls.WebView2; +#elif ANDROID +using AWebView = Android.Webkit.WebView; +#elif IOS || MACCATALYST +using WebKit; +#elif TIZEN +using TWebView = Tizen.WebView.WebView; +#endif + +namespace HybridWebView +{ + /// + /// Allows configuring the underlying web view after it has been initialized. + /// + public class HybridWebViewInitializedEventArgs : EventArgs + { +#nullable disable +#if WINDOWS + /// + /// Gets the instance that was initialized. + /// + public WebView2Control WebView { get; internal set; } +#elif ANDROID + /// + /// Gets the instance that was initialized. + /// + public AWebView WebView { get; internal set; } +#elif MACCATALYST || IOS + /// + /// Gets the instance that was initialized. + /// the default values to allow further configuring additional options. + /// + public WKWebView WebView { get; internal set; } +#elif TIZEN + /// + /// Gets the instance that was initialized. + /// + public TWebView WebView { get; internal set; } +#endif + } +} diff --git a/HybridWebView/Platforms/Android/HybridWebView.Android.cs b/HybridWebView/Platforms/Android/HybridWebView.Android.cs index ecec40e..5fcb84b 100644 --- a/HybridWebView/Platforms/Android/HybridWebView.Android.cs +++ b/HybridWebView/Platforms/Android/HybridWebView.Android.cs @@ -6,15 +6,12 @@ namespace HybridWebView { partial class HybridWebView { - // Using an IP address means that WebView2 doesn't wait for any DNS resolution, - // making it substantially faster. Note that this isn't real HTTP traffic, since - // we intercept all the requests within this origin. - internal static readonly string AppHostAddress = "0.0.0.0"; + private static readonly string AppHostAddress = "0.0.0.0"; /// /// Gets the application's base URI. Defaults to https://0.0.0.0/ /// - internal static readonly string AppOrigin = $"https://{AppHostAddress}/"; + private static readonly string AppOrigin = $"https://{AppHostAddress}/"; internal static readonly Uri AppOriginUri = new(AppOrigin); diff --git a/MauiCSharpInteropWebView/MainPage.xaml.cs b/MauiCSharpInteropWebView/MainPage.xaml.cs index 57d4e65..719e434 100644 --- a/MauiCSharpInteropWebView/MainPage.xaml.cs +++ b/MauiCSharpInteropWebView/MainPage.xaml.cs @@ -1,4 +1,5 @@ -using System.Globalization; +using HybridWebView; +using System.Globalization; using System.IO.Compression; using System.Text; @@ -27,6 +28,16 @@ public MainPage() myHybridWebView.JSInvokeTarget = new MyJSInvokeTarget(this); myHybridWebView.ProxyRequestReceived += MyHybridWebView_OnProxyRequestReceived; + + myHybridWebView.HybridWebViewInitialized += MyHybridWebView_WebViewInitialized; + } + + private void MyHybridWebView_WebViewInitialized(object sender, HybridWebViewInitializedEventArgs e) + { +#if WINDOWS + // Disable the user manually zooming + e.WebView.CoreWebView2.Settings.IsZoomControlEnabled = false; +#endif } public string CurrentPageName => $"Current hybrid page: {_currentPage}";