contentReceiver

Compose Modifier

Common
@ExperimentalFoundationApi
fun Modifier.contentReceiver(receiveContentListener: ReceiveContentListener): Modifier

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.

Note that only androidx.compose.foundation.text.input.TextFieldState override of the text field supports being a content receiver.

Parameters

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 Examples

ReceiveContentFullSample

@OptIn(ExperimentalFoundationApi::class)
@Composable
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) } }
        // Note that only TextFieldState override of the text field supports contentReceiver
        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 }
                                }
                            }
                    ),
        )
    }
}