From 2575a10fb09677ac3f131ab3715b20c46b588397 Mon Sep 17 00:00:00 2001 From: robloo Date: Sat, 15 Oct 2022 14:33:47 -0400 Subject: [PATCH] Add and implement IsHostedInColorPicker property to ColorView This works-around some lifecycle issues by allowing the ColorView to specially handle how it handles property changes based on whether or not it is within a ColorPicker. --- .../ColorPicker/ColorPicker.cs | 3 --- .../ColorView/ColorView.Properties.cs | 24 +++++++++++++++++++ .../ColorView/ColorView.cs | 20 ++++++++++++++++ .../Themes/Fluent/ColorPicker.xaml | 4 ++++ 4 files changed, 48 insertions(+), 3 deletions(-) diff --git a/src/Avalonia.Controls.ColorPicker/ColorPicker/ColorPicker.cs b/src/Avalonia.Controls.ColorPicker/ColorPicker/ColorPicker.cs index 39369bcbdb..92d8535272 100644 --- a/src/Avalonia.Controls.ColorPicker/ColorPicker/ColorPicker.cs +++ b/src/Avalonia.Controls.ColorPicker/ColorPicker/ColorPicker.cs @@ -11,9 +11,6 @@ /// public ColorPicker() : base() { - // Completely ignore property changes here - // The ColorView in the control template is responsible to manage this - base.ignorePropertyChanged = true; } } } diff --git a/src/Avalonia.Controls.ColorPicker/ColorView/ColorView.Properties.cs b/src/Avalonia.Controls.ColorPicker/ColorView/ColorView.Properties.cs index b76059037b..d1ee375e28 100644 --- a/src/Avalonia.Controls.ColorPicker/ColorView/ColorView.Properties.cs +++ b/src/Avalonia.Controls.ColorPicker/ColorView/ColorView.Properties.cs @@ -148,6 +148,14 @@ namespace Avalonia.Controls nameof(IsHexInputVisible), true); + /// + /// Defines the property. + /// + public static readonly StyledProperty IsHostedInColorPickerProperty = + AvaloniaProperty.Register( + nameof(IsHostedInColorPicker), + false); + /// /// Defines the property. /// @@ -395,6 +403,22 @@ namespace Avalonia.Controls set => SetValue(IsHexInputVisibleProperty, value); } + /// + /// Gets or sets a value indicating whether this is hosted + /// inside a 's control template. + /// + /// + /// This is a special property to change how the internally + /// processes property updates. When a is hosted within a + /// , it does not need to handle most property changes. + /// Instead, the primary calculations are done within the itself. + /// + public bool IsHostedInColorPicker + { + get => GetValue(IsHostedInColorPickerProperty); + set => SetValue(IsHostedInColorPickerProperty, value); + } + /// public int MaxHue { diff --git a/src/Avalonia.Controls.ColorPicker/ColorView/ColorView.cs b/src/Avalonia.Controls.ColorPicker/ColorView/ColorView.cs index 89f1afb1ac..b0d5250c50 100644 --- a/src/Avalonia.Controls.ColorPicker/ColorView/ColorView.cs +++ b/src/Avalonia.Controls.ColorPicker/ColorView/ColorView.cs @@ -206,6 +206,26 @@ namespace Avalonia.Controls return; } + // Special handling for when hosted within a ColorPicker and the ColorPicker is + // responsible for most of the internal processing and property updates. + if (IsHostedInColorPicker) + { + if (change.Property == ColorProperty || + change.Property == HsvColorProperty) + { + ignorePropertyChanged = true; + + // The Hex text isn't currently bound with the ColorPicker and therefore + // must still be updated here. + SetColorToHexTextBox(); + + ignorePropertyChanged = false; + } + + base.OnPropertyChanged(change); + return; + } + // Always keep the two color properties in sync if (change.Property == ColorProperty) { diff --git a/src/Avalonia.Controls.ColorPicker/Themes/Fluent/ColorPicker.xaml b/src/Avalonia.Controls.ColorPicker/Themes/Fluent/ColorPicker.xaml index 74a1df4991..acdd06480f 100644 --- a/src/Avalonia.Controls.ColorPicker/Themes/Fluent/ColorPicker.xaml +++ b/src/Avalonia.Controls.ColorPicker/Themes/Fluent/ColorPicker.xaml @@ -43,7 +43,11 @@ +