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

Fix material colors to match previous version better #13666

Merged
merged 9 commits into from
Feb 6, 2021
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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ namespace Xamarin.Forms.Controls.Issues
{
public partial class VisualControlsPage : TestShell
{
bool isVisible = false;
double percentage = 0.0;

public VisualControlsPage()
{
#if APP
Expand All @@ -29,47 +26,6 @@ protected override void Init()
BindingContext = this;
}

public double PercentageCounter
{
get { return percentage; }
set
{
percentage = value;
OnPropertyChanged();
OnPropertyChanged(nameof(Counter));
}
}

public double Counter => percentage * 10;

protected override void OnAppearing()
{
isVisible = true;

base.OnAppearing();

Device.StartTimer(TimeSpan.FromSeconds(1), () =>
{
var progress = PercentageCounter + 0.1;
if (progress > 1)
progress = 0;

PercentageCounter = progress;

return isVisible;
});
}

protected override void OnDisappearing()
{
isVisible = false;

base.OnDisappearing();
}




[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 4435, "Visual Gallery Loads",
PlatformAffected.iOS | PlatformAffected.Android)]
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace Xamarin.Forms.Controls.Issues
{
public partial class VisualGallery : ContentPage
{
bool isVisible = false;
double percentage = 0.0;
public VisualGallery()
{
#if APP
InitializeComponent();
Device.BeginInvokeOnMainThread(OnAppearing);
pushPage.Clicked += PushPage;
#endif
}

async void PushPage(object sender, EventArgs e)
{
await Navigation.PushAsync(new VisualGallery());
}

public double PercentageCounter
{
get { return percentage; }
set
{
percentage = value;
OnPropertyChanged();
OnPropertyChanged(nameof(Counter));
}
}

public double Counter => percentage * 10;

protected override void OnAppearing()
{
if (!isVisible)
return;

isVisible = true;

base.OnAppearing();

Device.StartTimer(TimeSpan.FromSeconds(1), () =>
{
var progress = PercentageCounter + 0.1;
if (progress > 1)
progress = 0;

PercentageCounter = progress;

return isVisible;
});
}

protected override void OnDisappearing()
{
isVisible = false;

base.OnDisappearing();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,10 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue10699.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue11185.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue10307.cs" />
<Compile Include="$(MSBuildThisFileDirectory)VisualGallery.xaml.cs">
<DependentUpon>VisualGallery.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)_TemplateMarkup.xaml.cs">
<DependentUpon>_TemplateMarkup.xaml</DependentUpon>
<SubType>Code</SubType>
Expand Down Expand Up @@ -2653,4 +2657,10 @@
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)VisualGallery.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup>
</Project>
16 changes: 15 additions & 1 deletion Xamarin.Forms.Material.Android/MaterialButtonRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,27 @@ protected Button Element

public override void Draw(Canvas canvas)
{
_hasDrawnOnce = true;
if (Element.IsEnabled != Enabled)
{
_hasDrawnOnce = true;
Enabled = Element.IsEnabled;
return;
}

if(!_hasDrawnOnce)
{
_hasDrawnOnce = true;

// https://github.com/xamarin/Xamarin.Forms/issues/13416
// This forces a redraw the first time which works around
// an issue with setting background color and having an image set
if (Enabled && Element.ImageSource != null)
{
Enabled = false;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Redth or @jsuarezruiz if you have any better thoughts or tricks to address this issue.

Enabled = true;
}
}

if (Element == null || Element.CornerRadius <= 0)
{
base.Draw(canvas);
Expand Down
28 changes: 16 additions & 12 deletions Xamarin.Forms.Material.Android/MaterialColors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ internal static class MaterialColors
const float kFilledTextFieldIndicatorLineAlpha = 0.42f;
const float kFilledTextFieldIconAlpha = 0.54f;

// the idea of this value is that I want Active to be the exact color the user specified
// and then all the other colors decrease according to the Material theme setup
static float kFilledPlaceHolderOffset = 1f - kFilledTextFieldActiveAlpha;

// State list from material-components-android
// https://github.com/material-components/material-components-android/blob/71694616056012fe1162adb9144be903d1e510d5/lib/java/com/google/android/material/textfield/res/values/colors.xml#L28
public static PlatformColor CreateEntryFilledInputBackgroundColor(Color backgroundColor, Color textColor)
Expand All @@ -77,24 +73,29 @@ public static PlatformColor CreateEntryFilledInputBackgroundColor(Color backgrou
return ToPlatformColor(backgroundColor);
}

public static (PlatformColor InlineColor, PlatformColor FloatingColor) GetPlaceHolderColor(Color placeholderColor, Color textColor)
public static (PlatformColor InlineColor, PlatformColor FloatingColor, PlatformColor DefaultColor) GetPlaceHolderColor(Color placeholderColor, Color textColor)
{
PlatformColor inlineColor;
PlatformColor floatingColor;
float inlineAlpha = kFilledTextFieldOnSurfaceAlpha + kFilledPlaceHolderOffset;
PlatformColor defaultColor;

if (placeholderColor == Color.Default)
{
inlineColor = WithAlpha(MaterialColors.Light.OnSurfaceColor, inlineAlpha);
inlineColor = WithAlpha(MaterialColors.Light.OnSurfaceColor, kFilledTextFieldDisabledAlpha);

defaultColor = WithAlpha(MaterialColors.Light.OnSurfaceColor, kFilledTextFieldOnSurfaceAlpha);

floatingColor = MaterialColors.Light.PrimaryColor;

}
else
{
inlineColor = WithAlpha(ToPlatformColor(placeholderColor), inlineAlpha);
inlineColor = ToPlatformColor(placeholderColor);
floatingColor = ToPlatformColor(placeholderColor);
defaultColor = ToPlatformColor(placeholderColor);
}

return (inlineColor, floatingColor);
return (inlineColor, floatingColor, defaultColor);
}

public static (PlatformColor FocusedColor, PlatformColor UnFocusedColor) GetUnderlineColor(Color placeholderColor)
Expand Down Expand Up @@ -138,7 +139,7 @@ public static PlatformColor GetCheckBoxColor(bool isChecked, bool isEnabled)
public static readonly int[][] ButtonTextStates =
{
new int[] { global::Android.Resource.Attribute.StateEnabled },
new int[] { ~global::Android.Resource.Attribute.StateEnabled },
new int[] { -global::Android.Resource.Attribute.StateEnabled },
new int[] { }
};

Expand All @@ -152,6 +153,9 @@ public static PlatformColor GetCheckBoxColor(bool isChecked, bool isEnabled)
{
new []{ global::Android.Resource.Attribute.StateFocused },
new []{ -global::Android.Resource.Attribute.StateFocused },
new int[] { global::Android.Resource.Attribute.StateEnabled },
new int[] { -global::Android.Resource.Attribute.StateEnabled },
new int[0] { }
};

// State list from material-components-android
Expand All @@ -174,7 +178,7 @@ public static ColorStateList CreateEntryFilledPlaceholderColors(PlatformColor in
{
int[][] States =
{
new []{ ~global::Android.Resource.Attribute.StateEnabled },
new []{ -global::Android.Resource.Attribute.StateEnabled },
new int[0] { }
};

Expand All @@ -184,7 +188,7 @@ public static ColorStateList CreateEntryFilledPlaceholderColors(PlatformColor in

public static ColorStateList CreateEntryUnderlineColors(PlatformColor focusedColor, PlatformColor unfocusedColor)
{
var colors = new int[] { focusedColor, unfocusedColor };
var colors = new int[] { focusedColor, unfocusedColor, focusedColor, focusedColor, unfocusedColor };
return new ColorStateList(EntryUnderlineStates, colors);
}

Expand Down
9 changes: 2 additions & 7 deletions Xamarin.Forms.Material.Android/MaterialDatePickerRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,8 @@ protected override void OnElementChanged(ElementChangedEventArgs<DatePicker> e)
UpdateBackgroundColor();
}

protected override void UpdateBackgroundColor()
{
if (_textInputLayout == null)
return;

_textInputLayout.BoxBackgroundColor = MaterialColors.CreateEntryFilledInputBackgroundColor(Element.BackgroundColor, Element.TextColor);
}
protected override void UpdateBackgroundColor() =>
_textInputLayout?.ApplyBackgroundColor(Element.BackgroundColor, Element.TextColor);

protected override void UpdateTextColor() => ApplyTheme();
void ApplyTheme() => _textInputLayout?.ApplyTheme(Element.TextColor, Color.Default);
Expand Down
9 changes: 2 additions & 7 deletions Xamarin.Forms.Material.Android/MaterialEditorRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,8 @@ protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
UpdateBackgroundColor();
}

protected override void UpdateBackgroundColor()
{
if (_disposed || _textInputLayout == null)
return;

_textInputLayout.BoxBackgroundColor = MaterialColors.CreateEntryFilledInputBackgroundColor(Element.BackgroundColor, Element.TextColor);
}
protected override void UpdateBackgroundColor() =>
_textInputLayout?.ApplyBackgroundColor(Element.BackgroundColor, Element.TextColor);

protected override void UpdatePlaceholderText()
{
Expand Down
9 changes: 2 additions & 7 deletions Xamarin.Forms.Material.Android/MaterialEntryRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,8 @@ protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)

protected override void UpdateColor() => ApplyTheme();

protected override void UpdateBackgroundColor()
{
if (_textInputLayout == null)
return;

_textInputLayout.BoxBackgroundColor = MaterialColors.CreateEntryFilledInputBackgroundColor(Element.BackgroundColor, Element.TextColor);
}
protected override void UpdateBackgroundColor() =>
_textInputLayout?.ApplyBackgroundColor(Element.BackgroundColor, Element.TextColor);

protected override void UpdatePlaceHolderText() => _textInputLayout.SetHint(Element.Placeholder, Element);
protected override EditText EditText => _textInputEditText;
Expand Down
24 changes: 18 additions & 6 deletions Xamarin.Forms.Material.Android/MaterialFormsTextInputLayoutBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public class MaterialFormsTextInputLayoutBase : TextInputLayout
Color _formsPlaceholderColor;
bool _isSetup = false;
ColorStateList _placeholderColorsList;

ColorStateList _placeholderDefaultColorList;
ColorStateList _underlineColorsList;

static readonly int[][] s_colorStates = { new[] { global::Android.Resource.Attribute.StateEnabled }, new[] { -global::Android.Resource.Attribute.StateEnabled } };
bool _disposed = false;

Expand Down Expand Up @@ -46,12 +48,21 @@ void ResetTextColors(Color formsTextColor, Color formsPlaceHolderColor)
var underlineColors = MaterialColors.GetUnderlineColor(_formsPlaceholderColor);
var placeHolderColors = MaterialColors.GetPlaceHolderColor(_formsPlaceholderColor, _formsTextColor);
_placeholderColorsList = MaterialColors.CreateEntryFilledPlaceholderColors(placeHolderColors.InlineColor, placeHolderColors.FloatingColor);


_placeholderDefaultColorList = MaterialColors.CreateEntryFilledPlaceholderColors(placeHolderColors.InlineColor, placeHolderColors.DefaultColor);
_underlineColorsList = MaterialColors.CreateEntryUnderlineColors(underlineColors.FocusedColor, underlineColors.UnFocusedColor);
var textColor = MaterialColors.GetEntryTextColor(formsTextColor).ToArgb();
EditText.SetTextColor(new ColorStateList(s_colorStates, new[] { textColor, textColor }));
}

public virtual void ApplyBackgroundColor(Color backgroundColor, Color textColor)
{
if (!this.IsAlive() || !this.EditText.IsAlive())
return;

var bgColor = (int)MaterialColors.CreateEntryFilledInputBackgroundColor(backgroundColor, textColor);
this.SetBoxBackgroundColorStateList(new ColorStateList(s_colorStates, new[] { bgColor, bgColor }));
}

public virtual void ApplyTheme(Color formsTextColor, Color formsPlaceHolderColor)
{
if (_disposed)
Expand All @@ -67,9 +78,10 @@ public virtual void ApplyTheme(Color formsTextColor, Color formsPlaceHolderColor
{
ResetTextColors(formsTextColor, formsPlaceHolderColor);
}
this.DefaultHintTextColor = _placeholderColorsList;
this.HintTextColor = _placeholderColorsList.ToDefaultOnlyColorStateList();
this.BoxStrokeColor = _placeholderColorsList.DefaultColor;

this.DefaultHintTextColor = _placeholderDefaultColorList;
this.HintTextColor = _placeholderColorsList;
this.SetBoxStrokeColorStateList(_underlineColorsList);
}

void ApplyTheme() => ApplyTheme(_formsTextColor, _formsPlaceholderColor);
Expand Down
9 changes: 2 additions & 7 deletions Xamarin.Forms.Material.Android/MaterialPickerRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,8 @@ protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
UpdateBackgroundColor();
}

protected override void UpdateBackgroundColor()
{
if (_textInputLayout == null)
return;

_textInputLayout.BoxBackgroundColor = MaterialColors.CreateEntryFilledInputBackgroundColor(Element.BackgroundColor, Element.TextColor);
}
protected override void UpdateBackgroundColor() =>
_textInputLayout?.ApplyBackgroundColor(Element.BackgroundColor, Element.TextColor);

protected override void UpdatePlaceHolderText()
{
Expand Down
9 changes: 2 additions & 7 deletions Xamarin.Forms.Material.Android/MaterialTimePickerRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,8 @@ protected override void OnElementChanged(ElementChangedEventArgs<TimePicker> e)
UpdateBackgroundColor();
}

protected override void UpdateBackgroundColor()
{
if (_textInputLayout == null)
return;

_textInputLayout.BoxBackgroundColor = MaterialColors.CreateEntryFilledInputBackgroundColor(Element.BackgroundColor, Element.TextColor);
}
protected override void UpdateBackgroundColor() =>
_textInputLayout?.ApplyBackgroundColor(Element.BackgroundColor, Element.TextColor);

protected override void UpdateTextColor() => ApplyTheme();

Expand Down
Loading