Browse Source

Preferences wip

flabbet 1 year ago
parent
commit
efd77c096b

+ 5 - 1
samples/Preferences/PreferencesSampleExtension.cs

@@ -17,7 +17,11 @@ public class PreferencesSampleExtension : WasmExtension
     /// </summary>
     /// </summary>
     public override void OnInitialized()
     public override void OnInitialized()
     {
     {
-        Api.Preferences.UpdatePreference("SAM_PREF:SaidHello", true);
+        int helloCount = Api.Preferences.GetPreference<int>("HelloCount");
+
+        Api.Logger.Log($"Hello count: {helloCount}");
+
+        Api.Preferences.UpdatePreference("HelloCount", helloCount + 1);
         Api.Preferences.Save();
         Api.Preferences.Save();
     }
     }
 }
 }

+ 5 - 5
src/PixiEditor.Extensions.Wasm/Api/UserPreferences/Preferences.cs

@@ -20,27 +20,27 @@ public class Preferences : IPreferences
 
 
     public void UpdateLocalPreference<T>(string name, T value)
     public void UpdateLocalPreference<T>(string name, T value)
     {
     {
-        throw new NotImplementedException();
+        Interop.UpdateLocalUserPreference(name, value);
     }
     }
 
 
     public T GetPreference<T>(string name)
     public T GetPreference<T>(string name)
     {
     {
-        throw new NotImplementedException();
+        return Interop.GetPreference<T>(name, default);
     }
     }
 
 
     public T GetPreference<T>(string name, T fallbackValue)
     public T GetPreference<T>(string name, T fallbackValue)
     {
     {
-        throw new NotImplementedException();
+        return Interop.GetPreference<T>(name, fallbackValue);
     }
     }
 
 
     public T GetLocalPreference<T>(string name)
     public T GetLocalPreference<T>(string name)
     {
     {
-        throw new NotImplementedException();
+        return Interop.GetLocalPreference<T>(name, default);
     }
     }
 
 
     public T GetLocalPreference<T>(string name, T fallbackValue)
     public T GetLocalPreference<T>(string name, T fallbackValue)
     {
     {
-        throw new NotImplementedException();
+        return Interop.GetLocalPreference(name, fallbackValue);
     }
     }
 
 
 
 

+ 1 - 1
src/PixiEditor.Extensions.Wasm/ApiExportAttribute.cs

@@ -1,6 +1,6 @@
 namespace PixiEditor.Extensions.Wasm;
 namespace PixiEditor.Extensions.Wasm;
 
 
-public class ApiExportAttribute : Attribute
+internal class ApiExportAttribute : Attribute
 {
 {
     public string ExportName { get; }
     public string ExportName { get; }
 
 

+ 51 - 0
src/PixiEditor.Extensions.Wasm/Bridge/Interop.cs

@@ -25,4 +25,55 @@ internal static class Interop
                 throw new ArgumentException($"Unsupported type {value.GetType().Name}");
                 throw new ArgumentException($"Unsupported type {value.GetType().Name}");
         }
         }
     }
     }
