blob: 5b7b34cb26293834b9ec8a76241908f1b63d078d [file] [log] [blame]
drchen30649a72023-05-16 12:44:29 -07001/*
2 * Copyright 2023 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
17package androidx.compose.material3.adaptive
18
drchen75405522023-05-23 10:04:54 -070019import android.app.Activity
drchend93af492023-05-31 14:03:59 -070020import androidx.compose.material3.windowsizeclass.ExperimentalMaterial3WindowSizeClassApi
21import androidx.compose.material3.windowsizeclass.WindowSizeClass
drchen30649a72023-05-16 12:44:29 -070022import androidx.compose.runtime.Composable
23import androidx.compose.runtime.State
drchen75405522023-05-23 10:04:54 -070024import androidx.compose.runtime.collectAsState
drchen30649a72023-05-16 12:44:29 -070025import androidx.compose.runtime.mutableStateOf
26import androidx.compose.runtime.remember
27import androidx.compose.ui.platform.LocalConfiguration
28import androidx.compose.ui.platform.LocalContext
drchend93af492023-05-31 14:03:59 -070029import androidx.compose.ui.platform.LocalDensity
drchen30649a72023-05-16 12:44:29 -070030import androidx.compose.ui.unit.IntSize
drchend93af492023-05-31 14:03:59 -070031import androidx.compose.ui.unit.toSize
drchen75405522023-05-23 10:04:54 -070032import androidx.window.layout.FoldingFeature
33import androidx.window.layout.WindowInfoTracker
drchen30649a72023-05-16 12:44:29 -070034import androidx.window.layout.WindowMetricsCalculator
drchen75405522023-05-23 10:04:54 -070035import kotlinx.coroutines.flow.map
drchen30649a72023-05-16 12:44:29 -070036
37/**
drchend93af492023-05-31 14:03:59 -070038 * Calculates and returns [WindowAdaptiveInfo] of the provided context. It's a convenient function
39 * that uses the Material default [WindowSizeClass.calculateFromSize] and [calculatePosture]
40 * functions to retrieve [WindowSizeClass] and [Posture].
41 *
drchend93af492023-05-31 14:03:59 -070042 * @return [WindowAdaptiveInfo] of the provided context
43 */
44@ExperimentalMaterial3AdaptiveApi
drchen69da28c2023-10-04 11:55:51 -070045@OptIn(ExperimentalMaterial3WindowSizeClassApi::class)
drchend93af492023-05-31 14:03:59 -070046@Composable
drchen830d7032023-10-16 11:29:26 -070047fun currentWindowAdaptiveInfo(): WindowAdaptiveInfo =
drchen69da28c2023-10-04 11:55:51 -070048 WindowAdaptiveInfo(
49 WindowSizeClass.calculateFromSize(
50 with(LocalDensity.current) {
drchen830d7032023-10-16 11:29:26 -070051 collectWindowSizeAsState().value.toSize().toDpSize()
drchen69da28c2023-10-04 11:55:51 -070052 }
53 ),
drchen830d7032023-10-16 11:29:26 -070054 calculatePosture(collectFoldingFeaturesAsState().value)
drchen69da28c2023-10-04 11:55:51 -070055 )
drchend93af492023-05-31 14:03:59 -070056
57/**
drchen30649a72023-05-16 12:44:29 -070058 * Collects the current window size from [WindowMetricsCalculator] in to a [State].
59 *
drchen30649a72023-05-16 12:44:29 -070060 * @return a [State] of [IntSize] that represents the current window size.
61 */
62@ExperimentalMaterial3AdaptiveApi
63@Composable
drchen830d7032023-10-16 11:29:26 -070064fun collectWindowSizeAsState(): State<IntSize> {
drchen30649a72023-05-16 12:44:29 -070065 val size = remember {
66 mutableStateOf(IntSize(0, 0))
67 }
68
69 // Observe view configuration changes and recalculate the size class on each change. We can't
70 // use Activity#onConfigurationChanged as this will sometimes fail to be called on different
71 // API levels, hence why this function needs to be @Composable so we can observe the
72 // ComposeView's configuration changes.
drchen69da28c2023-10-04 11:55:51 -070073 val context = LocalContext.current
drchen30649a72023-05-16 12:44:29 -070074 size.value = remember(context, LocalConfiguration.current) {
75 val windowBounds =
76 WindowMetricsCalculator
77 .getOrCreate()
78 .computeCurrentWindowMetrics(context)
79 .bounds
80 IntSize(windowBounds.width(), windowBounds.height())
81 }
82
83 return size
84}
drchen75405522023-05-23 10:04:54 -070085
86/**
87 * Collects the current window folding features from [WindowInfoTracker] in to a [State].
88 *
drchen75405522023-05-23 10:04:54 -070089 * @return a [State] of a [FoldingFeature] list.
90 */
91@ExperimentalMaterial3AdaptiveApi
92@Composable
drchen830d7032023-10-16 11:29:26 -070093fun collectFoldingFeaturesAsState(): State<List<FoldingFeature>> {
drchen69da28c2023-10-04 11:55:51 -070094 val context = LocalContext.current
drchen75405522023-05-23 10:04:54 -070095 return remember(context) {
96 if (context is Activity) {
97 // TODO(b/284347941) remove the instance check after the test bug is fixed.
98 WindowInfoTracker
99 .getOrCreate(context)
100 .windowLayoutInfo(context)
101 } else {
102 WindowInfoTracker
103 .getOrCreate(context)
104 .windowLayoutInfo(context)
105 }.map { it.displayFeatures.filterIsInstance<FoldingFeature>() }
106 }.collectAsState(emptyList())
107}