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

[Android] Converting dimensions to pixels in popup renderer #856

Merged
merged 12 commits into from
Feb 10, 2021
13 changes: 2 additions & 11 deletions samples/XCT.Sample/Pages/Views/Popups/ButtonPopup.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,9 @@
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
xmlns:local="clr-namespace:Xamarin.CommunityToolkit.Sample.Pages.Views.Popups"
Size="{x:Static local:PopupSize.Medium}"
x:Class="Xamarin.CommunityToolkit.Sample.Pages.Views.Popups.ButtonPopup">

<xct:Popup.Size>
<OnPlatform x:TypeArguments="Size">
<OnPlatform.Platforms>
<On Platform="Android" Value="{x:Static local:PopupSize.Android}" />
<On Platform="UWP" Value="{x:Static local:PopupSize.UWP}" />
<On Platform="iOS" Value="{x:Static local:PopupSize.iOS}" />
</OnPlatform.Platforms>
</OnPlatform>
</xct:Popup.Size>

<xct:Popup.Resources>
<ResourceDictionary>
<Style x:Key="Title" TargetType="Label">
Expand Down Expand Up @@ -53,4 +44,4 @@
Clicked="Button_Clicked" />
</StackLayout>

</xct:Popup>
</xct:Popup>
13 changes: 2 additions & 11 deletions samples/XCT.Sample/Pages/Views/Popups/CsharpBindingPopup.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,9 @@
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
xmlns:local="clr-namespace:Xamarin.CommunityToolkit.Sample.Pages.Views.Popups"
Size="{x:Static local:PopupSize.Large}"
x:Class="Xamarin.CommunityToolkit.Sample.Pages.Views.Popups.CsharpBindingPopup">

<xct:Popup.Size>
<OnPlatform x:TypeArguments="Size">
<OnPlatform.Platforms>
<On Platform="Android" Value="{x:Static local:PopupSize.Android}" />
<On Platform="UWP" Value="{x:Static local:PopupSize.UWP}" />
<On Platform="iOS" Value="{x:Static local:PopupSize.iOS}" />
</OnPlatform.Platforms>
</OnPlatform>
</xct:Popup.Size>

<xct:Popup.Resources>
<ResourceDictionary>
<Style x:Key="Title" TargetType="Label">
Expand Down Expand Up @@ -47,4 +38,4 @@
Text="{Binding Message}" />
</StackLayout>

</xct:Popup>
</xct:Popup>
13 changes: 2 additions & 11 deletions samples/XCT.Sample/Pages/Views/Popups/MultipleButtonPopup.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,9 @@
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
xmlns:local="clr-namespace:Xamarin.CommunityToolkit.Sample.Pages.Views.Popups"
Size="{x:Static local:PopupSize.Medium}"
x:Class="Xamarin.CommunityToolkit.Sample.Pages.Views.Popups.MultipleButtonPopup">

<xct:Popup.Size>
<OnPlatform x:TypeArguments="Size">
<OnPlatform.Platforms>
<On Platform="Android" Value="{x:Static local:PopupSize.Android}" />
<On Platform="UWP" Value="{x:Static local:PopupSize.UWP}" />
<On Platform="iOS" Value="{x:Static local:PopupSize.iOS}" />
</OnPlatform.Platforms>
</OnPlatform>
</xct:Popup.Size>

<xct:Popup.Resources>
<ResourceDictionary>
<Style x:Key="Title" TargetType="Label">
Expand Down Expand Up @@ -67,4 +58,4 @@

</StackLayout>

</xct:Popup>
</xct:Popup>
13 changes: 2 additions & 11 deletions samples/XCT.Sample/Pages/Views/Popups/NoLightDismissPopup.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,10 @@
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
xmlns:local="clr-namespace:Xamarin.CommunityToolkit.Sample.Pages.Views.Popups"
Size="{x:Static local:PopupSize.Medium}"
IsLightDismissEnabled="False"
x:Class="Xamarin.CommunityToolkit.Sample.Pages.Views.Popups.NoLightDismissPopup">

