Clara Bayarri | 581b24d | 2020-02-10 16:15:28 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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 androidx.ui.material.studies.rally |
| 18 | |
| 19 | import androidx.animation.LinearEasing |
| 20 | import androidx.animation.transitionDefinition |
Doris Liu | a69d17b | 2020-06-19 16:39:42 -0700 | [diff] [blame] | 21 | import androidx.animation.tween |
Clara Bayarri | 581b24d | 2020-02-10 16:15:28 +0000 | [diff] [blame] | 22 | import androidx.compose.Composable |
| 23 | import androidx.compose.remember |
| 24 | import androidx.ui.animation.ColorPropKey |
Doris Liu | d2aa99c | 2020-07-07 15:11:15 -0700 | [diff] [blame] | 25 | import androidx.ui.animation.transition |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 26 | import androidx.ui.core.Modifier |
Louis Pullen-Freilich | ddda7be | 2020-07-17 18:28:12 +0100 | [diff] [blame^] | 27 | import androidx.compose.foundation.Text |
| 28 | import androidx.compose.foundation.selection.selectable |
Clara Bayarri | 581b24d | 2020-02-10 16:15:28 +0000 | [diff] [blame] | 29 | import androidx.ui.graphics.Color |
| 30 | import androidx.ui.graphics.vector.VectorAsset |
Clara Bayarri | 581b24d | 2020-02-10 16:15:28 +0000 | [diff] [blame] | 31 | import androidx.ui.layout.Row |
| 32 | import androidx.ui.layout.Spacer |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 33 | import androidx.ui.layout.fillMaxWidth |
| 34 | import androidx.ui.layout.padding |
| 35 | import androidx.ui.layout.preferredHeight |
| 36 | import androidx.ui.layout.preferredWidth |
Clara Bayarri | 581b24d | 2020-02-10 16:15:28 +0000 | [diff] [blame] | 37 | import androidx.ui.material.MaterialTheme |
Andrey Kulikov | 602ef01 | 2020-03-05 18:12:54 +0000 | [diff] [blame] | 38 | import androidx.ui.material.Surface |
Matvei Malkov | 6788d20b | 2020-05-28 18:35:55 +0100 | [diff] [blame] | 39 | import androidx.ui.material.ripple.RippleIndication |
Clara Bayarri | 581b24d | 2020-02-10 16:15:28 +0000 | [diff] [blame] | 40 | import androidx.ui.unit.dp |
| 41 | import java.util.Locale |
| 42 | |
| 43 | @Composable |
| 44 | fun RallyTopAppBar( |
| 45 | allScreens: List<RallyScreenState>, |
Clara Bayarri | 255d76a | 2020-02-12 12:23:09 +0000 | [diff] [blame] | 46 | onTabSelected: (RallyScreenState) -> Unit, |
Clara Bayarri | 581b24d | 2020-02-10 16:15:28 +0000 | [diff] [blame] | 47 | currentScreen: RallyScreenState |
| 48 | ) { |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 49 | Surface(Modifier.preferredHeight(TabHeight).fillMaxWidth()) { |
Clara Bayarri | 581b24d | 2020-02-10 16:15:28 +0000 | [diff] [blame] | 50 | Row { |
| 51 | allScreens.forEachIndexed { index, screen -> |
| 52 | RallyTab( |
| 53 | text = screen.name.toUpperCase(Locale.getDefault()), |
| 54 | icon = screen.icon, |
Clara Bayarri | 255d76a | 2020-02-12 12:23:09 +0000 | [diff] [blame] | 55 | onSelected = { onTabSelected(screen) }, |
Andrey Kulikov | 602ef01 | 2020-03-05 18:12:54 +0000 | [diff] [blame] | 56 | selected = currentScreen.ordinal == index |
| 57 | ) |
Clara Bayarri | 581b24d | 2020-02-10 16:15:28 +0000 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | @Composable |
| 64 | private fun RallyTab( |
| 65 | text: String, |
| 66 | icon: VectorAsset, |
| 67 | onSelected: () -> Unit, |
| 68 | selected: Boolean |
| 69 | ) { |
| 70 | TabTransition(selected = selected) { tabTintColor -> |
Matvei Malkov | 6788d20b | 2020-05-28 18:35:55 +0100 | [diff] [blame] | 71 | Row( |
| 72 | modifier = Modifier |
| 73 | .padding(16.dp) |
| 74 | .preferredHeight(TabHeight) |
| 75 | .selectable( |
| 76 | selected = selected, |
| 77 | onClick = onSelected, |
| 78 | indication = RippleIndication(bounded = false) |
| 79 | ) |
| 80 | ) { |
| 81 | Icon(vectorImage = icon, tintColor = tabTintColor) |
| 82 | if (selected) { |
| 83 | Spacer(Modifier.preferredWidth(12.dp)) |
| 84 | Text(text, color = tabTintColor) |
Clara Bayarri | 581b24d | 2020-02-10 16:15:28 +0000 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | @Composable |
| 91 | private fun TabTransition( |
| 92 | selected: Boolean, |
Louis Pullen-Freilich | 3a54b94 | 2020-05-07 13:23:03 +0100 | [diff] [blame] | 93 | children: @Composable (color: Color) -> Unit |
Clara Bayarri | 581b24d | 2020-02-10 16:15:28 +0000 | [diff] [blame] | 94 | ) { |
Louis Pullen-Freilich | b259185 | 2020-03-23 19:00:09 +0000 | [diff] [blame] | 95 | val color = MaterialTheme.colors.onSurface |
Clara Bayarri | 581b24d | 2020-02-10 16:15:28 +0000 | [diff] [blame] | 96 | val transitionDefinition = remember { |
| 97 | transitionDefinition { |
| 98 | state(true) { |
| 99 | this[TabTintColorKey] = color |
| 100 | } |
| 101 | |
| 102 | state(false) { |
| 103 | this[TabTintColorKey] = color.copy(alpha = InactiveTabOpacity) |
| 104 | } |
| 105 | |
| 106 | transition(fromState = false, toState = true) { |
Doris Liu | a69d17b | 2020-06-19 16:39:42 -0700 | [diff] [blame] | 107 | TabTintColorKey using tween( |
| 108 | durationMillis = TabFadeInAnimationDuration, |
| 109 | delayMillis = TabFadeInAnimationDelay, |
Clara Bayarri | 581b24d | 2020-02-10 16:15:28 +0000 | [diff] [blame] | 110 | easing = LinearEasing |
Doris Liu | a69d17b | 2020-06-19 16:39:42 -0700 | [diff] [blame] | 111 | ) |
Clara Bayarri | 581b24d | 2020-02-10 16:15:28 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | transition(fromState = true, toState = false) { |
Doris Liu | a69d17b | 2020-06-19 16:39:42 -0700 | [diff] [blame] | 115 | TabTintColorKey using tween( |
| 116 | durationMillis = TabFadeOutAnimationDuration, |
| 117 | delayMillis = TabFadeInAnimationDelay, |
Clara Bayarri | 581b24d | 2020-02-10 16:15:28 +0000 | [diff] [blame] | 118 | easing = LinearEasing |
Doris Liu | a69d17b | 2020-06-19 16:39:42 -0700 | [diff] [blame] | 119 | ) |
Clara Bayarri | 581b24d | 2020-02-10 16:15:28 +0000 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | } |
Doris Liu | d2aa99c | 2020-07-07 15:11:15 -0700 | [diff] [blame] | 123 | val state = transition(transitionDefinition, selected) |
| 124 | children(state[TabTintColorKey]) |
Clara Bayarri | 581b24d | 2020-02-10 16:15:28 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | private val TabTintColorKey = ColorPropKey() |
| 128 | private val TabHeight = 56.dp |
| 129 | private const val InactiveTabOpacity = 0.60f |
| 130 | |
| 131 | private const val TabFadeInAnimationDuration = 150 |
| 132 | private const val TabFadeInAnimationDelay = 100 |
| 133 | private const val TabFadeOutAnimationDuration = 100 |