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

WinForms/WPF/MAUI-Windows API review changes #5998

Merged
merged 5 commits into from
Apr 12, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ Microsoft.AspNetCore.Components.WebView.Maui.RootComponent.Selector.set -> void
Microsoft.AspNetCore.Components.WebView.Maui.RootComponentsCollection
Microsoft.AspNetCore.Components.WebView.Maui.RootComponentsCollection.JSComponents.get -> Microsoft.AspNetCore.Components.Web.JSComponentConfigurationStore!
Microsoft.AspNetCore.Components.WebView.Maui.RootComponentsCollection.RootComponentsCollection(Microsoft.AspNetCore.Components.Web.JSComponentConfigurationStore! jsComponents) -> void
Microsoft.AspNetCore.Components.WebView.Maui.WinUIWebViewManager
Microsoft.AspNetCore.Components.WebView.Maui.WinUIWebViewManager.WinUIWebViewManager(Microsoft.UI.Xaml.Controls.WebView2! webview, System.IServiceProvider! services, Microsoft.AspNetCore.Components.Dispatcher! dispatcher, Microsoft.Extensions.FileProviders.IFileProvider! fileProvider, Microsoft.AspNetCore.Components.Web.JSComponentConfigurationStore! jsComponents, string! hostPageRelativePath, string! contentRootDir, Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler! webViewHandler) -> void
Microsoft.AspNetCore.Components.WebView.UrlLoadingEventArgs
Microsoft.AspNetCore.Components.WebView.UrlLoadingEventArgs.Url.get -> System.Uri!
Microsoft.AspNetCore.Components.WebView.UrlLoadingEventArgs.UrlLoadingStrategy.get -> Microsoft.AspNetCore.Components.WebView.UrlLoadingStrategy
Expand All @@ -43,12 +41,9 @@ Microsoft.AspNetCore.Components.WebView.UrlLoadingStrategy
Microsoft.AspNetCore.Components.WebView.UrlLoadingStrategy.CancelLoad = 2 -> Microsoft.AspNetCore.Components.WebView.UrlLoadingStrategy
Microsoft.AspNetCore.Components.WebView.UrlLoadingStrategy.OpenExternally = 0 -> Microsoft.AspNetCore.Components.WebView.UrlLoadingStrategy
Microsoft.AspNetCore.Components.WebView.UrlLoadingStrategy.OpenInWebView = 1 -> Microsoft.AspNetCore.Components.WebView.UrlLoadingStrategy
Microsoft.AspNetCore.Components.WebView.WebView2.WebView2WebViewManager
Microsoft.Extensions.DependencyInjection.BlazorWebViewServiceCollectionExtensions
override Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler.CreatePlatformView() -> Microsoft.UI.Xaml.Controls.WebView2!
override Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler.DisconnectHandler(Microsoft.UI.Xaml.Controls.WebView2! platformView) -> void
override Microsoft.AspNetCore.Components.WebView.Maui.WinUIWebViewManager.HandleWebResourceRequest(Microsoft.Web.WebView2.Core.CoreWebView2WebResourceRequestedEventArgs! eventArgs) -> System.Threading.Tasks.Task!
override Microsoft.AspNetCore.Components.WebView.Maui.WinUIWebViewManager.QueueBlazorStart() -> void
static Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler.MapHostPage(Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler! handler, Microsoft.AspNetCore.Components.WebView.Maui.IBlazorWebView! webView) -> void
static Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler.MapNotifyUrlLoading(Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler! handler, Microsoft.AspNetCore.Components.WebView.Maui.IBlazorWebView! webView) -> void
static Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler.MapRootComponents(Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler! handler, Microsoft.AspNetCore.Components.WebView.Maui.IBlazorWebView! webView) -> void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ Microsoft.AspNetCore.Components.WebView.Maui.RootComponent.Selector.set -> void
Microsoft.AspNetCore.Components.WebView.Maui.RootComponentsCollection
Microsoft.AspNetCore.Components.WebView.Maui.RootComponentsCollection.JSComponents.get -> Microsoft.AspNetCore.Components.Web.JSComponentConfigurationStore!
Microsoft.AspNetCore.Components.WebView.Maui.RootComponentsCollection.RootComponentsCollection(Microsoft.AspNetCore.Components.Web.JSComponentConfigurationStore! jsComponents) -> void
Microsoft.AspNetCore.Components.WebView.Maui.WinUIWebViewManager
Microsoft.AspNetCore.Components.WebView.Maui.WinUIWebViewManager.WinUIWebViewManager(Microsoft.UI.Xaml.Controls.WebView2! webview, System.IServiceProvider! services, Microsoft.AspNetCore.Components.Dispatcher! dispatcher, Microsoft.Extensions.FileProviders.IFileProvider! fileProvider, Microsoft.AspNetCore.Components.Web.JSComponentConfigurationStore! jsComponents, string! hostPageRelativePath, string! contentRootDir, Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler! webViewHandler) -> void
Microsoft.AspNetCore.Components.WebView.UrlLoadingEventArgs
Microsoft.AspNetCore.Components.WebView.UrlLoadingEventArgs.Url.get -> System.Uri!
Microsoft.AspNetCore.Components.WebView.UrlLoadingEventArgs.UrlLoadingStrategy.get -> Microsoft.AspNetCore.Components.WebView.UrlLoadingStrategy
Expand All @@ -43,12 +41,9 @@ Microsoft.AspNetCore.Components.WebView.UrlLoadingStrategy
Microsoft.AspNetCore.Components.WebView.UrlLoadingStrategy.CancelLoad = 2 -> Microsoft.AspNetCore.Components.WebView.UrlLoadingStrategy
Microsoft.AspNetCore.Components.WebView.UrlLoadingStrategy.OpenExternally = 0 -> Microsoft.AspNetCore.Components.WebView.UrlLoadingStrategy
Microsoft.AspNetCore.Components.WebView.UrlLoadingStrategy.OpenInWebView = 1 -> Microsoft.AspNetCore.Components.WebView.UrlLoadingStrategy
Microsoft.AspNetCore.Components.WebView.WebView2.WebView2WebViewManager
Microsoft.Extensions.DependencyInjection.BlazorWebViewServiceCollectionExtensions
override Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler.CreatePlatformView() -> Microsoft.UI.Xaml.Controls.WebView2!
override Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler.DisconnectHandler(Microsoft.UI.Xaml.Controls.WebView2! platformView) -> void
override Microsoft.AspNetCore.Components.WebView.Maui.WinUIWebViewManager.HandleWebResourceRequest(Microsoft.Web.WebView2.Core.CoreWebView2WebResourceRequestedEventArgs! eventArgs) -> System.Threading.Tasks.Task!
override Microsoft.AspNetCore.Components.WebView.Maui.WinUIWebViewManager.QueueBlazorStart() -> void
static Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler.MapHostPage(Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler! handler, Microsoft.AspNetCore.Components.WebView.Maui.IBlazorWebView! webView) -> void
static Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler.MapNotifyUrlLoading(Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler! handler, Microsoft.AspNetCore.Components.WebView.Maui.IBlazorWebView! webView) -> void
static Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler.MapRootComponents(Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler! handler, Microsoft.AspNetCore.Components.WebView.Maui.IBlazorWebView! webView) -> void
Expand Down
5 changes: 1 addition & 4 deletions src/BlazorWebView/src/Maui/Windows/WinUIWebViewManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Microsoft.Extensions.FileProviders;
using Microsoft.Web.WebView2.Core;
using Windows.ApplicationModel;
using Windows.Storage;
using Windows.Storage.Streams;
using WebView2Control = Microsoft.UI.Xaml.Controls.WebView2;

