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

Add initial ConstrainedBox control for AspectRatio and Scaling #4104

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
d996cb5
Add initial ConstrainedPresenter control for AspectRatio and simple S…
michael-hawker Jul 9, 2021
2fe411d
Add Viewbox to VisualTreeExtensions FindChild branches
michael-hawker Jul 15, 2021
39a95f2
Add implicit conversion operators to AspectRatio so it can be used in…
michael-hawker Jul 20, 2021
3b1518a
Rename ConstrainedPresenter to ConstrainedBox
michael-hawker Jul 22, 2021
4670e95
Fix trailing whitespace issue
michael-hawker Jul 28, 2021
4041c5c
Fix namespaces on Unit Tests for Controls
michael-hawker Jul 29, 2021
0fd2a37
Merge implementations of ConstrainedBox to handle all the scenarios
michael-hawker Jul 29, 2021
3a0f501
Split out properties into a separate file for ConstrainedBox
michael-hawker Jul 29, 2021
5283769
Tweak method for when we need to re-calculate ConstrainedBox
michael-hawker Jul 29, 2021
8ee8084
Add MultipleX and MultipleY to ConstrainedBox for floor snap to a mul…
michael-hawker Jul 30, 2021
b4fe600
Added another sample to the app, but it's a bit wonky still...
michael-hawker Jul 30, 2021
6057802
Add initial primitive test for each property in isolation
michael-hawker Jul 31, 2021
853a4a9
Apply suggestions from code review
michael-hawker Aug 2, 2021
52975a6
Update sample to show how to manipulate the image center
michael-hawker Jul 31, 2021
ee0e32e
Add extra mode to sample description
michael-hawker Aug 2, 2021
376c9f8
Update tolerance to be tighter and fix indentation/descriptions
michael-hawker Aug 2, 2021
a9cc874
Add a new combined test
michael-hawker Aug 2, 2021
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
Binary file not shown.
Binary file added Microsoft.Toolkit.Uwp.SampleApp/Assets/checker.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed Microsoft.Toolkit.Uwp.SampleApp/Assets/pixel.png
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand Down Expand Up @@ -130,6 +130,7 @@
<!-- A reference to the entire .Net Framework and Windows SDK are automatically included -->
<Content Include="Assets\BrushAssets\TileTexture.png" />
<Content Include="Assets\BrushAssets\NoiseTexture.png" />
<Content Include="Assets\checker.png" />
<Content Include="Assets\Llama.mp3" />
<Content Include="Assets\mslogo.png" />
<Content Include="Assets\NotificationAssets\Cloudy-Square.png" />
Expand Down Expand Up @@ -271,6 +272,7 @@
<Content Include="SamplePages\Graph\LoginButton.png" />
<Content Include="SamplePages\Graph\PeoplePicker.png" />
<Content Include="SamplePages\Graph\PersonView.png" />
<Content Include="SamplePages\Primitives\ConstrainedBox.png" />
<Content Include="SamplePages\Primitives\SwitchPresenter.png" />
<Content Include="SamplePages\TabbedCommandBar\TabbedCommandBar.png" />
<Content Include="SamplePages\Animations\Effects\FadeBehavior.png" />
Expand Down Expand Up @@ -621,6 +623,9 @@
<Content Include="SamplePages\Graph\PeoplePickerXaml.bind" />
<Content Include="SamplePages\Graph\PersonViewXaml.bind" />
<Content Include="SamplePages\ListViewExtensions\ListViewExtensionsCode.bind" />
<Content Include="SamplePages\Primitives\ConstrainedBox.bind">
<SubType>Designer</SubType>
</Content>
</ItemGroup>
<ItemGroup>
<Compile Include="App.xaml.cs">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
xmlns:brushes="using:Microsoft.Toolkit.Uwp.UI.Media"
mc:Ignorable="d">

