Skip to content

Commit

Permalink
Disable default reaction on secondary clicks when we click on Button,…
Browse files Browse the repository at this point in the history
… move Slider, etc

This CL disable all non-left button interactions for clickable, draggable, etc.

Now we have these event API's:
- low-level awaitPointerEvent, which triggers on any event (no matter if it is left or right mouse button)
- medium-level awaitFirstDown, which triggers only on left mouse event. If we would trigger it on right click too, the user can't distinguish it if it was left or right click, because we don't provide this information in PointerInputChange. In the future we can add something like `filter: (PointerEvent) -> Unit = PrimaryButtonFilter` to awaitFirstDown, and to high-level API.
- high-level clickable, draggable, toggleable, which use awaitFirstDown under the hood
- high level mouseClickable, which provides info about which button was pressed. It doesn't use awaitFirstDown under the hood.

Fixes JetBrains/compose-multiplatform#832
  • Loading branch information
igordmn committed Feb 18, 2022
1 parent 3034de9 commit bd52c7f
Showing 1 changed file with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ import androidx.compose.ui.input.pointer.PointerEvent
import androidx.compose.ui.input.pointer.PointerEventPass
import androidx.compose.ui.input.pointer.PointerInputChange
import androidx.compose.ui.input.pointer.PointerInputScope
import androidx.compose.ui.input.pointer.PointerType
import androidx.compose.ui.input.pointer.changedToDown
import androidx.compose.ui.input.pointer.changedToDownIgnoreConsumed
import androidx.compose.ui.input.pointer.changedToUp
import androidx.compose.ui.input.pointer.consumeAllChanges
import androidx.compose.ui.input.pointer.consumeDownChange
import androidx.compose.ui.input.pointer.isOutOfBounds
import androidx.compose.ui.input.pointer.isPrimaryPressed
import androidx.compose.ui.input.pointer.positionChangeConsumed
import androidx.compose.ui.platform.ViewConfiguration
import androidx.compose.ui.unit.Density
Expand Down Expand Up @@ -246,11 +248,17 @@ internal suspend fun AwaitPointerEventScope.awaitFirstDownOnPass(
} while (
!event.changes.fastAll {
if (requireUnconsumed) it.changedToDown() else it.changedToDownIgnoreConsumed()
}
} || !event.isPrimaryButton
)
return event.changes[0]
}

private val PointerEvent.isPrimaryButton: Boolean
get() {
val isMouse = changes.all { it.type == PointerType.Mouse }
return !isMouse || buttons.isPrimaryPressed
}

/**
* Reads events until all pointers are up or the gesture was canceled. The gesture
* is considered canceled when a pointer leaves the event region, a position change
Expand Down

0 comments on commit bd52c7f

Please sign in to comment.