Browse Source

Changed default new file size to 64 and UX improvements

CPKreuz 3 years ago
parent
commit
25f7a16064

+ 14 - 51
PixiEditor/Helpers/Behaviours/TextBoxFocusBehavior.cs

@@ -8,14 +8,6 @@ namespace PixiEditor.Helpers.Behaviours
 {
     internal class TextBoxFocusBehavior : Behavior<TextBox>
     {
-        // Using a DependencyProperty as the backing store for FillSize.  This enables animation, styling, binding, etc...
-        public static readonly DependencyProperty FillSizeProperty =
-            DependencyProperty.Register(
-                nameof(FillSize),
-                typeof(bool),
-                typeof(TextBoxFocusBehavior),
-                new PropertyMetadata(false));
-
         // Using a DependencyProperty as the backing store for FillSize.  This enables animation, styling, binding, etc...
         public static readonly DependencyProperty SelectOnFocusProperty =
             DependencyProperty.Register(
@@ -24,13 +16,13 @@ namespace PixiEditor.Helpers.Behaviours
                 typeof(TextBoxFocusBehavior),
                 new PropertyMetadata(true));
 
-        private string oldText; // Value of textbox before editing
-        private bool valueConverted; // This bool is used to avoid double convertion if enter is hitted
+        public static readonly DependencyProperty NextControlProperty =
+            DependencyProperty.Register(nameof(NextControl), typeof(FrameworkElement), typeof(TextBoxFocusBehavior));
 
-        public bool FillSize
+        public FrameworkElement NextControl
         {
-            get => (bool)GetValue(FillSizeProperty);
-            set => SetValue(FillSizeProperty, value);
+            get => (FrameworkElement)GetValue(NextControlProperty);
+            set => SetValue(NextControlProperty, value);
         }
 
         public bool SelectOnFocus
@@ -45,7 +37,6 @@ namespace PixiEditor.Helpers.Behaviours
             AssociatedObject.GotKeyboardFocus += AssociatedObjectGotKeyboardFocus;
             AssociatedObject.GotMouseCapture += AssociatedObjectGotMouseCapture;
             AssociatedObject.PreviewMouseLeftButtonDown += AssociatedObjectPreviewMouseLeftButtonDown;
-            AssociatedObject.LostKeyboardFocus += AssociatedObject_LostKeyboardFocus;
             AssociatedObject.KeyUp += AssociatedObject_KeyUp;
         }
 
@@ -55,7 +46,6 @@ namespace PixiEditor.Helpers.Behaviours
             AssociatedObject.GotKeyboardFocus -= AssociatedObjectGotKeyboardFocus;
             AssociatedObject.GotMouseCapture -= AssociatedObjectGotMouseCapture;
             AssociatedObject.PreviewMouseLeftButtonDown -= AssociatedObjectPreviewMouseLeftButtonDown;
-            AssociatedObject.LostKeyboardFocus -= AssociatedObject_LostKeyboardFocus;
             AssociatedObject.KeyUp -= AssociatedObject_KeyUp;
         }
 
@@ -67,19 +57,26 @@ namespace PixiEditor.Helpers.Behaviours
                 return;
             }
 
-            ConvertValue();
             RemoveFocus();
         }
 
         private void RemoveFocus()
         {
+            DependencyObject scope = FocusManager.GetFocusScope(AssociatedObject);
+
+            if (NextControl != null)
+            {
+                FocusManager.SetFocusedElement(scope, NextControl);
+                return;
+            }
+
             FrameworkElement parent = (FrameworkElement)AssociatedObject.Parent;
+
             while (parent != null && parent is IInputElement element && !element.Focusable)
             {
                 parent = (FrameworkElement)parent.Parent;
             }
 
-            DependencyObject scope = FocusManager.GetFocusScope(AssociatedObject);
             FocusManager.SetFocusedElement(scope, parent);
         }
 
@@ -89,11 +86,6 @@ namespace PixiEditor.Helpers.Behaviours
         {
             if (SelectOnFocus)
                 AssociatedObject.SelectAll();
-            if (FillSize)
-            {
-                valueConverted = false;
-                oldText = AssociatedObject.Text; // Sets old value when keyboard is focused on object
-            }
         }
 
         private void AssociatedObjectGotMouseCapture(
@@ -112,34 +104,5 @@ namespace PixiEditor.Helpers.Behaviours
                 e.Handled = true;
             }
         }