+    
+    public static void UpdateLocalUserPreference<T>(string name, T value)
+    {
+        switch (value)
+        {
+            case int intValue:
+                Native.update_local_preference_int(name, intValue);
+                break;
+            case bool boolValue:
+                Native.update_local_preference_bool(name, boolValue);
+                break;
+            case string stringValue:
+                Native.update_local_preference_string(name, stringValue);
+                break;
+            case float floatValue:
+                Native.update_local_preference_float(name, floatValue);
+                break;
+            case double doubleValue:
+                Native.update_local_preference_double(name, doubleValue);
+                break;
+            default:
+                throw new ArgumentException($"Unsupported type {value.GetType().Name}");
+        }
+    }
+
+
+    public static T GetPreference<T>(string name, T fallbackValue)
+    {
+        return fallbackValue switch
+        {
+            int intFallback => (T)(object)Native.get_preference_int(name, intFallback),
+            bool boolFallback => (T)(object)Native.get_preference_bool(name, boolFallback),
+            string stringFallback => (T)(object)Native.get_preference_string(name, stringFallback),
+            float floatFallback => (T)(object)Native.get_preference_float(name, floatFallback),
+            double doubleFallback => (T)(object)Native.get_preference_double(name, doubleFallback),
+            _ => throw new ArgumentException($"Unsupported type {fallbackValue.GetType().Name}")
+        };
+    }
+    
+    public static T GetLocalPreference<T>(string name, T fallbackValue)
+    {
+        return fallbackValue switch
+        {
+            int intFallback => (T)(object)Native.get_local_preference_int(name, intFallback),
+            bool boolFallback => (T)(object)Native.get_local_preference_bool(name, boolFallback),
+            string stringFallback => (T)(object)Native.get_local_preference_string(name, stringFallback),
+            float floatFallback => (T)(object)Native.get_local_preference_float(name, floatFallback),
+            double doubleFallback => (T)(object)Native.get_local_preference_double(name, doubleFallback),
+            _ => throw new ArgumentException($"Unsupported type {fallbackValue.GetType().Name}")
+        };
+    }
 }
 }

+ 45 - 0
src/PixiEditor.Extensions.Wasm/Bridge/Native.Preferences.cs

@@ -22,4 +22,49 @@ internal static partial class Native
     
     
     [MethodImpl(MethodImplOptions.InternalCall)]
     [MethodImpl(MethodImplOptions.InternalCall)]
     public static extern void update_preference_double(string name, double value);
     public static extern void update_preference_double(string name, double value);
+    
+    [MethodImpl(MethodImplOptions.InternalCall)]
+    public static extern void update_local_preference_int(string name, int value);
+    
+    [MethodImpl(MethodImplOptions.InternalCall)]
+    public static extern void update_local_preference_bool(string name, bool value);
+    
+    [MethodImpl(MethodImplOptions.InternalCall)]
+    public static extern void update_local_preference_string(string name, string value);
+    
+    [MethodImpl(MethodImplOptions.InternalCall)]
+    public static extern void update_local_preference_float(string name, float value);
+    
+    [MethodImpl(MethodImplOptions.InternalCall)]
+    public static extern void update_local_preference_double(string name, double value);
+
+    [MethodImpl(MethodImplOptions.InternalCall)]
+    public static extern int get_preference_int(string name, int fallbackInt);
+    
+    [MethodImpl(MethodImplOptions.InternalCall)]
+    public static extern bool get_preference_bool(string name, bool fallbackBool);
+    
+    [MethodImpl(MethodImplOptions.InternalCall)]
+    public static extern string get_preference_string(string name, string fallbackString);
+    
+    [MethodImpl(MethodImplOptions.InternalCall)]
+    public static extern float get_preference_float(string name, float fallbackFloat);
+    
+    [MethodImpl(MethodImplOptions.InternalCall)]
+    public static extern double get_preference_double(string name, double fallbackDouble);
+    
+    [MethodImpl(MethodImplOptions.InternalCall)]
+    public static extern int get_local_preference_int(string name, int fallbackInt);
+    
+    [MethodImpl(MethodImplOptions.InternalCall)]
+    public static extern bool get_local_preference_bool(string name, bool fallbackBool);
+    
+    [MethodImpl(MethodImplOptions.InternalCall)]
+    public static extern string get_local_preference_string(string name, string fallbackString);
+    
+    [MethodImpl(MethodImplOptions.InternalCall)]
+    public static extern float get_local_preference_float(string name, float fallbackFloat);
+    
+    [MethodImpl(MethodImplOptions.InternalCall)]
+    public static extern double get_local_preference_double(string name, double fallbackDouble);
 }
 }

+ 0 - 1
src/PixiEditor.Extensions.Wasm/PixiEditorApi.cs

