Skip to content

Commit

Permalink
feat: WebView and WebView2 on WebAssembly
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Sep 10, 2024
1 parent f43920b commit be3a0fb
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

namespace Uno.UI.RuntimeTests.Tests.Microsoft_UI_Xaml_Controls;

#if !HAS_UNO || __ANDROID__ || __IOS__
#if !HAS_UNO || __ANDROID__ || __IOS__ || __WASM__
[TestClass]
[RunsOnUIThread]
public class Given_WebView2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void When_Navigate()
Assert.AreEqual("https://bing.com", uri.OriginalString);
}

#if __ANDROID__ || __IOS__ || __MACOS__
#if __ANDROID__ || __IOS__ || __MACOS__ || __WASM__
[TestMethod]
public void When_NavigateWithHttpRequestMessage()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if !__ANDROID__ && !__IOS__ && !__MACOS__ && !__MACCATALYST__ && !__SKIA__
#if !__ANDROID__ && !__IOS__ && !__MACOS__ && !__MACCATALYST__ && !__SKIA__ && !__WASM__
#nullable enable

using Uno.UI.Xaml.Controls;
Expand Down
28 changes: 28 additions & 0 deletions src/Uno.UI/UI/Xaml/Controls/WebView/Core/CoreWebView2.wasm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#nullable enable

using System.Linq;
using Uno.UI;
using Uno.UI.Extensions;
using Uno.UI.Xaml.Controls;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace Microsoft.Web.WebView2.Core;

public partial class CoreWebView2
{
internal INativeWebView? GetNativeWebViewFromTemplate()
{
var webView = ((UIElement)_owner)
.GetChildren()
.OfType<NativeWebView>()
.FirstOrDefault();

if (webView is null)
{
return null;
}

return webView;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Microsoft.Web.WebView2.Core;

namespace Uno.UI.Xaml.Controls;

/// <summary>
/// Wrapper for a version-dependent native WASM WebView
/// </summary>
internal partial interface INativeWebView
{
void SetOwner(CoreWebView2 xamlWebView);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Web.WebView2.Core;
using Uno.Foundation;
using Uno.UI.Xaml.Controls;

namespace Windows.UI.Xaml.Controls;

public class NativeWebView : FrameworkElement, INativeWebView
{
private CoreWebView2 _coreWebView;

public NativeWebView() : base("iframe")
{
this.HorizontalAlignment = HorizontalAlignment.Stretch;
this.VerticalAlignment = VerticalAlignment.Stretch;
}

public void SetOwner(CoreWebView2 coreWebView)
{
_coreWebView = coreWebView;
}

public Task<string> ExecuteScriptAsync(string script, CancellationToken token)
{
var scriptString = WebAssemblyRuntime.EscapeJs(script);
return Task.FromResult(WebAssemblyRuntime.InvokeJS($"document.getElementById('{HtmlId}').contentWindow.eval(\"{scriptString}\")"));
}

public void GoBack() { }
public void GoForward() { }
public Task<string> InvokeScriptAsync(string script, string[] arguments, CancellationToken token) => Task.FromResult<string>("");
public async void ProcessNavigation(Uri uri)
{
this.SetAttribute("src", uri.ToString());
await Task.Delay(10);
_coreWebView.RaiseNavigationCompleted(uri, true, 200, CoreWebView2WebErrorStatus.Unknown);
}

public async void ProcessNavigation(string html)
{
this.SetAttribute("srcdoc", html);
await Task.Delay(10);
_coreWebView.RaiseNavigationCompleted(null, true, 200, CoreWebView2WebErrorStatus.Unknown);
}

public void ProcessNavigation(HttpRequestMessage httpRequestMessage)
{

}

public void Reload()
{
}
public void SetScrollingEnabled(bool isScrollingEnabled) { }
public void Stop() { }
}
2 changes: 1 addition & 1 deletion src/Uno.UI/UI/Xaml/Controls/WebView/WebView1/WebView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Microsoft.UI.Xaml.Controls;

#if IS_UNIT_TESTS || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__
#if IS_UNIT_TESTS || __SKIA__ || __NETSTD_REFERENCE__
[Uno.NotImplemented("IS_UNIT_TESTS", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__")]
#endif
public partial class WebView : Control, IWebView
Expand Down
4 changes: 2 additions & 2 deletions src/Uno.UI/UI/Xaml/Controls/WebView/WebView2/WebView2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ namespace Microsoft/* UWP don't rename */.UI.Xaml.Controls;
/// <summary>
/// Represents an object that enables the hosting of web content.
/// </summary>
#if IS_UNIT_TESTS || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__
[Uno.NotImplemented("IS_UNIT_TESTS", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__")]
#if IS_UNIT_TESTS || __SKIA__ || __NETSTD_REFERENCE__
[Uno.NotImplemented("IS_UNIT_TESTS", "__SKIA__", "__NETSTD_REFERENCE__")]
#endif
public partial class WebView2 : Control, IWebView
{
Expand Down
20 changes: 20 additions & 0 deletions src/Uno.UI/UI/Xaml/Style/Generic/Generic.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7740,6 +7740,26 @@
</Setter>
</not_netstdref:Style>

<wasm:Style TargetType="WebView">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<NativeWebView />
</ControlTemplate>
</Setter.Value>
</Setter>
</wasm:Style>

<wasm:Style TargetType="mux:WebView2">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<NativeWebView />
</ControlTemplate>
</Setter.Value>
</Setter>
</wasm:Style>

<skia:Style TargetType="WebView">
<Setter Property="Template">
<Setter.Value>
Expand Down
6 changes: 5 additions & 1 deletion src/Uno.UI/WasmCSS/Uno.UI.css
Original file line number Diff line number Diff line change
Expand Up @@ -294,5 +294,9 @@ input::-ms-clear {

/* Uno has its own HR indicator: hide default dotnet indicator. */
#dotnet-hotreload-toast {
visibility: collapse
visibility: collapse;
}

.uno-nativewebview {
border: 0px;
}

0 comments on commit be3a0fb

Please sign in to comment.