Skip to content

Commit

Permalink
Merge pull request #44 from lucacivale/AndroidMargin
Browse files Browse the repository at this point in the history
[Android] Add BottomSheet Margin
  • Loading branch information
lucacivale authored Feb 2, 2025
2 parents 835ed08 + 13aef92 commit 31f9dca
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 83 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,20 @@ xmlns:androidBottomsheet="http://pluginmauibottomsheet.com/platformconfiguration
androidBottomsheet:BottomSheet.MaxWidth="300"
```

### Margin

Set the [`BottomSheet` margin](https://learn.microsoft.com/en-us/dotnet/maui/user-interface/align-position?view=net-maui-9.0#position-controls).
The margin will only be applied on the left on right.

```
MyBottomSheet.On<Android>().SetMargin(new Thickness(10, 0, 10, 0));
or
xmlns:androidBottomsheet="http://pluginmauibottomsheet.com/platformconfiguration/android"
androidBottomsheet:BottomSheet.Margin="10,0,10,0"
```

# XAML usage

In order to make use of sheet within XAML you can use this namespace:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8" ?>

<ContentPage
x:Class="Plugin.Maui.BottomSheet.Sample.ShowCasePage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
Expand Down Expand Up @@ -44,7 +43,6 @@
<bottomsheet:BottomSheet
x:Name="ModalBottomSheet"
Padding="20"
androidBottomsheet:BottomSheet.MaxWidth="300"
CornerRadius="{Binding CornerRadius}"
HasHandle="{Binding HasHandle}"
IsCancelable="{Binding IsCancelable}"
Expand All @@ -53,6 +51,7 @@
IsOpen="{Binding IsOpen}"
ShowHeader="{Binding ShowHeader}"
States="Peek,Medium,Large"
androidBottomsheet:BottomSheet.MaxWidth="300"
WindowBackgroundColor="{Binding WindowBackgroundColor, Mode=TwoWay}">
<bottomsheet:BottomSheet.Header>
<bottomsheet:BottomSheetHeader
Expand Down Expand Up @@ -195,6 +194,7 @@
IsOpen="{Binding IsNonModalOpen}"
ShowHeader="{Binding ShowHeader}"
States="Peek,Medium,Large"
androidBottomsheet:BottomSheet.Margin="20,0,20,0"
WindowBackgroundColor="{Binding WindowBackgroundColor, Mode=TwoWay}">
<bottomsheet:BottomSheet.Header>
<bottomsheet:BottomSheetHeader
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
using Android.Widget;
using Google.Android.Material.BottomSheet;
using AColorDrawable = Android.Graphics.Drawables.ColorDrawable;
using AColorStateList = Android.Content.Res.ColorStateList;
using AGravityFlags = Android.Views.GravityFlags;
using AView = Android.Views.View;
using AViewGroup = Android.Views.ViewGroup;
using AWindowManagerFlags = Android.Views.WindowManagerFlags;
using AColorStateList = Android.Content.Res.ColorStateList;
#pragma warning disable SA1200

namespace Plugin.Maui.BottomSheet.Platform.Android;
Expand Down Expand Up @@ -214,6 +214,7 @@ public void Open(IBottomSheet bottomSheet)
SetWindowBackgroundColor(bottomSheet.WindowBackgroundColor);
SetMaxHeight(bottomSheet.GetMaxHeight());
SetMaxWidth(bottomSheet.GetMaxWidth());
SetMargin(bottomSheet.GetMargin());

_bottomSheetDialog.Show();
}
Expand Down Expand Up @@ -317,7 +318,7 @@ public void SetMaxHeight(int height)
/// <summary>
/// Set max width.
/// </summary>
/// <param name="width">Widht value in dp.</param>
/// <param name="width">Width value in dp.</param>
public void SetMaxWidth(int width)
{
if (_bottomSheetBehavior is null
Expand All @@ -329,6 +330,27 @@ public void SetMaxWidth(int width)
_bottomSheetBehavior.MaxWidth = Convert.ToInt32(_context.ToPixels(width));
}

/// <summary>
/// Set margin.
/// </summary>
/// <param name="margin">Margin value in dp.</param>
public void SetMargin(Thickness margin)
{
if (_sheetContainer.Parent is not AView bottomSheetFrame
|| bottomSheetFrame.LayoutParameters is not AViewGroup.MarginLayoutParams marginLayoutParams)
{
return;
}

var pixelMargin = _context.ToPixels(margin);

marginLayoutParams.SetMargins(
Convert.ToInt32(pixelMargin.Left),
0,
Convert.ToInt32(pixelMargin.Right),
0);
}

/// <summary>
/// Set current state.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,6 @@ public void SetIsCancelable()
_bottomSheet.SetIsCancelable(_virtualView?.IsCancelable ?? false);
}

/// <summary>
/// Set max height.
/// </summary>
/// <param name="height">Heigt value in dp.</param>
public void SetMaxHeight(int height)
{
_bottomSheet.SetMaxHeight(height);
}

/// <summary>
/// Set max width.
/// </summary>
/// <param name="width">Widht value in dp.</param>
public void SetMaxWidth(int width)
{
_bottomSheet.SetMaxWidth(width);
}

/// <summary>
/// Set whether show handle.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
#if ANDROID
using ABottomSheet = Plugin.Maui.BottomSheet.Platform.Android.MauiBottomSheet;
#endif

namespace Plugin.Maui.BottomSheet.PlatformConfiguration.AndroidSpecific;

using Microsoft.Maui.Controls.PlatformConfiguration;
Expand Down Expand Up @@ -48,6 +44,16 @@ public static class BottomSheet
typeof(BottomSheet),
defaultValue: int.MinValue);

/// <summary>
/// Bindable property.
/// </summary>
public static readonly BindableProperty MarginProperty =
BindableProperty.Create(
"Margin",
typeof(Thickness),
typeof(BottomSheet),
defaultValue: Thickness.Zero);

/// <summary>
/// Get theme resource id.
/// </summary>
Expand Down Expand Up @@ -115,16 +121,6 @@ public static int GetMaxWidth(this IPlatformElementConfiguration<Android, Maui.B
/// </summary>
/// <param name="element"><see cref="Maui.BottomSheet"/> instance.</param>
/// <returns>Max width.</returns>
public static int GetMaxWidth(this IBottomSheet element)
{
if (element is not BindableObject bindable)
{
throw new ArgumentException("Element must be a BindableObject");
}

return GetMaxWidth(bindable);
}

public static int GetMaxWidth(BindableObject element)
{
return (int)element.GetValue(MaxWidthProperty);
Expand All @@ -133,31 +129,21 @@ public static int GetMaxWidth(BindableObject element)
/// <summary>
/// Get <see cref="Maui.BottomSheet"/> max height.
/// </summary>
/// <param name="config">Android configuration for <see cref="Maui.BottomSheet"/> instance.</param>
/// <param name="element"><see cref="Maui.BottomSheet"/> instance.</param>
/// <returns>Max height.</returns>
public static int GetMaxHeight(this IPlatformElementConfiguration<Android, Maui.BottomSheet.BottomSheet> config)
public static int GetMaxHeight(BindableObject element)
{
return GetMaxHeight(config.Element as BindableObject);
return (int)element.GetValue(MaxHeightProperty);
}

/// <summary>
/// Get <see cref="Maui.BottomSheet"/> max height.
/// </summary>
/// <param name="element"><see cref="Maui.BottomSheet"/> instance.</param>
/// <param name="config">Android configuration for <see cref="Maui.BottomSheet"/> instance.</param>
/// <returns>Max height.</returns>
public static int GetMaxHeight(this IBottomSheet element)
{
if (element is not BindableObject bindable)
{
throw new ArgumentException("Element must be a BindableObject");
}

return GetMaxHeight(bindable);
}

public static int GetMaxHeight(BindableObject element)
public static int GetMaxHeight(this IPlatformElementConfiguration<Android, Maui.BottomSheet.BottomSheet> config)
{
return (int)element.GetValue(MaxHeightProperty);
return GetMaxHeight(config.Element as BindableObject);
}

/// <summary>
Expand All @@ -168,36 +154,18 @@ public static int GetMaxHeight(BindableObject element)
/// <returns><see cref="IPlatformElementConfiguration{TPlatform, TElement}"/>.</returns>
public static IPlatformElementConfiguration<Android, Maui.BottomSheet.BottomSheet> SetMaxWidth(this IPlatformElementConfiguration<Android, Maui.BottomSheet.BottomSheet> config, int value)
{
SetMaxWidth(config.Element as BindableObject, value);
SetMaxWidth(config.Element, value);
return config;
}

/// <summary>
/// Set <see cref="Maui.BottomSheet"/> max width.
/// Set max width.
/// </summary>
/// <param name="element"><see cref="Maui.BottomSheet"/> instance.</param>
/// <param name="value">Max width.</param>
public static void SetMaxWidth(this IBottomSheet element, int value)
{
if (element is not BindableObject bindable)
{
throw new ArgumentException("Element must be a BindableObject");
}

SetMaxWidth(bindable, value);
}

public static void SetMaxWidth(BindableObject element, int value)
{
element.SetValue(MaxWidthProperty, value);

#if ANDROID
if (element is Maui.BottomSheet.BottomSheet bottomSheet
&& bottomSheet.Handler?.PlatformView is ABottomSheet mauiBottomSheet)
{
mauiBottomSheet.SetMaxWidth(value);
}
#endif
}

/// <summary>
Expand All @@ -208,36 +176,103 @@ public static void SetMaxWidth(BindableObject element, int value)
/// <returns><see cref="IPlatformElementConfiguration{TPlatform, TElement}"/>.</returns>
public static IPlatformElementConfiguration<Android, Maui.BottomSheet.BottomSheet> SetMaxHeight(this IPlatformElementConfiguration<Android, Maui.BottomSheet.BottomSheet> config, int value)
{
SetMaxHeight(config.Element as BindableObject, value);
SetMaxHeight(config.Element, value);
return config;
}

/// <summary>
/// Set <see cref="Maui.BottomSheet"/> max height.
/// Set max height.
/// </summary>
/// <param name="element"><see cref="Maui.BottomSheet"/> instance.</param>
/// <param name="value">Max height.</param>
public static void SetMaxHeight(this IBottomSheet element, int value)
public static void SetMaxHeight(BindableObject element, int value)
{
element.SetValue(MaxHeightProperty, value);
}

/// <summary>
/// Get margin.
/// </summary>
/// <param name="config">Android configuration for <see cref="Maui.BottomSheet"/> instance.</param>.
/// <returns><see cref="IPlatformElementConfiguration{TPlatform, TElement}"/>.</returns>
public static Thickness GetMargin(this IPlatformElementConfiguration<Android, Maui.BottomSheet.BottomSheet> config)
{
return GetMargin(config.Element as BindableObject);
}

/// <summary>
/// Get margin.
/// </summary>
/// <param name="element"><see cref="Maui.BottomSheet"/> instance.</param>
/// <returns>Margin of <see cref="Maui.BottomSheet"/>.</returns>
public static Thickness GetMargin(BindableObject element)
{
return (Thickness)element.GetValue(MarginProperty);
}

/// <summary>
/// Set margin.
/// </summary>
/// <param name="config">Android configuration for <see cref="Maui.BottomSheet"/> instance.</param>.
/// <param name="value"><see cref="Maui.BottomSheet"/> margin.</param>
public static void SetMargin(this IPlatformElementConfiguration<Android, Maui.BottomSheet.BottomSheet> config, Thickness value)
{
SetMargin(config.Element, value);
}

/// <summary>
/// Set margin.
/// </summary>
/// <param name="element"><see cref="Maui.BottomSheet"/> instance.</param>
/// <param name="value"><see cref="Maui.BottomSheet"/> margin.</param>
public static void SetMargin(BindableObject element, Thickness value)
{
element.SetValue(MarginProperty, value);
}

/// <summary>
/// Get <see cref="Maui.BottomSheet"/> max width.
/// </summary>
/// <param name="element"><see cref="Maui.BottomSheet"/> instance.</param>
/// <returns>Max width.</returns>
internal static int GetMaxWidth(this IBottomSheet element)
{
if (element is not BindableObject bindable)
{
throw new ArgumentException("Element must be a BindableObject");
}

SetMaxHeight(bindable, value);
return GetMaxWidth(bindable);
}

public static void SetMaxHeight(BindableObject element, int value)
/// <summary>
/// Get <see cref="Maui.BottomSheet"/> max height.
/// </summary>
/// <param name="element"><see cref="Maui.BottomSheet"/> instance.</param>
/// <returns>Max height.</returns>
internal static int GetMaxHeight(this IBottomSheet element)
{
element.SetValue(MaxHeightProperty, value);
if (element is not BindableObject bindable)
{
throw new ArgumentException("Element must be a BindableObject");
}

#if ANDROID
if (element is Maui.BottomSheet.BottomSheet bottomSheet
&& bottomSheet.Handler?.PlatformView is ABottomSheet mauiBottomSheet)
return GetMaxHeight(bindable);
}

/// <summary>
/// Get margin.
/// </summary>
/// <param name="element"><see cref="Maui.BottomSheet"/> instance.</param>
/// <returns>Margin of <see cref="Maui.BottomSheet"/>.</returns>
internal static Thickness GetMargin(this IBottomSheet element)
{
if (element is not BindableObject bindable)
{
mauiBottomSheet.SetMaxHeight(value);
throw new ArgumentException("Element must be a BindableObject");
}
#endif

return GetMargin(bindable);
}

private static void SetTheme(BindableObject element, int value)
Expand Down

0 comments on commit 31f9dca

Please sign in to comment.