|
| 1 | +/* |
| 2 | + * Copyright 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 | + * https://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.example.compose.snippets.components |
| 18 | + |
| 19 | +import androidx.compose.foundation.interaction.MutableInteractionSource |
| 20 | +import androidx.compose.foundation.interaction.collectIsPressedAsState |
| 21 | +import androidx.compose.foundation.layout.Row |
| 22 | +import androidx.compose.foundation.layout.Spacer |
| 23 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 24 | +import androidx.compose.material3.Icon |
| 25 | +import androidx.compose.material3.IconButton |
| 26 | +import androidx.compose.material3.Text |
| 27 | +import androidx.compose.runtime.Composable |
| 28 | +import androidx.compose.runtime.LaunchedEffect |
| 29 | +import androidx.compose.runtime.getValue |
| 30 | +import androidx.compose.runtime.mutableIntStateOf |
| 31 | +import androidx.compose.runtime.mutableStateOf |
| 32 | +import androidx.compose.runtime.remember |
| 33 | +import androidx.compose.runtime.rememberUpdatedState |
| 34 | +import androidx.compose.runtime.saveable.rememberSaveable |
| 35 | +import androidx.compose.runtime.setValue |
| 36 | +import androidx.compose.ui.Alignment |
| 37 | +import androidx.compose.ui.Modifier |
| 38 | +import androidx.compose.ui.res.painterResource |
| 39 | +import androidx.compose.ui.tooling.preview.Preview |
| 40 | +import com.example.compose.snippets.R |
| 41 | +import kotlinx.coroutines.delay |
| 42 | + |
| 43 | +// [START android_compose_components_togglebuttonexample] |
| 44 | +@Preview |
| 45 | +@Composable |
| 46 | +fun ToggleIconButtonExample() { |
| 47 | + // isToggled initial value should be read from a view model or persistent storage. |
| 48 | + var isToggled by rememberSaveable { mutableStateOf(false) } |
| 49 | + |
| 50 | + IconButton( |
| 51 | + onClick = { isToggled = !isToggled } |
| 52 | + ) { |
| 53 | + Icon( |
| 54 | + painter = if (isToggled) painterResource(R.drawable.favorite_filled) else painterResource(R.drawable.favorite), |
| 55 | + contentDescription = if (isToggled) "Selected icon button" else "Unselected icon button." |
| 56 | + ) |
| 57 | + } |
| 58 | +} |
| 59 | +// [END android_compose_components_togglebuttonexample] |
| 60 | + |
| 61 | +// [START android_compose_components_iconbutton] |
| 62 | +@Composable |
| 63 | +fun MomentaryIconButton( |
| 64 | + unselectedImage: Int, |
| 65 | + selectedImage: Int, |
| 66 | + contentDescription: String, |
| 67 | + modifier: Modifier = Modifier, |
| 68 | + stepDelay: Long = 100L, // Minimum value is 1L milliseconds. |
| 69 | + onClick: () -> Unit |
| 70 | +) { |
| 71 | + val interactionSource = remember { MutableInteractionSource() } |
| 72 | + val isPressed by interactionSource.collectIsPressedAsState() |
| 73 | + val pressedListener by rememberUpdatedState(onClick) |
| 74 | + |
| 75 | + LaunchedEffect(isPressed) { |
| 76 | + while (isPressed) { |
| 77 | + delay(stepDelay.coerceIn(1L, Long.MAX_VALUE)) |
| 78 | + pressedListener() |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + IconButton( |
| 83 | + modifier = modifier, |
| 84 | + onClick = onClick, |
| 85 | + interactionSource = interactionSource |
| 86 | + ) { |
| 87 | + Icon( |
| 88 | + painter = if (isPressed) painterResource(id = selectedImage) else painterResource(id = unselectedImage), |
| 89 | + contentDescription = contentDescription, |
| 90 | + ) |
| 91 | + } |
| 92 | +} |
| 93 | +// [END android_compose_components_iconbutton] |
| 94 | + |
| 95 | +// [START android_compose_components_momentaryiconbuttons] |
| 96 | +@Preview() |
| 97 | +@Composable |
| 98 | +fun MomentaryIconButtonExample() { |
| 99 | + var pressedCount by remember { mutableIntStateOf(0) } |
| 100 | + |
| 101 | + Row( |
| 102 | + modifier = Modifier.fillMaxWidth(), |
| 103 | + verticalAlignment = Alignment.CenterVertically |
| 104 | + ) { |
| 105 | + MomentaryIconButton( |
| 106 | + unselectedImage = R.drawable.fast_rewind, |
| 107 | + selectedImage = R.drawable.fast_rewind_filled, |
| 108 | + stepDelay = 100L, |
| 109 | + onClick = { pressedCount -= 1 }, |
| 110 | + contentDescription = "Decrease count button" |
| 111 | + ) |
| 112 | + Spacer(modifier = Modifier) |
| 113 | + Text("advanced by $pressedCount frames") |
| 114 | + Spacer(modifier = Modifier) |
| 115 | + MomentaryIconButton( |
| 116 | + unselectedImage = R.drawable.fast_forward, |
| 117 | + selectedImage = R.drawable.fast_forward_filled, |
| 118 | + contentDescription = "Increase count button", |
| 119 | + stepDelay = 100L, |
| 120 | + onClick = { pressedCount += 1 } |
| 121 | + ) |
| 122 | + } |
| 123 | +} |
| 124 | +// [END android_compose_components_momentaryiconbuttons] |
0 commit comments