Skip to content

Commit

Permalink
Merge pull request #62 from ChasakisD/feature/border-suppression
Browse files Browse the repository at this point in the history
Suppress bottom border on picker
  • Loading branch information
ChasakisD authored Feb 19, 2020
2 parents b3af27c + f6c359b commit 1be56e8
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 39 deletions.
2 changes: 1 addition & 1 deletion nuget/XamarinBackgroundKit.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package>
<metadata>
<id>Xamarin.Forms.BackgroundKit</id>
<version>2.0.3</version>
<version>2.0.4</version>
<title>Xamarin.Forms.BackgroundKit</title>
<authors>ChasakisD</authors>
<owners>ChasakisD</owners>
Expand Down
81 changes: 59 additions & 22 deletions src/XamarinBackgroundKit.Android/Effects/BackgroundEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,10 @@ public class BackgroundEffectDroid : BasePlatformEffect<BackgroundEffect, Elemen
protected override void OnAttached()
{
base.OnAttached();

SetTracker();

HandleBackgroundManager();

_background = BackgroundEffect.GetBackground(Element);

_background?.SetBinding(BindableObject.BindingContextProperty,
new Binding("BindingContext", source: Element));

UpdateBackgroundIfNotRemoved();
}

protected override void OnDetached()
Expand All @@ -40,15 +36,43 @@ protected override void OnElementPropertyChanged(PropertyChangedEventArgs args)
{
base.OnElementPropertyChanged(args);

if (args.PropertyName != BackgroundEffect.BackgroundProperty.PropertyName) return;

var oldBackground = _background;
if (args.PropertyName == BackgroundEffect.BackgroundProperty.PropertyName)
{
UpdateBackgroundIfNotRemoved();
}
}

_background = BackgroundEffect.GetBackground(Element);
_background?.SetBinding(BindableObject.BindingContextProperty,
new Binding("BindingContext", source: Element));
private void UpdateBackgroundIfNotRemoved()
{
if (BackgroundEffect.GetBackground(Element) == null)
{
if (_backgroundManager != null)
{
_backgroundManager.Dispose();
_backgroundManager = null;
}

UpdateBackground(oldBackground, _background);
View.Background = null;
}
else
{
var shouldCreateTracker = _backgroundManager == null;
if (shouldCreateTracker)
{
SetTracker();
}

var oldBackground = _background;

_background = BackgroundEffect.GetBackground(Element);
_background?.SetBinding(BindableObject.BindingContextProperty,
new Binding("BindingContext", source: Element));

if (!shouldCreateTracker)
{
UpdateBackground(oldBackground, _background);
}
}
}

private void SetTracker()
Expand Down Expand Up @@ -82,14 +106,22 @@ private void HandleBackgroundManager()
* At this time, the BackgroundManager did the override
* and we are up and running!
*/
if (!(Element is Image) && !(Element is Label)) return;

Control.ViewAttachedToWindow += OnAttachedToWindow;
if (Element is Image || Element is Label)
{
Control.ViewAttachedToWindow += OnAttachedToWindow;
}
}

private void OnAttachedToWindow(object sender, AView.ViewAttachedToWindowEventArgs e)
{
UpdateBackground(null, _background);
if (_background == null)
{
View.Background = null;
}
else
{
UpdateBackground(null, _background);
}
}

private void UpdateBackground(IMaterialVisualElement oldBackground, IMaterialVisualElement newBackground)
Expand All @@ -99,11 +131,16 @@ private void UpdateBackground(IMaterialVisualElement oldBackground, IMaterialVis

private void Dispose()
{
_backgroundManager?.Dispose();

if (!(Element is Image) && !(Element is Label)) return;
if (_backgroundManager != null)
{
_backgroundManager.Dispose();
_background = null;
}

Control.ViewAttachedToWindow += OnAttachedToWindow;
if (Element is Image || Element is Label)
{
Control.ViewAttachedToWindow -= OnAttachedToWindow;
}
}
}
}
64 changes: 48 additions & 16 deletions src/XamarinBackgroundKit.iOS/Effects/BackgroundEffect.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel;
using System.Linq;
using FBKVOControllerNS;
using Foundation;
using UIKit;
Expand All @@ -25,14 +26,10 @@ private class KVOObserver : NSObject { }
protected override void OnAttached()
{
base.OnAttached();

SetTracker();


AddViewObservers();

_background = BackgroundEffect.GetBackground(Element);
_background?.SetBinding(BindableObject.BindingContextProperty,
new Binding("BindingContext", source: Element));

UpdateBackgroundIfNotRemoved();

//FIX for Ripple. The views must be focusable
if (Element is Layout || Container is BoxRenderer)
Expand All @@ -43,6 +40,46 @@ protected override void OnAttached()
}
}
}

private void UpdateBackgroundIfNotRemoved()
{
if (BackgroundEffect.GetBackground(Element) == null)
{
if (_backgroundManager != null)
{
_backgroundManager.Dispose();
_backgroundManager = null;
}

if (View is UITextField textField)
{
textField.BorderStyle = UITextBorderStyle.None;
}

View.Layer?.Sublayers
?.FirstOrDefault(x => x is GradientStrokeLayer)
?.RemoveFromSuperLayer();
}
else
{
var shouldCreateTracker = _backgroundManager == null;
if (shouldCreateTracker)
{
SetTracker();
}

var oldBackground = _background;

_background = BackgroundEffect.GetBackground(Element);
_background?.SetBinding(BindableObject.BindingContextProperty,
new Binding("BindingContext", source: Element));

if (!shouldCreateTracker)
{
_backgroundManager?.SetBackgroundElement(oldBackground, _background);
}
}
}

protected override void OnDetached()
{
Expand All @@ -61,15 +98,10 @@ protected override void OnElementPropertyChanged(PropertyChangedEventArgs args)
{
base.OnElementPropertyChanged(args);

if (args.PropertyName != BackgroundEffect.BackgroundProperty.PropertyName) return;

var oldBackground = _background;

_background = BackgroundEffect.GetBackground(Element);
_background?.SetBinding(BindableObject.BindingContextProperty,
new Binding("BindingContext", source: Element));

_backgroundManager?.SetBackgroundElement(oldBackground, _background);
if (args.PropertyName == BackgroundEffect.BackgroundProperty.PropertyName)
{
UpdateBackgroundIfNotRemoved();
}
}

private void SetTracker()
Expand Down
19 changes: 19 additions & 0 deletions src/XamarinBackgroundKitSample/PickerTestPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:controls="http://xamarin.com/schemas/2014/forms/background"
x:Class="XamarinBackgroundKitSample.PickerTestPage">
<StackLayout BackgroundColor="Red">
<controls:MaterialContentView Padding="16" Margin="16" Background="{controls:BgProvider Elevation=4, Color=White}" IsCornerRadiusHalfHeight="True">
<Picker controls:BackgroundEffect.Background="{x:Null}" Title="Test Title">
<Picker.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>Test 1</x:String>
<x:String>Test 2</x:String>
<x:String>Test 3</x:String>
</x:Array>
</Picker.ItemsSource>
</Picker>
</controls:MaterialContentView>
</StackLayout>
</ContentPage>
10 changes: 10 additions & 0 deletions src/XamarinBackgroundKitSample/PickerTestPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace XamarinBackgroundKitSample
{
public partial class PickerTestPage
{
public PickerTestPage()
{
InitializeComponent();
}
}
}

0 comments on commit 1be56e8

Please sign in to comment.