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

Move platform info from GetStart tut to general platform page #3340

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 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
26 changes: 13 additions & 13 deletions microsoft-edge/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1283,8 +1283,8 @@
items:
- name: Win32
items:
# - name: WebView2 in Win32 apps
# href: ./webview2/platforms/win32.md
- name: WebView2 in Win32 apps
href: ./webview2/platforms/win32.md

- name: Win32 sample app
mikehoffms marked this conversation as resolved.
Show resolved Hide resolved
href: ./webview2/samples/webview2apissample.md
Expand All @@ -1304,8 +1304,8 @@

- name: WinUI 2 (UWP)
items:
# - name: WebView2 in WinUI 2 (UWP) apps
# href: ./webview2/platforms/winui2-uwp.md
- name: WebView2 in WinUI 2 (UWP) apps
href: ./webview2/platforms/winui2-uwp.md

- name: WinUI 2 (UWP) sample app
href: ./webview2/samples/webview2_sample_uwp.md
Expand All @@ -1317,8 +1317,8 @@

- name: WinUI 3 (Windows App SDK)
items:
# - name: WebView2 in WinUI 3 (Windows App SDK) apps
# href: ./webview2/platforms/winui3-windows-app-sdk.md
- name: WebView2 in WinUI 3 (Windows App SDK) apps
href: ./webview2/platforms/winui3-windows-app-sdk.md

- name: WinUI 3 (Windows App SDK) sample app
href: ./webview2/samples/webview2-winui3-sample.md
Expand All @@ -1328,10 +1328,10 @@
href: ./webview2/get-started/winui.md
displayName: WinUI3_GettingStarted # repo dir name

- name: WebView2 in WPF apps
- name: WPF
items:
# - name: WebView2 apps on Win32
# href: ./webview2/platforms/wpf.md
- name: WebView2 in WPF apps
href: ./webview2/platforms/wpf.md

- name: WPF sample app
href: ./webview2/samples/webview2wpfbrowser.md
Expand All @@ -1347,8 +1347,8 @@

- name: WinForms
items:
# - name: WebView2 in WinForms apps
# href: ./webview2/platforms/winforms.md
- name: WebView2 in WinForms apps
href: ./webview2/platforms/winforms.md

- name: WinForms sample app
href: ./webview2/samples/webview2windowsformsbrowser.md
Expand All @@ -1360,8 +1360,8 @@

- name: HoloLens 2 Unity
items:
# - name: WebView2 in HoloLens 2 Unity apps
# href: ./webview2/platforms/hololens-2-unity.md
- name: WebView2 in HoloLens 2 Unity apps
href: ./webview2/platforms/hololens-2-unity.md

- name: Get started with WebView2 in HoloLens 2 Unity apps (Preview)
href: ./webview2/get-started/hololens2.md
Expand Down
3 changes: 2 additions & 1 deletion microsoft-edge/webview2/concepts/overview-features-apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ms.author: msedgedevrel
ms.topic: conceptual
ms.service: microsoft-edge
ms.subservice: webview
ms.date: 11/18/2024
ms.date: 01/20/2025
mikehoffms marked this conversation as resolved.
Show resolved Hide resolved
---
# Overview of WebView2 APIs

Expand Down Expand Up @@ -2986,6 +2986,7 @@ Use the following APIs to forward `IDropTarget` events from the system to the We

---


<!-- ------------------------------ -->
#### Accessibility

