public class SubcomposeLayoutState(private val slotReusePolicy: SubcomposeSlotReusePolicy)
State used by SubcomposeLayout.
slotReusePolicy the policy defining what slots should be retained to be reused later.
Functions
precompose
public fun precompose(slotId: Any?, content: @Composable () -> Unit): PrecomposedSlotHandle
Composes the content for the given slotId. This makes the next scope.subcompose(slotId) call during the measure pass faster as the content is already composed.
If the slotId was precomposed already but after the future calculations ended up to not be needed anymore (meaning this slotId is not going to be used during the measure pass anytime soon) you can use PrecomposedSlotHandle.dispose on a returned object to dispose the content.
Parameters
| slotId | unique id which represents the slot to compose into. |
| content | the composable content which defines the slot. |
Return
PrecomposedSlotHandle instance which allows you to dispose the content.
createPausedPrecomposition
public fun createPausedPrecomposition(
slotId: Any?,
content: @Composable () -> Unit,
): PausedPrecomposition
Creates PausedPrecomposition, which allows to perform the composition in an incremental manner.
Parameters
| slotId | unique id which represents the slot to compose into. |
| content | the composable content which defines the slot.] |
Return
PausedPrecomposition for the given slotId. It allows to perform the composition in an incremental manner. Performing full or partial precomposition makes the next scope.subcompose(slotId) call during the measure pass faster as the content is already composed.
Members
PausedPrecomposition
public sealed interface PausedPrecomposition
A PausedPrecomposition is a subcomposition that can be composed incrementally as it supports being paused and resumed.
Pausable subcomposition can be used between frames to prepare a subcomposition before it is required by the main composition. For example, this is used in lazy lists to prepare list items in between frames to that are likely to be scrolled in. The composition is paused when the start of the next frame is near, allowing composition to be spread across multiple frames without delaying the production of the next frame.
Properties
isComplete
public val isComplete: Boolean
Returns true when the PausedPrecomposition is complete. isComplete matches the last value returned from resume. Once a PausedPrecomposition is isComplete the apply method should be called. If the apply method is not called synchronously and immediately after resume returns true then this isComplete can return false as any state changes read by the paused composition while it is paused will cause the composition to require the paused composition to need to be resumed before it is used.
Functions
resume
public fun resume(shouldPause: ShouldPauseCallback): Boolean
Resume the composition that has been paused. This method should be called until resume returns true or isComplete is true which has the same result as the last result of calling resume. The shouldPause parameter is a lambda that returns whether the composition should be paused. For example, in lazy lists this returns false until just prior to the next frame starting in which it returns true
Calling resume after it returns true or when isComplete is true will throw an exception.
Parameters
| shouldPause | A lambda that is used to determine if the composition should be paused. This lambda is called often so should be a very simple calculation. Returning true does not guarantee the composition will pause, it should only be considered a request to pause the composition. Not all composable functions are pausable and only pausable composition functions will pause. |
Return
true if the composition is complete and false if one or more calls to resume are required to complete composition.
apply
public fun apply(): PrecomposedSlotHandle
Apply the composition. This is the last step of a paused composition and is required to be called prior to the composition is usable.
Calling apply should always be proceeded with a check of isComplete before it is called and potentially calling resume in a loop until isComplete returns true. This can happen if resume returned true but apply was not synchronously called immediately afterwords. Any state that was read that changed between when resume being called and apply being called may require the paused composition to be resumed before applied.
Return
PrecomposedSlotHandle you can use to premeasure the slot as well, or to dispose the composed content.
cancel
public fun cancel()
Cancels the paused composition. This should only be used if the composition is going to be disposed and the entire composition is not going to be used.
PrecomposedSlotHandle
public interface PrecomposedSlotHandle
Instance of this interface is returned by precompose function.
Properties
placeablesCount
public val placeablesCount: Int
The amount of placeables composed into this slot.
Functions
dispose
public fun dispose()
This function allows to dispose the content for the slot which was precomposed previously via precompose.
If this slot was already used during the regular measure pass via SubcomposeMeasureScope.subcompose this function will do nothing.
This could be useful if after the future calculations this item is not anymore expected to be used during the measure pass anytime soon.
premeasure
public fun premeasure(index: Int, constraints: Constraints)
Performs synchronous measure of the placeable at the given index.
Parameters
| index | the placeable index. Should be smaller than placeablesCount. |
| constraints | Constraints to measure this placeable with. |
traverseDescendants
public fun traverseDescendants(
key: Any?,
block: (TraversableNode) -> TraverseDescendantsAction,
)
Conditionally executes block for each Modifier.Node of this Composition that is a TraversableNode with a matching key.
See androidx.compose.ui.node.traverseDescendants for the complete semantics of this function.
getSize
public fun getSize(index: Int): IntSize
Retrieves the latest measured size for a given placeable index. This will return IntSize.Zero if this is called before premeasure.