protected override void OnElementChanged(ElementChangedEventArgs <Button> e)
        {
            base.OnElementChanged(e);

            if (e.OldElement == null)
            {
                AButton button = Control;
                if (button == null)
                {
                    button = CreateNativeControl();
                    button.SetOnClickListener(ButtonClickListener.Instance.Value);
                    button.SetOnTouchListener(ButtonTouchListener.Instance.Value);
                    button.Tag = this;
                    SetNativeControl(button);
                    _textColorSwitcher = new TextColorSwitcher(button.TextColors);
                    button.AddOnAttachStateChangeListener(this);
                }
            }
            else
            {
                if (_drawableEnabled)
                {
                    _drawableEnabled = false;
                    _backgroundDrawable.Reset();
                    _backgroundDrawable = null;
                }
            }

            UpdateAll();
        }
Exemple #2
0
 public void Reset()
 {
     if (_drawableEnabled)
     {
         _drawableEnabled = false;
         _backgroundDrawable?.Reset();
         _backgroundDrawable = null;
     }
 }
Exemple #3
0
        public void UpdateDrawable()
        {
            if (_button == null || _nativeButton == null)
            {
                return;
            }

            if (_button.BackgroundColor == Color.Default)
            {
                if (!_drawableEnabled)
                {
                    return;
                }

                if (_defaultDrawable != null)
                {
                    _nativeButton.SetBackground(_defaultDrawable);
                }

                _drawableEnabled = false;
            }
            else
            {
                if (_backgroundDrawable == null)
                {
                    _backgroundDrawable = new ButtonDrawable(_nativeButton.Context.ToPixels);
                }

                _backgroundDrawable.Button = _button;

                if (_drawableEnabled)
                {
                    return;
                }

                if (_defaultDrawable == null)
                {
                    _defaultDrawable = _nativeButton.Background;
                }

                _nativeButton.SetBackground(_backgroundDrawable);
                _drawableEnabled = true;
            }

            _nativeButton.Invalidate();
        }
Exemple #4
0
 protected virtual void Dispose(bool disposing)
 {
     if (!_disposed)
     {
         if (disposing)
         {
             _backgroundDrawable?.Dispose();
             _backgroundDrawable = null;
             _defaultDrawable?.Dispose();
             _defaultDrawable = null;
             if (_button != null)
             {
                 _button.PropertyChanged -= ButtonPropertyChanged;
                 _button = null;
             }
             _nativeButton = null;
         }
         _disposed = true;
     }
 }
        protected override void Dispose(bool disposing)
        {
            if (_isDisposed)
            {
                return;
            }

            _isDisposed = true;

            if (disposing)
            {
                if (_backgroundDrawable != null)
                {
                    _backgroundDrawable.Dispose();
                    _backgroundDrawable = null;
                }
            }

            base.Dispose(disposing);
        }
        void UpdateDrawable()
        {
            if (Element.BackgroundColor == Color.Default)
            {
                if (!_drawableEnabled)
                {
                    return;
                }

                if (_defaultDrawable != null)
                {
                    Control.SetBackground(_defaultDrawable);
                }

                _drawableEnabled = false;
            }
            else
            {
                if (_backgroundDrawable == null)
                {
                    _backgroundDrawable = new ButtonDrawable();
                }

                _backgroundDrawable.Button = Element;

                if (_drawableEnabled)
                {
                    return;
                }

                if (_defaultDrawable == null)
                {
                    _defaultDrawable = Control.Background;
                }

                Control.SetBackground(_backgroundDrawable);
                _drawableEnabled = true;
            }

            Control.Invalidate();
        }
