summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.WinRT.Phone/WindowsPhoneResourcesProvider.cs
blob: 3ef9b90aa30571e410d0cfa5dcd97c62d48da7b6 (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
´╗┐using Windows.UI.Text;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Xamarin.Forms.Internals;
using WStyle = Windows.UI.Xaml.Style;

namespace Xamarin.Forms.Platform.WinRT
{
	internal sealed class WindowsPhoneResourcesProvider
		: ISystemResourcesProvider
	{
		public IResourceDictionary GetSystemResources()
		{
			var prototype = new TextBlock();
			
			return new ResourceDictionary
			{
				[Device.Styles.TitleStyleKey] = GetStyle("HeaderTextBlockStyle", prototype),
				[Device.Styles.SubtitleStyleKey] = GetStyle("SubheaderTextBlockStyle", prototype),
				[Device.Styles.BodyStyleKey] = GetStyle("BodyTextBlockStyle", prototype),
				[Device.Styles.CaptionStyleKey] = GetStyle("BodyTextBlockStyle", prototype),
				[Device.Styles.ListItemTextStyleKey] = GetStyle("ListViewItemTextBlockStyle", prototype),
				[Device.Styles.ListItemDetailTextStyleKey] = GetStyle("ListViewItemContentTextBlockStyle", prototype)
			};
		}

		static Style GetStyle(object nativeKey, TextBlock prototype)
		{
			var style = (WStyle)Windows.UI.Xaml.Application.Current.Resources[nativeKey];

			prototype.Style = style;

			var formsStyle = new Style(typeof(Label));

			formsStyle.Setters.Add(Label.FontSizeProperty, prototype.FontSize);
			formsStyle.Setters.Add(Label.FontFamilyProperty, prototype.FontFamily.Source);
			formsStyle.Setters.Add(Label.FontAttributesProperty, ToAttributes(prototype.FontWeight));
			formsStyle.Setters.Add(Label.LineBreakModeProperty, ToLineBreakMode(prototype.TextWrapping));

			return formsStyle;
		}

		static FontAttributes ToAttributes(FontWeight fontWeight)
		{
			if (fontWeight.Weight == FontWeights.Bold.Weight || fontWeight.Weight == FontWeights.SemiBold.Weight 
				|| fontWeight.Weight == FontWeights.ExtraBold.Weight)
			{
				return FontAttributes.Bold;
			}

			return FontAttributes.None;
		}

		static LineBreakMode ToLineBreakMode(TextWrapping value)
		{
			switch (value)
			{
				case TextWrapping.Wrap:
					return LineBreakMode.CharacterWrap;
				case TextWrapping.WrapWholeWords:
					return LineBreakMode.WordWrap;
				default:
				case TextWrapping.NoWrap:
					return LineBreakMode.NoWrap;
			}
		}
	}
}