Skip to content
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

Fix Material3 slider getting stuck while dragging #1135

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions compose/material3/material3/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ if(AndroidXComposePlugin.isMultiplatformEnabled(project)) {

skikoTest.dependencies {
implementation(libs.kotlinTest)
implementation(project(":compose:ui:ui-test"))
}

desktopTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,18 +267,17 @@ fun Slider(
val state = remember(
steps,
valueRange,
onValueChangeFinished
) {
SliderState(
value,
steps,
onValueChangeFinished,
valueRange

)
}

state.onValueChange = onValueChange
state.onValueChangeFinished = onValueChangeFinished
state.value = value

Slider(
Expand Down Expand Up @@ -1789,7 +1788,7 @@ class SliderState(
value: Float = 0f,
@IntRange(from = 0)
val steps: Int = 0,
val onValueChangeFinished: (() -> Unit)? = null,
onValueChangeFinished: (() -> Unit)? = null,
val valueRange: ClosedFloatingPointRange<Float> = 0f..1f
) : DraggableState {

Expand Down Expand Up @@ -1842,6 +1841,9 @@ class SliderState(
*/
internal var onValueChange: ((Float) -> Unit)? = null

var onValueChangeFinished: (() -> Unit)? = onValueChangeFinished
internal set

internal val tickFractions = stepsToTickFractions(steps)
private var totalWidth by mutableIntStateOf(0)
internal var isRtl = false
Expand All @@ -1868,7 +1870,7 @@ class SliderState(
internal val gestureEndAction = {
if (!isDragging) {
// check isDragging in case the change is still in progress (touch -> drag case)
onValueChangeFinished?.invoke()
this.onValueChangeFinished?.invoke()
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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
*
* 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.material3

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performMouseInput
import androidx.compose.ui.test.runComposeUiTest
import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue

@OptIn(ExperimentalTestApi::class)
class SliderTest {
@Test
fun changingOnValueChangeFinishedDoesNotTriggerFinish() = runComposeUiTest {
var finish1Called = false
fun onFinish1() { finish1Called = true }

var finish2Called = false
fun onFinish2() { finish2Called = true }

var useFinish2 by mutableStateOf(false)

setContent {
var value by remember { mutableStateOf(0f) }
Slider(
value = value,
onValueChange = { value = it },
onValueChangeFinished = if (useFinish2) ::onFinish2 else ::onFinish1,
modifier = Modifier.testTag("slider")
)
}

onNodeWithTag("slider").apply {
val size = fetchSemanticsNode().size
performMouseInput {
moveTo(Offset(x = 0f, y = size.height / 2f))
press()
moveTo(Offset(x = size.width / 4f, y = size.height / 2f))
useFinish2 = true
moveTo(Offset(x = size.width / 2f, y = size.height / 2f))
}
}

assertFalse(finish1Called)
assertFalse(finish2Called)

onNodeWithTag("slider").performMouseInput {
release()
}

assertFalse(finish1Called)
assertTrue(finish2Called)
}
}