<xct:Popup.Size>
<OnPlatform x:TypeArguments="Size">
<OnPlatform.Platforms>
<On Platform="Android" Value="{x:Static local:PopupSize.Android}" />
<On Platform="UWP" Value="{x:Static local:PopupSize.UWP}" />
<On Platform="iOS" Value="{x:Static local:PopupSize.iOS}" />
</OnPlatform.Platforms>
</OnPlatform>
</xct:Popup.Size>

<xct:Popup.Resources>
<ResourceDictionary>
<Style x:Key="Title" TargetType="Label">
Expand Down Expand Up @@ -57,4 +48,4 @@
Clicked="Button_Clicked" />
</StackLayout>

</xct:Popup>
</xct:Popup>
13 changes: 2 additions & 11 deletions samples/XCT.Sample/Pages/Views/Popups/OpenedEventSimplePopup.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,9 @@
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
xmlns:local="clr-namespace:Xamarin.CommunityToolkit.Sample.Pages.Views.Popups"
Size="{x:Static local:PopupSize.Medium}"
x:Class="Xamarin.CommunityToolkit.Sample.Pages.Views.Popups.OpenedEventSimplePopup">

<xct:Popup.Size>
<OnPlatform x:TypeArguments="Size">
<OnPlatform.Platforms>
<On Platform="Android" Value="{x:Static local:PopupSize.Android}" />
<On Platform="UWP" Value="{x:Static local:PopupSize.UWP}" />
<On Platform="iOS" Value="{x:Static local:PopupSize.iOS}" />
</OnPlatform.Platforms>
</OnPlatform>
</xct:Popup.Size>

<xct:Popup.Resources>
<ResourceDictionary>
<Style x:Key="Title" TargetType="Label">
Expand Down Expand Up @@ -47,4 +38,4 @@
Style="{StaticResource Content}" />
</StackLayout>

</xct:Popup>
</xct:Popup>
15 changes: 10 additions & 5 deletions samples/XCT.Sample/Pages/Views/Popups/PopupSize.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
using Xamarin.Forms;
using Xamarin.Essentials;
using Xamarin.Forms;

namespace Xamarin.CommunityToolkit.Sample.Pages.Views.Popups
{
static class PopupSize
{
public static Size Android => new Size(1000, 1200);
// examples for fixed sizes
public static Size Tiny => new Size(100, 100);

public static Size iOS => new Size(250, 350);

public static Size UWP => new Size(300, 400);
public static Size Small => new Size(300, 300);

// examples for relative to screen sizes
public static Size Medium => new Size(0.7 * (DeviceDisplay.MainDisplayInfo.Width / DeviceDisplay.MainDisplayInfo.Density), 0.6 * (DeviceDisplay.MainDisplayInfo.Height / DeviceDisplay.MainDisplayInfo.Density));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this, great idea!


public static Size Large => new Size(0.9 * (DeviceDisplay.MainDisplayInfo.Width / DeviceDisplay.MainDisplayInfo.Density), 0.8 * (DeviceDisplay.MainDisplayInfo.Height / DeviceDisplay.MainDisplayInfo.Density));
}
}
13 changes: 2 additions & 11 deletions samples/XCT.Sample/Pages/Views/Popups/ReturnResultPopup.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,10 @@
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
xmlns:local="clr-namespace:Xamarin.CommunityToolkit.Sample.Pages.Views.Popups"
Size="{x:Static local:PopupSize.Medium}"
x:Class="Xamarin.CommunityToolkit.Sample.Pages.Views.Popups.ReturnResultPopup"
x:TypeArguments="x:String">

<xct:Popup.Size>
<OnPlatform x:TypeArguments="Size">
<OnPlatform.Platforms>
<On Platform="Android" Value="{x:Static local:PopupSize.Android}" />
<On Platform="UWP" Value="{x:Static local:PopupSize.UWP}" />
<On Platform="iOS" Value="{x:Static local:PopupSize.iOS}" />
</OnPlatform.Platforms>
</OnPlatform>
</xct:Popup.Size>

<xct:Popup.Resources>
<ResourceDictionary>
<Style x:Key="Title" TargetType="Label">
Expand Down Expand Up @@ -54,4 +45,4 @@
Clicked="Button_Clicked" />
</StackLayout>

