Example #1
0
        public static void SetClipToOutline(this AView view, bool value, VisualElement element)
        {
            if (view.IsDisposed())
            {
                return;
            }

            var shouldClip = value;

            if (element is Frame frame)
            {
                shouldClip = frame.IsSet(Layout.IsClippedToBoundsProperty)
                                        ? frame.IsClippedToBounds : frame.CornerRadius > 0f;
            }

            if (view is FastRenderers.FrameRenderer && System.Maui.Maui.IsLollipopOrNewer)
            {
                view.SetClipToOutline(shouldClip);
                return;
            }

            // setClipBounds is only available in API 18 +
            if ((int)Build.VERSION.SdkInt >= 18)
            {
                if (!(view is ViewGroup viewGroup))
                {
                    return;
                }

                // Forms layouts should not impose clipping on their children
                viewGroup.SetClipChildren(false);

                // But if IsClippedToBounds is true, they _should_ enforce clipping at their own edges
                viewGroup.ClipBounds = shouldClip ? new Rect(0, 0, viewGroup.Width, viewGroup.Height) : null;
            }
            else
            {
                // For everything in 17 and below, use the setClipChildren method
                if (!(view.Parent is ViewGroup parent))
                {
                    return;
                }

                if ((int)Build.VERSION.SdkInt >= 18 && parent.ClipChildren == shouldClip)
                {
                    return;
                }

                parent.SetClipChildren(shouldClip);
                parent.Invalidate();
            }
        }