Is there a way to dynamically disable the swipe to...
# compose-wear
a
Is there a way to dynamically disable the swipe to dismiss gesture? My use case is that I am using the HsvColorPicker composable from https://github.com/skydoves/colorpicker-compose and it works great, except when I start dragging to the right 😕
j
@Steve Bower [G] @Michail Kulaga If we don't have a way already this seems like something we should add
s
Hi - I've used this code snippet in the past to disable swipe-to-dismiss. Given that swipe-to-dismiss is the recommended default action for screens and dialogs, it was decided at the time not to include it in the library:
Copy code
/**
 * Override a composable to be unswipeable by blocking the
 * swipe-to-dismiss gesture.
 *
 * When a composable is on top of another composable that
 * supports swipe-to-dismiss, then [Modifier.unswipeable] 
 * can be applied to the top composable to handle and ignore
 * the swipe gesture. For example, this may be used to 
 * prevent swiping away a dialog.
 */
public fun Modifier.unswipeable() =
  this.then(
    Modifier.draggable(
      orientation = Orientation.Horizontal,
      enabled = true,
      state = DraggableState {}
    )
  )
a
Thanks @Steve Bower [G]. If I add this to the HsvColorPicker then I can’t select colors horizontally. I’m also not able to add the HsvColorPicker to
Alert
or
Confirmation
and
Dialog
doesn’t have a modifier property 😕 This also didn’t work. Any other suggestions?
Copy code
SwipeToDismissBox(
    modifier = Modifier.unswipeable(),
    state = state,
) {
    HsvColorPicker(
        modifier = Modifier.fillMaxSize(),
        controller = controller,
        onColorChanged = {
        }
    )
}
s
Hi - have you tried setting hasBackground = false on SwipeToDismissBox? That's used by the SwipeDismissableNavHost to disable swipe-to-dismiss at the top of the navigation hierarchy, when there's no background to display. It actually disables the swipe gesture, which is probably what you want. https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:wear/com[…]lin/androidx/wear/compose/material/SwipeToDismissBox.kt;l=259
a
Thanks for all the help! I was able to get a better understanding of SwipeToDismissBox, but unfortunately I was using a library for the color picker and couldn’t get it to work. Fortunately, I found another implementation that worked perfectly! Thanks again https://github.com/godaddy/compose-color-picker