Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[Spec] Add IsEnabled to GestureRecognizer #8756

Closed
dhindrik opened this issue Dec 5, 2019 · 6 comments
Closed

[Spec] Add IsEnabled to GestureRecognizer #8756

dhindrik opened this issue Dec 5, 2019 · 6 comments

Comments

@dhindrik
Copy link
Contributor

dhindrik commented Dec 5, 2019

Add IsEnabled to GestureRecognizer

To make it possible to disable a gesture recognizer I want to add an IsEnabled property to GestureRecognizers so event and Commands not are executed if it is false.

I found this issue about it, #8091. But there are other use cases as well. There could be cases where you want to bind to other UI-elements or if you don't use MVVM (there are people that don't use it). And it will be less code if you can handle it all in XAML.

API

GestureRecognizer.cs

public static BindableProperty IsEnabledProperty = BindableProperty.Create(nameof(IsEnabled), typeof(bool), typeof(GestureRecognizer), true);

public bool IsEnabled
{
     get => (bool)GetValue(IsEnabledProperty);
     set => SetValue(IsEnabledProperty, value);
}

Properties

API Description
IsEnabled Sets if the gesture recognizer should trigger events and commands

Scenarios

C# Example

var recognizer = new TapGestureRecognizer();
recognizer.Tapped += Grid_Tapped;
recognizer.SetBinding (TapGestureRecognizer.IsEnabledProperty, "CanTap");

var grid = new Grid();
grid.GestureRecognizers.Add(recognizer);

XAML Example

<Grid>
    <Grid.GestureRecognizers>
        <TapGestureRecognizer IsEnabled="{Binding CanTap}" Command="{Binding Select"} CommandParameter="{Binding } />
</Grid>

Backward Compatibility

Because it is true by default it is backward compatible

Difficulty : low

@dhindrik
Copy link
Contributor Author

dhindrik commented Dec 5, 2019

I have created an implementation, I can create a PR for it if you approve this suggestion.
dhindrik@69cf57c

@dhindrik
Copy link
Contributor Author

dhindrik commented Dec 5, 2019

A little bit more about why this should be implemented:

  • If we use IsEnabled on the element, the Grid in my example above, it will work if we will want to disable all gesture recognizers, but if we have multiple it will not work if we want to disable one of them for example.

  • If we are using the same command or event handler for multiple elements it can be useful to handle it here instead of in the ViewModel.

  • It is an easy way to disable gesture recognizers, for me it was my first thought when I wanted to a recognizer it for specific rows in a CollectionView.

@samhouts samhouts removed this from New in Triage Dec 5, 2019
@PureWeen
Copy link
Contributor

PureWeen commented Dec 5, 2019

@dhindrik some thoughts on this one

  1. I still feel like CanExecute on the Command is what this should get tied to. Also having IsEnabled and CanExecute can lead to confusing conversations The order of 'IsEnabled' and 'Command' should not matter #2758

  2. we have some specs internally where we are working through gesture orchestration which is a bit more how I would expect a property like this to work. On your PR you're just checking IsEnabled but the gesture recognizer still exists at the platform level. I would expect something like IsEnabled to detach the gesture at the platform level so you'd be using this completely disable it not just ignore the fact that it's firing

thoughts?

@PureWeen PureWeen added this to Discussion Required in Enhancements Dec 5, 2019
@dhindrik
Copy link
Contributor Author

  1. So you mean instead of the if-statement it should use CanExecute instead? What about naming the property to, CommandCanExecute?

  2. That should probarly be even better. My initial thought was to just make the Command or event to not execute, like if you have set CanExecute to false, but I wanted to do that from the XAML.

@jonx
Copy link

jonx commented Jan 30, 2020

I also prefer a solution closer to what @PureWeen is suggesting. I never need to explicitly disable a gesture but getting it disabled through the command that it is tied to would be a must. Just look at how a simple button is doing. That seems to be the way to go for me.

@samhouts samhouts moved this from Discussion Required to Under consideration in Enhancements Feb 12, 2020
@jfversluis
Copy link
Member

This will not happen anymore for Xamarin.Forms. If this is still important to you, make sure to check the .NET MAUI repo and see if it's already on the roadmap or open an issue with a detailed feature request. Thanks!

Enhancements automation moved this from Under consideration to Closed Nov 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Enhancements
  
Closed
Development

No branches or pull requests

5 participants