[go: nahoru, domu]

Don't add selectionMagnifier unless it is actually needed.

The initial SelectionContainer magnifier CL (aosp/1890195) about
doubled the first layout benchmark for the container (b/209754333).
Probably the reason is that the selection magnifier was being added
immediately, and even when there was no active dragging and the
magnifier wasn't being shown. This change only adds the magnifier
when it will actually be shown.

Verified benchmark results locally on a Pixel 3 (max of runs with
1/10/20 children in ns):
Pre-regression: 18389/21368/34316
Regression: 51886/46693/54860
Fix: 15986/17695/28093

Test: No new tests, verified benchmark results manually.
Fixes: b/209754333
Change-Id: Iacbc95443956db7d27f43d175702bd04c112d7f3
diff --git a/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/text/selection/SelectionManager.kt b/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/text/selection/SelectionManager.kt
index 353a734..63e6ce0 100644
--- a/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/text/selection/SelectionManager.kt
+++ b/compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/text/selection/SelectionManager.kt
@@ -132,7 +132,7 @@
                 false
             }
         }
-        .selectionMagnifier(this)
+        .then(if (shouldShowMagnifier) Modifier.selectionMagnifier(this) else Modifier)
 
     private var previousPosition: Offset? = null
     /**
@@ -189,6 +189,8 @@
     var draggingHandle: Handle? by mutableStateOf(null)
         private set
 
+    private val shouldShowMagnifier get() = draggingHandle != null
+
     init {
         selectionRegistrar. selectableId ->
             if (