mirror of
https://github.com/Myzel394/NumberHub.git
synced 2025-06-19 08:45:27 +02:00
Don't remember toolbar
This commit is contained in:
parent
6b47aeaaae
commit
be78dd2347
@ -85,25 +85,23 @@ fun ExpressionTextField(
|
|||||||
onCursorChange(TextRange(value.selection.end))
|
onCursorChange(TextRange(value.selection.end))
|
||||||
}
|
}
|
||||||
|
|
||||||
val textToolbar: UnittoTextToolbar = remember(readOnly) {
|
val textToolbar: UnittoTextToolbar = if (readOnly) {
|
||||||
if (readOnly) {
|
UnittoTextToolbar(
|
||||||
UnittoTextToolbar(
|
view = localView,
|
||||||
view = localView,
|
copyCallback = ::copyCallback,
|
||||||
copyCallback = ::copyCallback,
|
)
|
||||||
)
|
} else {
|
||||||
} else {
|
UnittoTextToolbar(
|
||||||
UnittoTextToolbar(
|
view = localView,
|
||||||
view = localView,
|
copyCallback = ::copyCallback,
|
||||||
copyCallback = ::copyCallback,
|
pasteCallback = {
|
||||||
pasteCallback = {
|
pasteCallback(clipboardManager.getText()?.text?.clearAndFilterExpression(formatterSymbols) ?: "")
|
||||||
pasteCallback(clipboardManager.getText()?.text?.clearAndFilterExpression(formatterSymbols) ?: "")
|
},
|
||||||
},
|
cutCallback = {
|
||||||
cutCallback = {
|
clipboardManager.copyWithoutGrouping(value, formatterSymbols)
|
||||||
clipboardManager.copyWithoutGrouping(value, formatterSymbols)
|
cutCallback()
|
||||||
cutCallback()
|
}
|
||||||
}
|
)
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
AutoSizableTextField(
|
AutoSizableTextField(
|
||||||
@ -114,8 +112,6 @@ fun ExpressionTextField(
|
|||||||
minRatio = minRatio,
|
minRatio = minRatio,
|
||||||
onValueChange = { onCursorChange(it.selection) },
|
onValueChange = { onCursorChange(it.selection) },
|
||||||
readOnly = readOnly,
|
readOnly = readOnly,
|
||||||
showToolbar = textToolbar::showMenu,
|
|
||||||
hideToolbar = textToolbar::hide,
|
|
||||||
visualTransformation = expressionTransformer,
|
visualTransformation = expressionTransformer,
|
||||||
placeholder = placeholder,
|
placeholder = placeholder,
|
||||||
textToolbar = textToolbar,
|
textToolbar = textToolbar,
|
||||||
@ -169,8 +165,6 @@ fun UnformattedTextField(
|
|||||||
minRatio = minRatio,
|
minRatio = minRatio,
|
||||||
onValueChange = { onCursorChange(it.selection) },
|
onValueChange = { onCursorChange(it.selection) },
|
||||||
readOnly = readOnly,
|
readOnly = readOnly,
|
||||||
showToolbar = textToolbar::showMenu,
|
|
||||||
hideToolbar = textToolbar::hide,
|
|
||||||
placeholder = placeholder,
|
placeholder = placeholder,
|
||||||
textToolbar = textToolbar
|
textToolbar = textToolbar
|
||||||
)
|
)
|
||||||
@ -186,8 +180,6 @@ private fun AutoSizableTextField(
|
|||||||
minRatio: Float = 1f,
|
minRatio: Float = 1f,
|
||||||
onValueChange: (TextFieldValue) -> Unit,
|
onValueChange: (TextFieldValue) -> Unit,
|
||||||
readOnly: Boolean = false,
|
readOnly: Boolean = false,
|
||||||
showToolbar: (rect: Rect) -> Unit = {},
|
|
||||||
hideToolbar: () -> Unit = {},
|
|
||||||
visualTransformation: VisualTransformation = VisualTransformation.None,
|
visualTransformation: VisualTransformation = VisualTransformation.None,
|
||||||
placeholder: String? = null,
|
placeholder: String? = null,
|
||||||
textToolbar: UnittoTextToolbar
|
textToolbar: UnittoTextToolbar
|
||||||
@ -199,57 +191,59 @@ private fun AutoSizableTextField(
|
|||||||
var nFontSize: TextUnit by remember { mutableStateOf(0.sp) }
|
var nFontSize: TextUnit by remember { mutableStateOf(0.sp) }
|
||||||
var minFontSize: TextUnit
|
var minFontSize: TextUnit
|
||||||
|
|
||||||
BoxWithConstraints(
|
CompositionLocalProvider(
|
||||||
modifier = modifier,
|
LocalTextInputService provides null,
|
||||||
contentAlignment = Alignment.BottomStart
|
LocalTextToolbar provides textToolbar
|
||||||
) {
|
) {
|
||||||
with(density) {
|
val localTextToolbar = LocalTextToolbar.current
|
||||||
// Cursor handle is not visible without this, 0.836f is the minimum required factor here
|
|
||||||
nFontSize = maxHeight.toSp() * 0.83f
|
|
||||||
minFontSize = nFontSize * minRatio
|
|
||||||
}
|
|
||||||
|
|
||||||
// Modified: https://blog.canopas.com/autosizing-textfield-in-jetpack-compose-7a80f0270853
|
BoxWithConstraints(
|
||||||
val calculateParagraph = @Composable {
|
modifier = modifier,
|
||||||
Paragraph(
|
contentAlignment = Alignment.BottomStart
|
||||||
text = formattedValue,
|
|
||||||
style = textStyle.copy(fontSize = nFontSize),
|
|
||||||
constraints = Constraints(
|
|
||||||
maxWidth = ceil(with(density) { maxWidth.toPx() }).toInt()
|
|
||||||
),
|
|
||||||
density = density,
|
|
||||||
fontFamilyResolver = createFontFamilyResolver(LocalContext.current),
|
|
||||||
spanStyles = listOf(),
|
|
||||||
placeholders = listOf(),
|
|
||||||
maxLines = 1,
|
|
||||||
ellipsis = false
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
var intrinsics = calculateParagraph()
|
|
||||||
with(density) {
|
|
||||||
while ((intrinsics.maxIntrinsicWidth > maxWidth.toPx()) && nFontSize >= minFontSize) {
|
|
||||||
nFontSize *= scaleFactor
|
|
||||||
intrinsics = calculateParagraph()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val nTextStyle = textStyle.copy(
|
|
||||||
// https://issuetracker.google.com/issues/266470454
|
|
||||||
// textAlign = TextAlign.End,
|
|
||||||
fontSize = nFontSize
|
|
||||||
)
|
|
||||||
var offset = Offset.Zero
|
|
||||||
|
|
||||||
CompositionLocalProvider(
|
|
||||||
LocalTextInputService provides null,
|
|
||||||
LocalTextToolbar provides textToolbar
|
|
||||||
) {
|
) {
|
||||||
|
with(density) {
|
||||||
|
// Cursor handle is not visible without this, 0.836f is the minimum required factor here
|
||||||
|
nFontSize = maxHeight.toSp() * 0.83f
|
||||||
|
minFontSize = nFontSize * minRatio
|
||||||
|
}
|
||||||
|
|
||||||
|
// Modified: https://blog.canopas.com/autosizing-textfield-in-jetpack-compose-7a80f0270853
|
||||||
|
val calculateParagraph = @Composable {
|
||||||
|
Paragraph(
|
||||||
|
text = formattedValue,
|
||||||
|
style = textStyle.copy(fontSize = nFontSize),
|
||||||
|
constraints = Constraints(
|
||||||
|
maxWidth = ceil(with(density) { maxWidth.toPx() }).toInt()
|
||||||
|
),
|
||||||
|
density = density,
|
||||||
|
fontFamilyResolver = createFontFamilyResolver(LocalContext.current),
|
||||||
|
spanStyles = listOf(),
|
||||||
|
placeholders = listOf(),
|
||||||
|
maxLines = 1,
|
||||||
|
ellipsis = false
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
var intrinsics = calculateParagraph()
|
||||||
|
with(density) {
|
||||||
|
while ((intrinsics.maxIntrinsicWidth > maxWidth.toPx()) && nFontSize >= minFontSize) {
|
||||||
|
nFontSize *= scaleFactor
|
||||||
|
intrinsics = calculateParagraph()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val nTextStyle = textStyle.copy(
|
||||||
|
// https://issuetracker.google.com/issues/266470454
|
||||||
|
// textAlign = TextAlign.End,
|
||||||
|
fontSize = nFontSize
|
||||||
|
)
|
||||||
|
var offset = Offset.Zero
|
||||||
|
|
||||||
BasicTextField(
|
BasicTextField(
|
||||||
value = textValue,
|
value = textValue,
|
||||||
onValueChange = {
|
onValueChange = {
|
||||||
showToolbar(Rect(offset, 0f))
|
localTextToolbar.showMenu(Rect(offset, 0f))
|
||||||
hideToolbar()
|
localTextToolbar.hide()
|
||||||
onValueChange(it)
|
onValueChange(it)
|
||||||
},
|
},
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
@ -258,10 +252,10 @@ private fun AutoSizableTextField(
|
|||||||
interactionSource = remember { MutableInteractionSource() },
|
interactionSource = remember { MutableInteractionSource() },
|
||||||
indication = null,
|
indication = null,
|
||||||
onClick = {
|
onClick = {
|
||||||
hideToolbar()
|
localTextToolbar.hide()
|
||||||
focusRequester.requestFocus()
|
focusRequester.requestFocus()
|
||||||
onValueChange(value.copy(selection = TextRange.Zero))
|
onValueChange(value.copy(selection = TextRange.Zero))
|
||||||
showToolbar(Rect(offset, 0f))
|
localTextToolbar.showMenu(Rect(offset, 0f))
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
.widthIn(max = with(density) { intrinsics.width.toDp() })
|
.widthIn(max = with(density) { intrinsics.width.toDp() })
|
||||||
|
Loading…
x
Reference in New Issue
Block a user