-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Vertical pager components (#1937)
* Add VerticalPager components * Add VerticalPager components * Add VerticalPager components * Add VerticalPager components * 🤖 reformat --------- Co-authored-by: yschimke <yschimke@users.noreply.github.com>
- Loading branch information
Showing
16 changed files
with
338 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...layout/src/main/java/com/google/android/horologist/compose/pager/VerticalPageIndicator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2024 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.android.horologist.compose.pager | ||
|
||
import androidx.compose.foundation.shape.CircleShape | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.draw.rotate | ||
import androidx.compose.ui.draw.scale | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.graphics.Shape | ||
import androidx.compose.ui.unit.Dp | ||
import androidx.compose.ui.unit.dp | ||
import androidx.wear.compose.material.HorizontalPageIndicator | ||
import androidx.wear.compose.material.MaterialTheme | ||
import androidx.wear.compose.material.PageIndicatorDefaults | ||
import androidx.wear.compose.material.PageIndicatorState | ||
import androidx.wear.compose.material.PageIndicatorStyle | ||
import com.google.android.horologist.annotations.ExperimentalHorologistApi | ||
|
||
/** | ||
* A Wear Material Compliant Vertical Page Indicator. | ||
*/ | ||
@Composable | ||
@ExperimentalHorologistApi | ||
fun VerticalPageIndicator( | ||
pageIndicatorState: PageIndicatorState, | ||
modifier: Modifier = Modifier, | ||
indicatorStyle: PageIndicatorStyle = PageIndicatorDefaults.style(), | ||
selectedColor: Color = MaterialTheme.colors.onBackground, | ||
unselectedColor: Color = selectedColor.copy(alpha = 0.3f), | ||
indicatorSize: Dp = 6.dp, | ||
spacing: Dp = 4.dp, | ||
indicatorShape: Shape = CircleShape, | ||
) { | ||
HorizontalPageIndicator( | ||
pageIndicatorState = pageIndicatorState, | ||
modifier = modifier | ||
.rotate(90f) | ||
.scale(scaleY = -1f, scaleX = 1f), | ||
indicatorStyle = indicatorStyle, | ||
selectedColor = selectedColor, | ||
unselectedColor = unselectedColor, | ||
indicatorSize = indicatorSize, | ||
spacing = spacing, | ||
indicatorShape = indicatorShape, | ||
) | ||
} |
71 changes: 71 additions & 0 deletions
71
...e-layout/src/main/java/com/google/android/horologist/compose/pager/VerticalPagerScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2024 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
@file:OptIn(ExperimentalFoundationApi::class, ExperimentalWearFoundationApi::class) | ||
|
||
package com.google.android.horologist.compose.pager | ||
|
||
import androidx.compose.foundation.ExperimentalFoundationApi | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.pager.PagerState | ||
import androidx.compose.foundation.pager.VerticalPager | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.unit.dp | ||
import androidx.wear.compose.foundation.ExperimentalWearFoundationApi | ||
import androidx.wear.compose.foundation.rememberActiveFocusRequester | ||
import com.google.android.horologist.annotations.ExperimentalHorologistApi | ||
import com.google.android.horologist.compose.layout.ScreenScaffold | ||
import com.google.android.horologist.compose.rotaryinput.onRotaryInputAccumulated | ||
import com.google.android.horologist.compose.rotaryinput.rotaryWithPager | ||
|
||
/** | ||
* A Wear Material Compliant Vertical Pager screen. | ||
* | ||
* Combines the Compose Foundation Pager, with a VerticalPageIndicator. | ||
* | ||
* The screens gets an [onRotaryInputAccumulated] modifier added for RSB handling. | ||
*/ | ||
@Composable | ||
@ExperimentalHorologistApi | ||
public fun VerticalPagerScreen( | ||
state: PagerState, | ||
modifier: Modifier = Modifier, | ||
content: @Composable ((Int) -> Unit), | ||
) { | ||
ScreenScaffold( | ||
modifier = modifier.fillMaxSize(), | ||
positionIndicator = { | ||
VerticalPageIndicator( | ||
pageIndicatorState = PageScreenIndicatorState(state), | ||
modifier = Modifier.padding(6.dp), | ||
) | ||
}, | ||
) { | ||
VerticalPager( | ||
modifier = Modifier | ||
.fillMaxSize() | ||
.rotaryWithPager(state, rememberActiveFocusRequester()), | ||
state = state, | ||
flingBehavior = HorizontalPagerDefaults.flingParams(state), | ||
) { page -> | ||
ClippedBox(state) { | ||
content(page) | ||
} | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...layout/src/main/java/com/google/android/horologist/compose/rotaryinput/RotaryWithPager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2024 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
@file:OptIn(ExperimentalFoundationApi::class) | ||
|
||
package com.google.android.horologist.compose.rotaryinput | ||
|
||
import androidx.compose.foundation.ExperimentalFoundationApi | ||
import androidx.compose.foundation.focusable | ||
import androidx.compose.foundation.pager.PagerState | ||
import androidx.compose.runtime.rememberCoroutineScope | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.composed | ||
import androidx.compose.ui.focus.FocusRequester | ||
import androidx.compose.ui.focus.focusRequester | ||
import com.google.android.horologist.compose.rotaryinput.RotaryInputConfigDefaults.DEFAULT_MIN_VALUE_CHANGE_DISTANCE_PX | ||
import kotlinx.coroutines.launch | ||
|
||
public fun Modifier.rotaryWithPager( | ||
state: PagerState, | ||
focusRequester: FocusRequester, | ||
): Modifier = composed { | ||
val coroutineScope = rememberCoroutineScope() | ||
val haptics = rememberDefaultRotaryHapticFeedback() | ||
|
||
onRotaryInputAccumulated(minValueChangeDistancePx = DEFAULT_MIN_VALUE_CHANGE_DISTANCE_PX * 3) { | ||
val pageChange = if (it > 0f) 1 else -1 | ||
|
||
if ((pageChange == 1 && state.currentPage >= state.pageCount - 1) || (pageChange == -1 && state.currentPage == 0)) { | ||
haptics.performHapticFeedback(RotaryHapticsType.ScrollLimit) | ||
} else { | ||
haptics.performHapticFeedback(RotaryHapticsType.ScrollItemFocus) | ||
|
||
coroutineScope.launch { | ||
state.animateScrollToPage(state.currentPage + pageChange) | ||
} | ||
} | ||
} | ||
.focusRequester(focusRequester) | ||
.focusable() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...est/java/com/google/android/horologist/compose/pager/VerticalPagerScreenScreenshotTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright 2023 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
@file:OptIn(ExperimentalFoundationApi::class) | ||
|
||
package com.google.android.horologist.compose.pager | ||
|
||
import androidx.compose.foundation.ExperimentalFoundationApi | ||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.pager.rememberPagerState | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.wear.compose.material.Text | ||
import com.google.android.horologist.screenshots.ScreenshotBaseTest | ||
import com.google.android.horologist.screenshots.ScreenshotTestRule | ||
import org.junit.Test | ||
|
||
class VerticalPagerScreenScreenshotTest : ScreenshotBaseTest( | ||
params = ScreenshotTestRule.screenshotTestRuleParams { | ||
screenTimeText = {} | ||
record = ScreenshotTestRule.RecordMode.Record | ||
}, | ||
) { | ||
|
||
@Test | ||
fun screens() { | ||
screenshotTestRule.setContent(takeScreenshot = true, roundScreen = true) { | ||
VerticalPagerScreen( | ||
state = rememberPagerState { | ||
10 | ||
}, | ||
) { | ||
Box( | ||
modifier = Modifier | ||
.fillMaxSize() | ||
.background(Color.DarkGray), | ||
) { | ||
Text(text = "Item $it", modifier = Modifier.align(Alignment.Center)) | ||
} | ||
} | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
....android.horologist.compose.pager_VerticalPagerScreenScreenshotTest_screens.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.