<commit_before>﻿using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interactivity;

namespace Org.Interactivity.Recognizer
{
    public class GestureRecognizer : TriggerBase<FrameworkElement>
    {
        private const double Threshold = 0.1;

        public Gesture TriggerOnGesture { get; set; }

        public GestureRecognizer()
        {
            TriggerOnGesture = Gesture.All;
        }

        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.ManipulationStarting += HandleManipulationStarting;
            AssociatedObject.ManipulationCompleted += HandleManipulationCompleted;
        }

        protected override void OnDetaching()
        {
            AssociatedObject.ManipulationStarting -= HandleManipulationStarting;
            AssociatedObject.ManipulationCompleted -= HandleManipulationCompleted;
            base.OnDetaching();
        }

        private void HandleManipulationStarting(object sender, ManipulationStartingEventArgs e)
        {
            e.ManipulationContainer = AssociatedObject;
            e.Mode = ManipulationModes.All;
        }

        private void HandleManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
        {
            if (IsWorthLookingAtThis(e.TotalManipulation.Translation.Length) || //swipe
                IsWorthLookingAtThis(e.FinalVelocities.LinearVelocity.Length)) //flick
            {
                InvokeActions(ToSwipeGesture(e.TotalManipulation.Translation, e.ManipulationOrigin));
            }
            else //tap
            {
                InvokeActions(Gesture.Tap);
            }
        }

        private static bool IsWorthLookingAtThis(double length)
        {
            return length > Threshold;
        }

        private static Gesture ToSwipeGesture(Vector vector, Point origin)
        {
            var isVerticalGesture = Math.Abs(vector.Y) > Math.Abs(vector.X);
            if (isVerticalGesture)
            {
                //vertical swipe
                return vector.Y > 0 ? Gesture.SwipeUp : Gesture.SwipeDown;
            }
            //horizontal
            return vector.X > 0 ? Gesture.SwipeRight : Gesture.SwipeLeft;
        }
    }

    public enum Gesture : byte
    {
        All = 0,
        SwipeUp,
        SwipeDown,
        SwipeLeft,
        SwipeRight,
        Tap
    }
}<commit_msg>Tweaking ToSwipeGesture based on collected data<commit_after>﻿using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interactivity;

namespace Org.Interactivity.Recognizer
{
    public class GestureRecognizer : TriggerBase<FrameworkElement>
    {
        private const int TapThreshold = 40;

        public Gesture TriggerOnGesture { get; set; }

        public GestureRecognizer()
        {
            TriggerOnGesture = Gesture.All;
        }

        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.ManipulationStarting += HandleManipulationStarting;
            AssociatedObject.ManipulationCompleted += HandleManipulationCompleted;
        }

        protected override void OnDetaching()
        {
            AssociatedObject.ManipulationStarting -= HandleManipulationStarting;
            AssociatedObject.ManipulationCompleted -= HandleManipulationCompleted;
            base.OnDetaching();
        }

        private void HandleManipulationStarting(object sender, ManipulationStartingEventArgs e)
        {
            e.ManipulationContainer = AssociatedObject;
            e.Mode = ManipulationModes.All;
        }

        private void HandleManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
        {
                InvokeActions(ToSwipeGesture(e.TotalManipulation.Translation));
        }

        private static Gesture ToSwipeGesture(Vector translation)
        {
            var deltaX = translation.X;
            var deltaY = translation.Y;
            var distX = Math.Abs(deltaX);
            var distY = Math.Abs(deltaY);
            if (deltaX <= TapThreshold && deltaY <= TapThreshold)
            {
                return Gesture.Tap;
            }
            else if (distY >= distX) // bias towards vertical swipe over horizontal if distances are equal
            {
                return deltaY > 0 ? Gesture.SwipeDown : Gesture.SwipeUp;
            }
            else
            {
                return deltaX > 0 ? Gesture.SwipeRight : Gesture.SwipeLeft;
            }
        }
    }

    public enum Gesture : byte
    {
        All = 0,
        SwipeUp,
        SwipeDown,
        SwipeLeft,
        SwipeRight,
        Tap
    }
}
{'fraction_non_alphanumeric': 0.06988105352591334, 'fraction_numerical': 0.0021240441801189465, 'mean_word_length': 1.932129514321295, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4436732', 'n_tokens_mistral': 1276, 'n_tokens_neox': 1195, 'n_words': 245}