From 9a5941ab91c3b72e19a020e6ba82639082738612 Mon Sep 17 00:00:00 2001 From: Toni Barzic Date: Wed, 9 Oct 2024 03:52:57 +0000 Subject: [PATCH] Account for all apps offset during taskbar layout When laying out its contents, TaskBarView generally lays the icons so they get centered in the task bar - this is done by calculating the total size of all icons during layout (this will generally be transient taskbar icon sizes), and substracting extra margins that get removed from the divider view. After initial layout, `TaskbarViewController` adjusts the icon view positions to match expected icon sizes (it offsets icons horizontally reducing margins between icons to match intended icon sizes for the taskbar type). Additionally, `TaskbarViewController` translates all apps button container - this transformation is asymetric, and causes the task bar contents to become off-center. To account for this, update taskbar layout to reduce the total icon size used for centering icons by the amount all apps button is offset. This makes the taskbar off-center after initial layout, but by the amount by which all apps button is eventually offset (resulting in centered taskbar content). This alone worked for left-to-right UI direction, but not right-to-left. To fix this, correct the reference point from which TaskBarViewController offsets icon positions. The offset used to be calculated by distance of an icon index from half of the icon count - instead offset needs to be calculated relative to mid-index. For example, icon at index i needs to be offset by (mid_index - i) * difference_in_icon_size: ((n-1) / 2 - i) * diff). Code used to work for LTR UI layout because the list of icons contained invisible qsb view, which incresed both n and indices by 1, so (n' / 2 - i') * diff_in_icon_size worked fine, as it evaluated to ((n + 1) / 2 - i - 1) * diff = ((n - 1) / - i) * diff Bug: 372567501 Test: Verify that taskbar icons are centered when it needs to be, both with ltr and rtl language UI. Flag: EXEMPT bugfix Change-Id: Ic06873cc225a4361d9140d72c055db23f446a1ad --- .../launcher3/taskbar/TaskbarView.java | 61 ++++++++++++++++--- .../taskbar/TaskbarViewController.java | 49 ++++++++------- 2 files changed, 76 insertions(+), 34 deletions(-) diff --git a/quickstep/src/com/android/launcher3/taskbar/TaskbarView.java b/quickstep/src/com/android/launcher3/taskbar/TaskbarView.java index c0891a972d..3144fb737e 100644 --- a/quickstep/src/com/android/launcher3/taskbar/TaskbarView.java +++ b/quickstep/src/com/android/launcher3/taskbar/TaskbarView.java @@ -122,6 +122,8 @@ public class TaskbarView extends FrameLayout implements FolderIcon.FolderIconPar private final int mMaxNumIcons; + private final int mAllAppsButtonTranslationOffset; + public TaskbarView(@NonNull Context context) { this(context, null); } @@ -141,8 +143,6 @@ public class TaskbarView extends FrameLayout implements FolderIcon.FolderIconPar mActivityContext = ActivityContext.lookupContext(context); mIconLayoutBounds = mActivityContext.getTransientTaskbarBounds(); Resources resources = getResources(); - boolean isTransientTaskbar = DisplayController.isTransientTaskbar(mActivityContext) - && !mActivityContext.isPhoneMode(); mIsRtl = Utilities.isRtl(resources); mTransientTaskbarMinWidth = resources.getDimension(R.dimen.transient_taskbar_min_width); @@ -173,6 +173,8 @@ public class TaskbarView extends FrameLayout implements FolderIcon.FolderIconPar setWillNotDraw(false); mAllAppsButtonContainer = new TaskbarAllAppsButtonContainer(context); + mAllAppsButtonTranslationOffset = (int) getResources().getDimension( + mAllAppsButtonContainer.getAllAppsButtonTranslationXOffset(isTransientTaskbar())); if (enableTaskbarPinning() || enableRecentsInTaskbar()) { mTaskbarDividerContainer = new TaskbarDividerContainer(context); @@ -221,9 +223,7 @@ public class TaskbarView extends FrameLayout implements FolderIcon.FolderIconPar enableTaskbarPinning() && !mActivityContext.isThreeButtonNav(); availableWidth -= iconSize - (int) getResources().getDimension( mAllAppsButtonContainer.getAllAppsButtonTranslationXOffset( - forceTransientTaskbarSize || ( - DisplayController.isTransientTaskbar(mActivityContext) - && !mActivityContext.isPhoneMode()))); + forceTransientTaskbarSize || isTransientTaskbar())); ++additionalIcons; return Math.floorDiv(availableWidth, iconSize) + additionalIcons; @@ -667,6 +667,15 @@ public class TaskbarView extends FrameLayout implements FolderIcon.FolderIconPar mIconLayoutBounds.right = iconEnd; mIconLayoutBounds.top = (bottom - top - mIconTouchSize) / 2; mIconLayoutBounds.bottom = mIconLayoutBounds.top + mIconTouchSize; + + // With rtl layout, the all apps button will be translated by `allAppsButtonOffset` after + // layout completion (by `TaskbarViewController`). Offset the icon end by the same amount + // when laying out icons, so the taskbar content remains centered after all apps button + // translation. + if (layoutRtl) { + iconEnd += mAllAppsButtonTranslationOffset; + } + int count = getChildCount(); for (int i = count; i > 0; i--) { View child = getChildAt(i - 1); @@ -698,6 +707,15 @@ public class TaskbarView extends FrameLayout implements FolderIcon.FolderIconPar mIconLayoutBounds.left = iconEnd; + // Adjust the icon layout bounds by the amount by which all apps button will be translated + // post layout to maintain margin between all apps button and the edge of the transient + // taskbar background. Done for ltr layout only - for rtl layout, the offset needs to be + // adjusted on the right, which is done by offsetting `iconEnd` after setting + // `mIconLayoutBounds.right`. + if (!layoutRtl) { + mIconLayoutBounds.left += mAllAppsButtonTranslationOffset; + } + if (mIconLayoutBounds.right - mIconLayoutBounds.left < mTransientTaskbarMinWidth) { int center = mIconLayoutBounds.centerX(); int distanceFromCenter = (int) mTransientTaskbarMinWidth / 2; @@ -740,12 +758,13 @@ public class TaskbarView extends FrameLayout implements FolderIcon.FolderIconPar /** * Returns the space used by the icons */ - public int getIconLayoutWidth() { + private int getIconLayoutWidth() { int countExcludingQsb = getChildCount(); DeviceProfile deviceProfile = mActivityContext.getDeviceProfile(); if (deviceProfile.isQsbInline) { countExcludingQsb--; } + int iconLayoutBoundsWidth = countExcludingQsb * (mItemMarginLeftRight * 2 + mIconTouchSize); @@ -754,17 +773,28 @@ public class TaskbarView extends FrameLayout implements FolderIcon.FolderIconPar // All Apps icon, divider icon, and first app icon in taskbar iconLayoutBoundsWidth -= mItemMarginLeftRight * 4; } + + // The all apps button container gets offset horizontally, reducing the overall taskbar + // view size. + iconLayoutBoundsWidth -= mAllAppsButtonTranslationOffset; + return iconLayoutBoundsWidth; } /** - * Returns the app icons currently shown in the taskbar. + * Returns the app icons currently shown in the taskbar. The returned list does not include qsb, + * but it includes all apps button and icon divider views. */ public View[] getIconViews() { final int count = getChildCount(); - View[] icons = new View[count]; + if (count == 0) { + return new View[0]; + } + View[] icons = new View[count - (mActivityContext.getDeviceProfile().isQsbInline ? 1 : 0)]; + int insertionPoint = 0; for (int i = 0; i < count; i++) { - icons[i] = getChildAt(i); + if (getChildAt(i) == mQsb) continue; + icons[insertionPoint++] = getChildAt(i); } return icons; } @@ -846,11 +876,24 @@ public class TaskbarView extends FrameLayout implements FolderIcon.FolderIconPar // Ignore, we just implement Insettable to draw behind system insets. } + private boolean isTransientTaskbar() { + return DisplayController.isTransientTaskbar(mActivityContext) + && !mActivityContext.isPhoneMode(); + } + public boolean areIconsVisible() { // Consider the overall visibility return getVisibility() == VISIBLE; } + /** + * @return The all apps button horizontal offset used to calculate the taskbar contents width + * during layout. + */ + public int getAllAppsButtonTranslationXOffsetUsedForLayout() { + return mAllAppsButtonTranslationOffset; + } + /** * Maps {@code op} over all the child views. */ diff --git a/quickstep/src/com/android/launcher3/taskbar/TaskbarViewController.java b/quickstep/src/com/android/launcher3/taskbar/TaskbarViewController.java index 253d0259db..0b6dea63f4 100644 --- a/quickstep/src/com/android/launcher3/taskbar/TaskbarViewController.java +++ b/quickstep/src/com/android/launcher3/taskbar/TaskbarViewController.java @@ -359,10 +359,6 @@ public class TaskbarViewController implements TaskbarControllers.LoggableTaskbar return mTaskbarView.getIconLayoutBounds(); } - public int getIconLayoutWidth() { - return mTaskbarView.getIconLayoutWidth(); - } - public View[] getIconViews() { return mTaskbarView.getIconViews(); } @@ -442,6 +438,19 @@ public class TaskbarViewController implements TaskbarControllers.LoggableTaskbar float allAppIconTranslateRange = mapRange(scale, transientTaskbarAllAppsOffset, persistentTaskbarAllAppsOffset); + // Task icons are laid out so the taskbar content is centered. The taskbar width (used for + // centering taskbar icons) depends on the all apps button X translation, and is different + // for persistent and transient taskbar. If the offset used for current taskbar layout is + // different than the offset used in final taskbar state, the icons may jump when the + // animation completes, and the taskbar is replaced. Adjust item transform to account for + // this mismatch. + float sizeDiffTranslationRange = + mapRange(scale, + (mTaskbarView.getAllAppsButtonTranslationXOffsetUsedForLayout() + - transientTaskbarAllAppsOffset) / 2, + (mTaskbarView.getAllAppsButtonTranslationXOffsetUsedForLayout() + - persistentTaskbarAllAppsOffset) / 2); + // no x translation required when all apps button is the only icon in taskbar. if (iconViews.length <= 1) { allAppIconTranslateRange = 0f; @@ -449,6 +458,7 @@ public class TaskbarViewController implements TaskbarControllers.LoggableTaskbar if (mIsRtl) { allAppIconTranslateRange *= -1; + sizeDiffTranslationRange *= -1; } if (mActivity.isThreeButtonNav()) { @@ -457,25 +467,18 @@ public class TaskbarViewController implements TaskbarControllers.LoggableTaskbar return; } - float taskbarCenterX = - mTaskbarView.getLeft() + (mTaskbarView.getRight() - mTaskbarView.getLeft()) / 2.0f; - float finalMarginScale = mapRange(scale, 0f, mTransientIconSize - mPersistentIconSize); - float halfIconCount = iconViews.length / 2.0f; + // The index of the "middle" icon which will be used as a index from which the icon margins + // will be scaled. If number of icons is even, using the middle point between indices of two + // central icons. + float middleIndex = (iconViews.length - 1) / 2.0f; for (int iconIndex = 0; iconIndex < iconViews.length; iconIndex++) { View iconView = iconViews[iconIndex]; MultiTranslateDelegate translateDelegate = ((Reorderable) iconView).getTranslateDelegate(); - float iconCenterX = - iconView.getLeft() + (iconView.getRight() - iconView.getLeft()) / 2.0f; - if (iconCenterX <= taskbarCenterX) { - translateDelegate.getTranslationX(INDEX_TASKBAR_PINNING_ANIM).setValue( - finalMarginScale * (halfIconCount - iconIndex)); - } else { - translateDelegate.getTranslationX(INDEX_TASKBAR_PINNING_ANIM).setValue( - -finalMarginScale * (iconIndex - halfIconCount)); - } + translateDelegate.getTranslationX(INDEX_TASKBAR_PINNING_ANIM).setValue( + finalMarginScale * (middleIndex - iconIndex) + sizeDiffTranslationRange); if (iconView.equals(mTaskbarView.getAllAppsButtonContainer())) { mTaskbarView.getAllAppsButtonContainer().setTranslationXForTaskbarAllAppsIcon( @@ -488,18 +491,14 @@ public class TaskbarViewController implements TaskbarControllers.LoggableTaskbar * Calculates visual taskbar view width. */ public float getCurrentVisualTaskbarWidth() { - if (mTaskbarView.getIconViews().length == 0) { + View[] iconViews = mTaskbarView.getIconViews(); + if (iconViews.length == 0) { return 0; } - View[] iconViews = mTaskbarView.getIconViews(); + float left = iconViews[0].getX(); - int leftIndex = mActivity.getDeviceProfile().isQsbInline && !mIsRtl ? 1 : 0; - int rightIndex = mActivity.getDeviceProfile().isQsbInline && mIsRtl - ? iconViews.length - 2 - : iconViews.length - 1; - - float left = iconViews[leftIndex].getX(); + int rightIndex = iconViews.length - 1; float right = iconViews[rightIndex].getRight() + iconViews[rightIndex].getTranslationX(); return right - left + (2 * mTaskbarLeftRightMargin);