Skip to content

Commit

Permalink
Set alignment for buttons in SheetView (#250)
Browse files Browse the repository at this point in the history
* Set alignment for buttons in SheetView

* added custom renderers for fixing text alignment  in sheet view button.

* Added xml comments

* added xml comments

* added xml comments

* Added initalize method
  • Loading branch information
Iyani-NK authored Nov 10, 2020
1 parent 9cb3a5e commit 27de7af
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 27 deletions.
1 change: 1 addition & 0 deletions src/DIPS.Xamarin.UI.Android/DIPS.Xamarin.UI.Android.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<Compile Include="Library.cs" />
<Compile Include="Resources\Resource.designer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="InternalButtonRenderer.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\AboutResources.txt" />
Expand Down
121 changes: 121 additions & 0 deletions src/DIPS.Xamarin.UI.Android/InternalButtonRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
using System.ComponentModel;
using Android.Content;
using Android.Views;
using DIPS.Xamarin.UI.Android;
using DIPS.Xamarin.UI.Internal;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using TextAlignment = Xamarin.Forms.TextAlignment;

[assembly: ExportRenderer(typeof(InternalButton), typeof(InternalButtonRenderer))]

namespace DIPS.Xamarin.UI.Android
{
/// <summary>
/// An internal class to use when having to interact with the buttons on Android platform
/// </summary>
internal class InternalButtonRenderer : ButtonRenderer
{
/// <summary>
/// Setting initialization
/// </summary>
internal static void Initialize() { }

/// <summary>
///
/// </summary>
/// <param name="context"></param>
public InternalButtonRenderer(Context context) : base(context)
{
}

/// <summary>
/// Gets element from base class
/// </summary>
public new InternalButton Element => (InternalButton)base.Element;

/// <summary>
/// Called when [element changed].
/// </summary>
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);

if (e.NewElement == null)
{
return;
}

SetHorizonalTextAlignment();
SetVerticalTextAlignment();
}

/// <summary>
/// Handles the <see cref="E:ElementPropertyChanged" /> event.
/// </summary>
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);

if (e.PropertyName == InternalButton.HorizontalTextAlignmentProperty.PropertyName)
{
SetHorizonalTextAlignment();
}

if (e.PropertyName == InternalButton.VerticalTextAlignmentProperty.PropertyName)
{
SetVerticalTextAlignment();
}
}

/// <summary>
/// To set the horizontal text alignment.
/// </summary>
private void SetHorizonalTextAlignment()
{
Control.Gravity = Element.HorizontalTextAlignment.ToHorizontalGravityFlags() |
Element.VerticalTextAlignment.ToVerticalGravityFlags();
}

/// <summary>
/// To set the vertical text alignment.
/// </summary>
private void SetVerticalTextAlignment()
{
Control.Gravity = Element.VerticalTextAlignment.ToVerticalGravityFlags() |
Element.HorizontalTextAlignment.ToHorizontalGravityFlags();
}
}

/// <summary>
/// An internal class to set the flags for horizontal, vertictal text alignment.
/// </summary>
internal static class AlignmentHelper
{
/// <summary>
/// To set the flags for horizontal text alignment.
/// </summary>
public static GravityFlags ToHorizontalGravityFlags(this TextAlignment alignment)
{
if (alignment == TextAlignment.Center)
{
return GravityFlags.CenterHorizontal;
}

return alignment == TextAlignment.End ? GravityFlags.Right : GravityFlags.Left;
}

/// <summary>
/// To set the flags for vertictal text alignment.
/// </summary>
public static GravityFlags ToVerticalGravityFlags(this TextAlignment alignment)
{
if (alignment == TextAlignment.Center)
{
return GravityFlags.CenterVertical;
}

return alignment == TextAlignment.End ? GravityFlags.Top : GravityFlags.Bottom;
}
}
}
1 change: 1 addition & 0 deletions src/DIPS.Xamarin.UI.Android/Library.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public static void Initialize()
{
Inspector.Instance = new Util.Inspector();
InternalDatePickerRenderer.Initialize();
InternalButtonRenderer.Initialize();
}
}
}
46 changes: 46 additions & 0 deletions src/DIPS.Xamarin.UI/Internal/InternalButton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Xamarin.Forms;

