Skip to content

A Swiss Army knife for developers.

Fixed monaco editor focus

Etienne Baudoux563fde42f1e1
diff --git a/src/Directory.Packages.props b/src/Directory.Packages.props
index 7655ffe3c..95ec791a2 100644
--- a/src/Directory.Packages.props
+++ b/src/Directory.Packages.props
@@ -11,7 +11,7 @@
<PolySharpVersion>1.8.1</PolySharpVersion>
<UnoVersion>4.6.19</UnoVersion>
<UnoWasmBootstrapVersion>8.0.0-dev.65</UnoWasmBootstrapVersion>
- <WebView2Version>1.0.1264.42</WebView2Version>
+ <WebView2Version>1.0.1462.37</WebView2Version>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="CommunityToolkit.Common" Version="$(CommunityToolkitVersion)" />
diff --git a/src/app/dev/DevToys.MonacoEditor/CodeEditor/CodeEditor.cs b/src/app/dev/DevToys.MonacoEditor/CodeEditor/CodeEditor.cs
index 38e2381df..74e3ffc79 100644
--- a/src/app/dev/DevToys.MonacoEditor/CodeEditor/CodeEditor.cs
+++ b/src/app/dev/DevToys.MonacoEditor/CodeEditor/CodeEditor.cs
@@ -52,6 +52,7 @@ public sealed partial class CodeEditor : Control, IParentAccessorAcceptor, IDisp
private readonly long _themeToken;
private ICodeEditorPresenter? _view;
+ private int _focusCount;
private bool _initialized;
private ModelHelper? _model;
@@ -501,8 +502,6 @@ public sealed partial class CodeEditor : Control, IParentAccessorAcceptor, IDisp
_view.NavigationCompleted -= WebView_NavigationCompleted;
_view.NewWindowRequested -= WebView_NewWindowRequested;
_view.DotNetObjectInjectionRequested -= WebView_DotNetObjectInjectionRequested;
- _view.GotFocus -= WebView_GotFocus;
- _view.LostFocus -= WebView_LostFocus;
Debug.WriteLine("Setting initialized - false");
_initialized = false;
}
@@ -517,8 +516,6 @@ public sealed partial class CodeEditor : Control, IParentAccessorAcceptor, IDisp
_view.NavigationCompleted += WebView_NavigationCompleted;
_view.NewWindowRequested += WebView_NewWindowRequested;
_view.DotNetObjectInjectionRequested += WebView_DotNetObjectInjectionRequested;
- _view.GotFocus += WebView_GotFocus;
- _view.LostFocus += WebView_LostFocus;
_view.LaunchAsync().Forget();
@@ -664,24 +661,22 @@ public sealed partial class CodeEditor : Control, IParentAccessorAcceptor, IDisp
OpenLinkRequested?.Invoke(this, args);
}
- private void WebView_GotFocus(ICodeEditorPresenter sender, RoutedEventArgs args)
- {
- OnMonacoEditorGotFocus();
- }
-
- private void WebView_LostFocus(ICodeEditorPresenter sender, RoutedEventArgs args)
- {
- OnMonacoEditorLostFocus();
- }
-
private void OnMonacoEditorGotFocus()
{
- VisualStateManager.GoToState(this, FocusedState, false);
+ _focusCount++;
+ if (_focusCount > 0)
+ {
+ VisualStateManager.GoToState(this, FocusedState, false);
+ }
}
private void OnMonacoEditorLostFocus()
{
- VisualStateManager.GoToState(this, NormalState, false);
+ _focusCount = Math.Max(0, _focusCount - 1);
+ if (_focusCount == 0)
+ {
+ VisualStateManager.GoToState(this, NormalState, false);
+ }
}
private void OnMonacoEditorLoaded()
@@ -709,9 +704,6 @@ public sealed partial class CodeEditor : Control, IParentAccessorAcceptor, IDisp
// We're done loading Monaco Editor.
IsEditorLoaded = true;
EditorLoaded?.Invoke(this, new RoutedEventArgs());
-
- // Give the focus if necessary.
- GiveFocusToInnerEditor();
}
private void GiveFocusToInnerEditor()
diff --git a/src/app/dev/DevToys.MonacoEditor/CodeEditor/CodeEditorPresenter.Windows.cs b/src/app/dev/DevToys.MonacoEditor/CodeEditor/CodeEditorPresenter.Windows.cs
index f00ead3fd..215f29eac 100644
--- a/src/app/dev/DevToys.MonacoEditor/CodeEditor/CodeEditorPresenter.Windows.cs
+++ b/src/app/dev/DevToys.MonacoEditor/CodeEditor/CodeEditorPresenter.Windows.cs
@@ -65,11 +65,10 @@ public sealed partial class CodeEditorPresenter : UserControl, ICodeEditorPresen
Content = _webView;
- _webView.Visibility = Visibility.Collapsed;
- //_webView.DefaultBackgroundColor = Colors.Transparent;
+ this.Visibility = Visibility.Collapsed;
+ _webView.AllowFocusOnInteraction = true;
+ _webView.IsFocusEngagementEnabled = true;
_webView.CoreWebView2Initialized += WebView_CoreWebView2Initialized;
- _webView.GotFocus += WebView_GotFocus;
- _webView.LostFocus += WebView_LostFocus;
ILogger logger = this.Log();
_debugLogger = logger.IsEnabled(LogLevel.Debug) ? logger : null;
@@ -139,7 +138,7 @@ public sealed partial class CodeEditorPresenter : UserControl, ICodeEditorPresen
_errorLogger?.Error($"{nameof(LaunchAsync)} failed", e);
}
- _webView.Visibility = Visibility.Visible;
+ this.Visibility = Visibility.Visible;
void OnDOMContentLoaded(CoreWebView2 sender, CoreWebView2DOMContentLoadedEventArgs args)
{
@@ -394,16 +393,6 @@ public sealed partial class CodeEditorPresenter : UserControl, ICodeEditorPresen
_webView.CoreWebView2.DOMContentLoaded += (wv, args) => DOMContentLoaded?.Invoke(this, args);
_webView.CoreWebView2.NavigationCompleted += (wv, args) => NavigationCompleted?.Invoke(this, args);
}
-
- private void WebView_LostFocus(object sender, RoutedEventArgs e)
- {
- LostFocus?.Invoke(this, e);
- }
-
- private void WebView_GotFocus(object sender, RoutedEventArgs e)
- {
- GotFocus?.Invoke(this, e);
- }
}
#endif
diff --git a/src/app/dev/DevToys.MonacoEditor/Themes/generic.xaml b/src/app/dev/DevToys.MonacoEditor/Themes/generic.xaml
index 38020026a..476b7ac95 100644
--- a/src/app/dev/DevToys.MonacoEditor/Themes/generic.xaml
+++ b/src/app/dev/DevToys.MonacoEditor/Themes/generic.xaml
@@ -54,15 +54,13 @@
BorderBrush="{ThemeResource TextControlBorderBrush}"
CornerRadius="4"
HorizontalAlignment="Stretch"
- VerticalAlignment="Stretch"
- IsHitTestVisible="False">
+ VerticalAlignment="Stretch">
<Grid>
- <Button Content="Hello world"/>
<monaco:CodeEditorPresenter
- x:Name="View"
- Margin="{TemplateBinding Padding}"
- HorizontalAlignment="Stretch"
- VerticalAlignment="Stretch"/>
+ x:Name="View"
+ Margin="{TemplateBinding Padding}"
+ HorizontalAlignment="Stretch"
+ VerticalAlignment="Stretch"/>
</Grid>
</Border>
<muxc:ProgressRing