This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
TextColorSwitcher.cs
57 lines (50 loc) · 1.58 KB
/
TextColorSwitcher.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using Android.Content.Res;
using Android.Widget;
namespace Xamarin.Forms.Platform.Android
{
/// <summary>
/// Handles color state management for the TextColor property
/// for Entry, Button, Picker, TimePicker, and DatePicker
/// </summary>
internal class TextColorSwitcher
{
static readonly int[][] s_colorStates = { new[] { global::Android.Resource.Attribute.StateEnabled }, new[] { -global::Android.Resource.Attribute.StateEnabled } };
readonly ColorStateList _defaultTextColors;
readonly bool _useLegacyColorManagement;
Color _currentTextColor;
public TextColorSwitcher(ColorStateList textColors, bool useLegacyColorManagement = true)
{
_defaultTextColors = textColors;
_useLegacyColorManagement = useLegacyColorManagement;
}
public void UpdateTextColor(TextView control, Color color, Action<ColorStateList> setColor = null)
{
if (color == _currentTextColor)
return;
if (setColor == null)
{
setColor = control.SetTextColor;
}
_currentTextColor = color;
if (color.IsDefault)
{
setColor(_defaultTextColors);
}
else
{
if (_useLegacyColorManagement)
{
// Set the new enabled state color, preserving the default disabled state color
int defaultDisabledColor = _defaultTextColors.GetColorForState(s_colorStates[1], color.ToAndroid());
setColor(new ColorStateList(s_colorStates, new[] { color.ToAndroid().ToArgb(), defaultDisabledColor }));
}
else
{
var acolor = color.ToAndroid().ToArgb();
setColor(new ColorStateList(s_colorStates, new[] { acolor, acolor }));
}
}
}
}
}