</xct:Popup>
</xct:Popup>
13 changes: 2 additions & 11 deletions samples/XCT.Sample/Pages/Views/Popups/SimplePopup.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,9 @@
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
xmlns:local="clr-namespace:Xamarin.CommunityToolkit.Sample.Pages.Views.Popups"
Size="{x:Static local:PopupSize.Small}"
x:Class="Xamarin.CommunityToolkit.Sample.Pages.Views.Popups.SimplePopup">

<xct:Popup.Size>
<OnPlatform x:TypeArguments="Size">
<OnPlatform.Platforms>
<On Platform="Android" Value="{x:Static local:PopupSize.Android}" />
<On Platform="UWP" Value="{x:Static local:PopupSize.UWP}" />
<On Platform="iOS" Value="{x:Static local:PopupSize.iOS}" />
</OnPlatform.Platforms>
</OnPlatform>
</xct:Popup.Size>

<xct:Popup.Resources>
<ResourceDictionary>
<Style x:Key="Title" TargetType="Label">
Expand Down Expand Up @@ -47,4 +38,4 @@
Text="This is a native popup with a Xamarin.Forms View being rendered. The behaviors of the popup will confirm to 100% native look and feel, but still allows you to use your Xamarin.Forms controls." />
</StackLayout>

</xct:Popup>
</xct:Popup>
13 changes: 2 additions & 11 deletions samples/XCT.Sample/Pages/Views/Popups/ToggleSizePopup.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,9 @@
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
xmlns:local="clr-namespace:Xamarin.CommunityToolkit.Sample.Pages.Views.Popups"
Size="{x:Static local:PopupSize.Medium}"
x:Class="Xamarin.CommunityToolkit.Sample.Pages.Views.Popups.ToggleSizePopup">

<xct:Popup.Size>
<OnPlatform x:TypeArguments="Size">
<OnPlatform.Platforms>
<On Platform="Android" Value="{x:Static local:PopupSize.Android}" />
<On Platform="UWP" Value="{x:Static local:PopupSize.UWP}" />
<On Platform="iOS" Value="{x:Static local:PopupSize.iOS}" />
</OnPlatform.Platforms>
</OnPlatform>
</xct:Popup.Size>

<xct:Popup.Resources>
<ResourceDictionary>
<Style x:Key="Title" TargetType="Label">
Expand Down Expand Up @@ -53,4 +44,4 @@
Clicked="Button_Clicked" />
</StackLayout>

</xct:Popup>
</xct:Popup>
5 changes: 3 additions & 2 deletions samples/XCT.Sample/Pages/Views/Popups/TransparentPopup.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
<xct:Popup xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
xmlns:local="clr-namespace:Xamarin.CommunityToolkit.Sample.Pages.Views.Popups"
xmlns:uwpxct="clr-namespace:Xamarin.CommunityToolkit.UI.Views.WindowsSpecific;assembly=Xamarin.CommunityToolkit"
Size="{OnPlatform Android='200, 200', UWP='100, 100', iOS='100, 100'}"
Size="{x:Static local:PopupSize.Tiny}"
Color="Transparent"
uwpxct:Popup.BorderColor="Transparent"
x:Class="Xamarin.CommunityToolkit.Sample.Pages.Views.Popups.TransparentPopup">
Expand All @@ -13,4 +14,4 @@
HeightRequest="{OnPlatform Android='100', UWP='50'}" WidthRequest="{OnPlatform Android='100', UWP='50'}"
BackgroundColor="Red" />

</xct:Popup>
</xct:Popup>
13 changes: 2 additions & 11 deletions samples/XCT.Sample/Pages/Views/Popups/XamlBindingPopup.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,9 @@
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
xmlns:vm="clr-namespace:Xamarin.CommunityToolkit.Sample.ViewModels.Views.Popups"
xmlns:local="clr-namespace:Xamarin.CommunityToolkit.Sample.Pages.Views.Popups"
Size="{x:Static local:PopupSize.Large}"
x:Class="Xamarin.CommunityToolkit.Sample.Pages.Views.Popups.XamlBindingPopup">

