forked from androidx/androidx
-
Notifications
You must be signed in to change notification settings - Fork 121
Migrate Modifier.textFieldDraw from Modifier.composed to Modifier.Node #2859
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c882fdf
Migrate Modifier.textFieldDraw from Modifier.composed to Modifier.Node
svastven e300315
Replace unused DelegatingNode with Modifier.Node
svastven 0bd4e04
Remove unused import
svastven d5a1121
Make TextFieldDrawElement a data class
svastven 2131b28
Update compose/foundation/foundation/src/iosMain/kotlin/androidx/comp…
svastven 57c39fd
Remove DefaultTextFieldOnDrawBehind from commonMain
svastven 9123443
Revert commonMain changes
svastven fa090a2
Move onDraw from lambda to function
svastven File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -22,9 +22,18 @@ import androidx.compose.runtime.LaunchedEffect | |
| import androidx.compose.ui.InternalComposeUiApi | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.composed | ||
| import androidx.compose.ui.draw.drawBehind | ||
| import androidx.compose.ui.graphics.Brush | ||
| import androidx.compose.ui.graphics.drawscope.ContentDrawScope | ||
| import androidx.compose.ui.graphics.drawscope.DrawScope | ||
| import androidx.compose.ui.graphics.drawscope.drawIntoCanvas | ||
| import androidx.compose.ui.node.CompositionLocalConsumerModifierNode | ||
| import androidx.compose.ui.node.DrawModifierNode | ||
| import androidx.compose.ui.node.ModifierNodeElement | ||
| import androidx.compose.ui.node.ObserverModifierNode | ||
| import androidx.compose.ui.node.currentValueOf | ||
| import androidx.compose.ui.node.invalidateDraw | ||
| import androidx.compose.ui.node.observeReads | ||
| import androidx.compose.ui.platform.InspectorInfo | ||
| import androidx.compose.ui.text.TextPainter | ||
| import androidx.compose.ui.text.input.OffsetMapping | ||
| import androidx.compose.ui.text.input.TextFieldValue | ||
|
|
@@ -61,21 +70,99 @@ internal actual fun Modifier.textFieldDraw( | |
| state: LegacyTextFieldState, | ||
| value: TextFieldValue, | ||
| offsetMapping: OffsetMapping, | ||
| ): Modifier = composed { | ||
| val nativeInputContext = LocalNativeTextInputContext.current | ||
| val usingNativeTextInput = nativeInputContext.usingNativeTextInput() | ||
| ): Modifier = this then TextFieldDrawElement(state, value, offsetMapping) | ||
|
|
||
| // iOS handles selection drawing itself in native text input mode | ||
| if (usingNativeTextInput) { | ||
| this.drawBehind { | ||
| state.layoutResult?.let { layoutResult -> | ||
| drawIntoCanvas { canvas -> | ||
| // Still need this for text rendering | ||
| TextPainter.paint(canvas, layoutResult.value) | ||
| } | ||
| private data class TextFieldDrawElement( | ||
| private val state: LegacyTextFieldState, | ||
| private val value: TextFieldValue, | ||
| private val offsetMapping: OffsetMapping, | ||
| ) : ModifierNodeElement<TextFieldDrawNode>() { | ||
|
|
||
| override fun create() = TextFieldDrawNode( | ||
| state = state, | ||
| value = value, | ||
| offsetMapping = offsetMapping, | ||
| ) | ||
|
|
||
| override fun update(node: TextFieldDrawNode) { | ||
| node.update( | ||
| state = state, | ||
| value = value, | ||
| offsetMapping = offsetMapping | ||
| ) | ||
| } | ||
|
|
||
| override fun InspectorInfo.inspectableProperties() { | ||
| name = "textFieldDraw" | ||
| properties["state"] = state | ||
| properties["value"] = value | ||
| properties["offsetMapping"] = offsetMapping | ||
| } | ||
| } | ||
|
|
||
| @OptIn(InternalComposeUiApi::class) | ||
| private class TextFieldDrawNode( | ||
| private var state: LegacyTextFieldState, | ||
| private var value: TextFieldValue, | ||
| private var offsetMapping: OffsetMapping, | ||
| ) : Modifier.Node(), | ||
| ObserverModifierNode, | ||
| CompositionLocalConsumerModifierNode, | ||
| DrawModifierNode { | ||
|
|
||
| private var onDraw: DrawScope.() -> Unit = {} | ||
| private var usingNativeTextInput: Boolean = false | ||
|
|
||
| override fun onAttach() { | ||
| super.onAttach() | ||
| onObservedReadsChanged() | ||
| updateOnDraw() | ||
| } | ||
|
|
||
| override fun onObservedReadsChanged() { | ||
| observeReads { | ||
| val usingNativeTextInput = currentValueOf(LocalNativeTextInputContext).usingNativeTextInput() | ||
| if (usingNativeTextInput != this.usingNativeTextInput) { | ||
| this.usingNativeTextInput = usingNativeTextInput | ||
| updateOnDraw() | ||
| } | ||
| } | ||
| } else { | ||
| defaultTextFieldDraw(state, value, offsetMapping) | ||
| } | ||
|
|
||
| fun update( | ||
| state: LegacyTextFieldState, | ||
| value: TextFieldValue, | ||
| offsetMapping: OffsetMapping, | ||
| ) { | ||
| this.state = state | ||
| this.value = value | ||
| this.offsetMapping = offsetMapping | ||
| updateOnDraw() | ||
| } | ||
|
|
||
| private fun updateOnDraw() { | ||
| onDraw = | ||
| if (usingNativeTextInput) { | ||
| { | ||
| // iOS handles selection drawing itself in native text input mode | ||
| // still needs this for text rendering | ||
| state.layoutResult?.let { layoutResult -> | ||
| drawIntoCanvas { canvas -> | ||
| TextPainter.paint(canvas, layoutResult.value) | ||
| } | ||
| } | ||
| } | ||
| } else { | ||
| DefaultTextFieldOnDrawBehind(state, value, offsetMapping) | ||
| } | ||
|
|
||
| invalidateDraw() | ||
| } | ||
|
|
||
| override fun ContentDrawScope.draw() = drawBehind() | ||
|
|
||
| private fun ContentDrawScope.drawBehind() { | ||
| onDraw() | ||
| drawContent() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, this is consistent with the |
||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.