Browse Source

Hopefully add PixiEditor Settings support to extensions

CPKreuz 1 year ago
parent
commit
74ad3afbd8

+ 26 - 0
src/PixiEditor.AvaloniaUI/Models/Preferences/PreferencesSettings.cs

@@ -42,6 +42,8 @@ internal class PreferencesSettings : IPreferences
 
 
     public void UpdatePreference<T>(string name, T value)
     public void UpdatePreference<T>(string name, T value)
     {
     {
+        name = TrimPrefix(name);
+
         if (IsLoaded == false)
         if (IsLoaded == false)
         {
         {
             Init();
             Init();
@@ -62,6 +64,8 @@ internal class PreferencesSettings : IPreferences
 
 
     public void UpdateLocalPreference<T>(string name, T value)
     public void UpdateLocalPreference<T>(string name, T value)
     {
     {
+        name = TrimPrefix(name);
+
         if (IsLoaded == false)
         if (IsLoaded == false)
         {
         {
             Init();
             Init();
@@ -95,6 +99,8 @@ internal class PreferencesSettings : IPreferences
 
 
     public void AddCallback(string name, Action<object> action)
     public void AddCallback(string name, Action<object> action)
     {
     {
+        name = TrimPrefix(name);
+
         if (action == null)
         if (action == null)
         {
         {
             throw new ArgumentNullException(nameof(action));
             throw new ArgumentNullException(nameof(action));
@@ -111,6 +117,8 @@ internal class PreferencesSettings : IPreferences
 
 
     public void AddCallback<T>(string name, Action<T> action)
     public void AddCallback<T>(string name, Action<T> action)
     {
     {
+        name = TrimPrefix(name);
+
         if (action == null)
         if (action == null)
         {
         {
             throw new ArgumentNullException(nameof(action));
             throw new ArgumentNullException(nameof(action));
@@ -121,6 +129,8 @@ internal class PreferencesSettings : IPreferences
 
 
     public void RemoveCallback(string name, Action<object> action)
     public void RemoveCallback(string name, Action<object> action)
     {
     {
+        name = TrimPrefix(name);
+
         if (action == null)
         if (action == null)
         {
         {
             throw new ArgumentNullException(nameof(action));
             throw new ArgumentNullException(nameof(action));
@@ -134,6 +144,8 @@ internal class PreferencesSettings : IPreferences
 
 
     public void RemoveCallback<T>(string name, Action<T> action)
     public void RemoveCallback<T>(string name, Action<T> action)
     {
     {
+        name = TrimPrefix(name);
+
         if (action == null)
         if (action == null)
         {
         {
             throw new ArgumentNullException(nameof(action));
             throw new ArgumentNullException(nameof(action));
@@ -146,11 +158,15 @@ internal class PreferencesSettings : IPreferences
 
 
     public T? GetPreference<T>(string name)
     public T? GetPreference<T>(string name)
     {
     {
+        name = TrimPrefix(name);
+
         return GetPreference(name, default(T));
         return GetPreference(name, default(T));
     }
     }
 
 
     public T? GetPreference<T>(string name, T? fallbackValue)
     public T? GetPreference<T>(string name, T? fallbackValue)
     {
     {
+        name = TrimPrefix(name);
+
         if (IsLoaded == false)
         if (IsLoaded == false)
         {
         {
             Init();
             Init();
@@ -171,11 +187,15 @@ internal class PreferencesSettings : IPreferences
 
 
     public T? GetLocalPreference<T>(string name)
     public T? GetLocalPreference<T>(string name)
     {
     {
+        name = TrimPrefix(name);
+
         return GetLocalPreference(name, default(T));
         return GetLocalPreference(name, default(T));
     }
     }
 
 
     public T? GetLocalPreference<T>(string name, T? fallbackValue)
     public T? GetLocalPreference<T>(string name, T? fallbackValue)
     {
     {
+        name = TrimPrefix(name);
+        
         if (IsLoaded == false)
         if (IsLoaded == false)
         {
         {
             Init();
             Init();
@@ -196,6 +216,8 @@ internal class PreferencesSettings : IPreferences
 
 
     private T? GetValue<T>(Dictionary<string, object> dict, string name, T? fallbackValue)
     private T? GetValue<T>(Dictionary<string, object> dict, string name, T? fallbackValue)
     {
     {
+        name = TrimPrefix(name);
+        
         if (!dict.ContainsKey(name)) return fallbackValue;
         if (!dict.ContainsKey(name)) return fallbackValue;
         var preference = dict[name];
         var preference = dict[name];
         if (typeof(T) == preference.GetType()) return (T)preference;
         if (typeof(T) == preference.GetType()) return (T)preference;
@@ -250,4 +272,8 @@ internal class PreferencesSettings : IPreferences
 
 
         return new Dictionary<string, object>();
         return new Dictionary<string, object>();
     }
     }
+
+    private const string Prefix = "PixiEditor:";
+
+    private string TrimPrefix(string value) => value.StartsWith("PixiEditor:") ? value[Prefix.Length..] : value;
 }
 }

+ 23 - 22
src/PixiEditor.Extensions.CommonApi/UserPreferences/Settings/PixiEditorSettings.cs

@@ -2,66 +2,67 @@
 
 
 public static class PixiEditorSettings
 public static class PixiEditorSettings
 {
 {
+    private const string PixiEditor = "PixiEditor";
+    
     public static class Palettes
     public static class Palettes
     {
     {
-        public static LocalSetting<IEnumerable<string>> FavouritePalettes { get; } = LocalSetting.Owned<IEnumerable<string>>();
+        public static LocalSetting<IEnumerable<string>> FavouritePalettes { get; } = LocalSetting.NonOwned<IEnumerable<string>>(PixiEditor);
     }
     }
 
 
     public static class Update
     public static class Update
     {
     {
-        public static SyncedSetting<bool> CheckUpdatesOnStartup { get; } = SyncedSetting.Owned(true);
+        public static SyncedSetting<bool> CheckUpdatesOnStartup { get; } = SyncedSetting.NonOwned(PixiEditor, true);
 
 
-        public static SyncedSetting<string> UpdateChannel { get; } = SyncedSetting.Owned<string>();
+        public static SyncedSetting<string> UpdateChannel { get; } = SyncedSetting.NonOwned<string>(PixiEditor);
     }
     }
 
 
     public static class Debug
     public static class Debug
     {
     {
-        public static SyncedSetting<bool> IsDebugModeEnabled { get; } = SyncedSetting.Owned<bool>();
+        public static SyncedSetting<bool> IsDebugModeEnabled { get; } = SyncedSetting.NonOwned<bool>(PixiEditor);
 
 
-        public static LocalSetting<string> PoEditorApiKey { get; } = new("POEditor_API_Key");
+        public static LocalSetting<string> PoEditorApiKey { get; } = new($"{PixiEditor}:POEditor_API_Key");
     }
     }
     
     
     public static class Tools
     public static class Tools
     {
     {
-        public static SyncedSetting<bool> EnableSharedToolbar { get; } = SyncedSetting.Owned<bool>();
+        public static SyncedSetting<bool> EnableSharedToolbar { get; } = SyncedSetting.NonOwned<bool>(PixiEditor);
 
 
         // TODO: Use RightClickMode
         // TODO: Use RightClickMode
-        public static SyncedSetting<object> RightClickMode { get; } = SyncedSetting.Owned<object>(0);
+        public static SyncedSetting<object> RightClickMode { get; } = SyncedSetting.NonOwned<object>(PixiEditor, 0);
         
         
-        public static SyncedSetting<bool> IsPenModeEnabled { get; } = SyncedSetting.Owned<bool>();
+        public static SyncedSetting<bool> IsPenModeEnabled { get; } = SyncedSetting.NonOwned<bool>(PixiEditor);
     }
     }
 
 
     public static class File
     public static class File
     {
     {
-        public static SyncedSetting<int> DefaultNewFileWidth { get; } = SyncedSetting.Owned(64);
+        public static SyncedSetting<int> DefaultNewFileWidth { get; } = SyncedSetting.NonOwned(PixiEditor, 64);
 
 
-        public static SyncedSetting<int> DefaultNewFileHeight { get; } = SyncedSetting.Owned(64);
+        public static SyncedSetting<int> DefaultNewFileHeight { get; } = SyncedSetting.NonOwned(PixiEditor, 64);
         
         
-        public static LocalSetting<IEnumerable<string>> RecentlyOpened { get; } = LocalSetting.Owned<IEnumerable<string>>();
+        public static LocalSetting<IEnumerable<string>> RecentlyOpened { get; } = LocalSetting.NonOwned<IEnumerable<string>>(PixiEditor);
     
     
-        public static SyncedSetting<int> MaxOpenedRecently { get; } = SyncedSetting.Owned(8);
+        public static SyncedSetting<int> MaxOpenedRecently { get; } = SyncedSetting.NonOwned(PixiEditor, 8);
     }
     }
     
     
     public static class StartupWindow
     public static class StartupWindow
     {
     {
-        public static SyncedSetting<bool> ShowStartupWindow { get; } = SyncedSetting.Owned(true);
+        public static SyncedSetting<bool> ShowStartupWindow { get; } = SyncedSetting.NonOwned(PixiEditor, true);
 
 
-        public static SyncedSetting<bool> DisableNewsPanel { get; } = SyncedSetting.Owned<bool>();
-    
-        public static SyncedSetting<bool> NewsPanelCollapsed { get; } = SyncedSetting.Owned<bool>();
+        public static SyncedSetting<bool> DisableNewsPanel { get; } = SyncedSetting.NonOwned<bool>(PixiEditor);
 
 
-        public static SyncedSetting<IEnumerable<int>> LastCheckedNewsIds { get; } = SyncedSetting.Owned<IEnumerable<int>>();
+        public static SyncedSetting<bool> NewsPanelCollapsed { get; } = SyncedSetting.NonOwned<bool>(PixiEditor);
+
+        public static SyncedSetting<IEnumerable<int>> LastCheckedNewsIds { get; } = SyncedSetting.NonOwned<IEnumerable<int>>(PixiEditor);
     }
     }
     
     
-    
     public static class Discord
     public static class Discord
     {
     {
-        public static SyncedSetting<bool> EnableRichPresence { get; } = SyncedSetting.Owned(true);
+        public static SyncedSetting<bool> EnableRichPresence { get; } = SyncedSetting.NonOwned(PixiEditor, true);
 
 
-        public static SyncedSetting<bool> ShowDocumentName { get; } = SyncedSetting.Owned<bool>();
+        public static SyncedSetting<bool> ShowDocumentName { get; } = SyncedSetting.NonOwned<bool>(PixiEditor);
 
 
-        public static SyncedSetting<bool> ShowDocumentSize { get; } = SyncedSetting.Owned(true);
+        public static SyncedSetting<bool> ShowDocumentSize { get; } = SyncedSetting.NonOwned(PixiEditor, true);
 
 
-        public static SyncedSetting<bool> ShowLayerCount { get; } = SyncedSetting.Owned(true);
+        public static SyncedSetting<bool> ShowLayerCount { get; } = SyncedSetting.NonOwned(PixiEditor, true);
     }
     }
 }
 }