Padding.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using System.ComponentModel;
  2. namespace System.Windows.Forms
  3. {
  4. [TypeConverter(typeof(PaddingConverter))]
  5. public struct Padding
  6. {
  7. private bool _all;
  8. private int _top;
  9. private int _left;
  10. private int _right;
  11. private int _bottom;
  12. public static readonly Padding Empty = new Padding(0);
  13. public int All
  14. {
  15. get
  16. {
  17. if (!_all)
  18. {
  19. return -1;
  20. }
  21. return _top;
  22. }
  23. set
  24. {
  25. if (!_all || _top != value)
  26. {
  27. _all = true;
  28. _top = (_left = (_right = (_bottom = value)));
  29. }
  30. }
  31. }
  32. public int Bottom
  33. {
  34. get
  35. {
  36. if (_all)
  37. {
  38. return _top;
  39. }
  40. return _bottom;
  41. }
  42. set
  43. {
  44. if (_all || _bottom != value)
  45. {
  46. _all = false;
  47. _bottom = value;
  48. }
  49. }
  50. }
  51. public int Left
  52. {
  53. get
  54. {
  55. if (_all)
  56. {
  57. return _top;
  58. }
  59. return _left;
  60. }
  61. set
  62. {
  63. if (_all || _left != value)
  64. {
  65. _all = false;
  66. _left = value;
  67. }
  68. }
  69. }
  70. public int Right
  71. {
  72. get
  73. {
  74. if (_all)
  75. {
  76. return _top;
  77. }
  78. return _right;
  79. }
  80. set
  81. {
  82. if (_all || _right != value)
  83. {
  84. _all = false;
  85. _right = value;
  86. }
  87. }
  88. }
  89. public int Top
  90. {
  91. get
  92. {
  93. return _top;
  94. }
  95. set
  96. {
  97. if (_all || _top != value)
  98. {
  99. _all = false;
  100. _top = value;
  101. }
  102. }
  103. }
  104. public int Horizontal => Left + Right;
  105. public int Vertical => Top + Bottom;
  106. public Size Size => new Size(Horizontal, Vertical);
  107. public Padding(int all)
  108. {
  109. _all = true;
  110. _top = (_left = (_right = (_bottom = all)));
  111. }
  112. public Padding(int left, int top, int right, int bottom)
  113. {
  114. _top = top;
  115. _left = left;
  116. _right = right;
  117. _bottom = bottom;
  118. _all = _top == _left && _top == _right && _top == _bottom;
  119. }
  120. public override bool Equals(object other)
  121. {
  122. if (other is not Padding otherPadding)
  123. {
  124. return false;
  125. }
  126. return Equals(otherPadding);
  127. }
  128. public bool Equals(Padding other)
  129. => Left == other.Left
  130. && Top == other.Top
  131. && Right == other.Right
  132. && Bottom == other.Bottom;
  133. public static Padding operator +(Padding p1, Padding p2)
  134. {
  135. return new Padding(p1.Left + p2.Left, p1.Top + p2.Top, p1.Right + p2.Right, p1.Bottom + p2.Bottom);
  136. }
  137. public static Padding operator -(Padding p1, Padding p2)
  138. {
  139. return new Padding(p1.Left - p2.Left, p1.Top - p2.Top, p1.Right - p2.Right, p1.Bottom - p2.Bottom);
  140. }
  141. public static bool operator ==(Padding p1, Padding p2)
  142. {
  143. return p1.Left == p2.Left && p1.Top == p2.Top && p1.Right == p2.Right && p1.Bottom == p2.Bottom;
  144. }
  145. public static bool operator !=(Padding p1, Padding p2) => !(p1 == p2);
  146. public override int GetHashCode()
  147. {
  148. var hashCode = 1903003160;
  149. hashCode = hashCode * -1521134295 + Left.GetHashCode();
  150. hashCode = hashCode * -1521134295 + Top.GetHashCode();
  151. hashCode = hashCode * -1521134295 + Right.GetHashCode();
  152. hashCode = hashCode * -1521134295 + Bottom.GetHashCode();
  153. return hashCode;
  154. }
  155. internal bool ShouldSerializeAll()
  156. {
  157. return _all;
  158. }
  159. }
  160. }