1 /* 2 * Copyright 2021 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.modifier 18 19 import androidx.compose.runtime.Stable 20 import androidx.compose.ui.Modifier 21 import androidx.compose.ui.internal.JvmDefaultWithCompatibility 22 import androidx.compose.ui.platform.InspectorInfo 23 import androidx.compose.ui.platform.InspectorValueInfo 24 import androidx.compose.ui.platform.debugInspectorInfo 25 26 /** 27 * A Modifier that can be used to consume [ModifierLocal]s that were provided by other modifiers to 28 * the left of this modifier, or above this modifier in the layout tree. 29 */ 30 @Stable 31 @JvmDefaultWithCompatibility 32 interface ModifierLocalConsumer : Modifier.Element { 33 /** 34 * This function is called whenever one of the consumed values has changed. This could be called 35 * in response to the modifier being added, removed or re-ordered. 36 */ onModifierLocalsUpdatednull37 fun onModifierLocalsUpdated(scope: ModifierLocalReadScope) 38 } 39 40 /** 41 * A Modifier that can be used to consume [ModifierLocal]s that were provided by other modifiers to 42 * the left of this modifier, or above this modifier in the layout tree. 43 */ 44 @Stable 45 fun Modifier.modifierLocalConsumer(consumer: ModifierLocalReadScope.() -> Unit): Modifier { 46 return this.then( 47 ModifierLocalConsumerImpl( 48 consumer, 49 debugInspectorInfo { 50 name = "modifierLocalConsumer" 51 properties["consumer"] = consumer 52 } 53 ) 54 ) 55 } 56 57 @Stable 58 private class ModifierLocalConsumerImpl( 59 val consumer: ModifierLocalReadScope.() -> Unit, 60 debugInspectorInfo: InspectorInfo.() -> Unit 61 ) : ModifierLocalConsumer, InspectorValueInfo(debugInspectorInfo) { 62 onModifierLocalsUpdatednull63 override fun onModifierLocalsUpdated(scope: ModifierLocalReadScope) { 64 consumer.invoke(scope) 65 } 66 equalsnull67 override fun equals(other: Any?): Boolean { 68 return other is ModifierLocalConsumerImpl && other.consumer === consumer 69 } 70 hashCodenull71 override fun hashCode() = consumer.hashCode() 72 } 73