ComposeScene



A virtual container that encapsulates Compose UI content. UI content can be constructed via setContent method and with any Composable that manipulates LayoutNode tree.

To draw content on Canvas, you can use render method.

To specify available size for the content, you should use constraints.

After ComposeScene will no longer needed, you should call close method, so all resources and subscriptions will be properly closed. Otherwise there can be a memory leak.

Summary

Public constructors

ComposeScene(
    coroutineContext: CoroutineContext,
    density: Density,
    invalidate: () -> Unit
)

Constructs ComposeScene

android

Public functions

Unit

Close all resources and subscriptions.

android
Boolean

Returns true if there are pending recompositions, renders or dispatched tasks.

android
Unit
render(canvas: Canvas, nanoTime: Long)

Render the current content on canvas.

android
Boolean

Send KeyEvent to the content.

android
Unit
sendPointerEvent(
    eventType: PointerEventType,
    position: Offset,
    scrollDelta: Offset,
    timeMillis: Long,
    type: PointerType,
    buttons: PointerButtons?,
    keyboardModifiers: PointerKeyboardModifiers?,
    nativeEvent: Any?
)

Send pointer event to the content.

android
Unit
setContent(content: @Composable () -> Unit)

Update the composition with the content described by the content composable.

android

Public properties

Constraints

Set constraints, which will be used to measure and layout content.

android
IntSize

Returns the current content size

android
Density

Density of the content which will be used to convert dp units.

android
Set<RootForTest>

All currently registered RootForTests.

android

Public constructors

ComposeScene

ComposeScene(
    coroutineContext: CoroutineContext = Dispatchers.Unconfined,
    density: Density = Density(1f),
    invalidate: () -> Unit = {}
)

Constructs ComposeScene

Parameters
coroutineContext: CoroutineContext = Dispatchers.Unconfined

Context which will be used to launch effects (LaunchedEffect, rememberCoroutineScope) and run recompositions.

density: Density = Density(1f)

Initial density of the content which will be used to convert dp units.

invalidate: () -> Unit = {}

Callback which will be called when the content need to be recomposed or rerendered. If you draw your content using render method, in this callback you should schedule the next render in your rendering loop.

Public functions

close

fun close(): Unit

Close all resources and subscriptions. Not calling this method when ComposeScene is no longer needed will cause a memory leak.

All effects launched via LaunchedEffect or rememberCoroutineScope will be cancelled (but not immediately).

After calling this method, you cannot call any other method of this ComposeScene.

hasInvalidations

fun hasInvalidations(): Boolean

Returns true if there are pending recompositions, renders or dispatched tasks. Can be called from any thread.

render

fun render(canvas: Canvas, nanoTime: Long): Unit

Render the current content on canvas. Passed nanoTime will be used to drive all animations in the content (or any other code, which uses withFrameNanos

sendKeyEvent

fun sendKeyEvent(event: KeyEvent): Boolean

Send KeyEvent to the content.

Returns
Boolean

true if the event was consumed by the content

sendPointerEvent

fun sendPointerEvent(
    eventType: PointerEventType,
    position: Offset,
    scrollDelta: Offset = Offset(0f, 0f),
    timeMillis: Long = currentMillis(),
    type: PointerType = PointerType.Mouse,
    buttons: PointerButtons? = null,
    keyboardModifiers: PointerKeyboardModifiers? = null,
    nativeEvent: Any? = null
): Unit

Send pointer event to the content.

Parameters
eventType: PointerEventType

Indicates the primary reason that the event was sent.

position: Offset

The Offset of the current pointer event, relative to the content.

scrollDelta: Offset = Offset(0f, 0f)

scroll delta for the PointerEventType.Scroll event

timeMillis: Long = currentMillis()

The time of the current pointer event, in milliseconds. The start (0) time is platform-dependent.

type: PointerType = PointerType.Mouse

The device type that produced the event, such as mouse, or touch.

buttons: PointerButtons? = null

Contains the state of pointer buttons (e.g. mouse and stylus buttons).

keyboardModifiers: PointerKeyboardModifiers? = null

Contains the state of modifier keys, such as Shift, Control, and Alt, as well as the state of the lock keys, such as Caps Lock and Num Lock.

nativeEvent: Any? = null

The original native event.

setContent

fun setContent(content: @Composable () -> Unit): Unit

Update the composition with the content described by the content composable. After this has been called the changes to produce the initial composition has been calculated and applied to the composition.

Will throw an IllegalStateException if the composition has been disposed.

Parameters
content: @Composable () -> Unit

Content of the ComposeScene

Public properties

constraints

var constraintsConstraints

Set constraints, which will be used to measure and layout content.

contentSize

val contentSizeIntSize

Returns the current content size

density

var densityDensity

Density of the content which will be used to convert dp units.

roots

val rootsSet<RootForTest>

All currently registered RootForTests. After calling setContent the first root will be added. If there is an any Popup is present in the content, it will be added as another RootForTest