shepshapard | 0cc02c4 | 2019-06-28 15:51:14 -0700 | [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 | |
George Mount | 54370f1 | 2020-05-06 16:03:20 -0700 | [diff] [blame] | 17 | package androidx.ui.core.demos.gestures |
shepshapard | 0cc02c4 | 2019-06-28 15:51:14 -0700 | [diff] [blame] | 18 | |
Louis Pullen-Freilich | a228adf | 2020-03-11 18:42:11 +0000 | [diff] [blame] | 19 | import androidx.compose.Composable |
shepshapard | 0cc02c4 | 2019-06-28 15:51:14 -0700 | [diff] [blame] | 20 | import androidx.compose.state |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 21 | import androidx.ui.core.Alignment |
| 22 | import androidx.ui.core.Modifier |
Shep Shapard | 70a8cfe | 2020-03-30 16:17:26 -0700 | [diff] [blame] | 23 | import androidx.ui.core.gesture.tapGestureFilter |
Matvei Malkov | 0e4dc6d | 2020-02-19 15:11:59 +0000 | [diff] [blame] | 24 | import androidx.ui.foundation.Border |
| 25 | import androidx.ui.foundation.Box |
Shep Shapard | 07c9e2c | 2020-05-21 16:44:25 -0700 | [diff] [blame^] | 26 | import androidx.ui.foundation.Text |
| 27 | import androidx.ui.layout.Column |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 28 | import androidx.ui.layout.fillMaxSize |
| 29 | import androidx.ui.layout.preferredSize |
| 30 | import androidx.ui.layout.wrapContentSize |
Shep Shapard | e7d051f | 2020-05-13 15:19:16 -0700 | [diff] [blame] | 31 | import androidx.ui.unit.PxPosition |
George Mount | 842c8c1 | 2020-01-08 16:03:42 -0800 | [diff] [blame] | 32 | import androidx.ui.unit.dp |
shepshapard | 0cc02c4 | 2019-06-28 15:51:14 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
Shep Shapard | 07c9e2c | 2020-05-21 16:44:25 -0700 | [diff] [blame^] | 35 | * Simple [tapGestureFilter] demo. |
shepshapard | 0cc02c4 | 2019-06-28 15:51:14 -0700 | [diff] [blame] | 36 | */ |
Louis Pullen-Freilich | a228adf | 2020-03-11 18:42:11 +0000 | [diff] [blame] | 37 | @Composable |
Shep Shapard | 07c9e2c | 2020-05-21 16:44:25 -0700 | [diff] [blame^] | 38 | fun TapGestureFilterDemo() { |
Louis Pullen-Freilich | a228adf | 2020-03-11 18:42:11 +0000 | [diff] [blame] | 39 | val color = state { Colors.random() } |
shepshapard | 0cc02c4 | 2019-06-28 15:51:14 -0700 | [diff] [blame] | 40 | |
Shep Shapard | e7d051f | 2020-05-13 15:19:16 -0700 | [diff] [blame] | 41 | val onTap: (PxPosition) -> Unit = { |
Louis Pullen-Freilich | a228adf | 2020-03-11 18:42:11 +0000 | [diff] [blame] | 42 | color.value = color.value.anotherRandomColor() |
| 43 | } |
shepshapard | 0cc02c4 | 2019-06-28 15:51:14 -0700 | [diff] [blame] | 44 | |
Shep Shapard | 07c9e2c | 2020-05-21 16:44:25 -0700 | [diff] [blame^] | 45 | Column { |
| 46 | Text("The box changes color when you tap it.") |
| 47 | Box( |
| 48 | Modifier.fillMaxSize() |
| 49 | .wrapContentSize(Alignment.Center) |
| 50 | .tapGestureFilter(onTap) |
| 51 | .preferredSize(192.dp), |
| 52 | backgroundColor = color.value, |
| 53 | border = Border(2.dp, BorderColor) |
| 54 | ) |
| 55 | } |
Shep Shapard | 8931646 | 2020-03-03 17:00:52 -0800 | [diff] [blame] | 56 | } |