-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
michael-hawker
merged 17 commits into
CommunityToolkit:main
from
michael-hawker:constrained-presenter
Aug 3, 2021
Merged
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 2fe411d
Add Viewbox to VisualTreeExtensions FindChild branches
michael-hawker 39a95f2
Add implicit conversion operators to AspectRatio so it can be used in…
michael-hawker 3b1518a
Rename ConstrainedPresenter to ConstrainedBox
michael-hawker 4670e95
Fix trailing whitespace issue
michael-hawker 4041c5c
Fix namespaces on Unit Tests for Controls
michael-hawker 0fd2a37
Merge implementations of ConstrainedBox to handle all the scenarios
michael-hawker 3a0f501
Split out properties into a separate file for ConstrainedBox
michael-hawker 5283769
Tweak method for when we need to re-calculate ConstrainedBox
michael-hawker 8ee8084
Add MultipleX and MultipleY to ConstrainedBox for floor snap to a mul…
michael-hawker b4fe600
Added another sample to the app, but it's a bit wonky still...
michael-hawker 6057802
Add initial primitive test for each property in isolation
michael-hawker 853a4a9
Apply suggestions from code review
michael-hawker 52975a6
Update sample to show how to manipulate the image center
michael-hawker ee0e32e
Add extra mode to sample description
michael-hawker 376c9f8
Update tolerance to be tighter and fix indentation/descriptions
michael-hawker a9cc874
Add a new combined test
michael-hawker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Primitives/ConstrainedBox.bind
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Binary file added
BIN
+825 Bytes
Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Primitives/ConstrainedBox.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
Microsoft.Toolkit.Uwp.UI.Controls.Primitives/ConstrainedBox/AspectRatio.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
Microsoft.Toolkit.Uwp.UI.Controls.Primitives/ConstrainedBox/ConstrainedBox.Properties.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); } | ||
} | ||
|
||
/// <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(); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?