Skip to content

Commit 799322c

Browse files
Updates for Win32, WPF, WinForms, UWP and WinUI3 sample apps from 130.0.2839.0
1 parent d1cf80d commit 799322c

File tree

7 files changed

+578
-320
lines changed

7 files changed

+578
-320
lines changed

SampleApps/WebView2APISample/AppWindow.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,11 +1429,11 @@ HRESULT AppWindow::OnCreateEnvironmentCompleted(
14291429
m_webViewEnvironment = environment;
14301430

14311431
if (m_webviewOption.entry == WebViewCreateEntry::EVER_FROM_CREATE_WITH_OPTION_MENU ||
1432-
m_creationModeId == IDM_CREATION_MODE_HOST_INPUT_PROCESSING
1433-
)
1432+
m_creationModeId == IDM_CREATION_MODE_HOST_INPUT_PROCESSING)
14341433
{
14351434
return CreateControllerWithOptions();
14361435
}
1436+
14371437
auto webViewEnvironment3 = m_webViewEnvironment.try_query<ICoreWebView2Environment3>();
14381438

14391439
if (webViewEnvironment3 && (m_dcompDevice || m_wincompCompositor))
@@ -1522,6 +1522,7 @@ HRESULT AppWindow::CreateControllerWithOptions()
15221522
}
15231523
}
15241524
//! [AllowHostInputProcessing]
1525+
15251526
if (m_dcompDevice || m_wincompCompositor)
15261527
{
15271528
//! [OnCreateCoreWebView2ControllerCompleted]

SampleApps/WebView2APISample/WebView2APISample.vcxproj

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<ItemGroup Label="ProjectConfigurations">
44
<ProjectConfiguration Include="Debug|Win32">
@@ -391,6 +391,12 @@
391391
<CopyFileToFolders Include="assets/ScenarioScreenCaptureIFrame2.html">
392392
<DestinationFolders>$(OutDir)\assets</DestinationFolders>
393393
</CopyFileToFolders>
394+
<CopyFileToFolders Include="assets/ScenarioServiceWorkerSyncRegistrationManager.html">
395+
<DestinationFolders>$(OutDir)\assets</DestinationFolders>
396+
</CopyFileToFolders>
397+
<CopyFileToFolders Include="assets/ScenarioServiceWorkerSyncRegistrationManagerServiceWorker.js">
398+
<DestinationFolders>$(OutDir)\assets</DestinationFolders>
399+
</CopyFileToFolders>
394400
<CopyFileToFolders Include="assets/ScenarioSharedBuffer.html">
395401
<DestinationFolders>$(OutDir)\assets</DestinationFolders>
396402
</CopyFileToFolders>
@@ -489,4 +495,4 @@
489495
<Error Condition="!Exists('..\packages\Microsoft.Windows.ImplementationLibrary.1.0.220201.1\build\native\Microsoft.Windows.ImplementationLibrary.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Windows.ImplementationLibrary.1.0.220201.1\build\native\Microsoft.Windows.ImplementationLibrary.targets'))" />
490496
<Error Condition="!Exists('..\packages\Microsoft.Web.WebView2.1.0.2783-prerelease\build\native\Microsoft.Web.WebView2.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Web.WebView2.1.0.2783-prerelease\build\native\Microsoft.Web.WebView2.targets'))" />
491497
</Target>
492-
</Project>
498+
</Project>
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>ScenarioServiceWorkerSyncRegistrationManager</title>
5+
</head>
6+
7+
<body>
8+
<h3>Periodic Background Sync Example</h3>
9+
<h4>Registrations</h4>
10+
<p id="periodic-sync-registrations"></p>
11+
<div style="display: inline-block;">
12+
<input type="text" placeholder="tag name" id="add-tag1">
13+
<input type="text" placeholder="min interval" id="add-interval">
14+
<button onclick="registeredSync('periodic-sync')"> Register </button>
15+
</div>
16+
17+
<div>
18+
<input type="text" placeholder="tag name" id="remove-tag1">
19+
<button onclick="unregisterSync('periodic-sync')"> Unregister </button>
20+
</div>
21+
22+
<div>
23+
<input type="text" placeholder="tag name" id="dispatch-tag">
24+
<button onclick="dispatchPeriodicSyncEvent()"> Dispatch Periodic Sync Task </button>
25+
</div>
26+
27+
<div>
28+
<button onclick="getSyncRegistrationsFromWebView2('periodic-sync')"> Get all Periodic Sync Registrations from WebView2</button>
29+
</div>
30+
<hr>
31+
32+
<h3>Background Sync Example</h3>
33+
<h4>Registrations</h4>
34+
<p id="background-sync-registrations"></p>
35+
<div style="display: inline-block;">
36+
<input type="text" placeholder="tag name" id="add-tag2">
37+
<button onclick="registeredSync('background-sync')"> Register </button>
38+
</div>
39+
40+
<div>
41+
<input type="text" placeholder="tag name" id="remove-tag2">
42+
<button onclick="unregisterSync('background-sync')"> Unregister </button>
43+
</div>
44+
<div>
45+
<button onclick="getSyncRegistrationsFromWebView2('background-sync')"> Get all Background Sync Registrations from WebView2</button>
46+
</div>
47+
48+
<script>
49+
async function checkPermission() {
50+
const status = await navigator.permissions.query({name: 'periodic-background-sync'});
51+
if (status.state === 'granted') {
52+
console.log("periodic-background-sync permission is granted");
53+
} else {
54+
console.log("periodic-background-sync permission is denied");
55+
}
56+
}
57+
58+
async function registerServiceWorker() {
59+
if ('serviceWorker' in navigator) {
60+
const registration = await navigator.serviceWorker.register(
61+
'ScenarioServiceWorkerSyncRegistrationManagerServiceWorker.js', {
62+
scope: './',
63+
});
64+
}
65+
}
66+
67+
async function unregisterSync(type) {
68+
const registration = await navigator.serviceWorker.ready;
69+
let tag = document.getElementById("remove-tag1").value;
70+
if (type === "periodic-sync") {
71+
if ('periodicSync' in registration) {
72+
await registration.periodicSync.unregister(tag);
73+
}
74+
} else if (type === "background-sync") {
75+
tag = document.getElementById("remove-tag2").value;
76+
if ('sync' in registration) {
77+
await registration.sync.unregister(tag);
78+
}
79+
}
80+
getSyncRegistrations(type);
81+
}
82+
83+
async function registeredSync(type) {
84+
const registration = await navigator.serviceWorker.ready;
85+
let tag = document.getElementById("add-tag1").value;
86+
if (type === "periodic-sync") {
87+
const interval = document.getElementById("add-interval").value;
88+
if ('periodicSync' in registration) {
89+
try {
90+
await registration.periodicSync.register(tag, { minInterval: interval });
91+
} catch (error) {
92+
console.log(error);
93+
}
94+
}
95+
} else if (type === "background-sync") {
96+
tag = document.getElementById("add-tag2").value;
97+
if ('sync' in registration) {
98+
try {
99+
await registration.sync.register(tag);
100+
} catch (error) {
101+
console.log(error);
102+
}
103+
}
104+
}
105+
getSyncRegistrations(type);
106+
}
107+
108+
async function getSyncRegistrations(type) {
109+
const registration = await navigator.serviceWorker.ready;
110+
let elementId = "periodic-sync-registrations";
111+
let tags;
112+
if (type === "background-sync") {
113+
elementId = "background-sync-registrations";
114+
tags = await registration.sync.getTags();
115+
} else if (type === "periodic-sync") {
116+
tags = await registration.periodicSync.getTags();
117+
}
118+
var registrations = "";
119+
tags.forEach(tag => {
120+
registrations += "Tag name: ";
121+
registrations += tag;
122+
registrations += " ";
123+
});
124+
if (registrations === "") {
125+
registrations = "empty";
126+
}
127+
document.getElementById(elementId).textContent = registrations;
128+
}
129+
130+
async function dispatchPeriodicSyncEvent() {
131+
const registration = await navigator.serviceWorker.ready;
132+
const tag = document.getElementById("dispatch-tag").value;
133+
chrome.webview.postMessage(`DispatchPeriodicSyncEvent ${tag}`);
134+
}
135+
136+
async function getSyncRegistrationsFromWebView2(type) {
137+
if (type === "periodic-sync") {
138+
chrome.webview.postMessage("GetPeriodicSyncRegistrations");
139+
} else if ("background-sync") {
140+
chrome.webview.postMessage("GetBackgroundSyncRegistrations");
141+
}
142+
}
143+
144+
checkPermission();
145+
registerServiceWorker();
146+
getSyncRegistrations("periodic-sync");
147+
getSyncRegistrations("background-sync");
148+
149+
</script>
150+
</body>
151+
</html>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function fetchAndCacheLatestNews() {
2+
console.log("Fetched news from a server");
3+
}
4+
5+
self.addEventListener("periodicsync", (event) => {
6+
console.log("Periodic Sync Task Tag: " + event.tag + " executed");
7+
event.waitUntil(fetchAndCacheLatestNews());
8+
});
9+
10+
self.addEventListener("sync", (event) => {
11+
console.log("Background Sync Task Tag: " + event.tag + " executed");
12+
});

SampleApps/WebView2WpfBrowser/App.xaml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,5 @@ found in the LICENSE file.
1212
xmlns:local="clr-namespace:WebView2WpfBrowser"
1313
StartupUri="MainWindow.xaml">
1414
<Application.Resources>
15-
<!--
16-
These CreationProperties use the Evergreen Edge executable that's installed on the machine.
17-
-->
18-
<wv2:CoreWebView2CreationProperties x:Key="EvergreenWebView2CreationProperties" />
19-
<!--
20-
If you want to use fixed version:
21-
1) Navigate to https://developer.microsoft.com/en-us/microsoft-edge/webview2/
22-
2) Choose "Fixed Version", select appropriate version and architecture and click download
23-
3) Unzip the CAB file to a folder
24-
4) Point that folder by `BrowserExecutableFolder` property
25-
-->
26-
<wv2:CoreWebView2CreationProperties x:Key="BYOWebView2CreationProperties" BrowserExecutableFolder="Replace this with BYO folder" />
2715
</Application.Resources>
2816
</Application>

