Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose platform webview for initialization to enable customization #53

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions HybridWebView/HybridWebView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,22 @@ public partial class HybridWebView : WebView
/// </summary>
public bool EnableWebDevTools { get; set; }

/// <summary>
/// Raised when a raw message is received from the web view. Raw messages are strings that have no additional processing.
/// </summary>
public event EventHandler<HybridWebViewRawMessageReceivedEventArgs>? RawMessageReceived;

/// <summary>
/// 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.
/// </summary>

public event Func<HybridWebViewProxyEventArgs, Task>? ProxyRequestReceived;

/// <summary>
/// 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.
/// </summary>
public event EventHandler<HybridWebViewInitializedEventArgs>? HybridWebViewInitialized;


public void Navigate(string url)
{
NavigateCore(url);
Expand All @@ -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);
}

Expand Down
43 changes: 43 additions & 0 deletions HybridWebView/HybridWebViewInitializedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Allows configuring the underlying web view after it has been initialized.
/// </summary>
public class HybridWebViewInitializedEventArgs : EventArgs
{
#nullable disable
#if WINDOWS
/// <summary>
/// Gets the <see cref="WebView2Control"/> instance that was initialized.
/// </summary>
public WebView2Control WebView { get; internal set; }
#elif ANDROID
/// <summary>
/// Gets the <see cref="AWebView"/> instance that was initialized.
/// </summary>
public AWebView WebView { get; internal set; }
#elif MACCATALYST || IOS
/// <summary>
/// Gets the <see cref="WKWebView"/> instance that was initialized.
/// the default values to allow further configuring additional options.
/// </summary>
public WKWebView WebView { get; internal set; }
#elif TIZEN
/// <summary>
/// Gets the <see cref="TWebView"/> instance that was initialized.
/// </summary>
public TWebView WebView { get; internal set; }
#endif
}
}
7 changes: 2 additions & 5 deletions HybridWebView/Platforms/Android/HybridWebView.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/// <summary>
/// Gets the application's base URI. Defaults to <c>https://0.0.0.0/</c>
/// </summary>
internal static readonly string AppOrigin = $"https://{AppHostAddress}/";
private static readonly string AppOrigin = $"https://{AppHostAddress}/";

internal static readonly Uri AppOriginUri = new(AppOrigin);

Expand Down
13 changes: 12 additions & 1 deletion MauiCSharpInteropWebView/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Globalization;
using HybridWebView;
using System.Globalization;
using System.IO.Compression;
using System.Text;

Expand Down Expand Up @@ -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}";
Expand Down
Loading