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

Commit 245fb731 authored by Bryce Lee's avatar Bryce Lee
Browse files

Only check for remove drag coordinates when remove is available.

This changelist ensures that CommunalHub only updates the remove state
based on dragging only when remove is available.

Test: atest CommunalHubUtilsTest#sPointerWithinEnabledRemoveButton_ensureDisabledStatePriority
Fixes: 341072351
Flag: EXEMPT bugfix
Change-Id: Ifa51aee8f21862b1da74070e9cbc737c5d55aa95
parent 78a10b5e
Loading
Loading
Loading
Loading
+10 −9
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import android.util.SizeF
import android.view.View.IMPORTANT_FOR_ACCESSIBILITY_AUTO
import android.view.View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS
import android.widget.FrameLayout
import androidx.annotation.VisibleForTesting
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.AnimatedVisibilityScope
import androidx.compose.animation.ExperimentalAnimationApi
@@ -162,7 +163,6 @@ fun CommunalHub(
    var removeButtonCoordinates: LayoutCoordinates? by remember { mutableStateOf(null) }
    var toolbarSize: IntSize? by remember { mutableStateOf(null) }
    var gridCoordinates: LayoutCoordinates? by remember { mutableStateOf(null) }
    var isDraggingToRemove by remember { mutableStateOf(false) }
    val gridState = rememberLazyGridState()
    val contentListState = rememberContentListState(widgetConfigurator, communalContent, viewModel)
    val reorderingWidgets by viewModel.reorderingWidgets.collectAsStateWithLifecycle()
@@ -250,12 +250,11 @@ fun CommunalHub(
                    contentOffset = contentOffset,
                    setGridCoordinates = { gridCoordinates = it },
                    updateDragPositionForRemove = { offset ->
                        isDraggingToRemove =
                            isPointerWithinCoordinates(
                        isPointerWithinEnabledRemoveButton(
                            removeEnabled = removeButtonEnabled,
                            offset = gridCoordinates?.let { it.positionInWindow() + offset },
                            containerToCheck = removeButtonCoordinates
                        )
                        isDraggingToRemove
                    },
                    gridState = gridState,
                    contentListState = contentListState,
@@ -1195,11 +1194,13 @@ private fun beforeContentPadding(paddingValues: PaddingValues): ContentPaddingIn
 * Check whether the pointer position that the item is being dragged at is within the coordinates of
 * the remove button in the toolbar. Returns true if the item is removable.
 */
private fun isPointerWithinCoordinates(
@VisibleForTesting
fun isPointerWithinEnabledRemoveButton(
    removeEnabled: Boolean,
    offset: Offset?,
    containerToCheck: LayoutCoordinates?
): Boolean {
    if (offset == null || containerToCheck == null) {
    if (!removeEnabled || offset == null || containerToCheck == null) {
        return false
    }
    val container = containerToCheck.boundsInWindow()
+41 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.communal.ui.compose

import android.testing.TestableLooper
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.layout.LayoutCoordinates
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.google.common.truth.Truth.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.mock

@RunWith(AndroidJUnit4::class)
@TestableLooper.RunWithLooper(setAsMainLooper = true)
@SmallTest
class CommunalHubUtilsTest : SysuiTestCase() {
    @Test
    fun isPointerWithinEnabledRemoveButton_ensureDisabledStatePriority() {
        assertThat(
                isPointerWithinEnabledRemoveButton(false, mock<Offset>(), mock<LayoutCoordinates>())
            )
            .isFalse()
    }
}