1 /*
2  * 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.credentialmanager.common.ui
18 
19 import androidx.compose.foundation.layout.PaddingValues
20 import androidx.compose.foundation.layout.padding
21 import com.android.credentialmanager.R
22 import androidx.compose.material.Icon
23 import androidx.compose.material.IconButton
24 import androidx.compose.material.icons.Icons
25 import androidx.compose.material.icons.outlined.Visibility
26 import androidx.compose.material.icons.outlined.VisibilityOff
27 import androidx.compose.material3.ButtonDefaults
28 import androidx.compose.material3.MaterialTheme
29 import androidx.compose.material3.TextButton
30 import androidx.compose.runtime.Composable
31 import androidx.compose.runtime.MutableState
32 import androidx.compose.runtime.mutableStateOf
33 import androidx.compose.runtime.remember
34 import androidx.compose.ui.Modifier
35 import androidx.compose.ui.res.stringResource
36 import androidx.compose.ui.unit.dp
37 
38 @Composable
ActionButtonnull39 fun ActionButton(text: String, onClick: () -> Unit) {
40     TextButton(
41         modifier = Modifier.padding(vertical = 4.dp),
42         onClick = onClick,
43         colors = ButtonDefaults.textButtonColors(
44             contentColor = MaterialTheme.colorScheme.primary,
45         ),
46         contentPadding = PaddingValues(start = 12.dp, top = 10.dp, end = 12.dp, bottom = 10.dp),
47     ) {
48         LargeLabelText(text = text)
49     }
50 }
51 
52 @Composable
ToggleVisibilityButtonnull53 fun ToggleVisibilityButton(modifier: Modifier = Modifier, onToggle: (Boolean) -> Unit) {
54     // default state is visibility off
55     val toggleState: MutableState<Boolean> = remember { mutableStateOf(false) }
56 
57     IconButton(
58         modifier = modifier,
59         onClick = {
60             toggleState.value = !toggleState.value
61             onToggle(toggleState.value)
62         }
63     ) {
64         Icon(
65             imageVector = if (toggleState.value)
66                 Icons.Outlined.Visibility else Icons.Outlined.VisibilityOff,
67             contentDescription = if (toggleState.value)
68                 stringResource(R.string.content_description_show_password) else
69                 stringResource(R.string.content_description_hide_password),
70             tint = MaterialTheme.colorScheme.onSurfaceVariant,
71         )
72     }
73 }