DO NOT USE: Bug 1271428 - Implement ClippedCardView draft
authorAndrzej Hunt <andrzej@ahunt.org>
Mon, 09 May 2016 15:23:30 -0700
changeset 365565 655c136c0417af9dcf129bcf9b66e92ccdc898e6
parent 365564 1910bee3c6649ba7a53ad48cc9335afda8f243c9
child 520600 148aa17caed932b3cbc8c84ae798d62e2f8c7aae
push id17789
push userbmo:ahunt@mozilla.com
push dateWed, 11 May 2016 03:29:35 +0000
bugs1271428
milestone49.0a1
DO NOT USE: Bug 1271428 - Implement ClippedCardView Brings rounded corner goodness to API < 21. We don't want to use this for performance reasons, this patch is posted primarily for reference. MozReview-Commit-ID: EcAWrCJPxyW
mobile/android/base/java/org/mozilla/gecko/ClippedCardView.java
mobile/android/base/java/org/mozilla/gecko/menu/MenuPopup.java
mobile/android/base/resources/layout/menu_popup.xml
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/java/org/mozilla/gecko/ClippedCardView.java
@@ -0,0 +1,74 @@
+/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+package org.mozilla.gecko;
+
+import android.content.Context;
+import android.graphics.Canvas;
+import android.graphics.Path;
+import android.graphics.Rect;
+import android.graphics.RectF;
+import android.os.Build;
+import android.support.v7.widget.CardView;
+import android.util.AttributeSet;
+import android.view.View;
+
+/**
+ * A version of CardView which clips its contents to not overlap the CardView corners.
+ * This is useful primarily for CardViews that contains items with a solid background that will
+ * touch any corner of the CardView.
+ *
+ * By default in pre-21 versions CardView adds padding to ensure that the corners don't need clipping,
+ * this can be disabled but results in solid contents overlapping the corners. We therefore need to
+ * clip the corners ourselves.
+ *
+ * If/when we build with API >=21 we can remove this class, since that results in the use
+ * of CardViewApi21 which correctly clips content:
+ * http://androidxref.com/6.0.1_r10/xref/frameworks/support/v7/cardview/src/android/support/v7/widget/CardView.java#75
+ * and
+ * http://androidxref.com/6.0.1_r10/xref/frameworks/support/v7/cardview/api21/android/support/v7/widget/CardViewApi21.java#29
+ */
+public class ClippedCardView extends CardView {
+    private Path clipPath = new Path();
+    private boolean clipPathIsStale = true;
+
+    public ClippedCardView(Context context, AttributeSet attrs) {
+        super(context, attrs);
+//        setWillNotDraw(false);
+    }
+
+    @Override
+    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
+        super.onSizeChanged(w, h, oldw, oldh);
+
+        clipPathIsStale = true;
+    }
+
+    @Override
+    protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
+        if (clipPathIsStale) {
+            final Rect childRectangle = new Rect();
+            child.getGlobalVisibleRect(childRectangle);
+
+            float radius = getRadius();
+
+            clipPath.reset();
+            clipPath.addRoundRect(new RectF(childRectangle),
+                    radius, radius,
+                    Path.Direction.CW);
+
+            clipPathIsStale = false;
+        }
+
+        final int flag = canvas.save();
+        canvas.clipPath(clipPath);
+
+        final boolean result = super.drawChild(canvas, child, drawingTime);
+
+        canvas.restoreToCount(flag);
+
+        return result;
+    }
+}
--- a/mobile/android/base/java/org/mozilla/gecko/menu/MenuPopup.java
+++ b/mobile/android/base/java/org/mozilla/gecko/menu/MenuPopup.java
@@ -37,28 +37,16 @@ public class MenuPopup extends PopupWind
         setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
         setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT,
                             ViewGroup.LayoutParams.WRAP_CONTENT);
 
         LayoutInflater inflater = LayoutInflater.from(context);
         mPanel = (CardView) inflater.inflate(R.layout.menu_popup, null);
         setContentView(mPanel);
 
-        // Disable corners on < lollipop:
-        // CardView only supports clipping content on API >= 21 (for performance reasons). Without
-        // content clipping the "action bar" will look ugly because it has its own background:
-        // by default there's a 2px white edge along the top and sides (i.e. an inset corresponding
-        // to the corner radius), if we disable the inset then the corners overlap.
-        // It's possible to implement custom clipping, however given that the support library
-        // chose not to support this for performance reasons, we too have chosen to just disable
-        // corners on < 21, see Bug 1271428.
-        if (Build.VERSION.SDK_INT < 21) {
-            mPanel.setRadius(0);
-        }
-
         setAnimationStyle(R.style.PopupAnimation);
     }
 
     /**
      * Adds the panel with the menu to its content.
      *
      * @param view The panel view with the menu to be shown.
      */
--- a/mobile/android/base/resources/layout/menu_popup.xml
+++ b/mobile/android/base/resources/layout/menu_popup.xml
@@ -1,18 +1,19 @@
 <?xml version="1.0" encoding="utf-8"?>
 <!-- This Source Code Form is subject to the terms of the Mozilla Public
    - License, v. 2.0. If a copy of the MPL was not distributed with this
    - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
 
-<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
+<org.mozilla.gecko.ClippedCardView xmlns:android="http://schemas.android.com/apk/res/android"
               xmlns:app="http://schemas.android.com/apk/res-auto"
               android:id="@+id/menu_panel"
               android:layout_width="@dimen/menu_popup_width"
               android:layout_height="wrap_content"
               android:layout_alignParentRight="true"
               android:minWidth="@dimen/menu_popup_width"
               app:cardBackgroundColor="@color/toolbar_grey"
-              app:cardUseCompatPadding="true">
+              app:cardUseCompatPadding="true"
+              app:cardPreventCornerOverlap="false">
 
     <!-- MenuPanel will be added here dynamically -->
 
-</android.support.v7.widget.CardView>
\ No newline at end of file
+</org.mozilla.gecko.ClippedCardView>
\ No newline at end of file