Browse Source

Added AddCallback<T> to Preferences

CPKreuz 4 years ago
parent
commit
63cf2a279d

+ 3 - 1
PixiEditor/Models/UserPreferences/IPreferences.cs

@@ -9,7 +9,9 @@ namespace PixiEditor.Models.UserPreferences
 
 
         public void Save();
         public void Save();
 
 
-        public void AddCallback(string setting, Action<object> action);
+        public void AddCallback(string name, Action<object> action);
+
+        public void AddCallback<T>(string name, Action<T> action);
 
 
         public void Init();
         public void Init();
 
 

+ 19 - 4
PixiEditor/Models/UserPreferences/PreferencesSettings.cs

@@ -94,15 +94,30 @@ namespace PixiEditor.Models.UserPreferences
 
 
         public Dictionary<string, List<Action<object>>> Callbacks { get; set; } = new Dictionary<string, List<Action<object>>>();
         public Dictionary<string, List<Action<object>>> Callbacks { get; set; } = new Dictionary<string, List<Action<object>>>();
 
 
-        public void AddCallback(string setting, Action<object> action)
+        public void AddCallback(string name, Action<object> action)
         {
         {
-            if (Callbacks.ContainsKey(setting))
+            if (action == null)
             {
             {
-                Callbacks[setting].Add(action);
+                throw new ArgumentNullException(nameof(action));
+            }
+
+            if (Callbacks.ContainsKey(name))
+            {
+                Callbacks[name].Add(action);
                 return;
                 return;
             }
             }
 
 
-            Callbacks.Add(setting, new List<Action<object>>() { action });
+            Callbacks.Add(name, new List<Action<object>>() { action });
+        }
+
+        public void AddCallback<T>(string name, Action<T> action)
+        {
+            if (action == null)
+            {
+                throw new ArgumentNullException(nameof(action));
+            }
+
+            AddCallback(name, new Action<object>(o => action((T)o)));
         }
         }
 
 
 #nullable enable
 #nullable enable

+ 2 - 2
PixiEditor/Views/MainWindow.xaml.cs

@@ -48,9 +48,9 @@ namespace PixiEditor
             Application.Current.ShutdownMode = ShutdownMode.OnMainWindowClose;
             Application.Current.ShutdownMode = ShutdownMode.OnMainWindowClose;
 
 
             DataContext.BitmapManager.DocumentChanged += BitmapManager_DocumentChanged;
             DataContext.BitmapManager.DocumentChanged += BitmapManager_DocumentChanged;
-            preferences.AddCallback("ImagePreviewInTaskbar", x =>
+            preferences.AddCallback<bool>("ImagePreviewInTaskbar", x =>
             {
             {
-                if ((bool)x)
+                if (x)
                 {
                 {
                     UpdateTaskbarIcon(DataContext.BitmapManager.ActiveDocument);
                     UpdateTaskbarIcon(DataContext.BitmapManager.ActiveDocument);
                 }
                 }

+ 4 - 0
PixiEditorTests/Mocks/PreferenceSettingsMock.cs

@@ -9,6 +9,10 @@ namespace PixiEditorTests.Mocks
         {
         {
         }
         }
 
 
+        public void AddCallback<T>(string name, Action<T> action)
+        {
+        }
+
 #nullable enable
 #nullable enable
 
 
         public T? GetLocalPreference<T>(string name)
         public T? GetLocalPreference<T>(string name)