Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit ccf02c4

Browse files
#704 [Bug] No padding around text in a Toast or Snackbar (#714)
1 parent ab0df7f commit ccf02c4

21 files changed

+271
-154
lines changed

samples/XCT.Sample/Pages/Views/SnackBarPage.xaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
<StackLayout Spacing="10" Margin="20">
88
<Button Clicked="DisplaySnackBarClicked" Text="Show SnackBar"></Button>
99
<Button Clicked="DisplayToastClicked" Text="Show toast"></Button>
10-
<Button Clicked="DisplaySnackBarAdvancedClicked" Text="Show SnackBar"></Button>
10+
<Button Clicked="DisplaySnackBarAdvancedClicked" Text="Show Advanced SnackBar"></Button>
11+
<Button Clicked="DisplaySnackBarWithPadding" Text="Show SnackBar with Padding"></Button>
1112
<Label x:Name="StatusText"></Label>
1213
</StackLayout>
1314
</pages:BasePage>

samples/XCT.Sample/Pages/Views/SnackBarPage.xaml.cs

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,22 @@ async void DisplaySnackBarClicked(object sender, EventArgs args)
2424
StatusText.Text = result ? "SnackBar is closed by user" : "SnackBar is closed by timeout";
2525
}
2626

27+
async void DisplaySnackBarWithPadding(object sender, EventArgs args)
28+
{
29+
var options = new SnackBarOptions()
30+
{
31+
BackgroundColor = Color.FromHex("#CC0000"),
32+
MessageOptions = new MessageOptions
33+
{
34+
Message = "msg",
35+
Foreground = Color.White,
36+
Font = Font.SystemFontOfSize(16),
37+
Padding = new Thickness(10, 20, 30, 40)
38+
}
39+
};
40+
41+
await this.DisplaySnackBarAsync(options);
42+
}
2743
async void DisplayToastClicked(object sender, EventArgs args)
2844
{
2945
await this.DisplayToastAsync(GenerateLongText(5));
@@ -33,48 +49,48 @@ async void DisplayToastClicked(object sender, EventArgs args)
3349
async void DisplaySnackBarAdvancedClicked(object sender, EventArgs args)
3450
{
3551
const string SmileIcon = "\uf118";
36-
var messageOptions = new MessageOptions
37-
{
38-
Foreground = Color.DeepSkyBlue,
39-
Font = Font.OfSize("FARegular", 40),
40-
Message = SmileIcon
41-
};
42-
43-
var actionOptions = new List<SnackBarActionOptions>
52+
var options = new SnackBarOptions
4453
{
45-
new SnackBarActionOptions
54+
MessageOptions = new MessageOptions
4655
{
47-
ForegroundColor = Color.Red,
48-
BackgroundColor = Color.Green,
49-
Font = Font.OfSize("Times New Roman", 15),
50-
Text = "Action1",
51-
Action = () =>
52-
{
53-
Debug.WriteLine("1");
54-
return Task.CompletedTask;
55-
}
56+
Foreground = Color.DeepSkyBlue,
57+
Font = Font.OfSize("FARegular", 40),
58+
Padding = new Thickness(10, 20, 30, 40),
59+
Message = SmileIcon
5660
},
57-
new SnackBarActionOptions
61+
Duration = TimeSpan.FromMilliseconds(5000),
62+
BackgroundColor = Color.Coral,
63+
IsRtl = CultureInfo.CurrentCulture.TextInfo.IsRightToLeft,
64+
Actions = new List<SnackBarActionOptions>
5865
{
59-
ForegroundColor = Color.Green,
60-
BackgroundColor = Color.Red,
61-
Font = Font.OfSize("Times New Roman", 20),
62-
Text = "Action2",
63-
Action = () =>
66+
new SnackBarActionOptions
67+
{
68+
ForegroundColor = Color.Red,
69+
BackgroundColor = Color.Green,
70+
Font = Font.OfSize("Times New Roman", 15),
71+
Padding = new Thickness(10, 20, 30, 40),
72+
Text = "Action1",
73+
Action = () =>
74+
{
75+
Debug.WriteLine("1");
76+
return Task.CompletedTask;
77+
}
78+
},
79+
new SnackBarActionOptions
6480
{
65-
Debug.WriteLine("2");
66-
return Task.CompletedTask;
81+
ForegroundColor = Color.Green,
82+
BackgroundColor = Color.Red,
83+
Font = Font.OfSize("Times New Roman", 20),
84+
Padding = new Thickness(40, 30, 20, 10),
85+
Text = "Action2",
86+
Action = () =>
87+
{
88+
Debug.WriteLine("2");
89+
return Task.CompletedTask;
90+
}
6791
}
6892
}
6993
};
70-
var options = new SnackBarOptions
71-
{
72-
MessageOptions = messageOptions,
73-
Duration = TimeSpan.FromMilliseconds(5000),
74-
BackgroundColor = Color.Coral,
75-
IsRtl = CultureInfo.CurrentCulture.TextInfo.IsRightToLeft,
76-
Actions = actionOptions
77-
};
7894
var result = await this.DisplaySnackBarAsync(options);
7995
StatusText.Text = result ? "SnackBar is closed by user" : "SnackBar is closed by timeout";
8096
}

src/CommunityToolkit/Xamarin.CommunityToolkit/Views/Snackbar/Helpers/NativeSnackButton.ios.macos.cs

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,75 @@
11
using System;
22
using System.Threading.Tasks;
3+
using CoreGraphics;
34
#if __IOS__
45
using Xamarin.CommunityToolkit.UI.Views.Helpers.iOS;
6+
using UIKit;
57
#elif __MACOS__
68
using Xamarin.CommunityToolkit.UI.Views.Helpers.macOS;
9+
using AppKit;
710
#endif
811

912
namespace Xamarin.CommunityToolkit.Views.Snackbar.Helpers
1013
{
11-
class NativeActionButton
14+
#if __IOS__
15+
class NativeSnackButton : UIButton
16+
#else
17+
class NativeSnackButton : NSButton
18+
#endif
1219
{
13-
public Func<Task> Action { get; protected set; }
20+
public NativeSnackButton(double left, double top, double right, double bottom)
21+
#if __IOS__
22+
: base(UIButtonType.System)
23+
#endif
24+
{
25+
Left = left;
26+
Top = top;
27+
Right = right;
28+
Bottom = bottom;
29+
LineBreakMode = NativeSnackButtonAppearance.LineBreakMode;
30+
#if __IOS__
31+
ContentEdgeInsets = new UIEdgeInsets((nfloat)top, (nfloat)left, (nfloat)bottom, (nfloat)right);
32+
TouchUpInside += async (s, e) =>
33+
{
34+
await SnackButtonAction();
35+
};
36+
}
37+
#else
38+
WantsLayer = true;
39+
Activated += async (s, e) =>
40+
{
41+
await SnackButtonAction();
42+
};
43+
}
1444

15-
public string ActionButtonText { get; protected set; }
45+
public override CGSize IntrinsicContentSize => new CGSize(
46+
base.IntrinsicContentSize.Width + Left + Right,
47+
base.IntrinsicContentSize.Height + Top + Bottom);
48+
#endif
49+
50+
public double Left { get; }
51+
52+
public double Top { get; }
53+
54+
public double Right { get; }
1655

17-
public NativeSnackButtonAppearance Appearance { get; protected set; } = new NativeSnackButtonAppearance();
56+
public double Bottom { get; }
1857

19-
public NativeActionButton SetAction(Func<Task> action)
58+
public Func<Task> SnackButtonAction { get; protected set; }
59+
60+
public NativeSnackButton SetAction(Func<Task> action)
2061
{
21-
Action = action;
62+
SnackButtonAction = action;
2263
return this;
2364
}
2465

25-
public NativeActionButton SetActionButtonText(string title)
66+
public NativeSnackButton SetActionButtonText(string title)
2667
{
27-
ActionButtonText = title;
68+
#if __IOS__
69+
SetTitle(title, UIControlState.Normal);
70+
#else
71+
Title = title;
72+
#endif
2873
return this;
2974
}
3075
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using CoreGraphics;
3+
#if __IOS__
4+
using UIKit;
5+
#else
6+
using AppKit;
7+
#endif
8+
9+
namespace Xamarin.CommunityToolkit.UI.Views.Helpers
10+
{
11+
#if __IOS__
12+
class PaddedLabel : UILabel
13+
#else
14+
class PaddedLabel : NSTextField
15+
#endif
16+
{
17+
public PaddedLabel(nfloat left, nfloat top, nfloat right, nfloat bottom)
18+
{
19+
Left = left;
20+
Top = top;
21+
Right = right;
22+
Bottom = bottom;
23+
}
24+
25+
public nfloat Left { get; }
26+
27+
public nfloat Top { get; }
28+
29+
public nfloat Right { get; }
30+
31+
public nfloat Bottom { get; }
32+
33+
public override CGSize IntrinsicContentSize => new CGSize(
34+
base.IntrinsicContentSize.Width + Left + Right,
35+
base.IntrinsicContentSize.Height + Top + Bottom);
36+
37+
#if __IOS__
38+
public override void DrawText(CGRect rect)
39+
{
40+
var insets = new UIEdgeInsets(Top, Left, Bottom, Right);
41+
base.DrawText(insets.InsetRect(rect));
42+
}
43+
#endif
44+
}
45+
}

src/CommunityToolkit/Xamarin.CommunityToolkit/Views/Snackbar/Helpers/SnackBarLayout.ios.macos.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,19 @@ class SnackBarLayout
66
{
77
public nfloat MarginBottom { get; set; } = 10.0f;
88

9-
public nfloat MarginCenter { get; set; } = 0.0f;
10-
11-
public nfloat MarginLeading { get; set; } = 10.0f;
9+
public nfloat MarginLeft { get; set; } = 10.0f;
1210

1311
public nfloat MarginTop { get; set; } = 10.0f;
1412

15-
public nfloat MarginTrailing { get; set; } = 10.0f;
13+
public nfloat MarginRight { get; set; } = 10.0f;
1614

1715
public nfloat PaddingBottom { get; set; } = 10.0f;
1816

19-
public nfloat PaddingLeading { get; set; } = 10.0f;
17+
public nfloat PaddingLeft { get; set; } = 10.0f;
2018

2119
public nfloat PaddingTop { get; set; } = 10.0f;
2220

23-
public nfloat PaddingTrailing { get; set; } = 10.0f;
21+
public nfloat PaddingRight { get; set; } = 10.0f;
2422

2523
public nfloat Spacing { get; set; } = 10.0f;
2624
}

src/CommunityToolkit/Xamarin.CommunityToolkit/Views/Snackbar/Helpers/SnackBarLayout.uwp.wpf.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,25 @@ public SnackBarLayout(SnackBarOptions options)
3333
#else
3434
var messageLabel = new Label
3535
{
36-
Content = options.MessageOptions.Message,
36+
Content = options.MessageOptions.Message
3737
};
3838
#endif
39+
messageLabel.Padding = new Thickness(options.MessageOptions.Padding.Left,
40+
options.MessageOptions.Padding.Top,
41+
options.MessageOptions.Padding.Right,
42+
options.MessageOptions.Padding.Bottom);
43+
3944
if (options.MessageOptions.Font != Forms.Font.Default)
4045
{
41-
messageLabel.FontSize = options.MessageOptions.Font.FontSize;
42-
messageLabel.FontFamily = new FontFamily(options.MessageOptions.Font.FontFamily);
46+
if (options.MessageOptions.Font.FontSize > 0)
47+
{
48+
messageLabel.FontSize = options.MessageOptions.Font.FontSize;
49+
}
50+
51+
if (options.MessageOptions.Font.FontFamily != null)
52+
{
53+
messageLabel.FontFamily = new FontFamily(options.MessageOptions.Font.FontFamily);
54+
}
4355
}
4456

4557
if (options.MessageOptions.Foreground != Forms.Color.Default)
@@ -61,7 +73,11 @@ public SnackBarLayout(SnackBarOptions options)
6173
{
6274
OnSnackBarActionExecuted?.Invoke();
6375
await action.Action();
64-
})
76+
}),
77+
Padding = new Thickness(action.Padding.Left,
78+
action.Padding.Top,
79+
action.Padding.Right,
80+
action.Padding.Bottom)
6581
};
6682
if (action.Font != Forms.Font.Default)
6783
{

src/CommunityToolkit/Xamarin.CommunityToolkit/Views/Snackbar/Helpers/iOS/IOSSnackBar.ios.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class NativeSnackBar
1414
{
1515
NSTimer timer;
1616

17-
public List<NativeActionButton> Actions { get; protected set; } = new List<NativeActionButton>();
17+
public List<NativeSnackButton> Actions { get; protected set; } = new List<NativeSnackButton>();
1818

1919
public Func<Task> TimeoutAction { get; protected set; }
2020

src/CommunityToolkit/Xamarin.CommunityToolkit/Views/Snackbar/Helpers/iOS/SnackBarAppearance.ios.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,9 @@ class NativeSnackBarAppearance
1919
public static UIFont DefaultFont { get; } = Forms.Font.Default.ToUIFont();
2020
}
2121

22-
class NativeSnackButtonAppearance
22+
static class NativeSnackButtonAppearance
2323
{
24-
public UIColor Background { get; set; } = DefaultColor;
25-
26-
public UIColor Foreground { get; set; } = DefaultColor;
27-
28-
public UIFont Font { get; set; } = DefaultFont;
29-
30-
public UILineBreakMode LineBreakMode { get; set; } = UILineBreakMode.MiddleTruncation;
24+
public static UILineBreakMode LineBreakMode { get; set; } = UILineBreakMode.MiddleTruncation;
3125

3226
public static UIColor DefaultColor { get; } = Forms.Color.Default.ToUIColor();
3327

src/CommunityToolkit/Xamarin.CommunityToolkit/Views/Snackbar/Helpers/iOS/SnackbarViews/ActionMessageSnackBarView.ios.cs

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using System;
2-
using UIKit;
1+
using UIKit;
32
using Xamarin.CommunityToolkit.UI.Views.Helpers.iOS.SnackBarViews;
3+
using Xamarin.CommunityToolkit.Views.Snackbar.Helpers;
44

55
namespace Xamarin.CommunityToolkit.UI.Views.Helpers.iOS
66
{
@@ -11,39 +11,12 @@ public ActionMessageSnackBarView(NativeSnackBar snackBar)
1111
{
1212
}
1313

14-
// Gets the maximum width of the action button. Possible values 0 to 1.
15-
protected virtual nfloat ActionButtonMaxWidth => 1f;
16-
1714
protected override void Initialize()
1815
{
1916
base.Initialize();
2017

21-
foreach (var action in SnackBar.Actions)
18+
foreach (var actionButton in SnackBar.Actions)
2219
{
23-
var actionButton = new UIButton(UIButtonType.System);
24-
if (action.Appearance.Background != NativeSnackButtonAppearance.DefaultColor)
25-
{
26-
actionButton.BackgroundColor = action.Appearance.Background;
27-
}
28-
29-
if (action.Appearance.Foreground != NativeSnackButtonAppearance.DefaultColor)
30-
{
31-
actionButton.SetTitleColor(action.Appearance.Foreground, UIControlState.Normal);
32-
}
33-
34-
if (action.Appearance.Font != NativeSnackButtonAppearance.DefaultFont)
35-
{
36-
actionButton.Font = action.Appearance.Font;
37-
}
38-
39-
actionButton.SetTitle(action.ActionButtonText, UIControlState.Normal);
40-
actionButton.TitleLabel.LineBreakMode = action.Appearance.LineBreakMode;
41-
actionButton.TouchUpInside += async (s, e) =>
42-
{
43-
await action.Action();
44-
Dismiss();
45-
};
46-
4720
StackView.AddArrangedSubview(actionButton);
4821
}
4922
}

0 commit comments

Comments
 (0)