CPKreuz 1 год назад
Родитель
Сommit
f3fd30e31f

+ 13 - 6
src/PixiEditor.Extensions/Common/UserPreferences/PreferencesConstants.cs

@@ -2,12 +2,19 @@
 
 public static class PreferencesConstants
 {
-    public const string FavouritePalettes = "FavouritePalettes";
-    public const string RecentlyOpened = "RecentlyOpened";
+    public const string FavouritePalettes = nameof(FavouritePalettes);
+    
+    public const string RecentlyOpened = nameof(RecentlyOpened);
 
-    public const string MaxOpenedRecently = "MaxOpenedRecently";
+    public const string MaxOpenedRecently = nameof(MaxOpenedRecently);
     public const int MaxOpenedRecentlyDefault = 8;
-    public const string DisableNewsPanel = "DisableNewsPanel";
-    public const string LastCheckedNewsIds = "LastCheckedNewsIds";
-    public const string NewsPanelCollapsed = "NewsPanelCollapsed";
+    
+    public const string DisableNewsPanel = nameof(DisableNewsPanel);
+    
+    public const string LastCheckedNewsIds = nameof(LastCheckedNewsIds);
+    
+    public const string NewsPanelCollapsed = nameof(NewsPanelCollapsed);
+    
+    public const string AutosavePeriodMinutes = nameof(AutosavePeriodMinutes);
+    public const double AutosavePeriodDefault = 3;
 }

+ 6 - 1
src/PixiEditor/Data/Localization/Languages/en.json

@@ -599,6 +599,11 @@
   "AUTOSAVE_PLEASE_RESAVE": "Error while saving. Save file manually to enable autosaving.",
   "AUTOSAVE_FAILED_RETRYING": "Error while saving. Retrying in {0} {1}.",
   
+  "AUTOSAVE_SETTINGS_PERIOD": "Autosave every",
+  
+  "DISABLED": "Disabled",
+  
   "MINUTE_SINGULAR": "minute",
-  "MINUTE_PLURAL": "minutes"
+  "MINUTE_PLURAL": "minutes",
+  "MINUTE_UNIVERSAL": "minute(s)"
 }

+ 28 - 0
src/PixiEditor/Helpers/Converters/AutosaveSettingsPeriodToValueConverter.cs

@@ -0,0 +1,28 @@
+using System.Globalization;
+using PixiEditor.Extensions.Common.Localization;
+using PixiEditor.Helpers.Converters;
+
+namespace PixiEditor.Helpers.Converters;
+
+internal class AutosaveSettingsPeriodToValueConverter : SingleInstanceConverter<AutosaveSettingsPeriodToValueConverter>
+{
+    public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
+    {
+        return value switch
+        {
+            -1 => new LocalizedString("DISABLED"),
+            double d => new LocalizedString(d.ToString(CultureInfo.InvariantCulture)),
+            _ => throw new ArgumentException($"{value} has invalid type")
+        };
+    }
+
+    public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
+    {
+        return value switch
+        {
+            LocalizedString { Key: "DISABLED" } => -1,
+            LocalizedString s when double.TryParse(s.Key, out double period) => period,
+            _ => throw new ArgumentException($"{value} has invalid type")
+        };
+    }
+}

+ 12 - 6
src/PixiEditor/ViewModels/SubViewModels/Document/AutosaveViewModel.cs

@@ -34,7 +34,7 @@ internal class AutosaveViewModel : NotifyableObject
         Document = document;
         tempGuid = Guid.NewGuid();
         savingTimer = new Timer();
-        updateTextTimer = new Timer(TimeSpan.FromSeconds(5));
+        updateTextTimer = new Timer(TimeSpan.FromSeconds(10));
 
         savingTimer.Elapsed += (_, _) => TryAutosave();
         savingTimer.AutoReset = false;
@@ -43,8 +43,8 @@ internal class AutosaveViewModel : NotifyableObject
 
         var preferences = IPreferences.Current;
         
-        preferences.AddCallback<double>(nameof(AutosavePeriodMinutes), AutosavePeriodChanged);
-        AutosavePeriodChanged(preferences.GetPreference(nameof(AutosavePeriodMinutes), TimeSpan.FromMinutes(3).TotalMinutes));
+        preferences.AddCallback<double>(PreferencesConstants.AutosavePeriodMinutes, AutosavePeriodChanged);
+        AutosavePeriodChanged(preferences.GetPreference(PreferencesConstants.AutosavePeriodMinutes, PreferencesConstants.AutosavePeriodDefault));
         SetAutosaveText();
         
         savingTimer.Start();
