Skip to content
Draft
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
53 changes: 53 additions & 0 deletions src/Avalonia.Controls.WebView/NativeWebView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public class NativeWebView : Control, Core.IWebView, Core.IWebViewHolder
{
private bool _ignoreNavigation;
private bool _ignoreFocusChanges;
#if AVALONIA
private WindowBase? _activationSource;
#endif
private object? _lastSource;

private EventHandler<Core.WebViewNavigationCompletedEventArgs>? _navigationCompleted;
Expand Down Expand Up @@ -446,6 +449,7 @@ private void WebViewAdapterOnNewWindowRequested(object? sender, Core.WebViewNewW
protected override async void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnAttachedToVisualTree(e);
TrackTopLevelActivation();
#elif WPF
protected override async void OnVisualParentChanged(DependencyObject oldParent)
{
Expand Down Expand Up @@ -493,6 +497,55 @@ private async void OnAttached()
#endif
}

#if AVALONIA
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
{
StopTrackingTopLevelActivation();
base.OnDetachedFromVisualTree(e);
}

// A GTK toplevel that is offscreen, or reparented into someone else's window, never hears from the window
// manager, so the focus this control synthesized for it outlives the user switching to another application:
// the caret goes on blinking and the page still reports document.hasFocus(). Avalonia leaves focus on the
// control while the window is deactivated, so window activation is the only thing left to follow.
private void TrackTopLevelActivation()
{
var window = TopLevel.GetTopLevel(this) as WindowBase;
if (ReferenceEquals(window, _activationSource))
return;

StopTrackingTopLevelActivation();
if (window is null)
return;

_activationSource = window;
window.Activated += OnTopLevelActivated;
window.Deactivated += OnTopLevelDeactivated;
}

private void StopTrackingTopLevelActivation()
{
if (_activationSource is null)
return;

_activationSource.Activated -= OnTopLevelActivated;
_activationSource.Deactivated -= OnTopLevelDeactivated;
_activationSource = null;
}

private void OnTopLevelActivated(object? sender, EventArgs e)
{
if (IsFocused && TryGetAdapter() is Core.IWebViewAdapterWithFocus withFocus)
withFocus.Focus();
}

private void OnTopLevelDeactivated(object? sender, EventArgs e)
{
if (IsFocused && TryGetAdapter() is Core.IWebViewAdapterWithFocus withFocus)
withFocus.ResignFocus();
}
#endif

private void WithFocusOnGotFocus(object? sender, EventArgs e)
{
Core.WebViewDispatcher.VerifyAccess();
Expand Down
Loading