forked from facebook/react-native
-
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.
Use compose to render Switch in Fabric
Summary: This change is a proof of concept to try Compose integration with RN component on Android by measuring and drawing Switch component through a Composable function. To interface Composable nodes with the view system, we use a "classic" view interoperability layer to plug Compose-based Switch into existing ViewManager and propagate props and events as we do with “classic” views. Fabric also requires initial measure during layout, which was implemented through custom background `Recomposer` and `LayoutNode` hacks to avoid creating full-blown view. Differential Revision: D31657259 fbshipit-source-id: ad5d2434d8c783d03f93b829e46e7e09ef5c4564
- Loading branch information
1 parent
0fccbd5
commit ac9eb36
Showing
9 changed files
with
606 additions
and
98 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
87 changes: 87 additions & 0 deletions
87
ReactAndroid/src/main/java/com/facebook/react/kotlin/BackgroundMeasure.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,87 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
package com.facebook.react.kotlin | ||
|
||
import android.content.Context | ||
import android.widget.FrameLayout | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.Composition | ||
import androidx.compose.runtime.CompositionLocalProvider | ||
import androidx.compose.runtime.MonotonicFrameClock | ||
import androidx.compose.runtime.Recomposer | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.focus.FocusDirection | ||
import androidx.compose.ui.geometry.Offset | ||
import androidx.compose.ui.geometry.Rect | ||
import androidx.compose.ui.input.key.KeyEvent | ||
import androidx.compose.ui.layout.Measurable | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.platform.LocalDensity | ||
import androidx.compose.ui.platform.LocalFontLoader | ||
import androidx.compose.ui.platform.LocalLayoutDirection | ||
import androidx.compose.ui.platform.LocalView | ||
import androidx.compose.ui.platform.LocalViewConfiguration | ||
import androidx.compose.ui.unit.Constraints | ||
import androidx.compose.ui.unit.IntSize | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.CoroutineStart | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
|
||
class BackgroundMeasure(private val context: Context) { | ||
private val view = FrameLayout(context) | ||
private val owner = | ||
object : ComposeShims.BackgroundMeasureOwner(context) { | ||
// These methods use inline classes, so we have to override them here | ||
override fun calculateLocalPosition(positionInWindow: Offset): Offset = positionInWindow | ||
override fun calculatePositionInWindow(localPosition: Offset): Offset = localPosition | ||
override fun getFocusDirection(keyEvent: KeyEvent): FocusDirection? = null | ||
override fun requestRectangleOnScreen(rect: Rect) {} | ||
} | ||
private val root = owner.root | ||
private val applier = ComposeShims.createApplier(root) | ||
|
||
val clock = | ||
object : MonotonicFrameClock { | ||
override suspend fun <R> withFrameNanos(onFrame: (frameTimeNanos: Long) -> R): R = | ||
onFrame(System.nanoTime()) | ||
} | ||
val coroutineContext = Dispatchers.Unconfined + clock | ||
val recomposer = Recomposer(coroutineContext) | ||
val composition = Composition(applier, recomposer) | ||
|
||
/** Synchronously (I hope?) measures Composable on a current thread (at least seems like so?) */ | ||
fun measureComposable(constraints: Constraints, content: @Composable () -> Unit): IntSize { | ||
composition.setContent { | ||
CompositionLocalProvider( | ||
// See ProvideCommonCompositionLocals or ProvideAndroidCompositionLocals for a full list | ||
// Here I only added things until Text composable stopped crashing | ||
LocalDensity.provides(owner.density), | ||
LocalFontLoader.provides(owner.fontLoader), | ||
LocalContext.provides(context), | ||
LocalLayoutDirection.provides(owner.layoutDirection), | ||
LocalViewConfiguration.provides(owner.viewConfiguration), | ||
LocalView.provides(view), | ||
content = content) | ||
} | ||
|
||
val runRecomposeJob = | ||
CoroutineScope(coroutineContext).launch(start = CoroutineStart.UNDISPATCHED) { | ||
recomposer.runRecomposeAndApplyChanges() | ||
} | ||
|
||
ComposeShims.attachOwner(root, owner) | ||
owner.nodes.forEach { ComposeShims.setLayoutRequired(it) } | ||
(root as Measurable).measure(constraints) | ||
|
||
runRecomposeJob.cancel() | ||
|
||
return IntSize(ComposeShims.getNodeWidth(root), ComposeShims.getNodeHeight(root)) | ||
} | ||
} |
Oops, something went wrong.