Prechádzať zdrojové kódy

Fix obscure bug where the shortcut box was getting triggered on its own

Equbuxu 2 rokov pred
rodič
commit
e018f171ec

+ 1 - 8
src/PixiEditor/Helpers/Behaviours/TextBoxFocusBehavior.cs

@@ -87,14 +87,7 @@ internal class TextBoxFocusBehavior : Behavior<TextBox>
     {
         if (!FocusNext)
         {
-            FrameworkElement parent = (FrameworkElement)AssociatedObject.Parent;
-            while (parent is IInputElement elem && !elem.Focusable)
-            {
-                parent = (FrameworkElement)parent.Parent;
-            }
-
-            DependencyObject scope = FocusManager.GetFocusScope(AssociatedObject);
-            FocusManager.SetFocusedElement(scope, parent);
+            FocusHelper.MoveFocusToParent(AssociatedObject);
         }
         else
         {

+ 26 - 0
src/PixiEditor/Helpers/FocusHelper.cs

@@ -0,0 +1,26 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Input;
+using System.Windows.Media;
+
+namespace PixiEditor.Helpers;
+
+internal static class FocusHelper
+{
+    public static void MoveFocusToParent(FrameworkElement element)
+    {
+        FrameworkElement parent = (FrameworkElement)VisualTreeHelper.GetParent(element);
+
+        while (parent is IInputElement elem && !elem.Focusable)
+        {
+            parent = (FrameworkElement)VisualTreeHelper.GetParent(parent);
+        }
+
+        DependencyObject scope = FocusManager.GetFocusScope(element);
+        FocusManager.SetFocusedElement(scope, parent);
+    }
+}

+ 4 - 4
src/PixiEditor/Views/Dialogs/SettingGroups/ShortcutsBinder.xaml

@@ -8,7 +8,7 @@
              xmlns:userControls="clr-namespace:PixiEditor.Views.UserControls"
              mc:Ignorable="d"
              d:DesignHeight="600" d:DesignWidth="400">
-    <ScrollViewer x:Name="commandScroll">
+    <ScrollViewer x:Name="commandScroll" FocusVisualStyle="{x:Null}">
                     <ScrollViewer.Template>
                         <ControlTemplate TargetType="{x:Type ScrollViewer}">
                             <Grid x:Name="Grid" Background="{TemplateBinding Background}">
@@ -31,17 +31,17 @@
                         <TextBlock Foreground="LightGray" HorizontalAlignment="Center" TextAlignment="Center"
                                    Visibility="{Binding VisibleGroups, ConverterParameter=0, Mode=OneWay, Converter={converters:EqualityBoolToVisibilityConverter}}"
                                    Text="Nothing found"/>
-                        <ItemsControl ItemsSource="{Binding Commands}" Foreground="White">
+                        <ItemsControl ItemsSource="{Binding Commands}" Foreground="White" Focusable="False">
                             <ItemsControl.ItemTemplate>
                                 <DataTemplate>
                                     <StackPanel Margin="0,0,0,20" Visibility="{Binding Visibility}">
                                         <TextBlock Text="{Binding DisplayName}" FontSize="22" FontWeight="SemiBold"/>
-                                        <ItemsControl ItemsSource="{Binding Commands}">
+                                        <ItemsControl ItemsSource="{Binding Commands}" Focusable="False">
                                             <ItemsControl.ItemTemplate>
                                                 <DataTemplate>
                                                     <Grid Margin="0,5,5,0" Visibility="{Binding Visibility}">
                                                         <TextBlock Text="{Binding Command.DisplayName}" ToolTip="{Binding Command.Description}"/>
-                                                        <userControls:ShortcutBox Width="120" Command="{Binding Command}" HorizontalAlignment="Right"/>
+                                                        <userControls:ShortcutBox Width="120" Command="{Binding Command}" HorizontalAlignment="Right" Focusable="False"/>
                                                     </Grid>
                                                 </DataTemplate>
                                             </ItemsControl.ItemTemplate>

+ 3 - 0
src/PixiEditor/Views/UserControls/KeyCombinationBox.xaml.cs

@@ -1,6 +1,8 @@
 using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Input;
+using PixiEditor.Helpers;
+using PixiEditor.Helpers.Behaviours;
 using PixiEditor.Models.DataHolders;
 
 namespace PixiEditor.Views.UserControls;
@@ -90,6 +92,7 @@ internal partial class KeyCombinationBox : UserControl
 
         UpdateText();
         UpdateButton();
+        FocusHelper.MoveFocusToParent((FrameworkElement)sender);
     }
 
     private void Button_Click(object sender, RoutedEventArgs e)