123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- 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;
- }
- }
- }
|