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

Fix drag-and-drop start gesture #1633

Merged
merged 1 commit into from
Oct 15, 2024
Merged

Conversation

m-sasha
Copy link
Member

@m-sasha m-sasha commented Oct 14, 2024

Our current DragAndDropSourceDefaults.DefaultStartDetector does detectTapGestures(onLongPress=...), which does two things wrong:

  1. It triggers on long-press with a mouse.
  2. It does not trigger on drag-start with a mouse.

This PR fixes these two issues.

Testing

  • Tested manually (only with a mouse) on
@OptIn(ExperimentalComposeUiApi::class)
fun main() = singleWindowApplication {
    val exportedText = "Hello, DnD!"
    Row(
        horizontalArrangement = Arrangement.SpaceEvenly,
        verticalAlignment = Alignment.CenterVertically,
        modifier = Modifier.fillMaxSize()
    ) {
        val textMeasurer = rememberTextMeasurer()
        Box(Modifier
            .size(200.dp)
            .background(Color.LightGray)
            .dragAndDropSource(
                drawDragDecoration = {
                    drawRect(
                        color = Color.White,
                        topLeft = Offset(x = 0f, y = size.height/4),
                        size = Size(size.width, size.height/2)
                    )
                    val textLayoutResult = textMeasurer.measure(
                        text = AnnotatedString(exportedText),
                        layoutDirection = layoutDirection,
                        density = this
                    )
                    drawText(
                        textLayoutResult = textLayoutResult,
                        topLeft = Offset(
                            x = (size.width - textLayoutResult.size.width) / 2,
                            y = (size.height - textLayoutResult.size.height) / 2,
                        )
                    )
                },
                transferData = { offset ->
                    DragAndDropTransferData(
                        transferable = DragAndDropTransferable(
                            StringSelection(exportedText)
                        ),
                        supportedActions = listOf(
                            DragAndDropTransferAction.Copy,
                            DragAndDropTransferAction.Move,
                            DragAndDropTransferAction.Link,
                        ),
                        dragDecorationOffset = offset,
                        onTransferCompleted = { action ->
                            println("Action at source: $action")
                        }
                    )
                }
            )
        ) {
            Text("Drag Me", Modifier.align(Alignment.Center))
        }
    }
}

This should be tested by QA to make sure that it works for touch.

@m-sasha m-sasha requested a review from MatkovIvan October 14, 2024 12:07
Copy link
Member

@MatkovIvan MatkovIvan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change LGTM. Can we add auto test for this?

@m-sasha
Copy link
Member Author

m-sasha commented Oct 14, 2024

The change LGTM. Can we add auto test for this?

I'll do it in a separate PR, since it requires to first add support for drag-and-drop in tests.

@m-sasha m-sasha force-pushed the m-sasha/add-mouse-dnd-start branch from df2d1c6 to 5561a83 Compare October 14, 2024 13:22
@m-sasha
Copy link
Member Author

m-sasha commented Oct 14, 2024

BTW, actually you can't do

pointerInput(Unit) {
   oneDetector()
   anotherDetector()
}

because the detectors never return. Fixed by wrapping them in a coroutine scope with launch.

@m-sasha m-sasha force-pushed the m-sasha/add-mouse-dnd-start branch from 5561a83 to a704aaa Compare October 15, 2024 10:37
@m-sasha m-sasha merged commit 464e2eb into jb-main Oct 15, 2024
7 checks passed
@m-sasha m-sasha deleted the m-sasha/add-mouse-dnd-start branch October 15, 2024 11:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants