forked from gkd-kit/gkd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
198 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
app/src/main/java/li/songe/gkd/ui/component/AppBarTextField.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package li.songe.gkd.ui.component | ||
|
||
import androidx.compose.foundation.interaction.MutableInteractionSource | ||
import androidx.compose.foundation.layout.PaddingValues | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.heightIn | ||
import androidx.compose.foundation.text.BasicTextField | ||
import androidx.compose.foundation.text.KeyboardActions | ||
import androidx.compose.foundation.text.KeyboardOptions | ||
import androidx.compose.foundation.text.selection.LocalTextSelectionColors | ||
import androidx.compose.material3.LocalTextStyle | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Text | ||
import androidx.compose.material3.TextFieldDefaults | ||
import androidx.compose.material3.TextFieldDefaults.indicatorLine | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.CompositionLocalProvider | ||
import androidx.compose.runtime.SideEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.focus.FocusRequester | ||
import androidx.compose.ui.focus.focusRequester | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.graphics.SolidColor | ||
import androidx.compose.ui.graphics.takeOrElse | ||
import androidx.compose.ui.text.TextRange | ||
import androidx.compose.ui.text.TextStyle | ||
import androidx.compose.ui.text.input.TextFieldValue | ||
import androidx.compose.ui.text.input.VisualTransformation | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.unit.sp | ||
|
||
/** | ||
* https://stackoverflow.com/questions/73664765 | ||
*/ | ||
@Composable | ||
fun AppBarTextField( | ||
value: String, | ||
onValueChange: (String) -> Unit, | ||
hint: String, | ||
modifier: Modifier = Modifier, | ||
keyboardOptions: KeyboardOptions = KeyboardOptions.Default, | ||
keyboardActions: KeyboardActions = KeyboardActions.Default, | ||
) { | ||
val interactionSource = remember { MutableInteractionSource() } | ||
val textStyle = LocalTextStyle.current | ||
// make sure there is no background color in the decoration box | ||
val colors = TextFieldDefaults.colors( | ||
focusedContainerColor = Color.Unspecified, | ||
unfocusedContainerColor = Color.Unspecified, | ||
disabledContainerColor = Color.Unspecified, | ||
) | ||
|
||
// If color is not provided via the text style, use content color as a default | ||
val textColor = textStyle.color.takeOrElse { | ||
MaterialTheme.colorScheme.onSurface | ||
} | ||
val mergedTextStyle = textStyle.merge(TextStyle(color = textColor, lineHeight = 50.sp)) | ||
|
||
// request focus when this composable is first initialized | ||
// val focusRequester = FocusRequester() | ||
// SideEffect { | ||
// focusRequester.requestFocus() | ||
// } | ||
|
||
// set the correct cursor position when this composable is first initialized | ||
var textFieldValue by remember { | ||
mutableStateOf(TextFieldValue(value, TextRange(value.length))) | ||
} | ||
textFieldValue = textFieldValue.copy(text = value) // make sure to keep the value updated | ||
|
||
CompositionLocalProvider( | ||
LocalTextSelectionColors provides LocalTextSelectionColors.current | ||
) { | ||
BasicTextField( | ||
value = textFieldValue, | ||
onValueChange = { | ||
textFieldValue = it | ||
// remove newlines to avoid strange layout issues, and also because singleLine=true | ||
onValueChange(it.text.replace("\n", "")) | ||
}, | ||
modifier = modifier | ||
.fillMaxWidth() | ||
.heightIn(32.dp) | ||
.indicatorLine( | ||
enabled = true, | ||
isError = false, | ||
interactionSource = interactionSource, | ||
colors = colors | ||
), | ||
// .focusRequester(focusRequester), | ||
textStyle = mergedTextStyle, | ||
cursorBrush = SolidColor(MaterialTheme.colorScheme.primary), | ||
keyboardOptions = keyboardOptions, | ||
keyboardActions = keyboardActions, | ||
interactionSource = interactionSource, | ||
singleLine = true, | ||
decorationBox = { innerTextField -> | ||
// places text field with placeholder and appropriate bottom padding | ||
TextFieldDefaults.DecorationBox( | ||
value = value, | ||
innerTextField = innerTextField, | ||
enabled = true, | ||
singleLine = true, | ||
visualTransformation = VisualTransformation.None, | ||
interactionSource = interactionSource, | ||
isError = false, | ||
placeholder = { Text(text = hint) }, | ||
colors = colors, | ||
contentPadding = PaddingValues(bottom = 4.dp), | ||
) | ||
}, | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters