1 /*
<lambda>null2 * Copyright (C) 2025 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.mechanics.demo.tuneable
18
19 import androidx.compose.foundation.layout.Column
20 import androidx.compose.foundation.layout.ColumnScope
21 import androidx.compose.foundation.layout.Row
22 import androidx.compose.foundation.layout.Spacer
23 import androidx.compose.foundation.layout.fillMaxSize
24 import androidx.compose.foundation.layout.fillMaxWidth
25 import androidx.compose.foundation.layout.width
26 import androidx.compose.material.icons.Icons
27 import androidx.compose.material.icons.filled.Settings
28 import androidx.compose.material3.Icon
29 import androidx.compose.material3.Text
30 import androidx.compose.material3.TextButton
31 import androidx.compose.runtime.Composable
32 import androidx.compose.runtime.getValue
33 import androidx.compose.runtime.mutableStateOf
34 import androidx.compose.runtime.remember
35 import androidx.compose.runtime.setValue
36 import androidx.compose.ui.Modifier
37 import androidx.compose.ui.unit.Dp
38 import androidx.compose.ui.unit.dp
39 import com.android.mechanics.demo.staging.debug.DebugUi
40
41 interface Demo<T> {
42 val identifier: String
43
44 @Composable fun rememberDefaultConfig(): T
45
46 @Composable fun ColumnScope.ConfigUi(config: T, onConfigChanged: (T) -> Unit)
47
48 @Composable fun DemoUi(config: T, modifier: Modifier)
49
50 val visualizationInputRange: ClosedFloatingPointRange<Float>
51
52 val expandedGraphHeight: Dp
53 get() = 96.dp
54
55 val collapsedGraphHeight: Dp
56 get() = 48.dp
57 }
58
59 @Composable
ConfigurableDemonull60 fun <T> Demo<T>.ConfigurableDemo(modifier: Modifier = Modifier) {
61 val defaultConfig = rememberDefaultConfig()
62 var config by remember(defaultConfig) { mutableStateOf(defaultConfig) }
63
64 var showConfigurationDialog by remember { mutableStateOf(false) }
65
66 if (showConfigurationDialog) {
67 ConfigDialog(
68 config,
69 onConfigurationChange = { config = it },
70 onDismissRequest = { showConfigurationDialog = false },
71 defaultConfig = defaultConfig,
72 ) { value, onValueChanged ->
73 ConfigUi(value, onValueChanged)
74 }
75 }
76
77 Column(modifier = modifier.fillMaxSize()) {
78 Row(modifier = Modifier.fillMaxWidth()) {
79 TextButton(onClick = { showConfigurationDialog = true }) {
80 Icon(Icons.Default.Settings, null)
81 Spacer(Modifier.width(8.dp))
82 Text("Config")
83 }
84 }
85
86 DebugUi(
87 visualizationInputRange,
88 expandedGraphHeight,
89 collapsedGraphHeight,
90 modifier = modifier.fillMaxWidth().weight(1f, fill = true),
91 ) { contentModifier ->
92 DemoUi(config, contentModifier)
93 }
94 }
95 }
96