From 0e6e689dd4c84b67a9285417f8a4aeeb7410cd31 Mon Sep 17 00:00:00 2001 From: Jiyun Yang Date: Mon, 6 Jan 2025 20:05:15 +0900 Subject: [PATCH] [NUI] Add value type: control state (1) Introduce the value type of ControlState. It replaces ControlState implementation. (2) Notify IsEnabled changed to control state outside of view. Signed-off-by: Jiyun Yang --- src/Tizen.NUI/src/devel/Base/DevelView.cs | 33 +++ src/Tizen.NUI/src/devel/Base/State.cs | 214 ++++++++++++++++++ .../src/public/BaseComponents/ControlState.cs | 85 +++---- 3 files changed, 273 insertions(+), 59 deletions(-) create mode 100755 src/Tizen.NUI/src/devel/Base/DevelView.cs create mode 100755 src/Tizen.NUI/src/devel/Base/State.cs diff --git a/src/Tizen.NUI/src/devel/Base/DevelView.cs b/src/Tizen.NUI/src/devel/Base/DevelView.cs new file mode 100755 index 00000000000..4c1864b3c25 --- /dev/null +++ b/src/Tizen.NUI/src/devel/Base/DevelView.cs @@ -0,0 +1,33 @@ +/* + * Copyright(c) 2025 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +using System; +using System.Text; +using System.ComponentModel; +using Tizen.NUI.BaseComponents; + +namespace Tizen.NUI.BaseComponents +{ + public partial class View + { + /// + /// Occurs when the value of changes. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public event EventHandler EnabledChanged; + } +} diff --git a/src/Tizen.NUI/src/devel/Base/State.cs b/src/Tizen.NUI/src/devel/Base/State.cs new file mode 100755 index 00000000000..cbdfe0c0404 --- /dev/null +++ b/src/Tizen.NUI/src/devel/Base/State.cs @@ -0,0 +1,214 @@ +/* + * Copyright(c) 2025 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +using System; +using System.Text; +using System.ComponentModel; +using Tizen.NUI.BaseComponents; + +namespace Tizen.NUI +{ + /// + /// Defines a value type of control state. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public readonly struct State + { + /// + /// The All state is used in a selector class. It represents all states, so if this state is defined in a selector, the other states are ignored. + /// + public static readonly State All = new (ControlStateUtility.FullMask); + + /// + /// Normal State. + /// + public static readonly State Normal = new (0UL); + + /// + /// Focused State. + /// + public static readonly State Focused = new (nameof(Focused)); + + /// + /// Pressed State. + /// + public static readonly State Pressed = new (nameof(Pressed)); + + /// + /// Disabled State. + /// + public static readonly State Disabled = new (nameof(Disabled)); + + /// + /// Selected State. + /// + public static readonly State Selected = new (nameof(Selected)); + + /// + /// Pressed caused by key state. + /// + public static readonly State PressedByKey = Pressed + new State(nameof(PressedByKey)); + + /// + /// Pressed caused by touch state. + /// + public static readonly State PressedByTouch = Pressed + new State(nameof(PressedByTouch)); + + /// + /// SelectedPressed State. + /// + public static readonly State SelectedPressed = Selected + Pressed; + + /// + /// DisabledSelected State. + /// + public static readonly State DisabledSelected = Disabled + Selected; + + /// + /// DisabledFocused State. + /// + public static readonly State DisabledFocused = Disabled + Focused; + + /// + /// SelectedFocused State. + /// + public static readonly State SelectedFocused = Selected + Focused; + + /// + /// This is used in a selector class. It represents all other states except for states that are already defined in a selector. + /// + public static readonly State Other = new State(nameof(Other)); + + private readonly ulong bitFlags; + + private State(ulong bitMask) + { + bitFlags = bitMask; + } + + private State(string name) : this(ControlStateUtility.Register(name)) + { + } + + /// + /// Gets or sets a value indicating whether it has combined states. + /// + internal bool IsCombined => (bitFlags != 0UL) && ((bitFlags & (bitFlags - 1UL)) != 0UL); + + /// + /// Create an instance of the with state name. + /// + /// The state name. + /// The instance which has single state. + /// Thrown when the given name is null. + /// Thrown when the given name is invalid. + public static State Create(string name) + { + return new State(name); + } + + /// + /// Determines whether a state contains a specified state. + /// + /// The state to search for + /// true if the state contain a specified state, otherwise, false. + public bool Contains(State state) => (bitFlags & state.bitFlags) == state.bitFlags; + + /// + /// Checks if there is a intersection part between this and the other. + /// + /// The other state to check. + /// True if an intersection exists, otherwise false. + public bool HasIntersectionWith(State other) => (bitFlags & other.bitFlags) != 0L; + + /// + public override string ToString() + { + var sbuilder = new StringBuilder(); + var states = ControlStateUtility.RegisteredStates(); + + if (bitFlags == 0UL) + { + return nameof(Normal); + } + else if (bitFlags == ControlStateUtility.FullMask) + { + return nameof(All); + } + + foreach (var (name, bitMask) in states) + { + if ((bitFlags & bitMask) > 0) + { + if (sbuilder.Length != 0) sbuilder.Append(", "); + sbuilder.Append(name); + } + } + + return sbuilder.ToString(); + } + + /// + /// Compares whether the two ControlStates are same or not. + /// + /// A on the left hand side. + /// A on the right hand side. + /// true if the ControlStates are equal; otherwise, false. + public static bool operator ==(State lhs, State rhs) => lhs.Equals(rhs); + + /// + /// Compares whether the two ControlStates are different or not. + /// + /// A on the left hand side. + /// A on the right hand side. + /// true if the ControlStates are not equal; otherwise, false. + public static bool operator !=(State lhs, State rhs) => !lhs.Equals(rhs); + + /// + /// The addition operator. + /// + /// A on the left hand side. + /// A on the right hand side. + /// The containing the result of the addition. + public static State operator +(State lhs, State rhs) => new State(lhs.bitFlags | rhs.bitFlags); + + /// + /// The substraction operator. + /// + /// A on the left hand side. + /// A on the right hand side. + /// The containing the result of the substraction. + public static State operator -(State lhs, State rhs) => new State(lhs.bitFlags & ~(rhs.bitFlags)); + + public bool Equals(State other) => bitFlags == other.bitFlags; + + /// + public override bool Equals(object obj) + { + if (obj is State otherState) + { + return Equals(otherState); + } + return base.Equals(obj); + } + + /// + public override int GetHashCode() => bitFlags.GetHashCode(); + + internal ControlState ToReferenceType() => new ControlState(this); + } +} diff --git a/src/Tizen.NUI/src/public/BaseComponents/ControlState.cs b/src/Tizen.NUI/src/public/BaseComponents/ControlState.cs index 199784c9e0c..bc3154b37af 100755 --- a/src/Tizen.NUI/src/public/BaseComponents/ControlState.cs +++ b/src/Tizen.NUI/src/public/BaseComponents/ControlState.cs @@ -35,74 +35,70 @@ public class ControlState : IEquatable /// The All state is used in a selector class. It represents all states, so if this state is defined in a selector, the other states are ignored. /// /// 9 - public static readonly ControlState All = new ControlState(ControlStateUtility.FullMask); + public static readonly ControlState All = new ControlState(State.All); /// /// Normal State. /// /// 9 - public static readonly ControlState Normal = new ControlState(0UL); + public static readonly ControlState Normal = new ControlState(State.Normal); /// /// Focused State. /// /// 9 - public static readonly ControlState Focused = new ControlState(nameof(Focused)); + public static readonly ControlState Focused = new ControlState(State.Focused); /// /// Pressed State. /// /// 9 - public static readonly ControlState Pressed = new ControlState(nameof(Pressed)); + public static readonly ControlState Pressed = new ControlState(State.Pressed); /// /// Disabled State. /// /// 9 - public static readonly ControlState Disabled = new ControlState(nameof(Disabled)); + public static readonly ControlState Disabled = new ControlState(State.Disabled); /// /// Selected State. /// /// 9 - public static readonly ControlState Selected = new ControlState(nameof(Selected)); + public static readonly ControlState Selected = new ControlState(State.Selected); /// /// SelectedPressed State. /// [EditorBrowsable(EditorBrowsableState.Never)] - public static readonly ControlState SelectedPressed = Selected + Pressed; + public static readonly ControlState SelectedPressed = new ControlState(State.SelectedPressed); /// /// DisabledSelected State. /// [EditorBrowsable(EditorBrowsableState.Never)] - public static readonly ControlState DisabledSelected = Disabled + Selected; + public static readonly ControlState DisabledSelected = new ControlState(State.DisabledSelected); /// /// DisabledFocused State. /// [EditorBrowsable(EditorBrowsableState.Never)] - public static readonly ControlState DisabledFocused = Disabled + Focused; + public static readonly ControlState DisabledFocused = new ControlState(State.DisabledFocused); /// /// SelectedFocused State. /// [EditorBrowsable(EditorBrowsableState.Never)] - public static readonly ControlState SelectedFocused = Selected + Focused; + public static readonly ControlState SelectedFocused = new ControlState(State.SelectedFocused); /// /// This is used in a selector class. It represents all other states except for states that are already defined in a selector. /// /// 9 - public static readonly ControlState Other = new ControlState(nameof(Other)); + public static readonly ControlState Other = new ControlState(State.Other); - readonly ulong bitFlags; + readonly State value; /// /// Gets or sets a value indicating whether it has combined states. /// [EditorBrowsable(EditorBrowsableState.Never)] - public bool IsCombined => (bitFlags != 0UL) && ((bitFlags & (bitFlags - 1UL)) != 0UL); + public bool IsCombined => value.IsCombined; - private ControlState(ulong bitMask) - { - bitFlags = bitMask; - } - - private ControlState(string name) : this(ControlStateUtility.Register(name)) + internal ControlState(State value) { + this.value = value; } /// @@ -115,7 +111,7 @@ private ControlState(string name) : this(ControlStateUtility.Register(name)) /// 9 public static ControlState Create(string name) { - return new ControlState(name); + return new ControlState(State.Create(name)); } /// @@ -126,14 +122,14 @@ public static ControlState Create(string name) [EditorBrowsable(EditorBrowsableState.Never)] public static ControlState Create(params ControlState[] states) { - ulong newbits = 0UL; + State result = State.Normal; for (int i = 0; i < states.Length; i++) { - newbits |= states[i].bitFlags; + result += states[i].value; } - return new ControlState(newbits); + return new ControlState(result); } /// @@ -147,16 +143,13 @@ public bool Contains(ControlState state) { if (state is null) throw new ArgumentNullException(nameof(state)); - return (bitFlags & state.bitFlags) == state.bitFlags; + + return value.Contains(state.value); } /// [EditorBrowsable(EditorBrowsableState.Never)] - public bool Equals(ControlState other) - { - if (other is null) return false; - return this.bitFlags == other.bitFlags; - } + public bool Equals(ControlState other) => value.Equals(other.value); /// /// 9 @@ -164,35 +157,11 @@ public bool Equals(ControlState other) /// [EditorBrowsable(EditorBrowsableState.Never)] - public override int GetHashCode() => bitFlags.GetHashCode(); + public override int GetHashCode() => value.GetHashCode(); /// [EditorBrowsable(EditorBrowsableState.Never)] - public override string ToString() - { - var sbuilder = new StringBuilder(); - var states = ControlStateUtility.RegisteredStates(); - - if (bitFlags == 0UL) - { - return nameof(Normal); - } - else if (bitFlags == ControlStateUtility.FullMask) - { - return nameof(All); - } - - foreach (var (name, bitMask) in states) - { - if ((bitFlags & bitMask) > 0) - { - if (sbuilder.Length != 0) sbuilder.Append(", "); - sbuilder.Append(name); - } - } - - return sbuilder.ToString(); - } + public override string ToString() => value.ToString(); /// /// Compares whether the two ControlStates are same or not. @@ -257,9 +226,7 @@ static ControlState Add(ControlState operand1, ControlState operand2) throw new ArgumentNullException(nameof(operand2)); } - ulong newBitFlags = operand1.bitFlags | operand2.bitFlags; - - return new ControlState(newBitFlags); + return new ControlState(operand1.value + operand2.value); } static ControlState Remove(ControlState operand1, ControlState operand2) @@ -273,7 +240,7 @@ static ControlState Remove(ControlState operand1, ControlState operand2) throw new ArgumentNullException(nameof(operand2)); } - return new ControlState(operand1.bitFlags & ~(operand2.bitFlags)); + return new ControlState(operand1.value - operand2.value); } class ControlStateTypeConverter : Binding.TypeConverter