Skip to content

Commit 28444e9

Browse files
author
msftbot[bot]
authored
Switch ColorPicker to Derive from WinUI Controls (#4010)
## Fixes # Fixes some potential crashes with the built-in Windows 10 ColorSpectrum by switching to the WinUI version instead with the latest bug fixes. * ColorPicker and ColorSpectrum now derive from the WinUI equivalent controls <!-- Add a brief overview here of the feature/bug & fix. --> ## PR Type What kind of change does this PR introduce? <!-- Please uncomment one or more that apply to this PR. --> - Bugfix <!-- - Feature --> <!-- - Code style update (formatting) --> - Refactoring (no functional changes, no api changes) <!-- - Build or CI related changes --> <!-- - Documentation content changes --> <!-- - Sample app changes --> <!-- - Other... Please describe: --> ## What is the current behavior? <!-- Please describe the current behavior that you are modifying, or link to a relevant issue. --> ## What is the new behavior? <!-- Describe how was this issue resolved or changed? --> ## PR Checklist Please check if your PR fulfills the following requirements: - [x] Tested code with current [supported SDKs](../readme.md#supported) - [ ] ~Pull Request has been submitted to the documentation repository [instructions](..\contributing.md#docs). Link:~ <!-- docs PR link --> - [ ] ~Sample in sample app has been added / updated (for bug fixes / features)~ - [ ] ~Icon has been created (if new sample) following the [Thumbnail Style Guide and templates](https://github.com/windows-toolkit/WindowsCommunityToolkit-design-assets)~ - [ ] ~New major technical changes in the toolkit have or will be added to the [Wiki](https://github.com/windows-toolkit/WindowsCommunityToolkit/wiki) e.g. build changes, source generators, testing infrastructure, sample creation changes, etc...~ - [ ] ~Tests for the changes have been added (for bug fixes / features) (if applicable)~ - [ ] ~Header has been added to all new source files (run *build/UpdateHeaders.bat*)~ - [ ] Contains **NO** breaking changes **Anyone customizing the Spectrum shape or components directly in C# will need to switch to Microsoft.UI.Xaml namespace** <!-- If this PR contains a breaking change, please describe the impact and migration path for existing applications below. Please note that breaking changes are likely to be rejected within minor release cycles or held until major versions. --> ## Other information See discussion in #3943 #3943 (comment)
2 parents 0dde090 + 25dafdd commit 28444e9

File tree

3 files changed

+31
-28
lines changed

3 files changed

+31
-28
lines changed

Microsoft.Toolkit.Uwp.UI.Controls.Input/ColorPicker/ColorPicker.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using Windows.UI.Xaml.Controls.Primitives;
1616
using Windows.UI.Xaml.Media;
1717
using ColorPickerSlider = Microsoft.Toolkit.Uwp.UI.Controls.Primitives.ColorPickerSlider;
18+
using ColorSpectrum = Microsoft.UI.Xaml.Controls.Primitives.ColorSpectrum;
1819

1920
namespace Microsoft.Toolkit.Uwp.UI.Controls
2021
{
@@ -52,7 +53,7 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
5253
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.LayoutRules", "SA1501:Statement should not be on a single line", Justification = "Inline brackets are used to improve code readability with repeated null checks.")]
5354
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.SpacingRules", "SA1025:Code should not contain multiple whitespace in a row", Justification = "Whitespace is used to align code in columns for readability.")]
5455
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1306:Field names should begin with lower-case letter", Justification = "Only template parts start with a capital letter. This differentiates them from other fields.")]
55-
public partial class ColorPicker : Windows.UI.Xaml.Controls.ColorPicker
56+
public partial class ColorPicker : Microsoft.UI.Xaml.Controls.ColorPicker
5657
{
5758
internal Color CheckerBackgroundColor { get; set; } = Color.FromArgb(0x19, 0x80, 0x80, 0x80); // Overridden later
5859

@@ -122,6 +123,7 @@ public partial class ColorPicker : Windows.UI.Xaml.Controls.ColorPicker
122123
public ColorPicker()
123124
{
124125
this.DefaultStyleKey = typeof(ColorPicker);
126+
this.DefaultStyleResourceUri = new System.Uri("ms-appx:///Microsoft.Toolkit.Uwp.UI.Controls.Input/Themes/Generic.xaml");
125127

126128
// Setup collections
127129
this.SetValue(CustomPaletteColorsProperty, new ObservableCollection<Color>());
@@ -486,22 +488,22 @@ private ColorChannel GetActiveColorSpectrumThirdDimension()
486488
{
487489
switch (this.ColorSpectrumComponents)
488490
{
489-
case Windows.UI.Xaml.Controls.ColorSpectrumComponents.SaturationValue:
490-
case Windows.UI.Xaml.Controls.ColorSpectrumComponents.ValueSaturation:
491+
case Microsoft.UI.Xaml.Controls.ColorSpectrumComponents.SaturationValue:
492+
case Microsoft.UI.Xaml.Controls.ColorSpectrumComponents.ValueSaturation:
491493
{
492494
// Hue
493495
return ColorChannel.Channel1;
494496
}
495497

496-
case Windows.UI.Xaml.Controls.ColorSpectrumComponents.HueValue:
497-
case Windows.UI.Xaml.Controls.ColorSpectrumComponents.ValueHue:
498+
case Microsoft.UI.Xaml.Controls.ColorSpectrumComponents.HueValue:
499+
case Microsoft.UI.Xaml.Controls.ColorSpectrumComponents.ValueHue:
498500
{
499501
// Saturation
500502
return ColorChannel.Channel2;
501503
}
502504

503-
case Windows.UI.Xaml.Controls.ColorSpectrumComponents.HueSaturation:
504-
case Windows.UI.Xaml.Controls.ColorSpectrumComponents.SaturationHue:
505+
case Microsoft.UI.Xaml.Controls.ColorSpectrumComponents.HueSaturation:
506+
case Microsoft.UI.Xaml.Controls.ColorSpectrumComponents.SaturationHue:
505507
{
506508
// Value
507509
return ColorChannel.Channel3;
@@ -1125,7 +1127,7 @@ private void DispatcherQueueTimer_Tick(object sender, object e)
11251127
***************************************************************************************/
11261128

11271129
/// <summary>
1128-
/// Callback for when the <see cref="Windows.UI.Xaml.Controls.ColorPicker.Color"/> dependency property value changes.
1130+
/// Callback for when the <see cref="Microsoft.UI.Xaml.Controls.ColorPicker.Color"/> dependency property value changes.
11291131
/// </summary>
11301132
private void OnColorChanged(DependencyObject d, DependencyProperty e)
11311133
{
@@ -1226,7 +1228,7 @@ private void CustomPaletteColors_CollectionChanged(object sender, NotifyCollecti
12261228
/// Event handler for when the color spectrum color is changed.
12271229
/// This occurs when the user presses on the spectrum to select a new color.
12281230
/// </summary>
1229-
private void ColorSpectrum_ColorChanged(ColorSpectrum sender, Windows.UI.Xaml.Controls.ColorChangedEventArgs args)
1231+
private void ColorSpectrum_ColorChanged(ColorSpectrum sender, Microsoft.UI.Xaml.Controls.ColorChangedEventArgs args)
12301232
{
12311233
// It is OK in this case to use the RGB representation
12321234
this.ScheduleColorUpdate(this.ColorSpectrumControl.Color);

Microsoft.Toolkit.Uwp.UI.Controls.Input/ColorPicker/ColorPicker.xaml

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
xmlns:converters="using:Microsoft.Toolkit.Uwp.UI.Converters"
55
xmlns:localconverters="using:Microsoft.Toolkit.Uwp.UI.Controls.ColorPickerConverters"
66
xmlns:primitives="using:Microsoft.Toolkit.Uwp.UI.Controls.Primitives"
7-
xmlns:ui="using:Microsoft.Toolkit.Uwp.UI">
7+
xmlns:ui="using:Microsoft.Toolkit.Uwp.UI"
8+
xmlns:winuiprimitives="using:Microsoft.UI.Xaml.Controls.Primitives">
89

910
<SolidColorBrush x:Key="CheckerboardColor0">
1011
<SolidColorBrush.Color>
@@ -120,23 +121,23 @@
120121
Orientation="Vertical" />
121122
<!-- Keep in mind the spectrum is always HSV -->
122123
<!-- This must also not be named 'ColorSpectrum' to differentiate from the base class -->
123-
<ColorSpectrum x:Name="ColorSpectrumControl"
124-
Grid.Row="0"
125-
Grid.RowSpan="2"
126-
Grid.Column="1"
127-
MinWidth="256"
128-
MinHeight="256"
129-
HorizontalAlignment="Stretch"
130-
VerticalAlignment="Stretch"
131-
AutomationProperties.Name="Third Dimension"
132-
Components="{TemplateBinding ColorSpectrumComponents}"
133-
MaxHue="{TemplateBinding MaxHue}"
134-
MaxSaturation="{TemplateBinding MaxSaturation}"
135-
MaxValue="{TemplateBinding MaxValue}"
136-
MinHue="{TemplateBinding MinHue}"
137-
MinSaturation="{TemplateBinding MinSaturation}"
138-
MinValue="{TemplateBinding MinValue}"
139-
Shape="{TemplateBinding ColorSpectrumShape}" />
124+
<winuiprimitives:ColorSpectrum x:Name="ColorSpectrumControl"
125+
Grid.Row="0"
126+
Grid.RowSpan="2"
127+
Grid.Column="1"
128+
MinWidth="256"
129+
MinHeight="256"
130+
HorizontalAlignment="Stretch"
131+
VerticalAlignment="Stretch"
132+
AutomationProperties.Name="Third Dimension"
133+
Components="{TemplateBinding ColorSpectrumComponents}"
134+
MaxHue="{TemplateBinding MaxHue}"
135+
MaxSaturation="{TemplateBinding MaxSaturation}"
136+
MaxValue="{TemplateBinding MaxValue}"
137+
MinHue="{TemplateBinding MinHue}"
138+
MinSaturation="{TemplateBinding MinSaturation}"
139+
MinValue="{TemplateBinding MinValue}"
140+
Shape="{TemplateBinding ColorSpectrumShape}" />
140141
<primitives:ColorPickerSlider x:Name="ColorSpectrumAlphaSlider"
141142
Grid.Row="1"
142143
Grid.RowSpan="2"

Microsoft.Toolkit.Uwp.UI.Controls.Input/ColorPicker/ColorPickerButton.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ protected override void OnApplyTemplate()
142142
}
143143
}
144144

145-
private void ColorPicker_ColorChanged(Windows.UI.Xaml.Controls.ColorPicker sender, ColorChangedEventArgs args)
145+
private void ColorPicker_ColorChanged(Microsoft.UI.Xaml.Controls.ColorPicker sender, Microsoft.UI.Xaml.Controls.ColorChangedEventArgs args)
146146
{
147147
SelectedColor = args.NewColor;
148148
}

0 commit comments

Comments
 (0)