1 /*
2 * Copyright 2022 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.Stable
20 import androidx.compose.runtime.compositionLocalOf
21
22 /**
23 * Use this composition local to get the [PinnableContainer] handling the current subhierarchy.
24 *
25 * It will be not null, for example, when the current content is composed as an item of lazy list.
26 */
<lambda>null27 val LocalPinnableContainer = compositionLocalOf<PinnableContainer?> { null }
28
29 /**
30 * Represents a container which can be pinned when the content of this container is important.
31 *
32 * For example, each item of lazy list represents one [PinnableContainer], and if this container is
33 * pinned, this item will not be disposed when scrolled out of the viewport.
34 *
35 * Pinning a currently focused item so the focus is not lost is one of the examples when this
36 * functionality can be useful.
37 *
38 * @see LocalPinnableContainer
39 */
40 @Stable
41 interface PinnableContainer {
42
43 /**
44 * Allows to pin this container when the associated content is considered important.
45 *
46 * For example, if this [PinnableContainer] is an item of lazy list pinning will mean this item
47 * will not be disposed when scrolled out of the viewport.
48 *
49 * Don't forget to call [PinnedHandle.release] when this content is not important anymore.
50 */
pinnull51 fun pin(): PinnedHandle
52
53 /** This is an object returned by [pin] which allows to release the pinning. */
54 @Suppress("NotCloseable")
55 fun interface PinnedHandle {
56 /**
57 * Releases the pin.
58 *
59 * For example, if this [PinnableContainer] is an item of lazy list releasing the pinning
60 * will allow lazy list to stop composing the item when it is not visible.
61 */
62 fun release()
63 }
64 }
65