summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core/Span.cs
blob: 06c421283e320c17f2ca2672993827e587342103 (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
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;
				var oldValue = _fontAttributes;
				_fontAttributes = value;
				OnPropertyChanged();

				((IFontElement)this).OnFontAttributesChanged(oldValue, value);
			}
		}

		public string FontFamily
		{
			get { return _fontFamily; }
			set
			{
				if (_fontFamily == value)
					return;
				var oldValue = _fontFamily;
				_fontFamily = value;
				OnPropertyChanged();
				((IFontElement)this).OnFontFamilyChanged(oldValue, value);
			}
		}

		[TypeConverter(typeof(FontSizeConverter))]
		public double FontSize
		{
			get { return _fontSize; }
			set
			{
				if (_fontSize == value)
					return;
				var oldValue = _fontSize;
				_fontSize = value;
				OnPropertyChanged();
				((IFontElement)this).OnFontSizeChanged(oldValue, value);
			}
		}

		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 OnSomeFontPropertyChanged()
		{
			{
				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

		//Those 4 methods are never used as Span isn't a BO, and doesn't compose with FontElement
		void IFontElement.OnFontFamilyChanged(string oldValue, string newValue)
		{
			throw new NotImplementedException();
		}

		void IFontElement.OnFontSizeChanged(double oldValue, double newValue)
		{
			throw new NotImplementedException();
		}

		double IFontElement.FontSizeDefaultValueCreator()
		{
			throw new NotImplementedException();
		}

		void IFontElement.OnFontAttributesChanged(FontAttributes oldValue, FontAttributes newValue)
		{
			throw new NotImplementedException();
		}

		void IFontElement.OnFontChanged(Font oldValue, Font newValue)
		{
			throw new NotImplementedException();
		}
	}
}