-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathAnimatedGlyphToggleButton.cs
263 lines (210 loc) · 8.01 KB
/
AnimatedGlyphToggleButton.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
//
// Copyright Fela Ameghino 2015-2025
//
// Distributed under the GNU General Public License v3.0. (See accompanying
// file LICENSE or copy at https://www.gnu.org/licenses/gpl-3.0.txt)
//
using System.Numerics;
using Telegram.Common;
using Windows.UI.Composition;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Automation.Peers;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Hosting;
namespace Telegram.Controls
{
public partial class AnimatedGlyphToggleButton : ToggleButton
{
private FrameworkElement _label1;
private FrameworkElement _label2;
private Visual _visual1;
private Visual _visual2;
private FrameworkElement _label;
private Visual _visual;
protected bool _animateOnToggle = true;
public AnimatedGlyphToggleButton()
{
DefaultStyleKey = typeof(AnimatedGlyphToggleButton);
Checked += OnToggle;
Unchecked += OnToggle;
}
protected virtual bool IsRuntimeCompatible()
{
return false;
}
protected override void OnApplyTemplate()
{
if (IsRuntimeCompatible())
{
return;
}
_label1 = _label = GetTemplateChild("ContentPresenter1") as FrameworkElement;
_label2 = GetTemplateChild("ContentPresenter2") as FrameworkElement;
if (_label1 != null && _label2 != null)
{
_visual1 = _visual = ElementComposition.GetElementVisual(_label1);
_visual2 = ElementComposition.GetElementVisual(_label2);
if (_label2 is TextBlock text2)
{
text2.Text = string.Empty;
}
else if (_label2 is ContentPresenter presenter2)
{
presenter2.Content = new object();
}
_visual2.Opacity = 0;
_visual2.Scale = new Vector3();
_visual2.CenterPoint = new Vector3(10);
if (_label1 is TextBlock text1)
{
text1.Text = (IsChecked == true ? CheckedGlyph : Glyph) ?? string.Empty;
}
else if (_label1 is ContentPresenter presenter1)
{
presenter1.Content = (IsChecked == true ? CheckedContent : Content) ?? new object();
}
_visual1.Opacity = 1;
_visual1.Scale = new Vector3(1);
_visual1.CenterPoint = new Vector3(10);
}
base.OnApplyTemplate();
}
private void OnToggle(object sender, RoutedEventArgs e)
{
if (_animateOnToggle is false)
{
return;
}
if (_label is TextBlock)
{
OnGlyphChanged(IsChecked == true ? CheckedGlyph : Glyph);
}
else
{
OnGlyphChanged(IsChecked == true ? CheckedContent : Content);
}
}
protected async void OnGlyphChanged(object newValue)
{
if (_visual == null || _label == null)
{
return;
}
var visualShow = _visual == _visual1 ? _visual2 : _visual1;
var visualHide = _visual == _visual1 ? _visual1 : _visual2;
var labelShow = _visual == _visual1 ? _label2 : _label1;
var labelHide = _visual == _visual1 ? _label1 : _label2;
if (labelShow is TextBlock textShow && newValue is string glyph)
{
textShow.Text = glyph;
}
else if (labelShow is ContentPresenter presenterShow)
{
presenterShow.Content = newValue;
}
await this.UpdateLayoutAsync();
_visual1.CenterPoint = new Vector3(_label1.ActualSize / 2f, 0);
_visual2.CenterPoint = new Vector3(_label2.ActualSize / 2f, 0);
var hide1 = _visual.Compositor.CreateVector3KeyFrameAnimation();
hide1.InsertKeyFrame(0, new Vector3(1));
hide1.InsertKeyFrame(1, new Vector3(0));
var hide2 = _visual.Compositor.CreateScalarKeyFrameAnimation();
hide2.InsertKeyFrame(0, 1);
hide2.InsertKeyFrame(1, 0);
visualHide.StartAnimation("Scale", hide1);
visualHide.StartAnimation("Opacity", hide2);
var show1 = _visual.Compositor.CreateVector3KeyFrameAnimation();
show1.InsertKeyFrame(1, new Vector3(1));
show1.InsertKeyFrame(0, new Vector3(0));
var show2 = _visual.Compositor.CreateScalarKeyFrameAnimation();
show2.InsertKeyFrame(1, 1);
show2.InsertKeyFrame(0, 0);
visualShow.StartAnimation("Scale", show1);
visualShow.StartAnimation("Opacity", show2);
_visual = visualShow;
_label = labelShow;
}
#region CheckedContent
public object CheckedContent
{
get => GetValue(CheckedContentProperty);
set => SetValue(CheckedContentProperty, value);
}
public static readonly DependencyProperty CheckedContentProperty =
DependencyProperty.Register("CheckedContent", typeof(object), typeof(AnimatedGlyphToggleButton), new PropertyMetadata(null));
#endregion
#region CheckedGlyph
public string CheckedGlyph
{
get => (string)GetValue(CheckedGlyphProperty);
set => SetValue(CheckedGlyphProperty, value);
}
public static readonly DependencyProperty CheckedGlyphProperty =
DependencyProperty.Register("CheckedGlyph", typeof(string), typeof(AnimatedGlyphToggleButton), new PropertyMetadata(null));
#endregion
#region Glyph
public string Glyph
{
get => (string)GetValue(GlyphProperty);
set => SetValue(GlyphProperty, value);
}
public static readonly DependencyProperty GlyphProperty =
DependencyProperty.Register("Glyph", typeof(string), typeof(AnimatedGlyphToggleButton), new PropertyMetadata(null));
#endregion
#region IsOneWay
public bool IsOneWay
{
get => (bool)GetValue(IsOneWayProperty);
set => SetValue(IsOneWayProperty, value);
}
public static readonly DependencyProperty IsOneWayProperty =
DependencyProperty.Register("IsOneWay", typeof(bool), typeof(AnimatedGlyphToggleButton), new PropertyMetadata(true));
#endregion
protected override void OnToggle()
{
if (IsOneWay)
{
var binding = GetBindingExpression(IsCheckedProperty);
if (binding != null && binding.ParentBinding.Mode == BindingMode.TwoWay)
{
base.OnToggle();
}
}
else
{
base.OnToggle();
}
}
}
public partial class AnimatedGlyphToggleButtonAutomationPeer : ToggleButtonAutomationPeer
{
private readonly AnimatedGlyphToggleButton _owner;
public AnimatedGlyphToggleButtonAutomationPeer(AnimatedGlyphToggleButton owner)
: base(owner)
{
_owner = owner;
}
protected override object GetPatternCore(PatternInterface patternInterface)
{
if (patternInterface == PatternInterface.Toggle)
{
return null;
}
return base.GetPatternCore(patternInterface);
}
protected override string GetNameCore()
{
if (_owner.IsChecked == true && _owner.CheckedContent is string checkedContent)
{
return checkedContent;
}
else if (_owner.IsChecked == false && _owner.Content is string content)
{
return content;
}
return base.GetNameCore();
}
}
}