BorderRenderHelper.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. using System;
  2. using Avalonia.Media;
  3. using Avalonia.Platform;
  4. namespace Avalonia.Controls.Utils
  5. {
  6. internal class BorderRenderHelper
  7. {
  8. private bool _useComplexRendering;
  9. private bool? _backendSupportsIndividualCorners;
  10. private StreamGeometry _backgroundGeometryCache;
  11. private StreamGeometry _borderGeometryCache;
  12. private Size _size;
  13. private Thickness _borderThickness;
  14. private CornerRadius _cornerRadius;
  15. private bool _initialized;
  16. void Update(Size finalSize, Thickness borderThickness, CornerRadius cornerRadius)
  17. {
  18. _backendSupportsIndividualCorners ??= AvaloniaLocator.Current.GetService<IPlatformRenderInterface>()
  19. .SupportsIndividualRoundRects;
  20. _size = finalSize;
  21. _borderThickness = borderThickness;
  22. _cornerRadius = cornerRadius;
  23. _initialized = true;
  24. if (borderThickness.IsUniform && (cornerRadius.IsUniform || _backendSupportsIndividualCorners == true))
  25. {
  26. _backgroundGeometryCache = null;
  27. _borderGeometryCache = null;
  28. _useComplexRendering = false;
  29. }
  30. else
  31. {
  32. _useComplexRendering = true;
  33. var boundRect = new Rect(finalSize);
  34. var innerRect = boundRect.Deflate(borderThickness);
  35. BorderGeometryKeypoints backgroundKeypoints = null;
  36. StreamGeometry backgroundGeometry = null;
  37. if (innerRect.Width != 0 && innerRect.Height != 0)
  38. {
  39. backgroundGeometry = new StreamGeometry();
  40. backgroundKeypoints = new BorderGeometryKeypoints(innerRect, borderThickness, cornerRadius, true);
  41. using (var ctx = backgroundGeometry.Open())
  42. {
  43. CreateGeometry(ctx, innerRect, backgroundKeypoints);
  44. }
  45. _backgroundGeometryCache = backgroundGeometry;
  46. }
  47. else
  48. {
  49. _backgroundGeometryCache = null;
  50. }
  51. if (boundRect.Width != 0 && innerRect.Height != 0)
  52. {
  53. var borderGeometryKeypoints = new BorderGeometryKeypoints(boundRect, borderThickness, cornerRadius, false);
  54. var borderGeometry = new StreamGeometry();
  55. using (var ctx = borderGeometry.Open())
  56. {
  57. CreateGeometry(ctx, boundRect, borderGeometryKeypoints);
  58. if (backgroundGeometry != null)
  59. {
  60. CreateGeometry(ctx, innerRect, backgroundKeypoints);
  61. }
  62. }
  63. _borderGeometryCache = borderGeometry;
  64. }
  65. else
  66. {
  67. _borderGeometryCache = null;
  68. }
  69. }
  70. }
  71. public void Render(DrawingContext context,
  72. Size finalSize, Thickness borderThickness, CornerRadius cornerRadius,
  73. IBrush background, IBrush borderBrush, BoxShadows boxShadows)
  74. {
  75. if (_size != finalSize
  76. || _borderThickness != borderThickness
  77. || _cornerRadius != cornerRadius
  78. || !_initialized)
  79. Update(finalSize, borderThickness, cornerRadius);
  80. RenderCore(context, background, borderBrush, boxShadows);
  81. }
  82. void RenderCore(DrawingContext context, IBrush background, IBrush borderBrush, BoxShadows boxShadows)
  83. {
  84. if (_useComplexRendering)
  85. {
  86. var backgroundGeometry = _backgroundGeometryCache;
  87. if (backgroundGeometry != null)
  88. {
  89. context.DrawGeometry(background, null, backgroundGeometry);
  90. }
  91. var borderGeometry = _borderGeometryCache;
  92. if (borderGeometry != null)
  93. {
  94. context.DrawGeometry(borderBrush, null, borderGeometry);
  95. }
  96. }
  97. else
  98. {
  99. var borderThickness = _borderThickness.Top;
  100. IPen pen = null;
  101. if (borderThickness > 0)
  102. {
  103. pen = new Pen(borderBrush, borderThickness);
  104. }
  105. var rrect = new RoundedRect(new Rect(_size), _cornerRadius.TopLeft, _cornerRadius.TopRight,
  106. _cornerRadius.BottomRight, _cornerRadius.BottomLeft);
  107. if (Math.Abs(borderThickness) > double.Epsilon)
  108. {
  109. rrect = rrect.Deflate(borderThickness * 0.5, borderThickness * 0.5);
  110. }
  111. context.PlatformImpl.DrawRectangle(background, pen, rrect, boxShadows);
  112. }
  113. }
  114. private static void CreateGeometry(StreamGeometryContext context, Rect boundRect, BorderGeometryKeypoints keypoints)
  115. {
  116. context.BeginFigure(keypoints.TopLeft, true);
  117. // Top
  118. context.LineTo(keypoints.TopRight);
  119. // TopRight corner
  120. var radiusX = boundRect.TopRight.X - keypoints.TopRight.X;
  121. var radiusY = keypoints.RightTop.Y - boundRect.TopRight.Y;
  122. if (radiusX != 0 || radiusY != 0)
  123. {
  124. context.ArcTo(keypoints.RightTop, new Size(radiusY, radiusY), 0, false, SweepDirection.Clockwise);
  125. }
  126. // Right
  127. context.LineTo(keypoints.RightBottom);
  128. // BottomRight corner
  129. radiusX = boundRect.BottomRight.X - keypoints.BottomRight.X;
  130. radiusY = boundRect.BottomRight.Y - keypoints.RightBottom.Y;
  131. if (radiusX != 0 || radiusY != 0)
  132. {
  133. context.ArcTo(keypoints.BottomRight, new Size(radiusX, radiusY), 0, false, SweepDirection.Clockwise);
  134. }
  135. // Bottom
  136. context.LineTo(keypoints.BottomLeft);
  137. // BottomLeft corner
  138. radiusX = keypoints.BottomLeft.X - boundRect.BottomLeft.X;
  139. radiusY = boundRect.BottomLeft.Y - keypoints.LeftBottom.Y;
  140. if (radiusX != 0 || radiusY != 0)
  141. {
  142. context.ArcTo(keypoints.LeftBottom, new Size(radiusX, radiusY), 0, false, SweepDirection.Clockwise);
  143. }
  144. // Left
  145. context.LineTo(keypoints.LeftTop);
  146. // TopLeft corner
  147. radiusX = keypoints.TopLeft.X - boundRect.TopLeft.X;
  148. radiusY = keypoints.LeftTop.Y - boundRect.TopLeft.Y;
  149. if (radiusX != 0 || radiusY != 0)
  150. {
  151. context.ArcTo(keypoints.TopLeft, new Size(radiusX, radiusY), 0, false, SweepDirection.Clockwise);
  152. }
  153. context.EndFigure(true);
  154. }
  155. private class BorderGeometryKeypoints
  156. {
  157. internal BorderGeometryKeypoints(Rect boundRect, Thickness borderThickness, CornerRadius cornerRadius, bool inner)
  158. {
  159. var left = 0.5 * borderThickness.Left;
  160. var top = 0.5 * borderThickness.Top;
  161. var right = 0.5 * borderThickness.Right;
  162. var bottom = 0.5 * borderThickness.Bottom;
  163. double leftTopY;
  164. double topLeftX;
  165. double topRightX;
  166. double rightTopY;
  167. double rightBottomY;
  168. double bottomRightX;
  169. double bottomLeftX;
  170. double leftBottomY;
  171. if (inner)
  172. {
  173. leftTopY = Math.Max(0, cornerRadius.TopLeft - top) + boundRect.TopLeft.Y;
  174. topLeftX = Math.Max(0, cornerRadius.TopLeft - left) + boundRect.TopLeft.X;
  175. topRightX = boundRect.Width - Math.Max(0, cornerRadius.TopRight - top) + boundRect.TopLeft.X;
  176. rightTopY = Math.Max(0, cornerRadius.TopRight - right) + boundRect.TopLeft.Y;
  177. rightBottomY = boundRect.Height - Math.Max(0, cornerRadius.BottomRight - bottom) + boundRect.TopLeft.Y;
  178. bottomRightX = boundRect.Width - Math.Max(0, cornerRadius.BottomRight - right) + boundRect.TopLeft.X;
  179. bottomLeftX = Math.Max(0, cornerRadius.BottomLeft - left) + boundRect.TopLeft.X;
  180. leftBottomY = boundRect.Height - Math.Max(0, cornerRadius.BottomLeft - bottom) + boundRect.TopLeft.Y;
  181. }
  182. else
  183. {
  184. leftTopY = cornerRadius.TopLeft + top + boundRect.TopLeft.Y;
  185. topLeftX = cornerRadius.TopLeft + left + boundRect.TopLeft.X;
  186. topRightX = boundRect.Width - (cornerRadius.TopRight + right) + boundRect.TopLeft.X;
  187. rightTopY = cornerRadius.TopRight + top + boundRect.TopLeft.Y;
  188. rightBottomY = boundRect.Height - (cornerRadius.BottomRight + bottom) + boundRect.TopLeft.Y;
  189. bottomRightX = boundRect.Width - (cornerRadius.BottomRight + right) + boundRect.TopLeft.X;
  190. bottomLeftX = cornerRadius.BottomLeft + left + boundRect.TopLeft.X;
  191. leftBottomY = boundRect.Height - (cornerRadius.BottomLeft + bottom) + boundRect.TopLeft.Y;
  192. }
  193. var leftTopX = boundRect.TopLeft.X;
  194. var topLeftY = boundRect.TopLeft.Y;
  195. var topRightY = boundRect.TopLeft.Y;
  196. var rightTopX = boundRect.Width + boundRect.TopLeft.X;
  197. var rightBottomX = boundRect.Width + boundRect.TopLeft.X;
  198. var bottomRightY = boundRect.Height + boundRect.TopLeft.Y;
  199. var bottomLeftY = boundRect.Height + boundRect.TopLeft.Y;
  200. var leftBottomX = boundRect.TopLeft.X;
  201. LeftTop = new Point(leftTopX, leftTopY);
  202. TopLeft = new Point(topLeftX, topLeftY);
  203. TopRight = new Point(topRightX, topRightY);
  204. RightTop = new Point(rightTopX, rightTopY);
  205. RightBottom = new Point(rightBottomX, rightBottomY);
  206. BottomRight = new Point(bottomRightX, bottomRightY);
  207. BottomLeft = new Point(bottomLeftX, bottomLeftY);
  208. LeftBottom = new Point(leftBottomX, leftBottomY);
  209. // Fix overlap
  210. if (TopLeft.X > TopRight.X)
  211. {
  212. var scaledX = topLeftX / (topLeftX + topRightX) * boundRect.Width;
  213. TopLeft = new Point(scaledX, TopLeft.Y);
  214. TopRight = new Point(scaledX, TopRight.Y);
  215. }
  216. if (RightTop.Y > RightBottom.Y)
  217. {
  218. var scaledY = rightBottomY / (rightTopY + rightBottomY) * boundRect.Height;
  219. RightTop = new Point(RightTop.X, scaledY);
  220. RightBottom = new Point(RightBottom.X, scaledY);
  221. }
  222. if (BottomRight.X < BottomLeft.X)
  223. {
  224. var scaledX = bottomLeftX / (bottomLeftX + bottomRightX) * boundRect.Width;
  225. BottomRight = new Point(scaledX, BottomRight.Y);
  226. BottomLeft = new Point(scaledX, BottomLeft.Y);
  227. }
  228. if (LeftBottom.Y < LeftTop.Y)
  229. {
  230. var scaledY = leftTopY / (leftTopY + leftBottomY) * boundRect.Height;
  231. LeftBottom = new Point(LeftBottom.X, scaledY);
  232. LeftTop = new Point(LeftTop.X, scaledY);
  233. }
  234. }
  235. internal Point LeftTop { get; }
  236. internal Point TopLeft { get; }
  237. internal Point TopRight { get; }
  238. internal Point RightTop { get; }
  239. internal Point RightBottom { get; }
  240. internal Point BottomRight { get; }
  241. internal Point BottomLeft { get; }
  242. internal Point LeftBottom { get; }
  243. }
  244. }
  245. }