@@ -70,12 +70,14 @@ internal class AutosaveViewModel : NotifyableObject
             UpdateMainMenuTextSave("AUTOSAVE_SAVING_IN_MINUTE");
             return;
         }
-            
-        var minute = timeLeft.Minutes < 2
+
+        var adjusted = timeLeft.Add(TimeSpan.FromSeconds(30));
+        
+        var minute = adjusted.Minutes < 2
             ? new LocalizedString("MINUTE_SINGULAR")
             : new LocalizedString("MINUTE_PLURAL");
 
-        UpdateMainMenuTextSave(new LocalizedString("AUTOSAVE_SAVING_IN", timeLeft.Minutes.ToString(), minute));
+        UpdateMainMenuTextSave(new LocalizedString("AUTOSAVE_SAVING_IN", adjusted.Minutes.ToString(), minute));
     }
 
     private void TryAutosave()
@@ -192,6 +194,10 @@ internal class AutosaveViewModel : NotifyableObject
         savingTimer.Enabled = timerEnabled;
         
         nextSave = DateTime.Now + timeSpan;
+        if (updateTextTimer.Enabled)
+        {
+            SetAutosaveText();
+        }
     }
 
     private void UpdateMainMenuTextSave(LocalizedString text)

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

@@ -52,6 +52,14 @@ internal class FileSettings : SettingsGroup
     public bool DisableNewsPanel
     {
         get => disableNewsPanel;
-        set => RaiseAndUpdatePreference(ref disableNewsPanel, value, PreferencesConstants.DisableNewsPanel);
+        set => RaiseAndUpdatePreference(ref disableNewsPanel, value);
+    }
+
+    private double autosavePeriodMinutes = GetPreference(PreferencesConstants.AutosavePeriodMinutes, PreferencesConstants.AutosavePeriodDefault);
+    
+    public double AutosavePeriodMinutes
+    {
+        get => autosavePeriodMinutes;
+        set => RaiseAndUpdatePreference(ref autosavePeriodMinutes, value);
     }
 }

+ 20 - 0
src/PixiEditor/Views/Dialogs/SettingsWindow.xaml

@@ -19,6 +19,7 @@
         xmlns:helpers="clr-namespace:PixiEditor.Helpers"
         xmlns:ui="clr-namespace:PixiEditor.Extensions.UI;assembly=PixiEditor.Extensions"
         xmlns:enums="clr-namespace:PixiEditor.Models.Enums"
+        xmlns:localization="clr-namespace:PixiEditor.Extensions.Common.Localization;assembly=PixiEditor.Extensions"
         mc:Ignorable="d"
         Name="window" 
         Height="688" Width="780"
@@ -132,6 +133,25 @@
                                    Value="{Binding SettingsSubViewModel.File.MaxOpenedRecently, Mode=TwoWay}" Height="19" Width="40"/>
                 </StackPanel>
 
+                <StackPanel Margin="27 10 27 0" Orientation="Horizontal">
+                    <Label Style="{StaticResource SettingsText}"
+                           ui:Translator.Key="AUTOSAVE_SETTINGS_PERIOD"/>
+                    <ComboBox SelectedItem="{Binding SettingsSubViewModel.File.AutosavePeriodMinutes, Mode=TwoWay, Converter={converters:AutosaveSettingsPeriodToValueConverter}}"
+                              MinWidth="100" Margin="7, 0">
+                        <ComboBox.ItemsSource>
+                            <x:Array Type="localization:LocalizedString">
+                                <localization:LocalizedString Key="DISABLED" />
+                                <localization:LocalizedString Key="1" />
+                                <localization:LocalizedString Key="3" />
+                                <localization:LocalizedString Key="5" />
+                                <localization:LocalizedString Key="10" />
+                            </x:Array>
+                        </ComboBox.ItemsSource>
+                    </ComboBox>
+                    <Label Style="{StaticResource SettingsText}"
+                           ui:Translator.Key="MINUTE_UNIVERSAL"/>
+                </StackPanel>
+
                 <Label Style="{StaticResource SettingsHeader}" d:Content="Default new file size" ui:Translator.Key="DEFAULT_NEW_SIZE"/>
 
                 <StackPanel Orientation="Horizontal" Margin="27 5">