-
Notifications
You must be signed in to change notification settings - Fork 750
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: WebView and WebView2 on WebAssembly
- Loading branch information
1 parent
f43920b
commit be3a0fb
Showing
10 changed files
with
129 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/Uno.UI/UI/Xaml/Controls/WebView/Core/CoreWebView2.unsupported.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/Uno.UI/UI/Xaml/Controls/WebView/Core/CoreWebView2.wasm.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Uno.UI/UI/Xaml/Controls/WebView/Native/Wasm/INativeWebView.wasm.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
59 changes: 59 additions & 0 deletions
59
src/Uno.UI/UI/Xaml/Controls/WebView/Native/Wasm/NativeWebView.wasm.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters