Browse Source

Merge pull request #141 from PixiEditor/disable-shortcut-input

Disable shortcut on textbox focus
Krzysztof Krysiński 4 years ago
parent
commit
fdfba0d314

+ 4 - 5
PixiEditor/Helpers/Behaviours/ClearFocusOnClickBehavior.cs

@@ -1,5 +1,4 @@
 using System.Windows;
-using System.Windows.Input;
 using System.Windows.Interactivity;
 
 namespace PixiEditor.Helpers.Behaviours
@@ -12,14 +11,14 @@ namespace PixiEditor.Helpers.Behaviours
             base.OnAttached();
         }
 
-        private void AssociatedObject_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
+        protected override void OnDetaching()
         {
-            AssociatedObject.Focus();
+            AssociatedObject.MouseDown -= AssociatedObject_MouseDown;
         }
 
-        protected override void OnDetaching()
+        private void AssociatedObject_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
         {
-            AssociatedObject.MouseDown -= AssociatedObject_MouseDown;
+            AssociatedObject.Focus();
         }
     }
 }

+ 38 - 0
PixiEditor/Helpers/Behaviours/GlobalShortcutFocusBehavior.cs

@@ -0,0 +1,38 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Interactivity;
+using PixiEditor.Models.Controllers.Shortcuts;
+
+namespace PixiEditor.Helpers.Behaviours
+{
+    public class GlobalShortcutFocusBehavior : Behavior<FrameworkElement>
+    {
+        protected override void OnAttached()
+        {
+            base.OnAttached();
+            AssociatedObject.GotKeyboardFocus += AssociatedObject_GotKeyboardFocus;
+            AssociatedObject.LostKeyboardFocus += AssociatedObject_LostKeyboardFocus;
+        }
+
+        protected override void OnDetaching()
+        {
+            base.OnDetaching();
+            AssociatedObject.GotKeyboardFocus -= AssociatedObject_GotKeyboardFocus;
+            AssociatedObject.LostKeyboardFocus -= AssociatedObject_LostKeyboardFocus;
+        }
+
+        private void AssociatedObject_LostKeyboardFocus(object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e)
+        {
+            ShortcutController.BlockShortcutExecution = false;
+        }
+
+        private void AssociatedObject_GotKeyboardFocus(object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e)
+        {
+            ShortcutController.BlockShortcutExecution = true;
+        }
+    }
+}

+ 1 - 0
PixiEditor/Helpers/Behaviours/TextBoxFocusBehavior.cs

@@ -3,6 +3,7 @@ using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Input;
 using System.Windows.Interactivity;
+using PixiEditor.Models.Controllers.Shortcuts;
 
 namespace PixiEditor.Helpers.Behaviours
 {

+ 1 - 1
PixiEditor/Models/Controllers/BitmapManager.cs

@@ -60,7 +60,7 @@ namespace PixiEditor.Models.Controllers
             : 1;
             set
             {
-                if (SelectedTool.Toolbar.GetSetting<SizeSetting>("ToolSize") is var toolSize)
+                if (SelectedTool.Toolbar.GetSetting<SizeSetting>("ToolSize") is SizeSetting toolSize)
                 {
                     toolSize.Value = value;
                     HighlightPixels(MousePositionConverter.CurrentCoordinates);

+ 7 - 2
PixiEditor/Models/Tools/ToolSettings/Settings/ColorSetting.cs

@@ -1,8 +1,10 @@
 using System.Windows;
 using System.Windows.Data;
+using System.Windows.Interactivity;
 using System.Windows.Media;
 using ColorPicker;
-
+using PixiEditor.Helpers.Behaviours;
+
 namespace PixiEditor.Models.Tools.ToolSettings.Settings
 {
     public class ColorSetting : Setting<Color>
@@ -18,7 +20,8 @@ namespace PixiEditor.Models.Tools.ToolSettings.Settings
         private PortableColorPicker GenerateColorPicker()
         {
             var resourceDictionary = new ResourceDictionary();
-            resourceDictionary.Source = new System.Uri("pack://application:,,,/ColorPicker;component/Styles/DefaultColorPickerStyle.xaml",
+            resourceDictionary.Source = new System.Uri(
+                "pack://application:,,,/ColorPicker;component/Styles/DefaultColorPickerStyle.xaml",
                 System.UriKind.RelativeOrAbsolute);
             PortableColorPicker picker = new PortableColorPicker
             {
@@ -29,6 +32,8 @@ namespace PixiEditor.Models.Tools.ToolSettings.Settings
             {
                 Mode = BindingMode.TwoWay
             };
+            GlobalShortcutFocusBehavior behavor = new GlobalShortcutFocusBehavior();
+            Interaction.GetBehaviors(picker).Add(behavor);
             picker.SetBinding(PortableColorPicker.SelectedColorProperty, binding);
             return picker;
         }

+ 11 - 22
PixiEditor/Models/Tools/ToolSettings/Settings/SizeSetting.cs

@@ -4,7 +4,8 @@ using System.Windows.Data;
 using System.Windows.Interactivity;
 using PixiEditor.Helpers;
 using PixiEditor.Helpers.Behaviours;
-
+using PixiEditor.Views;
+
 namespace PixiEditor.Models.Tools.ToolSettings.Settings
 {
     public class SizeSetting : Setting<int>
@@ -17,32 +18,20 @@ namespace PixiEditor.Models.Tools.ToolSettings.Settings
             Label = label;
         }
 
-        private TextBox GenerateTextBox()
+        private SizeInput GenerateTextBox()
         {
-            TextBox tb = new TextBox
+            SizeInput tb = new SizeInput
             {
-                TextAlignment = TextAlignment.Center,
-                MaxLength = 4,
                 Width = 40,
-                Height = 20
-            };
-
-            if (Application.Current != null)
-            {
-                tb.Style = (Style)Application.Current.TryFindResource("DarkTextBoxStyle");
-            }
+                Height = 20,
+                FontSize = 12,
+            };
 
-            Binding binding = new Binding("Value")
-            {
-                Converter = new ToolSizeToIntConverter(),
-                Mode = BindingMode.TwoWay
-            };
-            tb.SetBinding(TextBox.TextProperty, binding);
-            TextBoxFocusBehavior behavor = new TextBoxFocusBehavior
-            {
-                FillSize = true
+            Binding binding = new Binding("Value")
+            {
+                Mode = BindingMode.TwoWay,
             };
-            Interaction.GetBehaviors(tb).Add(behavor);
+            tb.SetBinding(SizeInput.SizeProperty, binding);
             return tb;
         }
     }

+ 2 - 2
PixiEditor/Properties/AssemblyInfo.cs

@@ -50,5 +50,5 @@ using System.Windows;
 // You can specify all the values or you can default the Build and Revision Numbers
 // by using the '*' as shown below:
 // [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("0.1.4.0")]
-[assembly: AssemblyFileVersion("0.1.4.0")]
+[assembly: AssemblyVersion("0.1.4.1")]
+[assembly: AssemblyFileVersion("0.1.4.1")]

+ 2 - 2
PixiEditor/Views/Dialogs/SettingsWindow.xaml

@@ -58,9 +58,9 @@
                         <Label Content="Default new file size:" Style="{StaticResource Header2}" Margin="0 20 0 20"/>
                         <StackPanel Orientation="Horizontal" Margin="40,0,0,0">
                             <Label Content="Width:" Style="{StaticResource BaseLabel}"/>
-                            <views:SizeInput Size="{Binding SettingsSubViewModel.DefaultNewFileWidth, Mode=TwoWay}" Width="60" Height="25"/>
+                            <views:SizeInput FontSize="16" Size="{Binding SettingsSubViewModel.DefaultNewFileWidth, Mode=TwoWay}" Width="60" Height="25"/>
                             <Label Content="Height:" Style="{StaticResource BaseLabel}"/>
-                            <views:SizeInput Size="{Binding SettingsSubViewModel.DefaultNewFileHeight, Mode=TwoWay}" Width="60" Height="25"/>
+                            <views:SizeInput FontSize="16" Size="{Binding SettingsSubViewModel.DefaultNewFileHeight, Mode=TwoWay}" Width="60" Height="25"/>
                         </StackPanel>
                     </StackPanel>
                 </StackPanel>

+ 6 - 2
PixiEditor/Views/MainWindow.xaml

@@ -161,7 +161,7 @@
                             <Label
                                 Visibility="{Binding HasLabel, Converter={StaticResource BoolToVisibilityConverter}}"
                                 Foreground="White" Content="{Binding Label}" />
-                            <ContentPresenter Content="{Binding SettingControl}" />
+                            <ContentControl Content="{Binding SettingControl}" />
                         </StackPanel>
                     </DataTemplate>
                 </ItemsControl.ItemTemplate>
@@ -234,7 +234,11 @@
                                                              CanClose="False" CanAutoHide="False"
                                                              CanDockAsTabbedDocument="True" CanFloat="True">
                                 <colorpicker:StandardColorPicker Grid.Row="0" SelectedColor="{Binding ColorsSubViewModel.PrimaryColor, Mode=TwoWay}"
-                                     SecondaryColor="{Binding ColorsSubViewModel.SecondaryColor, Mode=TwoWay}" Style="{StaticResource DefaultColorPickerStyle}" />
+                                     SecondaryColor="{Binding ColorsSubViewModel.SecondaryColor, Mode=TwoWay}" Style="{StaticResource DefaultColorPickerStyle}" >
+                                    <i:Interaction.Behaviors>
+                                        <behaviours:GlobalShortcutFocusBehavior/>
+                                    </i:Interaction.Behaviors>
+                                </colorpicker:StandardColorPicker>
                             </LayoutAnchorable>
                             <avalondock:LayoutAnchorable ContentId="swatches" Title="Swatches" CanHide="False"
                                                          CanClose="False" CanAutoHide="False"

+ 6 - 2
PixiEditor/Views/UserControls/EditableTextBlock.xaml

@@ -3,7 +3,7 @@
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
-             xmlns:converters="clr-namespace:PixiEditor.Helpers.Converters"
+             xmlns:converters="clr-namespace:PixiEditor.Helpers.Converters" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:behaviours="clr-namespace:PixiEditor.Helpers.Behaviours"
              mc:Ignorable="d"
              d:DesignHeight="60" d:DesignWidth="100">
     <UserControl.Resources>
@@ -20,6 +20,10 @@
                  LostKeyboardFocus="textBox_LostKeyboardFocus"
                  Visibility="{Binding Path=TextBlockVisibility, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, 
             Converter={StaticResource OppositeVisibilityConverter}}"
-                 Name="textBox" />
+                 Name="textBox">
+            <i:Interaction.Behaviors>
+                <behaviours:GlobalShortcutFocusBehavior/>
+            </i:Interaction.Behaviors>
+        </TextBox>
     </Grid>
 </UserControl>

+ 17 - 8
PixiEditor/Views/UserControls/EditableTextBlock.xaml.cs

@@ -7,19 +7,24 @@ using PixiEditor.Models.Controllers.Shortcuts;
 namespace PixiEditor.Views
 {
     /// <summary>
-    ///     Interaction logic for EditableTextBlock.xaml
+    ///     Interaction logic for EditableTextBlock.xaml.
     /// </summary>
     public partial class EditableTextBlock : UserControl
     {
         // Using a DependencyProperty as the backing store for TextBlockVisibility.  This enables animation, styling, binding, etc...
         public static readonly DependencyProperty TextBlockVisibilityProperty =
-            DependencyProperty.Register("TextBlockVisibility", typeof(Visibility), typeof(EditableTextBlock),
+            DependencyProperty.Register(
+                "TextBlockVisibility",
+                typeof(Visibility),
+                typeof(EditableTextBlock),
                 new PropertyMetadata(Visibility.Visible));
 
-
         // Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
         public static readonly DependencyProperty TextProperty =
-            DependencyProperty.Register("Text", typeof(string), typeof(EditableTextBlock),
+            DependencyProperty.Register(
+                "Text",
+                typeof(string),
+                typeof(EditableTextBlock),
                 new PropertyMetadata(default(string)));
 
         // Using a DependencyProperty as the backing store for EnableEditing.  This enables animation, styling, binding, etc...
@@ -38,14 +43,12 @@ namespace PixiEditor.Views
             set => SetValue(TextBlockVisibilityProperty, value);
         }
 
-
         public bool IsEditing
         {
             get => (bool) GetValue(EnableEditingProperty);
             set => SetValue(EnableEditingProperty, value);
         }
 
-
         public string Text
         {
             get => (string) GetValue(TextProperty);
@@ -80,12 +83,18 @@ namespace PixiEditor.Views
 
         private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
         {
-            if (e.ChangedButton == MouseButton.Left && e.ClickCount == 2) EnableEditing();
+            if (e.ChangedButton == MouseButton.Left && e.ClickCount == 2)
+            {
+                EnableEditing();
+            }
         }
 
         private void TextBox_KeyDown(object sender, KeyEventArgs e)
         {
-            if (e.Key == Key.Enter) DisableEditing();
+            if (e.Key == Key.Enter)
+            {
+                DisableEditing();
+            }
         }
 
         private void TextBox_LostFocus(object sender, RoutedEventArgs e)

+ 6 - 7
PixiEditor/Views/UserControls/NumberInput.xaml

@@ -9,12 +9,11 @@
              xmlns:ui="clr-namespace:PixiEditor.Helpers.UI"
              mc:Ignorable="d"
              d:DesignHeight="20" d:DesignWidth="40" x:Name="numberInput">
-    <Grid>
-        <TextBox TextAlignment="Center" Style="{StaticResource DarkTextBoxStyle}"
+    <TextBox TextAlignment="Center" Style="{StaticResource DarkTextBoxStyle}"
                  PreviewTextInput="TextBox_PreviewTextInput" Text="{Binding ElementName=numberInput, Path=Value}">
-            <i:Interaction.Behaviors>
-                <behaviours:TextBoxFocusBehavior />
-            </i:Interaction.Behaviors>
-        </TextBox>
-    </Grid>
+        <i:Interaction.Behaviors>
+            <behaviours:TextBoxFocusBehavior />
+            <behaviours:GlobalShortcutFocusBehavior/>
+        </i:Interaction.Behaviors>
+    </TextBox>
 </UserControl>

+ 16 - 7
PixiEditor/Views/UserControls/NumberInput.xaml.cs

@@ -1,4 +1,5 @@
-using System;
+using PixiEditor.Models.Controllers.Shortcuts;
+using System;
 using System.Text.RegularExpressions;
 using System.Windows;
 using System.Windows.Controls;
@@ -13,20 +14,28 @@ namespace PixiEditor.Views
     {
         // Using a DependencyProperty as the backing store for Value.  This enables animation, styling, binding, etc...
         public static readonly DependencyProperty ValueProperty =
-            DependencyProperty.Register("Value", typeof(float), typeof(NumberInput),
+            DependencyProperty.Register(
+                "Value",
+                typeof(float),
+                typeof(NumberInput),
                 new PropertyMetadata(0f, OnValueChanged));
 
         // Using a DependencyProperty as the backing store for Min.  This enables animation, styling, binding, etc...
         public static readonly DependencyProperty MinProperty =
-            DependencyProperty.Register("Min", typeof(float), typeof(NumberInput),
+            DependencyProperty.Register(
+                "Min",
+                typeof(float),
+                typeof(NumberInput),
                 new PropertyMetadata(float.NegativeInfinity));
 
         // Using a DependencyProperty as the backing store for Max.  This enables animation, styling, binding, etc...
         public static readonly DependencyProperty MaxProperty =
-            DependencyProperty.Register("Max", typeof(float), typeof(NumberInput),
+            DependencyProperty.Register(
+                "Max",
+                typeof(float), 
+                typeof(NumberInput),
                 new PropertyMetadata(float.PositiveInfinity));
 
-
         public NumberInput()
         {
             InitializeComponent();
@@ -53,8 +62,8 @@ namespace PixiEditor.Views
 
         private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
         {
-            NumberInput input = (NumberInput) d;
-            input.Value = Math.Clamp((float) e.NewValue, input.Min, input.Max);
+            NumberInput input = (NumberInput)d;
+            input.Value = Math.Clamp((float)e.NewValue, input.Min, input.Max);
         }
 
         private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)

+ 2 - 2
PixiEditor/Views/UserControls/SizeInput.xaml

@@ -3,7 +3,6 @@
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
-             xmlns:local="clr-namespace:PixiEditor.Views"
              xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
              xmlns:behaviors="clr-namespace:PixiEditor.Helpers.Behaviours"
              xmlns:converters="clr-namespace:PixiEditor.Helpers"
@@ -14,7 +13,7 @@
         <converters:ToolSizeToIntConverter x:Key="ToolSizeToIntConverter" />
     </UserControl.Resources>
     <TextBox IsEnabled="{Binding IsEnabled, ElementName=uc}" HorizontalContentAlignment="Center"
-             Style="{StaticResource DarkTextBoxStyle}" FontSize="16" MaxLength="4">
+             Style="{StaticResource DarkTextBoxStyle}" MaxLength="4">
         <TextBox.Text>
             <Binding ElementName="uc"
                      Path="Size" Mode="TwoWay"
@@ -25,6 +24,7 @@
             </Binding>
         </TextBox.Text>
         <i:Interaction.Behaviors>
+            <behaviors:GlobalShortcutFocusBehavior/>
             <behaviors:TextBoxFocusBehavior FillSize="True" />
         </i:Interaction.Behaviors>
     </TextBox>

+ 2 - 0
PixiEditor/Views/UserControls/SizePicker.xaml

@@ -15,6 +15,7 @@
                              AspectRatioValue="{Binding Path=ChosenHeight, ElementName=uc}"
                              AspectRatioControl="{Binding ElementName=HeightPicker}"
                              HorizontalAlignment="Left" Margin="10,0,0,0"
+                             FontSize="16"
                              Size="{Binding Path=ChosenWidth, ElementName=uc, Mode=TwoWay}" />
         </DockPanel>
         <DockPanel Margin="5,10,0,0" HorizontalAlignment="Center" VerticalAlignment="Center">
@@ -25,6 +26,7 @@
                              AspectRatioValue="{Binding Path=ChosenWidth, ElementName=uc}"
                              AspectRatioControl="{Binding ElementName=WidthPicker}"
                              HorizontalAlignment="Left" Width="150" Height="30"
+                             FontSize="16"
                              Size="{Binding ChosenHeight, ElementName=uc, Mode=TwoWay}" />
         </DockPanel>
         <CheckBox Name="aspectRatio" Content="Preserve aspect ratio" Foreground="White" HorizontalAlignment="Left"