Skip to content

Commit

Permalink
UI Changes with Text Area (Shift + Enter, Starting and Continuing Con…
Browse files Browse the repository at this point in the history
…versation) (#1001)

From the Jewel update, there were some changes to TextArea API, so I
modified it to handle a Shift + Enter and Enter functionality. Shift +
Enter will add a new line without sending, while Enter will send the
message. I set the `keyboardOptions = KeyboardOptions(imeAction =
ImeAction.None)` as a way to remove the default enter functionality
TextArea has.


https://github.com/user-attachments/assets/b529f2a5-cf94-462d-a288-370334f65766

UPDATE: I also added the functionality where if the conversation started
already, then it will say "continue the conversation" instead of 'start"
every time. The cursor looks off but I asked in #jewel in the
kotlin-lang workspace for any tips to remedy that.


https://github.com/user-attachments/assets/814a3053-2bb5-4c63-83d3-fa141a42ef84

(also still waiting on calling the API, which is why that isn't here)
<!--
  ⬆ Put your description above this! ⬆

  Please be descriptive and detailed.
  
Please read our [Contributing
Guidelines](https://github.com/tinyspeck/foundry/blob/main/.github/CONTRIBUTING.md)
and [Code of Conduct](https://slackhq.github.io/code-of-conduct).

Don't worry about deleting this, it's not visible in the PR!
-->
  • Loading branch information
kateliu20 authored Oct 1, 2024
1 parent f1d7702 commit 3fbd389
Showing 1 changed file with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.text.input.TextFieldLineLimits
import androidx.compose.foundation.text.input.TextFieldState
import androidx.compose.runtime.Composable
Expand All @@ -45,9 +46,10 @@ import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.KeyEventType
import androidx.compose.ui.input.key.isShiftPressed
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.onKeyEvent
import androidx.compose.ui.input.key.onPreviewKeyEvent
import androidx.compose.ui.input.key.type
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.unit.dp
import org.jetbrains.jewel.foundation.theme.JewelTheme
import org.jetbrains.jewel.ui.component.Icon
Expand Down Expand Up @@ -79,9 +81,14 @@ fun ChatWindowUi(state: ChatScreen.State, modifier: Modifier = Modifier) {
private fun ConversationField(modifier: Modifier = Modifier, onSendMessage: (String) -> Unit) {
val textState by remember { mutableStateOf(TextFieldState()) }
val isTextNotEmpty = textState.text.isNotBlank()
val (hasStartedConversation, setHasStartedConversation) = remember { mutableStateOf(false) }

val smileyFace = "\uD83D\uDE00"
val conversationBubble = "\uD83D\uDCAC"

fun sendMessage() {
if (isTextNotEmpty) {
setHasStartedConversation(true)
onSendMessage(textState.text.toString())
textState.clearText()
}
Expand All @@ -92,24 +99,34 @@ private fun ConversationField(modifier: Modifier = Modifier, onSendMessage: (Str
verticalAlignment = Alignment.Bottom,
) {
TextArea(
// handles shift + enter for new line, enter for send
state = textState,
modifier =
Modifier.weight(1f).heightIn(min = 56.dp).onKeyEvent { event ->
if (event.type == KeyEventType.KeyDown) {
when {
event.key == Key.Enter && event.isShiftPressed -> {
Modifier.weight(1f).heightIn(min = 56.dp).onPreviewKeyEvent { event ->
when {
(event.key == Key.Enter || event.key == Key.NumPadEnter) &&
event.type == KeyEventType.KeyDown -> {
if (event.isShiftPressed) {
textState.edit { append("\n") }
true
} else {
sendMessage()
true
}
else -> false
}
} else {
false
else -> false
}
},
placeholder = { Text("Start your conversation...") },
placeholder = {
modifier.padding(horizontal = 4.dp, vertical = 10.dp)
Text(
if (hasStartedConversation) "Continue the conversation... $conversationBubble"
else "Start your conversation... $smileyFace"
)
},
textStyle = JewelTheme.defaultTextStyle,
lineLimits = TextFieldLineLimits.MultiLine(Int.MAX_VALUE),
keyboardOptions = KeyboardOptions(imeAction = ImeAction.None),
)
Column(Modifier.fillMaxHeight(), verticalArrangement = Arrangement.Center) {
// button will be disabled if there is no text
Expand Down

0 comments on commit 3fbd389

Please sign in to comment.