-
Notifications
You must be signed in to change notification settings - Fork 381
Expand file tree
/
Copy pathDragAndDropSnippets.kt
More file actions
121 lines (107 loc) · 4 KB
/
Copy pathDragAndDropSnippets.kt
File metadata and controls
121 lines (107 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* Copyright 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
*
* https://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.example.compose.snippets.draganddrop
import android.content.ClipData
import android.content.ClipDescription
import android.os.Build
import android.view.View
import androidx.activity.compose.LocalActivity
import androidx.annotation.RequiresApi
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.draganddrop.dragAndDropSource
import androidx.compose.foundation.draganddrop.dragAndDropTarget
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draganddrop.DragAndDropEvent
import androidx.compose.ui.draganddrop.DragAndDropTarget
import androidx.compose.ui.draganddrop.DragAndDropTransferData
import androidx.compose.ui.draganddrop.mimeTypes
import androidx.compose.ui.draganddrop.toAndroidDragEvent
@RequiresApi(Build.VERSION_CODES.N)
@OptIn(ExperimentalFoundationApi::class)
@Composable
private fun DragAndDropSnippet() {
val url = ""
// [START android_compose_drag_and_drop_2]
Modifier.dragAndDropSource { _ ->
DragAndDropTransferData(
ClipData.newPlainText(
"image Url", url
)
)
}
// [END android_compose_drag_and_drop_2]
// [START android_compose_drag_and_drop_3]
Modifier.dragAndDropSource { _ ->
DragAndDropTransferData(
ClipData.newPlainText(
"image Url", url
),
flags = View.DRAG_FLAG_GLOBAL
)
}
// [END android_compose_drag_and_drop_3]
// [START android_compose_drag_and_drop_4]
val callback = remember {
object : DragAndDropTarget {
override fun onDrop(event: DragAndDropEvent): Boolean {
// Parse received data
return true
}
}
}
// [END android_compose_drag_and_drop_4]
LocalActivity.current?.let { activity ->
// [START android_compose_drag_and_drop_7]
val externalAppCallback = remember {
object : DragAndDropTarget {
override fun onDrop(event: DragAndDropEvent): Boolean {
val permission =
activity.requestDragAndDropPermissions(event.toAndroidDragEvent())
// Parse received data
permission?.release()
return true
}
}
}
// [END android_compose_drag_and_drop_7]
}
// [START android_compose_drag_and_drop_5]
Modifier.dragAndDropTarget(
shouldStartDragAndDrop = { event ->
event.mimeTypes().contains(ClipDescription.MIMETYPE_TEXT_PLAIN)
}, target = callback // or externalAppCallback
)
// [END android_compose_drag_and_drop_5]
// [START android_compose_drag_and_drop_6]
object : DragAndDropTarget {
override fun onStarted(event: DragAndDropEvent) {
// When the drag event starts
}
override fun onEntered(event: DragAndDropEvent) {
// When the dragged object enters the target surface
}
override fun onEnded(event: DragAndDropEvent) {
// When the drag event stops
}
override fun onExited(event: DragAndDropEvent) {
// When the dragged object exits the target surface
}
override fun onDrop(event: DragAndDropEvent): Boolean = true
}
// [END android_compose_drag_and_drop_6]
}