New Compose Multiplatform components arrived on Composables UICheck it out →

Modifier in Compose Foundation

contentReceiver

Common

Configures the current node and any children nodes as a Content Receiver.

Content in this context refers to a [TransferableContent] that could be received from another app through Drag-and-Drop, Copy/Paste, or from the Software Keyboard.

There is no pre-filtering for the received content by media type, e.g. software Keyboard would assume that the app can handle any content that's sent to it. Therefore, it's crucial to check the received content's type and other related information before reading and processing it. Please refer to [TransferableContent.hasMediaType] and [TransferableContent.clipMetadata] to learn more about how to do proper checks on the received item.

Last updated:

Installation

dependencies {
   implementation("androidx.compose.foundation:foundation:1.7.0-beta04")
}

Overloads

@ExperimentalFoundationApi
@Suppress("ExecutorRegistration"
fun Modifier.contentReceiver(
    receiveContentListener: ReceiveContentListener
)

Parameters

namedescription
receiveContentListenerListener to respond to the receive event. This interface also includes a set of callbacks for certain Drag-and-Drop state changes. Please checkout [ReceiveContentListener] docs for an explanation of each callback.

Code Example

ReceiveContentFullSample

@Composable
@Sampled
@OptIn(ExperimentalFoundationApi::class
fun ReceiveContentFullSample() {
    val state = rememberTextFieldState()
    var images by remember { mutableStateOf<List<ImageBitmap>>(emptyList()) }
    var dragging by remember { mutableStateOf(false) }
    var hovering by remember { mutableStateOf(false) }
    Column {
        Row {
            images.forEach {
                Image(bitmap = it, contentDescription = null)
            }
        }
        BasicTextField(
            state = state,
            modifier = Modifier
                .background(
                    when {
                        dragging -> Color.Red
                        hovering -> Color.Green
                        else -> MaterialTheme.colors.background
                    }
                )
                .contentReceiver(
                    receiveContentListener = object : ReceiveContentListener {
                        override fun onDragStart() {
                            dragging = true
                        }

                        override fun onDragEnd() {
                            hovering = false
                            dragging = false
                        }

                        override fun onDragEnter() {
                            hovering = true
                        }

                        override fun onDragExit() {
                            hovering = false
                        }

                        override fun onReceive(
                            transferableContent: TransferableContent
                        ): TransferableContent? {
                            if (!transferableContent.hasMediaType(MediaType.Image)) {
                                return transferableContent
                            }
                            val newImages = mutableListOf<ImageBitmap>()
                            return transferableContent
                                .consume { item ->
                                    // only consume this item if we can read an imageBitmap
                                    item
                                        .readImageBitmap()
                                        ?.let { newImages += it; true } ?: false
                                }
                                .also {
                                    images = newImages
                                }
                        }
                    }
                )
        )
    }
}