-
-        private void AssociatedObject_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
-        {
-            ConvertValue();
-        }
-
-        /// <summary>
-        ///     Converts number from textbox to format "number px" ex. "15 px".
-        /// </summary>
-        private void ConvertValue()
-        {
-            if (valueConverted || FillSize == false || AssociatedObject.Text == oldText)
-            {
-                return;
-            }
-
-            if (int.TryParse(Regex.Replace(AssociatedObject.Text, "\\p{L}", string.Empty).Trim(), out int result) && result > 0)
-            {
-                AssociatedObject.Text = $"{result} px";
-            }
-
-            // If text in textbox isn't number, set it to old value
-            else
-            {
-                AssociatedObject.Text = oldText;
-            }
-
-            valueConverted = true;
-        }
     }
 }

+ 14 - 25
PixiEditor/Models/Dialogs/NewFileDialog.cs

@@ -1,53 +1,42 @@
-using System;
-using System.Windows;
-using PixiEditor.Models.UserPreferences;
+using PixiEditor.Models.UserPreferences;
 using PixiEditor.Views;
 
 namespace PixiEditor.Models.Dialogs
 {
     public class NewFileDialog : CustomDialog
     {
-        private int height = (int)IPreferences.Current.GetPreference("DefaultNewFileHeight", 16L);
+        public const int defaultSize = 64;
 
-        private int width = (int)IPreferences.Current.GetPreference("DefaultNewFileWidth", 16L);
+        private int height = IPreferences.Current.GetPreference("DefaultNewFileHeight", defaultSize);
+
+        private int width = IPreferences.Current.GetPreference("DefaultNewFileWidth", defaultSize);
 
         public int Width
         {
             get => width;
-            set
-            {
-                if (width != value)
-                {
-                    width = value;
-                    RaisePropertyChanged("Width");
-                }
-            }
+            set => SetProperty(ref width, value);
         }
 
         public int Height
         {
             get => height;
-            set
-            {
-                if (height != value)
-                {
-                    height = value;
-                    RaisePropertyChanged("Height");
-                }
-            }
+            set => SetProperty(ref height, value);
         }
 
         public override bool ShowDialog()
         {
-            Window popup = new NewFilePopup()
+            NewFilePopup popup = new()
             {
                 FileWidth = Width,
                 FileHeight = Height
             };
+
             popup.ShowDialog();
-            Height = (popup as NewFilePopup).FileHeight;
-            Width = (popup as NewFilePopup).FileWidth;
-            return (bool)popup.DialogResult;
+
+            Height = popup.FileHeight;
+            Width = popup.FileWidth;
+
+            return popup.DialogResult.GetValueOrDefault();
         }
     }
 }

+ 5 - 9
PixiEditor/ViewModels/SubViewModels/UserPreferences/Settings/FileSettings.cs

@@ -1,8 +1,4 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
+using PixiEditor.Models.Dialogs;
 
 namespace PixiEditor.ViewModels.SubViewModels.UserPreferences.Settings
 {
@@ -16,9 +12,9 @@ namespace PixiEditor.ViewModels.SubViewModels.UserPreferences.Settings
             set => RaiseAndUpdatePreference(ref showStartupWindow, value);
         }
 
-        private long defaultNewFileWidth = GetPreference("DefaultNewFileWidth", 16L);
+        private int defaultNewFileWidth = GetPreference("DefaultNewFileWidth", NewFileDialog.defaultSize);
 
-        public long DefaultNewFileWidth
+        public int DefaultNewFileWidth
         {
             get => defaultNewFileWidth;
             set
@@ -29,9 +25,9 @@ namespace PixiEditor.ViewModels.SubViewModels.UserPreferences.Settings
             }
         }
 
-        private long defaultNewFileHeight = GetPreference("DefaultNewFileHeight", 16L);
+        private int defaultNewFileHeight = GetPreference("DefaultNewFileHeight", NewFileDialog.defaultSize);
 
