-
Notifications
You must be signed in to change notification settings - Fork 425
Migrate Compose touch and input snippets #1118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pengzhi-cb
wants to merge
6
commits into
android:main
Choose a base branch
from
pengzhi-cb:pengzhi-cb/compose-migrate-swipeable
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fba83f9
Add code snippets for migrate-swipeable
pengzhi-cb 0423276
Add code snippets for nested-scroll-modifiers
pengzhi-cb aa14dc4
Mark helper enum class DragValue private in MigrateSwipeable.kt
pengzhi-cb 0bcab17
Use plain [START_EXCLUDE] instead of long-form // ... + [START_EXCLUD…
pengzhi-cb c8f5d25
Restore unqualified Start, Center, End enum entries in MigrateSwipeab…
pengzhi-cb 253d2a9
Merge branch 'main' into pengzhi-cb/compose-migrate-swipeable
kkuan2011 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
216 changes: 216 additions & 0 deletions
216
...ts/src/main/java/com/example/compose/snippets/touchinput/pointerinput/MigrateSwipeable.kt
This file contains hidden or 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,216 @@ | ||
| /* | ||
| * Copyright 2026 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.example.compose.snippets.touchinput.pointerinput | ||
|
|
||
| import androidx.compose.animation.core.exponentialDecay | ||
| import androidx.compose.animation.core.spring | ||
| import androidx.compose.foundation.ExperimentalFoundationApi | ||
| import androidx.compose.foundation.gestures.AnchoredDraggableState | ||
| import androidx.compose.foundation.gestures.DraggableAnchors | ||
| import androidx.compose.foundation.layout.Box | ||
| import androidx.compose.foundation.layout.offset | ||
| import androidx.compose.material.ExperimentalMaterialApi | ||
| import androidx.compose.material.SwipeableState | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.SideEffect | ||
| import androidx.compose.runtime.remember | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.platform.LocalDensity | ||
| import androidx.compose.ui.unit.IntOffset | ||
| import androidx.compose.ui.unit.dp | ||
| import com.example.compose.snippets.touchinput.pointerinput.DragValue.Center | ||
| import com.example.compose.snippets.touchinput.pointerinput.DragValue.End | ||
| import com.example.compose.snippets.touchinput.pointerinput.DragValue.Start | ||
|
|
||
| @OptIn(ExperimentalMaterialApi::class) | ||
| private object SwipeableStateExample { | ||
| // [START android_compose_touchinput_pointerinput_migrate_swipeable_state_legacy] | ||
| class MySwitchState : SwipeableState<DragValue>(initialValue = DragValue.Start) | ||
| // [END android_compose_touchinput_pointerinput_migrate_swipeable_state_legacy] | ||
| } | ||
|
|
||
| @OptIn(ExperimentalFoundationApi::class) | ||
| private object AnchoredDraggableStateExample { | ||
| // [START android_compose_touchinput_pointerinput_migrate_swipeable_state_anchored] | ||
| class MySwitchState { | ||
| private val anchoredDraggableState = AnchoredDraggableState( | ||
| // [START_EXCLUDE] | ||
| initialValue = DragValue.Start, | ||
| positionalThreshold = { distance: Float -> distance * 0.5f }, | ||
| velocityThreshold = { 125f }, | ||
| snapAnimationSpec = spring(), | ||
| decayAnimationSpec = exponentialDecay() | ||
| // [END_EXCLUDE] | ||
| ) | ||
| } | ||
| // [END android_compose_touchinput_pointerinput_migrate_swipeable_state_anchored] | ||
| } | ||
|
|
||
| @OptIn(ExperimentalFoundationApi::class) | ||
| // [START android_compose_touchinput_pointerinput_migrate_swipeable_access_offset] | ||
| @Composable | ||
| fun AnchoredDraggableBox() { | ||
| val state = remember { | ||
| AnchoredDraggableState( | ||
| // [START_EXCLUDE] | ||
| initialValue = DragValue.Start, | ||
| positionalThreshold = { distance: Float -> distance * 0.5f }, | ||
| velocityThreshold = { 125f }, | ||
| snapAnimationSpec = spring(), | ||
| decayAnimationSpec = exponentialDecay() | ||
| // [END_EXCLUDE] | ||
| ) | ||
| } | ||
| val density = LocalDensity.current | ||
| val anchors = remember { | ||
| DraggableAnchors { | ||
| // [START_EXCLUDE] | ||
| DragValue.Start at -100f | ||
| DragValue.Center at 0f | ||
| DragValue.End at 100f | ||
| // [END_EXCLUDE] | ||
| } | ||
| } | ||
| SideEffect { | ||
| state.updateAnchors(anchors) | ||
| } | ||
| Box( | ||
| Modifier.offset { IntOffset(x = state.requireOffset().toInt(), y = 0) } | ||
| ) | ||
| } | ||
| // [END android_compose_touchinput_pointerinput_migrate_swipeable_access_offset] | ||
|
|
||
| @OptIn(ExperimentalFoundationApi::class) | ||
| private object AnchoredDraggableConstructorExample { | ||
| // [START android_compose_touchinput_pointerinput_migrate_swipeable_anchors_constructor] | ||
| enum class DragValue { Start, Center, End } | ||
|
|
||
| @Composable | ||
| fun AnchoredDraggableBox() { | ||
| val density = LocalDensity.current | ||
| val anchors = with(density) { | ||
| DraggableAnchors { | ||
| Start at -100.dp.toPx() | ||
| Center at 0f | ||
| End at 100.dp.toPx() | ||
| } | ||
| } | ||
| val state = remember { | ||
| AnchoredDraggableState( | ||
| anchors = anchors, | ||
| // [START_EXCLUDE silent] | ||
| initialValue = Start, | ||
| positionalThreshold = { distance: Float -> distance * 0.5f }, | ||
| velocityThreshold = { 125f }, | ||
| snapAnimationSpec = spring(), | ||
| decayAnimationSpec = exponentialDecay() | ||
| // [END_EXCLUDE] | ||
| ) | ||
| } | ||
| Box( | ||
| Modifier.offset { IntOffset(x = state.requireOffset().toInt(), y = 0) } | ||
| ) | ||
| } | ||
| // [END android_compose_touchinput_pointerinput_migrate_swipeable_anchors_constructor] | ||
| } | ||
|
|
||
| @OptIn(ExperimentalFoundationApi::class) | ||
| private object AnchoredDraggableUpdateAnchorsExample { | ||
| // [START android_compose_touchinput_pointerinput_migrate_swipeable_anchors_update] | ||
| enum class DragValue { Start, Center, End } | ||
|
|
||
| @Composable | ||
| fun AnchoredDraggableBox() { | ||
| val state = remember { | ||
| AnchoredDraggableState( | ||
| // [START_EXCLUDE] | ||
| initialValue = Start, | ||
| positionalThreshold = { distance: Float -> distance * 0.5f }, | ||
| velocityThreshold = { 125f }, | ||
| snapAnimationSpec = spring(), | ||
| decayAnimationSpec = exponentialDecay() | ||
| // [END_EXCLUDE] | ||
| ) | ||
| } | ||
| val density = LocalDensity.current | ||
| val anchors = with(density) { | ||
| DraggableAnchors { | ||
| Start at -100.dp.toPx() | ||
| Center at 0f | ||
| End at 100.dp.toPx() | ||
| } | ||
| } | ||
| SideEffect { | ||
| state.updateAnchors(anchors) | ||
| } | ||
| Box( | ||
| Modifier.offset { IntOffset(x = state.requireOffset().toInt(), y = 0) } | ||
| ) | ||
| } | ||
| // [END android_compose_touchinput_pointerinput_migrate_swipeable_anchors_update] | ||
| } | ||
|
|
||
| @OptIn(ExperimentalFoundationApi::class) | ||
| private fun PositionalThresholdFraction() { | ||
| // [START android_compose_touchinput_pointerinput_migrate_swipeable_positional_threshold_fraction] | ||
| val anchoredDraggableState = AnchoredDraggableState( | ||
| positionalThreshold = { distance -> distance * 0.5f }, | ||
| // [START_EXCLUDE] | ||
| initialValue = DragValue.Start, | ||
| velocityThreshold = { 125f }, | ||
| snapAnimationSpec = spring(), | ||
| decayAnimationSpec = exponentialDecay() | ||
| // [END_EXCLUDE] | ||
| ) | ||
| // [END android_compose_touchinput_pointerinput_migrate_swipeable_positional_threshold_fraction] | ||
| } | ||
|
|
||
| @OptIn(ExperimentalFoundationApi::class) | ||
| @Composable | ||
| private fun PositionalThresholdDp() { | ||
| // [START android_compose_touchinput_pointerinput_migrate_swipeable_positional_threshold_dp] | ||
| val density = LocalDensity.current | ||
| val anchoredDraggableState = AnchoredDraggableState( | ||
| positionalThreshold = { with(density) { 56.dp.toPx() } }, | ||
| // [START_EXCLUDE] | ||
| initialValue = DragValue.Start, | ||
| velocityThreshold = { 125f }, | ||
| snapAnimationSpec = spring(), | ||
| decayAnimationSpec = exponentialDecay() | ||
| // [END_EXCLUDE] | ||
| ) | ||
| // [END android_compose_touchinput_pointerinput_migrate_swipeable_positional_threshold_dp] | ||
| } | ||
|
|
||
| @OptIn(ExperimentalFoundationApi::class) | ||
| @Composable | ||
| private fun VelocityThreshold() { | ||
| // [START android_compose_touchinput_pointerinput_migrate_swipeable_velocity_threshold] | ||
| val density = LocalDensity.current | ||
| val anchoredDraggableState = AnchoredDraggableState( | ||
| velocityThreshold = { with(density) { 125.dp.toPx() } }, | ||
| // [START_EXCLUDE] | ||
| initialValue = DragValue.Start, | ||
| positionalThreshold = { distance: Float -> distance * 0.5f }, | ||
| snapAnimationSpec = spring(), | ||
| decayAnimationSpec = exponentialDecay() | ||
| // [END_EXCLUDE] | ||
| ) | ||
| // [END android_compose_touchinput_pointerinput_migrate_swipeable_velocity_threshold] | ||
| } | ||
|
|
||
| private enum class DragValue { Start, Center, End } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like we're using deprecated APIs in this file, so not sure if we should migrate those snippets