From 4b277194989c02b9fd44569ca2acb0ed65907e36 Mon Sep 17 00:00:00 2001 From: victor Date: Sat, 19 Oct 2024 14:00:45 -0700 Subject: [PATCH] attached behavior for scroll viewers to throttle touch pad scrolling --- .../Converters/ScrollThrottleBehavior.cs | 43 +++++++++++++++++++ FRBDK/Glue/Glue/Themes/Frb.Styles.xaml | 4 +- 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 FRBDK/Glue/Glue/Themes/Converters/ScrollThrottleBehavior.cs diff --git a/FRBDK/Glue/Glue/Themes/Converters/ScrollThrottleBehavior.cs b/FRBDK/Glue/Glue/Themes/Converters/ScrollThrottleBehavior.cs new file mode 100644 index 000000000..217025245 --- /dev/null +++ b/FRBDK/Glue/Glue/Themes/Converters/ScrollThrottleBehavior.cs @@ -0,0 +1,43 @@ +using System.Windows; +using System.Windows.Controls; +using System.Windows.Input; + +namespace FlatRedBall.Glue.Themes.Converters; + +public static class ScrollThrottleBehavior +{ + public static readonly DependencyProperty IsEnabledProperty = + DependencyProperty.RegisterAttached( + "IsEnabled", + typeof(bool), + typeof(ScrollThrottleBehavior), + new PropertyMetadata(false, OnIsEnabledChanged)); + + public static bool GetIsEnabled(ScrollViewer obj) => (bool)obj.GetValue(IsEnabledProperty); + public static void SetIsEnabled(ScrollViewer obj, bool value) => obj.SetValue(IsEnabledProperty, value); + + private static void OnIsEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + if (d is ScrollViewer scrollViewer + ) + { + if ((bool)e.NewValue) + { + scrollViewer.PreviewMouseWheel += OnMouseWheel; + } + else + { + scrollViewer.PreviewMouseWheel -= OnMouseWheel; + } + } + } + + private static void OnMouseWheel(object sender, MouseWheelEventArgs e) + { + if (sender is ScrollViewer scrollViewer && System.Math.Abs(e.Delta) != 120) + { + scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset + ((double)e.Delta /120)); + e.Handled = true; + } + } +} diff --git a/FRBDK/Glue/Glue/Themes/Frb.Styles.xaml b/FRBDK/Glue/Glue/Themes/Frb.Styles.xaml index b50ee3e8a..1b7d3efdf 100644 --- a/FRBDK/Glue/Glue/Themes/Frb.Styles.xaml +++ b/FRBDK/Glue/Glue/Themes/Frb.Styles.xaml @@ -39,7 +39,9 @@ -