namespace DIPS.Xamarin.UI.Internal
{
/// <summary>
/// An internal class to use when having to interact with the buttons
/// </summary>
internal class InternalButton : Button
{
/// <summary>
/// Bindable property for horizontal text alignment of a button.
/// </summary>
public static BindableProperty HorizontalTextAlignmentProperty =
BindableProperty.Create(nameof(HorizontalTextAlignment),
typeof(TextAlignment),
typeof(InternalButton),
TextAlignment.Center);

/// <summary>
/// Bindable property for vertical text alignment of a button.
/// </summary>
public static BindableProperty VerticalTextAlignmentProperty =
BindableProperty.Create(nameof(VerticalTextAlignment),
typeof(TextAlignment),
typeof(InternalButton),
TextAlignment.Center);

/// <summary>
/// Gets or sets the horizontal text alignment.
/// </summary>
public TextAlignment HorizontalTextAlignment
{
get => (TextAlignment)GetValue(HorizontalTextAlignmentProperty);
set => SetValue(HorizontalTextAlignmentProperty, value);
}

/// <summary>
/// Gets or sets the vertical text alignment.
/// </summary>
public TextAlignment VerticalTextAlignment
{
get => (TextAlignment)GetValue(VerticalTextAlignmentProperty);
set => SetValue(VerticalTextAlignmentProperty, value);
}
}
}
58 changes: 31 additions & 27 deletions src/DIPS.Xamarin.UI/Internal/Xaml/SheetView.xaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
<?xml version="1.0"
encoding="UTF-8"?>

<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
xmlns:Sheet="clr-namespace:DIPS.Xamarin.UI.Controls.Sheet;assembly=DIPS.Xamarin.UI"
xmlns:local="clr-namespace:DIPS.Xamarin.UI.Internal"
mc:Ignorable="d dblend"
x:Class="DIPS.Xamarin.UI.Internal.xaml.SheetView"
x:DataType="{x:Type Sheet:SheetBehavior}"
xmlns:dblend="http://schemas.microsoft.com/expression/blend/2008"
xmlns:ValueConverters="clr-namespace:DIPS.Xamarin.UI.Converters.ValueConverters"
xmlns:MultiValueConverters="clr-namespace:DIPS.Xamarin.UI.Converters.MultiValueConverters"
xmlns:Utilities="clr-namespace:DIPS.Xamarin.UI.Internal.Utilities;assembly=DIPS.Xamarin.UI"
dblend:DataContext="{dblend:DesignInstance Sheet:SheetBehavior}">
<Frame x:Name="OuterSheetFrame"
HasShadow="{Binding HasShadow}"
Expand Down Expand Up @@ -57,7 +58,8 @@
ColumnSpacing="0"
IsClippedToBounds="True">
<Grid.IsVisible>
<MultiBinding Converter="{MultiValueConverters:LogicalExpressionConverter
<MultiBinding
Converter="{MultiValueConverters:LogicalExpressionConverter
LogicalGate=Or}">
<Binding Source="{x:Reference TitleLabel}"
Path="IsVisible" />
Expand All @@ -84,30 +86,32 @@
IsVisible="{Binding Title, Converter={ValueConverters:IsEmptyConverter Inverted=True}}"
LineBreakMode="TailTruncation"
VerticalOptions="Center" />

<Button x:Name="cancelButton"
Grid.Column="0"
FontSize="{Binding CancelButtonSize}"
Padding="10"
IsVisible="{Binding IsCancelButtonVisible}"
TextColor="{Binding CancelTitleColor}"
CommandParameter="{Binding CancelCommandParameter}"
Command="{Binding CancelCommand}"
Text="{Binding CancelTitle}"
Clicked="CancelButtonClicked"
BackgroundColor="{Binding HeaderColor}" />

