/// 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>();
[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;
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException("name cannot be empty string", nameof(name));
+ name = name.Trim();
+
if (stateDictionary.TryGetValue(name, out ControlState state))
return state;
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
private readonly bool cloneable = typeof(ICloneable).IsAssignableFrom(typeof(T));
/// <summary>
- /// The list for adding state-value pair.
+ /// The list for adding <see cref="SelectorItem{T}"/>.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
- public IList<StateValuePair<T>> StateValueList { get; set; } = new List<StateValuePair<T>>();
+ public IList<SelectorItem<T>> SelectorItems { get; set; } = new List<SelectorItem<T>>();
/// <summary>
- /// Adds the specified state and value.
+ /// Adds the specified state and value to the <see cref="SelectorItems"/>.
/// </summary>
/// <param name="state">The state.</param>
/// <param name="value">The value associated with state.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
- public void Add(ControlState state, T value) => Add(new StateValuePair<T>(state, value));
+ public void Add(ControlState state, T value) => SelectorItems.Add(new SelectorItem<T>(state, value));
/// <summary>
- /// Adds the specified state and value.
+ /// Adds the specified state and value to the <see cref="SelectorItems"/>.
/// </summary>
- /// <param name="stateValuePair"></param>
+ /// <param name="selectorItem">The selector item class that stores a state-value pair.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
- public void Add(StateValuePair<T> stateValuePair)
+ public void Add(SelectorItem<T> selectorItem)
{
// To prevent a state from having multiple values, remove existing state-value pair.
- int index = ((List<StateValuePair<T>>)StateValueList).FindIndex(x => x.State == stateValuePair.State);
+ int index = ((List<SelectorItem<T>>)SelectorItems).FindIndex(x => x.State == selectorItem.State);
if (index != -1)
- StateValueList.RemoveAt(index);
+ SelectorItems.RemoveAt(index);
- StateValueList.Add(stateValuePair);
+ SelectorItems.Add(selectorItem);
}
/// <since_tizen> 6 </since_tizen>
[EditorBrowsable(EditorBrowsableState.Never)]
public T Normal
{
- get => ((List<StateValuePair<T>>)StateValueList).Find(x => x.State == ControlState.Normal).Value;
+ get => ((List<SelectorItem<T>>)SelectorItems).Find(x => x.State == ControlState.Normal).Value;
set => Add(ControlState.Normal, value);
}
/// <summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public T Pressed
{
-
- get => ((List<StateValuePair<T>>)StateValueList).Find(x => x.State == ControlState.Pressed).Value;
+ get => ((List<SelectorItem<T>>)SelectorItems).Find(x => x.State == ControlState.Pressed).Value;
set => Add(ControlState.Pressed, value);
}
/// <summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public T Focused
{
- get => ((List<StateValuePair<T>>)StateValueList).Find(x => x.State == ControlState.Focused).Value;
+ get => ((List<SelectorItem<T>>)SelectorItems).Find(x => x.State == ControlState.Focused).Value;
set => Add(ControlState.Focused, value);
}
/// <summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public T Selected
{
- get => ((List<StateValuePair<T>>)StateValueList).Find(x => x.State == ControlState.Selected).Value;
+ get => ((List<SelectorItem<T>>)SelectorItems).Find(x => x.State == ControlState.Selected).Value;
set => Add(ControlState.Selected, value);
}
/// <summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public T Disabled
{
-
- get => ((List<StateValuePair<T>>)StateValueList).Find(x => x.State == ControlState.Disabled).Value;
+ get => ((List<SelectorItem<T>>)SelectorItems).Find(x => x.State == ControlState.Disabled).Value;
set => Add(ControlState.Disabled, value);
}
/// <summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public T DisabledFocused
{
- get => ((List<StateValuePair<T>>)StateValueList).Find(x => x.State == ControlState.DisabledFocused).Value;
+ get => ((List<SelectorItem<T>>)SelectorItems).Find(x => x.State == ControlState.DisabledFocused).Value;
set => Add(ControlState.DisabledFocused, value);
}
/// <summary>
/// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
public T SelectedFocused
{
- get => ((List<StateValuePair<T>>)StateValueList).Find(x => x.State == ControlState.SelectedFocused).Value;
+ get => ((List<SelectorItem<T>>)SelectorItems).Find(x => x.State == ControlState.SelectedFocused).Value;
set => Add(ControlState.SelectedFocused, value);
}
/// <summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public T DisabledSelected
{
-
- get => ((List<StateValuePair<T>>)StateValueList).Find(x => x.State == ControlState.DisabledSelected).Value;
+ get => ((List<SelectorItem<T>>)SelectorItems).Find(x => x.State == ControlState.DisabledSelected).Value;
set => Add(ControlState.DisabledSelected, value);
}
[EditorBrowsable(EditorBrowsableState.Never)]
public T Other
{
- get => ((List<StateValuePair<T>>)StateValueList).Find(x => x.State == ControlState.Other).Value;
+ get => ((List<SelectorItem<T>>)SelectorItems).Find(x => x.State == ControlState.Other).Value;
set => Add(ControlState.Other, value);
}
/// Gets the number of elements.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
- public int Count => StateValueList.Count;
+ public int Count => SelectorItems.Count;
/// <summary>
/// Get value by State.
return true;
}
+ if (state == null)
+ throw new ArgumentNullException(nameof(state));
+
result = default;
- int index = ((List<StateValuePair<T>>)StateValueList).FindIndex(x => x.State == state);
+ int index = ((List<SelectorItem<T>>)SelectorItems).FindIndex(x => x.State == state);
if (index >= 0)
{
- result = StateValueList[index].Value;
+ result = SelectorItems[index].Value;
return true;
}
}
if (state.IsCombined)
{
- index = ((List<StateValuePair<T>>)StateValueList).FindIndex(x => state.Contains(x.State));
+ index = ((List<SelectorItem<T>>)SelectorItems).FindIndex(x => state.Contains(x.State));
if (index >= 0)
{
- result = StateValueList[index].Value;
+ result = SelectorItems[index].Value;
return true;
}
}
- index = ((List<StateValuePair<T>>)StateValueList).FindIndex(x => x.State == ControlState.Other);
+ index = ((List<SelectorItem<T>>)SelectorItems).FindIndex(x => x.State == ControlState.Other);
if (index >= 0)
{
- result = StateValueList[index].Value;
+ result = SelectorItems[index].Value;
return true;
}
public void Clear()
{
All = default;
- StateValueList.Clear();
+ SelectorItems.Clear();
}
/// <inheritdoc/>
{
string result = $"[All, {All}]";
- foreach (var item in StateValueList)
+ foreach (var item in SelectorItems)
{
result += $", {item}";
}
if (cloneable)
{
All = (T)((ICloneable)other.All)?.Clone();
- StateValueList = ((List<StateValuePair<T>>)other.StateValueList).ConvertAll(m => new StateValuePair<T>(m.State, (T)((ICloneable)m.Value)?.Clone()));
+ SelectorItems = ((List<SelectorItem<T>>)other.SelectorItems).ConvertAll(m => new SelectorItem<T>(m.State, (T)((ICloneable)m.Value)?.Clone()));
}
else
{
All = other.All;
- StateValueList = ((List<StateValuePair<T>>)other.StateValueList).ConvertAll(m => m);
+ SelectorItems = ((List<SelectorItem<T>>)other.SelectorItems).ConvertAll(m => m);
}
}
internal bool HasMultiValue()
{
- return StateValueList.Count > 1;
+ return SelectorItems.Count > 1;
}
-
}
/// <summary>
}
}
}
+
+ /// <summary>
+ /// The selector item class that stores a state-value pair.
+ /// </summary>
+ [EditorBrowsable(EditorBrowsableState.Never)]
+ public class SelectorItem<T>
+ {
+ /// <summary>
+ /// The default constructor.
+ /// </summary>
+ [EditorBrowsable(EditorBrowsableState.Never)]
+ public SelectorItem() {}
+
+ /// <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 SelectorItem(ControlState state, T value)
+ {
+ 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 override string ToString() => $"[{State}, {Value}]";
+ }
+
+ /// <summary>
+ /// Extension class for <see cref="Selector{T}"/>.
+ /// </summary>
+ [EditorBrowsable(EditorBrowsableState.Never)]
+ public static class SelectorExtensions
+ {
+ /// <summary>
+ /// Adds the specified state and value to the <see cref="Selector{T}.SelectorItems"/>.
+ /// </summary>
+ /// <param name="list">The list for adding state-value pair.</param>
+ /// <param name="state">The state.</param>
+ /// <param name="value">The value associated with state.</param>
+ [EditorBrowsable(EditorBrowsableState.Never)]
+ public static void Add<T>(this IList<SelectorItem<T>> list, ControlState state, T value)
+ {
+ list.Add(new SelectorItem<T>(state, value));
+ }
+ }
}