UNPKG

@nativescript-community/ui-neumorphiclayout

Version:
109 lines 4.54 kB
import { backgroundInternalProperty, borderTopColorProperty, borderTopLeftRadiusProperty, borderTopWidthProperty, LayoutBase, Screen } from '@nativescript/core'; import { darkShadowColorProperty, lightShadowColorProperty, NeumorphicCanvas, neumorphismProperty, shadowDistanceProperty, shadowRadiusProperty } from './common'; export * from './common'; var NeumorphicDrawable = /** @class */ (function (_super) { __extends(NeumorphicDrawable, _super); function NeumorphicDrawable(canvas) { var _this = _super.call(this) || this; _this.mCanvasRef = new WeakRef(canvas); return _this; } NeumorphicDrawable.prototype.draw = function (nativeCanvas) { var canvas = this.mCanvasRef && this.mCanvasRef.get(); if (canvas != null) { var scale = Screen.mainScreen.scale; nativeCanvas.save(); nativeCanvas.scale(scale, scale); // always scale to device density to work with dip canvas.mNative = nativeCanvas; canvas.onDraw(); nativeCanvas.restore(); } }; NeumorphicDrawable.prototype.getAugmentedCanvas = function () { return this.mCanvasRef && this.mCanvasRef.get(); }; NeumorphicDrawable.prototype.getOpacity = function () { return android.graphics.PixelFormat.TRANSLUCENT; }; NeumorphicDrawable.prototype.setColorFilter = function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } if (args.length === 2) { _super.prototype.setColorFilter.call(this, args[0], args[1]); } else { var canvas = this.mCanvasRef && this.mCanvasRef.get(); if (canvas) { canvas.getBasePaint().setColorFilter(args[0]); } } }; return NeumorphicDrawable; }(android.graphics.drawable.Drawable)); function _getNeumorphicDrawable(view) { const background = view.nativeViewProtected.getBackground(); if (background == null || !(background instanceof NeumorphicDrawable)) { return null; } return background; } function _refresh() { const drawable = _getNeumorphicDrawable(this); if (drawable != null) { drawable.invalidateSelf(); } } function _toggleViewClipping(view, clipChildren) { const nativeView = view?.nativeViewProtected; // Removing clipping from native views that extend org.nativescript.widgets.LayoutBase (this includes ContentView) if (nativeView != null && nativeView instanceof org.nativescript.widgets.LayoutBase) { nativeView.setClipChildren(clipChildren); nativeView.setClipToPadding(clipChildren); } } function _updateNeumorphismState(value) { const drawable = _getNeumorphicDrawable(this); const nativeView = this.nativeViewProtected; if (value) { if (drawable != null) { drawable.invalidateSelf(); } else { const canvas = new NeumorphicCanvas(this); // Keep neumorphic canvas as a reference to prevent GC from getting rid of it this.augmentedCanvas = canvas; _toggleViewClipping(this.parent, false); nativeView.setBackground(new NeumorphicDrawable(canvas)); } } else { if (this.augmentedCanvas != null) { delete this.augmentedCanvas; } if (drawable != null) { _toggleViewClipping(this.parent, true); nativeView.setBackground(null); } } } // Disable 'backgroundInternal' as it also uses 'setBackground' to apply a drawable const backgroundInternalOrigin = LayoutBase.prototype[backgroundInternalProperty.setNative]; LayoutBase.prototype[borderTopColorProperty.setNative] = _refresh; LayoutBase.prototype[borderTopWidthProperty.setNative] = _refresh; LayoutBase.prototype[borderTopLeftRadiusProperty.setNative] = _refresh; LayoutBase.prototype[lightShadowColorProperty.setNative] = _refresh; LayoutBase.prototype[darkShadowColorProperty.setNative] = _refresh; LayoutBase.prototype[shadowDistanceProperty.setNative] = _refresh; LayoutBase.prototype[shadowRadiusProperty.setNative] = _refresh; LayoutBase.prototype[neumorphismProperty.setNative] = _updateNeumorphismState; LayoutBase.prototype[backgroundInternalProperty.setNative] = function (value) { if (this.neumorphism) { _refresh.call(this); } else { backgroundInternalOrigin.call(this, value); } }; //# sourceMappingURL=index.android.js.map