summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core/Span.cs
blob: 6ffc3f171e6d2399a87c3ba65b96ce8bb3e0ca0c (plain)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace Xamarin.Forms
{
	[ContentProperty("Text")]
	public sealed class Span : INotifyPropertyChanged, IFontElement
	{
		Color _backgroundColor;

		Font _font;
		FontAttributes _fontAttributes;
		string _fontFamily;
		double _fontSize;

		Color _foregroundColor;
		bool _inUpdate; // if we ever make this thread safe we need to move to a mutex

		string _text;

		public Span()
		{
			_fontFamily = null;
			_fontAttributes = FontAttributes.None;
			_fontSize = Device.GetNamedSize(NamedSize.Default, typeof(Label), true);
			_font = Font.SystemFontOfSize(_fontSize);
		}

		public Color BackgroundColor
		{
			get { return _backgroundColor; }
			set
			{
				if (_backgroundColor == value)
					return;
				_backgroundColor = value;
				OnPropertyChanged();
			}
		}

		[Obsolete("Please use the Font properties directly. Obsolete in 1.3.0")]
		public Font Font
		{
			get { return _font; }
			set
			{
				if (_font == value)
					return;
				_font = value;
				OnPropertyChanged();
				UpdateFontPropertiesFromStruct();
			}
		}

		public Color ForegroundColor
		{
			get { return _foregroundColor; }
			set
			{
				if (_foregroundColor == value)
					return;
				_foregroundColor = value;
				OnPropertyChanged();
			}
		}

		public string Text
		{
			get { return _text; }
			set
			{
				if (_text == value)
					return;
				_text = value;
				OnPropertyChanged();
			}
		}

		public FontAttributes FontAttributes
		{
			get { return _fontAttributes; }
			set
			{
				if (_fontAttributes == value)
					return;
				_fontAttributes = value;
				OnPropertyChanged();
				UpdateStructFromFontProperties();
			}
		}

		public string FontFamily
		{
			get { return _fontFamily; }
			set
			{
				if (_fontFamily == value)
					return;
				_fontFamily = value;
				OnPropertyChanged();
				UpdateStructFromFontProperties();
			}
		}

		[TypeConverter(typeof(FontSizeConverter))]
		public double FontSize
		{
			get { return _fontSize; }
			set
			{
				if (_fontSize == value)
					return;
				_fontSize = value;
				OnPropertyChanged();
				UpdateStructFromFontProperties();
			}
		}

		public event PropertyChangedEventHandler PropertyChanged;

		void OnPropertyChanged([CallerMemberName] string propertyName = null)
		{
			PropertyChangedEventHandler handler = PropertyChanged;
			if (handler != null)
				handler(this, new PropertyChangedEventArgs(propertyName));
		}

#pragma warning disable 0618 // retain until Span.Font removed
		void UpdateFontPropertiesFromStruct()
		{
			if (_inUpdate)
				return;
			_inUpdate = true;

			if (Font == Font.Default)
			{
				FontSize = Device.GetNamedSize(NamedSize.Default, typeof(Label), true);
				FontFamily = null;
				FontAttributes = FontAttributes.None;
			}
			else
			{
				FontSize = Font.UseNamedSize ? Device.GetNamedSize(Font.NamedSize, typeof(Label), true) : Font.FontSize;
				FontFamily = Font.FontFamily;
				FontAttributes = Font.FontAttributes;
			}

			_inUpdate = false;
		}

		void UpdateStructFromFontProperties()
		{
			if (_inUpdate)
				return;
			_inUpdate = true;

			if (FontFamily != null)
			{
				Font = Font.OfSize(FontFamily, FontSize).WithAttributes(FontAttributes);
			}
			else
			{
				Font = Font.SystemFontOfSize(FontSize).WithAttributes(FontAttributes);
			}

			_inUpdate = false;
		}
	}
#pragma warning restore
}