Exemple #7
0
        public void UpdateDrawable()
        {
            if (_button == null || _nativeButton == null)
            {
                return;
            }

            bool cornerRadiusIsDefault    = !_button.IsSet(Button.CornerRadiusProperty) || (_button.CornerRadius == (int)Button.CornerRadiusProperty.DefaultValue || _button.CornerRadius == ButtonDrawable.DefaultCornerRadius);
            bool backgroundColorIsDefault = !_button.IsSet(VisualElement.BackgroundColorProperty) || _button.BackgroundColor == (Color)VisualElement.BackgroundColorProperty.DefaultValue;
            bool borderColorIsDefault     = !_button.IsSet(Button.BorderColorProperty) || _button.BorderColor == (Color)Button.BorderColorProperty.DefaultValue;
            bool borderWidthIsDefault     = !_button.IsSet(Button.BorderWidthProperty) || _button.BorderWidth == (double)Button.BorderWidthProperty.DefaultValue;

            if (backgroundColorIsDefault &&
                cornerRadiusIsDefault &&
                borderColorIsDefault &&
                borderWidthIsDefault)
            {
                if (!_drawableEnabled)
                {
                    return;
                }

                if (_defaultDrawable != null)
                {
                    _nativeButton.SetBackground(_defaultDrawable);
                }

                _drawableEnabled = false;
            }
            else
            {
                if (_backgroundDrawable == null)
                {
                    _backgroundDrawable = new ButtonDrawable(_nativeButton.Context.ToPixels, Forms.GetColorButtonNormal(_nativeButton.Context));
                }

                _backgroundDrawable.Button = _button;

                var useDefaultPadding = _button.OnThisPlatform().UseDefaultPadding();

                int paddingTop  = useDefaultPadding ? _nativeButton.PaddingTop : 0;
                int paddingLeft = useDefaultPadding ? _nativeButton.PaddingLeft : 0;

                var useDefaultShadow = _button.OnThisPlatform().UseDefaultShadow();

                float  shadowRadius = useDefaultShadow ? 2 : _nativeButton.ShadowRadius;
                float  shadowDx     = useDefaultShadow ? 0 : _nativeButton.ShadowDx;
                float  shadowDy     = useDefaultShadow ? 4 : _nativeButton.ShadowDy;
                AColor shadowColor  = useDefaultShadow ? _backgroundDrawable.PressedBackgroundColor.ToAndroid() : _nativeButton.ShadowColor;

                _backgroundDrawable.SetPadding(paddingTop, paddingLeft)
                .SetShadow(shadowDy, shadowDx, shadowColor, shadowRadius);

                if (_drawableEnabled)
                {
                    return;
                }

                if (_defaultDrawable == null)
                {
                    _defaultDrawable = _nativeButton.Background;
                }

                if (Forms.IsLollipopOrNewer)
                {
                    var rippleColor = _backgroundDrawable.PressedBackgroundColor.ToAndroid();

                    _rippleDrawable = new RippleDrawable(ColorStateList.ValueOf(rippleColor), _backgroundDrawable, null);
                    _nativeButton.SetBackground(_rippleDrawable);
                }
                else
                {
                    _nativeButton.SetBackground(_backgroundDrawable);
                }

                _drawableEnabled = true;
            }

            _nativeButton.Invalidate();
        }
        public void UpdateDrawable()
        {
            if (_button == null || _nativeButton == null)
            {
                return;
            }

            bool cornerRadiusIsDefault    = !_button.IsSet(Button.CornerRadiusProperty) || (_button.CornerRadius == (int)Button.CornerRadiusProperty.DefaultValue || _button.CornerRadius == ButtonDrawable.DefaultCornerRadius);
            bool backgroundColorIsDefault = !_button.IsSet(VisualElement.BackgroundColorProperty) || _button.BackgroundColor == (Color)VisualElement.BackgroundColorProperty.DefaultValue;
            bool borderColorIsDefault     = !_button.IsSet(Button.BorderColorProperty) || _button.BorderColor == (Color)Button.BorderColorProperty.DefaultValue;
            bool borderWidthIsDefault     = !_button.IsSet(Button.BorderWidthProperty) || _button.BorderWidth == (double)Button.BorderWidthProperty.DefaultValue;

            if (backgroundColorIsDefault &&
                cornerRadiusIsDefault &&
                borderColorIsDefault &&
                borderWidthIsDefault)
            {
                if (!_drawableEnabled)
                {
                    return;
                }

                if (_defaultDrawable != null)
                {
                    _nativeButton.SetBackground(_defaultDrawable);
                }

                _drawableEnabled = false;
            }
            else
            {
                if (_backgroundDrawable == null)
                {
                    _backgroundDrawable = new ButtonDrawable(_nativeButton.Context.ToPixels, Forms.GetColorButtonNormal(_nativeButton.Context));
                }

                _backgroundDrawable.Button = _button;

                var useDefaultPadding = _button.OnThisPlatform().UseDefaultPadding();

                int paddingTop  = useDefaultPadding ? _nativeButton.PaddingTop : 0;
                int paddingLeft = useDefaultPadding ? _nativeButton.PaddingLeft : 0;

                var useDefaultShadow = _button.OnThisPlatform().UseDefaultShadow();

                // Use no shadow by default for API < 16
                float  shadowRadius = 0;
                float  shadowDy     = 0;
                float  shadowDx     = 0;
                AColor shadowColor  = Color.Transparent.ToAndroid();
                // Add Android's default material shadow if we want it
                if (useDefaultShadow)
                {
                    shadowRadius = 2;
                    shadowDy     = 4;
                    shadowDx     = 0;
                    shadowColor  = _backgroundDrawable.PressedBackgroundColor.ToAndroid();
                }
                // Otherwise get values from the control (but only for supported APIs)
                else if ((int)Build.VERSION.SdkInt >= 16)
                {
                    shadowRadius = _nativeButton.ShadowRadius;
                    shadowDy     = _nativeButton.ShadowDy;
                    shadowDx     = _nativeButton.ShadowDx;
                    shadowColor  = _nativeButton.ShadowColor;
                }

                _backgroundDrawable.SetPadding(paddingTop, paddingLeft)
                .SetShadow(shadowDy, shadowDx, shadowColor, shadowRadius);

                if (_drawableEnabled)
                {
                    return;
                }

                if (_defaultDrawable == null)
                {
                    _defaultDrawable = _nativeButton.Background;
                }

                if (Forms.IsLollipopOrNewer)
                {
                    var rippleColor = _backgroundDrawable.PressedBackgroundColor.ToAndroid();

                    _rippleDrawable = new RippleDrawable(ColorStateList.ValueOf(rippleColor), _backgroundDrawable, null);
                    _nativeButton.SetBackground(_rippleDrawable);
                }
                else
                {
                    _nativeButton.SetBackground(_backgroundDrawable);
                }

                _drawableEnabled = true;
            }

            _nativeButton.Invalidate();
        }
        public void UpdateDrawable()
        {
            if (_button == null || _nativeButton == null)
            {
                return;
            }

            bool cornerRadiusIsDefault    = !_button.IsSet(Button.CornerRadiusProperty) || (_button.CornerRadius == (int)Button.CornerRadiusProperty.DefaultValue || _button.CornerRadius == ButtonDrawable.DefaultCornerRadius);
            bool backgroundColorIsDefault = !_button.IsSet(VisualElement.BackgroundColorProperty) || _button.BackgroundColor == (Color)VisualElement.BackgroundColorProperty.DefaultValue;
            bool borderColorIsDefault     = !_button.IsSet(Button.BorderColorProperty) || _button.BorderColor == (Color)Button.BorderColorProperty.DefaultValue;
            bool borderWidthIsDefault     = !_button.IsSet(Button.BorderWidthProperty) || _button.BorderWidth == (double)Button.BorderWidthProperty.DefaultValue;

            if (backgroundColorIsDefault &&
                cornerRadiusIsDefault &&
                borderColorIsDefault &&
                borderWidthIsDefault)
            {
                if (!_drawableEnabled)
                {
                    return;
                }

                if (_defaultDrawable != null)
                {
                    _nativeButton.SetBackground(_defaultDrawable);
                }

                _drawableEnabled = false;
            }
            else
            {
                if (_backgroundDrawable == null)
                {
                    _backgroundDrawable = new ButtonDrawable(_nativeButton.Context.ToPixels, Forms.GetColorButtonNormal(_nativeButton.Context));
                }

                _backgroundDrawable.Button = _button;
                _backgroundDrawable.SetPaddingTop(_nativeButton.PaddingTop);

                if (_drawableEnabled)
                {
                    return;
                }

                if (_defaultDrawable == null)
                {
                    _defaultDrawable = _nativeButton.Background;
                }

                if (Forms.IsLollipopOrNewer)
                {
                    var rippleColor = _backgroundDrawable.PressedBackgroundColor.ToAndroid();

                    _rippleDrawable = new RippleDrawable(ColorStateList.ValueOf(rippleColor), _backgroundDrawable, null);
                    _nativeButton.SetBackground(_rippleDrawable);
                }
                else
                {
                    _nativeButton.SetBackground(_backgroundDrawable);
                }

                _drawableEnabled = true;
            }

            _nativeButton.Invalidate();
        }