Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7ae64ffb authored by Jon Miranda's avatar Jon Miranda
Browse files

Scale widgets in multi-window mode.

To keep this CL small and focused, I'm going to create
a separate CL that handles the scaling for the widget in
drag and drop mode.

Bug: 32176631
Change-Id: Id6557d070edb664aa1f4851de7abf494cf8a0677
parent feba90fe
Loading
Loading
Loading
Loading
+5 −4
Original line number Diff line number Diff line
@@ -355,10 +355,11 @@ public class AppWidgetResizeFrame extends FrameLayout

    public void snapToWidget(boolean animate) {
        final DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
        int newWidth = mWidgetView.getWidth() + 2 * mBackgroundPadding
                - mWidgetPadding.left - mWidgetPadding.right;
        int newHeight = mWidgetView.getHeight() + 2 * mBackgroundPadding
                - mWidgetPadding.top - mWidgetPadding.bottom;
        DeviceProfile profile = mLauncher.getDeviceProfile();
        int newWidth = (int) (mWidgetView.getWidth() * profile.appWidgetScale.x)
                + 2 * mBackgroundPadding - mWidgetPadding.left - mWidgetPadding.right;
        int newHeight = (int) (mWidgetView.getHeight() * profile.appWidgetScale.y)
                + 2 * mBackgroundPadding - mWidgetPadding.top - mWidgetPadding.bottom;

        mTmpPt[0] = mWidgetView.getLeft();
        mTmpPt[1] = mWidgetView.getTop();
+14 −2
Original line number Diff line number Diff line
@@ -2690,6 +2690,18 @@ public class CellLayout extends ViewGroup implements BubbleTextShadowHandler {
        }

        public void setup(int cellWidth, int cellHeight, boolean invertHorizontally, int colCount) {
            setup(cellWidth, cellHeight, invertHorizontally, colCount, 1.0f, 1.0f);
        }

        /**
         * Use this method, as opposed to {@link #setup(int, int, boolean, int)}, if the view needs
         * to be scaled.
         *
         * ie. In multi-window mode, we setup widgets so that they are measured and laid out
         * using their full/invariant device profile sizes.
         */
        public void setup(int cellWidth, int cellHeight, boolean invertHorizontally, int colCount,
                float cellScaleX, float cellScaleY) {
            if (isLockedToGrid) {
                final int myCellHSpan = cellHSpan;
                final int myCellVSpan = cellVSpan;
@@ -2700,8 +2712,8 @@ public class CellLayout extends ViewGroup implements BubbleTextShadowHandler {
                    myCellX = colCount - myCellX - cellHSpan;
                }

                width = myCellHSpan * cellWidth - leftMargin - rightMargin;
                height = myCellVSpan * cellHeight - topMargin - bottomMargin;
                width = (int) (myCellHSpan * cellWidth / cellScaleX - leftMargin - rightMargin);
                height = (int) (myCellVSpan * cellHeight / cellScaleY - topMargin - bottomMargin);
                x = (myCellX * cellWidth + leftMargin);
                y = (myCellY * cellHeight + topMargin);
            }
+10 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.content.ComponentName;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.Gravity;
@@ -123,6 +124,9 @@ public class DeviceProfile {
    public int allAppsIconDrawablePaddingPx;
    public float allAppsIconTextSizePx;

    // Widgets
    public final PointF appWidgetScale = new PointF(1.0f, 1.0f);

    // Drop Target
    public int dropTargetBarSizePx;

@@ -220,6 +224,12 @@ public class DeviceProfile {
        // The nav bar is black so we add bottom padding to visually center hotseat icons.
        profile.hotseatBarBottomPaddingPx = profile.hotseatBarTopPaddingPx;

        // We use these scales to measure and layout the widgets using their full invariant profile
        // sizes and then draw them scaled and centered to fit in their multi-window mode cellspans.
        float appWidgetScaleX = (float) profile.getCellSize().x / getCellSize().x;
        float appWidgetScaleY = (float) profile.getCellSize().y / getCellSize().y;
        profile.appWidgetScale.set(appWidgetScaleX, appWidgetScaleY);

        return profile;
    }

+23 −7
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.launcher3;

import android.app.WallpaperManager;
import android.content.Context;
import android.graphics.PointF;
import android.graphics.Rect;
import android.support.annotation.IntDef;
import android.view.View;
@@ -120,20 +121,20 @@ public class ShortcutAndWidgetContainer extends ViewGroup {
    }

    public void measureChild(View child) {
        final DeviceProfile grid = mLauncher.getDeviceProfile();
        final int cellWidth = mCellWidth;
        final int cellHeight = mCellHeight;
        CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
        if (!lp.isFullscreen) {
            lp.setup(cellWidth, cellHeight, invertLayoutHorizontally(), mCountX);
            final DeviceProfile profile = mLauncher.getDeviceProfile();

            if (child instanceof LauncherAppWidgetHostView) {
                // Widgets have their own padding, so skip
                lp.setup(mCellWidth, mCellHeight, invertLayoutHorizontally(), mCountX,
                        profile.appWidgetScale.x, profile.appWidgetScale.y);
                // Widgets have their own padding
            } else {
                // Otherwise, center the icon/folder
                lp.setup(mCellWidth, mCellHeight, invertLayoutHorizontally(), mCountX);
                // Center the icon/folder
                int cHeight = getCellContentHeight();
                int cellPaddingY = (int) Math.max(0, ((lp.height - cHeight) / 2f));
                int cellPaddingX = (int) (grid.edgeMarginPx / 2f);
                int cellPaddingX = (int) (profile.edgeMarginPx / 2f);
                child.setPadding(cellPaddingX, cellPaddingY, cellPaddingX, 0);
            }
        } else {
@@ -158,6 +159,21 @@ public class ShortcutAndWidgetContainer extends ViewGroup {
            final View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();

                if (child instanceof LauncherAppWidgetHostView) {
                    // Scale and center the widget to fit within its cells.
                    DeviceProfile profile = mLauncher.getDeviceProfile();
                    float scaleX = profile.appWidgetScale.x;
                    float scaleY = profile.appWidgetScale.y;

                    float scale = Math.min(scaleX, scaleY);
                    child.setScaleX(scale);
                    child.setScaleY(scale);

                    child.setTranslationX(-(lp.width - (lp.width * scaleX)) / 2.0f);
                    child.setTranslationY(-(lp.height - (lp.height * scaleY)) / 2.0f);
                }

                int childLeft = lp.x;
                int childTop = lp.y;
                child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height);