|
| 1 | +/* |
| 2 | + * Copyright 2024 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 | +@file:OptIn(ExperimentalMaterial3AdaptiveApi::class) |
| 18 | + |
| 19 | +package com.example.compose.snippets.adaptivelayouts |
| 20 | + |
| 21 | +import androidx.activity.compose.BackHandler |
| 22 | +import androidx.compose.foundation.layout.safeContentPadding |
| 23 | +import androidx.compose.foundation.layout.wrapContentSize |
| 24 | +import androidx.compose.material3.Button |
| 25 | +import androidx.compose.material3.Text |
| 26 | +import androidx.compose.material3.adaptive.ExperimentalMaterial3AdaptiveApi |
| 27 | +import androidx.compose.material3.adaptive.layout.AnimatedPane |
| 28 | +import androidx.compose.material3.adaptive.layout.PaneAdaptedValue |
| 29 | +import androidx.compose.material3.adaptive.layout.SupportingPaneScaffold |
| 30 | +import androidx.compose.material3.adaptive.layout.SupportingPaneScaffoldRole |
| 31 | +import androidx.compose.material3.adaptive.layout.ThreePaneScaffoldRole |
| 32 | +import androidx.compose.material3.adaptive.layout.ThreePaneScaffoldScope |
| 33 | +import androidx.compose.material3.adaptive.navigation.rememberSupportingPaneScaffoldNavigator |
| 34 | +import androidx.compose.runtime.Composable |
| 35 | +import androidx.compose.ui.Modifier |
| 36 | + |
| 37 | +@Composable |
| 38 | +fun SampleSupportingPaneScaffoldParts() { |
| 39 | + // [START android_compose_adaptivelayouts_sample_supporting_pane_scaffold_nav_and_back] |
| 40 | + val navigator = rememberSupportingPaneScaffoldNavigator() |
| 41 | + |
| 42 | + BackHandler(navigator.canNavigateBack()) { |
| 43 | + navigator.navigateBack() |
| 44 | + } |
| 45 | + // [END android_compose_adaptivelayouts_sample_supporting_pane_scaffold_nav_and_back] |
| 46 | + |
| 47 | + // [START android_compose_adaptivelayouts_sample_supporting_pane_scaffold_params] |
| 48 | + SupportingPaneScaffold( |
| 49 | + directive = navigator.scaffoldDirective, |
| 50 | + value = navigator.scaffoldValue, |
| 51 | + mainPane = { /*...*/ }, |
| 52 | + supportingPane = { /*...*/ }, |
| 53 | + ) |
| 54 | + // [END android_compose_adaptivelayouts_sample_supporting_pane_scaffold_params] |
| 55 | +} |
| 56 | + |
| 57 | +@Composable |
| 58 | +fun SampleSupportingPaneScaffoldFull() { |
| 59 | + // [START android_compose_adaptivelayouts_sample_supporting_pane_scaffold_full] |
| 60 | + val navigator = rememberSupportingPaneScaffoldNavigator() |
| 61 | + |
| 62 | + BackHandler(navigator.canNavigateBack()) { |
| 63 | + navigator.navigateBack() |
| 64 | + } |
| 65 | + |
| 66 | + SupportingPaneScaffold( |
| 67 | + directive = navigator.scaffoldDirective, |
| 68 | + value = navigator.scaffoldValue, |
| 69 | + mainPane = { |
| 70 | + AnimatedPane(modifier = Modifier.safeContentPadding()) { |
| 71 | + // Main pane content |
| 72 | + if (navigator.scaffoldValue[SupportingPaneScaffoldRole.Supporting] == PaneAdaptedValue.Hidden) { |
| 73 | + Button( |
| 74 | + modifier = Modifier.wrapContentSize(), |
| 75 | + onClick = { |
| 76 | + navigator.navigateTo(SupportingPaneScaffoldRole.Supporting) |
| 77 | + } |
| 78 | + ) { |
| 79 | + Text("Show supporting pane") |
| 80 | + } |
| 81 | + } else { |
| 82 | + Text("Supporting pane is shown") |
| 83 | + } |
| 84 | + } |
| 85 | + }, |
| 86 | + supportingPane = { |
| 87 | + AnimatedPane(modifier = Modifier.safeContentPadding()) { |
| 88 | + // Supporting pane content |
| 89 | + Text("Supporting pane") |
| 90 | + } |
| 91 | + }, |
| 92 | + ) |
| 93 | + // [END android_compose_adaptivelayouts_sample_supporting_pane_scaffold_full] |
| 94 | +} |
| 95 | + |
| 96 | +// [START android_compose_adaptivelayouts_sample_supporting_pane_scaffold_extracted_panes] |
| 97 | +@Composable |
| 98 | +fun ThreePaneScaffoldScope.MainPane( |
| 99 | + shouldShowSupportingPaneButton: Boolean, |
| 100 | + onNavigateToSupportingPane: () -> Unit, |
| 101 | + modifier: Modifier = Modifier, |
| 102 | +) { |
| 103 | + AnimatedPane(modifier = modifier.safeContentPadding()) { |
| 104 | + // Main pane content |
| 105 | + if (shouldShowSupportingPaneButton) { |
| 106 | + Button(onClick = onNavigateToSupportingPane) { |
| 107 | + Text("Show supporting pane") |
| 108 | + } |
| 109 | + } else { |
| 110 | + Text("Supporting pane is shown") |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +@Composable |
| 116 | +fun ThreePaneScaffoldScope.SupportingPane( |
| 117 | + modifier: Modifier = Modifier, |
| 118 | +) { |
| 119 | + AnimatedPane(modifier = modifier.safeContentPadding()) { |
| 120 | + // Supporting pane content |
| 121 | + Text("This is the supporting pane") |
| 122 | + } |
| 123 | +} |
| 124 | +// [END android_compose_adaptivelayouts_sample_supporting_pane_scaffold_extracted_panes] |
| 125 | + |
| 126 | +@Composable |
| 127 | +fun SampleSupportingPaneScaffoldSimplified() { |
| 128 | +// [START android_compose_adaptivelayouts_sample_supporting_pane_scaffold_simplified] |
| 129 | + val navigator = rememberSupportingPaneScaffoldNavigator() |
| 130 | + |
| 131 | + BackHandler(navigator.canNavigateBack()) { |
| 132 | + navigator.navigateBack() |
| 133 | + } |
| 134 | + |
| 135 | + SupportingPaneScaffold( |
| 136 | + directive = navigator.scaffoldDirective, |
| 137 | + value = navigator.scaffoldValue, |
| 138 | + mainPane = { |
| 139 | + MainPane( |
| 140 | + shouldShowSupportingPaneButton = navigator.scaffoldValue.secondary == PaneAdaptedValue.Hidden, |
| 141 | + onNavigateToSupportingPane = { navigator.navigateTo(ThreePaneScaffoldRole.Secondary) } |
| 142 | + ) |
| 143 | + }, |
| 144 | + supportingPane = { SupportingPane() }, |
| 145 | + ) |
| 146 | +// [END android_compose_adaptivelayouts_sample_supporting_pane_scaffold_simplified] |
| 147 | +} |
0 commit comments