<Grid>
<controls:ConstrainedBox AspectRatio="16:3" VerticalAlignment="Top">
<Image Source="/Assets/Photos/WestSeattleView.jpg"
Stretch="UniformToFill"
VerticalAlignment="Center"/> <!-- Center on the City -->
</controls:ConstrainedBox>
<controls:ConstrainedBox MultipleX="64"
AspectRatio="1:1"
MinWidth="64" MaxWidth="512">
<controls:ConstrainedBox.Background>
<!-- TODO: TilesBrush doesn't support Dpi image loading for this scenario
This example is configured for 100% DPI at the moment.
See https://github.com/CommunityToolkit/WindowsCommunityToolkit/issues/4150
-->
<brushes:TilesBrush TextureUri="ms-appx:///Assets/checker.png"/>
</controls:ConstrainedBox.Background>
</controls:ConstrainedBox>
</Grid>
</Page>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<controls:Case IsDefault="True" />
</controls:CaseCollection>
</controls:SwitchPresenter>
<controls:ConstrainedBox x:Key="ConstrainedBoxControl" />
</Page.Resources>

<Grid>
Expand Down
11 changes: 10 additions & 1 deletion Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,15 @@
"XamlCodeFile": "/SamplePages/Primitives/SwitchPresenter.bind",
"Icon": "/SamplePages/Primitives/SwitchPresenter.png",
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/controls/SwitchPresenter.md"
},
{
"Name": "ConstrainedBox",
"Subcategory": "Layout",
"About": "The ConstrainedBox is a FrameworkElement which can allow a developer to constrain the scale, multiple of, or aspect ratio of its content.",
"CodeUrl": "https://github.com/CommunityToolkit/WindowsCommunityToolkit/tree/main/Microsoft.Toolkit.Uwp.UI.Controls.Primitives/ConstrainedBox",
"XamlCodeFile": "/SamplePages/Primitives/ConstrainedBox.bind",
"Icon": "/SamplePages/Primitives/ConstrainedBox.png",
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/controls/ConstrainedBox.md"
}
]
},
Expand Down Expand Up @@ -1259,4 +1268,4 @@
}
]
}
]
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;

