1 /* 2 * Copyright 2023 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 androidx.compose.ui.draganddrop 18 19 import android.graphics.Canvas as AndroidCanvas 20 import android.graphics.Point 21 import android.view.View 22 import androidx.compose.ui.geometry.Size 23 import androidx.compose.ui.graphics.Canvas 24 import androidx.compose.ui.graphics.drawscope.CanvasDrawScope 25 import androidx.compose.ui.graphics.drawscope.DrawScope 26 import androidx.compose.ui.unit.Density 27 import androidx.compose.ui.unit.LayoutDirection 28 29 /** 30 * Draws a drag shadow for a [View.DragShadowBuilder] with the DrawScope lambda provided by 31 * [drawDragDecoration]. 32 */ 33 internal class ComposeDragShadowBuilder( 34 private val density: Density, 35 private val decorationSize: Size, 36 private val drawDragDecoration: DrawScope.() -> Unit, 37 ) : View.DragShadowBuilder() { 38 onProvideShadowMetricsnull39 override fun onProvideShadowMetrics(outShadowSize: Point, outShadowTouchPoint: Point) = 40 with(density) { 41 outShadowSize.set( 42 decorationSize.width.toDp().roundToPx(), 43 decorationSize.height.toDp().roundToPx() 44 ) 45 outShadowTouchPoint.set(outShadowSize.x / 2, outShadowSize.y / 2) 46 } 47 onDrawShadownull48 override fun onDrawShadow(canvas: AndroidCanvas) { 49 CanvasDrawScope() 50 .draw( 51 density = density, 52 size = decorationSize, 53 layoutDirection = LayoutDirection.Ltr, 54 canvas = Canvas(canvas), 55 block = drawDragDecoration, 56 ) 57 } 58 } 59