<xct:Popup.Size>
<OnPlatform x:TypeArguments="Size">
<OnPlatform.Platforms>
<On Platform="Android" Value="{x:Static local:PopupSize.Android}" />
<On Platform="UWP" Value="{x:Static local:PopupSize.UWP}" />
<On Platform="iOS" Value="{x:Static local:PopupSize.iOS}" />
</OnPlatform.Platforms>
</OnPlatform>
</xct:Popup.Size>

<xct:Popup.BindingContext>
<vm:XamlBindingPopupViewModel />
</xct:Popup.BindingContext>
Expand Down Expand Up @@ -52,4 +43,4 @@
Text="{Binding Message}" />
</StackLayout>

</xct:Popup>
</xct:Popup>
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,15 @@ void SetSize()
var decorView = (ViewGroup)Window.DecorView;
var child = (FrameLayout)decorView.GetChildAt(0);

var realWidth = (int)Context.ToPixels(Element.Size.Width);
var realHeight = (int)Context.ToPixels(Element.Size.Height);

var realContentWidth = (int)Context.ToPixels(Element.Content.WidthRequest);
var realContentHeight = (int)Context.ToPixels(Element.Content.HeightRequest);

var childLayoutParams = (FrameLayout.LayoutParams)child.LayoutParameters;
childLayoutParams.Width = (int)Element.Size.Width;
childLayoutParams.Height = (int)Element.Size.Height;
childLayoutParams.Width = realWidth;
childLayoutParams.Height = realHeight;
child.LayoutParameters = childLayoutParams;

var horizontalParams = -1;
Expand Down Expand Up @@ -165,30 +171,30 @@ void SetSize()
break;
}

if (Element.Content.WidthRequest > -1)
if (realContentWidth > -1)
{
var inputMeasuredWidth = Element.Content.WidthRequest > Element.Size.Width ?
(int)Element.Size.Width : (int)Element.Content.WidthRequest;
var inputMeasuredWidth = realContentWidth > realWidth ?
realWidth : realContentWidth;
container.Measure(inputMeasuredWidth, (int)MeasureSpecMode.Unspecified);
horizontalParams = container.MeasuredWidth;
}
else
{
container.Measure((int)Element.Size.Width, (int)MeasureSpecMode.Unspecified);
horizontalParams = container.MeasuredWidth > Element.Size.Width ?
(int)Element.Size.Width : container.MeasuredWidth;
container.Measure(realWidth, (int)MeasureSpecMode.Unspecified);
horizontalParams = container.MeasuredWidth > realWidth ?
realWidth : container.MeasuredWidth;
}

if (Element.Content.HeightRequest > -1)
if (realContentHeight > -1)
{
verticalParams = (int)Element.Content.HeightRequest;
verticalParams = realContentHeight;
}
else
{
var inputMeasuredWidth = Element.Content.WidthRequest > -1 ? horizontalParams : (int)Element.Size.Width;
var inputMeasuredWidth = realContentWidth > -1 ? horizontalParams : realWidth;
container.Measure(inputMeasuredWidth, (int)MeasureSpecMode.Unspecified);
verticalParams = container.MeasuredHeight > Element.Size.Height ?
(int)Element.Size.Height : container.MeasuredHeight;
verticalParams = container.MeasuredHeight > realHeight ?
realHeight : container.MeasuredHeight;
}

var containerLayoutParams = new FrameLayout.LayoutParams(horizontalParams, verticalParams);
Expand All @@ -201,7 +207,7 @@ void SetSize()
case LayoutAlignment.Center:
case LayoutAlignment.Fill:
containerLayoutParams.Gravity = GravityFlags.FillVertical;
containerLayoutParams.Height = (int)Element.Size.Height;
containerLayoutParams.Height = realHeight;
container.MatchHeight = true;
break;
case LayoutAlignment.End:
Expand All @@ -217,7 +223,7 @@ void SetSize()
case LayoutAlignment.Center:
case LayoutAlignment.Fill:
containerLayoutParams.Gravity |= GravityFlags.FillHorizontal;
containerLayoutParams.Width = (int)Element.Size.Width;
containerLayoutParams.Width = realWidth;
container.MatchWidth = true;
break;
case LayoutAlignment.End:
Expand Down