From f7c36b0d4c339a6162b0d96290ec0e90ba3c0f6e Mon Sep 17 00:00:00 2001 From: Benedikt Stebner Date: Thu, 1 Jun 2023 07:06:47 +0200 Subject: [PATCH 1/3] Update to Avalonia 11 RC --- Directory.Build.props | 2 +- src/AvaloniaEdit.Demo/MainWindow.xaml.cs | 2 +- .../CodeCompletion/CompletionWindow.cs | 2 +- .../CodeCompletion/CompletionWindowBase.cs | 4 ++-- .../CodeCompletion/ICompletionData.cs | 2 +- .../Editing/CaretNavigationCommandHandler.cs | 2 +- src/AvaloniaEdit/RoutedCommand.cs | 23 +++++++++++++++++-- .../AvaloniaEdit.Tests.csproj | 8 +++---- 8 files changed, 32 insertions(+), 13 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index 6f2ded36..c21a30ab 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -2,7 +2,7 @@ latest true - 11.0.0-preview8 + 11.0.0-rc1.1 1.0.55 beta diff --git a/src/AvaloniaEdit.Demo/MainWindow.xaml.cs b/src/AvaloniaEdit.Demo/MainWindow.xaml.cs index 01ce49bc..47cb6cc8 100644 --- a/src/AvaloniaEdit.Demo/MainWindow.xaml.cs +++ b/src/AvaloniaEdit.Demo/MainWindow.xaml.cs @@ -347,7 +347,7 @@ public MyCompletionData(string text) Text = text; } - public IBitmap Image => null; + public Bitmap Image => null; public string Text { get; } diff --git a/src/AvaloniaEdit/CodeCompletion/CompletionWindow.cs b/src/AvaloniaEdit/CodeCompletion/CompletionWindow.cs index 139aebdb..e2d71c8b 100644 --- a/src/AvaloniaEdit/CodeCompletion/CompletionWindow.cs +++ b/src/AvaloniaEdit/CodeCompletion/CompletionWindow.cs @@ -174,7 +174,7 @@ protected override void OnKeyDown(KeyEventArgs e) private void TextArea_PreviewTextInput(object sender, TextInputEventArgs e) { e.Handled = RaiseEventPair(this, null, TextInputEvent, - new TextInputEventArgs { Device = e.Device, Text = e.Text }); + new TextInputEventArgs { Text = e.Text }); } private void TextArea_MouseWheel(object sender, PointerWheelEventArgs e) diff --git a/src/AvaloniaEdit/CodeCompletion/CompletionWindowBase.cs b/src/AvaloniaEdit/CodeCompletion/CompletionWindowBase.cs index ea7725f6..7f6ff600 100644 --- a/src/AvaloniaEdit/CodeCompletion/CompletionWindowBase.cs +++ b/src/AvaloniaEdit/CodeCompletion/CompletionWindowBase.cs @@ -218,7 +218,7 @@ public override void OnPreviewKeyDown(KeyEventArgs e) if (e.Key == Key.DeadCharProcessed) return; e.Handled = RaiseEventPair(Window, null, KeyDownEvent, - new KeyEventArgs { Device = e.Device, Key = e.Key }); + new KeyEventArgs { Key = e.Key }); } public override void OnPreviewKeyUp(KeyEventArgs e) @@ -226,7 +226,7 @@ public override void OnPreviewKeyUp(KeyEventArgs e) if (e.Key == Key.DeadCharProcessed) return; e.Handled = RaiseEventPair(Window, null, KeyUpEvent, - new KeyEventArgs { Device = e.Device, Key = e.Key }); + new KeyEventArgs { Key = e.Key }); } } #endregion diff --git a/src/AvaloniaEdit/CodeCompletion/ICompletionData.cs b/src/AvaloniaEdit/CodeCompletion/ICompletionData.cs index 3fc3e15f..a0147b43 100644 --- a/src/AvaloniaEdit/CodeCompletion/ICompletionData.cs +++ b/src/AvaloniaEdit/CodeCompletion/ICompletionData.cs @@ -35,7 +35,7 @@ public interface ICompletionData /// /// Gets the image. /// - IBitmap Image { get; } + Bitmap Image { get; } /// /// Gets the text. This property is used to filter the list of visible elements. diff --git a/src/AvaloniaEdit/Editing/CaretNavigationCommandHandler.cs b/src/AvaloniaEdit/Editing/CaretNavigationCommandHandler.cs index 2ea16d72..dc7c3710 100644 --- a/src/AvaloniaEdit/Editing/CaretNavigationCommandHandler.cs +++ b/src/AvaloniaEdit/Editing/CaretNavigationCommandHandler.cs @@ -82,7 +82,7 @@ private static void AddBinding(RoutedCommand command, KeyGesture gesture, EventH static CaretNavigationCommandHandler() { - var keymap = AvaloniaLocator.Current.GetService(); + var keymap = Application.Current.PlatformSettings.HotkeyConfiguration; AddBinding(EditingCommands.MoveLeftByCharacter, KeyModifiers.None, Key.Left, OnMoveCaret(CaretMovementType.CharLeft)); AddBinding(EditingCommands.SelectLeftByCharacter, keymap.SelectionModifiers, Key.Left, OnMoveCaretExtendSelection(CaretMovementType.CharLeft)); diff --git a/src/AvaloniaEdit/RoutedCommand.cs b/src/AvaloniaEdit/RoutedCommand.cs index a570115c..5b33fddb 100644 --- a/src/AvaloniaEdit/RoutedCommand.cs +++ b/src/AvaloniaEdit/RoutedCommand.cs @@ -3,6 +3,8 @@ using System.Linq; using System.Windows.Input; using Avalonia; +using Avalonia.Controls; +using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Input; using Avalonia.Interactivity; @@ -60,7 +62,7 @@ public bool CanExecute(object parameter, IInputElement target) bool ICommand.CanExecute(object parameter) { - return CanExecute(parameter, Application.Current.FocusManager.Current); + return CanExecute(parameter, GetGloballyFocusedElement()); } public static RoutedEvent ExecutedEvent { get; } = RoutedEvent.Register(nameof(ExecutedEvent), RoutingStrategies.Bubble, typeof(RoutedCommand)); @@ -75,7 +77,7 @@ public void Execute(object parameter, IInputElement target) void ICommand.Execute(object parameter) { - Execute(parameter, Application.Current.FocusManager.Current); + Execute(parameter, GetGloballyFocusedElement()); } // TODO @@ -84,6 +86,23 @@ event EventHandler ICommand.CanExecuteChanged add { } remove { } } + + IInputElement GetGloballyFocusedElement() + { + TopLevel topLevel = null; + + if(Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime lifetime) + { + topLevel = lifetime.MainWindow; + } + + if(Application.Current.ApplicationLifetime is ISingleViewApplicationLifetime singleView) + { + topLevel = TopLevel.GetTopLevel(singleView.MainView); + } + + return topLevel?.FocusManager?.GetFocusedElement(); + } } public interface IRoutedCommandBindable diff --git a/test/AvaloniaEdit.Tests/AvaloniaEdit.Tests.csproj b/test/AvaloniaEdit.Tests/AvaloniaEdit.Tests.csproj index b7b4234e..c1902d7c 100644 --- a/test/AvaloniaEdit.Tests/AvaloniaEdit.Tests.csproj +++ b/test/AvaloniaEdit.Tests/AvaloniaEdit.Tests.csproj @@ -5,10 +5,10 @@ - - - - + + + + From 93c5ea771ff514002f362b763bafa18f27d55494 Mon Sep 17 00:00:00 2001 From: Benedikt Stebner Date: Thu, 1 Jun 2023 07:46:14 +0200 Subject: [PATCH 2/3] Use InputElement.GotFocusEvent class handler --- src/AvaloniaEdit/RoutedCommand.cs | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/src/AvaloniaEdit/RoutedCommand.cs b/src/AvaloniaEdit/RoutedCommand.cs index 5b33fddb..b472ce1c 100644 --- a/src/AvaloniaEdit/RoutedCommand.cs +++ b/src/AvaloniaEdit/RoutedCommand.cs @@ -12,6 +12,8 @@ namespace AvaloniaEdit { public class RoutedCommand : ICommand { + private static IInputElement _inputElement; + public string Name { get; } public KeyGesture Gesture { get; } @@ -25,6 +27,7 @@ static RoutedCommand() { CanExecuteEvent.AddClassHandler(CanExecuteEventHandler); ExecutedEvent.AddClassHandler(ExecutedEventHandler); + InputElement.GotFocusEvent.AddClassHandler(GotFocusEventHandler); } private static void CanExecuteEventHandler(Interactive control, CanExecuteRoutedEventArgs args) @@ -48,6 +51,11 @@ private static void ExecutedEventHandler(Interactive control, ExecutedRoutedEven } } + private static void GotFocusEventHandler(Interactive control, GotFocusEventArgs args) + { + _inputElement = control as IInputElement; + } + public static RoutedEvent CanExecuteEvent { get; } = RoutedEvent.Register(nameof(CanExecuteEvent), RoutingStrategies.Bubble, typeof(RoutedCommand)); public bool CanExecute(object parameter, IInputElement target) @@ -62,7 +70,7 @@ public bool CanExecute(object parameter, IInputElement target) bool ICommand.CanExecute(object parameter) { - return CanExecute(parameter, GetGloballyFocusedElement()); + return CanExecute(parameter, _inputElement); } public static RoutedEvent ExecutedEvent { get; } = RoutedEvent.Register(nameof(ExecutedEvent), RoutingStrategies.Bubble, typeof(RoutedCommand)); @@ -77,7 +85,7 @@ public void Execute(object parameter, IInputElement target) void ICommand.Execute(object parameter) { - Execute(parameter, GetGloballyFocusedElement()); + Execute(parameter, _inputElement); } // TODO @@ -86,23 +94,6 @@ event EventHandler ICommand.CanExecuteChanged add { } remove { } } - - IInputElement GetGloballyFocusedElement() - { - TopLevel topLevel = null; - - if(Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime lifetime) - { - topLevel = lifetime.MainWindow; - } - - if(Application.Current.ApplicationLifetime is ISingleViewApplicationLifetime singleView) - { - topLevel = TopLevel.GetTopLevel(singleView.MainView); - } - - return topLevel?.FocusManager?.GetFocusedElement(); - } } public interface IRoutedCommandBindable From 01257d1fef237ce09b2c29e8f312e0a652e5a188 Mon Sep 17 00:00:00 2001 From: Benedikt Stebner Date: Thu, 1 Jun 2023 07:47:14 +0200 Subject: [PATCH 3/3] Remove redudant usings --- src/AvaloniaEdit/RoutedCommand.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/AvaloniaEdit/RoutedCommand.cs b/src/AvaloniaEdit/RoutedCommand.cs index b472ce1c..daa24f88 100644 --- a/src/AvaloniaEdit/RoutedCommand.cs +++ b/src/AvaloniaEdit/RoutedCommand.cs @@ -2,9 +2,6 @@ using System.Collections.Generic; using System.Linq; using System.Windows.Input; -using Avalonia; -using Avalonia.Controls; -using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Input; using Avalonia.Interactivity;