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

BlazorWebView WPF tab navigation improvements #7742

Merged
merged 1 commit into from
Jun 7, 2022
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
24 changes: 24 additions & 0 deletions src/BlazorWebView/src/Wpf/BlazorWebView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.AspNetCore.Components.WebView.WebView2;
using Microsoft.Extensions.FileProviders;
using WebView2Control = Microsoft.Web.WebView2.Wpf.WebView2;
Expand Down Expand Up @@ -79,6 +80,17 @@ public class BlazorWebView : Control, IAsyncDisposable
private WebView2WebViewManager? _webviewManager;
private bool _isDisposed;

static BlazorWebView()
{
// By default, prevent the BlazorWebView from receiving focus. Focus should typically be directed
// to the underlying WebView2 control.
FocusableProperty.OverrideMetadata(typeof(BlazorWebView), new FrameworkPropertyMetadata(false));

// Listen for changes to the IsTabStop property so we can manipulate how tab navigation affects
// the BlazorWebView's subtree.
IsTabStopProperty.OverrideMetadata(typeof(BlazorWebView), new FrameworkPropertyMetadata(OnIsTabStopPropertyChanged));
}

/// <summary>
/// Creates a new instance of <see cref="BlazorWebView"/>.
/// </summary>
Expand All @@ -93,6 +105,8 @@ public BlazorWebView()
{
VisualTree = new FrameworkElementFactory(typeof(WebView2Control), WebViewTemplateChildName)
};

ApplyTabNavigation(IsTabStop);
}

/// <summary>
Expand Down Expand Up @@ -168,6 +182,16 @@ public IServiceProvider Services

private void OnHostPagePropertyChanged(DependencyPropertyChangedEventArgs e) => StartWebViewCoreIfPossible();

private static void OnIsTabStopPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => ((BlazorWebView)d).OnIsTabStopPropertyChanged(e);

private void OnIsTabStopPropertyChanged(DependencyPropertyChangedEventArgs e) => ApplyTabNavigation((bool)e.NewValue);

private void ApplyTabNavigation(bool isTabStop)
{
var keyboardNavigationMode = isTabStop ? KeyboardNavigationMode.Local : KeyboardNavigationMode.None;
KeyboardNavigation.SetTabNavigation(this, keyboardNavigationMode);
}

private bool RequiredStartupPropertiesSet =>
_webview != null &&
HostPage != null &&
Expand Down