|
| 1 | +/* |
| 2 | + * Copyright 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 | + * https://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.example.compose.snippets.draganddrop |
| 18 | + |
| 19 | +import android.content.ClipData |
| 20 | +import android.content.ClipDescription |
| 21 | +import android.os.Build |
| 22 | +import android.view.View |
| 23 | +import androidx.annotation.RequiresApi |
| 24 | +import androidx.compose.foundation.ExperimentalFoundationApi |
| 25 | +import androidx.compose.foundation.draganddrop.dragAndDropSource |
| 26 | +import androidx.compose.foundation.draganddrop.dragAndDropTarget |
| 27 | +import androidx.compose.foundation.gestures.detectTapGestures |
| 28 | +import androidx.compose.runtime.Composable |
| 29 | +import androidx.compose.runtime.remember |
| 30 | +import androidx.compose.ui.Modifier |
| 31 | +import androidx.compose.ui.draganddrop.DragAndDropEvent |
| 32 | +import androidx.compose.ui.draganddrop.DragAndDropTarget |
| 33 | +import androidx.compose.ui.draganddrop.DragAndDropTransferData |
| 34 | +import androidx.compose.ui.draganddrop.mimeTypes |
| 35 | + |
| 36 | +@RequiresApi(Build.VERSION_CODES.N) |
| 37 | +@OptIn(ExperimentalFoundationApi::class) |
| 38 | +@Composable |
| 39 | +fun DragAndDropSnippet() { |
| 40 | + |
| 41 | + val url = "" |
| 42 | + |
| 43 | + // [START android_compose_drag_and_drop_1] |
| 44 | + Modifier.dragAndDropSource { |
| 45 | + detectTapGestures(onLongPress = { |
| 46 | + startTransfer( |
| 47 | + DragAndDropTransferData( |
| 48 | + ClipData.newPlainText( |
| 49 | + "image Url", url |
| 50 | + ) |
| 51 | + ) |
| 52 | + ) |
| 53 | + }) |
| 54 | + } |
| 55 | + // [END android_compose_drag_and_drop_1] |
| 56 | + |
| 57 | + // [START android_compose_drag_and_drop_2] |
| 58 | + Modifier.dragAndDropSource { |
| 59 | + detectTapGestures(onLongPress = { |
| 60 | + startTransfer( |
| 61 | + DragAndDropTransferData( |
| 62 | + ClipData.newPlainText( |
| 63 | + "image Url", url |
| 64 | + ), |
| 65 | + flags = View.DRAG_FLAG_GLOBAL |
| 66 | + ) |
| 67 | + ) |
| 68 | + }) |
| 69 | + } |
| 70 | + // [END android_compose_drag_and_drop_2] |
| 71 | + |
| 72 | + // [START android_compose_drag_and_drop_3] |
| 73 | + val callback = remember { |
| 74 | + object : DragAndDropTarget { |
| 75 | + override fun onDrop(event: DragAndDropEvent): Boolean { |
| 76 | + // Parse received data |
| 77 | + return true |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + // [END android_compose_drag_and_drop_3] |
| 82 | + |
| 83 | + // [START android_compose_drag_and_drop_4] |
| 84 | + Modifier.dragAndDropTarget( |
| 85 | + shouldStartDragAndDrop = { event -> |
| 86 | + event.mimeTypes().contains(ClipDescription.MIMETYPE_TEXT_PLAIN) |
| 87 | + }, target = callback |
| 88 | + ) |
| 89 | + // [END android_compose_drag_and_drop_4] |
| 90 | + |
| 91 | + // [START android_compose_drag_and_drop_5] |
| 92 | + object : DragAndDropTarget { |
| 93 | + override fun onStarted(event: DragAndDropEvent) { |
| 94 | + // When the drag event starts |
| 95 | + } |
| 96 | + |
| 97 | + override fun onEntered(event: DragAndDropEvent) { |
| 98 | + // When the dragged object enters the target surface |
| 99 | + } |
| 100 | + |
| 101 | + override fun onEnded(event: DragAndDropEvent) { |
| 102 | + // When the drag event stops |
| 103 | + } |
| 104 | + |
| 105 | + override fun onExited(event: DragAndDropEvent) { |
| 106 | + // When the dragged object exits the target surface |
| 107 | + } |
| 108 | + |
| 109 | + override fun onDrop(event: DragAndDropEvent): Boolean = true |
| 110 | + } |
| 111 | + // [END android_compose_drag_and_drop_5] |
| 112 | +} |
0 commit comments