Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selecting custom color on non-100% DPI crashes settings #247

Merged
merged 6 commits into from
Jan 31, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion dev/ColorPicker/APITests/ColorPickerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
using System.Numerics;
using System.Collections;
using System.Linq;
using System.Threading;
using Windows.Foundation;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Shapes;
using Windows.UI.Xaml.Media;
using Common;

#if USING_TAEF
Expand All @@ -29,14 +32,25 @@
using ColorPicker = Microsoft.UI.Xaml.Controls.ColorPicker;
using ColorChangedEventArgs = Microsoft.UI.Xaml.Controls.ColorChangedEventArgs;
using ColorSpectrum = Microsoft.UI.Xaml.Controls.Primitives.ColorSpectrum;
using XamlControlsXamlMetaDataProvider = Microsoft.UI.Xaml.XamlTypeInfo.XamlControlsXamlMetaDataProvider;
using XamlControlsXamlMetaDataProvider = Microsoft.UI.Xaml.XamlTypeInfo.XamlControlsXamlMetaDataProvider;
#endif

namespace Windows.UI.Xaml.Tests.MUXControls.ApiTests
{
[TestClass]
public class ColorPickerTests
{
[TestCleanup]
public void TestCleanup()
{
RunOnUIThread.Execute(() =>
{
Log.Comment("TestCleanup: Restore TestContentRoot to null");
// Put things back the way we found them.
MUXControlsTestApp.App.TestContentRoot = null;
});
}

[TestMethod]
public void ColorPickerTest()
{
Expand Down Expand Up @@ -232,6 +246,37 @@ public void ValidateHueRange()
IdleSynchronizer.Wait();
}

[TestMethod]
public void ValidateFractionalWidthDoesNotCrash()
{
ManualResetEvent spectrumLoadedEvent = new ManualResetEvent(false);

RunOnUIThread.Execute(() =>
{
ColorSpectrum colorSpectrum = new ColorSpectrum();
colorSpectrum.Width = 300.75;
colorSpectrum.Height = 300.75;
colorSpectrum.Loaded += (sender, args) =>
{
var spectrumRectangle = VisualTreeUtils.FindVisualChildByName(colorSpectrum, "SpectrumRectangle") as Rectangle;

if (spectrumRectangle != null)
{
spectrumRectangle.RegisterPropertyChangedCallback(Shape.FillProperty, (o, dp) =>
{
spectrumLoadedEvent.Set();
});
}
};

StackPanel root = new StackPanel();
root.Children.Add(colorSpectrum);
MUXControlsTestApp.App.TestContentRoot = root;
});

spectrumLoadedEvent.WaitOne();
}

// XamlControlsXamlMetaDataProvider does not exist in the OS repo,
// so we can't execute this test as authored there.
#if !BUILD_WINDOWS
Expand Down
16 changes: 8 additions & 8 deletions dev/ColorPicker/ColorSpectrum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -886,20 +886,20 @@ void ColorSpectrum::CreateBitmapsAndColorMap()
shared_ptr<vector<::byte>> bgraMaxPixelData = make_shared<vector<::byte>>();
shared_ptr<vector<Hsv>> newHsvValues = make_shared<vector<Hsv>>();

bgraMinPixelData->reserve(static_cast<size_t>(minDimension * minDimension * 4));
bgraMinPixelData->reserve(static_cast<size_t>(round(minDimension * minDimension * 4)));

// We'll only save pixel data for the middle bitmaps if our third dimension is hue.
if (components == winrt::ColorSpectrumComponents::ValueSaturation ||
components == winrt::ColorSpectrumComponents::SaturationValue)
{
bgraMiddle1PixelData->reserve(static_cast<size_t>(minDimension * minDimension * 4));
bgraMiddle2PixelData->reserve(static_cast<size_t>(minDimension * minDimension * 4));
bgraMiddle3PixelData->reserve(static_cast<size_t>(minDimension * minDimension * 4));
bgraMiddle4PixelData->reserve(static_cast<size_t>(minDimension * minDimension * 4));
bgraMiddle1PixelData->reserve(static_cast<size_t>(round(minDimension * minDimension * 4)));
bgraMiddle2PixelData->reserve(static_cast<size_t>(round(minDimension * minDimension * 4)));
bgraMiddle3PixelData->reserve(static_cast<size_t>(round(minDimension * minDimension * 4)));
bgraMiddle4PixelData->reserve(static_cast<size_t>(round(minDimension * minDimension * 4)));
}

bgraMaxPixelData->reserve(static_cast<size_t>(minDimension * minDimension * 4));
newHsvValues->reserve(static_cast<size_t>(minDimension * minDimension));
bgraMaxPixelData->reserve(static_cast<size_t>(round(minDimension * minDimension * 4)));
newHsvValues->reserve(static_cast<size_t>(round(minDimension * minDimension)));

winrt::WorkItemHandler workItemHandler(
[minDimension, hsv, minHue, maxHue, minSaturation, maxSaturation, minValue, maxValue, shape, components,
Expand Down Expand Up @@ -1651,4 +1651,4 @@ bool ColorSpectrum::SelectionEllipseShouldBeLight()
double bg = displayedColor.B <= 10 ? displayedColor.B / 3294.0 : pow(displayedColor.B / 269.0 + 0.0513, 2.4);

return 0.2126 * rg + 0.7152 * gg + 0.0722 * bg <= 0.5;
}
}
27 changes: 27 additions & 0 deletions test/MUXControlsTestApp/Utilities/VisualTreeUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,32 @@ public static T FindElementOfTypeInSubtree<T>(this DependencyObject element)

return null;
}

public static DependencyObject FindVisualChildByName(FrameworkElement parent, string name)
{
if (parent.Name == name)
{
return parent;
}

int childrenCount = VisualTreeHelper.GetChildrenCount(parent);

for (int i = 0; i < childrenCount; i++)
{
FrameworkElement childAsFE = VisualTreeHelper.GetChild(parent, i) as FrameworkElement;

if (childAsFE != null)
{
DependencyObject result = FindVisualChildByName(childAsFE, name);

if (result != null)
{
return result;
}
}
}

return null;
}
}
}