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

Commit 893615f2 authored by Sebastian Franco's avatar Sebastian Franco Committed by Sebastián Franco
Browse files

Reorder widgets no longer overlaps when no space is available

In the previous refactor I got confused between findNearestVacantArea
and findNearestArea thinking the later did the former. So it ignored the occupied spaces and treat it as a solution.

Changed the names to prevent further confusion.

Fix: 258023561
Test: manual, need to add this case to ReorderWidgets
Change-Id: I04b262ecce168d5c93a9d66ef62d5b0e148e38b6
parent 345ef9ff
Loading
Loading
Loading
Loading
+33 −18
Original line number Original line Diff line number Diff line
@@ -1651,8 +1651,19 @@ public class CellLayout extends ViewGroup {
        }
        }
    }
    }


    private ItemConfiguration findConfigurationNoShuffle(int pixelX, int pixelY, int minSpanX, int minSpanY,
    /**
            int spanX, int spanY, View dragView, ItemConfiguration solution) {
     * Returns a "reorder" where we simply drop the item in the closest empty space, without moving
     * any other item in the way.
     *
     * @param pixelX X coordinate in pixels in the screen
     * @param pixelY Y coordinate in pixels in the screen
     * @param spanX horizontal cell span
     * @param spanY vertical cell span
     * @return the configuration that represents the found reorder
     */
    private ItemConfiguration closestEmptySpaceReorder(int pixelX, int pixelY, int minSpanX,
            int minSpanY, int spanX, int spanY) {
        ItemConfiguration solution = new ItemConfiguration();
        int[] result = new int[2];
        int[] result = new int[2];
        int[] resultSpan = new int[2];
        int[] resultSpan = new int[2];
        findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, result,
        findNearestVacantArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, result,
@@ -1697,7 +1708,7 @@ public class CellLayout extends ViewGroup {


    boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
    boolean isNearestDropLocationOccupied(int pixelX, int pixelY, int spanX, int spanY,
            View dragView, int[] result) {
            View dragView, int[] result) {
        result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
        result = findNearestAreaIgnoreOccupied(pixelX, pixelY, spanX, spanY, result);
        getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
        getViewsIntersectingRegion(result[0], result[1], spanX, spanY, dragView, null,
                mIntersectingViews);
                mIntersectingViews);
        return !mIntersectingViews.isEmpty();
        return !mIntersectingViews.isEmpty();
@@ -2247,7 +2258,8 @@ public class CellLayout extends ViewGroup {
        //TODO(adamcohen) b/151776141 use the items visual center for the direction vector
        //TODO(adamcohen) b/151776141 use the items visual center for the direction vector
        int[] targetDestination = new int[2];
        int[] targetDestination = new int[2];


        findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
        findNearestAreaIgnoreOccupied(dragViewCenterX, dragViewCenterY, spanX, spanY,
                targetDestination);
        Rect dragRect = new Rect();
        Rect dragRect = new Rect();
        cellToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
        cellToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
        dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
        dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());
@@ -2400,7 +2412,7 @@ public class CellLayout extends ViewGroup {
        // We find the nearest cell into which we would place the dragged item, assuming there's
        // We find the nearest cell into which we would place the dragged item, assuming there's
        // nothing in its way.
        // nothing in its way.
        int result[] = new int[2];
        int result[] = new int[2];
        result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
        result = findNearestAreaIgnoreOccupied(pixelX, pixelY, spanX, spanY, result);


        boolean success;
        boolean success;
        // First we try the exact nearest position of the item being dragged,
        // First we try the exact nearest position of the item being dragged,
@@ -2445,19 +2457,21 @@ public class CellLayout extends ViewGroup {
    }
    }


    /**
    /**
     * Returns a "reorder" where we simply drop the item in the closest empty space, without moving
     * Returns a "reorder" if there is empty space without rearranging anything.
     * any other item in the way.
     *
     *
     * @param pixelX X coordinate in pixels in the screen
     * @param pixelX X coordinate in pixels in the screen
     * @param pixelY Y coordinate in pixels in the screen
     * @param pixelY Y coordinate in pixels in the screen
     * @param spanX horizontal cell span
     * @param spanX horizontal cell span
     * @param spanY vertical cell span
     * @param spanY vertical cell span
     * @param dragView view being dragged in reorder
     * @return the configuration that represents the found reorder
     * @return the configuration that represents the found reorder
     */
     */
    public ItemConfiguration closestEmptySpaceReorder(int pixelX, int pixelY, int spanX,
    public ItemConfiguration dropInPlaceSolution(int pixelX, int pixelY, int spanX,
            int spanY) {
            int spanY, View dragView) {
        int[] result = new int[2];
        int[] result = new int[2];
        result = findNearestArea(pixelX, pixelY, spanX, spanY, result);
        if (isNearestDropLocationOccupied(pixelX, pixelY, spanX, spanY, dragView, result)) {
            result[0] = result[1] = -1;
        }
        ItemConfiguration solution = new ItemConfiguration();
        ItemConfiguration solution = new ItemConfiguration();
        copyCurrentStateToSolution(solution, false);
        copyCurrentStateToSolution(solution, false);
        solution.isSolution = result[0] != -1;
        solution.isSolution = result[0] != -1;
@@ -2490,25 +2504,25 @@ public class CellLayout extends ViewGroup {
            int spanX, int spanY, View dragView) {
            int spanX, int spanY, View dragView) {
        getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);
        getDirectionVectorForDrop(pixelX, pixelY, spanX, spanY, dragView, mDirectionVector);


        ItemConfiguration closestSpaceSolution = closestEmptySpaceReorder(pixelX, pixelY, spanX,
        ItemConfiguration dropInPlaceSolution = dropInPlaceSolution(pixelX, pixelY, spanX, spanY,
                spanY);
                dragView);


        // Find a solution involving pushing / displacing any items in the way
        // Find a solution involving pushing / displacing any items in the way
        ItemConfiguration swapSolution = findReorderSolution(pixelX, pixelY, minSpanX, minSpanY,
        ItemConfiguration swapSolution = findReorderSolution(pixelX, pixelY, minSpanX, minSpanY,
                spanX,  spanY, mDirectionVector, dragView,  true,  new ItemConfiguration());
                spanX,  spanY, mDirectionVector, dragView,  true,  new ItemConfiguration());


        // We attempt the approach which doesn't shuffle views at all
        // We attempt the approach which doesn't shuffle views at all
        ItemConfiguration noShuffleSolution = findConfigurationNoShuffle(pixelX, pixelY, minSpanX,
        ItemConfiguration closestSpaceSolution = closestEmptySpaceReorder(pixelX, pixelY, minSpanX,
                minSpanY, spanX, spanY, dragView, new ItemConfiguration());
                minSpanY, spanX, spanY);


        // If the reorder solution requires resizing (shrinking) the item being dropped, we instead
        // If the reorder solution requires resizing (shrinking) the item being dropped, we instead
        // favor a solution in which the item is not resized, but
        // favor a solution in which the item is not resized, but
        if (swapSolution.isSolution && swapSolution.area() >= noShuffleSolution.area()) {
        if (swapSolution.isSolution && swapSolution.area() >= closestSpaceSolution.area()) {
            return swapSolution;
            return swapSolution;
        } else if (noShuffleSolution.isSolution) {
            return noShuffleSolution;
        } else if (closestSpaceSolution.isSolution) {
        } else if (closestSpaceSolution.isSolution) {
            return closestSpaceSolution;
            return closestSpaceSolution;
        } else if (dropInPlaceSolution.isSolution) {
            return dropInPlaceSolution;
        }
        }
        return null;
        return null;
    }
    }
@@ -2665,7 +2679,8 @@ public class CellLayout extends ViewGroup {
     * @return The X, Y cell of a vacant area that can contain this object,
     * @return The X, Y cell of a vacant area that can contain this object,
     *         nearest the requested location.
     *         nearest the requested location.
     */
     */
    public int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, int[] result) {
    public int[] findNearestAreaIgnoreOccupied(int pixelX, int pixelY, int spanX, int spanY,
            int[] result) {
        return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, true, result, null);
        return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, true, result, null);
    }
    }


+1 −2
Original line number Original line Diff line number Diff line
@@ -2615,7 +2615,6 @@ public class Workspace<T extends View & PageIndicator> extends PagedView<T>
                setDragMode(DRAG_MODE_REORDER);
                setDragMode(DRAG_MODE_REORDER);
            }
            }


            boolean resize = resultSpan[0] != spanX || resultSpan[1] != spanY;
            mDragTargetLayout.visualizeDropLocation(mTargetCell[0], mTargetCell[1],
            mDragTargetLayout.visualizeDropLocation(mTargetCell[0], mTargetCell[1],
                    resultSpan[0], resultSpan[1], dragObject);
                    resultSpan[0], resultSpan[1], dragObject);
        }
        }
