MCPcopy Create free account
hub / github.com/NativeScript/NativeScript / drawBoxShadow

Function drawBoxShadow

packages/core/ui/styling/background.ios.ts:1074–1167  ·  view source on GitHub ↗
(view: View)

Source from the content-addressed store, hash-verified

1072}
1073
1074function drawBoxShadow(view: View): void {
1075 const background = view.style.backgroundInternal;
1076 const nativeView = <NativeScriptUIView>view.nativeViewProtected;
1077 const layer = nativeView.layer;
1078
1079 // There is no parent to add shadow to
1080 if (!layer.superlayer) {
1081 return;
1082 }
1083
1084 const bounds = nativeView.bounds;
1085 const boxShadows: BoxShadow[] = background.getBoxShadows();
1086
1087 // Initialize outer shadows
1088 let shadowGroupLayer: CALayer;
1089
1090 if (nativeView.outerShadowContainerLayer) {
1091 shadowGroupLayer = nativeView.outerShadowContainerLayer;
1092 } else {
1093 shadowGroupLayer = CALayer.new();
1094
1095 // Instead of nesting it, add shadow container layer underneath view so that it's not affected by border masking
1096 layer.superlayer.insertSublayerBelow(shadowGroupLayer, layer);
1097 nativeView.outerShadowContainerLayer = shadowGroupLayer;
1098 }
1099
1100 // Check if the number of shadow layers is correct
1101 if (!shadowGroupLayer.sublayers || boxShadows.length > shadowGroupLayer.sublayers.count) {
1102 const length = shadowGroupLayer.sublayers ? boxShadows.length - shadowGroupLayer.sublayers.count : boxShadows.length;
1103
1104 for (let i = 0; i < length; i++) {
1105 const shadowLayer = CALayer.new();
1106 // This mask is necessary to maintain transparent background
1107 const maskLayer = CAShapeLayer.new();
1108
1109 maskLayer.fillRule = kCAFillRuleEvenOdd;
1110 shadowLayer.mask = maskLayer;
1111 shadowGroupLayer.addSublayer(shadowLayer);
1112 }
1113 } else if (shadowGroupLayer.sublayers && boxShadows.length < shadowGroupLayer.sublayers.count) {
1114 for (let i = 0, length = shadowGroupLayer.sublayers.count - boxShadows.length; i < length; i++) {
1115 shadowGroupLayer.sublayers[i].removeFromSuperlayer();
1116 }
1117 }
1118
1119 if (shadowGroupLayer.sublayers?.count) {
1120 for (let i = 0, count = shadowGroupLayer.sublayers.count; i < count; i++) {
1121 const shadowLayer = shadowGroupLayer.sublayers[i];
1122 const boxShadow = boxShadows[i];
1123 const shadowRadius = layout.toDeviceIndependentPixels(boxShadow.blurRadius);
1124 const offsetX = layout.toDeviceIndependentPixels(boxShadow.offsetX);
1125 const offsetY = layout.toDeviceIndependentPixels(boxShadow.offsetY);
1126 const { maskPath, shadowPath } = ios.generateShadowLayerPaths(view, boxShadow, bounds);
1127
1128 shadowLayer.allowsEdgeAntialiasing = true;
1129 shadowLayer.contentsScale = Screen.mainScreen.scale;
1130
1131 // Shadow opacity is handled on the shadow's color instance

Callers 1

Calls 2

getBoxShadowsMethod · 0.80
newMethod · 0.45

Tested by

no test coverage detected