Skip to content

Commit 9f211ca

Browse files
Open HostDefaultKey for extension
Convert `HostDefaultKey` from an expect class to a common interface. This enables keys to support multiple lookup mechanisms through interface composition rather than being restricted by single inheritance, allowing a single key to target various host systems. Introduce `ViewTreeHostDefaultKey` on Android to provide a standard contract for keys resolving via View tags. Update the provider logic to leverage this hierarchy for type-safe retrieval. RelNote: "Convert HostDefaultKey to an interface to allow keys to implement multiple lookup mechanisms, such as Android ViewTree tags, improving extensibility for custom hosting environments." Bug: 478146897 Test: existing passes Change-Id: I917a2b6d3e3b9e2bf6c44c0e63815e663940ec8b
1 parent 5a20fda commit 9f211ca

10 files changed

Lines changed: 93 additions & 90 deletions

File tree

‎compose/runtime/runtime/api/current.txt‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -419,11 +419,7 @@ package androidx.compose.runtime {
419419
property @androidx.compose.runtime.snapshots.AutoboxingStateValueProperty(preferredPropertyName="floatValue") public default Float value;
420420
}
421421

422-
public final class HostDefaultKey<T> {
423-
ctor public HostDefaultKey();
424-
ctor public HostDefaultKey(int id);
425-
method @InaccessibleFromKotlin @IdRes public int getId();
426-
property @IdRes public int id;
422+
public interface HostDefaultKey<T> {
427423
}
428424

429425
public interface HostDefaultProvider {
@@ -809,6 +805,11 @@ package androidx.compose.runtime {
809805
method @BytecodeOnly public static <V> void update-impl(androidx.compose.runtime.Composer!, V!, kotlin.jvm.functions.Function2<? super T!,? super V!,kotlin.Unit!>);
810806
}
811807

808+
public interface ViewTreeHostDefaultKey<T> extends androidx.compose.runtime.HostDefaultKey<T> {
809+
method @InaccessibleFromKotlin @IdRes public int getTagKey();
810+
property @IdRes public abstract int tagKey;
811+
}
812+
812813
@Deprecated public typealias CheckResult = androidx.annotation.CheckResult;
813814

814815
public typealias CompositeKeyHashCode = long;

‎compose/runtime/runtime/api/restricted_current.txt‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -460,11 +460,7 @@ package androidx.compose.runtime {
460460
property @androidx.compose.runtime.snapshots.AutoboxingStateValueProperty(preferredPropertyName="floatValue") public default Float value;
461461
}
462462

463-
public final class HostDefaultKey<T> {
464-
ctor public HostDefaultKey();
465-
ctor public HostDefaultKey(int id);
466-
method @InaccessibleFromKotlin @IdRes public int getId();
467-
property @IdRes public int id;
463+
public interface HostDefaultKey<T> {
468464
}
469465

470466
public interface HostDefaultProvider {
@@ -856,6 +852,11 @@ package androidx.compose.runtime {
856852
property @kotlin.PublishedApi internal androidx.compose.runtime.Composer composer;
857853
}
858854

855+
public interface ViewTreeHostDefaultKey<T> extends androidx.compose.runtime.HostDefaultKey<T> {
856+
method @InaccessibleFromKotlin @IdRes public int getTagKey();
857+
property @IdRes public abstract int tagKey;
858+
}
859+
859860
@Deprecated public typealias CheckResult = androidx.annotation.CheckResult;
860861

861862
public typealias CompositeKeyHashCode = long;

‎compose/runtime/runtime/bcv/native/current.txt‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ abstract interface <#A: kotlin/Any?> androidx.compose.runtime/Applier { // andro
177177

178178
abstract interface <#A: kotlin/Any?> androidx.compose.runtime/CompositionServiceKey // androidx.compose.runtime/CompositionServiceKey|null[0]
179179

180+
abstract interface <#A: kotlin/Any?> androidx.compose.runtime/HostDefaultKey // androidx.compose.runtime/HostDefaultKey|null[0]
181+
180182
abstract interface <#A: kotlin/Any?> androidx.compose.runtime/MutableState : androidx.compose.runtime/State<#A> { // androidx.compose.runtime/MutableState|null[0]
181183
abstract var value // androidx.compose.runtime/MutableState.value|{}value[0]
182184
abstract fun <get-value>(): #A // androidx.compose.runtime/MutableState.value.<get-value>|<get-value>(){}[0]
@@ -731,10 +733,6 @@ final class <#A: kotlin/Any?> androidx.compose.runtime.snapshots/SnapshotStateSe
731733
final fun toString(): kotlin/String // androidx.compose.runtime.snapshots/SnapshotStateSet.toString|toString(){}[0]
732734
}
733735

734-
final class <#A: kotlin/Any?> androidx.compose.runtime/HostDefaultKey { // androidx.compose.runtime/HostDefaultKey|null[0]
735-
constructor <init>() // androidx.compose.runtime/HostDefaultKey.<init>|<init>(){}[0]
736-
}
737-
738736
final class <#A: kotlin/Any?> androidx.compose.runtime/ProvidedValue { // androidx.compose.runtime/ProvidedValue|null[0]
739737
final val compositionLocal // androidx.compose.runtime/ProvidedValue.compositionLocal|{}compositionLocal[0]
740738
final fun <get-compositionLocal>(): androidx.compose.runtime/CompositionLocal<#A> // androidx.compose.runtime/ProvidedValue.compositionLocal.<get-compositionLocal>|<get-compositionLocal>(){}[0]
@@ -1193,7 +1191,6 @@ final val androidx.compose.runtime/androidx_compose_runtime_CompositionLocal$sta
11931191
final val androidx.compose.runtime/androidx_compose_runtime_CompositionLocalContext$stableprop // androidx.compose.runtime/androidx_compose_runtime_CompositionLocalContext$stableprop|#static{}androidx_compose_runtime_CompositionLocalContext$stableprop[0]
11941192
final val androidx.compose.runtime/androidx_compose_runtime_DerivedSnapshotState_ResultRecord$stableprop // androidx.compose.runtime/androidx_compose_runtime_DerivedSnapshotState_ResultRecord$stableprop|#static{}androidx_compose_runtime_DerivedSnapshotState_ResultRecord$stableprop[0]
11951193
final val androidx.compose.runtime/androidx_compose_runtime_DisposableEffectScope$stableprop // androidx.compose.runtime/androidx_compose_runtime_DisposableEffectScope$stableprop|#static{}androidx_compose_runtime_DisposableEffectScope$stableprop[0]
1196-
final val androidx.compose.runtime/androidx_compose_runtime_HostDefaultKey$stableprop // androidx.compose.runtime/androidx_compose_runtime_HostDefaultKey$stableprop|#static{}androidx_compose_runtime_HostDefaultKey$stableprop[0]
11971194
final val androidx.compose.runtime/androidx_compose_runtime_MovableContent$stableprop // androidx.compose.runtime/androidx_compose_runtime_MovableContent$stableprop|#static{}androidx_compose_runtime_MovableContent$stableprop[0]
11981195
final val androidx.compose.runtime/androidx_compose_runtime_MovableContentState$stableprop // androidx.compose.runtime/androidx_compose_runtime_MovableContentState$stableprop|#static{}androidx_compose_runtime_MovableContentState$stableprop[0]
11991196
final val androidx.compose.runtime/androidx_compose_runtime_MovableContentStateReference$stableprop // androidx.compose.runtime/androidx_compose_runtime_MovableContentStateReference$stableprop|#static{}androidx_compose_runtime_MovableContentStateReference$stableprop[0]
@@ -1403,7 +1400,6 @@ final fun androidx.compose.runtime/androidx_compose_runtime_CompositionLocal$sta
14031400
final fun androidx.compose.runtime/androidx_compose_runtime_CompositionLocalContext$stableprop_getter(): kotlin/Int // androidx.compose.runtime/androidx_compose_runtime_CompositionLocalContext$stableprop_getter|androidx_compose_runtime_CompositionLocalContext$stableprop_getter(){}[0]
14041401
final fun androidx.compose.runtime/androidx_compose_runtime_DerivedSnapshotState_ResultRecord$stableprop_getter(): kotlin/Int // androidx.compose.runtime/androidx_compose_runtime_DerivedSnapshotState_ResultRecord$stableprop_getter|androidx_compose_runtime_DerivedSnapshotState_ResultRecord$stableprop_getter(){}[0]
14051402
final fun androidx.compose.runtime/androidx_compose_runtime_DisposableEffectScope$stableprop_getter(): kotlin/Int // androidx.compose.runtime/androidx_compose_runtime_DisposableEffectScope$stableprop_getter|androidx_compose_runtime_DisposableEffectScope$stableprop_getter(){}[0]
1406-
final fun androidx.compose.runtime/androidx_compose_runtime_HostDefaultKey$stableprop_getter(): kotlin/Int // androidx.compose.runtime/androidx_compose_runtime_HostDefaultKey$stableprop_getter|androidx_compose_runtime_HostDefaultKey$stableprop_getter(){}[0]
14071403
final fun androidx.compose.runtime/androidx_compose_runtime_MovableContent$stableprop_getter(): kotlin/Int // androidx.compose.runtime/androidx_compose_runtime_MovableContent$stableprop_getter|androidx_compose_runtime_MovableContent$stableprop_getter(){}[0]
14081404
final fun androidx.compose.runtime/androidx_compose_runtime_MovableContentState$stableprop_getter(): kotlin/Int // androidx.compose.runtime/androidx_compose_runtime_MovableContentState$stableprop_getter|androidx_compose_runtime_MovableContentState$stableprop_getter(){}[0]
14091405
final fun androidx.compose.runtime/androidx_compose_runtime_MovableContentStateReference$stableprop_getter(): kotlin/Int // androidx.compose.runtime/androidx_compose_runtime_MovableContentStateReference$stableprop_getter|androidx_compose_runtime_MovableContentStateReference$stableprop_getter(){}[0]
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2026 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.runtime
18+
19+
import android.view.View
20+
import androidx.annotation.IdRes
21+
22+
/**
23+
* An Android-specific [HostDefaultKey] that identifies values stored within the [View] hierarchy
24+
* using Resource ID tags.
25+
*
26+
* This key is specifically designed for use with `ViewTreeHostDefaultProvider`. It allows the
27+
* runtime to resolve dependencies by traversing upward through the view tree—including across
28+
* disjoint parents like Dialogs and Popups—to find a value associated with the provided [tagKey].
29+
*
30+
* @param T The type of the value associated with this key.
31+
* @param tagKey The Android Resource ID used as the tag key in [android.view.View.getTag].
32+
*/
33+
public interface ViewTreeHostDefaultKey<T> : HostDefaultKey<T> {
34+
/** The Android Resource ID used as the tag key to retrieve the value from a View's tags. */
35+
@get:IdRes public val tagKey: Int
36+
}

compose/runtime/runtime/src/androidMain/kotlin/androidx/compose/runtime/HostDefaultProvider.android.kt renamed to compose/runtime/runtime/src/commonMain/kotlin/androidx/compose/runtime/HostDefaultKey.kt

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,16 @@
1616

1717
package androidx.compose.runtime
1818

19-
import androidx.annotation.IdRes
20-
21-
public actual class HostDefaultKey<T>(@get:IdRes public val id: Int) {
22-
23-
public actual constructor() : this(id = Int.MIN_VALUE)
24-
}
19+
/**
20+
* A type-safe identifier used to define a key for retrieving default values from the hosting
21+
* environment.
22+
*
23+
* This key is strictly required by [compositionLocalWithHostDefaultOf] to establish a mapping
24+
* between a [CompositionLocal] and a value provided by the host (via the internal
25+
* [HostDefaultProvider]).
26+
*
27+
* @param T The type of the value associated with this key.
28+
* @see compositionLocalWithHostDefaultOf
29+
* @see HostDefaultProvider
30+
*/
31+
public interface HostDefaultKey<T>

‎compose/runtime/runtime/src/commonMain/kotlin/androidx/compose/runtime/HostDefaultProvider.kt‎

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,41 +28,14 @@ public val LocalHostDefaultProvider: ProvidableCompositionLocal<HostDefaultProvi
2828
}
2929

3030
/**
31-
* An interface that allows the hosting environment (the platform embedding the Compose content) to
32-
* provide default values for [CompositionLocal]s based on a key.
31+
* An interface that allows the hosting environment (e.g., Android, Desktop, or iOS) to provide
32+
* default values for [CompositionLocal] using [compositionLocalWithHostDefaultOf].
3333
*
34-
* This acts as a bridge or service locator, allowing platform-agnostic libraries to retrieve
35-
* platform-specific components (like `LifecycleOwner` or `ViewModelStoreOwner`) without depending
36-
* directly on platform artifacts (e.g., `android.view.View`).
34+
* This acts as a decoupling layer, allowing platform-agnostic libraries to request
35+
* platform-specific components ((like `LifecycleOwner` or `ViewModelStoreOwner`) without depending
36+
* on platform-specific APIs or artifacts.
3737
*/
3838
public interface HostDefaultProvider {
39-
40-
/**
41-
* Retrieves a default value identified by [key] from the host environment.
42-
*
43-
* @param key The identifier for the value to retrieve. The type and meaning of the key are
44-
* defined by the host implementation (e.g., an Android resource ID).
45-
* @return The requested value, or null if not found.
46-
*/
39+
/** Retrieves a value associated with [key] from the host environment. */
4740
public fun <T> getHostDefault(key: HostDefaultKey<T>): T
4841
}
49-
50-
/**
51-
* A type-safe identifier used to define a key for retrieving default values from the hosting
52-
* environment.
53-
*
54-
* This key is strictly required by [compositionLocalWithHostDefaultOf] to establish a mapping
55-
* between a [CompositionLocal] and a value provided by the host (via the internal
56-
* [HostDefaultProvider]).
57-
*
58-
* The internal representation of this key is platform-specific. For example, on Android, this acts
59-
* as a wrapper around a Resource ID to query the View hierarchy, while ensuring the retrieved value
60-
* matches the type [T].
61-
*
62-
* @param T The type of the value associated with this key.
63-
* @see compositionLocalWithHostDefaultOf
64-
* @see HostDefaultProvider
65-
*/
66-
public expect class HostDefaultKey<T> {
67-
public constructor()
68-
}

‎compose/runtime/runtime/src/nonAndroidMain/kotlin/androidx/compose/runtime/HostDefaultProvider.nonAndroid.kt‎

Lines changed: 0 additions & 19 deletions
This file was deleted.

‎compose/runtime/runtime/src/nonEmulatorCommonTest/kotlin/androidx/compose/runtime/CompositionLocalTests.kt‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,7 @@ class CompositionLocalTests {
890890

891891
@Test
892892
fun hostDefault_resolvesValueFromProvider() = compositionTest {
893-
val key = HostDefaultKey<String>()
893+
val key = TestHostDefaultKey<String>()
894894
val local = compositionLocalWithHostDefaultOf(key)
895895
val provider = TestHostDefaultProvider(mapOf(key to "HostValue"))
896896

@@ -903,7 +903,7 @@ class CompositionLocalTests {
903903

904904
@Test
905905
fun hostDefault_explicitValueOverridesHost() = compositionTest {
906-
val key = HostDefaultKey<String>()
906+
val key = TestHostDefaultKey<String>()
907907
val local = compositionLocalWithHostDefaultOf(key)
908908
val provider = TestHostDefaultProvider(mapOf(key to "HostValue"))
909909

@@ -919,7 +919,7 @@ class CompositionLocalTests {
919919
@Test
920920
fun hostDefault_returnsNullForMissingNullableKey() = compositionTest {
921921
// Key expects a nullable String
922-
val key = HostDefaultKey<String?>()
922+
val key = TestHostDefaultKey<String?>()
923923
val local = compositionLocalWithHostDefaultOf(key)
924924
// Provider is empty
925925
val provider = TestHostDefaultProvider(emptyMap())
@@ -934,7 +934,7 @@ class CompositionLocalTests {
934934
@Test
935935
fun hostDefault_throwsForMissingNonNullableKey() = compositionTest {
936936
// Key expects a non-null String
937-
val key = HostDefaultKey<Int>()
937+
val key = TestHostDefaultKey<Int>()
938938
val local = compositionLocalWithHostDefaultOf(key)
939939
val provider = TestHostDefaultProvider(emptyMap())
940940

@@ -966,7 +966,7 @@ class CompositionLocalTests {
966966

967967
@Test
968968
fun hostDefault_dynamicUpdatesFromProvider() = compositionTest {
969-
val key = HostDefaultKey<String>()
969+
val key = TestHostDefaultKey<String>()
970970
val local = compositionLocalWithHostDefaultOf(key)
971971

972972
// We use mutable state to swap providers to simulate the host environment changing
@@ -1020,6 +1020,8 @@ data class SomeData(val value: String = "default")
10201020

10211021
@Stable class StableRef<T>(var value: T)
10221022

1023+
private class TestHostDefaultKey<T> : HostDefaultKey<T>
1024+
10231025
private class TestHostDefaultProvider(val values: Map<HostDefaultKey<*>, Any?>) :
10241026
HostDefaultProvider {
10251027
override fun <T> getHostDefault(key: HostDefaultKey<T>): T {

‎compose/ui/ui/src/androidMain/kotlin/androidx/compose/ui/platform/ComposeViewContext.android.kt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ internal class ComposeViewContext(
230230

231231
val scrollCaptureInProgress =
232232
LocalScrollCaptureInProgress.current or owner.scrollCaptureInProgress
233-
val hostDefaultProvider = remember(owner.view) { AndroidHostDefaultProvider(owner.view) }
233+
val hostDefaultProvider = remember(owner.view) { ViewTreeHostDefaultProvider(owner.view) }
234234
@Suppress("UNCHECKED_CAST")
235235
CompositionLocalProvider(
236236
LocalLifecycleOwner provides lifecycleOwner,

compose/ui/ui/src/androidMain/kotlin/androidx/compose/ui/platform/AndroidHostDefaultProvider.android.kt renamed to compose/ui/ui/src/androidMain/kotlin/androidx/compose/ui/platform/ViewTreeHostDefaultProvider.android.kt

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,39 @@ package androidx.compose.ui.platform
1919
import android.view.View
2020
import androidx.compose.runtime.HostDefaultKey
2121
import androidx.compose.runtime.HostDefaultProvider
22+
import androidx.compose.runtime.ViewTreeHostDefaultKey
2223
import androidx.core.viewtree.getParentOrViewTreeDisjointParent
2324

2425
/**
25-
* Android implementation of [HostDefaultProvider].
26+
* Android implementation of [HostDefaultProvider] that retrieves values from the [View] hierarchy.
2627
*
27-
* This provider resolves keys by treating them as Android Resource IDs (tags) and searching the
28-
* View hierarchy starting from the [AndroidComposeView] and walking up.
28+
* This provider performs a bottom-up search starting from the provided [view]. It resolves
29+
* [ViewTreeHostDefaultKey]s by querying [View.getTag] at each level of the tree.
2930
*
30-
* This mimics the behavior of `ViewTreeViewModelStoreOwner.get(view)` and similar APIs but allows
31-
* access from within the Composition without direct references to those APIs.
31+
* By using [getParentOrViewTreeDisjointParent] during traversal, it can find values even when the
32+
* Composition is hosted in "disjoint" windows like **Popups or Dialogs**. This mechanism allows the
33+
* Compose runtime to access platform-provided owners (e.g., Lifecycle, SavedState, or
34+
* ViewModelStore) without establishing hard-coded dependencies on those specific Android libraries.
3235
*/
33-
internal class AndroidHostDefaultProvider(private val view: View) : HostDefaultProvider {
34-
36+
internal class ViewTreeHostDefaultProvider(private val view: View) : HostDefaultProvider {
37+
@Suppress("UNCHECKED_CAST")
3538
override fun <T> getHostDefault(key: HostDefaultKey<T>): T {
39+
if (key !is ViewTreeHostDefaultKey<*>) {
40+
// Skip view tree traversal if key has no ID.
41+
return null as T
42+
}
43+
3644
var current: View? = view
3745
while (current != null) {
38-
val service = current.getTag(key.id)
46+
val service = current.getTag(/* key= */ key.tagKey)
3947
if (service != null) {
40-
@Suppress("UNCHECKED_CAST")
4148
return service as T
4249
}
4350

4451
// Traverse up the tree, handling disjoint parents like dialogs and popups.
4552
current = current.getParentOrViewTreeDisjointParent() as? View
4653
}
4754

48-
@Suppress("UNCHECKED_CAST")
4955
return null as T
5056
}
5157
}

0 commit comments

Comments
 (0)