summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core/Span.cs
blob: 724126be9328281394485a7d4f216d11a172b90c (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
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Xamarin.Forms.Internals;

namespace Xamarin.Forms
{
	[ContentProperty("Text")]
	public sealed class Span : INotifyPropertyChanged, IFontElement
	{
		class BindableSpan : BindableObject, IFontElement
		{
			Span _span;
			public BindableSpan(Span span)
			{
				_span = span;
			}

			public Font Font {
				get { return (Font)GetValue(FontElement.FontProperty); }
				set { SetValue(FontElement.FontProperty, value); }
			}

			public FontAttributes FontAttributes {
				get { return (FontAttributes)GetValue(FontElement.FontAttributesProperty); }
				set { SetValue(FontElement.FontAttributesProperty, value); }
			}

			public string FontFamily {
				get { return (string)GetValue(FontElement.FontFamilyProperty); }
				set { SetValue(FontElement.FontFamilyProperty, value); }
			}

			[TypeConverter(typeof(FontSizeConverter))]
			public double FontSize {
				get { return (double)GetValue(FontElement.FontSizeProperty); }
				set { SetValue(FontElement.FontSizeProperty, value); }
			}

			double IFontElement.FontSizeDefaultValueCreator() =>
				((IFontElement)_span).FontSizeDefaultValueCreator();

			public void OnFontAttributesChanged(FontAttributes oldValue, FontAttributes newValue) =>
				((IFontElement)_span).OnFontAttributesChanged(oldValue, newValue);

			public void OnFontChanged(Font oldValue, Font newValue) =>
				((IFontElement)_span).OnFontChanged(oldValue, newValue);

			public void OnFontFamilyChanged(string oldValue, string newValue) =>
				((IFontElement)_span).OnFontFamilyChanged(oldValue, newValue);

			public void OnFontSizeChanged(double oldValue, double newValue) =>
				((IFontElement)_span).OnFontSizeChanged(oldValue, newValue);

			protected override void OnPropertyChanged(string propertyName = null)
			{
				base.OnPropertyChanged(propertyName);
				_span.OnPropertyChanged(propertyName);
			}
		}

		Color _backgroundColor;

		BindableObject _fontElement;

		Color _foregroundColor;

		string _text;

		public Span()
		{
			_fontElement = new BindableSpan(this);
		}

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

		[Obsolete("Font is obsolete as of version 1.3.0. Please use the Font properties directly.")]
		public Font Font {
			get { return (Font)_fontElement.GetValue(FontElement.FontProperty); }
			set { _fontElement.SetValue(FontElement.FontProperty, value); }
		}

		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)_fontElement.GetValue(FontElement.FontAttributesProperty); }
			set { _fontElement.SetValue(FontElement.FontAttributesProperty, value); }
		}

		public string FontFamily
		{
			get { return (string)_fontElement.GetValue(FontElement.FontFamilyProperty); }
			set { _fontElement.SetValue(FontElement.FontFamilyProperty, value); }
		}

		[TypeConverter(typeof(FontSizeConverter))]
		public double FontSize
		{
			get { return (double)_fontElement.GetValue(FontElement.FontSizeProperty); }
			set { _fontElement.SetValue(FontElement.FontSizeProperty, value); }
		}

		public event PropertyChangedEventHandler PropertyChanged;

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

		void IFontElement.OnFontFamilyChanged(string oldValue, string newValue)
		{
		}

		void IFontElement.OnFontSizeChanged(double oldValue, double newValue)
		{
		}

		double IFontElement.FontSizeDefaultValueCreator() =>
			Device.GetNamedSize(NamedSize.Default, new Label());

		void IFontElement.OnFontAttributesChanged(FontAttributes oldValue, FontAttributes newValue)
		{
		}

		void IFontElement.OnFontChanged(Font oldValue, Font newValue)
		{
		}
	}
}