1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.wm.shell.common 18 19 import android.graphics.PointF 20 import android.graphics.Rect 21 import android.graphics.RectF 22 23 /** 24 * Utility class for calculating bounds during multi-display drag operations. 25 * 26 * This class provides helper functions to perform bounds calculation during window drag. 27 */ 28 object MultiDisplayDragMoveBoundsCalculator { 29 /** 30 * Calculates the global DP bounds of a window being dragged across displays. 31 * 32 * @param startDisplayLayout The DisplayLayout object of the display where the drag started. 33 * @param repositionStartPoint The starting position of the drag (in pixels), relative to the 34 * display where the drag started. 35 * @param boundsAtDragStart The initial bounds of the window (in pixels), relative to the 36 * display where the drag started. 37 * @param currentDisplayLayout The DisplayLayout object of the display where the pointer is 38 * currently located. 39 * @param x The current x-coordinate of the drag pointer (in pixels). 40 * @param y The current y-coordinate of the drag pointer (in pixels). 41 * @return A RectF object representing the calculated global DP bounds of the window. 42 */ calculateGlobalDpBoundsForDragnull43 fun calculateGlobalDpBoundsForDrag( 44 startDisplayLayout: DisplayLayout, 45 repositionStartPoint: PointF, 46 boundsAtDragStart: Rect, 47 currentDisplayLayout: DisplayLayout, 48 x: Float, 49 y: Float, 50 ): RectF { 51 // Convert all pixel values to DP. 52 val startCursorDp = 53 startDisplayLayout.localPxToGlobalDp(repositionStartPoint.x, repositionStartPoint.y) 54 val currentCursorDp = currentDisplayLayout.localPxToGlobalDp(x, y) 55 val startLeftTopDp = 56 startDisplayLayout.localPxToGlobalDp(boundsAtDragStart.left, boundsAtDragStart.top) 57 val widthDp = startDisplayLayout.pxToDp(boundsAtDragStart.width()) 58 val heightDp = startDisplayLayout.pxToDp(boundsAtDragStart.height()) 59 60 // Calculate DP bounds based on pointer movement delta. 61 val currentLeftDp = startLeftTopDp.x + (currentCursorDp.x - startCursorDp.x) 62 val currentTopDp = startLeftTopDp.y + (currentCursorDp.y - startCursorDp.y) 63 val currentRightDp = currentLeftDp + widthDp 64 val currentBottomDp = currentTopDp + heightDp 65 66 return RectF(currentLeftDp, currentTopDp, currentRightDp, currentBottomDp) 67 } 68 69 /** 70 * Converts global DP bounds to local pixel bounds for a specific display. 71 * 72 * @param rectDp The global DP bounds to convert. 73 * @param displayLayout The DisplayLayout representing the display to convert the bounds to. 74 * @return A Rect object representing the local pixel bounds on the specified display. 75 */ convertGlobalDpToLocalPxForRectnull76 fun convertGlobalDpToLocalPxForRect(rectDp: RectF, displayLayout: DisplayLayout): Rect { 77 val leftTopPxDisplay = displayLayout.globalDpToLocalPx(rectDp.left, rectDp.top) 78 val rightBottomPxDisplay = displayLayout.globalDpToLocalPx(rectDp.right, rectDp.bottom) 79 return Rect( 80 leftTopPxDisplay.x.toInt(), 81 leftTopPxDisplay.y.toInt(), 82 rightBottomPxDisplay.x.toInt(), 83 rightBottomPxDisplay.y.toInt(), 84 ) 85 } 86 } 87