-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Style.cs
200 lines (165 loc) · 6.48 KB
/
Style.cs
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
#nullable disable
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using Microsoft.Maui.Controls.Internals;
namespace Microsoft.Maui.Controls
{
/// <include file="../../docs/Microsoft.Maui.Controls/Style.xml" path="Type[@FullName='Microsoft.Maui.Controls.Style']/Docs/*" />
[ContentProperty(nameof(Setters))]
public sealed class Style : IStyle
{
internal const string StyleClassPrefix = "Microsoft.Maui.Controls.StyleClass.";
readonly BindableProperty _basedOnResourceProperty = BindableProperty.CreateAttached("BasedOnResource", typeof(Style), typeof(Style), default(Style),
propertyChanged: OnBasedOnResourceChanged);
readonly ConditionalWeakTable<BindableObject, object> _targets = new();
Style _basedOnStyle;
string _baseResourceKey;
IList<Behavior> _behaviors;
IList<TriggerBase> _triggers;
/// <include file="../../docs/Microsoft.Maui.Controls/Style.xml" path="//Member[@MemberName='.ctor']/Docs/*" />
public Style([System.ComponentModel.TypeConverter(typeof(TypeTypeConverter))][Parameter("TargetType")] Type targetType)
{
TargetType = targetType ?? throw new ArgumentNullException(nameof(targetType));
Setters = new List<Setter>();
}
/// <include file="../../docs/Microsoft.Maui.Controls/Style.xml" path="//Member[@MemberName='ApplyToDerivedTypes']/Docs/*" />
public bool ApplyToDerivedTypes { get; set; }
/// <include file="../../docs/Microsoft.Maui.Controls/Style.xml" path="//Member[@MemberName='BasedOn']/Docs/*" />
public Style BasedOn
{
get { return _basedOnStyle; }
set
{
if (_basedOnStyle == value)
return;
if (!ValidateBasedOn(value))
throw new ArgumentException("BasedOn.TargetType is not compatible with TargetType");
Style oldValue = _basedOnStyle;
_basedOnStyle = value;
BasedOnChanged(oldValue, value);
if (value != null)
BaseResourceKey = null;
}
}
/// <include file="../../docs/Microsoft.Maui.Controls/Style.xml" path="//Member[@MemberName='BaseResourceKey']/Docs/*" />
public string BaseResourceKey
{
get { return _baseResourceKey; }
set
{
if (_baseResourceKey == value)
return;
_baseResourceKey = value;
//update all DynamicResources
foreach (var target in (IEnumerable<KeyValuePair<BindableObject, object>>)(object)_targets)
{
target.Key.RemoveDynamicResource(_basedOnResourceProperty);
if (value != null)
target.Key.SetDynamicResource(_basedOnResourceProperty, value);
}
if (value != null)
BasedOn = null;
}
}
/// <include file="../../docs/Microsoft.Maui.Controls/Style.xml" path="//Member[@MemberName='Behaviors']/Docs/*" />
public IList<Behavior> Behaviors => _behaviors ??= new AttachedCollection<Behavior>();
/// <include file="../../docs/Microsoft.Maui.Controls/Style.xml" path="//Member[@MemberName='CanCascade']/Docs/*" />
public bool CanCascade { get; set; }
/// <include file="../../docs/Microsoft.Maui.Controls/Style.xml" path="//Member[@MemberName='Class']/Docs/*" />
public string Class { get; set; }
/// <include file="../../docs/Microsoft.Maui.Controls/Style.xml" path="//Member[@MemberName='Setters']/Docs/*" />
public IList<Setter> Setters { get; }
/// <include file="../../docs/Microsoft.Maui.Controls/Style.xml" path="//Member[@MemberName='Triggers']/Docs/*" />
public IList<TriggerBase> Triggers => _triggers ??= new AttachedCollection<TriggerBase>();
void IStyle.Apply(BindableObject bindable, SetterSpecificity specificity)
{
lock (_targets)
{
#if NETSTANDARD2_0
_targets.Remove(bindable);
_targets.Add(bindable, specificity);
#else
_targets.AddOrUpdate(bindable, specificity);
#endif
}
if (BaseResourceKey != null)
bindable.SetDynamicResource(_basedOnResourceProperty, BaseResourceKey);
ApplyCore(bindable, BasedOn ?? GetBasedOnResource(bindable), specificity);
}
/// <include file="../../docs/Microsoft.Maui.Controls/Style.xml" path="//Member[@MemberName='TargetType']/Docs/*" />
public Type TargetType { get; }
void IStyle.UnApply(BindableObject bindable)
{
UnApplyCore(bindable, BasedOn ?? GetBasedOnResource(bindable));
bindable.RemoveDynamicResource(_basedOnResourceProperty);
lock (_targets)
{
_targets.Remove(bindable);
}
}
internal bool CanBeAppliedTo(Type targetType)
{
if (TargetType == targetType)
return true;
if (!ApplyToDerivedTypes)
return false;
do
{
targetType = targetType.BaseType;
if (TargetType == targetType)
return true;
} while (targetType != typeof(Element));
return false;
}
void BasedOnChanged(Style oldValue, Style newValue)
{
foreach (var target in (IEnumerable<KeyValuePair<BindableObject, object>>)(object)_targets)
{
UnApplyCore(target.Key, oldValue);
ApplyCore(target.Key, newValue, (SetterSpecificity)target.Value);
}
}
Style GetBasedOnResource(BindableObject bindable) => (Style)bindable.GetValue(_basedOnResourceProperty);
static void OnBasedOnResourceChanged(BindableObject bindable, object oldValue, object newValue)
{
Style style = (bindable as IStyleElement).Style;
if (style == null)
return;
if (!style._targets.TryGetValue(bindable, out var objectspecificity))
return;
style.UnApplyCore(bindable, (Style)oldValue);
style.ApplyCore(bindable, (Style)newValue, (SetterSpecificity)objectspecificity);
}
ConditionalWeakTable<BindableObject, object> specificities = new();
void ApplyCore(BindableObject bindable, Style basedOn, SetterSpecificity specificity)
{
if (basedOn != null)
((IStyle)basedOn).Apply(bindable, specificity.AsBaseStyle());
#if NETSTANDARD2_0
specificities.Remove(bindable);
specificities.Add(bindable, specificity);
#else
specificities.AddOrUpdate(bindable, specificity);
#endif
foreach (Setter setter in Setters)
setter.Apply(bindable, specificity);
((AttachedCollection<Behavior>)Behaviors).AttachTo(bindable);
((AttachedCollection<TriggerBase>)Triggers).AttachTo(bindable);
}
void UnApplyCore(BindableObject bindable, Style basedOn)
{
((AttachedCollection<TriggerBase>)Triggers).DetachFrom(bindable);
((AttachedCollection<Behavior>)Behaviors).DetachFrom(bindable);
if (!specificities.TryGetValue(bindable, out var specificity))
return;
foreach (Setter setter in Setters)
setter.UnApply(bindable, (SetterSpecificity)specificity);
if (basedOn != null)
((IStyle)basedOn).UnApply(bindable);
}
bool ValidateBasedOn(Style value)
=> value is null || value.TargetType.IsAssignableFrom(TargetType);
}
}