Skip to content

Commit 3c3459a

Browse files
committed
Added VisualExtensions.Translation property
1 parent 7e2d8a8 commit 3c3459a

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

Microsoft.Toolkit.Uwp.UI/Extensions/VisualExtensions.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,36 @@ public static void SetOffset(DependencyObject obj, string value)
114114
obj.SetValue(OffsetProperty, value);
115115
}
116116

117+
/// <summary>
118+
/// Gets the <see cref="Visual.TransformMatrix"/>.<see cref="Matrix4x4.Translation"/> property of a <see cref="UIElement"/> in a <see cref="string"/> form.
119+
/// </summary>
120+
/// <param name="obj">The <see cref="DependencyObject"/> instance.</param>
121+
/// <returns>A <see cref="Vector3"/> <see cref="string"/> representation of the <see cref="Visual.TransformMatrix"/>.<see cref="Matrix4x4.Translation"/> property.</returns>
122+
public static string GetTranslation(DependencyObject obj)
123+
{
124+
if (!DesignTimeHelpers.IsRunningInLegacyDesignerMode && obj is UIElement element)
125+
{
126+
return GetTranslationForElement(element);
127+
}
128+
129+
return (string)obj.GetValue(TranslationProperty);
130+
}
131+
132+
/// <summary>
133+
/// Sets the <see cref="Visual.TransformMatrix"/>.<see cref="Matrix4x4.Translation"/> property of a <see cref="UIElement"/> in a <see cref="string"/> form.
134+
/// </summary>
135+
/// <param name="obj">The <see cref="DependencyObject"/> instance.</param>
136+
/// <param name="value">The <see cref="string"/> representation of the <see cref="Vector3"/> to be set.</param>
137+
public static void SetTranslation(DependencyObject obj, string value)
138+
{
139+
if (!DesignTimeHelpers.IsRunningInLegacyDesignerMode && obj is UIElement element)
140+
{
141+
SetTranslationForElement(value, element);
142+
}
143+
144+
obj.SetValue(TranslationProperty, value);
145+
}
146+
117147
/// <summary>
118148
/// Gets the <see cref="Visual.Opacity"/> of a UIElement
119149
/// </summary>
@@ -334,6 +364,12 @@ public static void SetNormalizedCenterPoint(DependencyObject obj, string value)
334364
public static readonly DependencyProperty OffsetProperty =
335365
DependencyProperty.RegisterAttached("Offset", typeof(string), typeof(VisualExtensions), new PropertyMetadata(null, OnOffsetChanged));
336366

367+
/// <summary>
368+
/// Identifies the Translation attached property.
369+
/// </summary>
370+
public static readonly DependencyProperty TranslationProperty =
371+
DependencyProperty.RegisterAttached("Translation", typeof(string), typeof(VisualExtensions), new PropertyMetadata(null, OnTranslationChanged));
372+
337373
/// <summary>
338374
/// Identifies the Opacity attached property.
339375
/// </summary>
@@ -400,6 +436,14 @@ private static void OnOffsetChanged(DependencyObject d, DependencyPropertyChange
400436
}
401437
}
402438

439+
private static void OnTranslationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
440+
{
441+
if (e.NewValue is string str)
442+
{
443+
SetTranslation(d, str);
444+
}
445+
}
446+
403447
private static void OnOpacityChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
404448
{
405449
if (e.NewValue is double dbl)
@@ -503,6 +547,23 @@ private static void SetOffsetForElement(string value, UIElement element)
503547
visual.Offset = value.ToVector3();
504548
}
505549

550+
private static string GetTranslationForElement(UIElement element)
551+
{
552+
return GetVisual(element).TransformMatrix.Translation.ToString();
553+
}
554+
555+
private static void SetTranslationForElement(string value, UIElement element)
556+
{
557+
ElementCompositionPreview.SetIsTranslationEnabled(element, true);
558+
559+
Visual visual = GetVisual(element);
560+
Matrix4x4 transform = visual.TransformMatrix;
561+
562+
transform.Translation = value.ToVector3();
563+
564+
visual.TransformMatrix = transform;
565+
}
566+
506567
private static double GetOpacityForElement(UIElement element)
507568
{
508569
var visual = GetVisual(element);

0 commit comments

Comments
 (0)