-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathRatingBar.cs
More file actions
536 lines (453 loc) · 21.6 KB
/
RatingBar.cs
File metadata and controls
536 lines (453 loc) · 21.6 KB
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
using System.Collections.ObjectModel;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;
namespace MaterialDesignThemes.Wpf;
/// <summary>
/// A custom control implementing a rating bar.
/// The icon aka content may be set as a DataTemplate via the ButtonContentTemplate property.
/// </summary>
public class RatingBar : Control
{
public static readonly RoutedCommand SelectRatingCommand = new();
static RatingBar()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(RatingBar), new FrameworkPropertyMetadata(typeof(RatingBar)));
}
private readonly ObservableCollection<RatingBarButton> _ratingButtonsInternal = new ObservableCollection<RatingBarButton>();
private readonly ReadOnlyObservableCollection<RatingBarButton> _ratingButtons;
public RatingBar()
{
CommandBindings.Add(new CommandBinding(SelectRatingCommand, SelectItemHandler));
_ratingButtons = new ReadOnlyObservableCollection<RatingBarButton>(_ratingButtonsInternal);
MouseLeave += RatingBar_MouseLeave;
}
private void SelectItemHandler(object sender, ExecutedRoutedEventArgs executedRoutedEventArgs)
{
if (executedRoutedEventArgs.Parameter is int value && !IsReadOnly)
{
if (!IsFractionalValueEnabled)
{
Value = value;
return;
}
Value = GetValueAtMousePosition((RatingBarButton)executedRoutedEventArgs.OriginalSource);
}
}
private double GetValueAtMousePosition(RatingBarButton ratingBarButton)
{
// Get mouse offset inside source
Point p = Mouse.GetPosition(ratingBarButton);
double percentSelected;
switch (Orientation)
{
case Orientation.Horizontal:
if (InvertDirection)
{
percentSelected = 1 - (p.X / ratingBarButton.ActualWidth);
break;
}
percentSelected = p.X / ratingBarButton.ActualWidth;
break;
case Orientation.Vertical:
if (InvertDirection)
{
percentSelected = 1 - (p.Y / ratingBarButton.ActualHeight);
break;
}
percentSelected = p.Y / ratingBarButton.ActualHeight;
break;
default:
throw new ArgumentOutOfRangeException();
}
return ratingBarButton.Value - 1 + percentSelected;
}
public static readonly DependencyProperty MinProperty = DependencyProperty.Register(
nameof(Min), typeof(int), typeof(RatingBar), new PropertyMetadata(1, MinPropertyChangedCallback, MinPropertyCoerceValueCallback));
private static void MinPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
var ratingBar = (RatingBar)dependencyObject;
ratingBar.CoerceValue(ValueProperty);
ratingBar.RebuildButtons();
}
private static object MinPropertyCoerceValueCallback(DependencyObject d, object baseValue)
{
var ratingBar = (RatingBar)d;
return Math.Min((int)baseValue, ratingBar.Max);
}
public int Min
{
get => (int)GetValue(MinProperty);
set => SetValue(MinProperty, value);
}
public static readonly DependencyProperty MaxProperty = DependencyProperty.Register(
nameof(Max), typeof(int), typeof(RatingBar), new PropertyMetadata(5, MaxPropertyChangedCallback, MaxPropertyCoerceValueCallback));
private static void MaxPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
var ratingBar = (RatingBar)dependencyObject;
ratingBar.CoerceValue(ValueProperty);
ratingBar.RebuildButtons();
}
private static object MaxPropertyCoerceValueCallback(DependencyObject d, object baseValue)
{
var ratingBar = (RatingBar)d;
return Math.Max((int)baseValue, ratingBar.Min);
}
public int Max
{
get => (int)GetValue(MaxProperty);
set => SetValue(MaxProperty, value);
}
private static readonly DependencyPropertyKey IsFractionalValueEnabledPropertyKey = DependencyProperty.RegisterReadOnly(
nameof(IsFractionalValueEnabled), typeof(bool), typeof(RatingBar), new PropertyMetadata(false));
internal static readonly DependencyProperty IsFractionalValueEnabledProperty =
IsFractionalValueEnabledPropertyKey.DependencyProperty;
internal bool IsFractionalValueEnabled
{
get => (bool)GetValue(IsFractionalValueEnabledProperty);
private set => SetValue(IsFractionalValueEnabledPropertyKey, value);
}
public static readonly DependencyProperty ValueIncrementsProperty = DependencyProperty.Register(
nameof(ValueIncrements), typeof(double), typeof(RatingBar), new PropertyMetadata(1.0, ValueIncrementsPropertyChangedCallback, ValueIncrementsCoerceValueCallback));
private static void ValueIncrementsPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var ratingBar = (RatingBar)d;
ratingBar.IsFractionalValueEnabled = Math.Abs(ratingBar.ValueIncrements - 1.0) > 1e-10;
ratingBar.RebuildButtons();
}
private static object ValueIncrementsCoerceValueCallback(DependencyObject d, object baseValue)
=> Math.Max(double.Epsilon, Math.Min(1.0, (double)baseValue));
/// <summary>
/// Gets or sets the value increments. Set to a value between 0.0 and 1.0 (both exclusive) to enable fractional values. Default value is 1.0 (i.e. fractional values disabled)
/// </summary>
public double ValueIncrements
{
get => (double)GetValue(ValueIncrementsProperty);
set => SetValue(ValueIncrementsProperty, value);
}
public static readonly DependencyProperty IsPreviewValueEnabledProperty = DependencyProperty.Register(
nameof(IsPreviewValueEnabled), typeof(bool), typeof(RatingBar), new PropertyMetadata(false));
public bool IsPreviewValueEnabled
{
get => (bool)GetValue(IsPreviewValueEnabledProperty);
set => SetValue(IsPreviewValueEnabledProperty, value);
}
private static readonly DependencyPropertyKey PreviewValuePropertyKey = DependencyProperty.RegisterReadOnly(
nameof(PreviewValue), typeof(double?), typeof(RatingBar), new PropertyMetadata(null, null, PreviewValuePropertyCoerceValueCallback));
private static object? PreviewValuePropertyCoerceValueCallback(DependencyObject d, object? baseValue)
{
if (baseValue is null)
return null;
var ratingBar = (RatingBar)d;
if (baseValue is double value)
{
if (!ratingBar.IsFractionalValueEnabled)
value = Math.Ceiling(value);
return ratingBar.CoerceToValidIncrement(value);
}
return (double)ratingBar.Min;
}
internal static readonly DependencyProperty PreviewValueProperty =
PreviewValuePropertyKey.DependencyProperty;
internal double? PreviewValue
{
get => (double?)GetValue(PreviewValueProperty);
private set => SetValue(PreviewValuePropertyKey, value);
}
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
nameof(Value), typeof(double), typeof(RatingBar), new PropertyMetadata(0.0, ValuePropertyChangedCallback, ValuePropertyCoerceValueCallback));
private static void ValuePropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
var ratingBar = (RatingBar)dependencyObject;
OnValueChanged(ratingBar, dependencyPropertyChangedEventArgs);
}
private static object ValuePropertyCoerceValueCallback(DependencyObject d, object baseValue)
{
var ratingBar = (RatingBar)d;
// If factional values are disabled we don't do any coercion. This maintains back-compat where coercion was not applied and Value could be outside of Min/Max range.
if (!ratingBar.IsFractionalValueEnabled)
return baseValue;
if (baseValue is double value)
{
return ratingBar.CoerceToValidIncrement(value);
}
return (double)ratingBar.Min;
}
private double CoerceToValidIncrement(double value)
{
// Coerce the value into a multiple of ValueIncrements and within the bounds.
double valueInCorrectMultiple = Math.Round(value / ValueIncrements, MidpointRounding.AwayFromZero) * ValueIncrements;
return Math.Min(Max, Math.Max(Min, valueInCorrectMultiple));
}
public double Value
{
get => (double)GetValue(ValueProperty);
set => SetValue(ValueProperty, value);
}
public static readonly RoutedEvent ValueChangedEvent =
EventManager.RegisterRoutedEvent(
nameof(Value),
RoutingStrategy.Bubble,
typeof(RoutedPropertyChangedEventHandler<double>),
typeof(RatingBar));
public event RoutedPropertyChangedEventHandler<double> ValueChanged
{
add => AddHandler(ValueChangedEvent, value);
remove => RemoveHandler(ValueChangedEvent, value);
}
private static void OnValueChanged(
DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var instance = (RatingBar)d;
var args = new RoutedPropertyChangedEventArgs<double>(
(double)e.OldValue,
(double)e.NewValue)
{ RoutedEvent = ValueChangedEvent };
instance.RaiseEvent(args);
}
public ReadOnlyObservableCollection<RatingBarButton> RatingButtons => _ratingButtons;
public static readonly DependencyProperty ValueItemContainerButtonStyleProperty = DependencyProperty.Register(
nameof(ValueItemContainerButtonStyle), typeof(Style), typeof(RatingBar), new PropertyMetadata(default(Style)));
public Style? ValueItemContainerButtonStyle
{
get => (Style?)GetValue(ValueItemContainerButtonStyleProperty);
set => SetValue(ValueItemContainerButtonStyleProperty, value);
}
public static readonly DependencyProperty ValueItemTemplateProperty = DependencyProperty.Register(
nameof(ValueItemTemplate), typeof(DataTemplate), typeof(RatingBar), new PropertyMetadata(default(DataTemplate)));
public DataTemplate? ValueItemTemplate
{
get => (DataTemplate?)GetValue(ValueItemTemplateProperty);
set => SetValue(ValueItemTemplateProperty, value);
}
public static readonly DependencyProperty ValueItemTemplateSelectorProperty = DependencyProperty.Register(
nameof(ValueItemTemplateSelector), typeof(DataTemplateSelector), typeof(RatingBar), new PropertyMetadata(default(DataTemplateSelector)));
public DataTemplateSelector? ValueItemTemplateSelector
{
get => (DataTemplateSelector?)GetValue(ValueItemTemplateSelectorProperty);
set => SetValue(ValueItemTemplateSelectorProperty, value);
}
public static readonly DependencyProperty OrientationProperty = DependencyProperty.Register(
nameof(Orientation), typeof(Orientation), typeof(RatingBar), new PropertyMetadata(default(Orientation)));
public Orientation Orientation
{
get => (Orientation)GetValue(OrientationProperty);
set => SetValue(OrientationProperty, value);
}
public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register(
nameof(IsReadOnly), typeof(bool), typeof(RatingBar), new PropertyMetadata(default(bool)));
public bool IsReadOnly
{
get => (bool)GetValue(IsReadOnlyProperty);
set => SetValue(IsReadOnlyProperty, value);
}
public static readonly DependencyProperty InvertDirectionProperty = DependencyProperty.Register(
nameof(InvertDirection), typeof(bool), typeof(RatingBar), new PropertyMetadata(default(bool)));
public bool InvertDirection
{
get => (bool)GetValue(InvertDirectionProperty);
set => SetValue(InvertDirectionProperty, value);
}
private void RebuildButtons()
{
foreach (var ratingBarButton in _ratingButtonsInternal)
{
ratingBarButton.MouseMove -= RatingBarButton_MouseMove;
}
_ratingButtonsInternal.Clear();
foreach (var ratingBarButton in _ratingButtonsInternal)
{
ratingBarButton.MouseMove -= RatingBarButton_MouseMove;
}
_ratingButtonsInternal.Clear();
// When fractional values are enabled, the first rating button represents the value Min when not selected at all and Min+1 when fully selected;
// thus we start with the value Min+1 for the values of the rating buttons.
int start = IsFractionalValueEnabled ? Min + 1 : Min;
if (InvertDirection)
{
for (int i = Max; i >= start; i--)
{
var ratingBarButton = new RatingBarButton(this)
{
Content = i,
ContentTemplate = ValueItemTemplate,
ContentTemplateSelector = ValueItemTemplateSelector,
Style = ValueItemContainerButtonStyle,
Value = i,
};
ratingBarButton.MouseMove += RatingBarButton_MouseMove;
_ratingButtonsInternal.Add(ratingBarButton);
}
}
else
{
for (int i = start; i <= Max; i++)
{
var ratingBarButton = new RatingBarButton(this)
{
Content = i,
ContentTemplate = ValueItemTemplate,
ContentTemplateSelector = ValueItemTemplateSelector,
Style = ValueItemContainerButtonStyle,
Value = i,
};
ratingBarButton.MouseMove += RatingBarButton_MouseMove;
_ratingButtonsInternal.Add(ratingBarButton);
}
}
}
private void RatingBar_MouseLeave(object sender, MouseEventArgs e) => PreviewValue = null;
private void RatingBarButton_MouseMove(object sender, MouseEventArgs e)
{
if (!IsPreviewValueEnabled)
return;
var ratingBarButton = (RatingBarButton)sender;
PreviewValue = GetValueAtMousePosition(ratingBarButton);
}
public override void OnApplyTemplate()
{
RebuildButtons();
base.OnApplyTemplate();
}
internal class TextBlockForegroundConverter : IMultiValueConverter
{
internal static byte SemiTransparent => 0x42; // ~26% opacity
public static TextBlockForegroundConverter Instance { get; } = new();
public object? Convert(object?[]? values, Type targetType, object? parameter, CultureInfo culture)
{
if (values?.Length == 5
&& values[0] is SolidColorBrush brush
&& values[1] is Orientation orientation
&& values[2] is bool invertDirection
&& values[3] is double value
&& values[4] is int buttonValue)
{
if (value >= buttonValue)
return brush;
var originalColor = brush.Color;
var semiTransparent = Color.FromArgb(SemiTransparent, brush.Color.R, brush.Color.G, brush.Color.B);
if (value > buttonValue - 1.0)
{
double offset = value - buttonValue + 1;
if (invertDirection)
{
offset = 1 - offset;
}
return new LinearGradientBrush
{
StartPoint = orientation == Orientation.Horizontal ? new Point(0, 0.5) : new Point(0.5, 0),
EndPoint = orientation == Orientation.Horizontal ? new Point(1, 0.5) : new Point(0.5, 1),
GradientStops = CreateGradientStopCollection(originalColor, semiTransparent, offset, invertDirection)
};
}
return new SolidColorBrush(semiTransparent);
}
static GradientStopCollection CreateGradientStopCollection(Color originalColor, Color semiTransparent, double offset, bool invertDirection)
{
if (invertDirection)
{
return
[
new GradientStop {Color = semiTransparent, Offset = offset},
new GradientStop {Color = originalColor, Offset = offset},
];
}
return
[
new GradientStop {Color = originalColor, Offset = offset},
new GradientStop {Color = semiTransparent, Offset = offset}
];
}
// This should never happen (returning actual brush to avoid the compilers squiggly line warning)
return Brushes.Transparent;
}
public object?[]? ConvertBack(object? value, Type[] targetTypes, object? parameter, CultureInfo culture) => throw new NotImplementedException();
}
internal class PreviewIndicatorTransformXConverter : IMultiValueConverter
{
public static PreviewIndicatorTransformXConverter Instance { get; } = new();
internal static double Margin => 2.0;
public object? Convert(object?[]? values, Type targetType, object? parameter, CultureInfo culture)
{
if (values?.Length >= 7
&& values[0] is double ratingBarButtonActualWidth
&& values[1] is double previewValueActualWidth
&& values[2] is Orientation ratingBarOrientation
&& values[3] is bool ratingBarInvertDirection
&& values[4] is bool isFractionalValueEnabled
&& values[5] is double previewValue
&& values[6] is int ratingButtonValue)
{
if (!isFractionalValueEnabled)
{
return ratingBarOrientation switch
{
Orientation.Horizontal => (ratingBarButtonActualWidth - previewValueActualWidth) / 2,
Orientation.Vertical => -previewValueActualWidth - Margin,
_ => throw new ArgumentOutOfRangeException()
};
}
// Special handling of edge cases due to the inaccuracy of how double values are stored
double percent = previewValue % 1;
if (Math.Abs(ratingButtonValue - previewValue) <= double.Epsilon)
percent = 1.0;
else if (percent <= double.Epsilon)
percent = 0.0;
return ratingBarOrientation switch
{
Orientation.Horizontal => ratingBarInvertDirection ?
(1 - percent) * ratingBarButtonActualWidth - (previewValueActualWidth / 2) :
percent * ratingBarButtonActualWidth - (previewValueActualWidth / 2)
,
Orientation.Vertical => -previewValueActualWidth - Margin,
_ => throw new ArgumentOutOfRangeException()
};
}
return 1.0;
}
public object?[]? ConvertBack(object? value, Type[] targetTypes, object? parameter, CultureInfo culture) => throw new NotImplementedException();
}
internal class PreviewIndicatorTransformYConverter : IMultiValueConverter
{
public static PreviewIndicatorTransformYConverter Instance { get; } = new();
internal static double Margin => 2.0;
public object? Convert(object?[]? values, Type targetType, object? parameter, CultureInfo culture)
{
if (values?.Length >= 7
&& values[0] is double ratingBarButtonActualHeight
&& values[1] is double previewValueActualHeight
&& values[2] is Orientation ratingBarOrientation
&& values[3] is bool ratingBarInvertDirection
&& values[4] is bool isFractionalValueEnabled
&& values[5] is double previewValue
&& values[6] is int ratingButtonValue)
{
if (!isFractionalValueEnabled)
{
return ratingBarOrientation switch
{
Orientation.Horizontal => -previewValueActualHeight - Margin,
Orientation.Vertical => (ratingBarButtonActualHeight - previewValueActualHeight) / 2,
_ => throw new ArgumentOutOfRangeException()
};
}
// Special handling of edge cases due to the inaccuracy of how double values are stored
double percent = previewValue % 1;
if (Math.Abs(ratingButtonValue - previewValue) <= double.Epsilon)
percent = 1.0;
else if (percent <= double.Epsilon)
percent = 0.0;
return ratingBarOrientation switch
{
Orientation.Horizontal => -previewValueActualHeight - Margin,
Orientation.Vertical => ratingBarInvertDirection ?
(1 - percent) * ratingBarButtonActualHeight - (previewValueActualHeight / 2) :
percent * ratingBarButtonActualHeight - (previewValueActualHeight / 2),
_ => throw new ArgumentOutOfRangeException()
};
}
return 1.0;
}
public object?[]? ConvertBack(object? value, Type[] targetTypes, object? parameter, CultureInfo culture) => throw new NotImplementedException();
}
}