From 58d1a4e41f669998371edcbc94e1efc595cea482 Mon Sep 17 00:00:00 2001 From: Oleg Tkachenko Date: Mon, 30 Sep 2024 08:43:25 -0700 Subject: [PATCH 1/2] don't mess with user caret in smart rename --- .../SmartRename/SmartRenameUserInputComboBox.xaml.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/EditorFeatures/Core.Wpf/InlineRename/UI/SmartRename/SmartRenameUserInputComboBox.xaml.cs b/src/EditorFeatures/Core.Wpf/InlineRename/UI/SmartRename/SmartRenameUserInputComboBox.xaml.cs index e387f28946df5..d0673e68bc269 100644 --- a/src/EditorFeatures/Core.Wpf/InlineRename/UI/SmartRename/SmartRenameUserInputComboBox.xaml.cs +++ b/src/EditorFeatures/Core.Wpf/InlineRename/UI/SmartRename/SmartRenameUserInputComboBox.xaml.cs @@ -6,7 +6,6 @@ using System.Collections.Specialized; using System.Windows; using System.Windows.Controls; -using System.Windows.Controls.Primitives; using System.Windows.Input; using Microsoft.CodeAnalysis.Editor.Implementation.InlineRename; @@ -23,6 +22,7 @@ internal sealed partial class SmartRenameUserInputComboBox : ComboBox, IRenameUs private readonly SmartRenameViewModel _smartRenameViewModel; private readonly RenameFlyoutViewModel _baseViewModel; private readonly Lazy _innerTextBox; + private bool _userChangedTextSelection; internal SmartRenameUserInputComboBox(RenameFlyoutViewModel viewModel) { @@ -36,7 +36,9 @@ internal SmartRenameUserInputComboBox(RenameFlyoutViewModel viewModel) _innerTextBox = new Lazy(() => { ApplyTemplate(); - return (TextBox)GetTemplateChild(InnerTextBox)!; + var textBox = (TextBox)GetTemplateChild(InnerTextBox)!; + textBox.SelectionChanged += (sender, e) => _userChangedTextSelection = true; + return textBox; }); _smartRenameViewModel.SuggestedNames.CollectionChanged += SuggestedNames_CollectionChanged; @@ -125,6 +127,9 @@ private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs private void SuggestedNames_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { - Focus(); + if (!_userChangedTextSelection) + { + Focus(); + } } } From acec6ee649a15ac2cee0d9285aade2d7bce3c38c Mon Sep 17 00:00:00 2001 From: Oleg Tkachenko Date: Mon, 30 Sep 2024 09:11:07 -0700 Subject: [PATCH 2/2] add comment --- .../UI/SmartRename/SmartRenameUserInputComboBox.xaml.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/EditorFeatures/Core.Wpf/InlineRename/UI/SmartRename/SmartRenameUserInputComboBox.xaml.cs b/src/EditorFeatures/Core.Wpf/InlineRename/UI/SmartRename/SmartRenameUserInputComboBox.xaml.cs index d0673e68bc269..3fcb2b5726837 100644 --- a/src/EditorFeatures/Core.Wpf/InlineRename/UI/SmartRename/SmartRenameUserInputComboBox.xaml.cs +++ b/src/EditorFeatures/Core.Wpf/InlineRename/UI/SmartRename/SmartRenameUserInputComboBox.xaml.cs @@ -127,6 +127,9 @@ private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs private void SuggestedNames_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { + // RenameFlyout handles GotFocus event by calling SelectAllText, which selects all text in the input text box. + // If user changed selection (e.g. by moving the caret or selecting text in the input text box we don't want + // to step on it once rename suggestions became available. if (!_userChangedTextSelection) { Focus();