Composables
Docs Login
Vela

Composer

Jetpack Compose Component

Download
package com.composables.vela.components

import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.scaleIn
import androidx.compose.animation.scaleOut
import androidx.compose.animation.togetherWith
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.unit.dp
import com.composables.vela.theme.ArrowUp
import com.composables.vela.theme.Icons
import com.composables.vela.theme.Microphone
import com.composables.vela.theme.Plus
import com.composables.vela.theme.Space
import com.composables.vela.theme.Stop
import com.composables.vela.theme.Theme
import com.composables.vela.theme.Waveform

@Composable
fun Composer(
    value: String,
    onValueChange: (String) -> Unit,
    model: String,
    responding: Boolean,
    canSend: Boolean,
    onSend: () -> Unit,
    onStop: () -> Unit,
    onAddClick: () -> Unit,
    onModelClick: () -> Unit,
    onDictateClick: () -> Unit,
    onVoiceClick: () -> Unit,
    modifier: Modifier = Modifier,
    placeholder: String = "Ask Vela",
    accessories: (@Composable RowScope.() -> Unit)? = null,
) {
    Column(
        modifier
            .fillMaxWidth()
            .clip(Theme.shapes.large)
            .background(Theme.colors.surface)
            .border(1.dp, Theme.colors.border, Theme.shapes.large)
            .padding(Space * 2)
    ) {
        if (accessories != null) {
            Row(
                Modifier.fillMaxWidth()
                    .horizontalScroll(rememberScrollState())
                    .padding(horizontal = Space * 2, vertical = Space),
                horizontalArrangement = Arrangement.spacedBy(Space * 2),
                verticalAlignment = Alignment.CenterVertically,
                content = accessories,
            )
        }
        BasicTextField(
            value = value,
            onValueChange = onValueChange,
            modifier = Modifier.fillMaxWidth().semantics { contentDescription = placeholder },
            textStyle = Theme.typography.body.copy(color = Theme.colors.onSurface),
            cursorBrush = SolidColor(Theme.colors.primary),
            maxLines = 6,
            decorationBox = { field ->
                Box(Modifier.padding(horizontal = Space * 2, vertical = Space * 2)) {
                    if (value.isEmpty()) Text(placeholder, color = Theme.colors.onBackgroundMuted)
                    field()
                }
            },
        )
        Row(
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.spacedBy(Space),
        ) {
            IconButton(
                onClick = onAddClick,
                contentDescription = "Add to chat",
                variant = IconButtonVariant.Tonal,
                size = IconButtonSize.Small,
            ) {
                Icon(Icons.Plus, contentDescription = null, modifier = Modifier.size(18.dp))
            }
            ModelPill(model = model, onClick = onModelClick)
            Spacer(Modifier.weight(1f))
            if (!responding) {
                IconButton(
                    onClick = onDictateClick,
                    contentDescription = "Dictate",
                    size = IconButtonSize.Small,
                ) {
                    Icon(
                        Icons.Microphone,
                        contentDescription = null,
                        modifier = Modifier.size(20.dp),
                        tint = Theme.colors.onBackgroundMuted,
                    )
                }
            }
            AnimatedContent(
                targetState =
                    when {
                        responding -> MainAction.Stop
                        canSend -> MainAction.Send
                        else -> MainAction.Voice
                    },
                transitionSpec = {
                    (fadeIn() + scaleIn(initialScale = 0.8f)) togetherWith
                        (fadeOut() + scaleOut(targetScale = 0.8f))
                },
                label = "Send, stop or voice",
            ) { action ->
                when (action) {
                    MainAction.Stop ->
                        IconButton(
                            onClick = onStop,
                            contentDescription = "Stop response",
                            variant = IconButtonVariant.Tonal,
                        ) {
                            Icon(
                                Icons.Stop,
                                contentDescription = null,
                                modifier = Modifier.size(16.dp),
                            )
                        }
                    MainAction.Send ->
                        IconButton(
                            onClick = onSend,
                            contentDescription = "Send",
                            variant = IconButtonVariant.Filled,
                        ) {
                            Icon(
                                Icons.ArrowUp,
                                contentDescription = null,
                                modifier = Modifier.size(20.dp),
                            )
                        }
                    MainAction.Voice ->
                        IconButton(
                            onClick = onVoiceClick,
                            contentDescription = "Voice chat",
                            variant = IconButtonVariant.Filled,
                        ) {
                            Icon(
                                Icons.Waveform,
                                contentDescription = null,
                                modifier = Modifier.size(20.dp),
                            )
                        }
                }
            }
        }
    }
}

private enum class MainAction {
    Send,
    Stop,
    Voice,
}

More components from Vela

by Alex Styl

Search screens and components

Screen flows

Screens

UI Elements

One Android lesson, every Wednesday

Building Android apps?

What Alex learned building Android apps for 14 years, so you don't have to. Reads in under a minute.

Join 3,500+ Android developers. Unsubscribe any time.

You're subscribed

The next lesson lands in on Wednesday.