Fix for webview2 not focusing properly issue.

This commit is contained in:
Burak Kaan Köse
2024-08-23 03:00:22 +02:00
parent ef4689619e
commit c1973023d0

View File

@@ -21,6 +21,7 @@ using Windows.Storage.Pickers;
using Windows.UI.ViewManagement.Core; using Windows.UI.ViewManagement.Core;
using Windows.UI.Xaml; using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media.Animation; using Windows.UI.Xaml.Media.Animation;
using Windows.UI.Xaml.Navigation; using Windows.UI.Xaml.Navigation;
using Wino.Core.Domain; using Wino.Core.Domain;
@@ -62,6 +63,19 @@ namespace Wino.Views
Environment.SetEnvironmentVariable("WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS", "--enable-features=OverlayScrollbar,msOverlayScrollbarWinStyle,msOverlayScrollbarWinStyleAnimation"); Environment.SetEnvironmentVariable("WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS", "--enable-features=OverlayScrollbar,msOverlayScrollbarWinStyle,msOverlayScrollbarWinStyleAnimation");
} }
private async void GlobalFocusManagerGotFocus(object sender, FocusManagerGotFocusEventArgs e)
{
// In order to delegate cursor to the inner editor for WebView2.
// When the control got focus, we invoke script to focus the editor.
// This is not done on the WebView2 handlers, because somehow it is
// repeatedly focusing itself, even though when it has the focus already.
if (e.NewFocusedElement == Chromium)
{
await FocusEditorAsync(false);
}
}
private static async void OnIsComposerDarkModeChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) private static async void OnIsComposerDarkModeChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{ {
if (obj is ComposePage page) if (obj is ComposePage page)
@@ -356,19 +370,26 @@ namespace Wino.Views
await InvokeScriptSafeAsync("imageInput.click();"); await InvokeScriptSafeAsync("imageInput.click();");
} }
private async Task FocusEditorAsync() /// <summary>
/// Places the cursor in the composer.
/// </summary>
/// <param name="focusControlAsWell">Whether control itself should be focused as well or not.</param>
private async Task FocusEditorAsync(bool focusControlAsWell)
{ {
await InvokeScriptSafeAsync("editor.selection.focus();"); await InvokeScriptSafeAsync("editor.selection.focus();");
Chromium.Focus(FocusState.Keyboard); if (focusControlAsWell)
Chromium.Focus(FocusState.Programmatic); {
Chromium.Focus(FocusState.Keyboard);
Chromium.Focus(FocusState.Programmatic);
}
} }
private async void EmojiButtonClicked(object sender, RoutedEventArgs e) private async void EmojiButtonClicked(object sender, RoutedEventArgs e)
{ {
CoreInputView.GetForCurrentView().TryShow(CoreInputViewKind.Emoji); CoreInputView.GetForCurrentView().TryShow(CoreInputViewKind.Emoji);
await FocusEditorAsync(); await FocusEditorAsync(focusControlAsWell: true);
} }
public async Task UpdateEditorThemeAsync() public async Task UpdateEditorThemeAsync()
@@ -440,6 +461,8 @@ namespace Wino.Views
{ {
base.OnNavigatedTo(e); base.OnNavigatedTo(e);
FocusManager.GotFocus += GlobalFocusManagerGotFocus;
var anim = ConnectedAnimationService.GetForCurrentView().GetAnimation("WebViewConnectedAnimation"); var anim = ConnectedAnimationService.GetForCurrentView().GetAnimation("WebViewConnectedAnimation");
anim?.TryStart(Chromium); anim?.TryStart(Chromium);
@@ -694,6 +717,7 @@ namespace Wino.Views
{ {
base.OnNavigatingFrom(e); base.OnNavigatingFrom(e);
FocusManager.GotFocus -= GlobalFocusManagerGotFocus;
await ViewModel.UpdateMimeChangesAsync(); await ViewModel.UpdateMimeChangesAsync();
DisposeDisposables(); DisposeDisposables();