Skip to content

WebView2Samples update for 1.0.3116-prerelease #266

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

Merged
merged 2 commits into from
Feb 12, 2025
Merged
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
4 changes: 2 additions & 2 deletions SampleApps/WebView2APISample/WebView2APISample.vcxproj
Original file line number Diff line number Diff line change
@@ -496,13 +496,13 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
<Import Project="..\packages\Microsoft.Windows.ImplementationLibrary.1.0.220201.1\build\native\Microsoft.Windows.ImplementationLibrary.targets" Condition="Exists('..\packages\Microsoft.Windows.ImplementationLibrary.1.0.220201.1\build\native\Microsoft.Windows.ImplementationLibrary.targets')" />
<Import Project="..\packages\Microsoft.Web.WebView2.1.0.3079-prerelease\build\native\Microsoft.Web.WebView2.targets" Condition="Exists('..\packages\Microsoft.Web.WebView2.1.0.3079-prerelease\build\native\Microsoft.Web.WebView2.targets')" />
<Import Project="..\packages\Microsoft.Web.WebView2.1.0.3116-prerelease\build\native\Microsoft.Web.WebView2.targets" Condition="Exists('..\packages\Microsoft.Web.WebView2.1.0.3116-prerelease\build\native\Microsoft.Web.WebView2.targets')" />
</ImportGroup>
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<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'))" />
<Error Condition="!Exists('..\packages\Microsoft.Web.WebView2.1.0.3079-prerelease\build\native\Microsoft.Web.WebView2.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Web.WebView2.1.0.3079-prerelease\build\native\Microsoft.Web.WebView2.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.Web.WebView2.1.0.3116-prerelease\build\native\Microsoft.Web.WebView2.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Web.WebView2.1.0.3116-prerelease\build\native\Microsoft.Web.WebView2.targets'))" />
</Target>
</Project>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion SampleApps/WebView2APISample/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Web.WebView2" version="1.0.3079-prerelease" targetFramework="native" />
<package id="Microsoft.Web.WebView2" version="1.0.3116-prerelease" targetFramework="native" />
<package id="Microsoft.Windows.ImplementationLibrary" version="1.0.220201.1" targetFramework="native" />
</packages>
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.3079-prerelease" />
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.3116-prerelease" />
</ItemGroup>
<ItemGroup>
<Folder Include="assets\" />
24 changes: 24 additions & 0 deletions SampleApps/WebView2WpfBrowser/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -112,6 +112,17 @@ found in the LICENSE file.
<CommandBinding Command="{x:Static local:MainWindow.FileExplorerCommand}" Executed="FileExplorerExecuted" CanExecute="CoreWebView2RequiringCmdsCanExecute"/>
<CommandBinding Command="{x:Static local:MainWindow.ToggleScreenCaptureEnableCommand}" Executed="TogglScreenCaptureEnabledCmdExecuted" CanExecute="CoreWebView2RequiringCmdsCanExecute"/>
<CommandBinding Command="{x:Static local:MainWindow.FileTypePolicyCommand}" Executed="FileTypePolicyExecuted" CanExecute="CoreWebView2RequiringCmdsCanExecute"/>
<CommandBinding Command="{x:Static local:MainWindow.StartCommand}" Executed="StartExecuted" CanExecute="CoreWebView2RequiringCmdsCanExecute"/>
<CommandBinding Command="{x:Static local:MainWindow.FindNextCommand}" Executed="FindNextExecuted" CanExecute="CoreWebView2RequiringCmdsCanExecute"/>
<CommandBinding Command="{x:Static local:MainWindow.FindPreviousCommand}" Executed="FindPreviousExecuted" CanExecute="CoreWebView2RequiringCmdsCanExecute"/>
<CommandBinding Command="{x:Static local:MainWindow.StopFindCommand}" Executed="StopFindExecuted" CanExecute="CoreWebView2RequiringCmdsCanExecute"/>
<CommandBinding Command="{x:Static local:MainWindow.FindTermCommand}" Executed="ChangeFindTermExecuted" CanExecute="CoreWebView2RequiringCmdsCanExecute"/>
<CommandBinding Command="{x:Static local:MainWindow.GetMatchCountCommand}" Executed="GetMatchCountExecuted" CanExecute="CoreWebView2RequiringCmdsCanExecute"/>
<CommandBinding Command="{x:Static local:MainWindow.GetActiveMatchIndexCommand}" Executed="GetActiveMatchIndexExecuted" CanExecute="CoreWebView2RequiringCmdsCanExecute"/>
<CommandBinding Command="{x:Static local:MainWindow.ToggleCaseSensitiveCommand}" Executed="ToggleCaseSensitiveExecuted" CanExecute="CoreWebView2RequiringCmdsCanExecute"/>
<CommandBinding Command="{x:Static local:MainWindow.ToggleShouldHighlightAllMatchesCommand}" Executed="ToggleShouldHighlightAllMatchesExecuted" CanExecute="CoreWebView2RequiringCmdsCanExecute"/>
<CommandBinding Command="{x:Static local:MainWindow.ToggleShouldMatchWordCommand}" Executed="ToggleShouldMatchWordExecuted" CanExecute="CoreWebView2RequiringCmdsCanExecute"/>
<CommandBinding Command="{x:Static local:MainWindow.ToggleSuppressDefaultFindDialogCommand}" Executed="ToggleSuppressDefaultFindDialogExecuted" CanExecute="CoreWebView2RequiringCmdsCanExecute"/>
</Window.CommandBindings>
<DockPanel>
<Menu DockPanel.Dock="Top">
@@ -251,6 +262,19 @@ found in the LICENSE file.
<MenuItem Header="File-system explorer" Command="{x:Static local:MainWindow.FileExplorerCommand}"/>
<MenuItem Header="Toggle Screen Capture" Command="{x:Static local:MainWindow.ToggleScreenCaptureEnableCommand}"/>
<MenuItem Header="File Type Policy" Command="{x:Static local:MainWindow.FileTypePolicyCommand}"/>
<MenuItem Header="Find on Page">
<MenuItem Header="Start Find" Command="{x:Static local:MainWindow.StartCommand}"/>
<MenuItem Header="Find Next" Command="{x:Static local:MainWindow.FindNextCommand}"/>
<MenuItem Header="Find Previous" Command="{x:Static local:MainWindow.FindPreviousCommand}"/>
<MenuItem Header="Stop Find" Command="{x:Static local:MainWindow.StopFindCommand}"/>
<MenuItem Header="Find Term" Command="{x:Static local:MainWindow.FindTermCommand}"/>
<MenuItem Header="Show Match Count" Command="{x:Static local:MainWindow.GetMatchCountCommand}"/>
<MenuItem Header="Show Active Match Index" Command="{x:Static local:MainWindow.GetActiveMatchIndexCommand}"/>
<MenuItem Header="Toggle Case Sensitive" Command="{x:Static local:MainWindow.ToggleCaseSensitiveCommand}"/>
<MenuItem Header="Toggle Highlight All" Command="{x:Static local:MainWindow.ToggleShouldHighlightAllMatchesCommand}"/>
<MenuItem Header="Toggle Match Word" Command="{x:Static local:MainWindow.ToggleShouldMatchWordCommand}"/>
<MenuItem Header="Toggle Default Find Dialog" Command="{x:Static local:MainWindow.ToggleSuppressDefaultFindDialogCommand}"/>
</MenuItem>
</MenuItem>
<MenuItem Header="_Audio">
<MenuItem Header="Toggle Mute State" IsCheckable="True" IsChecked="False" Command="{x:Static local:MainWindow.ToggleMuteStateCommand}"/>
257 changes: 257 additions & 0 deletions SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -140,6 +140,27 @@ public partial class MainWindow : Window
public static RoutedCommand ChildFrameEventsCommand = new RoutedCommand();
public static RoutedCommand RemoveChildFrameEventsCommand = new RoutedCommand();

