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.padding
27 import androidx.compose.material3.ExperimentalMaterial3Api
28 import androidx.compose.material3.Scaffold
29 import androidx.compose.material3.TopAppBarDefaults
30 import androidx.compose.runtime.Composable
31 import androidx.compose.runtime.LaunchedEffect
32 import androidx.compose.ui.Modifier
33 import androidx.compose.ui.input.nestedscroll.nestedScroll
34 import androidx.compose.ui.platform.LocalContext
35 import androidx.compose.ui.tooling.preview.Preview
36 import com.android.settingslib.spa.framework.compose.horizontalValues
37 import com.android.settingslib.spa.framework.compose.verticalValues
38 import com.android.settingslib.spa.framework.theme.SettingsTheme
39 import com.android.settingslib.spa.widget.preference.Preference
40 import com.android.settingslib.spa.widget.preference.PreferenceModel
41
42 /**
43 * A [Scaffold] which content is can be full screen when needed.
44 */
45 @OptIn(ExperimentalMaterial3Api::class)
46 @Composable
47 fun SettingsScaffold(
48 title: String,
49 actions: @Composable RowScope.() -> Unit = {},
50 content: @Composable (PaddingValues) -> Unit,
51 ) {
52 ActivityTitle(title)
53 val scrollBehavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior()
54 Scaffold(
55 modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
<lambda>null56 topBar = { SettingsTopAppBar(title, scrollBehavior, actions) },
paddingValuesnull57 ) { paddingValues ->
58 Box(Modifier.padding(paddingValues.horizontalValues())) {
59 content(paddingValues.verticalValues())
60 }
61 }
62 }
63
64 /**
65 * Sets a title for the activity.
66 *
67 * So the TalkBack can read out the correct page title.
68 */
69 @Composable
ActivityTitlenull70 internal fun ActivityTitle(title: String) {
71 val context = LocalContext.current
72 LaunchedEffect(true) {
73 context.getActivity()?.title = title
74 }
75 }
76
Contextnull77 private fun Context.getActivity(): Activity? = when (this) {
78 is Activity -> this
79 is ContextWrapper -> baseContext.getActivity()
80 else -> null
81 }
82
83 @Preview
84 @Composable
SettingsScaffoldPreviewnull85 private fun SettingsScaffoldPreview() {
86 SettingsTheme {
87 SettingsScaffold(title = "Display") { paddingValues ->
88 Column(Modifier.padding(paddingValues)) {
89 Preference(object : PreferenceModel {
90 override val title = "Item 1"
91 })
92 Preference(object : PreferenceModel {
93 override val title = "Item 2"
94 })
95 }
96 }
97 }
98 }
99