Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

External drag and drop support for compose application #391

Merged
merged 31 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
d0de9fd
Introduce external file drag and drop support for desktop
Walingar Jan 30, 2023
c67d3e2
Introduce more drop data types for external drag support
Walingar Jan 30, 2023
2cf9055
Add docs to external drag API
Walingar Jan 30, 2023
e07fa23
Add offset to dragStart callback like in onDrag function
Walingar Jan 30, 2023
b62ff6f
Move awt point -> offset logic closer to drag event
Walingar Jan 31, 2023
decc04a
Introduce tests for external drag support
Walingar Jan 31, 2023
6614da1
Mark external drop API as experimental
Walingar Jan 31, 2023
bae882e
Use handlers.values
Walingar Mar 8, 2023
c4945f4
Use componentBounds instead of LayoutCoordinates for external drag su…
Walingar Mar 8, 2023
bec5531
Wrap lambdas in rememberUpdatedState
Walingar Mar 8, 2023
b6150fe
Add experimental compose api opt in
Walingar Mar 8, 2023
79dc947
Introduce test to check that drag enter/cancel are not spammed on com…
Walingar Mar 8, 2023
27b7493
Don't reinstall component drag handlers on recomposition, just provid…
Walingar Mar 8, 2023
fe62f4f
Extract common code for drag event handling
Walingar Mar 8, 2023
850ba62
Update idsCounter immediately and use current handleId
Walingar Mar 8, 2023
b802866
Change doc
Walingar Mar 8, 2023
33470de
Update tests: don't notify onDrag if onDragEntered notified
Walingar Mar 8, 2023
c2f82de
Remove VisibleForTesting, can be added in the future if needed
Walingar Mar 9, 2023
d068e92
Move external drag support to androidx.compose.ui package
Walingar Mar 9, 2023
56f0cff
Rename forAll -> forEach
Walingar Mar 9, 2023
095c3a2
Merge value acquiring and remove
Walingar Mar 9, 2023
21943c6
Take changed callbacks into account in AwtWindowDropTarget
Walingar Mar 9, 2023
80eee0d
Modify DropData API
Walingar Mar 10, 2023
c0b992c
Always provide offset and drop data to external drag feature
Walingar Mar 10, 2023
5f650fb
Rename onDragCancel -> onDragExit
Walingar Mar 10, 2023
60e7b65
Rename DropData -> DragData and update javadocs
Walingar Mar 10, 2023
6a9c42b
Don't provide mimeTypes, it is not needed
Walingar Mar 13, 2023
5b2dac4
Rename State -> Value
Walingar Mar 13, 2023
2c31a2e
Convert interface to class for current drag state
Walingar Mar 13, 2023
b44a3b1
Add immutable annotation
Walingar Mar 13, 2023
62cec60
Update compose/ui/ui/src/desktopMain/kotlin/androidx/compose/ui/Exter…
Walingar Mar 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright 2023 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
*
* http://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 androidx.compose.desktop.examples.dnd

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.DragData
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.onExternalDrag
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.singleWindowApplication

@OptIn(ExperimentalComposeUiApi::class)
fun main() = singleWindowApplication(
title = "External dnd demo"
) {
MaterialTheme {
val size = 200.dp

var isDragging by remember { mutableStateOf(false) }
var text by remember { mutableStateOf<String?>(null) }
var painter by remember { mutableStateOf<Painter?>(null) }

Column(modifier = Modifier.padding(20.dp)) {
Box(
modifier = Modifier.size(size, size)
.background(
when {
isDragging -> Color.Green
text != null -> Color.White
else -> Color.Red
}
)
.onExternalDrag(
onDragStart = {
isDragging = true
},
onDragExit = {
isDragging = false
},
onDrag = {

},
onDrop = { state ->
val dragData = state.dragData
text = dragData.toString()
if (dragData is DragData.Image) {
painter = dragData.readImage()
}
isDragging = false
})
) {
Text(text ?: "Try to drag some files or image here", modifier = Modifier.align(Alignment.Center))
val currentPainter = painter
if (currentPainter != null) {
Image(currentPainter, contentDescription = "Pasted Image")
}
}
}
}
}
Loading