Browse Source

Support ignoring property changes in derived Color controls

pull/8215/head
robloo 4 years ago
parent
commit
55de1523c4
  1. 9
      src/Avalonia.Controls.ColorPicker/ColorPicker/ColorPicker.cs
  2. 22
      src/Avalonia.Controls.ColorPicker/ColorSlider/ColorSlider.cs
  3. 12
      src/Avalonia.Controls.ColorPicker/ColorView/ColorView.cs

9
src/Avalonia.Controls.ColorPicker/ColorPicker/ColorPicker.cs

@ -12,5 +12,14 @@ namespace Avalonia.Controls
/// </summary>
public class ColorPicker : ColorView
{
/// <summary>
/// Initializes a new instance of the <see cref="ColorPicker"/> class.
/// </summary>
public ColorPicker() : base()
{
// Completely ignore property changes here
// The ColorView in the control template is responsible to manage this
base.ignorePropertyChanged = true;
}
}
}

22
src/Avalonia.Controls.ColorPicker/ColorSlider/ColorSlider.cs

@ -29,7 +29,7 @@ namespace Avalonia.Controls.Primitives
/// </remarks>
private const double MaxHue = 359;
private bool disableUpdates = false;
protected bool ignorePropertyChanged = false;
/// <summary>
/// Initializes a new instance of the <see cref="ColorSlider"/> class.
@ -135,7 +135,7 @@ namespace Avalonia.Controls.Primitives
/// </summary>
/// <remarks>
/// Warning: This will trigger property changed updates.
/// Consider using <see cref="disableUpdates"/> externally.
/// Consider using <see cref="ignorePropertyChanged"/> externally.
/// </remarks>
private void SetColorToSliderValues()
{
@ -341,7 +341,7 @@ namespace Avalonia.Controls.Primitives
/// <inheritdoc/>
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
if (disableUpdates)
if (ignorePropertyChanged)
{
base.OnPropertyChanged(change);
return;
@ -350,7 +350,7 @@ namespace Avalonia.Controls.Primitives
// Always keep the two color properties in sync
if (change.Property == ColorProperty)
{
disableUpdates = true;
ignorePropertyChanged = true;
HsvColor = Color.ToHsv();
@ -362,21 +362,21 @@ namespace Avalonia.Controls.Primitives
change.GetOldValue<Color>(),
change.GetNewValue<Color>()));
disableUpdates = false;
ignorePropertyChanged = false;
}
else if (change.Property == ColorModelProperty)
{
disableUpdates = true;
ignorePropertyChanged = true;
SetColorToSliderValues();
UpdateBackground();
UpdatePseudoClasses();
disableUpdates = false;
ignorePropertyChanged = false;
}
else if (change.Property == HsvColorProperty)
{
disableUpdates = true;
ignorePropertyChanged = true;
Color = HsvColor.ToRgb();
@ -388,7 +388,7 @@ namespace Avalonia.Controls.Primitives
change.GetOldValue<HsvColor>().ToRgb(),
change.GetNewValue<HsvColor>().ToRgb()));
disableUpdates = false;
ignorePropertyChanged = false;
}
else if (change.Property == IsRoundingEnabledProperty)
{
@ -402,7 +402,7 @@ namespace Avalonia.Controls.Primitives
change.Property == MinimumProperty ||
change.Property == MaximumProperty)
{
disableUpdates = true;
ignorePropertyChanged = true;
Color oldColor = Color;
(var color, var hsvColor) = GetColorFromSliderValues();
@ -421,7 +421,7 @@ namespace Avalonia.Controls.Primitives
UpdatePseudoClasses();
OnColorChanged(new ColorChangedEventArgs(oldColor, Color));
disableUpdates = false;
ignorePropertyChanged = false;
}
base.OnPropertyChanged(change);

12
src/Avalonia.Controls.ColorPicker/ColorView/ColorView.cs

@ -27,7 +27,7 @@ namespace Avalonia.Controls
private ObservableCollection<Color> _customPaletteColors = new ObservableCollection<Color>();
private ColorToHexConverter colorToHexConverter = new ColorToHexConverter();
private bool disableUpdates = false;
protected bool ignorePropertyChanged = false;
/// <summary>
/// Initializes a new instance of the <see cref="ColorView"/> class.
@ -167,7 +167,7 @@ namespace Avalonia.Controls
/// <inheritdoc/>
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
if (disableUpdates)
if (ignorePropertyChanged)
{
base.OnPropertyChanged(change);
return;
@ -176,7 +176,7 @@ namespace Avalonia.Controls
// Always keep the two color properties in sync
if (change.Property == ColorProperty)
{
disableUpdates = true;
ignorePropertyChanged = true;
HsvColor = Color.ToHsv();
SetColorToHexTextBox();
@ -185,11 +185,11 @@ namespace Avalonia.Controls
change.GetOldValue<Color>(),
change.GetNewValue<Color>()));
disableUpdates = false;
ignorePropertyChanged = false;
}
else if (change.Property == HsvColorProperty)
{
disableUpdates = true;
ignorePropertyChanged = true;
Color = HsvColor.ToRgb();
SetColorToHexTextBox();
@ -198,7 +198,7 @@ namespace Avalonia.Controls
change.GetOldValue<HsvColor>().ToRgb(),
change.GetNewValue<HsvColor>().ToRgb()));
disableUpdates = false;
ignorePropertyChanged = false;
}
else if (change.Property == CustomPaletteProperty)
{

Loading…
Cancel
Save