using System.ComponentModel; namespace System.Windows.Forms { [TypeConverter(typeof(PaddingConverter))] public struct Padding { private bool _all; private int _top; private int _left; private int _right; private int _bottom; public static readonly Padding Empty = new Padding(0); public int All { get { if (!_all) { return -1; } return _top; } set { if (!_all || _top != value) { _all = true; _top = (_left = (_right = (_bottom = value))); } } } public int Bottom { get { if (_all) { return _top; } return _bottom; } set { if (_all || _bottom != value) { _all = false; _bottom = value; } } } public int Left { get { if (_all) { return _top; } return _left; } set { if (_all || _left != value) { _all = false; _left = value; } } } public int Right { get { if (_all) { return _top; } return _right; } set { if (_all || _right != value) { _all = false; _right = value; } } } public int Top { get { return _top; } set { if (_all || _top != value) { _all = false; _top = value; } } } public int Horizontal => Left + Right; public int Vertical => Top + Bottom; public Size Size => new Size(Horizontal, Vertical); public Padding(int all) { _all = true; _top = (_left = (_right = (_bottom = all))); } public Padding(int left, int top, int right, int bottom) { _top = top; _left = left; _right = right; _bottom = bottom; _all = _top == _left && _top == _right && _top == _bottom; } public override bool Equals(object other) { if (other is not Padding otherPadding) { return false; } return Equals(otherPadding); } public bool Equals(Padding other) => Left == other.Left && Top == other.Top && Right == other.Right && Bottom == other.Bottom; public static Padding operator +(Padding p1, Padding p2) { return new Padding(p1.Left + p2.Left, p1.Top + p2.Top, p1.Right + p2.Right, p1.Bottom + p2.Bottom); } public static Padding operator -(Padding p1, Padding p2) { return new Padding(p1.Left - p2.Left, p1.Top - p2.Top, p1.Right - p2.Right, p1.Bottom - p2.Bottom); } public static bool operator ==(Padding p1, Padding p2) { return p1.Left == p2.Left && p1.Top == p2.Top && p1.Right == p2.Right && p1.Bottom == p2.Bottom; } public static bool operator !=(Padding p1, Padding p2) => !(p1 == p2); public override int GetHashCode() { var hashCode = 1903003160; hashCode = hashCode * -1521134295 + Left.GetHashCode(); hashCode = hashCode * -1521134295 + Top.GetHashCode(); hashCode = hashCode * -1521134295 + Right.GetHashCode(); hashCode = hashCode * -1521134295 + Bottom.GetHashCode(); return hashCode; } internal bool ShouldSerializeAll() { return _all; } } }