-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
RoundRectangle.cs
116 lines (89 loc) · 3.2 KB
/
RoundRectangle.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#nullable disable
using System;
using System.Runtime.CompilerServices;
using Microsoft.Maui.Graphics;
namespace Microsoft.Maui.Controls.Shapes
{
public sealed partial class RoundRectangle : Shape, IShape, IRoundRectangle
{
public RoundRectangle() : base()
{
Aspect = Stretch.Fill;
}
/// <summary>Bindable property for <see cref="CornerRadius"/>.</summary>
public static readonly BindableProperty CornerRadiusProperty =
BindableProperty.Create(nameof(CornerRadius), typeof(CornerRadius), typeof(RoundRectangle), new CornerRadius());
public CornerRadius CornerRadius
{
set { SetValue(CornerRadiusProperty, value); }
get { return (CornerRadius)GetValue(CornerRadiusProperty); }
}
double _fallbackWidth;
double _fallbackHeight;
internal override double WidthForPathComputation
{
get
{
var width = Width;
return width == -1 ? _fallbackWidth : width;
}
}
internal override double HeightForPathComputation
{
get
{
var height = Height;
return height == -1 ? _fallbackHeight : height;
}
}
// TODO this should move to a remapped mapper
protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
base.OnPropertyChanged(propertyName);
if (propertyName == CornerRadiusProperty.PropertyName)
Handler?.UpdateValue(nameof(IShapeView.Shape));
}
public override PathF GetPath()
{
float w = (float)WidthForPathComputation;
float h = (float)HeightForPathComputation;
float topLeftCornerRadius = (float)CornerRadius.TopLeft;
float topRightCornerRadius = (float)CornerRadius.TopRight;
float bottomLeftCornerRadius = (float)CornerRadius.BottomLeft;
float bottomRightCornerRadius = (float)CornerRadius.BottomRight;
var path = new PathF();
path.AppendRoundedRectangle(0, 0, w, h, topLeftCornerRadius, topRightCornerRadius, bottomLeftCornerRadius, bottomRightCornerRadius);
return path;
}
PathF IShape.PathForBounds(Graphics.Rect viewBounds)
{
_fallbackHeight = viewBounds.Height;
_fallbackWidth = viewBounds.Width;
var path = GetPath();
TransformPathForBounds(path, viewBounds);
return path;
}
internal PathF GetInnerPath(float strokeThickness)
{
float w = (float)(WidthForPathComputation - strokeThickness);
float h = (float)(HeightForPathComputation - strokeThickness);
float x = strokeThickness / 2;
float y = strokeThickness / 2;
float topLeftCornerRadius = (float)Math.Max(0, CornerRadius.TopLeft - strokeThickness);
float topRightCornerRadius = (float)Math.Max(0, CornerRadius.TopRight - strokeThickness);
float bottomLeftCornerRadius = (float)Math.Max(0, CornerRadius.BottomLeft - strokeThickness);
float bottomRightCornerRadius = (float)Math.Max(0, CornerRadius.BottomRight - strokeThickness);
var path = new PathF();
path.AppendRoundedRectangle(x, y, w, h, topLeftCornerRadius, topRightCornerRadius, bottomLeftCornerRadius, bottomRightCornerRadius);
return path;
}
PathF IRoundRectangle.InnerPathForBounds(Rect viewBounds, float strokeThickness)
{
_fallbackHeight = viewBounds.Height;
_fallbackWidth = viewBounds.Width;
var path = GetInnerPath(strokeThickness);
TransformPathForBounds(path, viewBounds);
return path;
}
}
}