[NUI] Add ControlStateTypeConverter for xaml (#2002)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / BaseComponents / ControlState.cs
index 5d60fc1..cfbbfb1 100644 (file)
@@ -26,6 +26,7 @@ namespace Tizen.NUI.BaseComponents
     /// Class for describing the states of the view.
     /// </summary>
     [EditorBrowsable(EditorBrowsableState.Never)]
+    [Binding.TypeConverter(typeof(ControlStateTypeConverter))]
     public class ControlState : IEquatable<ControlState>
     {
         private static readonly Dictionary<string, ControlState> stateDictionary = new Dictionary<string, ControlState>();
@@ -90,12 +91,7 @@ namespace Tizen.NUI.BaseComponents
         [EditorBrowsable(EditorBrowsableState.Never)]
         public bool IsCombined => stateList.Count > 1;
 
-        /// <summary>
-        /// Default Contructor. Please use <see cref="Create(string)"/> or <see cref="Create(ControlState[])"/> instead.
-        /// </summary>
-        // Do not open this constructor. This is only for xaml support.
-        [EditorBrowsable(EditorBrowsableState.Never)]
-        public ControlState() { }
+        private ControlState() { }
 
         private ControlState(string name) : this() => this.name = name;
 
@@ -259,7 +255,7 @@ namespace Tizen.NUI.BaseComponents
             {
                 return ReferenceEquals(lhs, rhs) ? Normal : lhs;
             }
-            
+
             var rest = lhs.stateList.Except(rhs.stateList);
 
             if (rest.Count() == 0)
@@ -276,76 +272,26 @@ namespace Tizen.NUI.BaseComponents
             newState.stateList.AddRange(rest);
             return newState;
         }
-    }
 
-    /// <summary>
-    /// The Key/Value pair structure. this is mutable to support for xaml.
-    /// </summary>
-    [EditorBrowsable(EditorBrowsableState.Never)]
-    public struct StateValuePair<T> : IEquatable<StateValuePair<T>>
-    {
-        /// <summary>
-        /// The constructor with the specified state and value.
-        /// </summary>
-        /// <param name="state">The state</param>
-        /// <param name="value">The value associated with state.</param>
-        [EditorBrowsable(EditorBrowsableState.Never)]
-        public StateValuePair(ControlState state, T value)
+        class ControlStateTypeConverter : Binding.TypeConverter
         {
-            State = state;
-            Value = value;
-        }
-
-        /// <summary>
-        /// The state
-        /// </summary>
-        [EditorBrowsable(EditorBrowsableState.Never)]
-        public ControlState State { get; set; }
-        /// <summary>
-        /// The value associated with state.
-        /// </summary>
-        [EditorBrowsable(EditorBrowsableState.Never)]
-        public T Value { get; set; }
-
-        ///  <inheritdoc/>
-        [EditorBrowsable(EditorBrowsableState.Never)]
-        public bool Equals(StateValuePair<T> other) => (Value.Equals(other.Value)) && (State == other.State);
+            public override object ConvertFromInvariantString(string value)
+            {
+                if (value != null)
+                {
+                    value = value.Trim();
 
-        ///  <inheritdoc/>
-        [EditorBrowsable(EditorBrowsableState.Never)]
-        public override bool Equals(object obj)
-        {
-            if (!(obj is StateValuePair<T>))
-                return false;
+                    ControlState convertedState = new ControlState();
+                    string[] parts = value.Split(',');
+                    foreach (string part in parts)
+                    {
+                        convertedState += Create(part);
+                    }
+                    return convertedState;
+                }
 
-            return Equals((StateValuePair<T>)obj);
+                throw new InvalidOperationException($"Cannot convert \"{value}\" into {typeof(ControlState)}");
+            }
         }
-
-        ///  <inheritdoc/>
-        [EditorBrowsable(EditorBrowsableState.Never)]
-        public override int GetHashCode() => (State.GetHashCode() * 397) ^ Value.GetHashCode();
-
-
-        /// <summary>
-        /// Compares whether the two StateValuePair are different or not.
-        /// </summary>
-        /// <param name="lhs">A <see cref="StateValuePair{T}"/> on the left hand side.</param>
-        /// <param name="rhs">A <see cref="StateValuePair{T}"/> on the right hand side.</param>
-        /// <returns>true if the StateValuePair are equal; otherwise, false.</returns>
-        [EditorBrowsable(EditorBrowsableState.Never)]
-        public static bool operator ==(StateValuePair<T> lhs, StateValuePair<T> rhs) => lhs.Equals(rhs);
-
-        /// <summary>
-        /// Compares whether the two StateValuePair are same or not.
-        /// </summary>
-        /// <param name="lhs">A <see cref="StateValuePair{T}"/> on the left hand side.</param>
-        /// <param name="rhs">A <see cref="StateValuePair{T}"/> on the right hand side.</param>
-        /// <returns>true if the StateValuePair are not equal; otherwise, false.</returns>
-        [EditorBrowsable(EditorBrowsableState.Never)]
-        public static bool operator !=(StateValuePair<T> lhs, StateValuePair<T> rhs) => !(lhs == rhs);
-
-        ///  <inheritdoc/>
-        [EditorBrowsable(EditorBrowsableState.Never)]
-        public override string ToString() => $"[{State}, {Value}]";
     }
 }
\ No newline at end of file