Expand Down
189 changes: 0 additions & 189 deletions microsoft-edge/webview2/get-started/hololens2.md
Original file line number Diff line number Diff line change
Expand Up @@ -409,195 +409,6 @@ public class WebViewExample : MonoBehaviour, IWithPostMessage
```


<!-- ====================================================================== -->
## Additional considerations for WebView2 on HoloLens 2


<!-- ------------------------------ -->
#### Limitations and known issues

When developing a HoloLens 2 Unity app with WebView2, be aware of some limitations and known issues:

* **Pop-ups**: Pop-ups don't work well within WebView2 inside Unity apps on HoloLens 2, but they work fine in 2D XAML apps on the device. Avoid pop-ups and use alternative techniques or UI designs, such as custom pop-up-like elements within the WebView using HTML, CSS, and JavaScript.

* **New windows**: WebView2 instances on HoloLens 2 navigate within the same window by default, unlike on Desktop. Follow this default behavior for a better user experience. Additionally, the DevTools window cannot be launched.

* **Enterprise authentication**: Automatic Single Sign-On (SSO) leveraging OS-level tokens is currently not supported in WebView2 on HoloLens 2. Users can still sign in by providing credentials, except for cases requiring device-level authentication. Cookie storage works as expected.

* **User interactions**: Unlike native HoloLens 2 slates, WebView2 is best interacted with by using far-interaction hand rays. Touch-to-swipe and scroll interactions might not be supported.

* **Performance**: Complex websites with heavy use of JavaScript or advanced rendering may impact system performance or the host application's framerate. For general performance-related limitations and recommendations, see [Understanding performance for mixed reality](/windows/mixed-reality/develop/advanced-concepts/understanding-performance-for-mixed-reality) in the mixed reality documentation. Also see [Performance optimization](#performance-optimization), below.


<!-- ------------------------------ -->
#### Performance optimization

Optimizing the performance of WebView2 in your HoloLens 2 Unity app is crucial for a smooth user experience. Here are some recommendations:

* **Limit the number of WebView2 instances**: We suggest using only one instance of WebView2 within a Unity app. Reuse the same instance or tear down and create a new one as needed. Keep in mind that removing the WebView prefab from the scene might not destroy the underlying WebView2 instance. You must call the `Destroy()` method on the game object to destroy it properly.

* **Apply general Unity optimization techniques**: To optimize WebView2 performance, use the standard Unity optimization approaches, such as occlusion culling or limiting the update rate. For more information, see [Performance recommendations for Unity](/windows/mixed-reality/develop/unity/performance-recommendations-for-unity?tabs=openxr) in the mixed reality documentation.

* **Profile and monitor WebView2 performance**: There are several ways to profile the performance of a HoloLens 2 Unity application:

* **Unity Profiler**: A built-in tool in Unity that allows you to measure and optimize the performance of your application on various platforms, including HoloLens 2.

* **Visual Profiler**: A feature of the Mixed Reality Toolkit that provides an in-application view of your application's performance.

* **PIX**: A performance tuning and debugging tool for Windows that can also be used to profile Unity applications on HoloLens 2.


<!-- ------------------------------ -->
#### Navigation

In [Step 7 - Extending WebView2 functionality](#step-7---extending-webview2-functionality), we touched on some navigation methods. In this section, we'll expand on what we learned.

See also:
* [WebView2 API Reference](../webview2-api-reference.md)
* [API Reference for Mixed Reality WebView plugin](/windows/mixed-reality/develop/advanced-concepts/webview2-unity-plugin) - for HoloLens 2 in the WebView2 Unity plugin.
<!-- dest. TOC title:
WebView2 Unity Plugin API -->


<!-- ---------- -->
###### IWebView interface

<!-- [IWebView::Load method]()-->

The `IWebView` interface exposes a few methods, events, and properties related to page navigation. The main functionality exposed here is the ability to navigate to a given URL, by using `Load(Uri url)`:

```C#
public interface IWebView
{
// Non-navigation methods are removed for clarity.

event WebView_OnNavigated Navigated;

Uri Page { get; }

void Load(Uri url);

void Reload(bool ignoreCache);
}
```


<!-- ---------- -->
###### IWithBrowserHistory interface

The `IWithBrowserHistory` interface exposes a few methods and events related to page navigation. This mainly allows developers to navigate forward and backward, as you would expect with a typical web-browsing experience:

```C#
public interface IWithBrowserHistory : IWebView
{
// Non-navigation methods are removed for clarity.

event WebView_OnCanGoForwardUpdated CanGoForwardUpdated;

event WebView_OnCanGoBackUpdated CanGoBackUpdated;

void GoBack();

void GoForward();
}
```


<!-- ---------- -->
###### SetVirtualHostNameToFolderMapping and SetVirtualHostMapping

The [CoreWebView2.SetVirtualHostNameToFolderMapping Method](/dotnet/api/microsoft.web.webview2.core.corewebview2.setvirtualhostnametofoldermapping) enables mapping between a virtual host name and a folder path, making it accessible to websites using that host name. This method maps a local domain name to a local folder, so that the WebView2 control loads content from the specified local folder when attempting to access a resource for that domain.

The WebView plugin for Unity exposes this functionality through the `IWithVirtualHost` interface, which has a single method, `SetVirtualHostMapping(string hostName, string folderPath)`:

```C#
public interface IWithVirtualHost : IWebView
{
void SetVirtualHostMapping(string hostName, string folderPath);
}
```

To use the `SetVirtualHostMapping` method, set `hostName` to any valid URL conforming string, such as `webview2.sample`. `folderPath` can be an absolute path or a path relative to the application's working directory, such as `Assets\Html`.

Assuming we have an HTML file called `demo.html` under `Assets\Html`, the following code snippet demonstrates loading `demo.html` by using the WebView plugin for Unity:

```C#
using Microsoft.MixedReality.WebView;