SampleApps/WebView2WpfBrowser/MainWindow.xaml

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ found in the LICENSE file.
1616
Width="800"
1717
>
1818
<Window.Resources>
19+
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
1920
</Window.Resources>
2021
<Window.CommandBindings>
2122
<CommandBinding Command="ApplicationCommands.New" Executed="NewCmdExecuted"/>
@@ -60,6 +61,7 @@ found in the LICENSE file.
6061
<CommandBinding Command="{x:Static local:MainWindow.ClearServerCertificateErrorActionsCommand}" Executed="ClearServerCertificateErrorActionsCmdExecuted" CanExecute="CoreWebView2RequiringCmdsCanExecute"/>
6162
<CommandBinding Command="{x:Static local:MainWindow.SetDefaultDownloadPathCommand}" Executed="SetDefaultDownloadPathCmdExecuted" CanExecute="CoreWebView2RequiringCmdsCanExecute"/>
6263
<CommandBinding Command="{x:Static local:MainWindow.CreateDownloadsButtonCommand}" Executed="CreateDownloadsButtonCmdExecuted" CanExecute="CoreWebView2RequiringCmdsCanExecute"/>
64+
<CommandBinding Command="{x:Static local:MainWindow.ShowExtensionsWindowCommand}" Executed="ShowExtensionsWindowCmdExecuted" CanExecute="CoreWebView2RequiringCmdsCanExecute"/>
6365
<CommandBinding Command="{x:Static local:MainWindow.ToggleMuteStateCommand}" Executed="ToggleMuteStateCmdExecuted" CanExecute="CoreWebView2RequiringCmdsCanExecute"/>
6466
<CommandBinding Command="{x:Static local:MainWindow.PrintToPdfCommand}" Executed="PrintToPdfCmdExecuted" CanExecute="CoreWebView2RequiringCmdsCanExecute"/>
6567
<CommandBinding Command="{x:Static local:MainWindow.PinchZoomCommand}" Executed="PinchZoomCmdExecuted" CanExecute="CoreWebView2RequiringCmdsCanExecute"/>
@@ -94,7 +96,8 @@ found in the LICENSE file.
9496
<CommandBinding Command="{x:Static local:MainWindow.BrowserAcceleratorKeyEnabledCommand}" Executed="BrowserAcceleratorKeyEnabledCommandExecuted" CanExecute="CoreWebView2RequiringCmdsCanExecute"/>
9597