public static RoutedCommand StartCommand = new RoutedCommand();

public static RoutedCommand FindNextCommand = new RoutedCommand();

public static RoutedCommand FindPreviousCommand = new RoutedCommand();

public static RoutedCommand StopFindCommand = new RoutedCommand();
public static RoutedCommand FindTermCommand = new RoutedCommand();

public static RoutedCommand GetMatchCountCommand = new RoutedCommand();

public static RoutedCommand GetActiveMatchIndexCommand = new RoutedCommand();

public static RoutedCommand ToggleCaseSensitiveCommand = new RoutedCommand();

public static RoutedCommand ToggleShouldHighlightAllMatchesCommand = new RoutedCommand();

public static RoutedCommand ToggleShouldMatchWordCommand = new RoutedCommand();

public static RoutedCommand ToggleSuppressDefaultFindDialogCommand = new RoutedCommand();

#endregion commands

bool _isNavigating = false;
@@ -3931,5 +3952,241 @@ void HandleChildFrameNavigationCompleted(object sender, CoreWebView2NavigationCo
MessageBox.Show(this, "Id: " + Frame.FrameId + " NavigationCompleted", "Child frame Navigation Completed", MessageBoxButton.OK);
}
#endif
#if USE_WEBVIEW2_EXPERIMENTAL

