1 /*
2 * Copyright 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package androidx.compose.ui.layout
18
19 import androidx.compose.runtime.MutableState
20 import androidx.compose.runtime.Stable
21 import androidx.compose.ui.Modifier
22 import androidx.compose.ui.internal.JvmDefaultWithCompatibility
23 import androidx.compose.ui.node.LayoutAwareModifierNode
24 import androidx.compose.ui.node.ModifierNodeElement
25 import androidx.compose.ui.platform.InspectorInfo
26 import androidx.compose.ui.unit.IntSize
27
28 /**
29 * Invoked with the size of the modified Compose UI element when the element is first measured or
30 * when the size of the element changes.
31 *
32 * There are no guarantees `onSizeChanged` will not be re-invoked with the same size.
33 *
34 * Using the `onSizeChanged` size value in a [MutableState] to update layout causes the new size
35 * value to be read and the layout to be recomposed in the succeeding frame, resulting in a one
36 * frame lag.
37 *
38 * You can use `onSizeChanged` to affect drawing operations. Use [Layout] or [SubcomposeLayout] to
39 * enable the size of one component to affect the size of another.
40 *
41 * Example usage:
42 *
43 * @sample androidx.compose.ui.samples.OnSizeChangedSample
44 */
45 @Stable
onSizeChangednull46 fun Modifier.onSizeChanged(onSizeChanged: (IntSize) -> Unit) =
47 this.then(OnSizeChangedModifier(onSizeChanged = onSizeChanged))
48
49 private class OnSizeChangedModifier(private val onSizeChanged: (IntSize) -> Unit) :
50 ModifierNodeElement<OnSizeChangedNode>() {
51 override fun create(): OnSizeChangedNode = OnSizeChangedNode(onSizeChanged)
52
53 override fun update(node: OnSizeChangedNode) {
54 node.update(onSizeChanged)
55 }
56
57 override fun equals(other: Any?): Boolean {
58 if (this === other) return true
59 if (other !is OnSizeChangedModifier) return false
60
61 return onSizeChanged === other.onSizeChanged
62 }
63
64 override fun hashCode(): Int {
65 return onSizeChanged.hashCode()
66 }
67
68 override fun InspectorInfo.inspectableProperties() {
69 name = "onSizeChanged"
70 properties["onSizeChanged"] = onSizeChanged
71 }
72 }
73
74 private class OnSizeChangedNode(private var onSizeChanged: (IntSize) -> Unit) :
75 Modifier.Node(), LayoutAwareModifierNode {
76 // When onSizeChanged changes, we want to invalidate so onRemeasured is called again
77 override val shouldAutoInvalidate: Boolean = true
78 private var previousSize = IntSize(Int.MIN_VALUE, Int.MIN_VALUE)
79
updatenull80 fun update(onSizeChanged: (IntSize) -> Unit) {
81 this.onSizeChanged = onSizeChanged
82 // Reset the previous size, so when onSizeChanged changes the new lambda gets invoked,
83 // matching previous behavior
84 previousSize = IntSize(Int.MIN_VALUE, Int.MIN_VALUE)
85 }
86
onRemeasurednull87 override fun onRemeasured(size: IntSize) {
88 if (previousSize != size) {
89 onSizeChanged(size)
90 previousSize = size
91 }
92 }
93 }
94
95 /**
96 * A modifier whose [onRemeasured] is called when the layout content is remeasured. The most common
97 * usage is [onSizeChanged].
98 *
99 * Example usage:
100 *
101 * @sample androidx.compose.ui.samples.OnSizeChangedSample
102 */
103 @JvmDefaultWithCompatibility
104 interface OnRemeasuredModifier : Modifier.Element {
105 /** Called after a layout's contents have been remeasured. */
onRemeasurednull106 fun onRemeasured(size: IntSize)
107 }
108