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

Revert "Fix border on android buttons (#941)" #1192

Merged
merged 1 commit into from
Oct 10, 2017
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
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 @@ -79,7 +83,6 @@ protected override void Dispose(bool disposing)
Control.Tag = null;
_textColorSwitcher = null;
}
_backgroundTracker?.Dispose();
}

base.Dispose(disposing);
Expand Down Expand Up @@ -108,12 +111,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 @@ -140,7 +139,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 @@ -150,16 +184,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.

78 changes: 57 additions & 21 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 @@ -121,11 +124,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 @@ -184,8 +182,6 @@ protected override void Dispose(bool disposing)
_automationPropertiesProvider?.Dispose();
_tracker?.Dispose();

_backgroundTracker?.Dispose();

if (Element != null)
{
Element.PropertyChanged -= OnElementPropertyChanged;
Expand Down Expand Up @@ -213,11 +209,7 @@ Size MinimumSize()

void OnElementChanged(ElementChangedEventArgs<Button> e)
{
if (e.OldElement != null)
{
_backgroundTracker?.Reset();
}
if (e.NewElement != null && !_isDisposed)
if (e.NewElement != null)
{
this.EnsureId();

Expand All @@ -228,7 +220,6 @@ void OnElementChanged(ElementChangedEventArgs<Button> e)
UpdateIsEnabled();
UpdateInputTransparent();
UpdateBackgroundColor();
UpdateDrawable();
}

ElementChanged?.Invoke(this, new VisualElementChangedEventArgs(e.OldElement, e.NewElement));
Expand Down Expand Up @@ -263,6 +254,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 @@ -294,7 +289,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 @@ -456,11 +498,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
Loading