Matvei Malkov | 0f23661 | 2019-07-04 15:54:58 +0100 | [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.samples |
| 18 | |
| 19 | import androidx.annotation.Sampled |
| 20 | import androidx.compose.Composable |
Matvei Malkov | 0f23661 | 2019-07-04 15:54:58 +0100 | [diff] [blame] | 21 | import androidx.compose.state |
Matvei Malkov | 0f23661 | 2019-07-04 15:54:58 +0100 | [diff] [blame] | 22 | import androidx.ui.core.Text |
Clara Bayarri | 0e00c964 | 2019-07-02 13:08:40 +0100 | [diff] [blame] | 23 | import androidx.ui.foundation.ColoredRect |
Matvei Malkov | 0f23661 | 2019-07-04 15:54:58 +0100 | [diff] [blame] | 24 | import androidx.ui.graphics.Color |
Matvei Malkov | 0f23661 | 2019-07-04 15:54:58 +0100 | [diff] [blame] | 25 | import androidx.ui.layout.Center |
| 26 | import androidx.ui.layout.Column |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 27 | import androidx.ui.layout.Container |
Adam Powell | 712dc99 | 2019-12-04 12:48:30 -0800 | [diff] [blame] | 28 | import androidx.ui.layout.LayoutHeight |
Adam Powell | 31c1ebd | 2020-01-09 09:48:24 -0800 | [diff] [blame] | 29 | import androidx.ui.layout.LayoutWidth |
Matvei Malkov | 0f23661 | 2019-07-04 15:54:58 +0100 | [diff] [blame] | 30 | import androidx.ui.layout.Row |
George Mount | 842c8c1 | 2020-01-08 16:03:42 -0800 | [diff] [blame] | 31 | import androidx.ui.layout.Spacer |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 32 | import androidx.ui.material.BottomDrawerLayout |
Matvei Malkov | 0f23661 | 2019-07-04 15:54:58 +0100 | [diff] [blame] | 33 | import androidx.ui.material.Button |
| 34 | import androidx.ui.material.DrawerState |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 35 | import androidx.ui.material.ModalDrawerLayout |
Matvei Malkov | 0f23661 | 2019-07-04 15:54:58 +0100 | [diff] [blame] | 36 | import androidx.ui.material.StaticDrawer |
George Mount | 842c8c1 | 2020-01-08 16:03:42 -0800 | [diff] [blame] | 37 | import androidx.ui.unit.dp |
Matvei Malkov | 0f23661 | 2019-07-04 15:54:58 +0100 | [diff] [blame] | 38 | |
| 39 | @Sampled |
| 40 | @Composable |
| 41 | fun StaticDrawerSample() { |
Adam Powell | 31c1ebd | 2020-01-09 09:48:24 -0800 | [diff] [blame] | 42 | Row(LayoutWidth.Fill) { |
Matvei Malkov | 0f23661 | 2019-07-04 15:54:58 +0100 | [diff] [blame] | 43 | StaticDrawer { |
| 44 | Center { |
| 45 | Text("Drawer Content") |
| 46 | } |
| 47 | } |
| 48 | ColoredRect(Color.Black, width = 1.dp) |
| 49 | Text("Rest of App") |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | @Sampled |
| 54 | @Composable |
| 55 | fun ModalDrawerSample() { |
Leland Richardson | 7f848ab | 2019-12-12 13:43:41 -0800 | [diff] [blame] | 56 | val (state, onStateChange) = state { DrawerState.Closed } |
Matvei Malkov | 0f23661 | 2019-07-04 15:54:58 +0100 | [diff] [blame] | 57 | val appContentText = |
| 58 | if (state == DrawerState.Closed) ">>> Pull to open >>>" else "<<< Swipe to close <<<" |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 59 | ModalDrawerLayout( |
| 60 | drawerState = state, |
| 61 | onStateChange = onStateChange, |
| 62 | drawerContent = { YourDrawerContent(onStateChange) }, |
| 63 | bodyContent = { YourAppContent(appContentText, onStateChange) } |
| 64 | ) |
Matvei Malkov | 0f23661 | 2019-07-04 15:54:58 +0100 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @Sampled |
| 68 | @Composable |
| 69 | fun BottomDrawerSample() { |
Leland Richardson | 7f848ab | 2019-12-12 13:43:41 -0800 | [diff] [blame] | 70 | val (state, onStateChange) = state { DrawerState.Closed } |
Matvei Malkov | 0f23661 | 2019-07-04 15:54:58 +0100 | [diff] [blame] | 71 | val appContentText = |
| 72 | if (state == DrawerState.Closed) "▲▲▲ Pull to open ▲▲▲" else "▼▼▼ Drag down to close ▼▼▼" |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 73 | BottomDrawerLayout( |
| 74 | drawerState = state, |
| 75 | onStateChange = onStateChange, |
| 76 | drawerContent = { YourDrawerContent(onStateChange) }, |
| 77 | bodyContent = { YourAppContent(appContentText, onStateChange) } |
| 78 | ) |
| 79 | } |
| 80 | |
| 81 | @Composable |
| 82 | private fun YourDrawerContent(onStateChange: (DrawerState) -> Unit) { |
| 83 | Container(expanded = true) { |
Adam Powell | 31c1ebd | 2020-01-09 09:48:24 -0800 | [diff] [blame] | 84 | Column(LayoutHeight.Fill) { |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 85 | Text(text = "Drawer Content") |
Adam Powell | 712dc99 | 2019-12-04 12:48:30 -0800 | [diff] [blame] | 86 | Spacer(LayoutHeight(20.dp)) |
Louis Pullen-Freilich | e386f97dd | 2020-01-31 17:32:33 +0000 | [diff] [blame] | 87 | Button(onClick = { onStateChange(DrawerState.Closed) }) { |
| 88 | Text("Close Drawer") |
| 89 | } |
Matvei Malkov | 0f23661 | 2019-07-04 15:54:58 +0100 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | @Composable |
| 95 | private fun YourAppContent(text: String, onDrawerStateChange: (DrawerState) -> Unit) { |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 96 | Center { |
Adam Powell | 31c1ebd | 2020-01-09 09:48:24 -0800 | [diff] [blame] | 97 | Column(LayoutHeight.Fill) { |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 98 | Text(text = text) |
Adam Powell | 712dc99 | 2019-12-04 12:48:30 -0800 | [diff] [blame] | 99 | Spacer(LayoutHeight(20.dp)) |
Louis Pullen-Freilich | e386f97dd | 2020-01-31 17:32:33 +0000 | [diff] [blame] | 100 | Button(onClick = { onDrawerStateChange(DrawerState.Opened) }) { |
| 101 | Text("Click to open") |
| 102 | } |
Matvei Malkov | 7aa7fd2 | 2019-08-16 16:52:59 +0100 | [diff] [blame] | 103 | } |
Matvei Malkov | 0f23661 | 2019-07-04 15:54:58 +0100 | [diff] [blame] | 104 | } |
| 105 | } |