1
- using System . Linq ;
1
+ using System ;
2
+ using System . Linq ;
2
3
using AppKit ;
4
+ using Plugin . Badge . Abstractions ;
3
5
using Xamarin . Forms ;
4
6
using Xamarin . Forms . Platform . MacOS ;
5
7
@@ -10,6 +12,15 @@ namespace Plugin.Badge.Mac
10
12
/// </summary>
11
13
public class BadgeView : NSTextField
12
14
{
15
+ private NSLayoutConstraint _topConstraint ;
16
+ private NSLayoutConstraint _bottomConstraint ;
17
+ private NSLayoutConstraint _leftConstraint ;
18
+ private NSLayoutConstraint _rightConstraint ;
19
+ private NSLayoutConstraint _horizontalCenterConstraint ;
20
+ private NSLayoutConstraint _verticalCenterConstraint ;
21
+ private BadgePosition _position = BadgePosition . PositionTopRight ;
22
+ private Thickness _margin ;
23
+
13
24
public string Text
14
25
{
15
26
get => StringValue ;
@@ -36,6 +47,28 @@ public Color Color
36
47
set => base . Font = value == Font . Default ? NSFont . SystemFontOfSize ( NSFont . SmallSystemFontSize ) : value . ToNSFont ( ) ;
37
48
}
38
49
50
+ public BadgePosition Position
51
+ {
52
+ get => _position ;
53
+ set
54
+ {
55
+ _position = value ;
56
+ ApplyConstraints ( ) ;
57
+ }
58
+ }
59
+
60
+ public Thickness Margin
61
+ {
62
+ get => _margin ;
63
+ set {
64
+ _margin = value ;
65
+ _leftConstraint . Constant = ( nfloat ) value . Left ;
66
+ _topConstraint . Constant = ( nfloat ) value . Top ;
67
+ _rightConstraint . Constant = ( nfloat ) value . Right ;
68
+ _bottomConstraint . Constant = ( nfloat ) value . Bottom ;
69
+ }
70
+ }
71
+
39
72
/// <summary>
40
73
/// Creates and adds a badge to the target view.
41
74
/// </summary>
@@ -54,20 +87,102 @@ public BadgeView(NSView target, bool alignRelativeToContent = false)
54
87
WantsLayer = true ;
55
88
Layer . CornerRadius = 7.5f ;
56
89
target . AddSubview ( this ) ;
90
+ Layer . ZPosition = 1 ;
57
91
58
92
WidthAnchor . ConstraintLessThanOrEqualToConstant ( 35 ) . Active = true ;
59
93
60
-
61
94
if ( alignRelativeToContent && target . Subviews . Any ( ) )
62
95
{
63
- LeftAnchor . ConstraintEqualToAnchor ( target . Subviews . First ( ) . RightAnchor , 0 ) . Active = true ;
64
- CenterYAnchor . ConstraintEqualToAnchor ( target . Subviews . First ( ) . CenterYAnchor ) . Active = true ;
96
+ target = target . Subviews . First ( ) ;
65
97
}
66
- else
98
+
99
+ _topConstraint = TopAnchor . ConstraintEqualToAnchor ( target . TopAnchor , 0 ) ;
100
+ _bottomConstraint = BottomAnchor . ConstraintEqualToAnchor ( target . BottomAnchor , 0 ) ;
101
+ _leftConstraint = LeftAnchor . ConstraintEqualToAnchor ( target . LeftAnchor , 0 ) ;
102
+ _rightConstraint = RightAnchor . ConstraintEqualToAnchor ( target . RightAnchor , 0 ) ;
103
+ _horizontalCenterConstraint = CenterXAnchor . ConstraintEqualToAnchor ( target . CenterXAnchor , 0 ) ;
104
+ _verticalCenterConstraint = CenterYAnchor . ConstraintEqualToAnchor ( target . CenterYAnchor , 0 ) ;
105
+
106
+ ApplyConstraints ( ) ;
107
+ }
108
+
109
+ private void ApplyConstraints ( )
110
+ {
111
+ switch ( _position )
67
112
{
68
- TopAnchor . ConstraintEqualToAnchor ( target . TopAnchor , 0 ) . Active = true ;
69
- RightAnchor . ConstraintEqualToAnchor ( target . RightAnchor , 0 ) . Active = true ;
70
- }
113
+ case BadgePosition . PositionTopLeft :
114
+ _topConstraint . Active = true ;
115
+ _leftConstraint . Active = true ;
116
+ _rightConstraint . Active = false ;
117
+ _bottomConstraint . Active = false ;
118
+ _horizontalCenterConstraint . Active = false ;
119
+ _verticalCenterConstraint . Active = false ;
120
+ break ;
121
+ case BadgePosition . PositionTopRight :
122
+ _topConstraint . Active = true ;
123
+ _leftConstraint . Active = false ;
124
+ _rightConstraint . Active = true ;
125
+ _bottomConstraint . Active = false ;
126
+ _horizontalCenterConstraint . Active = false ;
127
+ _verticalCenterConstraint . Active = false ;
128
+ break ;
129
+ case BadgePosition . PositionBottomRight :
130
+ _topConstraint . Active = false ;
131
+ _leftConstraint . Active = false ;
132
+ _rightConstraint . Active = true ;
133
+ _bottomConstraint . Active = true ;
134
+ _horizontalCenterConstraint . Active = false ;
135
+ _verticalCenterConstraint . Active = false ;
136
+ break ;
137
+ case BadgePosition . PositionBottomLeft :
138
+ _topConstraint . Active = false ;
139
+ _leftConstraint . Active = true ;
140
+ _rightConstraint . Active = false ;
141
+ _bottomConstraint . Active = true ;
142
+ _horizontalCenterConstraint . Active = false ;
143
+ _verticalCenterConstraint . Active = false ;
144
+ break ;
145
+ case BadgePosition . PositionTopCenter :
146
+ _topConstraint . Active = true ;
147
+ _leftConstraint . Active = false ;
148
+ _rightConstraint . Active = false ;
149
+ _bottomConstraint . Active = false ;
150
+ _horizontalCenterConstraint . Active = true ;
151
+ _verticalCenterConstraint . Active = false ;
152
+ break ;
153
+ case BadgePosition . PositionRightCenter :
154
+ _topConstraint . Active = false ;
155
+ _leftConstraint . Active = false ;
156
+ _rightConstraint . Active = true ;
157
+ _bottomConstraint . Active = false ;
158
+ _horizontalCenterConstraint . Active = false ;
159
+ _verticalCenterConstraint . Active = true ;
160
+ break ;
161
+ case BadgePosition . PositionBottomCenter :
162
+ _topConstraint . Active = false ;
163
+ _leftConstraint . Active = false ;
164
+ _rightConstraint . Active = false ;
165
+ _bottomConstraint . Active = true ;
166
+ _horizontalCenterConstraint . Active = true ;
167
+ _verticalCenterConstraint . Active = false ;
168
+ break ;
169
+ case BadgePosition . PositionLeftCenter :
170
+ _topConstraint . Active = false ;
171
+ _leftConstraint . Active = true ;
172
+ _rightConstraint . Active = false ;
173
+ _bottomConstraint . Active = false ;
174
+ _horizontalCenterConstraint . Active = false ;
175
+ _verticalCenterConstraint . Active = true ;
176
+ break ;
177
+ case BadgePosition . PositionCenter :
178
+ _topConstraint . Active = false ;
179
+ _leftConstraint . Active = false ;
180
+ _rightConstraint . Active = false ;
181
+ _bottomConstraint . Active = false ;
182
+ _horizontalCenterConstraint . Active = true ;
183
+ _verticalCenterConstraint . Active = true ;
184
+ break ;
185
+ }
71
186
}
72
187
}
73
188
}
0 commit comments