9698
<CommandBinding Command="{x:Static local:MainWindow.CloseWebViewCommand}" Executed="CloseWebViewCommandExecuted" CanExecute="CoreWebView2RequiringCmdsCanExecute"/>
97-
<CommandBinding Command="{x:Static local:MainWindow.NewWebViewCommand}" Executed="NewWebViewCommandExecuted" CanExecute="CoreWebView2RequiringCmdsCanExecute"/>
99+
<CommandBinding Command="{x:Static local:MainWindow.NewWebViewCommand}" Executed="NewWebViewCommandExecuted"/>
100+
<CommandBinding Command="{x:Static local:MainWindow.NewWebViewCompositionControlCommand}" Executed="NewWebViewCompositionControlCommandExecuted" CanExecute="EpxerimentalCmdsCanExecute"/>
98101

99102
<CommandBinding Command="{x:Static local:MainWindow.PermissionManagementCommand}" Executed="PermissionManagementExecuted" CanExecute="CoreWebView2RequiringCmdsCanExecute"/>
100103
<CommandBinding Command="{x:Static local:MainWindow.NonClientRegionSupportCommand}" Executed="NonClientRegionSupportCmdExecuted" CanExecute="CoreWebView2RequiringCmdsCanExecute"/>
@@ -136,8 +139,9 @@ found in the LICENSE file.
136139
<!--TODO-->
137140
</MenuItem>
138141
<MenuItem Header="_Window">
139-
<MenuItem Header="_Close WebView" Command="ApplicationCommands.Close"/>
140-
<MenuItem Header="_New WebView" Command="{x:Static local:MainWindow.NewWebViewCommand}"/>
142+
<MenuItem Header="_Close WebView2" Command="{x:Static local:MainWindow.CloseWebViewCommand}"/>
143+
<MenuItem Header="_New WebView2" Command="{x:Static local:MainWindow.NewWebViewCommand}"/>
144+
<MenuItem Header="_New WebView2CompositionControl" Command="{x:Static local:MainWindow.NewWebViewCompositionControlCommand}"/>
141145
<MenuItem Header="_New Window" Command="ApplicationCommands.New"/>
142146
<MenuItem Header="_New Window With Options" Command="{x:Static local:MainWindow.NewWindowWithOptionsCommand}"/>
143147
<MenuItem Header="_Create New Thread" Command="{x:Static local:MainWindow.CreateNewThreadCommand}"/>
@@ -176,7 +180,7 @@ found in the LICENSE file.
176180
<MenuItem Header="Toggle Non-Client Region Support" Command="{x:Static local:MainWindow.NonClientRegionSupportCommand}"/>
177181
</MenuItem>
178182
<MenuItem Header="_View">
179-
<MenuItem Header="Toggle _Visibility" Name="webViewVisible" IsCheckable="True" IsChecked="True"/>
183+
<MenuItem Header="Toggle _Visibility" Name="webViewVisible" IsCheckable="True" IsChecked="True" Checked="OnWebViewVisibleChecked" Unchecked="OnWebViewVisibleUnchecked"/>
180184
<MenuItem Header="Suspend" Command="{x:Static local:MainWindow.SuspendCommand}"/>
181185
<MenuItem Header="Resume" Command="{x:Static local:MainWindow.ResumeCommand}"/>
182186
<MenuItem Header="_Increase Zoom" Command="NavigationCommands.IncreaseZoom"/>
@@ -188,6 +192,7 @@ found in the LICENSE file.
188192
<MenuItem Header="Transparent" Command="{x:Static local:MainWindow.BackgroundColorCommand}" CommandParameter="Transparent"/>
189193
</MenuItem>
190194
<MenuItem Header="_Create Downloads Button" Command="{x:Static local:MainWindow.CreateDownloadsButtonCommand}"/>
195+
<MenuItem Header="Show Extensions Manager" Command="{x:Static local:MainWindow.ShowExtensionsWindowCommand}" />
191196
</MenuItem>
192197
<MenuItem Header="S_cenario">
193198
<MenuItem Header="Au_thentication" Command="{x:Static local:MainWindow.AuthenticationCommand}"/>
@@ -259,7 +264,7 @@ found in the LICENSE file.
259264
<Button DockPanel.Dock="Left" Command="NavigationCommands.BrowseStop">Cancel</Button>
260265
<Button DockPanel.Dock="Right" Command="NavigationCommands.GoToPage" CommandParameter="{Binding ElementName=url,Path=Text}">Go</Button>
261266
<!-- We want the address bar to update based on the WebView's Source, but we don't want the WebView to navigate just from the user typing into the address bar. Therefore we use the OneWay binding mode. -->
262-
<TextBox x:Name="url" Text="{Binding ElementName=webView,Path=Source,Mode=OneWay}">
267+
<TextBox x:Name="url" Text="{Binding ElementName=webView2XamlElement,Path=Source,Mode=OneWay}">
263268
<TextBox.InputBindings>
264269
<KeyBinding Key="Return" Command="NavigationCommands.GoToPage" CommandParameter="{Binding ElementName=url,Path=Text}" />
265270
</TextBox.InputBindings>
@@ -270,14 +275,18 @@ found in the LICENSE file.
270275
<ImageBrush ImageSource="/assets/AppStartPageBackground.png" />
271276
</Grid.Background>
272277

