From: Jiyun Yang Date: Tue, 1 Apr 2025 05:12:55 +0000 (+0900) Subject: [NUI] Implement IEquatable for primitive structures and make them immutable X-Git-Tag: submit/tizen/20250401.115655~1^2~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=dab9bddff0faba523eea7b8b3a4c9d91799c542f;p=platform%2Fcore%2Fcsapi%2Ftizenfx.git [NUI] Implement IEquatable for primitive structures and make them immutable Signed-off-by: Jiyun Yang --- diff --git a/src/Tizen.NUI/src/devel/Lite/UICorner.cs b/src/Tizen.NUI/src/devel/Lite/UICorner.cs index 40d00453f..a63f8fe18 100644 --- a/src/Tizen.NUI/src/devel/Lite/UICorner.cs +++ b/src/Tizen.NUI/src/devel/Lite/UICorner.cs @@ -14,6 +14,7 @@ * limitations under the License. * */ +using System; using System.ComponentModel; namespace Tizen.NUI @@ -22,7 +23,7 @@ namespace Tizen.NUI /// Defines a value type of corner radius. /// [EditorBrowsable(EditorBrowsableState.Never)] - public struct UICorner + public struct UICorner : IEquatable { /// /// The default corner. (This is to distinguish from zero corners) @@ -92,27 +93,81 @@ namespace Tizen.NUI /// /// The radius of the top left corner of the rectangle. /// - public float TopLeft { get; } + public float TopLeft { get; init; } /// /// The radius of the top right corner of the rectangle. /// - public float TopRight { get; } + public float TopRight { get; init; } /// /// The radius of the bottom right corner of the rectangle. /// - public float BottomRight { get; } + public float BottomRight { get; init; } /// /// The radius of the bottom left corner of the rectangle. /// - public float BottomLeft { get; } + public float BottomLeft { get; init; } /// /// Gets a value indicating whether the values are relative to target box size. /// - public bool IsRelative { get; } + public bool IsRelative { get; init; } + + /// + /// Compares two value for equality. + /// + /// The first operand object. + /// The second operand object. + /// True if both are equal, otherwise false. + public static bool operator ==(UICorner operand1, UICorner operand2) + { + return operand1.Equals(operand2); + } + + /// + /// Compares two value for inequality. + /// + /// The first operand object. + /// The second operand object. + /// True if both are not equal, otherwise false. + public static bool operator !=(UICorner operand1, UICorner operand2) + { + return !operand1.Equals(operand2); + } + + /// + /// Whether this is equivalent to other. + /// + public bool Equals(UICorner other) + { + return TopLeft == other.TopLeft && TopRight == other.TopRight && BottomRight == other.BottomRight && BottomLeft == other.BottomLeft && IsRelative && other.IsRelative; + } + + /// + public override bool Equals(object obj) + { + if (obj is UICorner other) + { + return Equals(other); + } + return base.Equals(obj); + } + + /// + public override int GetHashCode() + { + unchecked + { + int hashcode = TopLeft.GetHashCode(); + hashcode = hashcode * 397 ^ TopRight.GetHashCode(); + hashcode = hashcode * 397 ^ BottomRight.GetHashCode(); + hashcode = hashcode * 397 ^ BottomLeft.GetHashCode(); + hashcode = hashcode * 397 ^ IsRelative.GetHashCode(); + return hashcode; + } + } internal readonly NUI.Vector4 ToReferenceType() => new NUI.Vector4(TopLeft, TopRight, BottomRight, BottomLeft); } diff --git a/src/Tizen.NUI/src/devel/Lite/UIExtents.cs b/src/Tizen.NUI/src/devel/Lite/UIExtents.cs index 0a7876d63..e20d46091 100644 --- a/src/Tizen.NUI/src/devel/Lite/UIExtents.cs +++ b/src/Tizen.NUI/src/devel/Lite/UIExtents.cs @@ -1,4 +1,5 @@ -using System.ComponentModel; +using System; +using System.ComponentModel; namespace Tizen.NUI { @@ -6,7 +7,7 @@ namespace Tizen.NUI /// Defines the thickness of a border around a control. /// [EditorBrowsable(EditorBrowsableState.Never)] - public struct UIExtents + public struct UIExtents : IEquatable { /// /// Initializes a new instance of the struct with the specified uniform size. @@ -43,22 +44,22 @@ namespace Tizen.NUI /// /// Gets or sets the width of the left border. /// - public float Start { get; set; } + public float Start { get; init; } /// /// Gets or sets the width of the right border. /// - public float End { get; set; } + public float End { get; init; } /// /// Gets or sets the width of the top border. /// - public float Top { get; set; } + public float Top { get; init; } /// /// Gets or sets the width of the bottom border. /// - public float Bottom { get; set; } + public float Bottom { get; init; } /// /// Gets the total width of the horizontal borders. @@ -89,7 +90,10 @@ namespace Tizen.NUI return new UIExtents(uniformSize); } - bool Equals(UIExtents other) + /// + /// Whether this is equivalent to other. + /// + public bool Equals(UIExtents other) { return Start.Equals(other.Start) && End.Equals(other.End) && Top.Equals(other.Top) && Bottom.Equals(other.Bottom); } diff --git a/src/Tizen.NUI/src/devel/Lite/UIRect.cs b/src/Tizen.NUI/src/devel/Lite/UIRect.cs index f0b38b671..e4de70517 100644 --- a/src/Tizen.NUI/src/devel/Lite/UIRect.cs +++ b/src/Tizen.NUI/src/devel/Lite/UIRect.cs @@ -18,12 +18,12 @@ using System.ComponentModel; using System.Globalization; namespace Tizen.NUI -{ +{ /// /// Represents a rectangular area by defining its position and size. /// [EditorBrowsable(EditorBrowsableState.Never)] - public struct UIRect + public struct UIRect : IEquatable { /// /// Initializes a new instance of the struct. @@ -43,22 +43,22 @@ namespace Tizen.NUI /// /// Gets or sets the x-coordinate of the upper-left corner of the rectangle. /// - public float X { get; set; } + public float X { get; init; } /// /// Gets or sets the y-coordinate of the upper-left corner of the rectangle. /// - public float Y { get; set; } + public float Y { get; init; } /// /// Gets or sets the width of the rectangle. /// - public float Width { get; set; } + public float Width { get; init; } /// /// Gets or sets the height of the rectangle. /// - public float Height { get; set; } + public float Height { get; init; } /// /// Gets a structure with all coordinates and sizes set to zero. @@ -96,7 +96,7 @@ namespace Tizen.NUI public UIVector2 Size { get => new UIVector2(Width, Height); - set + init { Width = value.Width; Height = value.Height; @@ -109,7 +109,7 @@ namespace Tizen.NUI public UIVector2 Location { get => new UIVector2(X, Y); - set + init { X = value.X; Y = value.Y; @@ -272,12 +272,13 @@ namespace Tizen.NUI /// A new rectangle that represents the inflated version of the current rectangle. public UIRect Inflate(float width, float height) { - UIRect r = this; - r.X -= width; - r.Y -= height; - r.Width += width * 2; - r.Height += height * 2; - return r; + return new () + { + X = X - width, + Y = Y - height, + Width = Width + width * 2, + Height = Height + height * 2 + }; } /// @@ -288,10 +289,13 @@ namespace Tizen.NUI /// A new rectangle with the offset applied. public UIRect Offset(float dx, float dy) { - UIRect r = this; - r.X += dx; - r.Y += dy; - return r; + return new () + { + X = X + dx, + Y = Y + dy, + Width = Width, + Height = Height + }; } /// diff --git a/src/Tizen.NUI/src/devel/Lite/UIState.cs b/src/Tizen.NUI/src/devel/Lite/UIState.cs index 87b3c8e73..489fc6cf7 100755 --- a/src/Tizen.NUI/src/devel/Lite/UIState.cs +++ b/src/Tizen.NUI/src/devel/Lite/UIState.cs @@ -26,7 +26,7 @@ namespace Tizen.NUI /// Defines a value type of view state. /// [EditorBrowsable(EditorBrowsableState.Never)] - public readonly struct UIState + public readonly struct UIState : 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. diff --git a/src/Tizen.NUI/src/devel/Lite/UIVector2.cs b/src/Tizen.NUI/src/devel/Lite/UIVector2.cs index e2e76ef6a..c2a75b4fc 100644 --- a/src/Tizen.NUI/src/devel/Lite/UIVector2.cs +++ b/src/Tizen.NUI/src/devel/Lite/UIVector2.cs @@ -14,6 +14,7 @@ * limitations under the License. * */ +using System; using System.ComponentModel; namespace Tizen.NUI @@ -22,7 +23,7 @@ namespace Tizen.NUI /// Defines a value type of vector2. /// [EditorBrowsable(EditorBrowsableState.Never)] - public readonly struct UIVector2 + public struct UIVector2 : IEquatable { /// /// The zero vector2. @@ -46,6 +47,7 @@ namespace Tizen.NUI public float X { get; + init; } /// @@ -54,6 +56,7 @@ namespace Tizen.NUI public float Y { get; + init; } public readonly bool IsZero => X == 0 && Y == 0; @@ -61,12 +64,69 @@ namespace Tizen.NUI /// /// Gets the width component of the vector2. /// - public float Width => X; + public float Width + { + get => X; + init => X = value; + } /// /// Gets the height component of the vector2. /// - public float Height => Y; + public float Height + { + get => Y; + init => Y = value; + } + + /// + /// Compares two value for equality. + /// + /// The first operand object. + /// The second operand object. + /// True if both are equal, otherwise false. + public static bool operator ==(UIVector2 operand1, UIVector2 operand2) + { + return operand1.Equals(operand2); + } + + /// + /// Compares two value for inequality. + /// + /// The first operand object. + /// The second operand object. + /// True if both are not equal, otherwise false. + public static bool operator !=(UIVector2 operand1, UIVector2 operand2) + { + return !operand1.Equals(operand2); + } + + /// + /// Whether this is equivalent to other. + /// + public bool Equals(UIVector2 other) + { + return X == other.X && Y == other.Y; + } + + /// + public override bool Equals(object obj) + { + if (obj is UIVector2 other) + { + return Equals(other); + } + return base.Equals(obj); + } + + /// + public override int GetHashCode() + { + unchecked + { + return X.GetHashCode() ^ (Y.GetHashCode() * 397); + } + } /// /// Converts the UIVector2 to Vector3 class implicitly. diff --git a/src/Tizen.NUI/src/devel/Lite/UIVector3.cs b/src/Tizen.NUI/src/devel/Lite/UIVector3.cs index 04dfee181..c4f7842f0 100644 --- a/src/Tizen.NUI/src/devel/Lite/UIVector3.cs +++ b/src/Tizen.NUI/src/devel/Lite/UIVector3.cs @@ -23,7 +23,7 @@ namespace Tizen.NUI /// Defines a value type of vector3. /// [EditorBrowsable(EditorBrowsableState.Never)] - public readonly struct UIVector3 : IEquatable + public struct UIVector3 : IEquatable { /// /// The zero vector3. @@ -58,6 +58,7 @@ namespace Tizen.NUI public float X { get; + init; } /// @@ -66,6 +67,7 @@ namespace Tizen.NUI public float Y { get; + init; } /// @@ -74,6 +76,7 @@ namespace Tizen.NUI public float Z { get; + init; } public readonly bool IsZero => X == 0 && Y == 0 && Z == 0; @@ -81,17 +84,29 @@ namespace Tizen.NUI /// /// Gets the width component of the vector3. /// - public float Width => X; + public float Width + { + get => X; + init => X = value; + } /// /// Gets the height component of the vector3. /// - public float Height => Y; + public float Height + { + get => Y; + init => Y = value; + } /// /// Gets the depth component of the vector3. /// - public float Depth => Z; + public float Depth + { + get => Z; + init => Z = value; + } /// /// Converts the UIVector2 to UIVector3 class implicitly.