Skip to content

Commit

Permalink
Ignore unpressed events during velocity calculation on iOS (#848)
Browse files Browse the repository at this point in the history
  • Loading branch information
elijah-semyonov committed Oct 12, 2023
1 parent e0bdb0e commit e1e5a87
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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
*
* http://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 androidx.compose.ui.input.pointer.util

/**
* Some platforms (e.g. iOS) ignore certain events during velocity calculation.
*/
internal actual fun VelocityTracker.shouldUse(event: PointerInputChange): Boolean = true
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,11 @@ private fun Array<DataPointAtTime?>.set(index: Int, time: Long, dataPoint: Float
}
}

/**
* Some platforms (e.g. iOS) ignore certain events during velocity calculation.
*/
internal expect fun VelocityTracker.shouldUse(event: PointerInputChange): Boolean

/**
* Track the positions and timestamps inside this event change.
*
Expand All @@ -308,6 +313,10 @@ fun VelocityTracker.addPointerInputChange(event: PointerInputChange) {
resetTracking()
}

if (!shouldUse(event)) {
return
}

// To calculate delta, for each step we want to do currentPosition - previousPosition.
// Initially the previous position is the previous position of the current event
var previousPointerPosition = event.previousPosition
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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
*
* http://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 androidx.compose.ui.input.pointer.util

import androidx.compose.ui.input.pointer.PointerInputChange

/**
* Some platforms (e.g. iOS) ignore certain events during velocity calculation.
*/
internal actual fun VelocityTracker.shouldUse(event: PointerInputChange): Boolean = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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
*
* http://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 androidx.compose.ui.input.pointer.util

import androidx.compose.ui.input.pointer.PointerInputChange

/**
* Some platforms (e.g. iOS) ignore certain events during velocity calculation.
*/
internal actual fun VelocityTracker.shouldUse(event: PointerInputChange): Boolean = event.pressed

0 comments on commit e1e5a87

Please sign in to comment.