-
Notifications
You must be signed in to change notification settings - Fork 746
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Break down elevation implementation to platform-specific files
- Loading branch information
1 parent
a11300f
commit 1b2e8df
Showing
7 changed files
with
125 additions
and
268 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace Uno.UI.Helpers; | ||
|
||
internal static class ElevationHelper | ||
{ | ||
internal static void SetElevation(DependencyObject element, double elevation, Color shadowColor) | ||
{ | ||
if (element is not Android.Views.View view) | ||
{ | ||
return; | ||
} | ||
|
||
AndroidX.Core.View.ViewCompat.SetElevation(view, (float)Uno.UI.ViewHelper.LogicalToPhysicalPixels(elevation)); | ||
if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.P) | ||
{ | ||
view.SetOutlineAmbientShadowColor(shadowColor); | ||
view.SetOutlineSpotShadowColor(shadowColor); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
namespace Uno.UI.Helpers; | ||
|
||
#if __IOS__ | ||
using _View = UIKit.UIView; | ||
#else | ||
using _View = AppKit.NSView; | ||
#endif | ||
|
||
internal static class ElevationHelper | ||
{ | ||
internal static void SetElevation(DependencyObject element, double elevation, Color shadowColor, CGPath path = null) | ||
{ | ||
if (element is not _View view) | ||
{ | ||
return; | ||
} | ||
|
||
if (elevation > 0) | ||
{ | ||
// 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 = shadowColor.A / 255f; | ||
#if __MACOS__ | ||
view.Layer.ShadowColor = AppKit.NSColor.FromRgb(shadowColor.R, shadowColor.G, shadowColor.B).CGColor; | ||
#else | ||
view.Layer.ShadowColor = UIKit.UIColor.FromRGB(shadowColor.R, shadowColor.G, shadowColor.B).CGColor; | ||
#endif | ||
view.Layer.ShadowRadius = (nfloat)(blur * elevation); | ||
view.Layer.ShadowOffset = new CoreGraphics.CGSize(x * elevation, y * elevation); | ||
view.Layer.ShadowPath = path; | ||
} | ||
else if (view.Layer != null) | ||
{ | ||
view.Layer.ShadowOpacity = 0; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Uno.UI.Composition.Composition; | ||
using Windows.UI; | ||
using Windows.UI.Xaml; | ||
|
||
namespace Uno.UI.Helpers; | ||
|
||
internal static class ElevationHelper | ||
{ | ||
internal static void SetElevation(DependencyObject element, double elevation, Color shadowColor) | ||
{ | ||
if (element is not UIElement uiElement) | ||
{ | ||
return; | ||
} | ||
|
||
var visual = uiElement.Visual; | ||
|
||
const float SHADOW_SIGMA_X_MODIFIER = 1f / 3.5f; | ||
const float SHADOW_SIGMA_Y_MODIFIER = 1f / 3.5f; | ||
float x = 0.3f; | ||
float y = 0.92f * 0.5f; | ||
|
||
var dx = (float)elevation * x; | ||
var dy = (float)elevation * y; | ||
var sigmaX = (float)elevation * SHADOW_SIGMA_X_MODIFIER; | ||
var sigmaY = (float)elevation * SHADOW_SIGMA_Y_MODIFIER; | ||
var shadow = new ShadowState(dx, dy, sigmaX, sigmaY, shadowColor); | ||
visual.ShadowState = shadow; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
namespace Uno.UI.Helpers; | ||
|
||
internal static class ElevationHelper | ||
{ | ||
internal static void SetElevation(DependencyObject element, double elevation, Color shadowColor) | ||
{ | ||
if (element is UIElement uiElement) | ||
{ | ||
return; | ||
} | ||
|
||
if (elevation > 0) | ||
{ | ||
// Values for 1dp elevation according to https://material.io/guidelines/resources/shadows.html#shadows-illustrator | ||
const double x = 0.25d; | ||
const double y = 0.92f * 0.5f; // Looks more accurate than the recommended 0.92f. | ||
const double blur = 0.5f; | ||
var color = Color.FromArgb((byte)(shadowColor.A * .35), shadowColor.R, shadowColor.G, shadowColor.B); | ||
|
||
var str = $"{(x * elevation).ToStringInvariant()}px {(y * elevation).ToStringInvariant()}px {(blur * elevation).ToStringInvariant()}px {color.ToCssString()}"; | ||
uiElement.SetStyle("box-shadow", str); | ||
uiElement.SetCssClasses("noclip"); | ||
} | ||
else | ||
{ | ||
uiElement.ResetStyle("box-shadow"); | ||
uiElement.UnsetCssClasses("noclip"); | ||
} | ||
} | ||
} |
Oops, something went wrong.