CycleRingLoading.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. namespace UICommon;
  2. [TemplateVisualState(Name = "Large", GroupName = "SizeStates")]
  3. [TemplateVisualState(Name = "Small", GroupName = "SizeStates")]
  4. [TemplateVisualState(Name = "Inactive", GroupName = "ActiveStates")]
  5. [TemplateVisualState(Name = "Active", GroupName = "ActiveStates")]
  6. public class CycleRingLoading : Control
  7. {
  8. static CycleRingLoading()
  9. {
  10. DefaultStyleKeyProperty.OverrideMetadata(typeof(CycleRingLoading), new FrameworkPropertyMetadata(typeof(CycleRingLoading)));
  11. VisibilityProperty.OverrideMetadata(typeof(CycleRingLoading), new FrameworkPropertyMetadata(delegate (DependencyObject ringObject, DependencyPropertyChangedEventArgs e)
  12. {
  13. bool flag = e.NewValue != e.OldValue;
  14. if (flag)
  15. {
  16. CycleRingLoading CycleRingLoading = (CycleRingLoading)ringObject;
  17. bool flag2 = (Visibility)e.NewValue > Visibility.Visible;
  18. if (flag2)
  19. {
  20. CycleRingLoading.SetCurrentValue(IsActiveProperty, false);
  21. }
  22. else
  23. {
  24. CycleRingLoading.IsActive = true;
  25. }
  26. }
  27. }));
  28. }
  29. public CycleRingLoading()
  30. {
  31. SizeChanged += (s, e) =>
  32. {
  33. BindableWidth = ActualWidth;
  34. };
  35. Unloaded += CycleRingLoading_Unloaded;
  36. }
  37. public static readonly DependencyProperty BindableWidthProperty = DependencyProperty.Register("BindableWidth", typeof(double), typeof(CycleRingLoading), new PropertyMetadata(0.0, new PropertyChangedCallback(BindableWidthCallback)));
  38. public static readonly DependencyProperty IsActiveProperty = DependencyProperty.Register("IsActive", typeof(bool), typeof(CycleRingLoading), new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(IsActiveChanged)));
  39. public static readonly DependencyProperty IsLargeProperty = DependencyProperty.Register("IsLarge", typeof(bool), typeof(CycleRingLoading), new PropertyMetadata(true, new PropertyChangedCallback(IsLargeChangedCallback)));
  40. public static readonly DependencyProperty MaxSideLengthProperty = DependencyProperty.Register("MaxSideLength", typeof(double), typeof(CycleRingLoading), new PropertyMetadata(0.0));
  41. public static readonly DependencyProperty EllipseDiameterProperty = DependencyProperty.Register("EllipseDiameter", typeof(double), typeof(CycleRingLoading), new PropertyMetadata(0.0));
  42. public static readonly DependencyProperty EllipseOffsetProperty = DependencyProperty.Register("EllipseOffset", typeof(Thickness), typeof(CycleRingLoading), new PropertyMetadata(default(Thickness)));
  43. private string StateActive = "Active";
  44. private string StateInActive = "InActive";
  45. private string StateLarge = "Large";
  46. private string StateSmall = "Small";
  47. public override void OnApplyTemplate()
  48. {
  49. base.OnApplyTemplate();
  50. UpdateLargeState();
  51. UpdateActiveState();
  52. }
  53. public double BindableWidth
  54. {
  55. get => (double)GetValue(BindableWidthProperty);
  56. private set => SetValue(BindableWidthProperty, value);
  57. }
  58. private static void BindableWidthCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
  59. {
  60. if (dependencyObject is not CycleRingLoading CycleRingLoading)
  61. return;
  62. bool flag = CycleRingLoading == null;
  63. if (!flag)
  64. {
  65. CycleRingLoading!.SetEllipseDiameter((double)e.NewValue);
  66. CycleRingLoading.SetEllipseOffset((double)e.NewValue);
  67. CycleRingLoading.SetMaxSideLength((double)e.NewValue);
  68. }
  69. }
  70. private void SetEllipseDiameter(double width) => EllipseDiameter = width / 8.0;
  71. private void SetEllipseOffset(double width) => EllipseOffset = new Thickness(0.0, width / 2.0, 0.0, 0.0);
  72. private void SetMaxSideLength(double width) => MaxSideLength = width <= 20.0 ? 20.0 : width;
  73. public bool IsActive
  74. {
  75. get => (bool)GetValue(IsActiveProperty);
  76. set => SetValue(IsActiveProperty, value);
  77. }
  78. private static void IsActiveChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
  79. {
  80. if (sender is CycleRingLoading CycleRingLoading)
  81. {
  82. bool flag = CycleRingLoading == null;
  83. if (!flag)
  84. CycleRingLoading!.UpdateActiveState();
  85. }
  86. }
  87. private void UpdateActiveState()
  88. {
  89. bool isActive = IsActive;
  90. if (isActive)
  91. VisualStateManager.GoToState(this, StateActive, true);
  92. else
  93. VisualStateManager.GoToState(this, StateInActive, true);
  94. }
  95. public bool IsLarge
  96. {
  97. get => (bool)GetValue(IsLargeProperty);
  98. set => SetValue(IsLargeProperty, value);
  99. }
  100. private static void IsLargeChangedCallback(DependencyObject sender, DependencyPropertyChangedEventArgs e)
  101. {
  102. if (sender is not CycleRingLoading CycleRingLoading || CycleRingLoading is null)
  103. return;
  104. bool flag = CycleRingLoading == null;
  105. if (!flag)
  106. CycleRingLoading!.UpdateLargeState();
  107. }
  108. private void UpdateLargeState()
  109. {
  110. bool isLarge = IsLarge;
  111. if (isLarge)
  112. VisualStateManager.GoToState(this, StateLarge, true);
  113. else
  114. VisualStateManager.GoToState(this, StateSmall, true);
  115. }
  116. private void CycleRingLoading_Unloaded(object sender, RoutedEventArgs e)
  117. {
  118. IsActive = false;
  119. }
  120. public double MaxSideLength
  121. {
  122. get => (double)GetValue(MaxSideLengthProperty);
  123. set => SetValue(MaxSideLengthProperty, value);
  124. }
  125. public double EllipseDiameter
  126. {
  127. get => (double)GetValue(EllipseDiameterProperty);
  128. set => SetValue(EllipseDiameterProperty, value);
  129. }
  130. public Thickness EllipseOffset
  131. {
  132. get => (Thickness)GetValue(EllipseOffsetProperty);
  133. set => SetValue(EllipseOffsetProperty, value);
  134. }
  135. }