summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.WinRT.Phone/WindowsPhoneResourcesProvider.cs
blob: adbe7dea2c8b3c5d770cf64ca8da60721f438b00 (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
using System;
using System.Collections.Generic;
using System.Linq;
using Windows.UI.Text;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace Xamarin.Forms.Platform.WinRT
{
	internal sealed class WindowsPhoneResourcesProvider
		: ISystemResourcesProvider
	{
		public IResourceDictionary GetSystemResources()
		{
			var windowsResources = Windows.UI.Xaml.Application.Current.Resources;

			var resources = new ResourceDictionary ();
			resources[Device.Styles.TitleStyleKey] = GetStyle ("HeaderTextBlockStyle");
			resources[Device.Styles.SubtitleStyleKey] = GetStyle ("SubheaderTextBlockStyle");
			resources[Device.Styles.BodyStyleKey] = GetStyle ("BodyTextBlockStyle");
			resources[Device.Styles.CaptionStyleKey] = GetStyle ("BodyTextBlockStyle");
			resources[Device.Styles.ListItemTextStyleKey] = GetStyle ("ListViewItemTextBlockStyle");
			resources[Device.Styles.ListItemDetailTextStyleKey] = GetStyle ("ListViewItemContentTextBlockStyle");
			return resources;
		}

		Style GetStyle (object nativeKey)
		{
			var style = (Windows.UI.Xaml.Style) Windows.UI.Xaml.Application.Current.Resources[nativeKey];
			style = GetAncestorSetters(style);

			var formsStyle = new Style (typeof (Label));
			foreach (var b in style.Setters) {
				var setter = b as Windows.UI.Xaml.Setter;
				if (setter == null)
					continue;

				// TODO: Need to implement a stealth pass-through for things we don't support

				if (setter.Property == TextBlock.FontSizeProperty)
					formsStyle.Setters.Add (Label.FontSizeProperty, setter.Value);
				else if (setter.Property == TextBlock.FontFamilyProperty)
					formsStyle.Setters.Add (Label.FontFamilyProperty, setter.Value);
				else if (setter.Property == TextBlock.FontWeightProperty)
					formsStyle.Setters.Add (Label.FontAttributesProperty, ToAttributes (Convert.ToUInt16 (setter.Value)));
				else if (setter.Property == TextBlock.TextWrappingProperty)
					formsStyle.Setters.Add (Label.LineBreakModeProperty, ToLineBreakMode ((TextWrapping) setter.Value));
			}

			return formsStyle;
		}

		static Windows.UI.Xaml.Style GetAncestorSetters (Windows.UI.Xaml.Style value)
		{
			var style = new Windows.UI.Xaml.Style (value.TargetType)
			{
				BasedOn = value.BasedOn
			};
			foreach (var valSetter in value.Setters)
			{
				style.Setters.Add(valSetter);
			}

			var ancestorStyles = new List<Windows.UI.Xaml.Style> ();

			Windows.UI.Xaml.Style currStyle = style;
			while (currStyle.BasedOn != null)
			{
				ancestorStyles.Add(currStyle.BasedOn);
				currStyle = currStyle.BasedOn;
			}

			foreach (var styleParent in ancestorStyles)
			{
				foreach (var b in styleParent.Setters)
				{
					var parentSetter = b as Windows.UI.Xaml.Setter;
					if (parentSetter == null)
						continue;

					var derviedSetter = style.Setters
						.OfType<Windows.UI.Xaml.Setter> ()
						.FirstOrDefault (x => x.Property == parentSetter.Property);

					//Ignore any ancestor setters which a child setter has already overridden
					if (derviedSetter != null)
						continue;
					else
						style.Setters.Add (parentSetter);
				}
			}
			return style;
		}

		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;
			}
		}

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

			return FontAttributes.None;
		}
	}
}