-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
Shadow.cs
91 lines (77 loc) · 3.27 KB
/
Shadow.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Documents;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Microsoft.Graphics.Canvas;
using Microsoft.Graphics.Canvas.Effects;
using Microsoft.Graphics.Canvas.UI.Xaml;
namespace MaterialDesignThemes.Uwp
{
[TemplatePart(Name = CanvasControlPartName, Type = typeof(CanvasControl))]
[TemplatePart(Name = ContentPresenterPartName, Type = typeof(ContentPresenter))]
public sealed class Shadow : ContentControl
{
private CanvasControl _canvasControl;
private ContentPresenter _contentPresenter;
private const string ContentPresenterPartName = "PART_ContentPresenter";
private const string CanvasControlPartName = "PART_CanvasControl";
public Shadow()
{
DefaultStyleKey = typeof(Shadow);
Unloaded += OnUnloaded;
}
private void OnUnloaded(object sender, RoutedEventArgs routedEventArgs)
{
if (_canvasControl == null) return;
_canvasControl.RemoveFromVisualTree();
_canvasControl = null;
}
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
_canvasControl = GetTemplateChild(CanvasControlPartName) as CanvasControl;
if (_canvasControl != null)
_canvasControl.Draw += Draw;
_contentPresenter = GetTemplateChild(ContentPresenterPartName) as ContentPresenter;
}
private void Draw(CanvasControl sender, CanvasDrawEventArgs args)
{
if (_contentPresenter == null) return;
var border = VisualTreeHelper.GetChild(_contentPresenter, 0) as Border;
if (border == null) return;
var borderPoint = border.TransformToVisual(this).TransformPoint(new Point(0, 0));
var cl = new CanvasCommandList(sender);
using (var clds = cl.CreateDrawingSession())
{
clds.FillRoundedRectangle(new Rect(borderPoint.X, borderPoint.Y, border.ActualWidth, border.ActualHeight), (float)border.CornerRadius.TopLeft, (float)border.CornerRadius.TopLeft, Color.FromArgb(128, 0, 0, 0));
}
var shadowEffect = new Transform2DEffect
{
Source =
new Transform2DEffect
{
Source = new ShadowEffect
{
BlurAmount = 2,
ShadowColor = Color.FromArgb(160, 0, 0, 0),
Source = cl
},
//TODO not doing any scaling right now, confirm with larger shadows
TransformMatrix = Matrix3x2.CreateScale(1.0f, new Vector2((float)(border.ActualWidth / 2), ((float)border.ActualHeight / 2)))
},
TransformMatrix = Matrix3x2.CreateTranslation(0, 1)
};
args.DrawingSession.DrawImage(shadowEffect);
// args.DrawingSession.DrawImage(cl);
}
}
}