Skip to content

Commit

Permalink
improve keyboard handling
Browse files Browse the repository at this point in the history
  • Loading branch information
avan1235 committed Feb 6, 2024
1 parent 9589792 commit 5d93f5e
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions composeApp/src/commonMain/kotlin/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ import androidx.compose.animation.expandVertically
import androidx.compose.animation.shrinkVertically
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.ClickableText
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.input.key.*
import androidx.compose.ui.platform.LocalUriHandler
import androidx.compose.ui.text.ParagraphStyle
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
Expand All @@ -33,11 +39,18 @@ fun App() {
}
}
MaterialTheme {
var response by remember<MutableState<String?>> { mutableStateOf(null) }
Column(
modifier = Modifier.fillMaxSize(),
modifier = Modifier
.fillMaxSize()
.onKeyEvent { event ->
if (event.run { key == Key.Escape && type == KeyEventType.KeyDown }) {
response = null
true
} else false
},
verticalArrangement = Arrangement.Center,
) {
var response by remember<MutableState<String?>> { mutableStateOf(null) }
ShortenRequest(client, onResponse = { response = it })

AnimatedVisibility(
Expand Down Expand Up @@ -161,10 +174,26 @@ private fun ShortenRequestElements(
}
}
}
val focusRequester = remember { FocusRequester() }
OutlinedTextField(
value = url,
onValueChange = { url = it },
modifier = Modifier.height(50.dp).applyIf(fillMaxWidth) { fillMaxWidth() },
modifier = Modifier
.focusRequester(focusRequester)
.height(50.dp)
.applyIf(fillMaxWidth) { fillMaxWidth() },
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
keyboardActions = KeyboardActions(
onDone = {
scope.launch {
client.post<Shorten>(Shorten(normalizeUrl(url, shortenedProtocol)))
.takeIf { it.status == HttpStatusCode.OK }
?.bodyAsText()
?.let(onResponse)
}
}
),
singleLine = true,
)
Button(
modifier = Modifier.height(50.dp).applyIf(fillMaxWidth) { fillMaxWidth() },
Expand All @@ -179,6 +208,9 @@ private fun ShortenRequestElements(
) {
Text(text = "Shorten")
}
LaunchedEffect(focusRequester) {
focusRequester.requestFocus()
}
}

private fun normalizeUrl(url: String, protocol: ShortenedProtocol): String {
Expand Down

0 comments on commit 5d93f5e

Please sign in to comment.