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

Add PointerWheelChanged to PointerGestureRecognizer #16130

Open
beto-rodriguez opened this issue Jul 12, 2023 · 7 comments
Open

Add PointerWheelChanged to PointerGestureRecognizer #16130

beto-rodriguez opened this issue Jul 12, 2023 · 7 comments
Labels
area-gestures Gesture types proposal/open t/desktop The issue relates to desktop scenarios (MacOS/MacCatalyst/Windows/WinUI/WinAppSDK) t/enhancement ☀️ New feature or request
Milestone

Comments

@beto-rodriguez
Copy link

Description

I think it is necessary to be able to detect when the mouse wheel is fired in desktop devices. In my case I want to fire the Zoom functionality of a chart control when the mouse wheel moves, I think this is currently not possible.

Public API Changes

<ContentView x:Class="LiveChartsCore.SkiaSharpView.Maui.CartesianChart"
             xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:lvc="clr-namespace:LiveChartsCore.SkiaSharpView.Maui">
    <ContentView.Content>
        <lvc:MotionCanvas x:Name="canvas" >
            <lvc:MotionCanvas.GestureRecognizers >
                <PointerGestureRecognizer
                    PointerExited="OnPointerExited"
                    PointerMoved="OnPointerMoved"
                    PointerWheelChanged="OnWheelChanged" /> <!-- new API -->
            </lvc:MotionCanvas.GestureRecognizers>
        </lvc:MotionCanvas>
    </ContentView.Content>
</ContentView>
```


### Intended Use-Case

In my case I want to fire the Zoom functionality of a chart control when the mouse wheel moves.
@daltzctr
Copy link
Contributor

This would be amazing. Alternatively, any guidance on how to best hook this in MAUI would be appreciated as well!

@rachelkang rachelkang added the t/enhancement ☀️ New feature or request label Jul 13, 2023
@rachelkang rachelkang added this to the Backlog milestone Jul 13, 2023
@ghost
Copy link

ghost commented Jul 13, 2023

We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process.

@rachelkang rachelkang added the legacy-area-desktop Windows / WinUI / Project Reunion & Mac Catalyst / macOS specifics (Menus & other Controls)) label Jul 13, 2023
@lfmouradasilva
Copy link

+1

@RamzesMWDN
Copy link

Hi @beto-rodriguez, I think found a solution to that issue. For detection when the mouse wheel is fired in Windows desktop devices, you could use an MAUI handler for that. I attached an example source below, I hope it helps.

    public View()
    {
        InitializeComponent();
#if WINDOWS
        chart.HandlerChanged += CartesianChart_HandlerChanged;
#endif
    }    

#if WINDOWS

    private void CartesianChart_HandlerChanged(object sender, EventArgs e)
    {
        var panel = (Microsoft.Maui.Platform.ContentPanel)chart.Handler.PlatformView;
        panel.PointerWheelChanged += Panel_PointerWheelChanged;
    }

    private void Panel_PointerWheelChanged(object sender, Microsoft.UI.Xaml.Input.PointerRoutedEventArgs e)
    {
        var point = e.GetCurrentPoint(sender as Microsoft.Maui.Platform.ContentPanel);
        var delta = point.Properties.MouseWheelDelta;
        var c = (LiveChartsCore.CartesianChart<SkiaSharpDrawingContext>)chart.CoreChart;        
        var charPoint = new LiveChartsCore.Drawing.LvcPoint(point.Position.X, point.Position.Y);
        var zoomDir = delta > 0 ? ZoomDirection.ZoomIn : ZoomDirection.ZoomOut;
        c.Zoom(charPoint, zoomDir);
        e.Handled = true;
    }
#endif

XAML

            <lvc:CartesianChart
                x:Name="chart"
                Grid.Row="1"
                Series="{Binding SeriesCollection}"                                    
                ZoomMode="Both"/>

@beto-rodriguez
Copy link
Author

@RamzesMWDN Thanks! that works!

@beto-rodriguez
Copy link
Author

beto-rodriguez commented Aug 30, 2023

#16130 (comment) makes it work on Windows, now to make it work on MacOS is tricky, I am using the gestures provided on MAUI and work nice on IOS, but the problem is that they are not fired when we use a magic mouse on MacOS.

I was able to make it work using the UIPanGestureRecognizer, but I am not sure if this is the recommended way.

My code zooms in/out when:

  • We use a magic mouse and slide vertically the fingers over the mouse (like using a mouse wheel).
  • We slide vertically two fingers over the touchpad of a laptop.

I was not able to achieve that with the Gestures provided in MAUI.

#if MACCATALYST
    private void OnHandlerChanged(object? sender, EventArgs e)
    {
        var view = (Microsoft.Maui.Platform.ContentView?)canvas.Handler?.PlatformView
            ?? throw new Exception("Unable to cast to ContentView");

        view.AddGestureRecognizer(new UIPanGestureRecognizer(GetOnPan(view))
        {
            AllowedScrollTypesMask = UIScrollTypeMask.Discrete | UIScrollTypeMask.Continuous,
            MinimumNumberOfTouches = 0
        });
    }

    private CoreGraphics.CGPoint? _last;
    private Action<UIPanGestureRecognizer> GetOnPan(UIView view)
    {
        return (UIPanGestureRecognizer e) =>
        {
            var l = e.LocationInView(view);
            _last ??= l;
            var delta = _last.Value.Y - l.Y;
            var isZoom = e.NumberOfTouches == 0;

            if (e.State == UIGestureRecognizerState.Ended || !isZoom || delta == 0) return;

            var c = (CartesianChart<SkiaSharpDrawingContext>)CoreChart;
            c.Zoom(new(l.X, l.Y), delta > 0 ? ZoomDirection.ZoomIn : ZoomDirection.ZoomOut);

            _last = l;
        };
    }
#endif

Even this seems to work properly, I am not sure if this is the way to handle this scenario.

I practically forced this, I just tested and noticed that e.NumberOfTouches is 0 when I use two fingers on the touchpad or a magic mouse.

Can I rely on this code? could this change in a future?

@Eilon Eilon added t/desktop The issue relates to desktop scenarios (MacOS/MacCatalyst/Windows/WinUI/WinAppSDK) and removed legacy-area-desktop Windows / WinUI / Project Reunion & Mac Catalyst / macOS specifics (Menus & other Controls)) labels May 10, 2024
@MaxDeVos
Copy link

I would greatly appreciate this feature being implemented into a future release of MAUI. It definintely feels like its absent from the GestureRecgonizer lineup

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-gestures Gesture types proposal/open t/desktop The issue relates to desktop scenarios (MacOS/MacCatalyst/Windows/WinUI/WinAppSDK) t/enhancement ☀️ New feature or request
Projects
None yet
Development

No branches or pull requests

8 participants