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

Platform specific API #949

Merged
merged 9 commits into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions samples/XCT.Sample/Pages/Views/Popups/TransparentPopup.xaml
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
<?xml version="1.0" encoding="utf-8" ?>
<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:iosxct="clr-namespace:Xamarin.CommunityToolkit.UI.Views.iOSSpecific;assembly=Xamarin.CommunityToolkit"
xmlns:uwpxct="clr-namespace:Xamarin.CommunityToolkit.UI.Views.WindowsSpecific;assembly=Xamarin.CommunityToolkit"
Size="{x:Static local:PopupSize.Tiny}"
Color="Transparent"
iosxct:Popup.ArrowDirection="Up"
uwpxct:Popup.BorderColor="Transparent"
x:Class="Xamarin.CommunityToolkit.Sample.Pages.Views.Popups.TransparentPopup">
<xct:Popup
x:Class="Xamarin.CommunityToolkit.Sample.Pages.Views.Popups.TransparentPopup"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Xamarin.CommunityToolkit.Sample.Pages.Views.Popups"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
Size="{x:Static local:PopupSize.Tiny}"
Copy link
Member

Choose a reason for hiding this comment

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

Totally unrelated, I think we can create an extension for this, right? I remember us doing it for something else, that will make this syntax better

Copy link
Contributor

Choose a reason for hiding this comment

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

What kind of markup extension are you thinking? The PopupSize is a helper for the sample project to make it easier to specify size.

Are you thinking about making this something that consumers of XCT could use for their Popups? That may be a really useful API.

Color="Transparent">

<Frame
BackgroundColor="Red"
CornerRadius="{OnPlatform Android='50',
UWP='50',
iOS='25'}"
HeightRequest="{OnPlatform Android='100',
UWP='50'}"
WidthRequest="{OnPlatform Android='100',
UWP='50'}" />

<Frame
CornerRadius="{OnPlatform Android='50', UWP='50', iOS='25'}"
HeightRequest="{OnPlatform Android='100', UWP='50'}" WidthRequest="{OnPlatform Android='100', UWP='50'}"
BackgroundColor="Red" />

</xct:Popup>
13 changes: 12 additions & 1 deletion samples/XCT.Sample/Pages/Views/Popups/TransparentPopup.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
namespace Xamarin.CommunityToolkit.Sample.Pages.Views.Popups
using Xamarin.Forms.PlatformConfiguration;
using Xamarin.CommunityToolkit.PlatformConfiguration.iOSSpecific;
using Xamarin.CommunityToolkit.PlatformConfiguration.WindowsSpecific;