Expand All @@ -17,7 +16,7 @@ namespace Microsoft.AspNetCore.Components.WebView.Maui
/// An implementation of <see cref="WebViewManager"/> that uses the Edge WebView2 browser control
/// to render web content in WinUI applications.
/// </summary>
public class WinUIWebViewManager : WebView2WebViewManager
internal class WinUIWebViewManager : WebView2WebViewManager
{
private readonly WebView2Control _webview;
private readonly string _hostPageRelativePath;
Expand All @@ -36,7 +35,6 @@ static WinUIWebViewManager()
}
}

#pragma warning disable RS0022
/// <summary>
/// Initializes a new instance of <see cref="WinUIWebViewManager"/>
/// </summary>
Expand All @@ -63,7 +61,6 @@ public WinUIWebViewManager(
_hostPageRelativePath = hostPageRelativePath;
_contentRootDir = contentRootDir;
}
#pragma warning restore RS0022

/// <inheritdoc />
protected override async Task HandleWebResourceRequest(CoreWebView2WebResourceRequestedEventArgs eventArgs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public static class BlazorWebViewServiceCollectionExtensions
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <returns>The <see cref="IServiceCollection"/>.</returns>
#if WEBVIEW2_WINFORMS
public static IServiceCollection AddWindowsFormsBlazorWebView(this IServiceCollection services)
public static IWindowsFormsBlazorWebViewBuilder AddWindowsFormsBlazorWebView(this IServiceCollection services)
#elif WEBVIEW2_WPF
public static IServiceCollection AddWpfBlazorWebView(this IServiceCollection services)
public static IWpfBlazorWebViewBuilder AddWpfBlazorWebView(this IServiceCollection services)
#elif WEBVIEW2_MAUI
public static IServiceCollection AddMauiBlazorWebView(this IServiceCollection services)
#else
Expand All @@ -39,12 +39,14 @@ public static IServiceCollection AddMauiBlazorWebView(this IServiceCollection se
#if WEBVIEW2_MAUI
services.TryAddSingleton<MauiBlazorMarkerService>();
services.ConfigureMauiHandlers(static handlers => handlers.AddHandler<IBlazorWebView, BlazorWebViewHandler>());
return services;
#elif WEBVIEW2_WINFORMS
services.TryAddSingleton<WindowsFormsBlazorMarkerService>();
return new WindowsFormsBlazorWebViewBuilder(services);
#elif WEBVIEW2_WPF
services.TryAddSingleton<WpfBlazorMarkerService>();
return new WpfBlazorWebViewBuilder(services);
#endif
return services;
}

/// <summary>
Expand Down
12 changes: 6 additions & 6 deletions src/BlazorWebView/src/SharedSource/WebView2WebViewManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ namespace Microsoft.AspNetCore.Components.WebView.WebView2
/// An implementation of <see cref="WebViewManager"/> that uses the Edge WebView2 browser control
/// to render web content.
/// </summary>
public class WebView2WebViewManager : WebViewManager
internal class WebView2WebViewManager : WebViewManager
{
// 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
Expand All @@ -65,7 +65,7 @@ public class WebView2WebViewManager : WebViewManager
private readonly Task _webviewReadyTask;

#if WEBVIEW2_WINFORMS || WEBVIEW2_WPF
private protected CoreWebView2Environment _coreWebView2Environment;
private protected CoreWebView2Environment? _coreWebView2Environment;
private readonly Action<UrlLoadingEventArgs> _urlLoading;
private readonly Action<BlazorWebViewInitializingEventArgs> _blazorWebViewInitializing;
private readonly Action<BlazorWebViewInitializedEventArgs> _blazorWebViewInitialized;
Expand Down Expand Up @@ -273,7 +273,7 @@ protected virtual Task HandleWebResourceRequest(CoreWebView2WebResourceRequested
{
var headerString = GetHeaderString(headers);

eventArgs.Response = _coreWebView2Environment.CreateWebResourceResponse(content, statusCode, statusMessage, headerString);
eventArgs.Response = _coreWebView2Environment!.CreateWebResourceResponse(content, statusCode, statusMessage, headerString);
}
#elif WEBVIEW2_MAUI
// No-op here because all the work is done in the derived WinUIWebViewManager
Expand All @@ -288,7 +288,7 @@ protected virtual void QueueBlazorStart()
{
}

private void CoreWebView2_NavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs args)
private void CoreWebView2_NavigationStarting(object? sender, CoreWebView2NavigationStartingEventArgs args)
{
if (Uri.TryCreate(args.Uri, UriKind.RelativeOrAbsolute, out var uri))
{
Expand All @@ -309,7 +309,7 @@ private void CoreWebView2_NavigationStarting(object sender, CoreWebView2Navigati
}
}

private void CoreWebView2_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs args)
private void CoreWebView2_NewWindowRequested(object? sender, CoreWebView2NewWindowRequestedEventArgs args)
{
// Intercept _blank target <a> tags to always open in device browser.
// The ExternalLinkCallback is not invoked.
Expand Down Expand Up @@ -349,7 +349,7 @@ private void ApplyDefaultWebViewSettings(BlazorWebViewDeveloperTools devTools)
}

#if WEBVIEW2_WINFORMS || WEBVIEW2_WPF
private static string GetWebView2UserDataFolder()
private static string? GetWebView2UserDataFolder()
{
if (Assembly.GetEntryAssembly() is { } mainAssembly)
{
Expand Down
36 changes: 16 additions & 20 deletions src/BlazorWebView/src/WindowsForms/BlazorWebView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Reflection;
Expand All @@ -21,9 +22,9 @@ namespace Microsoft.AspNetCore.Components.WebView.WindowsForms
public class BlazorWebView : ContainerControl
{
private readonly WebView2Control _webview;
private WebView2WebViewManager _webviewManager;
private string _hostPage;
private IServiceProvider _services;
private WebView2WebViewManager? _webviewManager;
private string? _hostPage;
private IServiceProvider? _services;

/// <summary>
/// Creates a new instance of <see cref="BlazorWebView"/>.
Expand Down Expand Up @@ -52,12 +53,6 @@ public BlazorWebView()
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public WebView2Control WebView => _webview;

/// <summary>
/// Returns the current <see cref="WebView2WebViewManager"/> used by this control. This property is <c>null</c>
/// until after the XYZ event is raised.
/// </summary>
public WebView2WebViewManager WebViewManager => _webviewManager;

private WindowsFormsDispatcher ComponentsDispatcher { get; }

/// <inheritdoc />
Expand All @@ -74,7 +69,7 @@ protected override void OnCreateControl()
/// </summary>
[Category("Behavior")]
[Description(@"Path to the host page within the application's static files. Example: wwwroot\index.html.")]
public string HostPage
public string? HostPage
{
get => _hostPage;
set
Expand Down Expand Up @@ -102,9 +97,10 @@ public string HostPage
/// </summary>
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[DisallowNull]
public IServiceProvider Services
{
get => _services;
get => _services!;
set
{
_services = value;
Expand All @@ -118,21 +114,21 @@ public IServiceProvider Services
/// </summary>
[Category("Action")]
[Description("Allows customizing how links are opened. By default, opens internal links in the webview and external links in an external app.")]
public EventHandler<UrlLoadingEventArgs> UrlLoading;
public EventHandler<UrlLoadingEventArgs>? UrlLoading;

/// <summary>
/// Allows customizing the web view before it is created.
/// </summary>
[Category("Action")]
[Description("Allows customizing the web view before it is created.")]
public EventHandler<BlazorWebViewInitializingEventArgs> BlazorWebViewInitializing;
public EventHandler<BlazorWebViewInitializingEventArgs>? BlazorWebViewInitializing;

/// <summary>
/// Allows customizing the web view after it is created.
/// </summary>
[Category("Action")]
[Description("Allows customizing the web view after it is created.")]
public EventHandler<BlazorWebViewInitializedEventArgs> BlazorWebViewInitialized;
public EventHandler<BlazorWebViewInitializedEventArgs>? BlazorWebViewInitialized;

private void OnHostPagePropertyChanged() => StartWebViewCoreIfPossible();

Expand Down Expand Up @@ -165,14 +161,14 @@ private void StartWebViewCoreIfPossible()
var entryAssemblyLocation = Assembly.GetEntryAssembly()?.Location;
if (!string.IsNullOrEmpty(entryAssemblyLocation))
{
appRootDir = Path.GetDirectoryName(entryAssemblyLocation);
appRootDir = Path.GetDirectoryName(entryAssemblyLocation)!;
}
else
{
appRootDir = Environment.CurrentDirectory;
}
var hostPageFullPath = Path.GetFullPath(Path.Combine(appRootDir, HostPage));
var contentRootDirFullPath = Path.GetDirectoryName(hostPageFullPath);
var hostPageFullPath = Path.GetFullPath(Path.Combine(appRootDir, HostPage!)); // HostPage is nonnull because RequiredStartupPropertiesSet is checked above
var contentRootDirFullPath = Path.GetDirectoryName(hostPageFullPath)!;
var hostPageRelativePath = Path.GetRelativePath(contentRootDirFullPath, hostPageFullPath);

var fileProvider = CreateFileProvider(contentRootDirFullPath);
Expand All @@ -196,16 +192,16 @@ private void StartWebViewCoreIfPossible()
_webviewManager.Navigate("/");
}

private void HandleRootComponentsCollectionChanged(object sender, NotifyCollectionChangedEventArgs eventArgs)
private void HandleRootComponentsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs eventArgs)
{
// If we haven't initialized yet, this is a no-op
if (_webviewManager != null)
{
// Dispatch because this is going to be async, and we want to catch any errors
_ = ComponentsDispatcher.InvokeAsync(async () =>
{
var newItems = eventArgs.NewItems.Cast<RootComponent>();
var oldItems = eventArgs.OldItems.Cast<RootComponent>();
var newItems = (eventArgs.NewItems ?? Array.Empty<object>()).Cast<RootComponent>();
var oldItems = (eventArgs.OldItems ?? Array.Empty<object>()).Cast<RootComponent>();

foreach (var item in newItems.Except(oldItems))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.AspNetCore.Components.WebView.WindowsForms
{
/// <summary>
/// A builder for WindowsForms Blazor WebViews.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// A builder for WindowsForms Blazor WebViews.
/// A builder for Windows Forms Blazor WebViews.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😬

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. However your approval triggered automerge! Followed up at #6001

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood, definitely my fault. I noticed it right as I was clicking Approve 😁

/// </summary>
public interface IWindowsFormsBlazorWebViewBuilder
{
/// <summary>
/// Gets the builder service collection.
/// </summary>
IServiceCollection Services { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<Description>Build Windows Forms applications with Blazor and WebView2.</Description>
<DefineConstants>$(DefineConstants);WEBVIEW2_WINFORMS</DefineConstants>
<UseWindowsForms>true</UseWindowsForms>
<Nullable>disable</Nullable>
<Nullable>enable</Nullable>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

Expand Down
1 change: 1 addition & 0 deletions src/BlazorWebView/src/WindowsForms/PublicAPI.Shipped.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#nullable enable
Loading