From 87dc2534f824b8c35a6961a32a15dfd3a85131a5 Mon Sep 17 00:00:00 2001 From: YeongJong Lee Date: Wed, 23 Sep 2020 12:24:33 +0900 Subject: [PATCH] [NUI] Add ControlStateTypeConverter for xaml (#2002) * [NUI] rename from StateValuePair to SelectorItem * [NUI] add ControlStateTypeConverter for xaml Custom ConstrolState is now available in xaml. ### Sample XamlPage.xaml ```xaml ``` MyButton.cs ```cs public class MyButton : Button { public MyButton(ButtonStyle buttonStyle) : base(buttonStyle) {} public void SetControlState(ControlState s) => ControlState = s; } ``` ```cs protected override void OnCreate() { base.OnCreate(); Window.Instance.BackgroundColor = new Color(227 / 255f, 255 / 255f, 227 / 255f, 1.0f); Window.Instance.KeyEvent += OnKeyEvent; View root = new View(); root.WidthSpecification = LayoutParamPolicies.MatchParent; root.HeightSpecification = LayoutParamPolicies.MatchParent; root.Layout = new AbsoluteLayout(); Window.Instance.GetDefaultLayer().Add(root); XamlPage xamlPage = new XamlPage(); root.Add(xamlPage); ControlState MyState = ControlState.Create("MyState"); Button button = new Button() { Position2D = new Position2D(100, 100) }; bool flag = false; button.Clicked += (object sender, ClickedEventArgs e) => { if (!flag) { xamlPage.MyButton.SetControlState(MyState); flag = true; } else { xamlPage.MyButton.SetControlState(ControlState.Focused + MyState); flag = false; } }; root.Add(button); } ``` Co-authored-by: dongsug-song <35130733+dongsug-song@users.noreply.github.com> --- .../src/public/BaseComponents/ControlState.cs | 92 +++++----------------- .../src/public/BaseComponents/Style/Selector.cs | 80 ++++++++++++++----- 2 files changed, 80 insertions(+), 92 deletions(-) diff --git a/src/Tizen.NUI/src/public/BaseComponents/ControlState.cs b/src/Tizen.NUI/src/public/BaseComponents/ControlState.cs index 5d60fc1..cfbbfb1 100644 --- a/src/Tizen.NUI/src/public/BaseComponents/ControlState.cs +++ b/src/Tizen.NUI/src/public/BaseComponents/ControlState.cs @@ -26,6 +26,7 @@ namespace Tizen.NUI.BaseComponents /// Class for describing the states of the view. /// [EditorBrowsable(EditorBrowsableState.Never)] + [Binding.TypeConverter(typeof(ControlStateTypeConverter))] public class ControlState : IEquatable { private static readonly Dictionary stateDictionary = new Dictionary(); @@ -90,12 +91,7 @@ namespace Tizen.NUI.BaseComponents [EditorBrowsable(EditorBrowsableState.Never)] public bool IsCombined => stateList.Count > 1; - /// - /// Default Contructor. Please use or instead. - /// - // 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; } - } - /// - /// The Key/Value pair structure. this is mutable to support for xaml. - /// - [EditorBrowsable(EditorBrowsableState.Never)] - public struct StateValuePair : IEquatable> - { - /// - /// The constructor with the specified state and value. - /// - /// The state - /// The value associated with state. - [EditorBrowsable(EditorBrowsableState.Never)] - public StateValuePair(ControlState state, T value) + class ControlStateTypeConverter : Binding.TypeConverter { - State = state; - Value = value; - } - - /// - /// The state - /// - [EditorBrowsable(EditorBrowsableState.Never)] - public ControlState State { get; set; } - /// - /// The value associated with state. - /// - [EditorBrowsable(EditorBrowsableState.Never)] - public T Value { get; set; } - - /// - [EditorBrowsable(EditorBrowsableState.Never)] - public bool Equals(StateValuePair other) => (Value.Equals(other.Value)) && (State == other.State); + public override object ConvertFromInvariantString(string value) + { + if (value != null) + { + value = value.Trim(); - /// - [EditorBrowsable(EditorBrowsableState.Never)] - public override bool Equals(object obj) - { - if (!(obj is StateValuePair)) - return false; + ControlState convertedState = new ControlState(); + string[] parts = value.Split(','); + foreach (string part in parts) + { + convertedState += Create(part); + } + return convertedState; + } - return Equals((StateValuePair)obj); + throw new InvalidOperationException($"Cannot convert \"{value}\" into {typeof(ControlState)}"); + } } - - /// - [EditorBrowsable(EditorBrowsableState.Never)] - public override int GetHashCode() => (State.GetHashCode() * 397) ^ Value.GetHashCode(); - - - /// - /// Compares whether the two StateValuePair are different or not. - /// - /// A on the left hand side. - /// A on the right hand side. - /// true if the StateValuePair are equal; otherwise, false. - [EditorBrowsable(EditorBrowsableState.Never)] - public static bool operator ==(StateValuePair lhs, StateValuePair rhs) => lhs.Equals(rhs); - - /// - /// Compares whether the two StateValuePair are same or not. - /// - /// A on the left hand side. - /// A on the right hand side. - /// true if the StateValuePair are not equal; otherwise, false. - [EditorBrowsable(EditorBrowsableState.Never)] - public static bool operator !=(StateValuePair lhs, StateValuePair rhs) => !(lhs == rhs); - - /// - [EditorBrowsable(EditorBrowsableState.Never)] - public override string ToString() => $"[{State}, {Value}]"; } } \ No newline at end of file diff --git a/src/Tizen.NUI/src/public/BaseComponents/Style/Selector.cs b/src/Tizen.NUI/src/public/BaseComponents/Style/Selector.cs index 8b155fe..7f67f83 100755 --- a/src/Tizen.NUI/src/public/BaseComponents/Style/Selector.cs +++ b/src/Tizen.NUI/src/public/BaseComponents/Style/Selector.cs @@ -36,7 +36,7 @@ namespace Tizen.NUI.BaseComponents /// The list for adding state-value pair. /// [EditorBrowsable(EditorBrowsableState.Never)] - public IList> StateValueList { get; private set; } = new List>(); + public IList> StateValueList { get; private set; } = new List>(); /// /// Adds the specified state and value to the . @@ -44,14 +44,14 @@ namespace Tizen.NUI.BaseComponents /// The state. /// The value associated with state. [EditorBrowsable(EditorBrowsableState.Never)] - public void Add(ControlState state, T value) => StateValueList.Add(new StateValuePair(state, value)); + public void Add(ControlState state, T value) => StateValueList.Add(new SelectorItem(state, value)); /// /// Adds the specified state and value to the . /// /// [EditorBrowsable(EditorBrowsableState.Never)] - public void Add(StateValuePair stateValuePair) => StateValueList.Add(stateValuePair); + public void Add(SelectorItem stateValuePair) => StateValueList.Add(stateValuePair); /// 6 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API. @@ -100,7 +100,7 @@ namespace Tizen.NUI.BaseComponents [EditorBrowsable(EditorBrowsableState.Never)] public T Normal { - get => ((List>)StateValueList).FindLast(x => x.State == ControlState.Normal).Value; + get => ((List>)StateValueList).FindLast(x => x.State == ControlState.Normal).Value; set => Add(ControlState.Normal, value); } /// @@ -112,7 +112,7 @@ namespace Tizen.NUI.BaseComponents public T Pressed { - get => ((List>)StateValueList).FindLast(x => x.State == ControlState.Pressed).Value; + get => ((List>)StateValueList).FindLast(x => x.State == ControlState.Pressed).Value; set => Add(ControlState.Pressed, value); } /// @@ -123,7 +123,7 @@ namespace Tizen.NUI.BaseComponents [EditorBrowsable(EditorBrowsableState.Never)] public T Focused { - get => ((List>)StateValueList).FindLast(x => x.State == ControlState.Focused).Value; + get => ((List>)StateValueList).FindLast(x => x.State == ControlState.Focused).Value; set => Add(ControlState.Focused, value); } /// @@ -134,7 +134,7 @@ namespace Tizen.NUI.BaseComponents [EditorBrowsable(EditorBrowsableState.Never)] public T Selected { - get => ((List>)StateValueList).FindLast(x => x.State == ControlState.Selected).Value; + get => ((List>)StateValueList).FindLast(x => x.State == ControlState.Selected).Value; set => Add(ControlState.Selected, value); } /// @@ -146,7 +146,7 @@ namespace Tizen.NUI.BaseComponents public T Disabled { - get => ((List>)StateValueList).FindLast(x => x.State == ControlState.Disabled).Value; + get => ((List>)StateValueList).FindLast(x => x.State == ControlState.Disabled).Value; set => Add(ControlState.Disabled, value); } /// @@ -157,7 +157,7 @@ namespace Tizen.NUI.BaseComponents [EditorBrowsable(EditorBrowsableState.Never)] public T DisabledFocused { - get => ((List>)StateValueList).FindLast(x => x.State == ControlState.DisabledFocused).Value; + get => ((List>)StateValueList).FindLast(x => x.State == ControlState.DisabledFocused).Value; set => Add(ControlState.DisabledFocused, value); } /// @@ -167,7 +167,7 @@ namespace Tizen.NUI.BaseComponents /// 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>)StateValueList).FindLast(x => x.State == ControlState.SelectedFocused).Value; + get => ((List>)StateValueList).FindLast(x => x.State == ControlState.SelectedFocused).Value; set => Add(ControlState.SelectedFocused, value); } /// @@ -179,7 +179,7 @@ namespace Tizen.NUI.BaseComponents public T DisabledSelected { - get => ((List>)StateValueList).FindLast(x => x.State == ControlState.DisabledSelected).Value; + get => ((List>)StateValueList).FindLast(x => x.State == ControlState.DisabledSelected).Value; set => Add(ControlState.DisabledSelected, value); } @@ -191,7 +191,7 @@ namespace Tizen.NUI.BaseComponents [EditorBrowsable(EditorBrowsableState.Never)] public T Other { - get => ((List>)StateValueList).FindLast(x => x.State == ControlState.Other).Value; + get => ((List>)StateValueList).FindLast(x => x.State == ControlState.Other).Value; set => Add(ControlState.Other, value); } /// @@ -212,7 +212,7 @@ namespace Tizen.NUI.BaseComponents result = default; - int index = ((List>)StateValueList).FindLastIndex(x => x.State == state); + int index = ((List>)StateValueList).FindLastIndex(x => x.State == state); if (index >= 0) { result = StateValueList[index].Value; @@ -221,7 +221,7 @@ namespace Tizen.NUI.BaseComponents if (state.IsCombined) { - index = ((List>)StateValueList).FindLastIndex(x => state.Contains(x.State)); + index = ((List>)StateValueList).FindLastIndex(x => state.Contains(x.State)); if (index >= 0) { result = StateValueList[index].Value; @@ -229,7 +229,7 @@ namespace Tizen.NUI.BaseComponents } } - index = ((List>)StateValueList).FindLastIndex(x => x.State == ControlState.Other); + index = ((List>)StateValueList).FindLastIndex(x => x.State == ControlState.Other); if (index >= 0) { result = StateValueList[index].Value; @@ -286,12 +286,12 @@ namespace Tizen.NUI.BaseComponents if (cloneable) { All = (T)((ICloneable)other.All)?.Clone(); - StateValueList = ((List>)other.StateValueList).ConvertAll(m => new StateValuePair(m.State, (T)((ICloneable)m.Value)?.Clone())); + StateValueList = ((List>)other.StateValueList).ConvertAll(m => new SelectorItem(m.State, (T)((ICloneable)m.Value)?.Clone())); } else { All = other.All; - StateValueList = ((List>)other.StateValueList).ConvertAll(m => m); + StateValueList = ((List>)other.StateValueList).ConvertAll(m => m); } } @@ -427,6 +427,48 @@ namespace Tizen.NUI.BaseComponents } } + + /// + /// The selector item that have Key-Value pair. it can be added to . + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public class SelectorItem + { + /// + /// The default constructor. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public SelectorItem() {} + + /// + /// The constructor with the specified state and value. + /// + /// The state + /// The value associated with state. + [EditorBrowsable(EditorBrowsableState.Never)] + public SelectorItem(ControlState state, T value) + { + State = state; + Value = value; + } + + /// + /// The state + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public ControlState State { get; set; } + + /// + /// The value associated with state. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public T Value { get; set; } + + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public override string ToString() => $"[{State}, {Value}]"; + } + /// /// Extension class for . /// @@ -440,9 +482,9 @@ namespace Tizen.NUI.BaseComponents /// The state. /// The value associated with state. [EditorBrowsable(EditorBrowsableState.Never)] - public static void Add(this IList> list, ControlState state, T value) + public static void Add(this IList> list, ControlState state, T value) { - list.Add(new StateValuePair(state, value)); + list.Add(new SelectorItem(state, value)); } } } -- 2.7.4