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

Commit

Permalink
Revert "Fix border on android buttons (#941)"
Browse files Browse the repository at this point in the history
This reverts commit aa9bca1.
  • Loading branch information
samhouts committed Oct 9, 2017
1 parent dff6ecd commit 828dac5
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 178 deletions.
60 changes: 42 additions & 18 deletions Xamarin.Forms.Platform.Android/AppCompat/ButtonRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
using System;
using System.ComponentModel;
using Android.Content;
using Android.Content.Res;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.Support.V4.Content;
using Android.Support.V7.Widget;
using Android.Util;
using Xamarin.Forms.Internals;
using GlobalResource = Android.Resource;
using Object = Java.Lang.Object;
using AView = Android.Views.View;
using AMotionEvent = Android.Views.MotionEvent;
Expand All @@ -14,7 +19,6 @@ namespace Xamarin.Forms.Platform.Android.AppCompat
{
public class ButtonRenderer : ViewRenderer<Button, AppCompatButton>, AView.IOnAttachStateChangeListener
{
ButtonBackgroundTracker _backgroundTracker;
TextColorSwitcher _textColorSwitcher;
float _defaultFontSize;
Typeface _defaultTypeface;
Expand Down Expand Up @@ -85,7 +89,6 @@ protected override void Dispose(bool disposing)
Control.Tag = null;
_textColorSwitcher = null;
}
_backgroundTracker?.Dispose();
}

base.Dispose(disposing);
Expand Down Expand Up @@ -114,12 +117,8 @@ protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
button.AddOnAttachStateChangeListener(this);
}

if (_backgroundTracker == null)
_backgroundTracker = new ButtonBackgroundTracker(Element, Control);
else
_backgroundTracker.Button = e.NewElement;

UpdateAll();
UpdateBackgroundColor();
}
}

Expand All @@ -146,7 +145,42 @@ protected override void UpdateBackgroundColor()
if (Element == null || Control == null)
return;

_backgroundTracker?.UpdateBackgroundColor();
Color backgroundColor = Element.BackgroundColor;
if (backgroundColor.IsDefault)
{
if (Control.SupportBackgroundTintList != null)
{
Context context = Context;
int id = GlobalResource.Attribute.ButtonTint;
unchecked
{
using (var value = new TypedValue())
{
try
{
Resources.Theme theme = context.Theme;
if (theme != null && theme.ResolveAttribute(id, value, true))
#pragma warning disable 618
Control.SupportBackgroundTintList = Resources.GetColorStateList(value.Data);
#pragma warning restore 618
else
Control.SupportBackgroundTintList = new ColorStateList(ColorExtensions.States, new[] { (int)0xffd7d6d6, 0x7fd7d6d6 });
}
catch (Exception ex)
{
Internals.Log.Warning("Xamarin.Forms.Platform.Android.ButtonRenderer", "Could not retrieve button background resource: {0}", ex);
Control.SupportBackgroundTintList = new ColorStateList(ColorExtensions.States, new[] { (int)0xffd7d6d6, 0x7fd7d6d6 });
}
}
}
}
}
else
{
int intColor = backgroundColor.ToAndroid().ToArgb();
int disableColor = backgroundColor.MultiplyAlpha(0.5).ToAndroid().ToArgb();
Control.SupportBackgroundTintList = new ColorStateList(ColorExtensions.States, new[] { intColor, disableColor });
}
}

void UpdateAll()
Expand All @@ -156,16 +190,6 @@ void UpdateAll()
UpdateBitmap();
UpdateTextColor();
UpdateEnabled();
UpdateBackgroundColor();
UpdateDrawable();
}

void UpdateDrawable()
{
if (Element == null || Control == null)
return;

_backgroundTracker?.UpdateDrawable();
}

void UpdateBitmap()
Expand Down
126 changes: 0 additions & 126 deletions Xamarin.Forms.Platform.Android/ButtonBackgroundTracker.cs

This file was deleted.

76 changes: 56 additions & 20 deletions Xamarin.Forms.Platform.Android/FastRenderers/ButtonRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
using System;
using System.ComponentModel;
using Android.Content;
using Android.Content.Res;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.Support.V7.Widget;
using Android.Util;
using Android.Views;
using Xamarin.Forms.Internals;
using GlobalResource = Android.Resource;
using AView = Android.Views.View;
using AMotionEvent = Android.Views.MotionEvent;
using AMotionEventActions = Android.Views.MotionEventActions;
using static System.String;
using Object = Java.Lang.Object;

namespace Xamarin.Forms.Platform.Android.FastRenderers
{
Expand All @@ -26,7 +30,6 @@ internal sealed class ButtonRenderer : AppCompatButton, IVisualElementRenderer,
readonly AutomationPropertiesProvider _automationPropertiesProvider;
readonly EffectControlProvider _effectControlProvider;
VisualElementTracker _tracker;
ButtonBackgroundTracker _backgroundTracker;

public event EventHandler<VisualElementChangedEventArgs> ElementChanged;
public event EventHandler<PropertyChangedEventArgs> ElementPropertyChanged;
Expand Down Expand Up @@ -131,11 +134,6 @@ void IVisualElementRenderer.SetElement(VisualElement element)
oldElement.PropertyChanged -= OnElementPropertyChanged;
}