-        public long DefaultNewFileHeight
+        public int DefaultNewFileHeight
         {
             get => defaultNewFileHeight;
             set

+ 9 - 6
PixiEditor/Views/Dialogs/NewFilePopup.xaml

@@ -41,12 +41,15 @@
         <StackPanel HorizontalAlignment="Center" Margin="0,60,0,0" Background="{StaticResource MainColor}"
                         VerticalAlignment="Top" Grid.Row="1" Width="350" Height="150">
             <local:SizePicker Margin="0,20" HorizontalAlignment="Center" Height="110"
-                                  ChosenHeight="{Binding FileHeight,Mode=TwoWay, ElementName=newFilePopup}"
-                                  ChosenWidth="{Binding FileWidth,Mode=TwoWay, ElementName=newFilePopup}" />
+                              ChosenHeight="{Binding FileHeight, Mode=TwoWay, ElementName=newFilePopup}"
+                              ChosenWidth="{Binding FileWidth, Mode=TwoWay, ElementName=newFilePopup}" 
+                              x:Name="sizePicker"
+                              NextControl="{Binding ElementName=createButton}"/>
         </StackPanel>
-        <Button VerticalAlignment="Bottom" HorizontalAlignment="Right" FontSize="20" Height="30" Width="60"
-                    Style="{StaticResource DarkRoundButton}" Content="OK" Margin="0,0,10,10" Grid.Row="1"
-                    Command="{Binding OkCommand}"
-                    CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
+        <Button VerticalAlignment="Bottom" HorizontalAlignment="Right" FontSize="20" Height="30" Width="120"
+                Style="{StaticResource DarkRoundButton}" Content="Create" Margin="0,0,10,10" Grid.Row="1"
+                Command="{Binding OkCommand}"
+                CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" 
+                x:Name="createButton"/>
     </Grid>
 </Window>

+ 3 - 4
PixiEditor/Views/Dialogs/NewFilePopup.xaml.cs

@@ -8,18 +8,17 @@ namespace PixiEditor.Views
     /// </summary>
     public partial class NewFilePopup : Window
     {
-        // Using a DependencyProperty as the backing store for FileHeight.  This enables animation, styling, binding, etc...
         public static readonly DependencyProperty FileHeightProperty =
-            DependencyProperty.Register("FileHeight", typeof(int), typeof(NewFilePopup), new PropertyMetadata(16));
+            DependencyProperty.Register(nameof(FileHeight), typeof(int), typeof(NewFilePopup));
 
-        // Using a DependencyProperty as the backing store for FileWidth.  This enables animation, styling, binding, etc...
         public static readonly DependencyProperty FileWidthProperty =
-            DependencyProperty.Register("FileWidth", typeof(int), typeof(NewFilePopup), new PropertyMetadata(16));
+            DependencyProperty.Register(nameof(FileWidth), typeof(int), typeof(NewFilePopup));
 
         public NewFilePopup()
         {
             InitializeComponent();
             Owner = Application.Current.MainWindow;
+            sizePicker.FocusWidthPicker();
         }
 
         public int FileHeight

+ 5 - 3
PixiEditor/Views/Dialogs/SettingsWindow.xaml

@@ -64,14 +64,16 @@
                                   IsChecked="{Binding SettingsSubViewModel.File.ShowStartupWindow}"/>
                         <StackPanel Orientation="Horizontal" Margin="0,10,0,0">
                             <Label Content="Max Saved Opened Recently:" ToolTip="How many documents are shown under File > Recent. Default: 8" Style="{StaticResource BaseLabel}"/>
-                            <views:NumberInput FontSize="16" Value="{Binding SettingsSubViewModel.File.MaxOpenedRecently, Mode=TwoWay}" Width="40"/>
+                            <views:NumberInput Min="0" FontSize="16" Value="{Binding SettingsSubViewModel.File.MaxOpenedRecently, Mode=TwoWay}" Width="40"/>
                         </StackPanel>
                         <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 FontSize="16" Size="{Binding SettingsSubViewModel.File.DefaultNewFileWidth, Mode=TwoWay}" Width="70" Height="25"/>
+                            <views:SizeInput FontSize="16" Size="{Binding SettingsSubViewModel.File.DefaultNewFileWidth, Mode=TwoWay}" 
+                                             Width="70" Height="25" MaxSize="9999"/>
                             <Label Content="Height:" Style="{StaticResource BaseLabel}"/>
-                            <views:SizeInput FontSize="16" Size="{Binding SettingsSubViewModel.File.DefaultNewFileHeight, Mode=TwoWay}" Width="70" Height="25"/>
+                            <views:SizeInput FontSize="16" Size="{Binding SettingsSubViewModel.File.DefaultNewFileHeight, Mode=TwoWay}" 
+                                             Width="70" Height="25" MaxSize="9999"/>
                         </StackPanel>
                     </StackPanel>
                 </StackPanel>

+ 6 - 5
PixiEditor/Views/UserControls/NumberInput.xaml.cs

@@ -15,7 +15,7 @@ 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",
+                nameof(Value),
                 typeof(float),
                 typeof(NumberInput),
                 new PropertyMetadata(0f, OnValueChanged));
@@ -23,7 +23,7 @@ namespace PixiEditor.Views
         // Using a DependencyProperty as the backing store for Min.  This enables animation, styling, binding, etc...
         public static readonly DependencyProperty MinProperty =
             DependencyProperty.Register(
-                "Min",
+                nameof(Min),
                 typeof(float),
                 typeof(NumberInput),
                 new PropertyMetadata(float.NegativeInfinity));
@@ -31,12 +31,12 @@ namespace PixiEditor.Views
         // Using a DependencyProperty as the backing store for Max.  This enables animation, styling, binding, etc...
         public static readonly DependencyProperty MaxProperty =
             DependencyProperty.Register(
-                "Max",
+                nameof(Max),
                 typeof(float),
                 typeof(NumberInput),
                 new PropertyMetadata(float.PositiveInfinity));
 
-        private Regex regex = new Regex("^[.][0-9]+$|^[0-9]*[.]{0,1}[0-9]*$");
+        private readonly Regex regex = new Regex("^[.][0-9]+$|^[0-9]*[.]{0,1}[0-9]*$", RegexOptions.Compiled);
 
         public NumberInput()
         {
@@ -78,7 +78,8 @@ namespace PixiEditor.Views
 
             if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
             {
-                Value += step * 2f;
+                float multiplier = (Max - Min) * 0.1f;
+                Value += step * multiplier;
             }
             else if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
             {

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

@@ -7,7 +7,7 @@
              xmlns:behaviors="clr-namespace:PixiEditor.Helpers.Behaviours"
              xmlns:converters="clr-namespace:PixiEditor.Helpers.Converters"
              xmlns:validators="clr-namespace:PixiEditor.Helpers.Validators"
-             mc:Ignorable="d" Foreground="White"
+             mc:Ignorable="d" Foreground="White" Focusable="True"
              d:DesignHeight="30" d:DesignWidth="160" Name="uc" LayoutUpdated="UserControlLayoutUpdated">
 
     <Border BorderThickness="1" CornerRadius="3.5"
@@ -43,7 +43,7 @@
                      d:Text="22">
                 <i:Interaction.Behaviors>
                     <behaviors:GlobalShortcutFocusBehavior/>
-                    <behaviors:TextBoxFocusBehavior FillSize="True" SelectOnFocus="{Binding SelectOnFocus, ElementName=uc}" />
+                    <behaviors:TextBoxFocusBehavior SelectOnFocus="{Binding SelectOnFocus, ElementName=uc}" NextControl="{Binding NextControl, ElementName=uc}"/>
                 </i:Interaction.Behaviors>
             </TextBox>
             <Grid Grid.Column="1" Background="{Binding BorderBrush, ElementName=border}"

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

@@ -17,8 +17,7 @@ namespace PixiEditor.Views
             DependencyProperty.Register(
                 nameof(PreserveAspectRatio),
                 typeof(bool),
-                typeof(SizeInput),
-                new PropertyMetadata(false));
+                typeof(SizeInput));
 
         public static readonly DependencyProperty AspectRatioValueProperty =
             DependencyProperty.Register(
@@ -47,8 +46,12 @@ namespace PixiEditor.Views
         private int loadedSize = -1;
         private bool blockUpdate = false;
 
+        public static readonly DependencyProperty NextControlProperty =
+            DependencyProperty.Register(nameof(NextControl), typeof(FrameworkElement), typeof(SizeInput));
+
         public SizeInput()
         {
+            GotKeyboardFocus += SizeInput_GotKeyboardFocus;
             InitializeComponent();
         }
 
@@ -58,6 +61,11 @@ namespace PixiEditor.Views
             set => SetValue(SelectOnFocusProperty, value);
         }
 
+        private void SizeInput_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
+        {
+            textBox.Focus();
+        }
+
         public int Size
         {
             get => (int)GetValue(SizeProperty);
@@ -82,6 +90,12 @@ namespace PixiEditor.Views
             set => SetValue(AspectRatioValueProperty, value);
         }
 
+        public FrameworkElement NextControl
+        {
+            get => (FrameworkElement)GetValue(NextControlProperty);
+            set => SetValue(NextControlProperty, value);
+        }
+
         private static void InputSizeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
         {
             int newValue = (int)e.NewValue;

+ 5 - 4
PixiEditor/Views/UserControls/SizePicker.xaml

@@ -15,7 +15,8 @@
                              AspectRatioValue="{Binding Path=ChosenHeight, ElementName=uc}"
                              AspectRatioControl="{Binding ElementName=HeightPicker}"
                              HorizontalAlignment="Left" Margin="10,0,0,0"
-                             FontSize="16" MaxSize="9999" SelectOnFocus="False"
+                             FontSize="16" NextControl="{Binding ElementName=HeightPicker}"
+                             MaxSize="9999" SelectOnFocus="False"
                              Size="{Binding Path=ChosenWidth, ElementName=uc, Mode=TwoWay}" />
         </DockPanel>
         <DockPanel Margin="5,10,0,0" HorizontalAlignment="Center" VerticalAlignment="Center">
@@ -26,10 +27,10 @@
                              AspectRatioValue="{Binding Path=ChosenWidth, ElementName=uc}"
                              AspectRatioControl="{Binding ElementName=WidthPicker}"
                              HorizontalAlignment="Left" Width="150" Height="30"
-                             FontSize="16" MaxSize="9999" SelectOnFocus="False"
+                             FontSize="16" NextControl="{Binding NextControl, ElementName=uc}"
+                             MaxSize="9999" SelectOnFocus="False"
                              Size="{Binding ChosenHeight, ElementName=uc, Mode=TwoWay}" />
         </DockPanel>
-        <CheckBox Name="aspectRatio" Content="Preserve aspect ratio" Foreground="White" HorizontalAlignment="Left"
-                  IsChecked="True" Margin="50,10,0,0" />
+        <CheckBox Name="aspectRatio" Content="Preserve aspect ratio" Foreground="White" HorizontalAlignment="Left" Margin="50,10,0,0" />
     </StackPanel>
 </UserControl>

+ 17 - 6
PixiEditor/Views/UserControls/SizePicker.xaml.cs

@@ -8,17 +8,17 @@ namespace PixiEditor.Views
     /// </summary>
     public partial class SizePicker : UserControl
     {
-        // Using a DependencyProperty as the backing store for EditingEnabled.  This enables animation, styling, binding, etc...
         public static readonly DependencyProperty EditingEnabledProperty =
-            DependencyProperty.Register("EditingEnabled", typeof(bool), typeof(SizePicker), new PropertyMetadata(true));
+            DependencyProperty.Register(nameof(EditingEnabled), typeof(bool), typeof(SizePicker), new PropertyMetadata(true));
 
-        // Using a DependencyProperty as the backing store for ChosenWidth.  This enables animation, styling, binding, etc...
         public static readonly DependencyProperty ChosenWidthProperty =
-            DependencyProperty.Register("ChosenWidth", typeof(int), typeof(SizePicker), new PropertyMetadata(1));
+            DependencyProperty.Register(nameof(ChosenWidth), typeof(int), typeof(SizePicker), new PropertyMetadata(1));
 
-        // Using a DependencyProperty as the backing store for ChosenHeight.  This enables animation, styling, binding, etc...
         public static readonly DependencyProperty ChosenHeightProperty =
-            DependencyProperty.Register("ChosenHeight", typeof(int), typeof(SizePicker), new PropertyMetadata(1));
+            DependencyProperty.Register(nameof(ChosenHeight), typeof(int), typeof(SizePicker), new PropertyMetadata(1));
+
+        public static readonly DependencyProperty NextControlProperty =
+            DependencyProperty.Register(nameof(NextControl), typeof(FrameworkElement), typeof(SizePicker));
 
         public SizePicker()
         {
@@ -42,5 +42,16 @@ namespace PixiEditor.Views
             get => (int)GetValue(ChosenHeightProperty);
             set => SetValue(ChosenHeightProperty, value);
         }
+
+        public FrameworkElement NextControl
+        {
+            get => (FrameworkElement)GetValue(NextControlProperty);
+            set => SetValue(NextControlProperty, value);
+        }
+
+        public void FocusWidthPicker()
+        {
+            WidthPicker.Focus();
+        }
     }
 }