Skip to content

Commit 5012b73

Browse files
fix(Cupertino): add CupertinoTheme class
1 parent ba10e00 commit 5012b73

File tree

1 file changed

+224
-0
lines changed

1 file changed

+224
-0
lines changed
+224
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
#if WinUI
6+
using Microsoft.UI;
7+
using Microsoft.UI.Xaml;
8+
using Microsoft.UI.Xaml.Controls;
9+
using Microsoft.UI.Xaml.Controls.Primitives;
10+
using Microsoft.UI.Xaml.Media;
11+
#else
12+
using Windows.UI;
13+
using Windows.UI.Xaml;
14+
using Windows.UI.Xaml.Controls;
15+
using Windows.UI.Xaml.Controls.Primitives;
16+
using Windows.UI.Xaml.Data;
17+
using Windows.UI.Xaml.Media;
18+
#endif
19+
20+
namespace Uno.Cupertino
21+
{
22+
public class CupertinoTheme : ResourceDictionary
23+
{
24+
private bool _isColorOverrideMuted;
25+
private bool _isFontOverrideMuted;
26+
27+
#region FontOverrideSource (DP)
28+
/// <summary>
29+
/// (Optional) Gets or sets a Uniform Resource Identifier (<see cref="Uri"/>) that provides the source location
30+
/// of a <see cref="ResourceDictionary"/> containing overrides for the default Uno.Cupertino <see cref="FontFamily"/> resources
31+
/// </summary>
32+
public string FontOverrideSource
33+
{
34+
get => (string)GetValue(FontOverrideSourceProperty);
35+
set => SetValue(FontOverrideSourceProperty, value);
36+
}
37+
38+
public static DependencyProperty FontOverrideSourceProperty { get; } =
39+
DependencyProperty.Register(
40+
nameof(FontOverrideSource),
41+
typeof(string),
42+
typeof(CupertinoTheme),
43+
new PropertyMetadata(null, OnFontOverrideSourceChanged));
44+
45+
private static void OnFontOverrideSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
46+
{
47+
if (d is CupertinoTheme theme && e.NewValue is string sourceUri)
48+
{
49+
theme.FontOverrideDictionary = new ResourceDictionary() { Source = new Uri(sourceUri) };
50+
}
51+
}
52+
#endregion
53+
54+
#region ColorOverrideSource (DP)
55+
/// <summary>
56+
/// (Optional) Gets or sets a Uniform Resource Identifier (<see cref="Uri"/>) that provides the source location
57+
/// of a <see cref="ResourceDictionary"/> containing overrides for the default Uno.Cupertino <see cref="Color"/> resources
58+
/// </summary>
59+
/// <remarks>The overrides set here should be re-defining the <see cref="Color"/> resources used by Uno.Cupertino, not the <see cref="SolidColorBrush"/> resources</remarks>
60+
public string ColorOverrideSource
61+
{
62+
get => (string)GetValue(ColorOverrideSourceProperty);
63+
set => SetValue(ColorOverrideSourceProperty, value);
64+
}
65+
66+
public static DependencyProperty ColorOverrideSourceProperty { get; } =
67+
DependencyProperty.Register(
68+
nameof(ColorOverrideSource),
69+
typeof(string),
70+
typeof(CupertinoTheme),
71+
new PropertyMetadata(null, OnColorOverrideSourceChanged));
72+
73+
private static void OnColorOverrideSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
74+
{
75+
if (d is CupertinoTheme theme && e.NewValue is string sourceUri)
76+
{
77+
theme.ColorOverrideDictionary = new ResourceDictionary() { Source = new Uri(sourceUri) };
78+
}
79+
}
80+
#endregion
81+
82+
#region FontOverrideDictionary (DP)
83+
/// <summary>
84+
/// (Optional) Gets or sets a <see cref="ResourceDictionary"/> containing overrides for the default Uno.Cupertino <see cref="FontFamily"/> resources
85+
/// </summary>
86+
public ResourceDictionary FontOverrideDictionary
87+
{
88+
get => (ResourceDictionary)GetValue(FontOverrideDictionaryProperty);
89+
set => SetValue(FontOverrideDictionaryProperty, value);
90+
}
91+
92+
public static DependencyProperty FontOverrideDictionaryProperty { get; } =
93+
DependencyProperty.Register(
94+
nameof(FontOverrideDictionary),
95+
typeof(ResourceDictionary),
96+
typeof(CupertinoTheme),
97+
new PropertyMetadata(null, OnFontOverrideChanged));
98+
99+
private static void OnFontOverrideChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
100+
{
101+
if (d is CupertinoTheme { _isFontOverrideMuted: false } theme)
102+
{
103+
theme.UpdateSource();
104+
}
105+
}
106+
#endregion
107+
108+
#region ColorOverrideDictionary (DP)
109+
/// <summary>
110+
/// (Optional) Gets or sets a <see cref="ResourceDictionary"/> containing overrides for the default Uno.Cupertino <see cref="Color"/> resources
111+
/// </summary>
112+
/// <remarks>The overrides set here should be re-defining the <see cref="Color"/> resources used by Uno.Cupertino, not the <see cref="SolidColorBrush"/> resources</remarks>
113+
public ResourceDictionary ColorOverrideDictionary
114+
{
115+
get => (ResourceDictionary)GetValue(ColorOverrideDictionaryProperty);
116+
set => SetValue(ColorOverrideDictionaryProperty, value);
117+
}
118+
119+
public static DependencyProperty ColorOverrideDictionaryProperty { get; } =
120+
DependencyProperty.Register(
121+
nameof(ColorOverrideDictionary),
122+
typeof(ResourceDictionary),
123+
typeof(CupertinoTheme),
124+
new PropertyMetadata(null, OnColorOverrideChanged));
125+
126+
private static void OnColorOverrideChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
127+
{
128+
if (d is CupertinoTheme { _isColorOverrideMuted: false } theme)
129+
{
130+
theme.UpdateSource();
131+
}
132+
}
133+
#endregion
134+
135+
public CupertinoTheme() : this(colorOverride: null, fontOverride: null)
136+
{
137+
138+
}
139+
140+
public CupertinoTheme(ResourceDictionary colorOverride = null, ResourceDictionary fontOverride = null)
141+
{
142+
if (colorOverride is { })
143+
{
144+
SetColorOverrideSilently(colorOverride);
145+
}
146+
147+
if (fontOverride is { })
148+
{
149+
SetFontOverrideSilently(fontOverride);
150+
}
151+
152+
UpdateSource();
153+
}
154+
155+
private void SetColorOverrideSilently(ResourceDictionary colorOverride)
156+
{
157+
try
158+
{
159+
_isColorOverrideMuted = true;
160+
ColorOverrideDictionary = colorOverride;
161+
}
162+
finally
163+
{
164+
_isColorOverrideMuted = false;
165+
}
166+
}
167+
168+
private void SetFontOverrideSilently(ResourceDictionary fontOverride)
169+
{
170+
try
171+
{
172+
_isFontOverrideMuted = true;
173+
FontOverrideDictionary = fontOverride;
174+
}
175+
finally
176+
{
177+
_isFontOverrideMuted = false;
178+
}
179+
}
180+
181+
private void UpdateSource()
182+
{
183+
#if !HAS_UNO
184+
Source = null;
185+
#endif
186+
ThemeDictionaries.Clear();
187+
MergedDictionaries.Clear();
188+
this.Clear();
189+
190+
var colors = new ResourceDictionary { Source = new Uri("ms-appx:///Uno.Cupertino/Styles/Application/ColorPalette.xaml") };
191+
192+
colors.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("ms-appx:///Uno.Cupertino/Styles/Application/CupertinoColors.xaml") });
193+
194+
if (ColorOverrideDictionary is { } colorOverride)
195+
{
196+
colors.MergedDictionaries
197+
#if !HAS_UNO
198+
.Add(colorOverride.Duplicate());
199+
#else
200+
.Add(colorOverride);
201+
#endif
202+
}
203+
204+
var mergedPages = new ResourceDictionary { Source = new Uri("ms-appx:///Uno.Cupertino/Generated/mergedpages.xaml") };
205+
206+
var fonts = new ResourceDictionary { Source = new Uri("ms-appx:///Uno.Cupertino/Styles/Application/Fonts.xaml") };
207+
208+
if (FontOverrideDictionary is { } fontOverride)
209+
{
210+
fonts.MergedDictionaries
211+
#if !HAS_UNO
212+
.Add(fontOverride.Duplicate());
213+
#else
214+
.Add(fontOverride);
215+
#endif
216+
}
217+
218+
mergedPages.MergedDictionaries.Add(colors);
219+
mergedPages.MergedDictionaries.Add(fonts);
220+
221+
MergedDictionaries.Add(mergedPages);
222+
}
223+
}
224+
}

0 commit comments

Comments
 (0)