Skip to content

Commit

Permalink
set max width of textfield
Browse files Browse the repository at this point in the history
  • Loading branch information
avan1235 committed Feb 6, 2024
1 parent bebe53a commit cb37136
Showing 1 changed file with 46 additions and 30 deletions.
76 changes: 46 additions & 30 deletions composeApp/src/commonMain/kotlin/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ private fun ShortenRequest(
space: Dp = 8.dp,
) {
BoxWithConstraints {
val maxWidth = maxWidth
if (maxHeight > maxWidth) {
Column(
modifier = Modifier.fillMaxWidth().padding(16.dp),
Expand All @@ -123,7 +124,7 @@ private fun ShortenRequest(
),
horizontalAlignment = Alignment.CenterHorizontally,
) {
ShortenRequestElements(client, onResponse, fillMaxWidth = true)
ShortenRequestElements(client, onResponse, fillMaxWidth = true, maxTextFieldWidth = maxWidth / 2)
}
} else {
Row(
Expand All @@ -134,57 +135,37 @@ private fun ShortenRequest(
),
verticalAlignment = Alignment.CenterVertically,
) {
ShortenRequestElements(client, onResponse, fillMaxWidth = false)
ShortenRequestElements(client, onResponse, fillMaxWidth = false, maxTextFieldWidth = maxWidth / 2)
}
}
}
}

@OptIn(ExperimentalMaterialApi::class)
@Composable
private fun ShortenRequestElements(
client: HttpClient,
onResponse: (String) -> Unit,
fillMaxWidth: Boolean,
maxTextFieldWidth: Dp,
) {
var url by remember { mutableStateOf("") }
val scope = rememberCoroutineScope()
var expanded by remember { mutableStateOf(false) }
var shortenedProtocol by remember { mutableStateOf(ShortenedProtocol.entries.first()) }

ExposedDropdownMenuBox(
expanded = expanded,
onExpandedChange = { expanded = it },
) {
OutlinedTextField(
value = shortenedProtocol.presentableName,
onValueChange = {},
readOnly = true,
modifier = Modifier.height(50.dp).applyIf(fillMaxWidth) { fillMaxWidth() }.width(128.dp),
trailingIcon = { ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded) },
)
ExposedDropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false }
) {
ShortenedProtocol.entries.forEach { protocol ->
DropdownMenuItem(
content = { Text(protocol.presentableName) },
onClick = {
shortenedProtocol = protocol
expanded = false
})
}
}
}
ProtocolChooser(
protocol = shortenedProtocol,
onChange = { shortenedProtocol = it },
fillMaxWidth = fillMaxWidth
)
val focusRequester = remember { FocusRequester() }
OutlinedTextField(
value = url,
onValueChange = { url = it },
modifier = Modifier
.focusRequester(focusRequester)
.height(50.dp)
.applyIf(fillMaxWidth) { fillMaxWidth() },
.applyIf(fillMaxWidth) { fillMaxWidth() }
.applyIf(!fillMaxWidth) { widthIn(max = maxTextFieldWidth) },
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
keyboardActions = KeyboardActions(
onDone = {
Expand Down Expand Up @@ -216,6 +197,41 @@ private fun ShortenRequestElements(
}
}

@OptIn(ExperimentalMaterialApi::class)
@Composable
private fun ProtocolChooser(
protocol: ShortenedProtocol,
onChange: (ShortenedProtocol) -> Unit,
fillMaxWidth: Boolean,
) {
var expanded by remember { mutableStateOf(false) }
ExposedDropdownMenuBox(
expanded = expanded,
onExpandedChange = { expanded = it },
) {
OutlinedTextField(
value = protocol.presentableName,
onValueChange = {},
readOnly = true,
modifier = Modifier.height(50.dp).applyIf(fillMaxWidth) { fillMaxWidth() }.width(128.dp),
trailingIcon = { ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded) },
)
ExposedDropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false }
) {
ShortenedProtocol.entries.forEach { protocol ->
DropdownMenuItem(
content = { Text(protocol.presentableName) },
onClick = {
onChange(protocol)
expanded = false
})
}
}
}
}

private fun normalizeUrl(url: String, protocol: ShortenedProtocol): String {
val protocolDelimiter = "://"
val idx = url.indexOf(protocolDelimiter)
Expand Down

0 comments on commit cb37136

Please sign in to comment.