This repository has been archived by the owner on May 1, 2024. It is now read-only.
Fix border on android buttons #941
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
Xamarin.Forms.Platform.Android/ButtonBackgroundTracker.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
using System; | ||
using System.ComponentModel; | ||
using Android.Graphics.Drawables; | ||
using AButton = Android.Widget.Button; | ||
|
||
namespace Xamarin.Forms.Platform.Android | ||
{ | ||
internal class ButtonBackgroundTracker : IDisposable | ||
{ | ||
Drawable _defaultDrawable; | ||
ButtonDrawable _backgroundDrawable; | ||
Button _button; | ||
AButton _nativeButton; | ||
bool _drawableEnabled; | ||
bool _disposed; | ||
|
||
public ButtonBackgroundTracker(Button button, AButton nativeButton) | ||
{ | ||
Button = button; | ||
_nativeButton = nativeButton; | ||
} | ||
|
||
public Button Button | ||
{ | ||
get { return _button; } | ||
set | ||
{ | ||
if (_button == value) | ||
return; | ||
if (_button != null) | ||
_button.PropertyChanged -= ButtonPropertyChanged; | ||
_button = value; | ||
_button.PropertyChanged += ButtonPropertyChanged; | ||
} | ||
} | ||
|
||
public void UpdateDrawable() | ||
{ | ||
if (_button.BackgroundColor == Color.Default) | ||
{ | ||
if (!_drawableEnabled) | ||
return; | ||
|
||
if (_defaultDrawable != null) | ||
_nativeButton.SetBackground(_defaultDrawable); | ||
|
||
_drawableEnabled = false; | ||
} | ||
else | ||
{ | ||
if (_backgroundDrawable == null) | ||
_backgroundDrawable = new ButtonDrawable(); | ||
|
||
_backgroundDrawable.Button = _button; | ||
|
||
if (_drawableEnabled) | ||
return; | ||
|
||
if (_defaultDrawable == null) | ||
_defaultDrawable = _nativeButton.Background; | ||
|
||
_nativeButton.SetBackground(_backgroundDrawable); | ||
_drawableEnabled = true; | ||
} | ||
|
||
_nativeButton.Invalidate(); | ||
} | ||
|
||
public void Reset() | ||
{ | ||
if (_drawableEnabled) | ||
{ | ||
_drawableEnabled = false; | ||
_backgroundDrawable.Reset(); | ||
_backgroundDrawable = null; | ||
} | ||
} | ||
|
||
public void UpdateBackgroundColor() | ||
{ | ||
if (_button == null) | ||
return; | ||
UpdateDrawable(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
} | ||
|
||
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; | ||
} | ||
} | ||
|
||
void ButtonPropertyChanged(object sender, PropertyChangedEventArgs e) | ||
{ | ||
if (e.PropertyName.Equals(Button.BorderColorProperty.PropertyName) || | ||
e.PropertyName.Equals(Button.BorderWidthProperty.PropertyName) || | ||
e.PropertyName.Equals(Button.BorderRadiusProperty.PropertyName) || | ||
e.PropertyName.Equals(VisualElement.BackgroundColorProperty.PropertyName)) | ||
{ | ||
Reset(); | ||
UpdateDrawable(); | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is something we also added on non app compact button so users select fast renderers, should we remove it @ez ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that should have been removed a while ago.