if (_backgroundTracker == null)
_backgroundTracker = new ButtonBackgroundTracker(Button, this);
else
_backgroundTracker.Button = Button;

Color currentColor = oldElement?.BackgroundColor ?? Color.Default;
if (element.BackgroundColor != currentColor)
{
Expand Down Expand Up @@ -194,8 +192,6 @@ protected override void Dispose(bool disposing)
_automationPropertiesProvider?.Dispose();
_tracker?.Dispose();

_backgroundTracker?.Dispose();

if (Element != null)
{
Element.PropertyChanged -= OnElementPropertyChanged;
Expand Down Expand Up @@ -223,10 +219,6 @@ Size MinimumSize()

void OnElementChanged(ElementChangedEventArgs<Button> e)
{
if (e.OldElement != null)
{
_backgroundTracker?.Reset();
}
if (e.NewElement != null && !_isDisposed)
{
this.EnsureId();
Expand All @@ -238,7 +230,6 @@ void OnElementChanged(ElementChangedEventArgs<Button> e)
UpdateIsEnabled();
UpdateInputTransparent();
UpdateBackgroundColor();
UpdateDrawable();

ElevationHelper.SetElevation(this, e.NewElement);
}
Expand Down Expand Up @@ -275,6 +266,10 @@ void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
else if (e.PropertyName == VisualElement.InputTransparentProperty.PropertyName)
{
UpdateInputTransparent();
}
else if (e.PropertyName == VisualElement.BackgroundColorProperty.PropertyName)
{
UpdateBackgroundColor();
}

ElementPropertyChanged?.Invoke(this, e);
Expand Down Expand Up @@ -306,7 +301,54 @@ void SetTracker(VisualElementTracker tracker)

void UpdateBackgroundColor()
{
_backgroundTracker?.UpdateBackgroundColor();
if (Element == null)
{
return;
}

Color backgroundColor = Element.BackgroundColor;
if (backgroundColor.IsDefault)
{
if (SupportBackgroundTintList != null)
{
Context context = Context;
int id = GlobalResource.Attribute.ButtonTint;
unchecked
{
using (var value = new TypedValue())
{
try
{
Resources.Theme theme = context.Theme;
if (theme != null && theme.ResolveAttribute(id, value, true))
#pragma warning disable 618
{
SupportBackgroundTintList = Resources.GetColorStateList(value.Data);
}
#pragma warning restore 618
else
{
SupportBackgroundTintList = new ColorStateList(ColorExtensions.States,
new[] { (int)0xffd7d6d6, 0x7fd7d6d6 });
}
}
catch (Exception ex)
{
Internals.Log.Warning("Xamarin.Forms.Platform.Android.ButtonRenderer",
"Could not retrieve button background resource: {0}", ex);
SupportBackgroundTintList = new ColorStateList(ColorExtensions.States,
new[] { (int)0xffd7d6d6, 0x7fd7d6d6 });
}
}
}
}
}
else
{
int intColor = backgroundColor.ToAndroid().ToArgb();
int disableColor = backgroundColor.MultiplyAlpha(0.5).ToAndroid().ToArgb();
SupportBackgroundTintList = new ColorStateList(ColorExtensions.States, new[] { intColor, disableColor });
}
}

internal void OnNativeFocusChanged(bool hasFocus)
Expand Down Expand Up @@ -468,11 +510,5 @@ void UpdateTextColor()

_textColorSwitcher.Value.UpdateTextColor(this, Button.TextColor);
}

void UpdateDrawable()
{
_backgroundTracker?.UpdateDrawable();
}

}
}
1 change: 1 addition & 0 deletions Xamarin.Forms.Platform.Android/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
[assembly: ExportRenderer (typeof (Stepper), typeof (StepperRenderer))]
[assembly: ExportRenderer (typeof (ProgressBar), typeof (ProgressBarRenderer))]
[assembly: ExportRenderer (typeof (ScrollView), typeof (ScrollViewRenderer))]
[assembly: ExportRenderer (typeof (Toolbar), typeof (ToolbarRenderer))]
[assembly: ExportRenderer (typeof (ActivityIndicator), typeof (ActivityIndicatorRenderer))]
[assembly: ExportRenderer (typeof (Frame), typeof (FrameRenderer))]
[assembly: ExportRenderer (typeof (NavigationMenu), typeof (NavigationMenuRenderer))]
Expand Down
1 change: 0 additions & 1 deletion Xamarin.Forms.Platform.Android/Renderers/ButtonDrawable.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Linq;
using Android.Graphics;
using Android.Graphics.Drawables;
using System;

namespace Xamarin.Forms.Platform.Android
{
Expand Down
Loading

0 comments on commit 828dac5

Please sign in to comment.