• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.animation.AnimatedVisibility
20 import androidx.compose.foundation.layout.padding
21 import androidx.compose.material3.Text
22 import androidx.compose.runtime.Composable
23 import androidx.compose.runtime.getValue
24 import androidx.compose.runtime.mutableFloatStateOf
25 import androidx.compose.runtime.remember
26 import androidx.compose.runtime.setValue
27 import androidx.compose.ui.Modifier
28 import androidx.compose.ui.platform.LocalDensity
29 import androidx.compose.ui.unit.dp
30 import com.android.mechanics.demo.util.GuaranteeType
31 import com.android.mechanics.demo.util.asLabel
32 import com.android.mechanics.demo.util.type
33 import com.android.mechanics.spec.Guarantee
34 
35 @Composable
36 fun GuaranteeSection(
37     label: String,
38     value: Guarantee,
39     onValueChanged: (Guarantee) -> Unit,
40     sectionKey: String,
41     modifier: Modifier = Modifier,
42     description: (@Composable () -> Unit)? = null,
43 ) {
44 
45     val density = LocalDensity.current
46     Section(
47         label = label,
48         summary = { it.asLabel(density) },
49         value = value,
50         sectionKey = sectionKey,
51         onValueChanged = onValueChanged,
52         modifier = modifier,
53     ) { guarantee, onGuaranteeChanged ->
54         description?.invoke()
55 
56         var lastDelta by remember {
57             mutableFloatStateOf(
58                 when (guarantee) {
59                     is Guarantee.None -> with(density) { 4.dp.toPx() }
60                     is Guarantee.InputDelta -> guarantee.delta
61                     is Guarantee.GestureDragDelta -> guarantee.delta
62                 }
63             )
64         }
65 
66         Dropdown(
67             value.type,
68             GuaranteeType.entries,
69             { it.label },
70             onChange = {
71                 onGuaranteeChanged(
72                     when (it) {
73                         GuaranteeType.None -> Guarantee.None
74                         GuaranteeType.Input -> Guarantee.InputDelta(lastDelta)
75                         GuaranteeType.Drag -> Guarantee.GestureDragDelta(lastDelta)
76                     }
77                 )
78             },
79         )
80 
81         AnimatedVisibility(value.type != GuaranteeType.None) {
82             Text(text = "Delta", modifier = Modifier.padding(start = 8.dp))
83             DpSlider(
84                 with(density) { lastDelta.toDp() },
85                 {
86                     with(density) { lastDelta = it.toPx() }
87                     onGuaranteeChanged(
88                         when (guarantee) {
89                             Guarantee.None -> Guarantee.None
90                             is Guarantee.InputDelta -> Guarantee.InputDelta(lastDelta)
91                             is Guarantee.GestureDragDelta -> Guarantee.GestureDragDelta(lastDelta)
92                         }
93                     )
94                 },
95                 0.dp..48.dp,
96             )
97         }
98     }
99 }
100