|
| 1 | +using System.Globalization; |
| 2 | +using System; |
| 3 | +using System.Windows; |
| 4 | +using System.Windows.Controls; |
| 5 | +using System.Windows.Data; |
| 6 | +using System.Windows.Media; |
| 7 | + |
| 8 | +namespace Wpf.Ui.Violeta.Controls; |
| 9 | + |
| 10 | +public class Loading : Control |
| 11 | +{ |
| 12 | + public bool IsRunning |
| 13 | + { |
| 14 | + get => (bool)GetValue(IsRunningProperty); |
| 15 | + set => SetValue(IsRunningProperty, value); |
| 16 | + } |
| 17 | + |
| 18 | + public static readonly DependencyProperty IsRunningProperty = |
| 19 | + DependencyProperty.Register(nameof(IsRunning), typeof(bool), typeof(Loading), new PropertyMetadata(true)); |
| 20 | + |
| 21 | + public LoadingStyle LoadingStyle |
| 22 | + { |
| 23 | + get => (LoadingStyle)GetValue(LoadingStyleProperty); |
| 24 | + set => SetValue(LoadingStyleProperty, value); |
| 25 | + } |
| 26 | + |
| 27 | + public static readonly DependencyProperty LoadingStyleProperty = |
| 28 | + DependencyProperty.Register(nameof(LoadingStyle), typeof(LoadingStyle), typeof(Loading), new PropertyMetadata(LoadingStyle.Ring)); |
| 29 | + |
| 30 | + public double Minimum |
| 31 | + { |
| 32 | + get => (double)GetValue(MinimumProperty); |
| 33 | + set => SetValue(MinimumProperty, value); |
| 34 | + } |
| 35 | + |
| 36 | + public static readonly DependencyProperty MinimumProperty = |
| 37 | + DependencyProperty.Register(nameof(Minimum), typeof(double), typeof(Loading), new PropertyMetadata(0d)); |
| 38 | + |
| 39 | + public double Maximum |
| 40 | + { |
| 41 | + get => (double)GetValue(MaximumProperty); |
| 42 | + set => SetValue(MaximumProperty, value); |
| 43 | + } |
| 44 | + |
| 45 | + public static readonly DependencyProperty MaximumProperty = |
| 46 | + DependencyProperty.Register(nameof(Maximum), typeof(double), typeof(Loading), new PropertyMetadata(100d)); |
| 47 | + |
| 48 | + public double Value |
| 49 | + { |
| 50 | + get => (double)GetValue(ValueProperty); |
| 51 | + set => SetValue(ValueProperty, value); |
| 52 | + } |
| 53 | + |
| 54 | + public static readonly DependencyProperty ValueProperty = |
| 55 | + DependencyProperty.Register(nameof(Value), typeof(double), typeof(Loading), new PropertyMetadata(0d)); |
| 56 | + |
| 57 | + static Loading() |
| 58 | + { |
| 59 | + DefaultStyleKeyProperty.OverrideMetadata(typeof(Loading), new FrameworkPropertyMetadata(typeof(Loading))); |
| 60 | + } |
| 61 | + |
| 62 | + public Loading() |
| 63 | + { |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +public enum LoadingStyle |
| 68 | +{ |
| 69 | + Ring, |
| 70 | +} |
| 71 | + |
| 72 | +internal sealed class LoadingBackgroundThicknessConverter : IValueConverter |
| 73 | +{ |
| 74 | + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) |
| 75 | + { |
| 76 | + var actualWidth = (value as double?).GetValueOrDefault(); |
| 77 | + return actualWidth == 0 ? 0 : (object)Math.Ceiling(actualWidth / 15); |
| 78 | + } |
| 79 | + |
| 80 | + public object ConvertBack(object value, Type targetTypes, object parameter, CultureInfo culture) |
| 81 | + { |
| 82 | + return DependencyProperty.UnsetValue; |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +internal sealed class LoadingLineYConverter : IValueConverter |
| 87 | +{ |
| 88 | + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) |
| 89 | + { |
| 90 | + var actualWidth = (value as double?).GetValueOrDefault(); |
| 91 | + if (actualWidth == 0) |
| 92 | + return 0; |
| 93 | + return Math.Ceiling(actualWidth / 4); |
| 94 | + } |
| 95 | + |
| 96 | + public object ConvertBack(object value, Type targetTypes, object parameter, CultureInfo culture) |
| 97 | + { |
| 98 | + return new object[] { DependencyProperty.UnsetValue, DependencyProperty.UnsetValue }; |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +internal sealed class LoadingClassicMarginConverter : IValueConverter |
| 103 | +{ |
| 104 | + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) |
| 105 | + { |
| 106 | + var actualWidth = (value as double?).GetValueOrDefault(); |
| 107 | + if (actualWidth == 0) |
| 108 | + return 0; |
| 109 | + return new Thickness(actualWidth / 2, actualWidth / 2, 0, 0); |
| 110 | + } |
| 111 | + |
| 112 | + public object ConvertBack(object value, Type targetTypes, object parameter, CultureInfo culture) |
| 113 | + { |
| 114 | + return DependencyProperty.UnsetValue; |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +internal sealed class LoadingClassicEllipseSizeConverter : IValueConverter |
| 119 | +{ |
| 120 | + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) |
| 121 | + { |
| 122 | + var actualWidth = (value as double?).GetValueOrDefault(); |
| 123 | + if (actualWidth == 0) |
| 124 | + return 0; |
| 125 | + return Math.Ceiling(actualWidth / 8); |
| 126 | + } |
| 127 | + |
| 128 | + public object ConvertBack(object value, Type targetTypes, object parameter, CultureInfo culture) |
| 129 | + { |
| 130 | + return DependencyProperty.UnsetValue; |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +internal sealed class RingProgressBarConverter : IMultiValueConverter |
| 135 | +{ |
| 136 | + public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) |
| 137 | + { |
| 138 | + var width = (double)values[0]; |
| 139 | + var height = (double)values[1]; |
| 140 | + |
| 141 | + double radius = 0; |
| 142 | + if (values[2] is Thickness) |
| 143 | + radius = ((Thickness)values[2]).Left; |
| 144 | + else if (values[2] is double) |
| 145 | + radius = ((double)values[2]); |
| 146 | + |
| 147 | + var percent = 0.33; |
| 148 | + if (values.Length == 6) |
| 149 | + { |
| 150 | + var min = (double)values[3]; |
| 151 | + var max = (double)values[4]; |
| 152 | + var value = (double)values[5]; |
| 153 | + value = value > max ? max : value; |
| 154 | + value = value < min ? min : value; |
| 155 | + percent = (value - min) / (max - min); |
| 156 | + } |
| 157 | + |
| 158 | + var point1X = height / 2 * Math.Cos((2 * percent - 0.5) * Math.PI) + height / 2; |
| 159 | + var point1Y = height / 2 - height / 2 * Math.Sin((2 * percent + 0.5) * Math.PI); |
| 160 | + var point2X = (height - radius) / 2 * Math.Cos((2 * percent - 0.5) * Math.PI) + height / 2; |
| 161 | + var point2Y = height / 2 - (height - radius) / 2 * Math.Sin((2 * percent + 0.5) * Math.PI); |
| 162 | + |
| 163 | + var path = ""; |
| 164 | + |
| 165 | + if (percent == 0) |
| 166 | + { |
| 167 | + path = ""; |
| 168 | + } |
| 169 | + else if (percent < 0.5) |
| 170 | + { |
| 171 | + path = "M " + width / 2 + "," + radius / 2 + " A " + (width - radius) / 2 + "," + (width - radius) / 2 + " 0 0 1 " + point2X + "," + point2Y + ""; |
| 172 | + } |
| 173 | + else if (percent == 0.5) |
| 174 | + { |
| 175 | + path = "M " + width / 2 + "," + radius / 2 + " A " + (width - radius) / 2 + "," + (width - radius) / 2 + " 0 0 1 " + width / 2 + "," + (height - radius / 2); |
| 176 | + } |
| 177 | + else |
| 178 | + { |
| 179 | + path = "M " + width / 2 + "," + radius / 2 + " A " + (width - radius) / 2 + "," + (width - radius) / 2 + " 0 0 1 " + width / 2 + "," + (height - radius / 2) + |
| 180 | + " A " + (width - radius) / 2 + "," + (width - radius) / 2 + " 0 0 1 " + point2X + "," + point2Y + ""; |
| 181 | + } |
| 182 | + return PathGeometry.Parse(path); |
| 183 | + } |
| 184 | + |
| 185 | + public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) |
| 186 | + { |
| 187 | + return new object[] { DependencyProperty.UnsetValue, DependencyProperty.UnsetValue }; |
| 188 | + } |
| 189 | +} |
0 commit comments