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

Commit 39f8e60

Browse files
authored
Fix material colors to match previous version better (#13666)
* Bump Google Material * Bump material and set HintText to transparent on multi line controls * Fix material colors to match previous version better * - additional test cases * - fix issue with button background color * - push button to bottom of page * Update VisualGallery.xaml.cs * Update MaterialColors.cs
1 parent 3d054b8 commit 39f8e60

14 files changed

+528
-416
lines changed

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/VisualControlsPage.xaml

Lines changed: 19 additions & 315 deletions
Large diffs are not rendered by default.

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/VisualControlsPage.xaml.cs

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ namespace Xamarin.Forms.Controls.Issues
1313
{
1414
public partial class VisualControlsPage : TestShell
1515
{
16-
bool isVisible = false;
17-
double percentage = 0.0;
18-
1916
public VisualControlsPage()
2017
{
2118
#if APP
@@ -29,47 +26,6 @@ protected override void Init()
2926
BindingContext = this;
3027
}
3128

32-
public double PercentageCounter
33-
{
34-
get { return percentage; }
35-
set
36-
{
37-
percentage = value;
38-
OnPropertyChanged();
39-
OnPropertyChanged(nameof(Counter));
40-
}
41-
}
42-
43-
public double Counter => percentage * 10;
44-
45-
protected override void OnAppearing()
46-
{
47-
isVisible = true;
48-
49-
base.OnAppearing();
50-
51-
Device.StartTimer(TimeSpan.FromSeconds(1), () =>
52-
{
53-
var progress = PercentageCounter + 0.1;
54-
if (progress > 1)
55-
progress = 0;
56-
57-
PercentageCounter = progress;
58-
59-
return isVisible;
60-
});
61-
}
62-
63-
protected override void OnDisappearing()
64-
{
65-
isVisible = false;
66-
67-
base.OnDisappearing();
68-
}
69-
70-
71-
72-
7329
[Preserve(AllMembers = true)]
7430
[Issue(IssueTracker.Github, 4435, "Visual Gallery Loads",
7531
PlatformAffected.iOS | PlatformAffected.Android)]

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/VisualGallery.xaml

Lines changed: 363 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
using Xamarin.Forms;
8+
using Xamarin.Forms.Xaml;
9+
10+
namespace Xamarin.Forms.Controls.Issues
11+
{
12+
public partial class VisualGallery : ContentPage
13+
{
14+
bool isVisible = false;
15+
double percentage = 0.0;
16+
public VisualGallery()
17+
{
18+
#if APP
19+
InitializeComponent();
20+
Device.BeginInvokeOnMainThread(OnAppearing);
21+
pushPage.Clicked += PushPage;
22+
#endif
23+
}
24+
25+
async void PushPage(object sender, EventArgs e)
26+
{
27+
await Navigation.PushAsync(new VisualGallery());
28+
}
29+
30+
public double PercentageCounter
31+
{
32+
get { return percentage; }
33+
set
34+
{
35+
percentage = value;
36+
OnPropertyChanged();
37+
OnPropertyChanged(nameof(Counter));
38+
}
39+
}
40+
41+
public double Counter => percentage * 10;
42+
43+
protected override void OnAppearing()
44+
{
45+
if (!isVisible)
46+
return;
47+
48+
isVisible = true;
49+
50+
base.OnAppearing();
51+
52+
Device.StartTimer(TimeSpan.FromSeconds(1), () =>
53+
{
54+
var progress = PercentageCounter + 0.1;
55+
if (progress > 1)
56+
progress = 0;
57+
58+
PercentageCounter = progress;
59+
60+
return isVisible;
61+
});
62+
}
63+
64+
protected override void OnDisappearing()
65+
{
66+
isVisible = false;
67+
68+
base.OnDisappearing();
69+
}
70+
}
71+
}

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,10 @@
900900
<Compile Include="$(MSBuildThisFileDirectory)Issue10699.cs" />
901901
<Compile Include="$(MSBuildThisFileDirectory)Issue11185.cs" />
902902
<Compile Include="$(MSBuildThisFileDirectory)Issue10307.cs" />
903+
<Compile Include="$(MSBuildThisFileDirectory)VisualGallery.xaml.cs">
904+
<DependentUpon>VisualGallery.xaml</DependentUpon>
905+
<SubType>Code</SubType>
906+
</Compile>
903907
<Compile Include="$(MSBuildThisFileDirectory)_TemplateMarkup.xaml.cs">
904908
<DependentUpon>_TemplateMarkup.xaml</DependentUpon>
905909
<SubType>Code</SubType>
@@ -2654,4 +2658,10 @@
26542658
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
26552659
</EmbeddedResource>
26562660
</ItemGroup>
2661+
<ItemGroup>
2662+
<EmbeddedResource Include="$(MSBuildThisFileDirectory)VisualGallery.xaml">
2663+
<SubType>Designer</SubType>
2664+
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
2665+
</EmbeddedResource>
2666+
</ItemGroup>
26572667
</Project>

Xamarin.Forms.Material.Android/MaterialButtonRenderer.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,27 @@ protected Button Element
104104

105105
public override void Draw(Canvas canvas)
106106
{
107-
_hasDrawnOnce = true;
108107
if (Element.IsEnabled != Enabled)
109108
{
109+
_hasDrawnOnce = true;
110110
Enabled = Element.IsEnabled;
111111
return;
112112
}
113113

114+
if(!_hasDrawnOnce)
115+
{
116+
_hasDrawnOnce = true;
117+
118+
// https://github.com/xamarin/Xamarin.Forms/issues/13416
119+
// This forces a redraw the first time which works around
120+
// an issue with setting background color and having an image set
121+
if (Enabled && Element.ImageSource != null)
122+
{
123+
Enabled = false;
124+
Enabled = true;
125+
}
126+
}
127+
114128
if (Element == null || Element.CornerRadius <= 0)
115129
{
116130
base.Draw(canvas);

Xamarin.Forms.Material.Android/MaterialColors.cs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,6 @@ internal static class MaterialColors
5656
const float kFilledTextFieldIndicatorLineAlpha = 0.42f;
5757
const float kFilledTextFieldIconAlpha = 0.54f;
5858

59-
// the idea of this value is that I want Active to be the exact color the user specified
60-
// and then all the other colors decrease according to the Material theme setup
61-
static float kFilledPlaceHolderOffset = 1f - kFilledTextFieldActiveAlpha;
62-
6359
// State list from material-components-android
6460
// https://github.com/material-components/material-components-android/blob/71694616056012fe1162adb9144be903d1e510d5/lib/java/com/google/android/material/textfield/res/values/colors.xml#L28
6561
public static PlatformColor CreateEntryFilledInputBackgroundColor(Color backgroundColor, Color textColor)
@@ -77,24 +73,29 @@ public static PlatformColor CreateEntryFilledInputBackgroundColor(Color backgrou
7773
return ToPlatformColor(backgroundColor);
7874
}
7975

80-
public static (PlatformColor InlineColor, PlatformColor FloatingColor) GetPlaceHolderColor(Color placeholderColor, Color textColor)
76+
public static (PlatformColor InlineColor, PlatformColor FloatingColor, PlatformColor DefaultColor) GetPlaceHolderColor(Color placeholderColor, Color textColor)
8177
{
8278
PlatformColor inlineColor;
8379
PlatformColor floatingColor;
84-
float inlineAlpha = kFilledTextFieldOnSurfaceAlpha + kFilledPlaceHolderOffset;
80+
PlatformColor defaultColor;
8581

8682
if (placeholderColor == Color.Default)
8783
{
88-
inlineColor = WithAlpha(MaterialColors.Light.OnSurfaceColor, inlineAlpha);
84+
inlineColor = WithAlpha(MaterialColors.Light.OnSurfaceColor, kFilledTextFieldDisabledAlpha);
85+
86+
defaultColor = WithAlpha(MaterialColors.Light.OnSurfaceColor, kFilledTextFieldOnSurfaceAlpha);
87+
8988
floatingColor = MaterialColors.Light.PrimaryColor;
89+
9090
}
9191
else
9292
{
93-
inlineColor = WithAlpha(ToPlatformColor(placeholderColor), inlineAlpha);
93+
inlineColor = ToPlatformColor(placeholderColor);
9494
floatingColor = ToPlatformColor(placeholderColor);
95+
defaultColor = ToPlatformColor(placeholderColor);
9596
}
9697

97-
return (inlineColor, floatingColor);
98+
return (inlineColor, floatingColor, defaultColor);
9899
}
99100

100101
public static (PlatformColor FocusedColor, PlatformColor UnFocusedColor) GetUnderlineColor(Color placeholderColor)
@@ -138,7 +139,7 @@ public static PlatformColor GetCheckBoxColor(bool isChecked, bool isEnabled)
138139
public static readonly int[][] ButtonTextStates =
139140
{
140141
new int[] { global::Android.Resource.Attribute.StateEnabled },
141-
new int[] { ~global::Android.Resource.Attribute.StateEnabled },
142+
new int[] { -global::Android.Resource.Attribute.StateEnabled },
142143
new int[] { }
143144
};
144145

@@ -152,6 +153,9 @@ public static PlatformColor GetCheckBoxColor(bool isChecked, bool isEnabled)
152153
{
153154
new []{ global::Android.Resource.Attribute.StateFocused },
154155
new []{ -global::Android.Resource.Attribute.StateFocused },
156+
new int[] { global::Android.Resource.Attribute.StateEnabled },
157+
new int[] { -global::Android.Resource.Attribute.StateEnabled },
158+
new int[0] { }
155159
};
156160

157161
// State list from material-components-android
@@ -174,7 +178,7 @@ public static ColorStateList CreateEntryFilledPlaceholderColors(PlatformColor in
174178
{
175179
int[][] States =
176180
{
177-
new []{ ~global::Android.Resource.Attribute.StateEnabled },
181+
new []{ -global::Android.Resource.Attribute.StateEnabled },
178182
new int[0] { }
179183
};
180184

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

185189
public static ColorStateList CreateEntryUnderlineColors(PlatformColor focusedColor, PlatformColor unfocusedColor)
186190
{
187-
var colors = new int[] { focusedColor, unfocusedColor };
191+
var colors = new int[] { focusedColor, unfocusedColor, focusedColor, focusedColor, unfocusedColor };
188192
return new ColorStateList(EntryUnderlineStates, colors);
189193
}
190194

Xamarin.Forms.Material.Android/MaterialDatePickerRenderer.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,8 @@ protected override void OnElementChanged(ElementChangedEventArgs<DatePicker> e)
3939
UpdateBackgroundColor();
4040
}
4141

42-
protected override void UpdateBackgroundColor()
43-
{
44-
if (_textInputLayout == null)
45-
return;
46-
47-
_textInputLayout.BoxBackgroundColor = MaterialColors.CreateEntryFilledInputBackgroundColor(Element.BackgroundColor, Element.TextColor);
48-
}
42+
protected override void UpdateBackgroundColor() =>
43+
_textInputLayout?.ApplyBackgroundColor(Element.BackgroundColor, Element.TextColor);
4944

5045
protected override void UpdateTextColor() => ApplyTheme();
5146
void ApplyTheme() => _textInputLayout?.ApplyTheme(Element.TextColor, Color.Default);

Xamarin.Forms.Material.Android/MaterialEditorRenderer.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,8 @@ protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
3939
UpdateBackgroundColor();
4040
}
4141

42-
protected override void UpdateBackgroundColor()
43-
{
44-
if (_disposed || _textInputLayout == null)
45-
return;
46-
47-
_textInputLayout.BoxBackgroundColor = MaterialColors.CreateEntryFilledInputBackgroundColor(Element.BackgroundColor, Element.TextColor);
48-
}
42+
protected override void UpdateBackgroundColor() =>
43+
_textInputLayout?.ApplyBackgroundColor(Element.BackgroundColor, Element.TextColor);
4944

5045
protected override void UpdatePlaceholderText()
5146
{

Xamarin.Forms.Material.Android/MaterialEntryRenderer.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,8 @@ protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
3939

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

42-
protected override void UpdateBackgroundColor()
43-
{
44-
if (_textInputLayout == null)
45-
return;
46-
47-
_textInputLayout.BoxBackgroundColor = MaterialColors.CreateEntryFilledInputBackgroundColor(Element.BackgroundColor, Element.TextColor);
48-
}
42+
protected override void UpdateBackgroundColor() =>
43+
_textInputLayout?.ApplyBackgroundColor(Element.BackgroundColor, Element.TextColor);
4944

5045
protected override void UpdatePlaceHolderText() => _textInputLayout.SetHint(Element.Placeholder, Element);
5146
protected override EditText EditText => _textInputEditText;

0 commit comments

Comments
 (0)