273-
<!-- If you want to use a specific version of WebView2 Runtime change EvergreenWebView2CreationProperties
274-
to BYOWebView2CreationProperties and follow the steps in MainWindow.xaml
275-
-->
276-
<wv2:WebView2
277-
x:Name="webView"
278-
CreationProperties="{StaticResource EvergreenWebView2CreationProperties}"
279-
Source="https://www.bing.com/"
280-
/>
278+
<wv2:WebView2 x:Name="webView2XamlElement">
279+
<wv2:WebView2.CreationProperties>
280+
<!--
281+
By default, this CreationProperties uses the Evergreen WebView2 Runtime that's installed on the machine.
282+
If you want to use a specific version of WebView2 Runtime, change BrowserExecutableFolder to point to the folder
283+
with the runtime. https://learn.microsoft.com/en-us/microsoft-edge/webview2/concepts/distribution#the-fixed-version-runtime-distribution-mode
284+
-->
285+
<wv2:CoreWebView2CreationProperties
286+
BrowserExecutableFolder=""
287+
AdditionalBrowserArguments="" />
288+
</wv2:WebView2.CreationProperties>
289+
</wv2:WebView2>
281290
<!-- The control event handlers are set in code behind so they can be reused when replacing the control after
282291
a WebView2 Runtime's browser process failure
283292
-->

0 commit comments

Comments
 (0)