Matvei Malkov | b814df0 | 2019-11-21 15:58:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 |
| 18 | |
| 19 | import androidx.compose.Composable |
Matvei Malkov | b814df0 | 2019-11-21 15:58:03 +0000 | [diff] [blame] | 20 | import androidx.ui.core.CurrentTextStyleProvider |
| 21 | import androidx.ui.core.FirstBaseline |
Matvei Malkov | b814df0 | 2019-11-21 15:58:03 +0000 | [diff] [blame] | 22 | import androidx.ui.core.LastBaseline |
| 23 | import androidx.ui.core.Layout |
Mihai Popa | 8c474e9 | 2019-12-10 17:43:20 +0000 | [diff] [blame] | 24 | import androidx.ui.core.LayoutTagParentData |
Matvei Malkov | b814df0 | 2019-11-21 15:58:03 +0000 | [diff] [blame] | 25 | import androidx.ui.core.Modifier |
Mihai Popa | 8c474e9 | 2019-12-10 17:43:20 +0000 | [diff] [blame] | 26 | import androidx.ui.core.ParentData |
Matvei Malkov | b814df0 | 2019-11-21 15:58:03 +0000 | [diff] [blame] | 27 | import androidx.ui.core.Text |
Mihai Popa | 8c474e9 | 2019-12-10 17:43:20 +0000 | [diff] [blame] | 28 | import androidx.ui.core.tag |
Matvei Malkov | 59bac36 | 2020-02-13 20:23:13 +0000 | [diff] [blame^] | 29 | import androidx.ui.foundation.DrawBackground |
Matvei Malkov | b814df0 | 2019-11-21 15:58:03 +0000 | [diff] [blame] | 30 | import androidx.ui.foundation.shape.corner.RoundedCornerShape |
| 31 | import androidx.ui.graphics.Color |
| 32 | import androidx.ui.layout.AlignmentLineOffset |
| 33 | import androidx.ui.layout.Column |
| 34 | import androidx.ui.layout.Container |
Adam Powell | 712dc99 | 2019-12-04 12:48:30 -0800 | [diff] [blame] | 35 | import androidx.ui.layout.LayoutGravity |
| 36 | import androidx.ui.layout.LayoutPadding |
Adam Powell | 31c1ebd | 2020-01-09 09:48:24 -0800 | [diff] [blame] | 37 | import androidx.ui.layout.LayoutWidth |
Matvei Malkov | b814df0 | 2019-11-21 15:58:03 +0000 | [diff] [blame] | 38 | import androidx.ui.material.surface.Surface |
George Mount | 842c8c1 | 2020-01-08 16:03:42 -0800 | [diff] [blame] | 39 | import androidx.ui.unit.IntPx |
| 40 | import androidx.ui.unit.dp |
| 41 | import androidx.ui.unit.ipx |
| 42 | import androidx.ui.unit.max |
Matvei Malkov | b814df0 | 2019-11-21 15:58:03 +0000 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Snackbars provide brief messages about app processes at the bottom of the screen. |
| 46 | * |
| 47 | * Snackbars inform users of a process that an app has performed or will perform. They appear |
| 48 | * temporarily, towards the bottom of the screen. They shouldn’t interrupt the user experience, |
| 49 | * and they don’t require user input to disappear. |
| 50 | * |
| 51 | * A snackbar can contain a single action. Because they disappear automatically, the action |
| 52 | * shouldn’t be “Dismiss” or “Cancel.” |
| 53 | * |
| 54 | * @sample androidx.ui.material.samples.SimpleSnackbar |
| 55 | * |
| 56 | * @param text information about a process that an app has performed or will perform |
| 57 | * @param actionText action name in the snackbar. If null, there will be text label with no |
| 58 | * action button |
| 59 | * @param onActionClick lambda to be invoked when the action is clicked |
| 60 | * @param modifier modifiers for the the Snackbar layout |
| 61 | * @param actionOnNewLine whether or not action should be put on the separate line. Recommended |
| 62 | * for action with long action text |
| 63 | */ |
| 64 | @Composable |
| 65 | fun Snackbar( |
| 66 | text: String, |
| 67 | actionText: String? = null, |
| 68 | onActionClick: (() -> Unit)? = null, |
| 69 | modifier: Modifier = Modifier.None, |
| 70 | actionOnNewLine: Boolean = false |
| 71 | ) { |
| 72 | val actionSlot: @Composable() (() -> Unit)? = |
| 73 | if (actionText != null) { |
| 74 | @Composable { |
Louis Pullen-Freilich | e386f97dd | 2020-01-31 17:32:33 +0000 | [diff] [blame] | 75 | TextButton( |
Matvei Malkov | b814df0 | 2019-11-21 15:58:03 +0000 | [diff] [blame] | 76 | onClick = onActionClick, |
Louis Pullen-Freilich | e386f97dd | 2020-01-31 17:32:33 +0000 | [diff] [blame] | 77 | // TODO: remove this when primary light variant is figured out |
| 78 | contentColor = makePrimaryVariantLight(MaterialTheme.colors().primary) |
| 79 | ) { |
| 80 | Text(actionText) |
| 81 | } |
Matvei Malkov | b814df0 | 2019-11-21 15:58:03 +0000 | [diff] [blame] | 82 | } |
| 83 | } else { |
| 84 | null |
| 85 | } |
| 86 | Snackbar( |
| 87 | modifier = modifier, |
| 88 | actionOnNewLine = actionOnNewLine, |
| 89 | text = { Text(text, maxLines = TextMaxLines) }, |
| 90 | action = actionSlot |
| 91 | ) |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Snackbars provide brief messages about app processes at the bottom of the screen. |
| 96 | * |
| 97 | * Snackbars inform users of a process that an app has performed or will perform. They appear |
| 98 | * temporarily, towards the bottom of the screen. They shouldn’t interrupt the user experience, |
| 99 | * and they don’t require user input to disappear. |
| 100 | * |
| 101 | * A snackbar can contain a single action. Because they disappear automatically, the action |
| 102 | * shouldn’t be “Dismiss” or “Cancel.” |
| 103 | * |
| 104 | * This version provides more granular control over the content of the Snackbar. Use it if you |
| 105 | * want to customize the content inside. |
| 106 | * |
| 107 | * @sample androidx.ui.material.samples.SlotsSnackbar |
| 108 | * |
| 109 | * @param text text component to show information about a process that an app has performed or |
| 110 | * will perform |
| 111 | * @param action action / button component to add as an action to the snackbar |
| 112 | * @param modifier modifiers for the the Snackbar layout |
| 113 | * @param actionOnNewLine whether or not action should be put on the separate line. Recommended |
| 114 | * for action with long action text |
| 115 | */ |
| 116 | @Composable |
| 117 | fun Snackbar( |
| 118 | text: @Composable() () -> Unit, |
| 119 | action: @Composable() (() -> Unit)? = null, |
| 120 | modifier: Modifier = Modifier.None, |
| 121 | actionOnNewLine: Boolean = false |
| 122 | ) { |
Leland Richardson | 7f848ab | 2019-12-12 13:43:41 -0800 | [diff] [blame] | 123 | val colors = MaterialTheme.colors() |
Matvei Malkov | b814df0 | 2019-11-21 15:58:03 +0000 | [diff] [blame] | 124 | Surface( |
| 125 | modifier = modifier, |
| 126 | shape = SnackbarShape, |
| 127 | elevation = SnackbarElevation, |
| 128 | color = colors.surface |
| 129 | ) { |
Leland Richardson | 7f848ab | 2019-12-12 13:43:41 -0800 | [diff] [blame] | 130 | val textStyle = MaterialTheme.typography().body2.copy(color = colors.surface) |
Matvei Malkov | 59bac36 | 2020-02-13 20:23:13 +0000 | [diff] [blame^] | 131 | val additionalBackground = DrawBackground( |
| 132 | color = colors.onSurface.copy(alpha = SnackbarOverlayAlpha), |
| 133 | shape = SnackbarShape |
Matvei Malkov | b814df0 | 2019-11-21 15:58:03 +0000 | [diff] [blame] | 134 | ) |
| 135 | CurrentTextStyleProvider(value = textStyle) { |
| 136 | when { |
Matvei Malkov | 59bac36 | 2020-02-13 20:23:13 +0000 | [diff] [blame^] | 137 | action == null -> TextOnlySnackbar(additionalBackground, text) |
| 138 | actionOnNewLine -> NewLineButtonSnackbar(additionalBackground, text, action) |
| 139 | else -> OneRowSnackbar(additionalBackground, text, action) |
Matvei Malkov | b814df0 | 2019-11-21 15:58:03 +0000 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | @Composable |
Matvei Malkov | 59bac36 | 2020-02-13 20:23:13 +0000 | [diff] [blame^] | 146 | private fun TextOnlySnackbar(modifier: Modifier = Modifier.None, text: @Composable() () -> Unit) { |
Matvei Malkov | b814df0 | 2019-11-21 15:58:03 +0000 | [diff] [blame] | 147 | Layout( |
Matvei Malkov | 59bac36 | 2020-02-13 20:23:13 +0000 | [diff] [blame^] | 148 | text, |
| 149 | modifier = modifier + LayoutPadding(start = HorizontalSpacing, end = HorizontalSpacing) |
Matvei Malkov | b814df0 | 2019-11-21 15:58:03 +0000 | [diff] [blame] | 150 | ) { measurables, constraints -> |
| 151 | require(measurables.size == 1) { |
| 152 | "text for Snackbar expected to have exactly only one child" |
| 153 | } |
| 154 | val textPlaceable = measurables.first().measure(constraints) |
| 155 | val firstBaseline = requireNotNull(textPlaceable[FirstBaseline]) { "No baselines for text" } |
| 156 | val lastBaseline = requireNotNull(textPlaceable[LastBaseline]) { "No baselines for text" } |
| 157 | |
| 158 | val minHeight = if (firstBaseline == lastBaseline) MinHeightOneLine else MinHeightTwoLines |
| 159 | layout(constraints.maxWidth, max(minHeight.toIntPx(), textPlaceable.height)) { |
| 160 | val textPlaceY = HeightToFirstLine.toIntPx() - firstBaseline |
| 161 | textPlaceable.place(0.ipx, textPlaceY) |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | @Composable |
| 167 | private fun NewLineButtonSnackbar( |
Matvei Malkov | 59bac36 | 2020-02-13 20:23:13 +0000 | [diff] [blame^] | 168 | modifier: Modifier = Modifier.None, |
Matvei Malkov | b814df0 | 2019-11-21 15:58:03 +0000 | [diff] [blame] | 169 | text: @Composable() () -> Unit, |
| 170 | button: @Composable() () -> Unit |
| 171 | ) { |
| 172 | Column( |
Matvei Malkov | 59bac36 | 2020-02-13 20:23:13 +0000 | [diff] [blame^] | 173 | modifier = modifier + LayoutWidth.Fill + LayoutPadding( |
Anastasia Soboleva | 24bacea | 2020-02-06 19:10:26 +0000 | [diff] [blame] | 174 | start = HorizontalSpacing, |
| 175 | end = HorizontalSpacingButtonSide, |
Matvei Malkov | b814df0 | 2019-11-21 15:58:03 +0000 | [diff] [blame] | 176 | bottom = SeparateButtonExtraY |
| 177 | ) |
| 178 | ) { |
| 179 | AlignmentLineOffset(alignmentLine = LastBaseline, after = LongButtonVerticalOffset) { |
| 180 | AlignmentLineOffset(alignmentLine = FirstBaseline, before = HeightToFirstLine) { |
Anastasia Soboleva | 24bacea | 2020-02-06 19:10:26 +0000 | [diff] [blame] | 181 | Container(LayoutPadding(end = HorizontalSpacingButtonSide), children = text) |
Matvei Malkov | b814df0 | 2019-11-21 15:58:03 +0000 | [diff] [blame] | 182 | } |
| 183 | } |
Adam Powell | 712dc99 | 2019-12-04 12:48:30 -0800 | [diff] [blame] | 184 | Container(modifier = LayoutGravity.End, children = button) |
Matvei Malkov | b814df0 | 2019-11-21 15:58:03 +0000 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
| 188 | @Composable |
| 189 | private fun OneRowSnackbar( |
Matvei Malkov | 59bac36 | 2020-02-13 20:23:13 +0000 | [diff] [blame^] | 190 | modifier: Modifier = Modifier.None, |
Matvei Malkov | b814df0 | 2019-11-21 15:58:03 +0000 | [diff] [blame] | 191 | text: @Composable() () -> Unit, |
| 192 | button: @Composable() () -> Unit |
| 193 | ) { |
| 194 | Layout( |
Mihai Popa | 8c474e9 | 2019-12-10 17:43:20 +0000 | [diff] [blame] | 195 | { |
| 196 | ParentData( |
| 197 | object : LayoutTagParentData { |
| 198 | override val tag: Any = "text" |
| 199 | }, |
| 200 | text |
| 201 | ) |
| 202 | ParentData( |
| 203 | object : LayoutTagParentData { |
| 204 | override val tag: Any = "button" |
| 205 | }, |
| 206 | button |
| 207 | ) |
| 208 | }, |
Matvei Malkov | 59bac36 | 2020-02-13 20:23:13 +0000 | [diff] [blame^] | 209 | modifier = modifier + |
| 210 | LayoutPadding(start = HorizontalSpacing, end = HorizontalSpacingButtonSide) |
Matvei Malkov | b814df0 | 2019-11-21 15:58:03 +0000 | [diff] [blame] | 211 | ) { measurables, constraints -> |
Mihai Popa | 8c474e9 | 2019-12-10 17:43:20 +0000 | [diff] [blame] | 212 | val buttonPlaceable = measurables.first { it.tag == "button" }.measure(constraints) |
Matvei Malkov | b814df0 | 2019-11-21 15:58:03 +0000 | [diff] [blame] | 213 | val textMaxWidth = |
| 214 | (constraints.maxWidth - buttonPlaceable.width - TextEndExtraSpacing.toIntPx()) |
| 215 | .coerceAtLeast(constraints.minWidth) |
Mihai Popa | 8c474e9 | 2019-12-10 17:43:20 +0000 | [diff] [blame] | 216 | val textPlaceable = measurables.first { it.tag == "text" }.measure( |
| 217 | constraints.copy(minHeight = IntPx.Zero, maxWidth = textMaxWidth) |
| 218 | ) |
Matvei Malkov | b814df0 | 2019-11-21 15:58:03 +0000 | [diff] [blame] | 219 | |
| 220 | val firstTextBaseline = |
| 221 | requireNotNull(textPlaceable[FirstBaseline]) { "No baselines for text" } |
| 222 | val lastTextBaseline = |
| 223 | requireNotNull(textPlaceable[LastBaseline]) { "No baselines for text" } |
| 224 | val baselineOffset = HeightToFirstLine.toIntPx() |
| 225 | val isOneLine = firstTextBaseline == lastTextBaseline |
| 226 | val textPlaceY = baselineOffset - firstTextBaseline |
| 227 | val buttonPlaceX = constraints.maxWidth - buttonPlaceable.width |
| 228 | |
| 229 | val containerHeight: IntPx |
| 230 | val buttonPlaceY: IntPx |
| 231 | if (isOneLine) { |
| 232 | val minContainerHeight = MinHeightOneLine.toIntPx() |
| 233 | val contentHeight = buttonPlaceable.height + SingleTextYPadding.toIntPx() * 2 |
| 234 | containerHeight = max(minContainerHeight, contentHeight) |
| 235 | val buttonBaseline = buttonPlaceable.get(FirstBaseline) |
| 236 | buttonPlaceY = |
| 237 | buttonBaseline?.let { baselineOffset - it } ?: SingleTextYPadding.toIntPx() |
| 238 | } else { |
| 239 | val minContainerHeight = MinHeightTwoLines.toIntPx() |
| 240 | val contentHeight = textPlaceY + textPlaceable.height |
| 241 | containerHeight = max(minContainerHeight, contentHeight) |
| 242 | buttonPlaceY = (containerHeight - buttonPlaceable.height) / 2 |
| 243 | } |
| 244 | |
| 245 | layout(constraints.maxWidth, containerHeight) { |
| 246 | textPlaceable.place(0.ipx, textPlaceY) |
| 247 | buttonPlaceable.place(buttonPlaceX, buttonPlaceY) |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | // TODO: remove this when primary light variant is figured out in MaterialTheme |
| 253 | private fun makePrimaryVariantLight(primary: Color): Color { |
| 254 | val blendColor = Color.White.copy(alpha = 0.6f) |
| 255 | return Color( |
| 256 | red = blendColor.red * blendColor.alpha + primary.red * (1 - blendColor.alpha), |
| 257 | green = blendColor.green * blendColor.alpha + primary.green * (1 - blendColor.alpha), |
| 258 | blue = blendColor.blue * blendColor.alpha + primary.blue * (1 - blendColor.alpha) |
| 259 | ) |
| 260 | } |
| 261 | |
| 262 | private val TextMaxLines = 2 |
| 263 | private val SnackbarOverlayAlpha = 0.8f |
| 264 | private val SnackbarShape = RoundedCornerShape(4.dp) |
| 265 | private val SnackbarElevation = 6.dp |
| 266 | |
| 267 | private val MinHeightOneLine = 48.dp |
| 268 | private val MinHeightTwoLines = 68.dp |
| 269 | private val HeightToFirstLine = 30.dp |
| 270 | private val HorizontalSpacing = 16.dp |
| 271 | private val HorizontalSpacingButtonSide = 8.dp |
| 272 | private val SeparateButtonExtraY = 8.dp |
| 273 | private val SingleTextYPadding = 6.dp |
| 274 | private val TextEndExtraSpacing = 8.dp |
| 275 | private val LongButtonVerticalOffset = 18.dp |