public class WebViewExample : MonoBehaviour
{
private void Start()
{
var webViewComponent = gameObject.GetComponent<WebView>();

webViewComponent.GetWebViewWhenReady((IWebView webView) =>
{
((IWithVirtualHost)webView).SetVirtualHostMapping("webview2.sample", "Assets\\Html");

// Navigate to our local content.
webViewComponent.Load(new Uri("http://webview2.sample/demo.html"));
});
}
}
```


<!-- ------------------------------ -->
#### Input

There are various ways to handle input in Unity for mixed reality applications.

* [Input overview — MRTK2](/windows/mixed-reality/mrtk-unity/mrtk2/features/input/overview) - recommended for Mixed Reality Toolkit 2.8 applications.
* [Input — MRTK3](/windows/mixed-reality/mrtk-unity/mrtk3-input/packages/input/overview) - recommended for Mixed Reality Toolkit 3 applications.
* [Unity Input System](https://docs.unity3d.com/Packages/[email protected]/manual/index.html)

Regardless of the input system used within your Unity application, interop code between the various application input events and the WebView plugin for Unity is required. This means translating those events (such as Pointer events) into a `WebViewMouseEventData` object and then forwarding those events to the plugin via the `IWithMouseEvent` interface:

```C#
public interface IWithMouseEvents : IWithInputEvents
{
void MouseEvent(WebViewMouseEventData mouseEvent);
}
```

WebView2 is unaware of Unity's input system and likely has a different coordinate system than your Unity scene. As a result, when there is a pointer-down event, its coordinates must be translated into the coordinate system of the WebView2 control. Additionally, the pointer-down event needs to be converted into an appropriate `WebViewMouseEventData` event type.

Simple example:

```C#
using Microsoft.MixedReality.WebView;