<Button x:Name="actionButton"
Grid.Column="2"
FontSize="{Binding ActionButtonSize}"
Padding="10"
BackgroundColor="{Binding HeaderColor}"
Command="{Binding ActionCommand}"
CommandParameter="{Binding ActionCommandParameter}"
TextColor="{Binding ActionTitleColor}"
Text="{Binding ActionTitle}"
Clicked="ActionButtonClicked"
IsVisible="{Binding ActionTitle, Converter={ValueConverters:IsEmptyConverter Inverted=True}}" />
<local:InternalButton x:Name="cancelButton"
Grid.Column="0"
Padding="15,10,10,10"
FontSize="{Binding CancelButtonSize}"
IsVisible="{Binding IsCancelButtonVisible}"
TextColor="{Binding CancelTitleColor}"
CommandParameter="{Binding CancelCommandParameter}"
Command="{Binding CancelCommand}"
Text="{Binding CancelTitle}"
Clicked="CancelButtonClicked"
BackgroundColor="{Binding HeaderColor}"
HorizontalTextAlignment="Start"
VerticalTextAlignment="Center" />
<local:InternalButton x:Name="actionButton"
Grid.Column="2"
Padding="10,10,15,10"
FontSize="{Binding ActionButtonSize}"
BackgroundColor="{Binding HeaderColor}"
Command="{Binding ActionCommand}"
CommandParameter="{Binding ActionCommandParameter}"
TextColor="{Binding ActionTitleColor}"
Text="{Binding ActionTitle}"
Clicked="ActionButtonClicked"
HorizontalTextAlignment="End"
VerticalTextAlignment="Center"
IsVisible="{Binding ActionTitle, Converter={ValueConverters:IsEmptyConverter Inverted=True}}" />
</Grid>

<BoxView x:Name="SeparatorBoxView"
Expand Down Expand Up @@ -142,7 +146,7 @@
<RowDefinition Height="*" />
</Grid.RowDefinitions>

<ContentView x:Name="sheetContentView" Content="{Binding SheetContent}"/>
<ContentView x:Name="sheetContentView" Content="{Binding SheetContent}" />
</Grid>
</Grid>
</Frame>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,6 @@
<Folder Include="Github203\" />
<Folder Include="Github211\" />
<Folder Include="Github234\" />
<Folder Include="Github248\" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0"
encoding="utf-8"?>

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="DIPS.Xamarin.Forms.IssuesRepro.Github248.Github248"
xmlns:dxui="http://dips.xamarin.ui.com"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<dxui:ModalityLayout>
<dxui:ModalityLayout.Behaviors>
<dxui:SheetBehavior x:Name="sheetBehavior"
IsOpen="True"
IsCancelButtonVisible="True"
CancelTitle="Cancel"
ActionTitle="Save this"
Title="Title"
TitleColor="{dxui:DIPSColor ColorPalette=Dark}"
TitleSize="16"
HeaderColor="{x:Static dxui:ColorPalette.LightLight}"
MinPosition=".5"
MaxPosition="0.98"
IsDraggable="True"
VerticalContentAlignment="Fit">
<dxui:SheetBehavior.SheetContentTemplate>
<DataTemplate>
<StackLayout Spacing="0"
Orientation="Vertical">
<Editor x:Name="editor"
TextColor="{x:Static dxui:ColorPalette.Dark}"
Text="Sample text sample text sample text sample text sample iiiii text sample text sample text sample text"
Margin="10,0"
FontSize="Small"
BackgroundColor="{x:Static dxui:ColorPalette.LightAir}" />
</StackLayout>
</DataTemplate>
</dxui:SheetBehavior.SheetContentTemplate>
</dxui:SheetBehavior>
</dxui:ModalityLayout.Behaviors>
</dxui:ModalityLayout>
</ContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace DIPS.Xamarin.Forms.IssuesRepro.Github248
{
[Issue(248)]
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Github248 : ContentPage
{
public Github248()
{
InitializeComponent();
}
}
}

0 comments on commit 27de7af

Please sign in to comment.