From a2e1499a2fac8e7a3848d7efff8d130dfe4bea5d Mon Sep 17 00:00:00 2001 From: Pat Manning Date: Fri, 22 Apr 2022 11:29:17 +0100 Subject: [PATCH] Move spring loaded scale calculation to DeviceProfile. Fix: 229838395 Test: manual Change-Id: I6dbbc850e88aaacceb1363e342404b06104f8c10 --- src/com/android/launcher3/DeviceProfile.java | 37 +++++++++++++++---- src/com/android/launcher3/DropTargetBar.java | 6 +-- .../launcher3/states/SpringLoadedState.java | 11 +----- 3 files changed, 33 insertions(+), 21 deletions(-) diff --git a/src/com/android/launcher3/DeviceProfile.java b/src/com/android/launcher3/DeviceProfile.java index 33bb0a5eb9..6728f90af0 100644 --- a/src/com/android/launcher3/DeviceProfile.java +++ b/src/com/android/launcher3/DeviceProfile.java @@ -658,7 +658,7 @@ public class DeviceProfile { Point workspacePadding = getTotalWorkspacePadding(); // Check to see if the icons fit within the available height. - float usedHeight = getCellLayoutHeight(); + float usedHeight = getCellLayoutHeightSpecification(); final int maxHeight = getWorkspaceHeight(workspacePadding); float extraHeight = Math.max(0, maxHeight - usedHeight); float scaleY = maxHeight / usedHeight; @@ -669,7 +669,8 @@ public class DeviceProfile { // We scale to fit the cellWidth and cellHeight in the available space. // The benefit of scalable grids is that we can get consistent aspect ratios between // devices. - float usedWidth = getCellLayoutWidth() + (desiredWorkspaceHorizontalMarginPx * 2); + float usedWidth = + getCellLayoutWidthSpecification() + (desiredWorkspaceHorizontalMarginPx * 2); // We do not subtract padding here, as we also scale the workspace padding if needed. scaleX = availableWidthPx / usedWidth; shouldScale = true; @@ -678,19 +679,19 @@ public class DeviceProfile { if (shouldScale) { float scale = Math.min(scaleX, scaleY); updateIconSize(scale, res); - extraHeight = Math.max(0, maxHeight - getCellLayoutHeight()); + extraHeight = Math.max(0, maxHeight - getCellLayoutHeightSpecification()); } updateAvailableFolderCellDimensions(res); return Math.round(extraHeight); } - private int getCellLayoutHeight() { + private int getCellLayoutHeightSpecification() { return (cellHeightPx * inv.numRows) + (cellLayoutBorderSpacePx.y * (inv.numRows - 1)) + cellLayoutPaddingPx.top + cellLayoutPaddingPx.bottom; } - private int getCellLayoutWidth() { + private int getCellLayoutWidthSpecification() { int numColumns = isTwoPanels ? inv.numColumns * 2 : inv.numColumns; return (cellWidthPx * numColumns) + (cellLayoutBorderSpacePx.x * (numColumns - 1)) + cellLayoutPaddingPx.left + cellLayoutPaddingPx.right; @@ -920,7 +921,7 @@ public class DeviceProfile { /** * Gets the scaled bottom of the workspace in px for the spring-loaded edit state. */ - public float getWorkspaceSpringLoadShrunkBottom() { + private float getWorkspaceSpringLoadShrunkBottom() { int topOfHotseat = hotseatBarSizePx + springLoadedHotseatBarTopMarginPx; workspaceSpringLoadShrunkBottom = heightPx - (isVerticalBarLayout() ? getVerticalHotseatLastItemBottomOffset() @@ -931,10 +932,30 @@ public class DeviceProfile { /** * Gets the minimum visible amount of the next workspace page when in the spring-loaded state. */ - public float getWorkspaceSpringLoadedMinimumNextPageVisible() { + private float getWorkspaceSpringLoadedMinimumNextPageVisible() { return getCellSize().x / 2f; } + /** + * Gets the scale of the workspace for the spring-loaded edit state. + */ + public float getWorkspaceSpringLoadScale() { + float cellLayoutHeight = availableHeightPx - workspacePadding.top - workspacePadding.bottom; + float scale = (getWorkspaceSpringLoadShrunkBottom() - getWorkspaceSpringLoadShrunkTop()) + / cellLayoutHeight; + scale = Math.min(scale, 1f); + + // Reduce scale if next pages would not be visible after scaling the workspace + int workspaceWidth = getWorkspaceWidth(); + float scaledWorkspaceWidth = workspaceWidth * scale; + float maxAvailableWidth = + workspaceWidth - (2 * getWorkspaceSpringLoadedMinimumNextPageVisible()); + if (scaledWorkspaceWidth > maxAvailableWidth) { + scale *= maxAvailableWidth / scaledWorkspaceWidth; + } + return scale; + } + public int getWorkspaceWidth() { return getWorkspaceWidth(getTotalWorkspacePadding()); } @@ -1344,6 +1365,8 @@ public class DeviceProfile { prefix + pxToDpStr("workspaceSpringLoadShrunkTop", workspaceSpringLoadShrunkTop)); writer.println(prefix + pxToDpStr("workspaceSpringLoadShrunkBottom", workspaceSpringLoadShrunkBottom)); + writer.println( + prefix + pxToDpStr("getWorkspaceSpringLoadScale()", getWorkspaceSpringLoadScale())); } private static Context getContext(Context c, Info info, int orientation, WindowBounds bounds) { diff --git a/src/com/android/launcher3/DropTargetBar.java b/src/com/android/launcher3/DropTargetBar.java index ec3629de5e..2e3f26c7d0 100644 --- a/src/com/android/launcher3/DropTargetBar.java +++ b/src/com/android/launcher3/DropTargetBar.java @@ -237,10 +237,8 @@ public class DropTargetBar extends FrameLayout rightButton.getMeasuredHeight()); } else if (dp.isPhone) { // Buttons aligned to outer edges of scaled workspace. - float shrunkTop = dp.getWorkspaceSpringLoadShrunkTop(); - float shrunkBottom = dp.getWorkspaceSpringLoadShrunkBottom(); - float scale = - (shrunkBottom - shrunkTop) / launcher.getWorkspace().getNormalChildHeight(); + float scale = dp.getWorkspaceSpringLoadScale(); + int workspaceWidth = (int) (launcher.getWorkspace().getNormalChildWidth() * scale); int start = barCenter - (workspaceWidth / 2); int end = barCenter + (workspaceWidth / 2); diff --git a/src/com/android/launcher3/states/SpringLoadedState.java b/src/com/android/launcher3/states/SpringLoadedState.java index 7e9d56d858..e311bc8740 100644 --- a/src/com/android/launcher3/states/SpringLoadedState.java +++ b/src/com/android/launcher3/states/SpringLoadedState.java @@ -52,16 +52,7 @@ public class SpringLoadedState extends LauncherState { } float shrunkTop = grid.getWorkspaceSpringLoadShrunkTop(); - float shrunkBottom = grid.getWorkspaceSpringLoadShrunkBottom(); - float scale = Math.min((shrunkBottom - shrunkTop) / ws.getNormalChildHeight(), 1f); - - // Reduce scale if next pages would not be visible after scaling the workspace - float scaledWorkspaceWidth = ws.getWidth() * scale; - float maxAvailableWidth = - ws.getWidth() - (2 * grid.getWorkspaceSpringLoadedMinimumNextPageVisible()); - if (scaledWorkspaceWidth > maxAvailableWidth) { - scale *= maxAvailableWidth / scaledWorkspaceWidth; - } + float scale = grid.getWorkspaceSpringLoadScale(); float halfHeight = ws.getHeight() / 2; float myCenter = ws.getTop() + halfHeight;