@@ -10,7 +10,6 @@ public class PixiEditorApi
 {
 {
     public Logger Logger { get; }
     public Logger Logger { get; }
     public WindowProvider WindowProvider { get; }
     public WindowProvider WindowProvider { get; }
-    
     public Preferences Preferences { get; }
     public Preferences Preferences { get; }
 
 
     public PixiEditorApi()
     public PixiEditorApi()

+ 90 - 0
src/PixiEditor.Extensions.WasmRuntime/Api/PreferencesApi.cs

@@ -37,4 +37,94 @@ internal class PreferencesApi : ApiGroupHandler
     {
     {
         Api.Preferences.UpdatePreference(name, value);
         Api.Preferences.UpdatePreference(name, value);
     }
     }
+    
+    [ApiFunction("update_local_preference_int")]
+    public void UpdateLocalPreference(string name, int value)
+    {
+        Api.Preferences.UpdateLocalPreference(name, value);
+    }
+    
+    [ApiFunction("update_local_preference_bool")]
+    public void UpdateLocalPreference(string name, bool value)
+    {
+        Api.Preferences.UpdateLocalPreference(name, value);
+    }
+    
+    [ApiFunction("update_local_preference_string")]
+    public void UpdateLocalPreference(string name, string value)
+    {
+        Api.Preferences.UpdateLocalPreference(name, value);
+    }
+    
+    [ApiFunction("update_local_preference_float")]
+    public void UpdateLocalPreference(string name, float value)
+    {
+        Api.Preferences.UpdateLocalPreference(name, value);
+    }
+    
+    [ApiFunction("update_local_preference_double")]
+    public void UpdateLocalPreference(string name, double value)
+    {
+        Api.Preferences.UpdateLocalPreference(name, value);
+    }
+    
+    [ApiFunction("get_preference_int")]
+    public int GetPreference(string name, int fallbackValue)
+    {
+        return Api.Preferences.GetPreference(name, fallbackValue);
+    }
+    
+    [ApiFunction("get_preference_bool")]
+    public bool GetPreference(string name, bool fallbackValue)
+    {
+        return Api.Preferences.GetPreference(name, fallbackValue);
+    }
+    
+    [ApiFunction("get_preference_string")]
+    public string GetPreference(string name, string fallbackValue)
+    {
+        return Api.Preferences.GetPreference(name, fallbackValue);
+    }
+    
+    [ApiFunction("get_preference_float")]
+    public float GetPreference(string name, float fallbackValue)
+    {
+        return Api.Preferences.GetPreference(name, fallbackValue);
+    }
+    
+    [ApiFunction("get_preference_double")]
+    public double GetPreference(string name, double fallbackValue)
+    {
+        return Api.Preferences.GetPreference(name, fallbackValue);
+    }
+    
+    [ApiFunction("get_local_preference_int")]
+    public int GetLocalPreference(string name, int fallbackValue)
+    {
+        return Api.Preferences.GetLocalPreference(name, fallbackValue);
+    }
+    
+    [ApiFunction("get_local_preference_bool")]
+    public bool GetLocalPreference(string name, bool fallbackValue)
+    {
+        return Api.Preferences.GetLocalPreference(name, fallbackValue);
+    }
+    
+    [ApiFunction("get_local_preference_string")]
+    public string GetLocalPreference(string name, string fallbackValue)
+    {
+        return Api.Preferences.GetLocalPreference(name, fallbackValue);
+    }
+    
+    [ApiFunction("get_local_preference_float")]
+    public float GetLocalPreference(string name, float fallbackValue)
+    {
+        return Api.Preferences.GetLocalPreference(name, fallbackValue);
+    }
+    
+    [ApiFunction("get_local_preference_double")]
+    public double GetLocalPreference(string name, double fallbackValue)
+    {
+        return Api.Preferences.GetLocalPreference(name, fallbackValue);
+    }
 }
 }