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.glance 18 19 import android.content.Context 20 import androidx.compose.runtime.Composable 21 import androidx.compose.runtime.ProvidableCompositionLocal 22 import androidx.compose.runtime.compositionLocalOf 23 import androidx.compose.runtime.staticCompositionLocalOf 24 import androidx.compose.ui.unit.DpSize 25 import androidx.datastore.preferences.core.Preferences 26 import androidx.glance.color.ColorProviders 27 import androidx.glance.color.DynamicThemeColorProviders 28 import androidx.glance.state.GlanceStateDefinition 29 30 /** 31 * Size of the glance view being generated. 32 * 33 * The glance view will have at least that much space to be displayed. The exact meaning may changed 34 * depending on the surface and how it is configured. 35 */ <lambda>null36val LocalSize = staticCompositionLocalOf<DpSize> { error("No default size") } 37 38 /** Context of application when generating the glance view. */ <lambda>null39val LocalContext = staticCompositionLocalOf<Context> { error("No default context") } 40 41 /** 42 * Local view state, defined in surface implementation. A customizable store for view specific state 43 * data. 44 */ <lambda>null45val LocalState = compositionLocalOf<Any?> { null } 46 47 /** Unique Id for the glance view being generated by the current composition. */ <lambda>null48val LocalGlanceId = staticCompositionLocalOf<GlanceId> { error("No default glance id") } 49 50 /** 51 * Retrieves the current customizable store for view specific state data as defined by 52 * [GlanceStateDefinition] in the surface implementation. 53 * 54 * @return the current store of the provided type [T] 55 */ currentStatenull56@Composable inline fun <reified T> currentState(): T = LocalState.current as T 57 58 /** 59 * Retrieves the current [Preferences] value of the provided [Preferences.Key] from the current 60 * state when [Preferences] store is used as [GlanceStateDefinition] in the surface implementation. 61 * 62 * @param key the [Preferences.Key] to retrieve its value 63 * @return the stored value or null if not available. 64 */ 65 @Composable 66 inline fun <reified T> currentState(key: Preferences.Key<T>): T? = currentState<Preferences>()[key] 67 68 /** 69 * The colors currently provided by [GlanceTheme]. If no overrides are provided, it will provide a 70 * standard set of Material3 colors, see [DynamicThemeColorProviders]. This should usually be 71 * accessed through [GlanceTheme.colors] rather than directly. 72 */ 73 internal val LocalColors: ProvidableCompositionLocal<ColorProviders> = staticCompositionLocalOf { 74 DynamicThemeColorProviders 75 } 76