SceneCoreEntitySizeAdapter

interface SceneCoreEntitySizeAdapter<T : Entity>


The sizing strategy used by SceneCoreEntity to control and read the size of an entity.

The developer should use onLayoutSizeChanged to apply compose layout size changes to the entity. Compose will not inherently affect the size of the Entity.

If the developer uses onLayoutSizeChanged to change the size of the entity, but currentSize is not provided, then the intrinsic size of the entity will be ignored and the layout size as determined solely by compose will be used to size the entity. If the SceneCoreEntity has no children or size modifiers then compose doesn't know how to size this node and it will be size 0, causing it not to render at all. In such a case, please do one of the following: (1) provide currentSize so compose can infer the size from the entity, (2) add a sizing modifier to control the size of the entity, or (3) remove the adapter from the SceneCoreEntity as without an adapter compose will not try to control the size of this entity.

Note that many SceneCore entities accept sizes in meter units instead of pixels. The Meter type may be used to convert from pixels to meters.

import androidx.compose.ui.platform.LocalDensity
import androidx.xr.compose.subspace.SceneCoreEntitySizeAdapter
import androidx.xr.compose.unit.IntVolumeSize
import androidx.xr.compose.unit.Meter
import androidx.xr.runtime.math.IntSize2d
import androidx.xr.scenecore.PanelEntity

val density = LocalDensity.current
val panelSizeAdapter =
    object : SceneCoreEntitySizeAdapter<PanelEntity> {
        override fun onLayoutSizeChanged(entity: PanelEntity, size: IntVolumeSize) {
            entity.sizeInPixels =
                IntSize2d(
                    Meter.fromPixel(size.width.toFloat(), density).toM().toInt(),
                    Meter.fromPixel(size.height.toFloat(), density).toM().toInt(),
                )
        }

        override fun currentSize(entity: PanelEntity): IntVolumeSize {
            return IntVolumeSize(
                width = entity.sizeInPixels.width,
                height = entity.sizeInPixels.height,
                depth = 0,
            )
        }
    }

Summary

Public functions

open IntVolumeSize?
currentSize(entity: T)

A getter method that returns the current IntVolumeSize in pixels of the entity.

Unit
onLayoutSizeChanged(entity: T, size: IntVolumeSize)

A callback that is invoked with the final layout size of the composable in pixels.

Public functions

currentSize

Added in 1.0.0-alpha13
open fun currentSize(entity: T): IntVolumeSize?

A getter method that returns the current IntVolumeSize in pixels of the entity. This isn't as critical for compose as onLayoutSizeChanged; however, this can help to inform compose of the intrinsic size of the entity.

onLayoutSizeChanged

Added in 1.0.0-alpha13
fun onLayoutSizeChanged(entity: T, size: IntVolumeSize): Unit

A callback that is invoked with the final layout size of the composable in pixels.