Skip to content

Commit b52cd2d

Browse files
committed
Add OffsetBox
1 parent d2c22bd commit b52cd2d

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
using Microsoft.UI.Xaml.Input;
2+
3+
namespace DevWinUI;
4+
5+
public partial class OffsetBox : ContentControl
6+
{
7+
private Visual _visual;
8+
private Compositor _compositor;
9+
10+
public bool ShowShadowOnHover
11+
{
12+
get { return (bool)GetValue(ShowShadowOnHoverProperty); }
13+
set { SetValue(ShowShadowOnHoverProperty, value); }
14+
}
15+
16+
public static readonly DependencyProperty ShowShadowOnHoverProperty =
17+
DependencyProperty.Register(nameof(ShowShadowOnHover), typeof(bool), typeof(OffsetBox), new PropertyMetadata(true, OnShowShadowOnHoverChanged));
18+
19+
private static void OnShowShadowOnHoverChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
20+
{
21+
var ctl = (OffsetBox)d;
22+
if (ctl != null)
23+
{
24+
ctl.UpdateShowShadowOnHover();
25+
}
26+
}
27+
28+
public Vector3 HoverValue
29+
{
30+
get { return (Vector3)GetValue(HoverValueProperty); }
31+
set { SetValue(HoverValueProperty, value); }
32+
}
33+
34+
public static readonly DependencyProperty HoverValueProperty =
35+
DependencyProperty.Register(nameof(HoverValue), typeof(Vector3), typeof(OffsetBox), new PropertyMetadata(new Vector3(0, -5f, 0)));
36+
37+
public Vector3 NormalValue
38+
{
39+
get { return (Vector3)GetValue(NormalValueProperty); }
40+
set { SetValue(NormalValueProperty, value); }
41+
}
42+
43+
public static readonly DependencyProperty NormalValueProperty =
44+
DependencyProperty.Register(nameof(NormalValue), typeof(Vector3), typeof(OffsetBox), new PropertyMetadata(new Vector3(0, 0f, 0)));
45+
46+
public TimeSpan HoverDuration
47+
{
48+
get { return (TimeSpan)GetValue(HoverDurationProperty); }
49+
set { SetValue(HoverDurationProperty, value); }
50+
}
51+
52+
public static readonly DependencyProperty HoverDurationProperty =
53+
DependencyProperty.Register(nameof(HoverDuration), typeof(TimeSpan), typeof(OffsetBox), new PropertyMetadata(TimeSpan.FromMilliseconds(150)));
54+
55+
public TimeSpan NormalDuration
56+
{
57+
get { return (TimeSpan)GetValue(NormalDurationProperty); }
58+
set { SetValue(NormalDurationProperty, value); }
59+
}
60+
61+
public static readonly DependencyProperty NormalDurationProperty =
62+
DependencyProperty.Register(nameof(NormalDuration), typeof(TimeSpan), typeof(OffsetBox), new PropertyMetadata(TimeSpan.FromMilliseconds(150)));
63+
64+
public Vector3 HoverShadowTranslation
65+
{
66+
get { return (Vector3)GetValue(HoverShadowTranslationProperty); }
67+
set { SetValue(HoverShadowTranslationProperty, value); }
68+
}
69+
70+
public static readonly DependencyProperty HoverShadowTranslationProperty =
71+
DependencyProperty.Register(nameof(HoverShadowTranslation), typeof(Vector3), typeof(OffsetBox), new PropertyMetadata(new Vector3(0, 0, 32)));
72+
73+
public Vector3 NormalShadowTranslation
74+
{
75+
get { return (Vector3)GetValue(NormalShadowTranslationProperty); }
76+
set { SetValue(NormalShadowTranslationProperty, value); }
77+
}
78+
79+
public static readonly DependencyProperty NormalShadowTranslationProperty =
80+
DependencyProperty.Register(nameof(NormalShadowTranslation), typeof(Vector3), typeof(OffsetBox), new PropertyMetadata(new Vector3(0, 0, 0)));
81+
82+
protected override void OnApplyTemplate()
83+
{
84+
base.OnApplyTemplate();
85+
_visual = ElementCompositionPreview.GetElementVisual(this);
86+
87+
_compositor = _visual.Compositor;
88+
89+
if (Content is UIElement element && ShowShadowOnHover)
90+
{
91+
element.Shadow = new ThemeShadow();
92+
}
93+
}
94+
95+
protected override void OnContentChanged(object oldContent, object newContent)
96+
{
97+
base.OnContentChanged(oldContent, newContent);
98+
if (Content is UIElement element && ShowShadowOnHover)
99+
{
100+
element.Shadow = new ThemeShadow();
101+
}
102+
}
103+
104+
private void UpdateShowShadowOnHover()
105+
{
106+
if (Content is UIElement element && element.Shadow == null)
107+
{
108+
element.Shadow = new ThemeShadow();
109+
}
110+
}
111+
private void AnimateHover(bool hover)
112+
{
113+
if (_visual == null) return;
114+
115+
if (Content is UIElement element && ShowShadowOnHover)
116+
{
117+
element.Translation = hover ? HoverShadowTranslation : NormalShadowTranslation;
118+
}
119+
120+
var offsetAnim = _compositor.CreateVector3KeyFrameAnimation();
121+
offsetAnim.Target = "Offset";
122+
offsetAnim.InsertKeyFrame(1f, hover ? HoverValue : NormalValue, _compositor.CreateLinearEasingFunction());
123+
124+
offsetAnim.Duration = hover ? HoverDuration : NormalDuration;
125+
_visual.StartAnimation("Offset", offsetAnim);
126+
}
127+
128+
protected override void OnPointerEntered(PointerRoutedEventArgs e)
129+
{
130+
base.OnPointerEntered(e);
131+
AnimateHover(true);
132+
}
133+
134+
protected override void OnPointerExited(PointerRoutedEventArgs e)
135+
{
136+
base.OnPointerExited(e);
137+
AnimateHover(false);
138+
}
139+
}

0 commit comments

Comments
 (0)