Browse Source

Added default new file size and wrote tests

flabbet 4 years ago
parent
commit
cfc1f742b3

+ 10 - 4
PixiEditor/Models/Dialogs/NewFileDialog.cs

@@ -1,13 +1,15 @@
-using System.Windows;
+using System;
+using System.Windows;
+using PixiEditor.Models.UserPreferences;
 using PixiEditor.Views;
 
 namespace PixiEditor.Models.Dialogs
 {
     public class NewFileDialog : CustomDialog
     {
-        private int height;
+        private int height = (int)PreferencesSettings.GetPreference("DefaultNewFileHeight", 16L);
 
-        private int width;
+        private int width = (int)PreferencesSettings.GetPreference("DefaultNewFileWidth", 16L);
 
         public int Width
         {
@@ -37,7 +39,11 @@ namespace PixiEditor.Models.Dialogs
 
         public override bool ShowDialog()
         {
-            Window popup = new NewFilePopup();
+            Window popup = new NewFilePopup()
+            {
+                FileWidth = Width,
+                FileHeight = Height
+            };
             popup.ShowDialog();
             Height = (popup as NewFilePopup).FileHeight;
             Width = (popup as NewFilePopup).FileWidth;

+ 12 - 8
PixiEditor/Models/UserPreferences/PreferencesSettings.cs

@@ -1,8 +1,6 @@
 using System;
 using System.Collections.Generic;
-using System.Configuration;
 using System.IO;
-using System.Reflection.Metadata;
 using Newtonsoft.Json;
 
 namespace PixiEditor.Models.UserPreferences
@@ -11,7 +9,7 @@ namespace PixiEditor.Models.UserPreferences
     {
         public static bool IsLoaded { get; private set; } = false;
 
-        public static string PathToUserPreferences { get; } = Path.Join(
+        public static string PathToUserPreferences { get; private set; } = Path.Join(
             Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
             "PixiEditor",
             "user_preferences.json");
@@ -20,21 +18,27 @@ namespace PixiEditor.Models.UserPreferences
 
         public static void Init()
         {
+            Init(PathToUserPreferences);
+        }
+
+        public static void Init(string path)
+        {
+            PathToUserPreferences = path;
             if (IsLoaded == false)
             {
-                string dir = Path.GetDirectoryName(PathToUserPreferences);
+                string dir = Path.GetDirectoryName(path);
                 if (!Directory.Exists(dir))
                 {
                     Directory.CreateDirectory(dir);
                 }
 
-                if (!File.Exists(PathToUserPreferences))
+                if (!File.Exists(path))
                 {
-                    File.WriteAllText(PathToUserPreferences, "{\n}");
+                    File.WriteAllText(path, "{\n}");
                 }
                 else
                 {
-                    string json = File.ReadAllText(PathToUserPreferences);
+                    string json = File.ReadAllText(path);
                     Preferences = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
                 }
 
@@ -42,7 +46,7 @@ namespace PixiEditor.Models.UserPreferences
             }
         }
 
-        public static void UpdatePreference(string name, object value)
+        public static void UpdatePreference<T>(string name, T value)
         {
             if (IsLoaded == false)
             {

+ 1 - 1
PixiEditor/Styles/DarkCheckboxStyle.xaml

@@ -11,7 +11,7 @@
                 <ControlTemplate TargetType="CheckBox">
                     <BulletDecorator Background="Transparent">
                         <BulletDecorator.Bullet>
-                            <Border x:Name="Border" Width="15" Height="15" CornerRadius="2" Background="#FF1B1B1B" BorderThickness="0">
+                            <Border x:Name="Border" Width="20" Height="20" CornerRadius="2" Background="#FF1B1B1B" BorderThickness="0">
                                 <Path Width="9" Height="9" x:Name="CheckMark" SnapsToDevicePixels="False" Stroke="#FF0077C9" StrokeThickness="2" Data="M 0 4 L 3 8 8 0" />
                             </Border>
                         </BulletDecorator.Bullet>

+ 9 - 0
PixiEditor/Styles/LabelStyles.xaml

@@ -10,4 +10,13 @@
         <Setter Property="FontSize" Value="36"/>
         <Setter Property="Margin" Value="20"/>
     </Style>
+
+    <Style x:Key="Header2" TargetType="Label" BasedOn="{StaticResource BaseLabel}">
+        <Setter Property="FontSize" Value="20"/>
+        <Setter Property="Margin" Value="20"/>
+    </Style>
+
+    <Style x:Key="Paragraph" TargetType="Label" BasedOn="{StaticResource BaseLabel}">
+        <Setter Property="Margin" Value="0 10 0 10"/>
+    </Style>
 </ResourceDictionary>

+ 3 - 3
PixiEditor/ViewModels/NewFileMenuViewModel.cs

@@ -20,13 +20,13 @@ namespace PixiEditor.ViewModels
 
         private void OkButton(object parameter)
         {
-            ((Window) parameter).DialogResult = true;
-            ((Window) parameter).Close();
+            ((Window)parameter).DialogResult = true;
+            ((Window)parameter).Close();
         }
 
         private void CloseWindow(object parameter)
         {
-            ((Window) parameter).DialogResult = false;
+            ((Window)parameter).DialogResult = false;
             CloseButton(parameter);
         }
 

+ 1 - 1
PixiEditor/ViewModels/SubViewModels/Main/FileViewModel.cs

@@ -92,7 +92,7 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             }
             else
             {
-                if (PreferencesSettings.GetPreference<bool>("ShowNewFilePopupOnStartup"))
+                if (PreferencesSettings.GetPreference("ShowNewFilePopupOnStartup", true))
                 {
                     OpenNewFilePopup(null);
                 }

+ 33 - 4
PixiEditor/ViewModels/SubViewModels/UserPreferences/SettingsViewModel.cs

@@ -15,8 +15,7 @@ namespace PixiEditor.ViewModels.SubViewModels.UserPreferences
             {
                 showNewFilePopupOnStartup = value;
                 string name = nameof(ShowNewFilePopupOnStartup);
-                RaisePropertyChanged(name);
-                PreferencesSettings.UpdatePreference(name, value);
+                RaiseAndUpdatePreference(name, value);
             }
         }
 
@@ -29,11 +28,41 @@ namespace PixiEditor.ViewModels.SubViewModels.UserPreferences
             {
                 checkUpdatesOnStartup = value;
                 string name = nameof(CheckUpdatesOnStartup);
-                RaisePropertyChanged(name);
-                PreferencesSettings.UpdatePreference(name, value);
+                RaiseAndUpdatePreference(name, value);
             }
         }
 
+        private long defaultNewFileWidth = (int)PreferencesSettings.GetPreference("DefaultNewFileWidth", 16L);
+
+        public long DefaultNewFileWidth
+        {
+            get => defaultNewFileWidth;
+            set
+            {
+                defaultNewFileWidth = value;
+                string name = nameof(DefaultNewFileWidth);
+                RaiseAndUpdatePreference(name, value);
+            }
+        }
+
+        private long defaultNewFileHeight = (int)PreferencesSettings.GetPreference("DefaultNewFileHeight", 16L);
+
+        public long DefaultNewFileHeight
+        {
+            get => defaultNewFileHeight;
+            set
+            {
+                defaultNewFileHeight = value;
+                string name = nameof(DefaultNewFileHeight);
+                RaiseAndUpdatePreference(name, value);
+            }
+        }
+
+        public void RaiseAndUpdatePreference<T>(string name, T value)
+        {
+            RaisePropertyChanged(name);
+            PreferencesSettings.UpdatePreference(name, value);
+        }
 
         public SettingsViewModel(SettingsWindowViewModel owner)
             : base(owner)

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

@@ -3,7 +3,7 @@
 namespace PixiEditor.Views
 {
     /// <summary>
-    ///     Interaction logic for NewFilePopup.xaml
+    ///     Interaction logic for NewFilePopup.xaml.
     /// </summary>
     public partial class NewFilePopup : Window
     {
@@ -20,17 +20,15 @@ namespace PixiEditor.Views
             InitializeComponent();
         }
 
-
         public int FileHeight
         {
-            get => (int) GetValue(FileHeightProperty);
+            get => (int)GetValue(FileHeightProperty);
             set => SetValue(FileHeightProperty, value);
         }
 
-
         public int FileWidth
         {
-            get => (int) GetValue(FileWidthProperty);
+            get => (int)GetValue(FileWidthProperty);
             set => SetValue(FileWidthProperty, value);
         }
     }

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

@@ -3,7 +3,7 @@
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
-        xmlns:local="clr-namespace:PixiEditor.Views.Dialogs" xmlns:viewmodels="clr-namespace:PixiEditor.ViewModels" xmlns:converters="clr-namespace:PixiEditor.Helpers.Converters"
+        xmlns:local="clr-namespace:PixiEditor.Views.Dialogs" xmlns:viewmodels="clr-namespace:PixiEditor.ViewModels" xmlns:converters="clr-namespace:PixiEditor.Helpers.Converters" xmlns:views="clr-namespace:PixiEditor.Views"
         mc:Ignorable="d"
         Title="Settings" Name="window" 
         Height="450" Width="800" WindowStyle="None" DataContext="{DynamicResource SettingsWindowViewModel}"
@@ -50,7 +50,15 @@
                 <StackPanel Orientation="Vertical">
                     <Label Content="File" Style="{StaticResource Header1}"/>
                     <StackPanel Orientation="Vertical" Margin="50 0 50 0">
-                        <CheckBox Content="Show New File dialog on startup" IsChecked="{Binding SettingsSubViewModel.ShowNewFilePopupOnStartup}"/>
+                        <CheckBox Content="Show New File dialog on startup" 
+                                  IsChecked="{Binding SettingsSubViewModel.ShowNewFilePopupOnStartup}"/>
+                        <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"/>
+                            <Label Content="Height:" Style="{StaticResource BaseLabel}"/>
+                            <views:SizeInput Size="{Binding SettingsSubViewModel.DefaultNewFileHeight, Mode=TwoWay}" Width="60" Height="25"/>
+                        </StackPanel>
                     </StackPanel>
                 </StackPanel>
             </Grid>

+ 4 - 7
PixiEditor/Views/UserControls/SizePicker.xaml.cs

@@ -4,7 +4,7 @@ using System.Windows.Controls;
 namespace PixiEditor.Views
 {
     /// <summary>
-    ///     Interaction logic for SizePicker.xaml
+    ///     Interaction logic for SizePicker.xaml.
     /// </summary>
     public partial class SizePicker : UserControl
     {
@@ -25,24 +25,21 @@ namespace PixiEditor.Views
             InitializeComponent();
         }
 
-
         public bool EditingEnabled
         {
-            get => (bool) GetValue(EditingEnabledProperty);
+            get => (bool)GetValue(EditingEnabledProperty);
             set => SetValue(EditingEnabledProperty, value);
         }
 
-
         public int ChosenWidth
         {
-            get => (int) GetValue(ChosenWidthProperty);
+            get => (int)GetValue(ChosenWidthProperty);
             set => SetValue(ChosenWidthProperty, value);
         }
 
-
         public int ChosenHeight
         {
-            get => (int) GetValue(ChosenHeightProperty);
+            get => (int)GetValue(ChosenHeightProperty);
             set => SetValue(ChosenHeightProperty, value);
         }
     }

+ 67 - 0
PixiEditorTests/ModelsTests/UserPreferencesTests/PreferencesSettingsTests.cs

@@ -0,0 +1,67 @@
+using System.Collections.Generic;
+using System.IO;
+using Newtonsoft.Json;
+using PixiEditor.Models.UserPreferences;
+using Xunit;
+
+namespace PixiEditorTests.ModelsTests.UserPreferencesTests
+{
+    public class PreferencesSettingsTests
+    {
+        public static string PathToPreferencesFile { get; } = Path.Join("PixiEditor", "test_preferences.json");
+
+        public PreferencesSettingsTests()
+        {
+            PreferencesSettings.Init(PathToPreferencesFile);
+        }
+
+        [Fact]
+        public void TestThatPreferencesSettingsIsLoaded()
+        {
+            Assert.True(PreferencesSettings.IsLoaded);
+        }
+
+        [Fact]
+        public void TestThatInitCreatesUserPreferencesJson()
+        {
+            Assert.True(File.Exists(PathToPreferencesFile));
+        }
+
+        [Theory]
+        [InlineData(-2)]
+        [InlineData(false)]
+        [InlineData("string")]
+        [InlineData(null)]
+        public void TestThatGetPreferenceOnNonExistingKeyReturnsFallbackValue<T>(T value)
+        {
+            T fallbackValue = value;
+            T preferenceValue = PreferencesSettings.GetPreference<T>("NonExistingPreference", fallbackValue);
+            Assert.Equal(fallbackValue, preferenceValue);
+        }
+
+        [Theory]
+        [InlineData("IntPreference", 1)]
+        [InlineData("BoolPreference", true)]
+        public void TestThatUpdatePreferenceUpdatesDictionary<T>(string name, T value)
+        {
+            PreferencesSettings.UpdatePreference(name, value);
+            Assert.Equal(value, PreferencesSettings.GetPreference<T>(name));
+        }
+
+        [Theory]
+        [InlineData("LongPreference", 1L)]
+        public void TestThatSaveUpdatesFile<T>(string name, T value)
+        {
+            PreferencesSettings.Preferences[name] = value;
+            PreferencesSettings.Save();
+            using (var fs = new FileStream(PathToPreferencesFile, FileMode.Open, FileAccess.Read, FileShare.Read))
+            {
+                using StreamReader sr = new StreamReader(fs);
+                string json = sr.ReadToEnd();
+                var dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
+                Assert.True(dict.ContainsKey(name));
+                Assert.Equal(value, dict[name]);
+            }
+        }
+    }
+}