Skip to content

Commit adfc824

Browse files
committed
feat: ThemeShadow support on iOS/macOS
1 parent bfaca1f commit adfc824

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using Windows.UI.Xaml;
3+
4+
namespace Uno.UI.Xaml.Media
5+
{
6+
internal static partial class ThemeShadowManager
7+
{
8+
static partial void UnsetShadow(UIElement uiElement)
9+
{
10+
var translation = uiElement.Translation;
11+
12+
#if __MACOS__
13+
if (uiElement is AppKit.NSView view)
14+
#else
15+
if (uiElement is UIKit.UIView view)
16+
#endif
17+
{
18+
view.Layer.ShadowOpacity = 0;
19+
}
20+
}
21+
22+
static partial void SetShadow(UIElement uiElement)
23+
{
24+
var translation = uiElement.Translation;
25+
26+
#if __MACOS__
27+
if (uiElement is AppKit.NSView view)
28+
#else
29+
if (uiElement is UIKit.UIView view)
30+
#endif
31+
{
32+
// Values for 1dp elevation according to https://material.io/guidelines/resources/shadows.html#shadows-illustrator
33+
const float x = 0.25f;
34+
const float y = 0.92f * 0.5f; // Looks more accurate than the recommended 0.92f.
35+
const float blur = 0.5f;
36+
37+
#if __MACOS__
38+
view.WantsLayer = true;
39+
view.Shadow ??= new AppKit.NSShadow();
40+
#endif
41+
view.Layer.MasksToBounds = false;
42+
view.Layer.ShadowOpacity = 1;
43+
#if __MACOS__
44+
view.Layer.ShadowColor = AppKit.NSColor.Black.CGColor;
45+
#else
46+
view.Layer.ShadowColor = UIKit.UIColor.Black.CGColor;
47+
#endif
48+
view.Layer.ShadowRadius = (nfloat)(blur * translation.Z);
49+
view.Layer.ShadowOffset = new CoreGraphics.CGSize(x * translation.Z, y * translation.Z);
50+
//view.Layer.ShadowPath = path; - needed for rounded corners?
51+
}
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)