Skip to content

Commit 2eacf6c

Browse files
committed
Merge branch 'MacOS' of https://github.com/xabre/xamarin-forms-tab-badge into MacOS
2 parents c9a1e03 + b82fed5 commit 2eacf6c

File tree

3 files changed

+136
-9
lines changed

3 files changed

+136
-9
lines changed

Source/Plugin.Badge.Abstractions/TabBadge.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public static Thickness GetDefaultMargins()
8383
case Device.Android:
8484
return new Thickness(-10, -5);
8585
case Device.UWP:
86-
return new Thickness(0);
86+
case Device.macOS:
8787
case Device.iOS:
8888
return new Thickness(0);
8989
}

Source/Plugin.Badge.Mac/BadgeView.cs

Lines changed: 123 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using System.Linq;
1+
using System;
2+
using System.Linq;
23
using AppKit;
4+
using Plugin.Badge.Abstractions;
35
using Xamarin.Forms;
46
using Xamarin.Forms.Platform.MacOS;
57

@@ -10,6 +12,15 @@ namespace Plugin.Badge.Mac
1012
/// </summary>
1113
public class BadgeView : NSTextField
1214
{
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+
1324
public string Text
1425
{
1526
get => StringValue;
@@ -36,6 +47,28 @@ public Color Color
3647
set => base.Font = value == Font.Default ? NSFont.SystemFontOfSize(NSFont.SmallSystemFontSize) : value.ToNSFont();
3748
}
3849

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+
3972
/// <summary>
4073
/// Creates and adds a badge to the target view.
4174
/// </summary>
@@ -54,20 +87,102 @@ public BadgeView(NSView target, bool alignRelativeToContent = false)
5487
WantsLayer = true;
5588
Layer.CornerRadius = 7.5f;
5689
target.AddSubview(this);
90+
Layer.ZPosition = 1;
5791

5892
WidthAnchor.ConstraintLessThanOrEqualToConstant(35).Active = true;
5993

60-
6194
if (alignRelativeToContent && target.Subviews.Any())
6295
{
63-
LeftAnchor.ConstraintEqualToAnchor(target.Subviews.First().RightAnchor, 0).Active = true;
64-
CenterYAnchor.ConstraintEqualToAnchor(target.Subviews.First().CenterYAnchor).Active = true;
96+
target = target.Subviews.First();
6597
}
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)
67112
{
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+
}
71186
}
72187
}
73188
}

Source/Plugin.Badge.Mac/BadgedTabbedPageRenderer.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,18 @@ protected virtual void OnTabbedPagePropertyChanged(object sender, System.Compone
9595
badgeView.Font = TabBadge.GetBadgeFont(element);
9696
return;
9797
}
98+
99+
if (e.PropertyName == TabBadge.BadgePositionProperty.PropertyName)
100+
{
101+
badgeView.Position = TabBadge.GetBadgePosition(element);
102+
return;
103+
}
104+
105+
if (e.PropertyName == TabBadge.BadgeMarginProperty.PropertyName)
106+
{
107+
badgeView.Margin = TabBadge.GetBadgeMargin(element);
108+
return;
109+
}
98110
}
99111

100112
private void OnTabRemoved(object sender, ElementEventArgs e)

0 commit comments

Comments
 (0)