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 @file:Suppress("DEPRECATION")
18
19 package androidx.compose.ui.platform
20
21 import androidx.annotation.RestrictTo
22 import androidx.compose.runtime.Composable
23 import androidx.compose.runtime.CompositionLocal
24 import androidx.compose.runtime.CompositionLocalProvider
25 import androidx.compose.runtime.ProvidableCompositionLocal
26 import androidx.compose.runtime.compositionLocalOf
27 import androidx.compose.runtime.staticCompositionLocalOf
28 import androidx.compose.ui.ExperimentalComposeUiApi
29 import androidx.compose.ui.autofill.Autofill
30 import androidx.compose.ui.autofill.AutofillManager
31 import androidx.compose.ui.autofill.AutofillTree
32 import androidx.compose.ui.draw.DrawModifier
33 import androidx.compose.ui.focus.FocusManager
34 import androidx.compose.ui.graphics.GraphicsContext
35 import androidx.compose.ui.graphics.layer.GraphicsLayer
36 import androidx.compose.ui.hapticfeedback.HapticFeedback
37 import androidx.compose.ui.input.InputModeManager
38 import androidx.compose.ui.input.pointer.PointerIconService
39 import androidx.compose.ui.layout.Layout
40 import androidx.compose.ui.node.Owner
41 import androidx.compose.ui.text.font.Font
42 import androidx.compose.ui.text.font.FontFamily
43 import androidx.compose.ui.text.input.TextInputService
44 import androidx.compose.ui.unit.Density
45 import androidx.compose.ui.unit.LayoutDirection
46 import androidx.lifecycle.LifecycleOwner
47
48 /** The CompositionLocal to provide communication with platform accessibility service. */
<lambda>null49 val LocalAccessibilityManager = staticCompositionLocalOf<AccessibilityManager?> { null }
50
51 /**
52 * The CompositionLocal that can be used to trigger autofill actions. Eg.
53 * [Autofill.requestAutofillForNode].
54 */
55 @Deprecated(
56 """
57 Use the new semantics-based Autofill APIs androidx.compose.ui.autofill.ContentType and
58 androidx.compose.ui.autofill.ContentDataType instead.
59 """
60 )
<lambda>null61 val LocalAutofill = staticCompositionLocalOf<Autofill?> { null }
62
63 /**
64 * The CompositionLocal that can be used to add [AutofillNode][import
65 * androidx.compose.ui.autofill.AutofillNode]s to the autofill tree. The [AutofillTree] is a
66 * temporary data structure that will be replaced by Autofill Semantics (b/138604305).
67 */
68 @Deprecated(
69 """
70 Use the new semantics-based Autofill APIs androidx.compose.ui.autofill.ContentType and
71 androidx.compose.ui.autofill.ContentDataType instead.
72 """
73 )
74 val LocalAutofillTree =
<lambda>null75 staticCompositionLocalOf<AutofillTree> { noLocalProvidedFor("LocalAutofillTree") }
76
77 /**
78 * The CompositionLocal that can be used to trigger autofill actions. Eg. [AutofillManager.commit].
79 */
80 val LocalAutofillManager =
<lambda>null81 staticCompositionLocalOf<AutofillManager?> { noLocalProvidedFor("LocalAutofillManager") }
82
83 /** The CompositionLocal to provide communication with platform clipboard service. */
84 @Deprecated(
85 "Use LocalClipboard instead which supports suspend functions",
86 ReplaceWith("LocalClipboard", "androidx.compose.ui.platform.LocalClipboard")
87 )
88 val LocalClipboardManager =
<lambda>null89 staticCompositionLocalOf<ClipboardManager> { noLocalProvidedFor("LocalClipboardManager") }
90
91 /** The CompositionLocal to provide communication with platform clipboard service. */
<lambda>null92 val LocalClipboard = staticCompositionLocalOf<Clipboard> { noLocalProvidedFor("LocalClipboard") }
93
94 /**
95 * The CompositionLocal to provide access to a [GraphicsContext] instance for creation of
96 * [GraphicsLayer]s.
97 *
98 * Consumers that access this Local directly and call [GraphicsContext.createGraphicsLayer] are
99 * responsible for calling [GraphicsContext.releaseGraphicsLayer].
100 *
101 * It is recommended that consumers invoke [rememberGraphicsLayer][import
102 * androidx.compose.ui.graphics.rememberGraphicsLayer] instead to ensure that a [GraphicsLayer] is
103 * released when the corresponding composable is disposed.
104 */
105 val LocalGraphicsContext =
<lambda>null106 staticCompositionLocalOf<GraphicsContext> { noLocalProvidedFor("LocalGraphicsContext") }
107
108 /**
109 * Provides the [Density] to be used to transform between
110 * [density-independent pixel units (DP)][androidx.compose.ui.unit.Dp] and pixel units or
111 * [scale-independent pixel units (SP)][androidx.compose.ui.unit.TextUnit] and pixel units. This is
112 * typically used when a [DP][androidx.compose.ui.unit.Dp] is provided and it must be converted in
113 * the body of [Layout] or [DrawModifier].
114 */
<lambda>null115 val LocalDensity = staticCompositionLocalOf<Density> { noLocalProvidedFor("LocalDensity") }
116
117 /** The CompositionLocal that can be used to control focus within Compose. */
118 val LocalFocusManager =
<lambda>null119 staticCompositionLocalOf<FocusManager> { noLocalProvidedFor("LocalFocusManager") }
120
121 /** The CompositionLocal to provide platform font loading methods. */
122 @Suppress("DEPRECATION")
123 @Deprecated(
124 "LocalFontLoader is replaced with LocalFontFamilyResolver",
125 replaceWith = ReplaceWith("LocalFontFamilyResolver")
126 )
127 @get:RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
128 val LocalFontLoader =
<lambda>null129 staticCompositionLocalOf<Font.ResourceLoader> { noLocalProvidedFor("LocalFontLoader") }
130
131 /** The CompositionLocal for compose font resolution from FontFamily. */
132 val LocalFontFamilyResolver =
<lambda>null133 staticCompositionLocalOf<FontFamily.Resolver> { noLocalProvidedFor("LocalFontFamilyResolver") }
134
135 /** The CompositionLocal to provide haptic feedback to the user. */
136 val LocalHapticFeedback =
<lambda>null137 staticCompositionLocalOf<HapticFeedback> { noLocalProvidedFor("LocalHapticFeedback") }
138
139 /**
140 * The CompositionLocal to provide an instance of InputModeManager which controls the current input
141 * mode.
142 */
143 val LocalInputModeManager =
<lambda>null144 staticCompositionLocalOf<InputModeManager> { noLocalProvidedFor("LocalInputManager") }
145
146 /** The CompositionLocal to provide the layout direction. */
147 val LocalLayoutDirection =
<lambda>null148 staticCompositionLocalOf<LayoutDirection> { noLocalProvidedFor("LocalLayoutDirection") }
149
150 /** The CompositionLocal to provide communication with platform text input service. */
151 @Deprecated("Use PlatformTextInputModifierNode instead.")
<lambda>null152 val LocalTextInputService = staticCompositionLocalOf<TextInputService?> { null }
153
154 /**
155 * The [CompositionLocal] to provide a [SoftwareKeyboardController] that can control the current
156 * software keyboard.
157 *
158 * Will be null if the software keyboard cannot be controlled.
159 */
<lambda>null160 val LocalSoftwareKeyboardController = staticCompositionLocalOf<SoftwareKeyboardController?> { null }
161
162 /** The CompositionLocal to provide text-related toolbar. */
163 val LocalTextToolbar =
<lambda>null164 staticCompositionLocalOf<TextToolbar> { noLocalProvidedFor("LocalTextToolbar") }
165
166 /** The CompositionLocal to provide functionality related to URL, e.g. open URI. */
<lambda>null167 val LocalUriHandler = staticCompositionLocalOf<UriHandler> { noLocalProvidedFor("LocalUriHandler") }
168
169 /** The CompositionLocal that provides the ViewConfiguration. */
170 val LocalViewConfiguration =
<lambda>null171 staticCompositionLocalOf<ViewConfiguration> { noLocalProvidedFor("LocalViewConfiguration") }
172
173 /**
174 * The CompositionLocal that provides information about the window that hosts the current [Owner].
175 */
<lambda>null176 val LocalWindowInfo = staticCompositionLocalOf<WindowInfo> { noLocalProvidedFor("LocalWindowInfo") }
177
178 /** The CompositionLocal containing the current [LifecycleOwner]. */
179 @Deprecated(
180 "Moved to lifecycle-runtime-compose library in androidx.lifecycle.compose package.",
181 ReplaceWith("androidx.lifecycle.compose.LocalLifecycleOwner"),
182 )
183 expect val LocalLifecycleOwner: ProvidableCompositionLocal<LifecycleOwner>
184
<lambda>null185 internal val LocalPointerIconService = staticCompositionLocalOf<PointerIconService?> { null }
186
187 /** @see LocalScrollCaptureInProgress */
<lambda>null188 internal val LocalProvidableScrollCaptureInProgress = compositionLocalOf { false }
189
190 /**
191 * True when the system is currently capturing the contents of a scrollable in this compose view or
192 * any parent compose view.
193 */
194 val LocalScrollCaptureInProgress: CompositionLocal<Boolean>
195 get() = LocalProvidableScrollCaptureInProgress
196
197 /**
198 * Text cursor blinking
199 * - _true_ normal cursor behavior (interactive blink)
200 * - _false_ never blink (always on)
201 *
202 * The default of _true_ is the user-expected system behavior for Text editing.
203 *
204 * Typically you should not set _false_ outside of screenshot tests without also providing a
205 * `cursorBrush` to `BasicTextField` to implement a custom design
206 */
<lambda>null207 val LocalCursorBlinkEnabled: ProvidableCompositionLocal<Boolean> = staticCompositionLocalOf { true }
208
209 @ExperimentalComposeUiApi
210 @Composable
211 internal fun ProvideCommonCompositionLocals(
212 owner: Owner,
213 uriHandler: UriHandler,
214 content: @Composable () -> Unit
215 ) {
216 CompositionLocalProvider(
217 LocalAccessibilityManager provides owner.accessibilityManager,
218 LocalAutofill provides owner.autofill,
219 LocalAutofillManager provides owner.autofillManager,
220 LocalAutofillTree provides owner.autofillTree,
221 LocalClipboardManager provides owner.clipboardManager,
222 LocalClipboard provides owner.clipboard,
223 LocalDensity provides owner.density,
224 LocalFocusManager provides owner.focusOwner,
225 @Suppress("DEPRECATION") LocalFontLoader providesDefault
226 @Suppress("DEPRECATION") owner.fontLoader,
227 LocalFontFamilyResolver providesDefault owner.fontFamilyResolver,
228 LocalHapticFeedback provides owner.hapticFeedBack,
229 LocalInputModeManager provides owner.inputModeManager,
230 LocalLayoutDirection provides owner.layoutDirection,
231 LocalTextInputService provides owner.textInputService,
232 LocalSoftwareKeyboardController provides owner.softwareKeyboardController,
233 LocalTextToolbar provides owner.textToolbar,
234 LocalUriHandler provides uriHandler,
235 LocalViewConfiguration provides owner.viewConfiguration,
236 LocalWindowInfo provides owner.windowInfo,
237 LocalPointerIconService provides owner.pointerIconService,
238 LocalGraphicsContext provides owner.graphicsContext,
239 content = content
240 )
241 }
242
noLocalProvidedFornull243 private fun noLocalProvidedFor(name: String): Nothing {
244 error("CompositionLocal $name not present")
245 }
246