namespace Microsoft.Toolkit.Uwp.UI.Controls
{
/// <summary>
/// The <see cref="AspectRatio"/> structure is used by the <see cref="ConstrainedBox"/> control to
/// define a specific ratio to restrict its content.
/// </summary>
[Windows.Foundation.Metadata.CreateFromString(MethodName = "Microsoft.Toolkit.Uwp.UI.Controls.AspectRatio.ConvertToAspectRatio")]
public readonly struct AspectRatio
{
/// <summary>
/// Gets the width component of the aspect ratio or the aspect ratio itself (and height will be 1).
/// </summary>
public double Width { get; }

/// <summary>
/// Gets the height component of the aspect ratio.
/// </summary>
public double Height { get; }

/// <summary>
/// Gets the raw numeriucal aspect ratio value itself (Width / Height).
/// </summary>
public double Value => Width / Height;

/// <summary>
/// Initializes a new instance of the <see cref="AspectRatio"/> struct with the provided width and height.
/// </summary>
/// <param name="width">Width side of the ratio.</param>
/// <param name="height">Height side of the ratio.</param>
public AspectRatio(double width, double height)
{
Width = width;
Height = height;
}

/// <summary>
/// Initializes a new instance of the <see cref="AspectRatio"/> struct with the specific numerical aspect ratio.
/// </summary>
/// <param name="ratio">Raw Aspect Ratio, Height will be 1.</param>
public AspectRatio(double ratio)
{
Width = ratio;
Height = 1;
}

/// <summary>
/// Implicit conversion operator to convert an <see cref="AspectRatio"/> to a <see cref="double"/> value.
/// This lets you use them easily in mathmatical expressions.
/// </summary>
/// <param name="aspect"><see cref="AspectRatio"/> instance.</param>
public static implicit operator double(AspectRatio aspect) => aspect.Value;

/// <summary>
/// Implicit conversion operator to convert a <see cref="double"/> to an <see cref="AspectRatio"/> value.
/// This allows for x:Bind to bind to a double value.
/// </summary>
/// <param name="ratio"><see cref="double"/> value representing the <see cref="AspectRatio"/>.</param>
public static implicit operator AspectRatio(double ratio) => new AspectRatio(ratio);

/// <summary>
/// Converter to take a string aspect ration like "16:9" and convert it to an <see cref="AspectRatio"/> struct.
/// Used automatically by XAML.
/// </summary>
/// <param name="rawString">The string to be converted in format "Width:Height" or a decimal value.</param>
/// <returns>The <see cref="AspectRatio"/> struct representing that ratio.</returns>
public static AspectRatio ConvertToAspectRatio(string rawString)
{
string[] ratio = rawString.Split(":");

if (ratio.Length == 2)
{
return new AspectRatio(Convert.ToDouble(ratio[0]), Convert.ToDouble(ratio[1]));
}
else if (ratio.Length == 1)
{
return new AspectRatio(Convert.ToDouble(ratio[0]));
}

return new AspectRatio(1);
}

/// <inheritdoc/>
public override string ToString()
{
return Width + ":" + Height;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace Microsoft.Toolkit.Uwp.UI.Controls
{
/// <summary>
/// Dependency properties for the <see cref="ConstrainedBox"/> class.
/// </summary>
public partial class ConstrainedBox
{
/// <summary>
/// Gets or sets the scale for the width of the panel. Should be a value between 0-1.0. Default is 1.0.
/// </summary>
public double ScaleX
{
get { return (double)GetValue(ScaleXProperty); }
set { SetValue(ScaleXProperty, value); }
}

/// <summary>
/// Identifies the <see cref="ScaleX"/> property.
/// </summary>
public static readonly DependencyProperty ScaleXProperty =
DependencyProperty.Register(nameof(ScaleX), typeof(double), typeof(ConstrainedBox), new PropertyMetadata(1.0, ConstraintPropertyChanged));

/// <summary>
/// Gets or sets the scale for the height of the panel. Should be a value between 0-1.0. Default is 1.0.
/// </summary>
public double ScaleY
{
get { return (double)GetValue(ScaleYProperty); }
set { SetValue(ScaleYProperty, value); }
}

/// <summary>
/// Identifies the <see cref="ScaleY"/> property.
/// </summary>
public static readonly DependencyProperty ScaleYProperty =
DependencyProperty.Register(nameof(ScaleY), typeof(double), typeof(ConstrainedBox), new PropertyMetadata(1.0, ConstraintPropertyChanged));

/// <summary>
/// Gets or sets the integer multiple that the width of the panel should be floored to. Default is null (no snap).
/// </summary>
public int MultipleX
{
get { return (int)GetValue(MultipleXProperty); }
set { SetValue(MultipleXProperty, value); }
}

/// <summary>
/// Identifies the <see cref="MultipleX"/> property.
/// </summary>
public static readonly DependencyProperty MultipleXProperty =
DependencyProperty.Register(nameof(MultipleX), typeof(int), typeof(ConstrainedBox), new PropertyMetadata(null));

/// <summary>
/// Gets or sets the integer multiple that the height of the panel should be floored to. Default is null (no snap).
/// </summary>
public int MultipleY
{
get { return (int)GetValue(MultipleYProperty); }
set { SetValue(MultipleYProperty, value); }
}
Comment on lines +60 to +73
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be an integer? What if I want the value to be a multiple of 3.5.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose, but why would you want to snap to an un-even number of pixels?


/// <summary>
/// Identifies the <see cref="MultipleY"/> property.
/// </summary>
public static readonly DependencyProperty MultipleYProperty =
DependencyProperty.Register(nameof(MultipleY), typeof(int), typeof(ConstrainedBox), new PropertyMetadata(null));

/// <summary>
/// Gets or sets aspect Ratio to use for the contents of the Panel (after scaling).
/// </summary>
public AspectRatio AspectRatio
{
get { return (AspectRatio)GetValue(AspectRatioProperty); }
set { SetValue(AspectRatioProperty, value); }
}

/// <summary>
/// Identifies the <see cref="AspectRatio"/> property.
/// </summary>
public static readonly DependencyProperty AspectRatioProperty =
DependencyProperty.Register(nameof(AspectRatio), typeof(AspectRatio), typeof(ConstrainedBox), new PropertyMetadata(null, ConstraintPropertyChanged));

private static void ConstraintPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is ConstrainedBox panel)
{
panel.InvalidateMeasure();
}
}
}
}
Loading