Skip to content

Commit 6348b04

Browse files
Update AbsoluteLayout Flags Extensions (#216)
1 parent 7658c62 commit 6348b04

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

samples/CommunityToolkit.Maui.Markup.Sample/Pages/SettingsPage.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public SettingsPage(SettingsViewModel settingsViewModel) : base(settingsViewMode
1111
Children =
1212
{
1313
new Image().Source("dotnet_bot.png").Opacity(0.25).IsOpaque(false).Aspect(Aspect.AspectFit)
14+
.ClearLayoutFlags()
1415
.LayoutFlags(AbsoluteLayoutFlags.SizeProportional | AbsoluteLayoutFlags.PositionProportional)
1516
.LayoutBounds(0.5, 0.5, 0.5, 0.5)
1617
.AutomationIsInAccessibleTree(false),
@@ -27,7 +28,7 @@ public SettingsPage(SettingsViewModel settingsViewModel) : base(settingsViewMode
2728
new Entry { Keyboard = Keyboard.Numeric }
2829
.BackgroundColor(Colors.White)
2930
.Placeholder($"Provide a value between {SettingsService.MinimumStoriesToFetch} and {SettingsService.MaximumStoriesToFetch}", Colors.Grey)
30-
.LayoutFlags(AbsoluteLayoutFlags.XProportional | AbsoluteLayoutFlags.WidthProportional)
31+
.LayoutFlags(AbsoluteLayoutFlags.XProportional, AbsoluteLayoutFlags.WidthProportional)
3132
.LayoutBounds(0.5, 45, 0.8, 40)
3233
.Behaviors(new NumericValidationBehavior
3334
{

src/CommunityToolkit.Maui.Markup.UnitTests/AbsoluteLayoutExtensionsTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ public void LayoutFlags() => TestPropertiesSet(
1212
b => b.LayoutFlags(AbsoluteLayoutFlags.PositionProportional),
1313
(AbsoluteLayout.LayoutFlagsProperty, AbsoluteLayoutFlags.PositionProportional));
1414

15+
[Test]
16+
public void MultipleLayoutFlagsUsingBooleanLogic() => TestPropertiesSet(
17+
b => b.LayoutFlags(AbsoluteLayoutFlags.PositionProportional | AbsoluteLayoutFlags.XProportional),
18+
(AbsoluteLayout.LayoutFlagsProperty, AbsoluteLayoutFlags.PositionProportional | AbsoluteLayoutFlags.XProportional));
19+
20+
[Test]
21+
public void MultipleLayoutFlagsUsingParams() => TestPropertiesSet(
22+
b => b.LayoutFlags(AbsoluteLayoutFlags.PositionProportional, AbsoluteLayoutFlags.XProportional),
23+
(AbsoluteLayout.LayoutFlagsProperty, AbsoluteLayoutFlags.PositionProportional | AbsoluteLayoutFlags.XProportional));
24+
1525
[Test]
1626
public void LayoutBoundsPositionOnlyDouble() => TestPropertiesSet(
1727
b =>
@@ -67,4 +77,20 @@ public void LayoutBoundsAllDouble() => TestPropertiesSet(
6777
b.LayoutBounds(100, 100, 100, 100);
6878
},
6979
(AbsoluteLayout.LayoutBoundsProperty, new Rect(100, 100, 100, 100)));
80+
81+
// Cannot use TestPropertiesSet() because ClearLayoutFlags sets AbsoluteLayout.LayoutFlagsProperty to its default
82+
[Test]
83+
public void ClearLayoutFlags()
84+
{
85+
var label = new Label().ClearLayoutFlags();
86+
Assert.AreEqual(AbsoluteLayoutFlags.None, label.GetPropertyIfSet(AbsoluteLayout.LayoutFlagsProperty, (AbsoluteLayoutFlags)(-1)));
87+
}
88+
89+
// Cannot use TestPropertiesSet() because ClearLayoutFlags sets AbsoluteLayout.LayoutFlagsProperty to its default
90+
[Test]
91+
public void ClearLayoutFlagsAfterSettingLayoutFlags()
92+
{
93+
var label = new Label().LayoutFlags(AbsoluteLayoutFlags.XProportional).ClearLayoutFlags();
94+
Assert.AreEqual(AbsoluteLayoutFlags.None, label.GetPropertyIfSet(AbsoluteLayout.LayoutFlagsProperty, AbsoluteLayoutFlags.XProportional));
95+
}
7096
}

src/CommunityToolkit.Maui.Markup/AbsoluteLayoutExtensions.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,25 @@ namespace CommunityToolkit.Maui.Markup;
77
/// </summary>
88
public static class AbsoluteLayoutExtensions
99
{
10+
/// <summary>
11+
/// Removes all <see cref="AbsoluteLayoutFlags"/>, reverting to the default configuraion of <see cref="AbsoluteLayoutFlags.None"/>.
12+
/// </summary>
13+
/// <typeparam name="TBindable"></typeparam>
14+
/// <param name="bindable"></param>
15+
/// <returns></returns>
16+
public static TBindable ClearLayoutFlags<TBindable>(this TBindable bindable) where TBindable : BindableObject
17+
{
18+
AbsoluteLayout.SetLayoutFlags(bindable, AbsoluteLayoutFlags.None);
19+
return bindable;
20+
}
21+
1022
/// <summary>
1123
/// Set an <see cref="AbsoluteLayoutFlags"/> that indicates whether the layout bounds position and size values for a child are proportional to the size of the <see cref="AbsoluteLayout"/>.
24+
/// Appends <paramref name="flag"/> to existing <see cref="AbsoluteLayoutFlags"/>
1225
/// </summary>
26+
/// <remarks>
27+
/// To clear existing <see cref="AbsoluteLayoutFlags"/>, use <see cref="ClearLayoutFlags{TBindable}(TBindable)"/>
28+
/// </remarks>
1329
/// <typeparam name="TBindable"></typeparam>
1430
/// <param name="bindable"></param>
1531
/// <param name="flag"></param>
@@ -20,6 +36,29 @@ public static TBindable LayoutFlags<TBindable>(this TBindable bindable, Absolute
2036
return bindable;
2137
}
2238

39+
/// <summary>
40+
/// Set an <see cref="AbsoluteLayoutFlags"/> that indicates whether the layout bounds position and size values for a child are proportional to the size of the <see cref="AbsoluteLayout"/>.
41+
/// </summary>
42+
/// <remarks>
43+
/// To clear existing <see cref="AbsoluteLayoutFlags"/>, use <see cref="ClearLayoutFlags{TBindable}(TBindable)"/>
44+
/// </remarks>
45+
/// <typeparam name="TBindable"></typeparam>
46+
/// <param name="bindable"></param>
47+
/// <param name="flags"></param>
48+
/// <returns></returns>
49+
public static TBindable LayoutFlags<TBindable>(this TBindable bindable, params AbsoluteLayoutFlags[] flags) where TBindable : BindableObject
50+
{
51+
var newFlags = AbsoluteLayoutFlags.None;
52+
53+
foreach(var flag in flags)
54+
{
55+
newFlags |= flag;
56+
}
57+
58+
AbsoluteLayout.SetLayoutFlags(bindable, newFlags);
59+
return bindable;
60+
}
61+
2362
/// <summary>
2463
/// Set the position and size of a <see cref="View"/> in an <see cref="AbsoluteLayout"/>
2564
/// </summary>

0 commit comments

Comments
 (0)