1 /*
<lambda>null2 * Copyright (C) 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 com.android.settingslib.spa.widget.scaffold
18
19 import android.app.Activity
20 import android.content.Context
21 import android.content.ContextWrapper
22 import androidx.compose.foundation.layout.Box
23 import androidx.compose.foundation.layout.Column
24 import androidx.compose.foundation.layout.PaddingValues
25 import androidx.compose.foundation.layout.RowScope
26 import androidx.compose.foundation.layout.WindowInsets
27 import androidx.compose.foundation.layout.padding
28 import androidx.compose.foundation.layout.safeDrawing
29 import androidx.compose.material3.ExperimentalMaterial3Api
30 import androidx.compose.material3.MaterialTheme
31 import androidx.compose.material3.Scaffold
32 import androidx.compose.material3.TopAppBarDefaults
33 import androidx.compose.runtime.Composable
34 import androidx.compose.runtime.LaunchedEffect
35 import androidx.compose.ui.Modifier
36 import androidx.compose.ui.input.nestedscroll.nestedScroll
37 import androidx.compose.ui.platform.LocalContext
38 import androidx.compose.ui.tooling.preview.Preview
39 import com.android.settingslib.spa.framework.compose.horizontalValues
40 import com.android.settingslib.spa.framework.compose.verticalValues
41 import com.android.settingslib.spa.framework.theme.SettingsTheme
42 import com.android.settingslib.spa.framework.theme.settingsBackground
43 import com.android.settingslib.spa.widget.preference.Preference
44 import com.android.settingslib.spa.widget.preference.PreferenceModel
45
46 /**
47 * A [Scaffold] which content is can be full screen when needed.
48 */
49 @OptIn(ExperimentalMaterial3Api::class)
50 @Composable
51 fun SettingsScaffold(
52 title: String,
53 actions: @Composable RowScope.() -> Unit = {},
54 content: @Composable (PaddingValues) -> Unit,
55 ) {
56 ActivityTitle(title)
57 val scrollBehavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior()
58 Scaffold(
59 modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
<lambda>null60 topBar = { SettingsTopAppBar(title, scrollBehavior, actions) },
61 containerColor = MaterialTheme.colorScheme.settingsBackground,
62 contentWindowInsets = WindowInsets.safeDrawing,
paddingValuesnull63 ) { paddingValues ->
64 Box(Modifier.padding(paddingValues.horizontalValues())) {
65 content(paddingValues.verticalValues())
66 }
67 }
68 }
69
70 /**
71 * Sets a title for the activity.
72 *
73 * So the TalkBack can read out the correct page title.
74 */
75 @Composable
ActivityTitlenull76 internal fun ActivityTitle(title: String) {
77 val context = LocalContext.current
78 LaunchedEffect(true) {
79 context.getActivity()?.title = title
80 }
81 }
82
Contextnull83 private fun Context.getActivity(): Activity? = when (this) {
84 is Activity -> this
85 is ContextWrapper -> baseContext.getActivity()
86 else -> null
87 }
88
89 @Preview
90 @Composable
SettingsScaffoldPreviewnull91 private fun SettingsScaffoldPreview() {
92 SettingsTheme {
93 SettingsScaffold(title = "Display") { paddingValues ->
94 Column(Modifier.padding(paddingValues)) {
95 Preference(object : PreferenceModel {
96 override val title = "Item 1"
97 })
98 Preference(object : PreferenceModel {
99 override val title = "Item 2"
100 })
101 }
102 }
103 }
104 }
105