private CoreWebView2FindOptions _findOptions;
private bool _findEventHandlersSet = false;
private string _lastSearchTerm = string.Empty;
#endif
void StartExecuted(object target, ExecutedRoutedEventArgs e)
{
#if USE_WEBVIEW2_EXPERIMENTAL

if (_iWebView2?.CoreWebView2 == null || _iWebView2.CoreWebView2.Find == null)
{
MessageBox.Show("Find API is unavailable.");
return;
}

var dialog = new TextInputDialog(
title: "Find on Page Term",
description: "Enter find term:",
defaultInput: "WebView2");

if (dialog.ShowDialog() == true)
{
_iWebView2.CoreWebView2.Find.Stop();


if (_findOptions == null)
{
_findOptions = CreateDefaultFindOptions();
}

_findOptions.FindTerm = dialog.Input.Text;

SetupFindEventHandlers();

_ = _iWebView2.CoreWebView2.Find.StartAsync(_findOptions);
}
#endif
}

void FindNextExecuted(object target, ExecutedRoutedEventArgs e)
{
#if USE_WEBVIEW2_EXPERIMENTAL

if (_iWebView2?.CoreWebView2?.Find == null) return;
_iWebView2.CoreWebView2.Find.FindNext();
#endif
}

void FindPreviousExecuted(object target, ExecutedRoutedEventArgs e)
{
#if USE_WEBVIEW2_EXPERIMENTAL
if (_iWebView2?.CoreWebView2?.Find == null) return;
_iWebView2.CoreWebView2.Find.FindPrevious();
#endif
}

void StopFindExecuted(object target, ExecutedRoutedEventArgs e)
{
#if USE_WEBVIEW2_EXPERIMENTAL
_iWebView2.CoreWebView2.Find.Stop();
#endif
}
#if USE_WEBVIEW2_EXPERIMENTAL
// Creating find options via environment
private CoreWebView2FindOptions CreateDefaultFindOptions()
{
var webviewEnv = _iWebView2.CoreWebView2.Environment;
if (webviewEnv == null) return null;

var findOptions = webviewEnv.CreateFindOptions();
findOptions.FindTerm = "WebView2";
findOptions.IsCaseSensitive = false;
findOptions.ShouldHighlightAllMatches = true;
findOptions.ShouldMatchWord = false;
findOptions.SuppressDefaultFindDialog = false;
return findOptions;
}
#endif
void ChangeFindTermExecuted(object target, ExecutedRoutedEventArgs e)
{
#if USE_WEBVIEW2_EXPERIMENTAL

// Make sure the WebView2 and its Find interface are available
if (_iWebView2?.CoreWebView2 == null || _iWebView2.CoreWebView2.Find == null)
{
MessageBox.Show("Find API is unavailable or not yet initialized.");
return;
}

// If we haven’t created _findOptions yet, do it now
if (_findOptions == null)
{
_findOptions = CreateDefaultFindOptions();
}

// Prompt the user for a new find term
var dialog = new TextInputDialog(
title: "Change Find on Page Term",
description: "Enter new find term:",
defaultInput: _findOptions.FindTerm // show the current term if you like
);

// If user clicks OK, update the find term
if (dialog.ShowDialog() == true)
{
_findOptions.FindTerm = dialog.Input.Text;
_lastSearchTerm = _findOptions.FindTerm; // track if desired

// We do *not* start the find here; that’s done in StartExecuted.
MessageBox.Show($"Find term changed to: {_findOptions.FindTerm}", "Find on Page");
}
#endif
}