namespace Xamarin.CommunityToolkit.Sample.Pages.Views.Popups
{
public partial class TransparentPopup
{
public TransparentPopup() => InitializeComponent();

protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();
On<iOS>().UseArrowDirection(PopoverArrowDirection.Right);
On<Windows>().SetBorderColor(Xamarin.Forms.Color.Red);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Xamarin.CommunityToolkit.UI.Views;
using Xamarin.Forms;
using XFPC = Xamarin.Forms.PlatformConfiguration;
using XCTElement = Xamarin.CommunityToolkit.UI.Views.BasePopup;

namespace Xamarin.CommunityToolkit.PlatformConfiguration.WindowsSpecific
{
public static class PopUp
{
public static readonly BindableProperty BorderColorProperty = BindableProperty.Create(
"BorderColor", typeof(Color), typeof(BasePopup), default(Color));

public static void SetBorderColor(BindableObject element, Color color) =>
element.SetValue(BorderColorProperty, color);

public static Color GetBorderColor(BindableObject element) =>
(Color)element.GetValue(BorderColorProperty);

public static IPlatformElementConfiguration<XFPC.Windows, XCTElement> SetBorderColor(this IPlatformElementConfiguration<XFPC.Windows, XCTElement> config, Color value)
{
SetBorderColor(config.Element, value);
return config;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Xamarin.CommunityToolkit.UI.Views;
using Xamarin.Forms;
using XFPC = Xamarin.Forms.PlatformConfiguration;
using XCTElement = Xamarin.CommunityToolkit.UI.Views.BasePopup;

namespace Xamarin.CommunityToolkit.PlatformConfiguration.iOSSpecific
{
public static class PopUp
{
public static readonly BindableProperty ArrowDirectionProperty = BindableProperty.Create(
"ArrowDirection", typeof(PopoverArrowDirection), typeof(BasePopup), PopoverArrowDirection.None);

public static void SetArrowDirection(BindableObject element, PopoverArrowDirection color) =>
element.SetValue(ArrowDirectionProperty, color);

public static PopoverArrowDirection GetArrowDirection(BindableObject element) =>
(PopoverArrowDirection)element.GetValue(ArrowDirectionProperty);

public static IPlatformElementConfiguration<XFPC.iOS, XCTElement> UseArrowDirection(this IPlatformElementConfiguration<XFPC.iOS, XCTElement> config, PopoverArrowDirection value)
Copy link
Contributor Author

@pictos pictos Feb 20, 2021

Choose a reason for hiding this comment

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

need to use the XFPC.iOS because the compiler complains that iOS is a namespace, during the build.

{
SetArrowDirection(config.Element, value);
return config;
}
}

public enum PopoverArrowDirection
{
None,
Up,
Down,
Left,
Right,
Any,
Unknown
Copy link
Member

Choose a reason for hiding this comment

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

What's the use of Unknown? @ahoefling?

Copy link
Contributor

Choose a reason for hiding this comment

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

This was added in #854 by @geskill and I approved the changes.

I am not 100% sure what Unknown or Any is used for. Looking at the code this maps 1:1 with the iOS implementation. Since this is iOS specific I think it is okay to leave both of them in. If we get to a point where we want to make this a shared API instead of attached property we would need to determine if it is still applicable.

Copy link
Contributor

Choose a reason for hiding this comment

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

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Xamarin.CommunityToolkit.UI.Views
/// The popup control's base implementation.
/// </summary>
[ContentProperty(nameof(Content))]
public abstract class BasePopup : VisualElement
public abstract class BasePopup : VisualElement, IElementConfiguration<BasePopup>
{
readonly WeakEventManager<PopupDismissedEventArgs> dismissWeakEventManager = new WeakEventManager<PopupDismissedEventArgs>();
readonly WeakEventManager<PopupOpenedEventArgs> openedWeakEventManager = new WeakEventManager<PopupOpenedEventArgs>();
Expand All @@ -21,8 +21,14 @@ protected BasePopup()
VerticalOptions = LayoutOptions.CenterAndExpand;
HorizontalOptions = LayoutOptions.CenterAndExpand;
IsLightDismissEnabled = true;
platformConfigurationRegistry = new Lazy<PlatformConfigurationRegistry<BasePopup>>(() => new PlatformConfigurationRegistry<BasePopup>(this));
}

readonly Lazy<PlatformConfigurationRegistry<BasePopup>> platformConfigurationRegistry;

public IPlatformElementConfiguration<T, BasePopup> On<T>() where T : IConfigPlatform =>
platformConfigurationRegistry.Value.On<T>();

public static readonly BindableProperty ContentProperty = BindableProperty.Create(nameof(Content), typeof(View), typeof(BasePopup), propertyChanged: OnContentChanged);

public static readonly BindableProperty ColorProperty = BindableProperty.Create(nameof(Color), typeof(Color), typeof(BasePopup), Color.Default);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Xamarin.Forms.Platform.UWP;
using UWPThickness = Windows.UI.Xaml.Thickness;
using XamlStyle = Windows.UI.Xaml.Style;
using Specific = Xamarin.CommunityToolkit.PlatformConfiguration.WindowsSpecific.PopUp;

[assembly: ExportRenderer(typeof(BasePopup), typeof(PopupRenderer))]

Expand Down Expand Up @@ -149,7 +150,13 @@ void SetBorderColor()
flyoutStyle.Setters.Add(new Windows.UI.Xaml.Setter(FlyoutPresenter.PaddingProperty, 0));
flyoutStyle.Setters.Add(new Windows.UI.Xaml.Setter(FlyoutPresenter.BorderThicknessProperty, new UWPThickness(defaultBorderThickness)));

var borderColor = WindowsSpecific.Popup.GetBorderColor(Element);
if (Element == null)
{
Log.Warning("warning", "The PopUpView is null.");
return;
}

var borderColor = Specific.GetBorderColor(Element);
if (borderColor == default(Color))
flyoutStyle.Setters.Add(new Windows.UI.Xaml.Setter(FlyoutPresenter.BorderBrushProperty, Color.FromHex("#2e6da0").ToWindowsColor()));
else
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using Xamarin.Forms;
using Xamarin.Forms.Internals;
using Xamarin.Forms.Platform.iOS;
using Xamarin.CommunityToolkit.PlatformConfiguration.iOSSpecific;
using Specifics = Xamarin.CommunityToolkit.PlatformConfiguration.iOSSpecific.PopUp;

[assembly: ExportRenderer(typeof(BasePopup), typeof(PopupRenderer))]

Expand Down Expand Up @@ -179,15 +181,15 @@ void SetLayout()
var view = Platform.GetRenderer(Element.Anchor).NativeView;
PopoverPresentationController.SourceView = view;
PopoverPresentationController.SourceRect = view.Bounds;
var arrowDirection = Views.iOSSpecific.Popup.GetArrowDirection(Element);
var arrowDirection = Specifics.GetArrowDirection(Element);
PopoverPresentationController.PermittedArrowDirections = arrowDirection switch
{
Views.iOSSpecific.PopoverArrowDirection.Up => UIPopoverArrowDirection.Up,
Views.iOSSpecific.PopoverArrowDirection.Down => UIPopoverArrowDirection.Down,
Views.iOSSpecific.PopoverArrowDirection.Left => UIPopoverArrowDirection.Left,
Views.iOSSpecific.PopoverArrowDirection.Right => UIPopoverArrowDirection.Right,
Views.iOSSpecific.PopoverArrowDirection.Any => UIPopoverArrowDirection.Any,
Views.iOSSpecific.PopoverArrowDirection.Unknown => UIPopoverArrowDirection.Unknown,
PopoverArrowDirection.Up => UIPopoverArrowDirection.Up,
PopoverArrowDirection.Down => UIPopoverArrowDirection.Down,
PopoverArrowDirection.Left => UIPopoverArrowDirection.Left,
PopoverArrowDirection.Right => UIPopoverArrowDirection.Right,
PopoverArrowDirection.Any => UIPopoverArrowDirection.Any,
PopoverArrowDirection.Unknown => UIPopoverArrowDirection.Unknown,
_ => 0
};
}
Expand Down