Browse Source

Fix selection when moving via tab and caret position when clicking on textbox

Equbuxu 3 years ago
parent
commit
7c126eb010
1 changed files with 11 additions and 16 deletions
  1. 11 16
      PixiEditor/Views/UserControls/SizeInput.xaml.cs

+ 11 - 16
PixiEditor/Views/UserControls/SizeInput.xaml.cs

@@ -71,27 +71,13 @@ namespace PixiEditor.Views
 
         public SizeInput()
         {
-            GotKeyboardFocus += SizeInput_GotKeyboardFocus;
             InitializeComponent();
         }
 
         public void FocusAndSelect()
-        {
-            Focus();
-            textBox.SelectAll();
-        }
-
-        private void SizeInput_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
         {
             textBox.Focus();
-            Point pos = Mouse.GetPosition(textBox);
-            int charIndex = textBox.GetCharacterIndexFromPoint(pos, true);
-            var charRect = textBox.GetRectFromCharacterIndex(charIndex);
-            double middleX = (charRect.Left + charRect.Right) / 2;
-            if (pos.X > middleX)
-                textBox.CaretIndex = charIndex + 1;
-            else
-                textBox.CaretIndex = charIndex;
+            textBox.SelectAll();
         }
 
         private static void InputSizeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
@@ -144,7 +130,16 @@ namespace PixiEditor.Views
 
         private void Border_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
         {
-            textBox.Focus();
+            if (!textBox.IsFocused)
+                textBox.Focus();
+            Point pos = Mouse.GetPosition(textBox);
+            int charIndex = textBox.GetCharacterIndexFromPoint(pos, true);
+            var charRect = textBox.GetRectFromCharacterIndex(charIndex);
+            double middleX = (charRect.Left + charRect.Right) / 2;
+            if (pos.X > middleX)
+                textBox.CaretIndex = charIndex + 1;
+            else
+                textBox.CaretIndex = charIndex;
             e.Handled = true;
         }