Skip to content

Commit a7a8aa3

Browse files
github-actions[bot]tj-devel709PureWeenmattleibowCopilot
authored
[release/10.0.1xx-preview7] [HybridWebView] Add WebViewInitializing and WebViewInitialized Events (#31027)
* adding in initialize and initializing # Conflicts: # src/Controls/src/Core/HybridWebView/HybridWebView.cs # src/Controls/src/Core/PublicAPI/net-android/PublicAPI.Unshipped.txt # src/Controls/src/Core/PublicAPI/net-ios/PublicAPI.Unshipped.txt # src/Controls/src/Core/PublicAPI/net-maccatalyst/PublicAPI.Unshipped.txt # src/Controls/src/Core/PublicAPI/net-tizen/PublicAPI.Unshipped.txt # src/Controls/src/Core/PublicAPI/net-windows/PublicAPI.Unshipped.txt # src/Controls/src/Core/PublicAPI/net/PublicAPI.Unshipped.txt # src/Controls/src/Core/PublicAPI/netstandard/PublicAPI.Unshipped.txt # src/Core/src/Handlers/HybridWebView/HybridWebViewHandler.Windows.cs # src/Core/src/PublicAPI/net-android/PublicAPI.Unshipped.txt # src/Core/src/PublicAPI/net-ios/PublicAPI.Unshipped.txt # src/Core/src/PublicAPI/net-maccatalyst/PublicAPI.Unshipped.txt # src/Core/src/PublicAPI/net-tizen/PublicAPI.Unshipped.txt # src/Core/src/PublicAPI/net-windows/PublicAPI.Unshipped.txt # src/Core/src/PublicAPI/net/PublicAPI.Unshipped.txt # src/Core/src/PublicAPI/netstandard/PublicAPI.Unshipped.txt # src/Core/src/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt * remove sandbox changes * remove unneeded change * Change Windows type * - txt file fixes * Follow the pattern from others # Conflicts: # src/Controls/tests/DeviceTests/Elements/HybridWebView/HybridWebViewTests.cs * windows is done * Rework windows * regen apis * [WIP] Can you have a look at this PR and tell me what you think (#30819) * Initial plan * Fix documentation bug in IInitializationAwareWebView.cs Co-authored-by: mattleibow <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: mattleibow <[email protected]> * typo * Refactor things a bit * fix this * did this thing * tests * better a bit * windows needs elements * Tests need to run in the test block * WebView2 has restrictions WebView2 does not allow different envvars and some settings in a single process simultaneously without also configuring different user data folders. Also, better to run the webview tests in series as they can confict with other things we do. * I already knew this... --------- Co-authored-by: tj-devel709 <[email protected]> Co-authored-by: TJ Lambert <[email protected]> Co-authored-by: Shane Neuville (HE/HIM) <[email protected]> Co-authored-by: Matthew Leibowitz <[email protected]> Co-authored-by: Copilot <[email protected]> Co-authored-by: mattleibow <[email protected]>
1 parent fd1a594 commit a7a8aa3

File tree

43 files changed

+2464
-1157
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2464
-1157
lines changed

src/Controls/samples/Controls.Sample/Maui.Controls.Sample.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@
7878
<ItemGroup>
7979
<EmbeddedResource Include="Resources\Embedded\*" />
8080
<MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
81-
<MauiAsset Include="..\..\..\Core\src\Handlers\HybridWebView\HybridWebView.js" LogicalName="HybridSamplePage\scripts\HybridWebView.js" />
8281
<MauiImage Include="Resources\Images\*" />
8382
<MauiImage Update="Resources\Images\*.gif" Resize="false" />
8483
<MauiIcon Include="Resources\AppIcons\appicon.svg" ForegroundFile="Resources\AppIcons\appicon_foreground.svg" />

src/Controls/src/Core/HybridWebView/HybridWebView.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,32 @@ void IHybridWebView.RawMessageReceived(string rawMessage)
6666
/// </summary>
6767
public event EventHandler<HybridWebViewRawMessageReceivedEventArgs>? RawMessageReceived;
6868

69+
/// <inheritdoc/>
70+
void IInitializationAwareWebView.WebViewInitializationStarted(WebViewInitializationStartedEventArgs args)
71+
{
72+
var platformArgs = new PlatformWebViewInitializingEventArgs(args);
73+
var e = new WebViewInitializingEventArgs(platformArgs);
74+
WebViewInitializing?.Invoke(this, e);
75+
}
76+
77+
/// <summary>
78+
/// Raised when the web view is initializing. This event allows the application to perform additional configuration.
79+
/// </summary>
80+
public event EventHandler<WebViewInitializingEventArgs>? WebViewInitializing;
81+
82+
/// <inheritdoc/>
83+
void IInitializationAwareWebView.WebViewInitializationCompleted(WebViewInitializationCompletedEventArgs args)
84+
{
85+
var platformArgs = new PlatformWebViewInitializedEventArgs(args);
86+
var e = new WebViewInitializedEventArgs(platformArgs);
87+
WebViewInitialized?.Invoke(this, e);
88+
}
89+
90+
/// <summary>
91+
/// Raised when the web view has been initialized.
92+
/// </summary>
93+
public event EventHandler<WebViewInitializedEventArgs>? WebViewInitialized;
94+
6995
/// <inheritdoc/>
7096
bool IWebRequestInterceptingWebView.WebResourceRequested(WebResourceRequestedEventArgs args)
7197
{
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#if WINDOWS
2+
using Microsoft.Web.WebView2.Core;
3+
#elif IOS || MACCATALYST
4+
using WebKit;
5+
#elif ANDROID
6+
using Android.Webkit;
7+
#endif
8+
9+
namespace Microsoft.Maui.Controls;
10+
11+
/// <summary>
12+
/// Provides platform-specific information about the <see cref="WebViewInitializedEventArgs"/> event.
13+
/// </summary>
14+
public class PlatformWebViewInitializedEventArgs
15+
{
16+
#if IOS || MACCATALYST
17+
18+
/// <summary>
19+
/// Initializes a new instance of the <see cref="WebViewInitializationCompletedEventArgs"/> class.
20+
/// </summary>
21+
/// <param name="sender">The native view that is being initialized.</param>
22+
/// <param name="configuration">The settings for the web view, which can be used to configure various aspects of the web view.</param>
23+
internal PlatformWebViewInitializedEventArgs(WKWebView sender, WKWebViewConfiguration configuration)
24+
{
25+
Sender = sender;
26+
Configuration = configuration;
27+
}
28+
29+
/// <summary>
30+
/// Initializes a new instance of the <see cref="PlatformWebViewInitializedEventArgs"/> class.
31+
/// </summary>
32+
/// <param name="args">The event arguments containing the native view and configuration.</param>
33+
internal PlatformWebViewInitializedEventArgs(WebViewInitializationCompletedEventArgs args)
34+
: this(args.Sender, args.Configuration)
35+
{
36+
}
37+
38+
/// <summary>
39+
/// Gets the native view attached to the event.
40+
/// </summary>
41+
public WKWebView Sender { get; }
42+
43+
/// <summary>
44+
/// Gets or sets the settings attached to the web view.
45+
/// </summary>
46+
public WKWebViewConfiguration Configuration { get; }
47+
48+
#elif ANDROID
49+
50+
/// <summary>
51+
/// Initializes a new instance of the <see cref="WebViewInitializationCompletedEventArgs"/> class.
52+
/// </summary>
53+
/// <param name="sender">The native view that is being initialized.</param>
54+
/// <param name="settings">The settings for the web view, which can be used to configure various aspects of the web view.</param>
55+
internal PlatformWebViewInitializedEventArgs(global::Android.Webkit.WebView sender, WebSettings settings)
56+
{
57+
Sender = sender;
58+
Settings = settings;
59+
}
60+
61+
/// <summary>
62+
/// Initializes a new instance of the <see cref="PlatformWebViewInitializedEventArgs"/> class.
63+
/// </summary>
64+
/// <param name="args">The event arguments containing the native view and configuration.</param>
65+
internal PlatformWebViewInitializedEventArgs(WebViewInitializationCompletedEventArgs args)
66+
: this(args.Sender, args.Settings)
67+
{
68+
}
69+
70+
/// <summary>
71+
/// Gets the native view attached to the event.
72+
/// </summary>
73+
public global::Android.Webkit.WebView Sender { get; }
74+
75+
/// <summary>
76+
/// Gets or sets the settings attached to the web view.
77+
/// </summary>
78+
public WebSettings Settings { get; }
79+
80+
#elif WINDOWS
81+
82+
/// <summary>
83+
/// Initializes a new instance of the <see cref="WebViewInitializationCompletedEventArgs"/> class.
84+
/// </summary>
85+
/// <param name="sender">The native view that is being initialized.</param>
86+
/// <param name="settings">The settings for the web view, which can be used to configure various aspects of the web view.</param>
87+
internal PlatformWebViewInitializedEventArgs(CoreWebView2 sender, CoreWebView2Settings settings)
88+
{
89+
Sender = sender;
90+
Settings = settings;
91+
}
92+
93+
/// <summary>
94+
/// Initializes a new instance of the <see cref="PlatformWebViewInitializedEventArgs"/> class.
95+
/// </summary>
96+
/// <param name="args">The event arguments containing the native view and configuration.</param>
97+
internal PlatformWebViewInitializedEventArgs(WebViewInitializationCompletedEventArgs args)
98+
: this(args.Sender, args.Settings)
99+
{
100+
}
101+
102+
/// <summary>
103+
/// Gets the native view attached to the event.
104+
/// </summary>
105+
public CoreWebView2 Sender { get; }
106+
107+
/// <summary>
108+
/// Gets or sets the settings attached to the web view.
109+
/// </summary>
110+
public CoreWebView2Settings Settings { get; }
111+
112+
#else
113+
114+
internal PlatformWebViewInitializedEventArgs(WebViewInitializationCompletedEventArgs args)
115+
{
116+
}
117+
118+
#endif
119+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#if IOS || MACCATALYST
2+
using WebKit;
3+
#elif ANDROID
4+
using Android.Webkit;
5+
#elif WINDOWS
6+
using Microsoft.Web.WebView2.Core;
7+
#endif
8+
9+
namespace Microsoft.Maui.Controls;
10+
11+
/// <summary>
12+
/// Provides platform-specific information about the <see cref="WebViewInitializingEventArgs"/> event.
13+
/// </summary>
14+
public class PlatformWebViewInitializingEventArgs
15+
{
16+
readonly WebViewInitializationStartedEventArgs _coreArgs;
17+
18+
internal PlatformWebViewInitializingEventArgs(WebViewInitializationStartedEventArgs args)
19+
{
20+
_coreArgs = args;
21+
}
22+
23+
#if WINDOWS
24+
25+
/// <summary>
26+
/// Gets or sets the relative path to the folder that contains a custom
27+
/// version of WebView2 Runtime.
28+
/// </summary>
29+
/// <remarks>
30+
/// To use a fixed version of the WebView2 Runtime, set this property to
31+
/// the folder path that contains the fixed version of the WebView2 Runtime.
32+
/// </remarks>
33+
public string? BrowserExecutableFolder
34+
{
35+
get => _coreArgs.BrowserExecutableFolder;
36+
set => _coreArgs.BrowserExecutableFolder = value;
37+
}
38+
39+
/// <summary>
40+
/// Gets or sets the user data folder location for WebView2.
41+
/// </summary>
42+
/// <remarks>
43+
/// The default user data folder {Executable File Name}.WebView2 is created
44+
/// in the same directory next to the compiled code for the app.
45+
/// WebView2 creation fails if the compiled code is running in a directory
46+
/// in which the process does not have permission to create a new directory.
47+
/// The app is responsible to clean up the associated user data folder
48+
/// when it is done.
49+
/// </remarks>
50+
public string? UserDataFolder
51+
{
52+
get => _coreArgs.UserDataFolder;
53+
set => _coreArgs.UserDataFolder = value;
54+
}
55+
56+
/// <summary>
57+
/// Gets or sets the options used to create WebView2 Environment.
58+
/// </summary>
59+
/// <remarks>
60+
/// As a browser process may be shared among WebViews, WebView creation fails
61+
/// if the specified options does not match the options of the WebViews
62+
/// that are currently running in the shared browser process.
63+
/// </remarks>
64+
public CoreWebView2EnvironmentOptions? EnvironmentOptions
65+
{
66+
get => _coreArgs.EnvironmentOptions;
67+
set => _coreArgs.EnvironmentOptions = value;
68+
}
69+
70+
/// <summary>
71+
/// Gets or sets whether the WebView2 controller is in private mode.
72+
/// </summary>
73+
public bool IsInPrivateModeEnabled
74+
{
75+
get => _coreArgs.IsInPrivateModeEnabled;
76+
set => _coreArgs.IsInPrivateModeEnabled = value;
77+
}
78+
79+
/// <summary>
80+
/// Gets or sets the name of the controller profile.
81+
/// </summary>
82+
/// <remarks>
83+
/// Profile names are only allowed to contain the following ASCII characters:
84+
/// * alphabet characters: a-z and A-Z
85+
/// * digit characters: 0-9
86+
/// * and '#', '@', '$', '(', ')', '+', '-', '_', '~', '.', ' ' (space).
87+
/// It has a maximum length of 64 characters excluding the null-terminator.
88+
/// It is ASCII case insensitive.
89+
/// </remarks>
90+
public string? ProfileName
91+
{
92+
get => _coreArgs.ProfileName;
93+
set => _coreArgs.ProfileName = value;
94+
}
95+
96+
/// <summary>
97+
/// Gets or sets the controller's default script locale.
98+
/// </summary>
99+
/// <remarks>
100+
/// This property sets the default locale for all Intl JavaScript APIs and other JavaScript APIs that
101+
/// depend on it, namely Intl.DateTimeFormat() which affects string formatting like in the time/date
102+
/// formats. The intended locale value is in the format of BCP 47 Language Tags.
103+
/// More information can be found from https://www.ietf.org/rfc/bcp/bcp47.html.
104+
/// The default value for ScriptLocale will be depend on the WebView2 language and OS region.
105+
/// If the language portions of the WebView2 language and OS region match, then it will use the OS region.
106+
/// Otherwise, it will use the WebView2 language.
107+
/// </remarks>
108+
public string? ScriptLocale
109+
{
110+
get => _coreArgs.ScriptLocale;
111+
set => _coreArgs.ScriptLocale = value;
112+
}
113+
114+
#elif IOS || MACCATALYST
115+
116+
/// <summary>
117+
/// Gets or sets the configuration to be used in the construction of the WKWebView instance.
118+
/// </summary>
119+
public WKWebViewConfiguration Configuration => _coreArgs.Configuration;
120+
121+
#elif ANDROID
122+
123+
/// <summary>
124+
/// Gets the platform-specific settings for the WebView.
125+
/// </summary>
126+
public WebSettings Settings => _coreArgs.Settings;
127+
128+
#else
129+
130+
#endif
131+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
3+
namespace Microsoft.Maui.Controls;
4+
5+
/// <summary>
6+
/// Event arguments for the <see cref="HybridWebView.WebViewInitialized"/> event.
7+
/// </summary>
8+
public class WebViewInitializedEventArgs : EventArgs
9+
{
10+
/// <summary>
11+
/// Initializes a new instance of the <see cref="WebViewInitializedEventArgs"/> class
12+
/// with the specified platform-specific arguments.
13+
/// </summary>
14+
/// <param name="platformArgs">The platform-specific event arguments.</param>
15+
public WebViewInitializedEventArgs(PlatformWebViewInitializedEventArgs platformArgs)
16+
{
17+
PlatformArgs = platformArgs;
18+
}
19+
20+
/// <summary>
21+
/// Gets the platform-specific event arguments.
22+
/// </summary>
23+
public PlatformWebViewInitializedEventArgs? PlatformArgs { get; }
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
3+
namespace Microsoft.Maui.Controls;
4+
5+
/// <summary>
6+
/// Event arguments for the <see cref="HybridWebView.WebViewInitializing"/> event.
7+
/// </summary>
8+
public class WebViewInitializingEventArgs : EventArgs
9+
{
10+
/// <summary>
11+
/// Initializes a new instance of the <see cref="WebViewInitializingEventArgs"/> class
12+
/// with the specified platform-specific arguments.
13+
/// </summary>
14+
/// <param name="platformArgs">The platform-specific event arguments.</param>
15+
public WebViewInitializingEventArgs(PlatformWebViewInitializingEventArgs platformArgs)
16+
{
17+
PlatformArgs = platformArgs;
18+
}
19+
20+
/// <summary>
21+
/// Gets the platform-specific event arguments.
22+
/// </summary>
23+
public PlatformWebViewInitializingEventArgs? PlatformArgs { get; }
24+
}

src/Controls/src/Core/PublicAPI/net-android/PublicAPI.Unshipped.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ Microsoft.Maui.Controls.Handlers.TabbedPageManager.NotifyDataSetChanged() -> voi
7676
~Microsoft.Maui.Controls.Handlers.TabbedPageManager.previousPage -> Microsoft.Maui.Controls.Page
7777
Microsoft.Maui.Controls.HybridWebView.InvokeJavaScriptAsync(string! methodName, object?[]? paramValues = null, System.Text.Json.Serialization.Metadata.JsonTypeInfo?[]? paramJsonTypeInfos = null) -> System.Threading.Tasks.Task!
7878
Microsoft.Maui.Controls.HybridWebView.WebResourceRequested -> System.EventHandler<Microsoft.Maui.Controls.WebViewWebResourceRequestedEventArgs!>?
79+
Microsoft.Maui.Controls.HybridWebView.WebViewInitialized -> System.EventHandler<Microsoft.Maui.Controls.WebViewInitializedEventArgs!>?
80+
Microsoft.Maui.Controls.HybridWebView.WebViewInitializing -> System.EventHandler<Microsoft.Maui.Controls.WebViewInitializingEventArgs!>?
7981
Microsoft.Maui.Controls.ICornerElement
8082
Microsoft.Maui.Controls.ICornerElement.CornerRadius.get -> Microsoft.Maui.CornerRadius
8183
Microsoft.Maui.Controls.IExtendedTypeConverter.ConvertFromInvariantString(string! value, System.IServiceProvider! serviceProvider) -> object?
@@ -141,6 +143,11 @@ Microsoft.Maui.Controls.PickerClosedEventArgs.PickerClosedEventArgs() -> void
141143
Microsoft.Maui.Controls.PickerOpenedEventArgs
142144
Microsoft.Maui.Controls.PickerOpenedEventArgs.PickerOpenedEventArgs() -> void
143145
Microsoft.Maui.Controls.PlatformConfiguration.iOSSpecific.UIModalPresentationStyle.Popover = 5 -> Microsoft.Maui.Controls.PlatformConfiguration.iOSSpecific.UIModalPresentationStyle
146+
Microsoft.Maui.Controls.PlatformWebViewInitializedEventArgs
147+
Microsoft.Maui.Controls.PlatformWebViewInitializedEventArgs.Sender.get -> Android.Webkit.WebView!
148+
Microsoft.Maui.Controls.PlatformWebViewInitializedEventArgs.Settings.get -> Android.Webkit.WebSettings!
149+
Microsoft.Maui.Controls.PlatformWebViewInitializingEventArgs
150+
Microsoft.Maui.Controls.PlatformWebViewInitializingEventArgs.Settings.get -> Android.Webkit.WebSettings!
144151
Microsoft.Maui.Controls.PlatformWebViewWebResourceRequestedEventArgs
145152
Microsoft.Maui.Controls.PlatformWebViewWebResourceRequestedEventArgs.Request.get -> Android.Webkit.IWebResourceRequest!
146153
Microsoft.Maui.Controls.PlatformWebViewWebResourceRequestedEventArgs.Response.get -> Android.Webkit.WebResourceResponse?
@@ -194,6 +201,12 @@ Microsoft.Maui.Controls.TimePickerClosedEventArgs
194201
Microsoft.Maui.Controls.TimePickerClosedEventArgs.TimePickerClosedEventArgs() -> void
195202
Microsoft.Maui.Controls.TimePickerOpenedEventArgs
196203
Microsoft.Maui.Controls.TimePickerOpenedEventArgs.TimePickerOpenedEventArgs() -> void
204+
Microsoft.Maui.Controls.WebViewInitializedEventArgs
205+
Microsoft.Maui.Controls.WebViewInitializedEventArgs.PlatformArgs.get -> Microsoft.Maui.Controls.PlatformWebViewInitializedEventArgs?
206+
Microsoft.Maui.Controls.WebViewInitializedEventArgs.WebViewInitializedEventArgs(Microsoft.Maui.Controls.PlatformWebViewInitializedEventArgs! platformArgs) -> void
207+
Microsoft.Maui.Controls.WebViewInitializingEventArgs
208+
Microsoft.Maui.Controls.WebViewInitializingEventArgs.PlatformArgs.get -> Microsoft.Maui.Controls.PlatformWebViewInitializingEventArgs?
209+
Microsoft.Maui.Controls.WebViewInitializingEventArgs.WebViewInitializingEventArgs(Microsoft.Maui.Controls.PlatformWebViewInitializingEventArgs! platformArgs) -> void
197210
Microsoft.Maui.Controls.WebViewWebResourceRequestedEventArgs
198211
Microsoft.Maui.Controls.WebViewWebResourceRequestedEventArgs.Handled.get -> bool
199212
Microsoft.Maui.Controls.WebViewWebResourceRequestedEventArgs.Handled.set -> void

0 commit comments

Comments
 (0)