|
| 1 | +using Microsoft.UI.Input; |
| 2 | +using Microsoft.UI.Xaml.Input; |
| 3 | + |
| 4 | +namespace DevWinUI; |
| 5 | + |
| 6 | +public partial class Spoiler : ContentControl |
| 7 | +{ |
| 8 | + public event EventHandler<SpoilerRevealChangedEventArgs> RevealChanged; |
| 9 | + public string Header |
| 10 | + { |
| 11 | + get { return (string)GetValue(HeaderProperty); } |
| 12 | + set { SetValue(HeaderProperty, value); } |
| 13 | + } |
| 14 | + |
| 15 | + public static readonly DependencyProperty HeaderProperty = |
| 16 | + DependencyProperty.Register(nameof(Header), typeof(string), typeof(Spoiler), new PropertyMetadata(default(string))); |
| 17 | + |
| 18 | + public IconSource HeaderIcon |
| 19 | + { |
| 20 | + get { return (IconSource)GetValue(HeaderIconProperty); } |
| 21 | + set { SetValue(HeaderIconProperty, value); } |
| 22 | + } |
| 23 | + |
| 24 | + public static readonly DependencyProperty HeaderIconProperty = |
| 25 | + DependencyProperty.Register(nameof(HeaderIcon), typeof(IconSource), typeof(Spoiler), new PropertyMetadata(default(IconSource))); |
| 26 | + |
| 27 | + public VerticalAlignment HeaderVerticalAlignment |
| 28 | + { |
| 29 | + get { return (VerticalAlignment)GetValue(HeaderVerticalAlignmentProperty); } |
| 30 | + set { SetValue(HeaderVerticalAlignmentProperty, value); } |
| 31 | + } |
| 32 | + |
| 33 | + public static readonly DependencyProperty HeaderVerticalAlignmentProperty = |
| 34 | + DependencyProperty.Register(nameof(HeaderVerticalAlignment), typeof(VerticalAlignment), typeof(Spoiler), new PropertyMetadata(default(VerticalAlignment))); |
| 35 | + |
| 36 | + public HorizontalAlignment HeaderHorizontalAlignment |
| 37 | + { |
| 38 | + get { return (HorizontalAlignment)GetValue(HeaderHorizontalAlignmentProperty); } |
| 39 | + set { SetValue(HeaderHorizontalAlignmentProperty, value); } |
| 40 | + } |
| 41 | + |
| 42 | + public static readonly DependencyProperty HeaderHorizontalAlignmentProperty = |
| 43 | + DependencyProperty.Register(nameof(HeaderHorizontalAlignment), typeof(HorizontalAlignment), typeof(Spoiler), new PropertyMetadata(default(HorizontalAlignment))); |
| 44 | + |
| 45 | + public Thickness HeaderMargin |
| 46 | + { |
| 47 | + get { return (Thickness)GetValue(HeaderMarginProperty); } |
| 48 | + set { SetValue(HeaderMarginProperty, value); } |
| 49 | + } |
| 50 | + |
| 51 | + public static readonly DependencyProperty HeaderMarginProperty = |
| 52 | + DependencyProperty.Register(nameof(HeaderMargin), typeof(Thickness), typeof(Spoiler), new PropertyMetadata(default(Thickness))); |
| 53 | + |
| 54 | + public double ShimmerWidth |
| 55 | + { |
| 56 | + get { return (double)GetValue(ShimmerWidthProperty); } |
| 57 | + set { SetValue(ShimmerWidthProperty, value); } |
| 58 | + } |
| 59 | + |
| 60 | + public static readonly DependencyProperty ShimmerWidthProperty = |
| 61 | + DependencyProperty.Register(nameof(ShimmerWidth), typeof(double), typeof(Spoiler), new PropertyMetadata(double.NaN)); |
| 62 | + |
| 63 | + public double ShimmerHeight |
| 64 | + { |
| 65 | + get { return (double)GetValue(ShimmerHeightProperty); } |
| 66 | + set { SetValue(ShimmerHeightProperty, value); } |
| 67 | + } |
| 68 | + |
| 69 | + public static readonly DependencyProperty ShimmerHeightProperty = |
| 70 | + DependencyProperty.Register(nameof(ShimmerHeight), typeof(double), typeof(Spoiler), new PropertyMetadata(double.NaN)); |
| 71 | + |
| 72 | + public CornerRadius ShimmerCornerRadius |
| 73 | + { |
| 74 | + get { return (CornerRadius)GetValue(ShimmerCornerRadiusProperty); } |
| 75 | + set { SetValue(ShimmerCornerRadiusProperty, value); } |
| 76 | + } |
| 77 | + |
| 78 | + public static readonly DependencyProperty ShimmerCornerRadiusProperty = |
| 79 | + DependencyProperty.Register(nameof(ShimmerCornerRadius), typeof(CornerRadius), typeof(Spoiler), new PropertyMetadata(default(CornerRadius))); |
| 80 | + |
| 81 | + public DataTemplate ShimmerTemplate |
| 82 | + { |
| 83 | + get { return (DataTemplate)GetValue(ShimmerTemplateProperty); } |
| 84 | + set { SetValue(ShimmerTemplateProperty, value); } |
| 85 | + } |
| 86 | + |
| 87 | + public static readonly DependencyProperty ShimmerTemplateProperty = |
| 88 | + DependencyProperty.Register(nameof(ShimmerTemplate), typeof(DataTemplate), typeof(Spoiler), new PropertyMetadata(null)); |
| 89 | + |
| 90 | + public bool IsActiveShimmer |
| 91 | + { |
| 92 | + get { return (bool)GetValue(IsActiveShimmerProperty); } |
| 93 | + set { SetValue(IsActiveShimmerProperty, value); } |
| 94 | + } |
| 95 | + |
| 96 | + public static readonly DependencyProperty IsActiveShimmerProperty = |
| 97 | + DependencyProperty.Register(nameof(IsActiveShimmer), typeof(bool), typeof(Spoiler), new PropertyMetadata(true, OnIsActiveShimmerChanged)); |
| 98 | + |
| 99 | + private static void OnIsActiveShimmerChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 100 | + { |
| 101 | + var ctl = (Spoiler)d; |
| 102 | + if (ctl != null) |
| 103 | + { |
| 104 | + ctl.InternalIsActiveShimmer = (bool)e.NewValue; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + internal bool InternalIsActiveShimmer |
| 109 | + { |
| 110 | + get { return (bool)GetValue(InternalIsActiveShimmerProperty); } |
| 111 | + set { SetValue(InternalIsActiveShimmerProperty, value); } |
| 112 | + } |
| 113 | + |
| 114 | + public static readonly DependencyProperty InternalIsActiveShimmerProperty = |
| 115 | + DependencyProperty.Register(nameof(InternalIsActiveShimmer), typeof(bool), typeof(Spoiler), new PropertyMetadata(true)); |
| 116 | + |
| 117 | + public bool IsRevealed |
| 118 | + { |
| 119 | + get { return (bool)GetValue(IsRevealedProperty); } |
| 120 | + set { SetValue(IsRevealedProperty, value); } |
| 121 | + } |
| 122 | + |
| 123 | + public static readonly DependencyProperty IsRevealedProperty = |
| 124 | + DependencyProperty.Register(nameof(IsRevealed), typeof(bool), typeof(Spoiler), new PropertyMetadata(false, OnIsRevealedChanged)); |
| 125 | + private static void OnIsRevealedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 126 | + { |
| 127 | + var ctl = (Spoiler)d; |
| 128 | + if (ctl != null) |
| 129 | + { |
| 130 | + var result = (bool)e.NewValue; |
| 131 | + ctl.UpdateVisualState(); |
| 132 | + |
| 133 | + if (result) |
| 134 | + { |
| 135 | + ctl.InternalIsActiveShimmer = false; |
| 136 | + } |
| 137 | + else |
| 138 | + { |
| 139 | + if (ctl.IsActiveShimmer) |
| 140 | + { |
| 141 | + ctl.InternalIsActiveShimmer = true; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + if (result && ctl.RevealMode == SpoilerRevealMode.Once) |
| 146 | + { |
| 147 | + GeneralHelper.ChangeCursor(ctl, InputSystemCursor.Create(InputSystemCursorShape.Arrow)); |
| 148 | + } |
| 149 | + else |
| 150 | + { |
| 151 | + GeneralHelper.ChangeCursor(ctl, InputSystemCursor.Create(InputSystemCursorShape.Hand)); |
| 152 | + } |
| 153 | + |
| 154 | + ctl.RevealChanged?.Invoke(ctl, new SpoilerRevealChangedEventArgs(result)); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + public SpoilerRevealMode RevealMode |
| 159 | + { |
| 160 | + get { return (SpoilerRevealMode)GetValue(RevealModeProperty); } |
| 161 | + set { SetValue(RevealModeProperty, value); } |
| 162 | + } |
| 163 | + |
| 164 | + public static readonly DependencyProperty RevealModeProperty = |
| 165 | + DependencyProperty.Register(nameof(RevealMode), typeof(SpoilerRevealMode), typeof(Spoiler), new PropertyMetadata(SpoilerRevealMode.Once)); |
| 166 | + |
| 167 | + public Spoiler() |
| 168 | + { |
| 169 | + DefaultStyleKey = typeof(Spoiler); |
| 170 | + } |
| 171 | + |
| 172 | + protected override void OnApplyTemplate() |
| 173 | + { |
| 174 | + base.OnApplyTemplate(); |
| 175 | + |
| 176 | + Tapped -= OnTapped; |
| 177 | + Tapped += OnTapped; |
| 178 | + |
| 179 | + UpdateVisualState(); |
| 180 | + |
| 181 | + if (RevealMode == SpoilerRevealMode.Toggle || !IsRevealed) |
| 182 | + { |
| 183 | + GeneralHelper.ChangeCursor(this, InputSystemCursor.Create(InputSystemCursorShape.Hand)); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + private void OnTapped(object sender, TappedRoutedEventArgs e) |
| 188 | + { |
| 189 | + if (RevealMode == SpoilerRevealMode.Once) |
| 190 | + { |
| 191 | + IsRevealed = true; |
| 192 | + } |
| 193 | + else |
| 194 | + { |
| 195 | + IsRevealed = !IsRevealed; |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + private void UpdateVisualState() |
| 200 | + { |
| 201 | + VisualStateManager.GoToState(this, IsRevealed ? "Revealed" : "Hidden", true); |
| 202 | + } |
| 203 | +} |
0 commit comments