void GetMatchCountExecuted(object target, ExecutedRoutedEventArgs e)
{
#if USE_WEBVIEW2_EXPERIMENTAL

if (_iWebView2?.CoreWebView2?.Find == null) return;

int matchCount = _iWebView2.CoreWebView2.Find.MatchCount;
MessageBox.Show($"Match Count: {matchCount}", "Find Operation", MessageBoxButton.OK);
#endif

}

void GetActiveMatchIndexExecuted(object target, ExecutedRoutedEventArgs e)
{
#if USE_WEBVIEW2_EXPERIMENTAL

if (_iWebView2?.CoreWebView2?.Find == null) return;

int activeIndex = _iWebView2.CoreWebView2.Find.ActiveMatchIndex;
MessageBox.Show($"Active Match Index: {activeIndex}", "Find Operation", MessageBoxButton.OK);
#endif

}

void ToggleCaseSensitiveExecuted(object target, ExecutedRoutedEventArgs e)
{
#if USE_WEBVIEW2_EXPERIMENTAL

ToggleFindOptionAndRestart(
() => _findOptions.IsCaseSensitive,
val => _findOptions.IsCaseSensitive = val);
#endif

}

void ToggleShouldHighlightAllMatchesExecuted(object target, ExecutedRoutedEventArgs e)
{
#if USE_WEBVIEW2_EXPERIMENTAL

ToggleFindOptionAndRestart(
() => _findOptions.ShouldHighlightAllMatches,
val => _findOptions.ShouldHighlightAllMatches = val);
#endif

}

void ToggleShouldMatchWordExecuted(object target, ExecutedRoutedEventArgs e)
{
#if USE_WEBVIEW2_EXPERIMENTAL

ToggleFindOptionAndRestart(
() => _findOptions.ShouldMatchWord,
val => _findOptions.ShouldMatchWord = val);
#endif

}

void ToggleSuppressDefaultFindDialogExecuted(object target, ExecutedRoutedEventArgs e)
{
#if USE_WEBVIEW2_EXPERIMENTAL

ToggleFindOptionAndRestart(
() => _findOptions.SuppressDefaultFindDialog,
val => _findOptions.SuppressDefaultFindDialog = val);
#endif

}
#if USE_WEBVIEW2_EXPERIMENTAL

private async void ToggleFindOptionAndRestart(Func<bool> optionGetter, Action<bool> optionSetter)
{
if (_iWebView2?.CoreWebView2?.Find == null)
return;

_iWebView2.CoreWebView2.Find.Stop();


if (_findOptions == null)
{
_findOptions = CreateDefaultFindOptions();
}

bool currentVal = optionGetter();
optionSetter(!currentVal);

await _iWebView2.CoreWebView2.Find.StartAsync(_findOptions);
}

private void SetupFindEventHandlers()
{
if (_findEventHandlersSet) return; // Only attach once
var findObject = _iWebView2.CoreWebView2.Find;
findObject.MatchCountChanged += FindObject_MatchCountChanged;
findObject.ActiveMatchIndexChanged += FindObject_ActiveMatchIndexChanged;
_findEventHandlersSet = true;
}

private void RemoveFindEventHandlers()
{
if (!_findEventHandlersSet) return;
var findObject = _iWebView2.CoreWebView2.Find;
findObject.MatchCountChanged -= FindObject_MatchCountChanged;
findObject.ActiveMatchIndexChanged -= FindObject_ActiveMatchIndexChanged;
_findEventHandlersSet = false;
}

private void FindObject_MatchCountChanged(object sender, object e)
{
var findObject = _iWebView2.CoreWebView2.Find;
int matchCount = findObject.MatchCount;
Debug.WriteLine($"[FindOnPage] MatchCountChanged -> {matchCount}");
}

private void FindObject_ActiveMatchIndexChanged(object sender, object e)
{
var findObject = _iWebView2.CoreWebView2.Find;
int activeIndex = findObject.ActiveMatchIndex;
Debug.WriteLine($"[FindOnPage] ActiveMatchIndexChanged -> {activeIndex}");
}
#endif

}
}
2 changes: 1 addition & 1 deletion SampleApps/WebView2WpfBrowser/WebView2WpfBrowser.csproj
Original file line number Diff line number Diff line change
@@ -61,7 +61,7 @@
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.3079-prerelease" />
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.3116-prerelease" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>
</Project>