@@ -2972,7 +2971,7 @@ public class Workspace<T extends View & PageIndicator> extends PagedView<T>
     */
     */
    @Thunk int[] findNearestArea(int pixelX, int pixelY,
    @Thunk int[] findNearestArea(int pixelX, int pixelY,
            int spanX, int spanY, CellLayout layout, int[] recycle) {
            int spanX, int spanY, CellLayout layout, int[] recycle) {
        return layout.findNearestArea(
        return layout.findNearestAreaIgnoreOccupied(
                pixelX, pixelY, spanX, spanY, recycle);
                pixelX, pixelY, spanX, spanY, recycle);
    }
    }


+1 −1
Original line number Original line Diff line number Diff line
@@ -365,7 +365,7 @@ public class FolderPagedView extends PagedView<PageIndicatorDots> implements Cli
    public int findNearestArea(int pixelX, int pixelY) {
    public int findNearestArea(int pixelX, int pixelY) {
        int pageIndex = getNextPage();
        int pageIndex = getNextPage();
        CellLayout page = getPageAt(pageIndex);
        CellLayout page = getPageAt(pageIndex);
        page.findNearestArea(pixelX, pixelY, 1, 1, sTmpArray);
        page.findNearestAreaIgnoreOccupied(pixelX, pixelY, 1, 1, sTmpArray);
        if (mFolder.isLayoutRtl()) {
        if (mFolder.isLayoutRtl()) {
            sTmpArray[0] = page.getCountX() - sTmpArray[0] - 1;
            sTmpArray[0] = page.getCountX() - sTmpArray[0] - 1;
        }
        }