public class WebViewExample : MonoBehaviour, IPointerDownHandler
{
// WebView setup steps skipped for brevity

public void OnPointerDown(PointerEventData eventData)
{
IWithMouseEvents mouseEventsWebView = webView as IWithMouseEvents;

// Call hypothetical function which converts the event's x, y into the WebView2's coordinate space.
var hitCoord = ConvertToWebViewSpace(eventData.position.x, eventData.position.y);

WebViewMouseEventData mouseEvent = new WebViewMouseEventData
{
X = hitCoord.x,
Y = hitCoord.y,
Type = WebViewMouseEventData.EventType.MouseDown,
Button = WebViewMouseEventData.MouseButton.ButtonLeft,
TertiaryAxisDeviceType = WebViewMouseEventData.TertiaryAxisDevice.PointingDevice
};

// Propagate the event to the WebView plugin.
mouseEventsWebView.MouseEvent(mouseEvent);
}
}
```

In the above example, pointer-down events are converted into `WebViewMouseEventData` objects and forwarded to the WebView plugin for Unity. It is essentially converted into a mouse-down event. In order to create mouse click events, pointer-up events would need to be handled in a similar fashion.

In the example above, `ConvertToWebViewSpace` is intentionally not implemented.


<!-- ====================================================================== -->
## See also

Expand Down
82 changes: 1 addition & 81 deletions microsoft-edge/webview2/get-started/winui.md
Original file line number Diff line number Diff line change
Expand Up @@ -512,89 +512,9 @@ To display an alert when the user tries to navigate to a non-HTTPS site:
Congratulations, you've built a WebView2 WinUI 3 (Windows App SDK) app!


<!-- ====================================================================== -->
## WinAppSDK supports custom WebView2 environments

WinAppSDK supports custom WebView2 environments, starting with WinAppSDK 1.5 ([1.5.0-experimental2](/windows/apps/windows-app-sdk/experimental-channel#version-15-experimental-150-experimental2)). For more information, see [WinUI3 WebView2 with a custom CoreWebView2Environment](https://github.com/microsoft/microsoft-ui-xaml/issues/6150).

To instantiate a custom WebView2 environment, you could initialize WebView2 with one of the overrides of `WebView2.EnsureCoreWebView2Async` (listed below), and pass in your custom `CoreWebView2Environment` (and, optionally, custom `CoreWebView2ControllerOptions`):

```csharp
public IAsyncAction EnsureCoreWebView2Async (CoreWebView2Environment environment)
public IAsyncAction EnsureCoreWebView2Async (CoreWebView2Environment environment, CoreWebView2ControllerOptions controllerOptions)
```

Also see the example code in [Disabling SmartScreen navigation](#disabling-smartscreen-navigation), below.

API Reference:
* [WebView2.EnsureCoreWebView2Async](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.webview2.ensurecorewebview2async)
* [CoreWebView2ControllerOptions](/microsoft-edge/webview2/reference/winrt/microsoft_web_webview2_core/corewebview2controlleroptions)
* [CoreWebView2Environment](/microsoft-edge/webview2/reference/winrt/microsoft_web_webview2_core/corewebview2environment)
* [CoreWebView2EnvironmentOptions](/microsoft-edge/webview2/reference/winrt/microsoft_web_webview2_core/corewebview2environmentoptions)


<!-- ====================================================================== -->
## WinUI 3 WebView2 special considerations


<!-- ------------------------------ -->
#### Disabling SmartScreen navigation

WebView2 sends URLs that are navigated to in your application to the [SmartScreen](/windows/security/threat-protection/microsoft-defender-smartscreen/microsoft-defender-smartscreen-overview) service, to ensure that your customers stay secure. If you want to disable this navigation, use a custom `CoreWebView2Environment`, as follows:

```csharp
CoreWebView2EnvironmentOptions environmentOptions = new CoreWebView2EnvironmentOptions();
environmentOptions.AdditionalBrowserArguments = "--disable-features=msSmartScreenProtection";

string browserFolder = null; // Use null to get default browser folder
string userDataFolder = null; // Use null to get default user data folder
CoreWebView2Environment environment = await CoreWebView2Environment.CreateWithOptionsAsync(
browserFolder, userDataFolder, environmentOptions);

// ...

this.WebView2.EnsureCoreWebView2Async(environment);
```


<!-- ---------- -->
###### Disabling SmartScreen by using an environment variable

We no longer recommend using an environment variable. Use the above API code-based approach instead.

* `Environment.SetEnvironmentVariable("WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS", "--disable-features=msSmartScreenProtection");`

This environment variable must be set prior to `CoreWebView2` creation, which occurs when the [WebView2.Source property](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.webview2.source#microsoft-ui-xaml-controls-webview2-source) is initially set or the [WebView2.EnsureCoreWebView2Async method](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.webview2.ensurecorewebview2async#microsoft-ui-xaml-controls-webview2-ensurecorewebview2async) is initially called.


<!-- ------------------------------ -->
#### Setting DefaultBackgroundColor

In WebView2 for WinUI 3, the `DefaultBackgroundColor` setting exists on the WebView2 XAML object. For example:

```csharp
public MainWindow()
{
this.InitializeComponent();
MyWebView.DefaultBackgroundColor = Colors.LightBlue;
}
```


<!-- ------------------------------ -->
#### Transparency

WinUI 3 doesn't support transparent backgrounds. See [Transparent background support for WebView2? · Issue #2992](https://github.com/microsoft/microsoft-ui-xaml/issues/2992).


<!-- ------------------------------ -->
#### WinAppSDK support for custom WebView2 environments

See [WinAppSDK supports custom WebView2 environments](#winappsdk-supports-custom-webview2-environments), above.


<!-- ====================================================================== -->
## See also
<!-- todo: all links in article -->

* [WebView2 API Reference](../webview2-api-reference.md)
* [Introduction to Microsoft Edge WebView2](../index.md) - overview of features.
Expand Down
Loading