From adfc824e54f45dc44da00c71d8c4bdb7663f7012 Mon Sep 17 00:00:00 2001 From: Martin Zikmund Date: Wed, 14 Jul 2021 09:56:27 +0200 Subject: [PATCH] feat: ThemeShadow support on iOS/macOS --- .../Xaml/Media/ThemeShadowManager.iOSmacOS.cs | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/Uno.UI/UI/Xaml/Media/ThemeShadowManager.iOSmacOS.cs diff --git a/src/Uno.UI/UI/Xaml/Media/ThemeShadowManager.iOSmacOS.cs b/src/Uno.UI/UI/Xaml/Media/ThemeShadowManager.iOSmacOS.cs new file mode 100644 index 000000000000..68c71ad584f0 --- /dev/null +++ b/src/Uno.UI/UI/Xaml/Media/ThemeShadowManager.iOSmacOS.cs @@ -0,0 +1,54 @@ +using System; +using Windows.UI.Xaml; + +namespace Uno.UI.Xaml.Media +{ + internal static partial class ThemeShadowManager + { + static partial void UnsetShadow(UIElement uiElement) + { + var translation = uiElement.Translation; + +#if __MACOS__ + if (uiElement is AppKit.NSView view) +#else + if (uiElement is UIKit.UIView view) +#endif + { + view.Layer.ShadowOpacity = 0; + } + } + + static partial void SetShadow(UIElement uiElement) + { + var translation = uiElement.Translation; + +#if __MACOS__ + if (uiElement is AppKit.NSView view) +#else + if (uiElement is UIKit.UIView view) +#endif + { + // Values for 1dp elevation according to https://material.io/guidelines/resources/shadows.html#shadows-illustrator + const float x = 0.25f; + const float y = 0.92f * 0.5f; // Looks more accurate than the recommended 0.92f. + const float blur = 0.5f; + +#if __MACOS__ + view.WantsLayer = true; + view.Shadow ??= new AppKit.NSShadow(); +#endif + view.Layer.MasksToBounds = false; + view.Layer.ShadowOpacity = 1; +#if __MACOS__ + view.Layer.ShadowColor = AppKit.NSColor.Black.CGColor; +#else + view.Layer.ShadowColor = UIKit.UIColor.Black.CGColor; +#endif + view.Layer.ShadowRadius = (nfloat)(blur * translation.Z); + view.Layer.ShadowOffset = new CoreGraphics.CGSize(x * translation.Z, y * translation.Z); + //view.Layer.ShadowPath = path; - needed for rounded corners? + } + } + } +}