Skip to content

Commit fb106d0

Browse files
committed
Replace ComposeScene.calculateContentSize with extension function unconstrainedSize
1 parent 9b6698a commit fb106d0

File tree

6 files changed

+27
-44
lines changed

6 files changed

+27
-44
lines changed

compose/ui/ui/src/desktopMain/kotlin/androidx/compose/ui/scene/ComposeSceneMediator.desktop.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -345,12 +345,11 @@ internal class ComposeSceneMediator(
345345
ComposeFeatureFlags.redispatchUnconsumedMouseWheelEvents.value
346346

347347
/**
348-
* Provides the size of ComposeScene content inside infinity constraints
348+
* Provides the size of the scene content in infinity constraints.
349349
*
350-
* This is needed for the bridge between Compose and Swing since
351-
* in some cases, Swing's LayoutManagers need
352-
* to calculate the preferred size of the content without max/min constraints
353-
* to properly lay it out.
350+
* This is needed for the bridge between Compose and Swing since in some cases, Swing's
351+
* LayoutManagers need to calculate the preferred size of the content without max/min
352+
* constraints to properly lay it out.
354353
*
355354
* Example: Compose content inside Popup without a preferred size.
356355
* Swing will calculate the preferred size of the Compose content and set Popup's side for that.
@@ -359,7 +358,7 @@ internal class ComposeSceneMediator(
359358
*/
360359
val preferredSize: Dimension
361360
get() {
362-
val contentSize = scene.calculateContentSize()
361+
val contentSize = scene.unconstrainedSize()
363362
val scale = scene.density.density
364363
return Dimension(
365364
(contentSize.width / scale).toInt(),

compose/ui/ui/src/skikoMain/kotlin/androidx/compose/ui/ImageComposeScene.skiko.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import androidx.compose.ui.platform.WindowInfoImpl
3737
import androidx.compose.ui.scene.CanvasLayersComposeScene
3838
import androidx.compose.ui.scene.ComposeScene
3939
import androidx.compose.ui.scene.ComposeScenePointer
40+
import androidx.compose.ui.scene.unconstrainedSize
4041
import androidx.compose.ui.semantics.SemanticsOwner
4142
import androidx.compose.ui.unit.Constraints
4243
import androidx.compose.ui.unit.Density
@@ -258,7 +259,7 @@ class ImageComposeScene @ExperimentalComposeUiApi constructor(
258259
*/
259260
@Deprecated("Use calculateContentSize() instead", replaceWith = ReplaceWith("calculateContentSize()"))
260261
val contentSize: IntSize
261-
get() = scene.calculateContentSize()
262+
get() = scene.unconstrainedSize()
262263

263264
/**
264265
* Returns the current content size in infinity constraints.
@@ -268,7 +269,7 @@ class ImageComposeScene @ExperimentalComposeUiApi constructor(
268269
*/
269270
@ExperimentalComposeUiApi
270271
fun calculateContentSize(): IntSize {
271-
return scene.calculateContentSize()
272+
return scene.unconstrainedSize()
272273
}
273274

274275
/**

compose/ui/ui/src/skikoMain/kotlin/androidx/compose/ui/node/RootNodeOwner.skiko.kt

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@ internal class RootNodeOwner(
245245
}
246246
}
247247

248+
/**
249+
* Provides a way to measure Owner's content in given [constraints]
250+
* Draw/pointer and other callbacks won't be called here like in [measureAndLayout] functions
251+
*/
248252
fun <T> measuringRootWithConstraints(constraints: Constraints, block: (LayoutNode) -> T): T {
249253
return try {
250254
// TODO: is it possible to measure without reassigning root constraints?
@@ -256,21 +260,6 @@ internal class RootNodeOwner(
256260
}
257261
}
258262

259-
/**
260-
* Provides a way to measure Owner's content in given [constraints]
261-
* Draw/pointer and other callbacks won't be called here like in [measureAndLayout] functions
262-
*/
263-
fun measureInConstraints(constraints: Constraints): IntSize {
264-
return measuringRootWithConstraints(constraints) { rootNode ->
265-
// Don't use mainOwner.root.width here, as it strictly coerced by [constraints]
266-
val children = rootNode.children
267-
IntSize(
268-
width = children.fastMaxOfOrDefault(0) { it.outerCoordinator.measuredWidth },
269-
height = children.fastMaxOfOrDefault(0) { it.outerCoordinator.measuredHeight },
270-
)
271-
}
272-
}
273-
274263
fun measureAndLayout() {
275264
owner.measureAndLayout(sendPointerUpdate = true)
276265
updatePositionCacheAndDispatch()

compose/ui/ui/src/skikoMain/kotlin/androidx/compose/ui/scene/CanvasLayersComposeScene.skiko.kt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ import androidx.compose.ui.node.InternalCoreApi
4343
import androidx.compose.ui.node.RootNodeOwner
4444
import androidx.compose.ui.platform.PlatformContext
4545
import androidx.compose.ui.platform.setContent
46-
import androidx.compose.ui.unit.Constraints
4746
import androidx.compose.ui.unit.Density
4847
import androidx.compose.ui.unit.Dp
4948
import androidx.compose.ui.unit.IntOffset
@@ -200,11 +199,6 @@ private class CanvasLayersComposeSceneImpl(
200199
super.close()
201200
}
202201

203-
override fun calculateContentSize(): IntSize {
204-
check(!isClosed) { "calculateContentSize called after ComposeScene is closed" }
205-
return mainOwner.measureInConstraints(Constraints())
206-
}
207-
208202
override val measurableContent: MeasurableRootContent
209203
get() {
210204
check(!isClosed) { "measurableSceneContent called after ComposeScene is closed" }

compose/ui/ui/src/skikoMain/kotlin/androidx/compose/ui/scene/ComposeScene.skiko.kt

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import androidx.compose.ui.node.LayoutNode
4343
import androidx.compose.ui.platform.LocalLayoutDirection
4444
import androidx.compose.ui.platform.PlatformContext
4545
import androidx.compose.ui.platform.PlatformDragAndDropManager
46+
import androidx.compose.ui.unit.Constraints
4647
import androidx.compose.ui.unit.Density
4748
import androidx.compose.ui.unit.Dp
4849
import androidx.compose.ui.unit.IntSize
@@ -133,14 +134,6 @@ sealed interface ComposeScene : AutoCloseable {
133134
*/
134135
override fun close()
135136

136-
/**
137-
* Returns the current content size (in pixels) in infinity constraints.
138-
*
139-
* @throws IllegalStateException when [ComposeScene] content has lazy layouts without maximum
140-
* size bounds (e.g. LazyColumn without maximum height).
141-
*/
142-
fun calculateContentSize(): IntSize
143-
144137
/**
145138
* An object through which the composable content of the scene can be queried for its size
146139
* properties.
@@ -308,4 +301,17 @@ sealed interface ComposeScene : AutoCloseable {
308301
* Set the visual debug option that shows bounds for all nodes in the hierarchy.
309302
*/
310303
var showLayoutBounds: Boolean
311-
}
304+
}
305+
306+
/**
307+
* Returns the current content size (in pixels) in infinity constraints.
308+
*
309+
* @throws IllegalStateException when [ComposeScene] content has lazy layouts without maximum
310+
* size bounds (e.g., LazyColumn without maximum height).
311+
*/
312+
@InternalComposeUiApi
313+
fun ComposeScene.unconstrainedSize(): IntSize {
314+
return measurableContent.measuringIn(Constraints()) {
315+
IntSize(it.measuredWidth, it.measuredHeight)
316+
}
317+
}

compose/ui/ui/src/skikoMain/kotlin/androidx/compose/ui/scene/PlatformLayersComposeScene.skiko.kt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import androidx.compose.ui.node.InternalCoreApi
3131
import androidx.compose.ui.node.LayoutNode
3232
import androidx.compose.ui.node.RootNodeOwner
3333
import androidx.compose.ui.platform.setContent
34-
import androidx.compose.ui.unit.Constraints
3534
import androidx.compose.ui.unit.Density
3635
import androidx.compose.ui.unit.Dp
3736
import androidx.compose.ui.unit.IntSize
@@ -146,11 +145,6 @@ private class PlatformLayersComposeSceneImpl(
146145
super.close()
147146
}
148147

149-
override fun calculateContentSize(): IntSize {
150-
check(!isClosed) { "calculateContentSize called after ComposeScene is closed" }
151-
return mainOwner.measureInConstraints(Constraints())
152-
}
153-
154148
override val measurableContent: MeasurableRootContent
155149
get() {
156150
check(!isClosed) { "measurableSceneContent called after ComposeScene is closed" }

0 commit comments

Comments
 (0)