From 3031177b29a1480204617dca7703a892e6163822 Mon Sep 17 00:00:00 2001 From: "huayong.xu" Date: Wed, 23 Mar 2022 18:57:37 +0800 Subject: [PATCH] Make PropertyMap look like Dictionary. --- .../src/internal/Interop/Interop.PropertyMap.cs | 6 + src/Tizen.NUI/src/public/Common/PropertyMap.cs | 160 ++++++++++++++++++--- .../Tizen.NUI.Samples/Samples/PropertyMapSample.cs | 64 +++++++++ 3 files changed, 207 insertions(+), 23 deletions(-) mode change 100644 => 100755 src/Tizen.NUI/src/public/Common/PropertyMap.cs create mode 100755 test/Tizen.NUI.Samples/Tizen.NUI.Samples/Samples/PropertyMapSample.cs diff --git a/src/Tizen.NUI/src/internal/Interop/Interop.PropertyMap.cs b/src/Tizen.NUI/src/internal/Interop/Interop.PropertyMap.cs index 099c3df..3a44d7a 100755 --- a/src/Tizen.NUI/src/internal/Interop/Interop.PropertyMap.cs +++ b/src/Tizen.NUI/src/internal/Interop/Interop.PropertyMap.cs @@ -49,6 +49,12 @@ namespace Tizen.NUI [global::System.Runtime.InteropServices.DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_Property_Map_Add__SWIG_2")] public static extern global::System.IntPtr Add(global::System.Runtime.InteropServices.HandleRef jarg1, int jarg2, global::System.Runtime.InteropServices.HandleRef jarg3); + [global::System.Runtime.InteropServices.DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_Property_Map_Remove__SWIG_0")] + public static extern bool Remove(global::System.Runtime.InteropServices.HandleRef jarg1, int jarg2); + + [global::System.Runtime.InteropServices.DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_Property_Map_Remove__SWIG_1")] + public static extern bool Remove(global::System.Runtime.InteropServices.HandleRef jarg1, string jarg2); + [global::System.Runtime.InteropServices.DllImport(NDalicPINVOKE.Lib, EntryPoint = "CSharp_Dali_Property_Map_GetValue")] public static extern global::System.IntPtr GetValue(global::System.Runtime.InteropServices.HandleRef jarg1, uint jarg2); diff --git a/src/Tizen.NUI/src/public/Common/PropertyMap.cs b/src/Tizen.NUI/src/public/Common/PropertyMap.cs old mode 100644 new mode 100755 index 004c326..c03c8ce --- a/src/Tizen.NUI/src/public/Common/PropertyMap.cs +++ b/src/Tizen.NUI/src/public/Common/PropertyMap.cs @@ -14,6 +14,7 @@ * limitations under the License. * */ +using System.Collections.Generic; using System.ComponentModel; namespace Tizen.NUI @@ -24,7 +25,6 @@ namespace Tizen.NUI /// 3 public class PropertyMap : Disposable { - /// /// The constructor. /// @@ -49,6 +49,42 @@ namespace Tizen.NUI } /// + /// Retrieves all keys. + /// + /// A list of keys. + [EditorBrowsable(EditorBrowsableState.Never)] + public IList Keys + { + get + { + List keys = new List(); + for (uint i = 0; i < Count(); i++) + { + keys.Add(GetKeyAt(i)); + } + return keys; + } + } + + /// + /// Retrieves all values. + /// + /// A list of values. + [EditorBrowsable(EditorBrowsableState.Never)] + public IList Values + { + get + { + List values = new List(); + for (uint i = 0; i < Count(); i++) + { + values.Add(GetValue(i)); + } + return values; + } + } + + /// /// The operator to access the element with the specified string key.
/// If an element with the key does not exist, then it is created.
///
@@ -61,9 +97,12 @@ namespace Tizen.NUI { return ValueOfIndex(key); } - internal set + set { - SetValue(key, value); + using (PropertyKey pKey = new PropertyKey(key)) + { + SetValue(pKey, value); + } } } @@ -80,9 +119,12 @@ namespace Tizen.NUI { return ValueOfIndex(key); } - internal set + set { - SetValue(key, value); + using (PropertyKey pKey = new PropertyKey(key)) + { + SetValue(pKey, value); + } } } @@ -112,33 +154,43 @@ namespace Tizen.NUI /// /// Inserts the key-value pair in the map, with the key type as string.
- /// Does not check for duplicates.
+ /// The exception would be thrown if the pair with the same key already exists. ///
/// The key to insert. /// The value to insert. /// 3 public void Insert(string key, PropertyValue value) { + global::System.IntPtr cPtr = Interop.PropertyMap.Find(SwigCPtr, key); + if (cPtr != global::System.IntPtr.Zero) + { + throw new global::System.ArgumentException($"The key {key} already exists.", nameof(key)); + } Interop.PropertyMap.Insert(SwigCPtr, key, PropertyValue.getCPtr(value)); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); } /// /// Inserts the key-value pair in the map, with the key type as index.
- /// Does not check for duplicates.
+ /// The exception would be thrown if the pair with the same key already exists. ///
/// The key to insert. /// The value to insert. /// 3 public void Insert(int key, PropertyValue value) { + global::System.IntPtr cPtr = Interop.PropertyMap.Find(SwigCPtr, key); + if (cPtr != global::System.IntPtr.Zero) + { + throw new global::System.ArgumentException($"The key {key} already exists.", nameof(key)); + } Interop.PropertyMap.Insert(SwigCPtr, key, PropertyValue.getCPtr(value)); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); } /// /// Inserts the key-value pair in the map, with the key type as string.
- /// Does not check for duplicates.
+ /// The exception would be thrown if the pair with the same key already exists. ///
/// The key to insert. /// The value to insert. @@ -146,6 +198,11 @@ namespace Tizen.NUI /// 3 public PropertyMap Add(string key, PropertyValue value) { + global::System.IntPtr cPtr = Interop.PropertyMap.Find(SwigCPtr, key); + if (cPtr != global::System.IntPtr.Zero) + { + throw new global::System.ArgumentException($"The key {key} already exists.", nameof(key)); + } Interop.PropertyMap.Add(SwigCPtr, key, PropertyValue.getCPtr(value)); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return this; @@ -153,7 +210,7 @@ namespace Tizen.NUI /// /// Inserts the key-value pair in the map, with the key type as string.
- /// Does not check for duplicates.
+ /// The exception would be thrown if the pair with the same key already exists. ///
/// The key to insert. /// The value to insert. @@ -161,6 +218,11 @@ namespace Tizen.NUI /// 3 public PropertyMap Add(int key, PropertyValue value) { + global::System.IntPtr cPtr = Interop.PropertyMap.Find(SwigCPtr, key); + if (cPtr != global::System.IntPtr.Zero) + { + throw new global::System.ArgumentException($"The key {key} already exists.", nameof(key)); + } Interop.PropertyMap.Add(SwigCPtr, key, PropertyValue.getCPtr(value)); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return this; @@ -168,7 +230,7 @@ namespace Tizen.NUI /// /// Inserts the keyvalue to the map.
- /// Does not check for duplicates.
+ /// The exception would be thrown if the pair with the same key already exists. ///
/// The keyvalue to insert. /// Returns a reference to this object. @@ -179,20 +241,67 @@ namespace Tizen.NUI { throw new global::System.ArgumentNullException(nameof(keyValue)); } - if (keyValue.KeyInt != null) + if (keyValue.IntegerKey != null) { - Interop.PropertyMap.Add(SwigCPtr, (int)keyValue.KeyInt, PropertyValue.getCPtr(keyValue.TrueValue)); + Add((int)keyValue.IntegerKey, keyValue.PropertyValue); } - else if (keyValue.KeyString != null) + else if (keyValue.StringKey != null) { - Interop.PropertyMap.Add(SwigCPtr, keyValue.KeyString, PropertyValue.getCPtr(keyValue.TrueValue)); + Add(keyValue.StringKey, keyValue.PropertyValue); } - if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); return this; } /// + /// Removes the element by the specified key. + /// + /// The index key to find. + /// True if the element is removed, false otherwise. + [EditorBrowsable(EditorBrowsableState.Never)] + public bool Remove(PropertyKey key) + { + if (null == key) + { + throw new global::System.ArgumentNullException(nameof(key)); + } + bool isRemoved = false; + if (key.Type == PropertyKey.KeyType.Index) + { + isRemoved = Interop.PropertyMap.Remove(SwigCPtr, key.IndexKey); + } + else if(key.Type == PropertyKey.KeyType.String) + { + isRemoved = Interop.PropertyMap.Remove(SwigCPtr, key.StringKey); + } + return isRemoved; + } + + /// + /// Determines whether the PropertyMap contains the specified key. + /// + /// The index key to find. + /// True if it exists, false otherwise. + [EditorBrowsable(EditorBrowsableState.Never)] + public bool Contains(PropertyKey key) + { + if (null == key) + { + throw new global::System.ArgumentNullException(nameof(key)); + } + global::System.IntPtr cPtr = global::System.IntPtr.Zero; + if (key.Type == PropertyKey.KeyType.Index) + { + cPtr = Interop.PropertyMap.Find(SwigCPtr, key.IndexKey); + } + else if (key.Type == PropertyKey.KeyType.String) + { + cPtr = Interop.PropertyMap.Find(SwigCPtr, key.StringKey); + } + return cPtr != global::System.IntPtr.Zero; + } + + /// /// Retrieves the value at the specified position. /// /// The specified position. @@ -265,6 +374,10 @@ namespace Tizen.NUI /// 3 public void Merge(PropertyMap from) { + if (from == null) + { + throw new global::System.ArgumentNullException(nameof(from)); + } Interop.PropertyMap.Merge(SwigCPtr, PropertyMap.getCPtr(from)); if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); } @@ -293,15 +406,16 @@ namespace Tizen.NUI return ret; } - internal void SetValue(int key, PropertyValue value) + internal void SetValue(PropertyKey key, PropertyValue value) { - Interop.PropertyMap.SetValueIntKey(SwigCPtr, key, PropertyValue.getCPtr(value)); - if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); - } - - internal void SetValue(string key, PropertyValue value) - { - Interop.PropertyMap.SetValueStringKey(SwigCPtr, key, PropertyValue.getCPtr(value)); + if (key.Type == PropertyKey.KeyType.Index) + { + Interop.PropertyMap.SetValueIntKey(SwigCPtr, key.IndexKey, PropertyValue.getCPtr(value)); + } + else if (key.Type == PropertyKey.KeyType.String) + { + Interop.PropertyMap.SetValueStringKey(SwigCPtr, key.StringKey, PropertyValue.getCPtr(value)); + } if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); } diff --git a/test/Tizen.NUI.Samples/Tizen.NUI.Samples/Samples/PropertyMapSample.cs b/test/Tizen.NUI.Samples/Tizen.NUI.Samples/Samples/PropertyMapSample.cs new file mode 100755 index 0000000..ccc874d --- /dev/null +++ b/test/Tizen.NUI.Samples/Tizen.NUI.Samples/Samples/PropertyMapSample.cs @@ -0,0 +1,64 @@ +using System.Collections.Generic; +using Tizen.NUI.BaseComponents; +using Tizen.NUI.Components; + +namespace Tizen.NUI.Samples +{ + public class PropertyMapSample : IExample + { + private TextLabel view1; + + public void Activate() + { + Window window = NUIApplication.GetDefaultWindow(); + + view1 = new TextLabel + { + Position = new Position(400, 400), + Size = new Size(600, 100), + Text = "Shadow on Label" + }; + + PropertyMap shadow = new PropertyMap(); + + // insert + shadow.Insert("offset", new PropertyValue(new Vector2(10, 10))); + shadow.Insert("color", new PropertyValue(Color.Red)); + shadow.Insert("blurRadius", new PropertyValue(10f)); + + // update + shadow["color"] = new PropertyValue(Color.Blue); + + // remove + shadow.Remove(new PropertyKey("color")); + Log.Info("test", $"removed color: {shadow.Contains(new PropertyKey("color"))}"); + + // add + shadow.Add("color", new PropertyValue(Color.Blue)); + + view1.Shadow = shadow; + var map = view1.Shadow; + + // query + Log.Info("test", $"offset : {map.Contains(new PropertyKey("offset"))}"); + Log.Info("test", $"color: {map.Contains(new PropertyKey("color"))}"); + Log.Info("test", $"color: {map["color"]}"); + + Color vectorValue = new Color(); + map["color"].Get(vectorValue); + bool isBlue = vectorValue.EqualTo(Color.Blue); + Log.Info("test", $"color: {isBlue}"); + + window.Add(view1); + } + + public void Deactivate() + { + if (view1 != null) + { + NUIApplication.GetDefaultWindow().Remove(view1); + view1.Dispose(); + } + } + } +} -- 2.7.4