|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 | // See the LICENSE file in the project root for more information. |
4 | 4 |
|
| 5 | +using CommunityToolkit.WinUI; |
| 6 | + |
5 | 7 | namespace CommunityToolkit.Labs.WinUI; |
6 | 8 |
|
7 | 9 | public partial class Shimmer : Control |
8 | 10 | { |
9 | | - /// <summary> |
10 | | - /// Identifies the <see cref="Duration"/> dependency property. |
11 | | - /// </summary> |
12 | | - public static readonly DependencyProperty DurationProperty = DependencyProperty.Register( |
13 | | - nameof(Duration), |
14 | | - typeof(object), |
15 | | - typeof(Shimmer), |
16 | | - new PropertyMetadata(defaultValue: TimeSpan.FromMilliseconds(1600), PropertyChanged)); |
| 11 | + static TimeSpan _defaultValueForDuration = TimeSpan.FromMilliseconds(1600); |
17 | 12 |
|
18 | 13 | /// <summary> |
19 | | - /// Identifies the <see cref="IsActive"/> dependency property. |
| 14 | + /// Gets or sets the animation duration |
20 | 15 | /// </summary> |
21 | | - public static readonly DependencyProperty IsActiveProperty = DependencyProperty.Register( |
22 | | - nameof(IsActive), |
23 | | - typeof(bool), |
24 | | - typeof(Shimmer), |
25 | | - new PropertyMetadata(defaultValue: true, PropertyChanged)); |
26 | | - |
| 16 | + [GeneratedDependencyProperty(DefaultValueCallback = nameof(DefaultValueForDurationCallback))] |
| 17 | + public partial TimeSpan Duration { get; set; } |
27 | 18 |
|
28 | 19 | /// <summary> |
29 | | - /// Gets or sets the animation duration |
| 20 | + /// Gets or sets if the animation is playing |
30 | 21 | /// </summary> |
31 | | - public TimeSpan Duration |
| 22 | + [GeneratedDependencyProperty(DefaultValue = true)] |
| 23 | + public partial bool IsActive { get; set; } |
| 24 | + |
| 25 | + private static TimeSpan DefaultValueForDurationCallback() |
32 | 26 | { |
33 | | - get => (TimeSpan)GetValue(DurationProperty); |
34 | | - set => SetValue(DurationProperty, value); |
| 27 | + return _defaultValueForDuration; |
35 | 28 | } |
36 | 29 |
|
37 | | - /// <summary> |
38 | | - /// Gets or sets if the animation is playing |
39 | | - /// </summary> |
40 | | - public bool IsActive |
| 30 | + partial void OnDurationChanged(TimeSpan newValue) |
41 | 31 | { |
42 | | - get => (bool)GetValue(IsActiveProperty); |
43 | | - set => SetValue(IsActiveProperty, value); |
| 32 | + if (IsActive) TryStartAnimation(); |
| 33 | + StopAnimation(); |
44 | 34 | } |
45 | 35 |
|
46 | | - private static void PropertyChanged(DependencyObject s, DependencyPropertyChangedEventArgs e) |
| 36 | + partial void OnIsActiveChanged(bool newValue) |
47 | 37 | { |
48 | | - var self = (Shimmer)s; |
49 | | - if (self.IsActive) |
50 | | - { |
51 | | - self.StopAnimation(); |
52 | | - self.TryStartAnimation(); |
53 | | - } |
54 | | - else |
55 | | - { |
56 | | - self.StopAnimation(); |
57 | | - } |
| 38 | + if (IsActive) TryStartAnimation(); |
| 39 | + StopAnimation(); |
58 | 40 | } |
59 | 41 | } |
0 commit comments