Browse Source

Added helper classes for creating owned and non-owned settings without nameof

CPKreuz 1 year ago
parent
commit
e092be5bfa

+ 15 - 1
src/PixiEditor.Extensions.CommonApi/UserPreferences/Settings/LocalSetting.cs

@@ -1,4 +1,18 @@
-namespace PixiEditor.Extensions.CommonApi.UserPreferences.Settings;
+using System.Runtime.CompilerServices;
+
+namespace PixiEditor.Extensions.CommonApi.UserPreferences.Settings;
+
+/// <summary>
+/// A static class for creating a LocalSetting from the property name
+/// </summary>
+public static class LocalSetting
+{
+    public static LocalSetting<T> Owned<T>(T? fallbackValue = default, [CallerMemberName] string name = "") =>
+        new(name, fallbackValue);
+    
+    public static LocalSetting<T> NonOwned<T>(string prefix, T? fallbackValue = default, [CallerMemberName] string name = "") =>
+        new($"{prefix}:{name}", fallbackValue);
+}
 
 
 /// <summary>
 /// <summary>
 /// A preference which will only be available on the current device
 /// A preference which will only be available on the current device

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

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

+ 15 - 1
src/PixiEditor.Extensions.CommonApi/UserPreferences/Settings/SyncedSetting.cs

@@ -1,4 +1,18 @@
-namespace PixiEditor.Extensions.CommonApi.UserPreferences.Settings;
+using System.Runtime.CompilerServices;
+
+namespace PixiEditor.Extensions.CommonApi.UserPreferences.Settings;
+
+/// <summary>
+/// A static class for creating a LocalSetting from the property name
+/// </summary>
+public static class SyncedSetting
+{
+    public static SyncedSetting<T> Owned<T>(T? fallbackValue = default, [CallerMemberName] string name = "") =>
+        new(name, fallbackValue);
+    
+    public static SyncedSetting<T> NonOwned<T>(string prefix, T? fallbackValue = default, [CallerMemberName] string name = "") =>
+        new($"{prefix}:{name}", fallbackValue);
+}
 
 
 /// <summary>
 /// <summary>
 /// A preference which may be synced across multiple devices
 /// A preference which may be synced across multiple devices